xref: /rk3399_rockchip-uboot/common/spl/spl_atf.c (revision 01b8c4d110abb0dcbe36dc5b6b10d93b2b8e2667)
1 /*
2  * Reference to the ARM TF Project,
3  * plat/arm/common/arm_bl2_setup.c
4  * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights
5  * reserved.
6  * Copyright (C) 2016 Rockchip Electronic Co.,Ltd
7  * Written by Kever Yang <kever.yang@rock-chips.com>
8  * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
9  *
10  * SPDX-License-Identifier:     BSD-3-Clause
11  */
12 
13 #include <common.h>
14 #include <atf_common.h>
15 #include <errno.h>
16 #include <spl.h>
17 
18 static struct bl2_to_bl31_params_mem bl31_params_mem;
19 static struct bl31_params *bl2_to_bl31_params;
20 
21 /**
22  * bl2_plat_get_bl31_params() - prepare params for bl31.
23  *
24  * This function assigns a pointer to the memory that the platform has kept
25  * aside to pass platform specific and trusted firmware related information
26  * to BL31. This memory is allocated by allocating memory to
27  * bl2_to_bl31_params_mem structure which is a superset of all the
28  * structure whose information is passed to BL31
29  * NOTE: This function should be called only once and should be done
30  * before generating params to BL31
31  *
32  * @return bl31 params structure pointer
33  */
34 static struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
35 						    uintptr_t bl33_entry)
36 {
37 	struct entry_point_info *bl32_ep_info;
38 	struct entry_point_info *bl33_ep_info;
39 
40 	/*
41 	 * Initialise the memory for all the arguments that needs to
42 	 * be passed to BL31
43 	 */
44 	memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
45 
46 	/* Assign memory for TF related information */
47 	bl2_to_bl31_params = &bl31_params_mem.bl31_params;
48 	SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
49 
50 	/* Fill BL31 related information */
51 	SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
52 		       ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
53 
54 	if (bl32_entry == -1)
55 		goto bl33_setup;
56 
57 	/* Fill BL32 related information */
58 	bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
59 	bl32_ep_info = &bl31_params_mem.bl32_ep_info;
60 	SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
61 		       ATF_EP_SECURE);
62 
63 	bl32_ep_info->pc = bl32_entry;
64 	bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
65 				     DISABLE_ALL_EXECPTIONS);
66 
67 	bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
68 	SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
69 		       ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
70 
71 bl33_setup:
72 	/* Fill BL33 related information */
73 	bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
74 	bl33_ep_info = &bl31_params_mem.bl33_ep_info;
75 	SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
76 		       ATF_EP_NON_SECURE);
77 
78 	/* BL33 expects to receive the primary CPU MPID (through x0) */
79 	bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
80 	bl33_ep_info->pc = bl33_entry;
81 	bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
82 				     DISABLE_ALL_EXECPTIONS);
83 #if defined(CONFIG_SPL_KERNEL_BOOT) && defined(CONFIG_ARM64)
84 	/*
85 	 * Reference: arch/arm/lib/bootm.c
86 	 * boot_jump_linux(bootm_headers_t *images, int flag)
87 	 * {
88 	 * 	......
89 	 * 	armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
90 	 * 			   images->ep, ES_TO_AARCH64);
91 	 * }
92 	 */
93 	bl33_ep_info->args.arg0 = CONFIG_SPL_FDT_ADDR;
94 #endif
95 	bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
96 	SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
97 		       ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
98 
99 	return bl2_to_bl31_params;
100 }
101 
102 static inline void raw_write_daif(unsigned int daif)
103 {
104 	__asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
105 }
106 
107 typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
108 
109 void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
110 		uintptr_t bl33_entry, uintptr_t fdt_addr)
111 {
112 	struct bl31_params *bl31_params;
113 	atf_entry_t  atf_entry = (atf_entry_t)bl31_entry;
114 
115 	bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry);
116 
117 	atf_entry((void *)bl31_params, (void *)fdt_addr);
118 }
119 
120 static int spl_fit_images_find(void *blob, int os)
121 {
122 	int parent, node, ndepth;
123 	const void *data;
124 
125 	if (!blob)
126 		return -FDT_ERR_BADMAGIC;
127 
128 	parent = fdt_path_offset(blob, "/fit-images");
129 	if (parent < 0)
130 		return -FDT_ERR_NOTFOUND;
131 
132 	for (node = fdt_next_node(blob, parent, &ndepth);
133 	     (node >= 0) && (ndepth > 0);
134 	     node = fdt_next_node(blob, node, &ndepth)) {
135 		if (ndepth != 1)
136 			continue;
137 
138 		data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
139 		if (!data)
140 			continue;
141 
142 		if (genimg_get_os_id(data) == os)
143 			return node;
144 	};
145 
146 	return -FDT_ERR_NOTFOUND;
147 }
148 
149 uintptr_t spl_fit_images_get_entry(void *blob, int node)
150 {
151 	ulong  val;
152 
153 	val = fdt_getprop_u32(blob, node, "entry-point");
154 	if (val == FDT_ERROR)
155 		val = fdt_getprop_u32(blob, node, "load-addr");
156 
157 	debug("%s: entry point 0x%lx\n", __func__, val);
158 	return val;
159 }
160 
161 void spl_invoke_atf(struct spl_image_info *spl_image)
162 {
163 	uintptr_t bl32_entry, bl33_entry;
164 	void *blob = spl_image->fdt_addr;
165 	uintptr_t platform_param = (uintptr_t)blob;
166 	int node;
167 
168 	/*
169 	 * Find the OP-TEE binary (in /fit-images) load address or
170 	 * entry point (if different) and pass it as the BL3-2 entry
171 	 * point, this is optional.
172 	 * This will need to be extended to support Falcon mode.
173 	 */
174 	node = spl_fit_images_find(blob, IH_OS_OP_TEE);
175 	if (node >= 0)
176 		bl32_entry = spl_fit_images_get_entry(blob, node);
177 	else
178 		bl32_entry = spl_image->entry_point_bl32; /* optional */
179 
180 	/*
181 	 * Find the U-Boot binary (in /fit-images) load addreess or
182 	 * entry point (if different) and pass it as the BL3-3 entry
183 	 * point.
184 	 * This will need to be extended to support Falcon mode.
185 	 */
186 	node = spl_fit_images_find(blob, IH_OS_U_BOOT);
187 	if (node >= 0)
188 		bl33_entry = spl_fit_images_get_entry(blob, node);
189 	else
190 		bl33_entry = spl_image->entry_point_bl33;
191 
192 	/*
193 	 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
194 	 * parameter and always pass 0.  This is a workaround for
195 	 * older ATF versions that have insufficiently robust (or
196 	 * overzealous) argument validation.
197 	 */
198 	if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
199 		platform_param = 0;
200 
201 	/* do cleanup */
202 	spl_cleanup_before_jump(spl_image);
203 
204 	/*
205 	 * We don't provide a BL3-2 entry yet, but this will be possible
206 	 * using similar logic.
207 	 */
208 	bl31_entry(spl_image->entry_point, bl32_entry,
209 		   bl33_entry, platform_param);
210 }
211