blob: e1b68dd561677ba0bd810ec044f3bf602d46be41 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Kever Yang6e79a912017-05-05 11:47:45 +08002/*
3 * Reference to the ARM TF Project,
4 * plat/arm/common/arm_bl2_setup.c
5 * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights
6 * reserved.
7 * Copyright (C) 2016 Rockchip Electronic Co.,Ltd
8 * Written by Kever Yang <kever.yang@rock-chips.com>
Philipp Tomsich4ab63e02017-09-13 21:29:35 +02009 * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
Kever Yang6e79a912017-05-05 11:47:45 +080010 */
11
12#include <common.h>
13#include <atf_common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070014#include <cpu_func.h>
Kever Yang6e79a912017-05-05 11:47:45 +080015#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060016#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Kever Yang6e79a912017-05-05 11:47:45 +080018#include <spl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Kever Yang6e79a912017-05-05 11:47:45 +080020
Michael Walle0e4d90c2020-11-18 17:45:57 +010021/* Holds all the structures we need for bl31 parameter passing */
22struct bl2_to_bl31_params_mem {
23 struct bl31_params bl31_params;
24 struct atf_image_info bl31_image_info;
25 struct atf_image_info bl32_image_info;
26 struct atf_image_info bl33_image_info;
27 struct entry_point_info bl33_ep_info;
28 struct entry_point_info bl32_ep_info;
29 struct entry_point_info bl31_ep_info;
30};
31
Michael Walleafaf6ea2020-11-18 17:45:58 +010032struct bl2_to_bl31_params_mem_v2 {
33 struct bl_params bl_params;
34 struct bl_params_node bl31_params_node;
35 struct bl_params_node bl32_params_node;
36 struct bl_params_node bl33_params_node;
37 struct atf_image_info bl31_image_info;
38 struct atf_image_info bl32_image_info;
39 struct atf_image_info bl33_image_info;
40 struct entry_point_info bl33_ep_info;
41 struct entry_point_info bl32_ep_info;
42 struct entry_point_info bl31_ep_info;
43};
44
Michael Walle2def7d92020-11-18 17:45:56 +010045struct bl31_params *bl2_plat_get_bl31_params_default(uintptr_t bl32_entry,
46 uintptr_t bl33_entry,
47 uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080048{
Michael Walle203fede2020-11-18 17:45:55 +010049 static struct bl2_to_bl31_params_mem bl31_params_mem;
50 struct bl31_params *bl2_to_bl31_params;
Joseph Chen8dc7abe2019-10-06 20:10:22 +020051 struct entry_point_info *bl32_ep_info;
Kever Yang6e79a912017-05-05 11:47:45 +080052 struct entry_point_info *bl33_ep_info;
53
54 /*
55 * Initialise the memory for all the arguments that needs to
56 * be passed to BL31
57 */
58 memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
59
60 /* Assign memory for TF related information */
61 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
62 SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
63
64 /* Fill BL31 related information */
Frieder Schrempfb76d8c22019-06-27 07:03:16 +000065 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
Kever Yang6e79a912017-05-05 11:47:45 +080066 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
67 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
68
Joseph Chen8dc7abe2019-10-06 20:10:22 +020069 /* Fill BL32 related information */
Kever Yang6e79a912017-05-05 11:47:45 +080070 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
Joseph Chen8dc7abe2019-10-06 20:10:22 +020071 bl32_ep_info = &bl31_params_mem.bl32_ep_info;
72 SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
73 ATF_EP_SECURE);
74
75 /* secure payload is optional, so set pc to 0 if absent */
76 bl32_ep_info->args.arg3 = fdt_addr;
77 bl32_ep_info->pc = bl32_entry ? bl32_entry : 0;
78 bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
79 DISABLE_ALL_EXECPTIONS);
80
Kever Yang6e79a912017-05-05 11:47:45 +080081 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
82 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
83 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
Kever Yang6e79a912017-05-05 11:47:45 +080084
85 /* Fill BL33 related information */
86 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
87 bl33_ep_info = &bl31_params_mem.bl33_ep_info;
88 SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
89 ATF_EP_NON_SECURE);
90
91 /* BL33 expects to receive the primary CPU MPID (through x0) */
92 bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020093 bl33_ep_info->pc = bl33_entry;
Kever Yang6e79a912017-05-05 11:47:45 +080094 bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
95 DISABLE_ALL_EXECPTIONS);
96
97 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
98 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
99 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
100
101 return bl2_to_bl31_params;
102}
103
Michael Walle2def7d92020-11-18 17:45:56 +0100104__weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
105 uintptr_t bl33_entry,
106 uintptr_t fdt_addr)
107{
108 return bl2_plat_get_bl31_params_default(bl32_entry, bl33_entry,
109 fdt_addr);
110}
111
Michael Walleafaf6ea2020-11-18 17:45:58 +0100112struct bl_params *bl2_plat_get_bl31_params_v2_default(uintptr_t bl32_entry,
113 uintptr_t bl33_entry,
114 uintptr_t fdt_addr)
115{
116 static struct bl2_to_bl31_params_mem_v2 bl31_params_mem;
117 struct bl_params *bl_params;
118 struct bl_params_node *bl_params_node;
119
120 /*
121 * Initialise the memory for all the arguments that needs to
122 * be passed to BL31
123 */
124 memset(&bl31_params_mem, 0, sizeof(bl31_params_mem));
125
126 /* Assign memory for TF related information */
127 bl_params = &bl31_params_mem.bl_params;
128 SET_PARAM_HEAD(bl_params, ATF_PARAM_BL_PARAMS, ATF_VERSION_2, 0);
129 bl_params->head = &bl31_params_mem.bl31_params_node;
130
131 /* Fill BL31 related information */
132 bl_params_node = &bl31_params_mem.bl31_params_node;
133 bl_params_node->image_id = ATF_BL31_IMAGE_ID;
134 bl_params_node->image_info = &bl31_params_mem.bl31_image_info;
135 bl_params_node->ep_info = &bl31_params_mem.bl31_ep_info;
136 bl_params_node->next_params_info = &bl31_params_mem.bl32_params_node;
137 SET_PARAM_HEAD(bl_params_node->image_info, ATF_PARAM_IMAGE_BINARY,
138 ATF_VERSION_2, 0);
139
140 /* Fill BL32 related information */
141 bl_params_node = &bl31_params_mem.bl32_params_node;
142 bl_params_node->image_id = ATF_BL32_IMAGE_ID;
143 bl_params_node->image_info = &bl31_params_mem.bl32_image_info;
144 bl_params_node->ep_info = &bl31_params_mem.bl32_ep_info;
145 bl_params_node->next_params_info = &bl31_params_mem.bl33_params_node;
146 SET_PARAM_HEAD(bl_params_node->ep_info, ATF_PARAM_EP,
147 ATF_VERSION_2, ATF_EP_SECURE);
148
149 /* secure payload is optional, so set pc to 0 if absent */
150 bl_params_node->ep_info->args.arg3 = fdt_addr;
151 bl_params_node->ep_info->pc = bl32_entry ? bl32_entry : 0;
152 bl_params_node->ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
153 DISABLE_ALL_EXECPTIONS);
154 SET_PARAM_HEAD(bl_params_node->image_info, ATF_PARAM_IMAGE_BINARY,
155 ATF_VERSION_2, 0);
156
157 /* Fill BL33 related information */
158 bl_params_node = &bl31_params_mem.bl33_params_node;
159 bl_params_node->image_id = ATF_BL33_IMAGE_ID;
160 bl_params_node->image_info = &bl31_params_mem.bl33_image_info;
161 bl_params_node->ep_info = &bl31_params_mem.bl33_ep_info;
162 bl_params_node->next_params_info = NULL;
163 SET_PARAM_HEAD(bl_params_node->ep_info, ATF_PARAM_EP,
164 ATF_VERSION_2, ATF_EP_NON_SECURE);
165
166 /* BL33 expects to receive the primary CPU MPID (through x0) */
167 bl_params_node->ep_info->args.arg0 = 0xffff & read_mpidr();
168 bl_params_node->ep_info->pc = bl33_entry;
169 bl_params_node->ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
170 DISABLE_ALL_EXECPTIONS);
171 SET_PARAM_HEAD(bl_params_node->image_info, ATF_PARAM_IMAGE_BINARY,
172 ATF_VERSION_2, 0);
173
174 return bl_params;
175}
176
177__weak struct bl_params *bl2_plat_get_bl31_params_v2(uintptr_t bl32_entry,
178 uintptr_t bl33_entry,
179 uintptr_t fdt_addr)
180{
181 return bl2_plat_get_bl31_params_v2_default(bl32_entry, bl33_entry,
182 fdt_addr);
183}
184
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200185static inline void raw_write_daif(unsigned int daif)
Kever Yang6e79a912017-05-05 11:47:45 +0800186{
187 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
188}
189
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200190typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
191
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200192static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
193 uintptr_t bl33_entry, uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +0800194{
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200195 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Michael Walleafaf6ea2020-11-18 17:45:58 +0100196 void *bl31_params;
Kever Yang6e79a912017-05-05 11:47:45 +0800197
Michael Walleafaf6ea2020-11-18 17:45:58 +0100198 if (CONFIG_IS_ENABLED(ATF_LOAD_IMAGE_V2))
199 bl31_params = bl2_plat_get_bl31_params_v2(bl32_entry,
200 bl33_entry,
201 fdt_addr);
202 else
203 bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
204 fdt_addr);
Kever Yang6e79a912017-05-05 11:47:45 +0800205
206 raw_write_daif(SPSR_EXCEPTION_MASK);
207 dcache_disable();
208
Michael Walleafaf6ea2020-11-18 17:45:58 +0100209 atf_entry(bl31_params, (void *)fdt_addr);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200210}
211
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200212static int spl_fit_images_find(void *blob, int os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200213{
Michal Simekdb878c02019-12-19 15:42:13 +0100214 int parent, node, ndepth = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200215 const void *data;
216
217 if (!blob)
218 return -FDT_ERR_BADMAGIC;
219
220 parent = fdt_path_offset(blob, "/fit-images");
221 if (parent < 0)
222 return -FDT_ERR_NOTFOUND;
223
224 for (node = fdt_next_node(blob, parent, &ndepth);
225 (node >= 0) && (ndepth > 0);
226 node = fdt_next_node(blob, node, &ndepth)) {
227 if (ndepth != 1)
228 continue;
229
230 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
231 if (!data)
232 continue;
233
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200234 if (genimg_get_os_id(data) == os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200235 return node;
236 };
237
238 return -FDT_ERR_NOTFOUND;
239}
240
241uintptr_t spl_fit_images_get_entry(void *blob, int node)
242{
243 ulong val;
Michal Simek9e64a552020-09-03 11:24:28 +0200244 int ret;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200245
Michal Simek9e64a552020-09-03 11:24:28 +0200246 ret = fit_image_get_entry(blob, node, &val);
247 if (ret)
248 ret = fit_image_get_load(blob, node, &val);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200249
250 debug("%s: entry point 0x%lx\n", __func__, val);
251 return val;
252}
253
254void spl_invoke_atf(struct spl_image_info *spl_image)
255{
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200256 uintptr_t bl32_entry = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200257 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
258 void *blob = spl_image->fdt_addr;
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100259 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200260 int node;
261
262 /*
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200263 * Find the OP-TEE binary (in /fit-images) load address or
264 * entry point (if different) and pass it as the BL3-2 entry
265 * point, this is optional.
266 */
267 node = spl_fit_images_find(blob, IH_OS_TEE);
268 if (node >= 0)
269 bl32_entry = spl_fit_images_get_entry(blob, node);
270
271 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200272 * Find the U-Boot binary (in /fit-images) load addreess or
273 * entry point (if different) and pass it as the BL3-3 entry
274 * point.
275 * This will need to be extended to support Falcon mode.
276 */
277
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200278 node = spl_fit_images_find(blob, IH_OS_U_BOOT);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200279 if (node >= 0)
280 bl33_entry = spl_fit_images_get_entry(blob, node);
281
282 /*
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100283 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
284 * parameter and always pass 0. This is a workaround for
285 * older ATF versions that have insufficiently robust (or
286 * overzealous) argument validation.
287 */
288 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
289 platform_param = 0;
290
291 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200292 * We don't provide a BL3-2 entry yet, but this will be possible
293 * using similar logic.
294 */
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200295 bl31_entry(spl_image->entry_point, bl32_entry,
296 bl33_entry, platform_param);
Kever Yang6e79a912017-05-05 11:47:45 +0800297}