blob: 231f23a77fdcdd1618ca114e988f4b22f9fc99a3 [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
Raymond Mao032ba022023-06-28 15:07:15 -07002 * Copyright (c) 2015-2023, 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 */
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00006
Fu Weic2f78442017-05-27 21:21:42 +08007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
9
Jens Wiklander52c798e2015-12-07 14:37:10 +010010#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Jens Wiklander52c798e2015-12-07 14:37:10 +010012#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013
14#include <arch_helpers.h>
15#include <common/bl_common.h>
16#include <common/debug.h>
17#include <common/desc_image_load.h>
Andre Przywaraffbacb02019-07-10 17:27:17 +010018#include <common/fdt_fixup.h>
Jens Wiklandera43c1282022-11-22 14:39:26 +010019#include <common/fdt_wrappers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <lib/optee_utils.h>
Raymond Mao032ba022023-06-28 15:07:15 -070021#if TRANSFER_LIST
22#include <lib/transfer_list.h>
23#endif
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <lib/utils.h>
25#include <plat/common/platform.h>
26
Isla Mitchelle3631462017-07-14 10:46:32 +010027#include "qemu_private.h"
Jens Wiklander52c798e2015-12-07 14:37:10 +010028
Chen Baozif7d9aa82023-02-20 10:50:15 +000029#define MAP_BL2_TOTAL MAP_REGION_FLAT( \
30 bl2_tzram_layout.total_base, \
31 bl2_tzram_layout.total_size, \
32 MT_MEMORY | MT_RW | MT_SECURE)
33
34#define MAP_BL2_RO MAP_REGION_FLAT( \
35 BL_CODE_BASE, \
36 BL_CODE_END - BL_CODE_BASE, \
37 MT_CODE | MT_SECURE), \
38 MAP_REGION_FLAT( \
39 BL_RO_DATA_BASE, \
40 BL_RO_DATA_END \
41 - BL_RO_DATA_BASE, \
42 MT_RO_DATA | MT_SECURE)
43
Chen Baozi097a43a2023-03-12 20:58:04 +080044#if USE_COHERENT_MEM
Chen Baozif7d9aa82023-02-20 10:50:15 +000045#define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \
46 BL_COHERENT_RAM_BASE, \
47 BL_COHERENT_RAM_END \
48 - BL_COHERENT_RAM_BASE, \
49 MT_DEVICE | MT_RW | MT_SECURE)
Chen Baozi097a43a2023-03-12 20:58:04 +080050#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +010051
Fu Weic2f78442017-05-27 21:21:42 +080052/* Data structure which holds the extents of the trusted SRAM for BL2 */
53static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
Raymond Mao032ba022023-06-28 15:07:15 -070054#if TRANSFER_LIST
55static struct transfer_list_header *bl2_tl;
56#endif
Fu Weic2f78442017-05-27 21:21:42 +080057
Jens Wiklandere22b91e2018-09-04 14:07:19 +020058void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1,
59 u_register_t arg2, u_register_t arg3)
Jens Wiklander52c798e2015-12-07 14:37:10 +010060{
Jens Wiklandere22b91e2018-09-04 14:07:19 +020061 meminfo_t *mem_layout = (void *)arg1;
62
Jens Wiklander52c798e2015-12-07 14:37:10 +010063 /* Initialize the console to provide early debug support */
Michalis Pappascca6cb72018-03-04 15:43:38 +080064 qemu_console_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +010065
66 /* Setup the BL2 memory layout */
67 bl2_tzram_layout = *mem_layout;
68
69 plat_qemu_io_setup();
70}
71
72static void security_setup(void)
73{
74 /*
75 * This is where a TrustZone address space controller and other
76 * security related peripherals, would be configured.
77 */
78}
79
80static void update_dt(void)
81{
Raymond Mao032ba022023-06-28 15:07:15 -070082#if TRANSFER_LIST
83 struct transfer_list_entry *te;
84#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +010085 int ret;
Andrew Walbran9c4d0692020-01-15 14:11:31 +000086 void *fdt = (void *)(uintptr_t)ARM_PRELOADED_DTB_BASE;
Jens Wiklander52c798e2015-12-07 14:37:10 +010087
88 ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
89 if (ret < 0) {
90 ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
91 return;
92 }
93
94 if (dt_add_psci_node(fdt)) {
95 ERROR("Failed to add PSCI Device Tree node\n");
96 return;
97 }
98
99 if (dt_add_psci_cpu_enable_methods(fdt)) {
100 ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
101 return;
102 }
103
104 ret = fdt_pack(fdt);
105 if (ret < 0)
106 ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret);
Raymond Mao032ba022023-06-28 15:07:15 -0700107
108#if TRANSFER_LIST
109 // create a TE
110 te = transfer_list_add(bl2_tl, TL_TAG_FDT, fdt_totalsize(fdt), fdt);
111 if (!te) {
112 ERROR("Failed to add FDT entry to Transfer List\n");
113 return;
114 }
115#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100116}
117
118void bl2_platform_setup(void)
119{
Raymond Mao032ba022023-06-28 15:07:15 -0700120#if TRANSFER_LIST
121 bl2_tl = transfer_list_init((void *)(uintptr_t)FW_HANDOFF_BASE,
122 FW_HANDOFF_SIZE);
123 if (!bl2_tl) {
124 ERROR("Failed to initialize Transfer List at 0x%lx\n",
125 (unsigned long)FW_HANDOFF_BASE);
126 }
127#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100128 security_setup();
129 update_dt();
130
131 /* TODO Initialize timer */
132}
133
Raymond Mao032ba022023-06-28 15:07:15 -0700134void qemu_bl2_sync_transfer_list(void)
135{
136#if TRANSFER_LIST
137 transfer_list_update_checksum(bl2_tl);
138#endif
139}
140
Chen Baozif7d9aa82023-02-20 10:50:15 +0000141void bl2_plat_arch_setup(void)
142{
143 const mmap_region_t bl_regions[] = {
144 MAP_BL2_TOTAL,
145 MAP_BL2_RO,
Chen Baozi097a43a2023-03-12 20:58:04 +0800146#if USE_COHERENT_MEM
Chen Baozif7d9aa82023-02-20 10:50:15 +0000147 MAP_BL_COHERENT_RAM,
Chen Baozi097a43a2023-03-12 20:58:04 +0800148#endif
Chen Baozif7d9aa82023-02-20 10:50:15 +0000149 {0}
150 };
151
152 setup_page_tables(bl_regions, plat_qemu_get_mmap());
153
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700154#ifdef __aarch64__
Chen Baozif7d9aa82023-02-20 10:50:15 +0000155 enable_mmu_el1(0);
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700156#else
Chen Baozif7d9aa82023-02-20 10:50:15 +0000157 enable_mmu_svc_mon(0);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100158#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100159}
160
161/*******************************************************************************
162 * Gets SPSR for BL32 entry
163 ******************************************************************************/
164static uint32_t qemu_get_spsr_for_bl32_entry(void)
165{
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700166#ifdef __aarch64__
Jens Wiklander52c798e2015-12-07 14:37:10 +0100167 /*
168 * The Secure Payload Dispatcher service is responsible for
169 * setting the SPSR prior to entry into the BL3-2 image.
170 */
171 return 0;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100172#else
173 return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE,
174 DISABLE_ALL_EXCEPTIONS);
175#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100176}
177
178/*******************************************************************************
179 * Gets SPSR for BL33 entry
180 ******************************************************************************/
181static uint32_t qemu_get_spsr_for_bl33_entry(void)
182{
Jens Wiklander52c798e2015-12-07 14:37:10 +0100183 uint32_t spsr;
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700184#ifdef __aarch64__
Etienne Carriere911de8c2018-02-02 13:23:22 +0100185 unsigned int mode;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100186
187 /* Figure out what mode we enter the non-secure world in */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000188 mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100189
190 /*
191 * TODO: Consider the possibility of specifying the SPSR in
192 * the FIP ToC and allowing the platform to have a say as
193 * well.
194 */
195 spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100196#else
197 spsr = SPSR_MODE32(MODE32_svc,
198 plat_get_ns_image_entrypoint() & 0x1,
199 SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
200#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100201 return spsr;
202}
203
Jens Wiklandera43c1282022-11-22 14:39:26 +0100204#if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
205static int load_sps_from_tb_fw_config(struct image_info *image_info)
206{
207 void *dtb = (void *)image_info->image_base;
208 const char *compat_str = "arm,sp";
209 const struct fdt_property *uuid;
210 uint32_t load_addr;
211 const char *name;
212 int sp_node;
213 int node;
214
215 node = fdt_node_offset_by_compatible(dtb, -1, compat_str);
216 if (node < 0) {
217 ERROR("Can't find %s in TB_FW_CONFIG", compat_str);
218 return -1;
219 }
220
221 fdt_for_each_subnode(sp_node, dtb, node) {
222 name = fdt_get_name(dtb, sp_node, NULL);
223 if (name == NULL) {
224 ERROR("Can't get name of node in dtb\n");
225 return -1;
226 }
227 uuid = fdt_get_property(dtb, sp_node, "uuid", NULL);
228 if (uuid == NULL) {
229 ERROR("Can't find property uuid in node %s", name);
230 return -1;
231 }
232 if (fdt_read_uint32(dtb, sp_node, "load-address",
233 &load_addr) < 0) {
234 ERROR("Can't read load-address in node %s", name);
235 return -1;
236 }
237 if (qemu_io_register_sp_pkg(name, uuid->data, load_addr) < 0) {
238 return -1;
239 }
240 }
241
242 return 0;
243}
244#endif /*defined(SPD_spmd) && SPMD_SPM_AT_SEL2*/
245
Fu Weic2f78442017-05-27 21:21:42 +0800246static int qemu_bl2_handle_post_image_load(unsigned int image_id)
247{
248 int err = 0;
249 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200250#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200251 bl_mem_params_node_t *pager_mem_params = NULL;
252 bl_mem_params_node_t *paged_mem_params = NULL;
253#endif
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200254#if defined(SPD_spmd)
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100255 bl_mem_params_node_t *bl32_mem_params = NULL;
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200256#endif
Raymond Mao032ba022023-06-28 15:07:15 -0700257#if TRANSFER_LIST
258 struct transfer_list_header *ns_tl = NULL;
259 struct transfer_list_entry *te = NULL;
260#endif
Fu Weic2f78442017-05-27 21:21:42 +0800261
262 assert(bl_mem_params);
263
264 switch (image_id) {
Fu Weic2f78442017-05-27 21:21:42 +0800265 case BL32_IMAGE_ID:
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200266#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200267 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
268 assert(pager_mem_params);
269
270 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
271 assert(paged_mem_params);
272
273 err = parse_optee_header(&bl_mem_params->ep_info,
274 &pager_mem_params->image_info,
275 &paged_mem_params->image_info);
276 if (err != 0) {
277 WARN("OPTEE header parse error.\n");
278 }
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200279#endif
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200280
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100281#if defined(SPMC_OPTEE)
282 /*
283 * Explicit zeroes to unused registers since they may have
284 * been populated by parse_optee_header() above.
285 *
286 * OP-TEE expects system DTB in x2 and TOS_FW_CONFIG in x0,
287 * the latter is filled in below for TOS_FW_CONFIG_ID and
288 * applies to any other SPMC too.
289 */
290 bl_mem_params->ep_info.args.arg2 = ARM_PRELOADED_DTB_BASE;
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200291#elif defined(SPD_opteed)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200292 /*
293 * OP-TEE expect to receive DTB address in x2.
294 * This will be copied into x2 by dispatcher.
295 */
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000296 bl_mem_params->ep_info.args.arg3 = ARM_PRELOADED_DTB_BASE;
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200297#elif defined(AARCH32_SP_OPTEE)
Etienne Carriere911de8c2018-02-02 13:23:22 +0100298 bl_mem_params->ep_info.args.arg0 =
299 bl_mem_params->ep_info.args.arg1;
300 bl_mem_params->ep_info.args.arg1 = 0;
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000301 bl_mem_params->ep_info.args.arg2 = ARM_PRELOADED_DTB_BASE;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100302 bl_mem_params->ep_info.args.arg3 = 0;
303#endif
Fu Weic2f78442017-05-27 21:21:42 +0800304 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry();
305 break;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100306
Fu Weic2f78442017-05-27 21:21:42 +0800307 case BL33_IMAGE_ID:
Etienne Carriere911de8c2018-02-02 13:23:22 +0100308#ifdef AARCH32_SP_OPTEE
309 /* AArch32 only core: OP-TEE expects NSec EP in register LR */
310 pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
311 assert(pager_mem_params);
312 pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
313#endif
314
Raymond Mao032ba022023-06-28 15:07:15 -0700315 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry();
316
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000317#if ARM_LINUX_KERNEL_AS_BL33
318 /*
319 * According to the file ``Documentation/arm64/booting.txt`` of
320 * the Linux kernel tree, Linux expects the physical address of
321 * the device tree blob (DTB) in x0, while x1-x3 are reserved
322 * for future use and must be 0.
323 */
324 bl_mem_params->ep_info.args.arg0 =
325 (u_register_t)ARM_PRELOADED_DTB_BASE;
326 bl_mem_params->ep_info.args.arg1 = 0U;
327 bl_mem_params->ep_info.args.arg2 = 0U;
328 bl_mem_params->ep_info.args.arg3 = 0U;
Raymond Mao032ba022023-06-28 15:07:15 -0700329#elif TRANSFER_LIST
330 if (bl2_tl) {
331 // relocate the tl to pre-allocate NS memory
332 ns_tl = transfer_list_relocate(bl2_tl,
333 (void *)(uintptr_t)FW_NS_HANDOFF_BASE,
334 bl2_tl->max_size);
335 if (!ns_tl) {
336 ERROR("Relocate TL to 0x%lx failed\n",
337 (unsigned long)FW_NS_HANDOFF_BASE);
338 return -1;
339 }
340 NOTICE("Transfer list handoff to BL33\n");
341 transfer_list_dump(ns_tl);
342
343 te = transfer_list_find(ns_tl, TL_TAG_FDT);
344
345 bl_mem_params->ep_info.args.arg1 =
346 TRANSFER_LIST_SIGNATURE |
347 REGISTER_CONVENTION_VERSION_MASK;
348 bl_mem_params->ep_info.args.arg3 = (uintptr_t)ns_tl;
349
350 if (GET_RW(bl_mem_params->ep_info.spsr) == MODE_RW_32) {
351 // aarch32
352 bl_mem_params->ep_info.args.arg0 = 0;
353 bl_mem_params->ep_info.args.arg2 = te ?
354 (uintptr_t)transfer_list_entry_data(te)
355 : 0;
356 } else {
357 // aarch64
358 bl_mem_params->ep_info.args.arg0 = te ?
359 (uintptr_t)transfer_list_entry_data(te)
360 : 0;
361 bl_mem_params->ep_info.args.arg2 = 0;
362 }
363 } else {
364 // Legacy handoff
365 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
366 }
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000367#else
Fu Weic2f78442017-05-27 21:21:42 +0800368 /* BL33 expects to receive the primary CPU MPID (through r0) */
369 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
Raymond Mao032ba022023-06-28 15:07:15 -0700370#endif // ARM_LINUX_KERNEL_AS_BL33
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000371
Fu Weic2f78442017-05-27 21:21:42 +0800372 break;
Jens Wiklandera43c1282022-11-22 14:39:26 +0100373#ifdef SPD_spmd
374#if SPMD_SPM_AT_SEL2
375 case TB_FW_CONFIG_ID:
376 err = load_sps_from_tb_fw_config(&bl_mem_params->image_info);
377 break;
378#endif
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100379 case TOS_FW_CONFIG_ID:
380 /* An SPMC expects TOS_FW_CONFIG in x0/r0 */
381 bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
382 bl32_mem_params->ep_info.args.arg0 =
383 bl_mem_params->image_info.image_base;
384 break;
385#endif
Jonathan Wrightff957ed2018-03-14 15:24:00 +0000386 default:
387 /* Do nothing in default case */
388 break;
Fu Weic2f78442017-05-27 21:21:42 +0800389 }
390
391 return err;
392}
393
394/*******************************************************************************
395 * This function can be used by the platforms to update/use image
396 * information for given `image_id`.
397 ******************************************************************************/
398int bl2_plat_handle_post_image_load(unsigned int image_id)
399{
400 return qemu_bl2_handle_post_image_load(image_id);
401}
Jens Wiklander52c798e2015-12-07 14:37:10 +0100402
Etienne Carriere911de8c2018-02-02 13:23:22 +0100403uintptr_t plat_get_ns_image_entrypoint(void)
Jens Wiklander52c798e2015-12-07 14:37:10 +0100404{
405 return NS_IMAGE_OFFSET;
406}