blob: be5587714a4eac6bed341e64bf5491df01baeaaa [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
Jens Wiklanderd4b84f02022-11-18 15:40:04 +01002 * Copyright (c) 2015-2022, 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>
21#include <lib/utils.h>
22#include <plat/common/platform.h>
23
Isla Mitchelle3631462017-07-14 10:46:32 +010024#include "qemu_private.h"
Jens Wiklander52c798e2015-12-07 14:37:10 +010025
Jens Wiklander52c798e2015-12-07 14:37:10 +010026
Fu Weic2f78442017-05-27 21:21:42 +080027/* Data structure which holds the extents of the trusted SRAM for BL2 */
28static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
29
Jens Wiklandere22b91e2018-09-04 14:07:19 +020030void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1,
31 u_register_t arg2, u_register_t arg3)
Jens Wiklander52c798e2015-12-07 14:37:10 +010032{
Jens Wiklandere22b91e2018-09-04 14:07:19 +020033 meminfo_t *mem_layout = (void *)arg1;
34
Jens Wiklander52c798e2015-12-07 14:37:10 +010035 /* Initialize the console to provide early debug support */
Michalis Pappascca6cb72018-03-04 15:43:38 +080036 qemu_console_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +010037
38 /* Setup the BL2 memory layout */
39 bl2_tzram_layout = *mem_layout;
40
41 plat_qemu_io_setup();
42}
43
44static void security_setup(void)
45{
46 /*
47 * This is where a TrustZone address space controller and other
48 * security related peripherals, would be configured.
49 */
50}
51
52static void update_dt(void)
53{
54 int ret;
Andrew Walbran9c4d0692020-01-15 14:11:31 +000055 void *fdt = (void *)(uintptr_t)ARM_PRELOADED_DTB_BASE;
Jens Wiklander52c798e2015-12-07 14:37:10 +010056
57 ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
58 if (ret < 0) {
59 ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
60 return;
61 }
62
63 if (dt_add_psci_node(fdt)) {
64 ERROR("Failed to add PSCI Device Tree node\n");
65 return;
66 }
67
68 if (dt_add_psci_cpu_enable_methods(fdt)) {
69 ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
70 return;
71 }
72
73 ret = fdt_pack(fdt);
74 if (ret < 0)
75 ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret);
76}
77
78void bl2_platform_setup(void)
79{
80 security_setup();
81 update_dt();
82
83 /* TODO Initialize timer */
84}
85
Julius Werner8e0ef0f2019-07-09 14:02:43 -070086#ifdef __aarch64__
Etienne Carriere911de8c2018-02-02 13:23:22 +010087#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_el1(__VA_ARGS__)
Julius Werner8e0ef0f2019-07-09 14:02:43 -070088#else
89#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_svc_mon(__VA_ARGS__)
Etienne Carriere911de8c2018-02-02 13:23:22 +010090#endif
91
Jens Wiklander52c798e2015-12-07 14:37:10 +010092void bl2_plat_arch_setup(void)
93{
Etienne Carriere911de8c2018-02-02 13:23:22 +010094 QEMU_CONFIGURE_BL2_MMU(bl2_tzram_layout.total_base,
Jens Wiklander52c798e2015-12-07 14:37:10 +010095 bl2_tzram_layout.total_size,
Michalis Pappasba861122018-02-28 14:36:03 +080096 BL_CODE_BASE, BL_CODE_END,
97 BL_RO_DATA_BASE, BL_RO_DATA_END,
Masahiro Yamada0fac5af2016-12-28 16:11:41 +090098 BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END);
Jens Wiklander52c798e2015-12-07 14:37:10 +010099}
100
101/*******************************************************************************
102 * Gets SPSR for BL32 entry
103 ******************************************************************************/
104static uint32_t qemu_get_spsr_for_bl32_entry(void)
105{
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700106#ifdef __aarch64__
Jens Wiklander52c798e2015-12-07 14:37:10 +0100107 /*
108 * The Secure Payload Dispatcher service is responsible for
109 * setting the SPSR prior to entry into the BL3-2 image.
110 */
111 return 0;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100112#else
113 return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE,
114 DISABLE_ALL_EXCEPTIONS);
115#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100116}
117
118/*******************************************************************************
119 * Gets SPSR for BL33 entry
120 ******************************************************************************/
121static uint32_t qemu_get_spsr_for_bl33_entry(void)
122{
Jens Wiklander52c798e2015-12-07 14:37:10 +0100123 uint32_t spsr;
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700124#ifdef __aarch64__
Etienne Carriere911de8c2018-02-02 13:23:22 +0100125 unsigned int mode;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100126
127 /* Figure out what mode we enter the non-secure world in */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000128 mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100129
130 /*
131 * TODO: Consider the possibility of specifying the SPSR in
132 * the FIP ToC and allowing the platform to have a say as
133 * well.
134 */
135 spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100136#else
137 spsr = SPSR_MODE32(MODE32_svc,
138 plat_get_ns_image_entrypoint() & 0x1,
139 SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
140#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100141 return spsr;
142}
143
Jens Wiklandera43c1282022-11-22 14:39:26 +0100144#if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
145static int load_sps_from_tb_fw_config(struct image_info *image_info)
146{
147 void *dtb = (void *)image_info->image_base;
148 const char *compat_str = "arm,sp";
149 const struct fdt_property *uuid;
150 uint32_t load_addr;
151 const char *name;
152 int sp_node;
153 int node;
154
155 node = fdt_node_offset_by_compatible(dtb, -1, compat_str);
156 if (node < 0) {
157 ERROR("Can't find %s in TB_FW_CONFIG", compat_str);
158 return -1;
159 }
160
161 fdt_for_each_subnode(sp_node, dtb, node) {
162 name = fdt_get_name(dtb, sp_node, NULL);
163 if (name == NULL) {
164 ERROR("Can't get name of node in dtb\n");
165 return -1;
166 }
167 uuid = fdt_get_property(dtb, sp_node, "uuid", NULL);
168 if (uuid == NULL) {
169 ERROR("Can't find property uuid in node %s", name);
170 return -1;
171 }
172 if (fdt_read_uint32(dtb, sp_node, "load-address",
173 &load_addr) < 0) {
174 ERROR("Can't read load-address in node %s", name);
175 return -1;
176 }
177 if (qemu_io_register_sp_pkg(name, uuid->data, load_addr) < 0) {
178 return -1;
179 }
180 }
181
182 return 0;
183}
184#endif /*defined(SPD_spmd) && SPMD_SPM_AT_SEL2*/
185
Fu Weic2f78442017-05-27 21:21:42 +0800186static int qemu_bl2_handle_post_image_load(unsigned int image_id)
187{
188 int err = 0;
189 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200190#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200191 bl_mem_params_node_t *pager_mem_params = NULL;
192 bl_mem_params_node_t *paged_mem_params = NULL;
193#endif
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200194#if defined(SPD_spmd)
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100195 bl_mem_params_node_t *bl32_mem_params = NULL;
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200196#endif
Fu Weic2f78442017-05-27 21:21:42 +0800197
198 assert(bl_mem_params);
199
200 switch (image_id) {
Fu Weic2f78442017-05-27 21:21:42 +0800201 case BL32_IMAGE_ID:
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200202#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200203 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
204 assert(pager_mem_params);
205
206 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
207 assert(paged_mem_params);
208
209 err = parse_optee_header(&bl_mem_params->ep_info,
210 &pager_mem_params->image_info,
211 &paged_mem_params->image_info);
212 if (err != 0) {
213 WARN("OPTEE header parse error.\n");
214 }
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200215#endif
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200216
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100217#if defined(SPMC_OPTEE)
218 /*
219 * Explicit zeroes to unused registers since they may have
220 * been populated by parse_optee_header() above.
221 *
222 * OP-TEE expects system DTB in x2 and TOS_FW_CONFIG in x0,
223 * the latter is filled in below for TOS_FW_CONFIG_ID and
224 * applies to any other SPMC too.
225 */
226 bl_mem_params->ep_info.args.arg2 = ARM_PRELOADED_DTB_BASE;
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200227#elif defined(SPD_opteed)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200228 /*
229 * OP-TEE expect to receive DTB address in x2.
230 * This will be copied into x2 by dispatcher.
231 */
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000232 bl_mem_params->ep_info.args.arg3 = ARM_PRELOADED_DTB_BASE;
Jens Wiklanderff263dc2021-05-25 18:15:11 +0200233#elif defined(AARCH32_SP_OPTEE)
Etienne Carriere911de8c2018-02-02 13:23:22 +0100234 bl_mem_params->ep_info.args.arg0 =
235 bl_mem_params->ep_info.args.arg1;
236 bl_mem_params->ep_info.args.arg1 = 0;
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000237 bl_mem_params->ep_info.args.arg2 = ARM_PRELOADED_DTB_BASE;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100238 bl_mem_params->ep_info.args.arg3 = 0;
239#endif
Fu Weic2f78442017-05-27 21:21:42 +0800240 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry();
241 break;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100242
Fu Weic2f78442017-05-27 21:21:42 +0800243 case BL33_IMAGE_ID:
Etienne Carriere911de8c2018-02-02 13:23:22 +0100244#ifdef AARCH32_SP_OPTEE
245 /* AArch32 only core: OP-TEE expects NSec EP in register LR */
246 pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
247 assert(pager_mem_params);
248 pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
249#endif
250
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000251#if ARM_LINUX_KERNEL_AS_BL33
252 /*
253 * According to the file ``Documentation/arm64/booting.txt`` of
254 * the Linux kernel tree, Linux expects the physical address of
255 * the device tree blob (DTB) in x0, while x1-x3 are reserved
256 * for future use and must be 0.
257 */
258 bl_mem_params->ep_info.args.arg0 =
259 (u_register_t)ARM_PRELOADED_DTB_BASE;
260 bl_mem_params->ep_info.args.arg1 = 0U;
261 bl_mem_params->ep_info.args.arg2 = 0U;
262 bl_mem_params->ep_info.args.arg3 = 0U;
263#else
Fu Weic2f78442017-05-27 21:21:42 +0800264 /* BL33 expects to receive the primary CPU MPID (through r0) */
265 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
Andrew Walbran9c4d0692020-01-15 14:11:31 +0000266#endif
267
Fu Weic2f78442017-05-27 21:21:42 +0800268 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry();
269 break;
Jens Wiklandera43c1282022-11-22 14:39:26 +0100270#ifdef SPD_spmd
271#if SPMD_SPM_AT_SEL2
272 case TB_FW_CONFIG_ID:
273 err = load_sps_from_tb_fw_config(&bl_mem_params->image_info);
274 break;
275#endif
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100276 case TOS_FW_CONFIG_ID:
277 /* An SPMC expects TOS_FW_CONFIG in x0/r0 */
278 bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
279 bl32_mem_params->ep_info.args.arg0 =
280 bl_mem_params->image_info.image_base;
281 break;
282#endif
Jonathan Wrightff957ed2018-03-14 15:24:00 +0000283 default:
284 /* Do nothing in default case */
285 break;
Fu Weic2f78442017-05-27 21:21:42 +0800286 }
287
288 return err;
289}
290
291/*******************************************************************************
292 * This function can be used by the platforms to update/use image
293 * information for given `image_id`.
294 ******************************************************************************/
295int bl2_plat_handle_post_image_load(unsigned int image_id)
296{
297 return qemu_bl2_handle_post_image_load(image_id);
298}
Jens Wiklander52c798e2015-12-07 14:37:10 +0100299
Etienne Carriere911de8c2018-02-02 13:23:22 +0100300uintptr_t plat_get_ns_image_entrypoint(void)
Jens Wiklander52c798e2015-12-07 14:37:10 +0100301{
302 return NS_IMAGE_OFFSET;
303}