blob: 987c6028c43d40c6b70ebd3d4b28b5b34c59e4bb [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
Douglas Raillarda8954fc2017-01-26 15:54:44 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Jens Wiklander52c798e2015-12-07 14:37:10 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Jens Wiklander52c798e2015-12-07 14:37:10 +01005 */
6#include <arch_helpers.h>
Fu Weic2f78442017-05-27 21:21:42 +08007#include <assert.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +01008#include <bl_common.h>
9#include <console.h>
10#include <debug.h>
Fu Weic2f78442017-05-27 21:21:42 +080011#include <desc_image_load.h>
Jens Wiklander0acbaaa2017-08-24 13:16:26 +020012#include <optee_utils.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010013#include <libfdt.h>
Etienne Carriere911de8c2018-02-02 13:23:22 +010014#include <platform.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010015#include <platform_def.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010016#include <string.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000017#include <utils.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010018#include "qemu_private.h"
Jens Wiklander52c798e2015-12-07 14:37:10 +010019
Jens Wiklander52c798e2015-12-07 14:37:10 +010020
Fu Weic2f78442017-05-27 21:21:42 +080021/* Data structure which holds the extents of the trusted SRAM for BL2 */
22static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
23
24#if !LOAD_IMAGE_V2
Jens Wiklander52c798e2015-12-07 14:37:10 +010025/*******************************************************************************
26 * This structure represents the superset of information that is passed to
27 * BL3-1, e.g. while passing control to it from BL2, bl31_params
28 * and other platform specific params
29 ******************************************************************************/
30typedef struct bl2_to_bl31_params_mem {
31 bl31_params_t bl31_params;
32 image_info_t bl31_image_info;
33 image_info_t bl32_image_info;
34 image_info_t bl33_image_info;
35 entry_point_info_t bl33_ep_info;
36 entry_point_info_t bl32_ep_info;
37 entry_point_info_t bl31_ep_info;
38} bl2_to_bl31_params_mem_t;
39
40
41static bl2_to_bl31_params_mem_t bl31_params_mem;
42
Jens Wiklander52c798e2015-12-07 14:37:10 +010043
44meminfo_t *bl2_plat_sec_mem_layout(void)
45{
46 return &bl2_tzram_layout;
47}
48
49/*******************************************************************************
50 * This function assigns a pointer to the memory that the platform has kept
51 * aside to pass platform specific and trusted firmware related information
52 * to BL31. This memory is allocated by allocating memory to
53 * bl2_to_bl31_params_mem_t structure which is a superset of all the
54 * structure whose information is passed to BL31
55 * NOTE: This function should be called only once and should be done
56 * before generating params to BL31
57 ******************************************************************************/
58bl31_params_t *bl2_plat_get_bl31_params(void)
59{
60 bl31_params_t *bl2_to_bl31_params;
61
62 /*
63 * Initialise the memory for all the arguments that needs to
64 * be passed to BL3-1
65 */
Douglas Raillarda8954fc2017-01-26 15:54:44 +000066 zeromem(&bl31_params_mem, sizeof(bl2_to_bl31_params_mem_t));
Jens Wiklander52c798e2015-12-07 14:37:10 +010067
68 /* Assign memory for TF related information */
69 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
70 SET_PARAM_HEAD(bl2_to_bl31_params, PARAM_BL31, VERSION_1, 0);
71
72 /* Fill BL3-1 related information */
73 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
74 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, PARAM_IMAGE_BINARY,
75 VERSION_1, 0);
76
77 /* Fill BL3-2 related information */
78 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
79 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, PARAM_EP,
80 VERSION_1, 0);
81 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
82 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, PARAM_IMAGE_BINARY,
83 VERSION_1, 0);
84
85 /* Fill BL3-3 related information */
86 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
87 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_ep_info,
88 PARAM_EP, VERSION_1, 0);
89
90 /* BL3-3 expects to receive the primary CPU MPID (through x0) */
91 bl2_to_bl31_params->bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
92
93 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
94 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, PARAM_IMAGE_BINARY,
95 VERSION_1, 0);
96
97 return bl2_to_bl31_params;
98}
99
100/* Flush the TF params and the TF plat params */
101void bl2_plat_flush_bl31_params(void)
102{
103 flush_dcache_range((unsigned long)&bl31_params_mem,
104 sizeof(bl2_to_bl31_params_mem_t));
105}
106
107/*******************************************************************************
108 * This function returns a pointer to the shared memory that the platform
109 * has kept to point to entry point information of BL31 to BL2
110 ******************************************************************************/
111struct entry_point_info *bl2_plat_get_bl31_ep_info(void)
112{
113#if DEBUG
114 bl31_params_mem.bl31_ep_info.args.arg1 = QEMU_BL31_PLAT_PARAM_VAL;
115#endif
116
117 return &bl31_params_mem.bl31_ep_info;
118}
Fu Weic2f78442017-05-27 21:21:42 +0800119#endif /* !LOAD_IMAGE_V2 */
Jens Wiklander52c798e2015-12-07 14:37:10 +0100120
121
122
123void bl2_early_platform_setup(meminfo_t *mem_layout)
124{
125 /* Initialize the console to provide early debug support */
126 console_init(PLAT_QEMU_BOOT_UART_BASE, PLAT_QEMU_BOOT_UART_CLK_IN_HZ,
127 PLAT_QEMU_CONSOLE_BAUDRATE);
128
129 /* Setup the BL2 memory layout */
130 bl2_tzram_layout = *mem_layout;
131
132 plat_qemu_io_setup();
133}
134
135static void security_setup(void)
136{
137 /*
138 * This is where a TrustZone address space controller and other
139 * security related peripherals, would be configured.
140 */
141}
142
143static void update_dt(void)
144{
145 int ret;
146 void *fdt = (void *)(uintptr_t)PLAT_QEMU_DT_BASE;
147
148 ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
149 if (ret < 0) {
150 ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
151 return;
152 }
153
154 if (dt_add_psci_node(fdt)) {
155 ERROR("Failed to add PSCI Device Tree node\n");
156 return;
157 }
158
159 if (dt_add_psci_cpu_enable_methods(fdt)) {
160 ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
161 return;
162 }
163
164 ret = fdt_pack(fdt);
165 if (ret < 0)
166 ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret);
167}
168
169void bl2_platform_setup(void)
170{
171 security_setup();
172 update_dt();
173
174 /* TODO Initialize timer */
175}
176
Etienne Carriere911de8c2018-02-02 13:23:22 +0100177#ifdef AARCH32
178#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_secure(__VA_ARGS__)
179#else
180#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_el1(__VA_ARGS__)
181#endif
182
Jens Wiklander52c798e2015-12-07 14:37:10 +0100183void bl2_plat_arch_setup(void)
184{
Etienne Carriere911de8c2018-02-02 13:23:22 +0100185 QEMU_CONFIGURE_BL2_MMU(bl2_tzram_layout.total_base,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100186 bl2_tzram_layout.total_size,
Michalis Pappasba861122018-02-28 14:36:03 +0800187 BL_CODE_BASE, BL_CODE_END,
188 BL_RO_DATA_BASE, BL_RO_DATA_END,
Masahiro Yamada0fac5af2016-12-28 16:11:41 +0900189 BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END);
Jens Wiklander52c798e2015-12-07 14:37:10 +0100190}
191
192/*******************************************************************************
193 * Gets SPSR for BL32 entry
194 ******************************************************************************/
195static uint32_t qemu_get_spsr_for_bl32_entry(void)
196{
Etienne Carriere911de8c2018-02-02 13:23:22 +0100197#ifdef AARCH64
Jens Wiklander52c798e2015-12-07 14:37:10 +0100198 /*
199 * The Secure Payload Dispatcher service is responsible for
200 * setting the SPSR prior to entry into the BL3-2 image.
201 */
202 return 0;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100203#else
204 return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE,
205 DISABLE_ALL_EXCEPTIONS);
206#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100207}
208
209/*******************************************************************************
210 * Gets SPSR for BL33 entry
211 ******************************************************************************/
212static uint32_t qemu_get_spsr_for_bl33_entry(void)
213{
Jens Wiklander52c798e2015-12-07 14:37:10 +0100214 uint32_t spsr;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100215#ifdef AARCH64
216 unsigned int mode;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100217
218 /* Figure out what mode we enter the non-secure world in */
Jeenu Viswambharan2a9b8822017-02-21 14:40:44 +0000219 mode = EL_IMPLEMENTED(2) ? MODE_EL2 : MODE_EL1;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100220
221 /*
222 * TODO: Consider the possibility of specifying the SPSR in
223 * the FIP ToC and allowing the platform to have a say as
224 * well.
225 */
226 spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100227#else
228 spsr = SPSR_MODE32(MODE32_svc,
229 plat_get_ns_image_entrypoint() & 0x1,
230 SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
231#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100232 return spsr;
233}
234
Fu Weic2f78442017-05-27 21:21:42 +0800235#if LOAD_IMAGE_V2
236static int qemu_bl2_handle_post_image_load(unsigned int image_id)
237{
238 int err = 0;
239 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100240#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200241 bl_mem_params_node_t *pager_mem_params = NULL;
242 bl_mem_params_node_t *paged_mem_params = NULL;
243#endif
Fu Weic2f78442017-05-27 21:21:42 +0800244
245 assert(bl_mem_params);
246
247 switch (image_id) {
Fu Weic2f78442017-05-27 21:21:42 +0800248 case BL32_IMAGE_ID:
Etienne Carriere911de8c2018-02-02 13:23:22 +0100249#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200250 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
251 assert(pager_mem_params);
252
253 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
254 assert(paged_mem_params);
255
256 err = parse_optee_header(&bl_mem_params->ep_info,
257 &pager_mem_params->image_info,
258 &paged_mem_params->image_info);
259 if (err != 0) {
260 WARN("OPTEE header parse error.\n");
261 }
262
Etienne Carriere911de8c2018-02-02 13:23:22 +0100263#if defined(SPD_opteed)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200264 /*
265 * OP-TEE expect to receive DTB address in x2.
266 * This will be copied into x2 by dispatcher.
267 */
268 bl_mem_params->ep_info.args.arg3 = PLAT_QEMU_DT_BASE;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100269#else /* case AARCH32_SP_OPTEE */
270 bl_mem_params->ep_info.args.arg0 =
271 bl_mem_params->ep_info.args.arg1;
272 bl_mem_params->ep_info.args.arg1 = 0;
273 bl_mem_params->ep_info.args.arg2 = PLAT_QEMU_DT_BASE;
274 bl_mem_params->ep_info.args.arg3 = 0;
275#endif
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200276#endif
Fu Weic2f78442017-05-27 21:21:42 +0800277 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry();
278 break;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100279
Fu Weic2f78442017-05-27 21:21:42 +0800280 case BL33_IMAGE_ID:
Etienne Carriere911de8c2018-02-02 13:23:22 +0100281#ifdef AARCH32_SP_OPTEE
282 /* AArch32 only core: OP-TEE expects NSec EP in register LR */
283 pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
284 assert(pager_mem_params);
285 pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
286#endif
287
Fu Weic2f78442017-05-27 21:21:42 +0800288 /* BL33 expects to receive the primary CPU MPID (through r0) */
289 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
290 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry();
291 break;
292 }
293
294 return err;
295}
296
297/*******************************************************************************
298 * This function can be used by the platforms to update/use image
299 * information for given `image_id`.
300 ******************************************************************************/
301int bl2_plat_handle_post_image_load(unsigned int image_id)
302{
303 return qemu_bl2_handle_post_image_load(image_id);
304}
305
306#else /* LOAD_IMAGE_V2 */
307
Jens Wiklander52c798e2015-12-07 14:37:10 +0100308/*******************************************************************************
309 * Before calling this function BL3-1 is loaded in memory and its entrypoint
310 * is set by load_image. This is a placeholder for the platform to change
311 * the entrypoint of BL3-1 and set SPSR and security state.
312 * On ARM standard platforms we only set the security state of the entrypoint
313 ******************************************************************************/
314void bl2_plat_set_bl31_ep_info(image_info_t *bl31_image_info,
315 entry_point_info_t *bl31_ep_info)
316{
317 SET_SECURITY_STATE(bl31_ep_info->h.attr, SECURE);
318 bl31_ep_info->spsr = SPSR_64(MODE_EL3, MODE_SP_ELX,
319 DISABLE_ALL_EXCEPTIONS);
320}
321
322/*******************************************************************************
323 * Before calling this function BL3-2 is loaded in memory and its entrypoint
324 * is set by load_image. This is a placeholder for the platform to change
325 * the entrypoint of BL3-2 and set SPSR and security state.
326 * On ARM standard platforms we only set the security state of the entrypoint
327 ******************************************************************************/
328void bl2_plat_set_bl32_ep_info(image_info_t *bl32_image_info,
329 entry_point_info_t *bl32_ep_info)
330{
331 SET_SECURITY_STATE(bl32_ep_info->h.attr, SECURE);
332 bl32_ep_info->spsr = qemu_get_spsr_for_bl32_entry();
333}
334
335/*******************************************************************************
336 * Before calling this function BL3-3 is loaded in memory and its entrypoint
337 * is set by load_image. This is a placeholder for the platform to change
338 * the entrypoint of BL3-3 and set SPSR and security state.
339 * On ARM standard platforms we only set the security state of the entrypoint
340 ******************************************************************************/
341void bl2_plat_set_bl33_ep_info(image_info_t *image,
342 entry_point_info_t *bl33_ep_info)
343{
344
345 SET_SECURITY_STATE(bl33_ep_info->h.attr, NON_SECURE);
346 bl33_ep_info->spsr = qemu_get_spsr_for_bl33_entry();
347}
348
349/*******************************************************************************
350 * Populate the extents of memory available for loading BL32
351 ******************************************************************************/
352void bl2_plat_get_bl32_meminfo(meminfo_t *bl32_meminfo)
353{
354 /*
355 * Populate the extents of memory available for loading BL32.
356 */
357 bl32_meminfo->total_base = BL32_BASE;
358 bl32_meminfo->free_base = BL32_BASE;
359 bl32_meminfo->total_size = (BL32_MEM_BASE + BL32_MEM_SIZE) - BL32_BASE;
360 bl32_meminfo->free_size = (BL32_MEM_BASE + BL32_MEM_SIZE) - BL32_BASE;
361}
362
363/*******************************************************************************
364 * Populate the extents of memory available for loading BL33
365 ******************************************************************************/
366void bl2_plat_get_bl33_meminfo(meminfo_t *bl33_meminfo)
367{
368 bl33_meminfo->total_base = NS_DRAM0_BASE;
369 bl33_meminfo->total_size = NS_DRAM0_SIZE;
370 bl33_meminfo->free_base = NS_DRAM0_BASE;
371 bl33_meminfo->free_size = NS_DRAM0_SIZE;
372}
Fu Weic2f78442017-05-27 21:21:42 +0800373#endif /* !LOAD_IMAGE_V2 */
Jens Wiklander52c798e2015-12-07 14:37:10 +0100374
Etienne Carriere911de8c2018-02-02 13:23:22 +0100375uintptr_t plat_get_ns_image_entrypoint(void)
Jens Wiklander52c798e2015-12-07 14:37:10 +0100376{
377 return NS_IMAGE_OFFSET;
378}