blob: d76621d8fb94ba572aa1285ba86bde8250ab58f8 [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>
Jens Wiklander52c798e2015-12-07 14:37:10 +01009#include <debug.h>
Fu Weic2f78442017-05-27 21:21:42 +080010#include <desc_image_load.h>
Jens Wiklander0acbaaa2017-08-24 13:16:26 +020011#include <optee_utils.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010012#include <libfdt.h>
Etienne Carriere911de8c2018-02-02 13:23:22 +010013#include <platform.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010014#include <platform_def.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010015#include <string.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000016#include <utils.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010017#include "qemu_private.h"
Jens Wiklander52c798e2015-12-07 14:37:10 +010018
Jens Wiklander52c798e2015-12-07 14:37:10 +010019
Fu Weic2f78442017-05-27 21:21:42 +080020/* Data structure which holds the extents of the trusted SRAM for BL2 */
21static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
22
Jens Wiklandere22b91e2018-09-04 14:07:19 +020023void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1,
24 u_register_t arg2, u_register_t arg3)
Jens Wiklander52c798e2015-12-07 14:37:10 +010025{
Jens Wiklandere22b91e2018-09-04 14:07:19 +020026 meminfo_t *mem_layout = (void *)arg1;
27
Jens Wiklander52c798e2015-12-07 14:37:10 +010028 /* Initialize the console to provide early debug support */
Michalis Pappascca6cb72018-03-04 15:43:38 +080029 qemu_console_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +010030
31 /* Setup the BL2 memory layout */
32 bl2_tzram_layout = *mem_layout;
33
34 plat_qemu_io_setup();
35}
36
37static void security_setup(void)
38{
39 /*
40 * This is where a TrustZone address space controller and other
41 * security related peripherals, would be configured.
42 */
43}
44
45static void update_dt(void)
46{
47 int ret;
48 void *fdt = (void *)(uintptr_t)PLAT_QEMU_DT_BASE;
49
50 ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
51 if (ret < 0) {
52 ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
53 return;
54 }
55
56 if (dt_add_psci_node(fdt)) {
57 ERROR("Failed to add PSCI Device Tree node\n");
58 return;
59 }
60
61 if (dt_add_psci_cpu_enable_methods(fdt)) {
62 ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
63 return;
64 }
65
66 ret = fdt_pack(fdt);
67 if (ret < 0)
68 ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret);
69}
70
71void bl2_platform_setup(void)
72{
73 security_setup();
74 update_dt();
75
76 /* TODO Initialize timer */
77}
78
Etienne Carriere911de8c2018-02-02 13:23:22 +010079#ifdef AARCH32
Antonio Nino Diaz099b0b12018-09-26 09:29:45 +010080#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_svc_mon(__VA_ARGS__)
Etienne Carriere911de8c2018-02-02 13:23:22 +010081#else
82#define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_el1(__VA_ARGS__)
83#endif
84
Jens Wiklander52c798e2015-12-07 14:37:10 +010085void bl2_plat_arch_setup(void)
86{
Etienne Carriere911de8c2018-02-02 13:23:22 +010087 QEMU_CONFIGURE_BL2_MMU(bl2_tzram_layout.total_base,
Jens Wiklander52c798e2015-12-07 14:37:10 +010088 bl2_tzram_layout.total_size,
Michalis Pappasba861122018-02-28 14:36:03 +080089 BL_CODE_BASE, BL_CODE_END,
90 BL_RO_DATA_BASE, BL_RO_DATA_END,
Masahiro Yamada0fac5af2016-12-28 16:11:41 +090091 BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END);
Jens Wiklander52c798e2015-12-07 14:37:10 +010092}
93
94/*******************************************************************************
95 * Gets SPSR for BL32 entry
96 ******************************************************************************/
97static uint32_t qemu_get_spsr_for_bl32_entry(void)
98{
Etienne Carriere911de8c2018-02-02 13:23:22 +010099#ifdef AARCH64
Jens Wiklander52c798e2015-12-07 14:37:10 +0100100 /*
101 * The Secure Payload Dispatcher service is responsible for
102 * setting the SPSR prior to entry into the BL3-2 image.
103 */
104 return 0;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100105#else
106 return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE,
107 DISABLE_ALL_EXCEPTIONS);
108#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100109}
110
111/*******************************************************************************
112 * Gets SPSR for BL33 entry
113 ******************************************************************************/
114static uint32_t qemu_get_spsr_for_bl33_entry(void)
115{
Jens Wiklander52c798e2015-12-07 14:37:10 +0100116 uint32_t spsr;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100117#ifdef AARCH64
118 unsigned int mode;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100119
120 /* Figure out what mode we enter the non-secure world in */
Jeenu Viswambharan2a9b8822017-02-21 14:40:44 +0000121 mode = EL_IMPLEMENTED(2) ? MODE_EL2 : MODE_EL1;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100122
123 /*
124 * TODO: Consider the possibility of specifying the SPSR in
125 * the FIP ToC and allowing the platform to have a say as
126 * well.
127 */
128 spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100129#else
130 spsr = SPSR_MODE32(MODE32_svc,
131 plat_get_ns_image_entrypoint() & 0x1,
132 SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
133#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100134 return spsr;
135}
136
Fu Weic2f78442017-05-27 21:21:42 +0800137static int qemu_bl2_handle_post_image_load(unsigned int image_id)
138{
139 int err = 0;
140 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
Etienne Carriere911de8c2018-02-02 13:23:22 +0100141#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200142 bl_mem_params_node_t *pager_mem_params = NULL;
143 bl_mem_params_node_t *paged_mem_params = NULL;
144#endif
Fu Weic2f78442017-05-27 21:21:42 +0800145
146 assert(bl_mem_params);
147
148 switch (image_id) {
Fu Weic2f78442017-05-27 21:21:42 +0800149 case BL32_IMAGE_ID:
Etienne Carriere911de8c2018-02-02 13:23:22 +0100150#if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200151 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
152 assert(pager_mem_params);
153
154 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
155 assert(paged_mem_params);
156
157 err = parse_optee_header(&bl_mem_params->ep_info,
158 &pager_mem_params->image_info,
159 &paged_mem_params->image_info);
160 if (err != 0) {
161 WARN("OPTEE header parse error.\n");
162 }
163
Etienne Carriere911de8c2018-02-02 13:23:22 +0100164#if defined(SPD_opteed)
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200165 /*
166 * OP-TEE expect to receive DTB address in x2.
167 * This will be copied into x2 by dispatcher.
168 */
169 bl_mem_params->ep_info.args.arg3 = PLAT_QEMU_DT_BASE;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100170#else /* case AARCH32_SP_OPTEE */
171 bl_mem_params->ep_info.args.arg0 =
172 bl_mem_params->ep_info.args.arg1;
173 bl_mem_params->ep_info.args.arg1 = 0;
174 bl_mem_params->ep_info.args.arg2 = PLAT_QEMU_DT_BASE;
175 bl_mem_params->ep_info.args.arg3 = 0;
176#endif
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200177#endif
Fu Weic2f78442017-05-27 21:21:42 +0800178 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry();
179 break;
Etienne Carriere911de8c2018-02-02 13:23:22 +0100180
Fu Weic2f78442017-05-27 21:21:42 +0800181 case BL33_IMAGE_ID:
Etienne Carriere911de8c2018-02-02 13:23:22 +0100182#ifdef AARCH32_SP_OPTEE
183 /* AArch32 only core: OP-TEE expects NSec EP in register LR */
184 pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
185 assert(pager_mem_params);
186 pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
187#endif
188
Fu Weic2f78442017-05-27 21:21:42 +0800189 /* BL33 expects to receive the primary CPU MPID (through r0) */
190 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
191 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry();
192 break;
Jonathan Wrightff957ed2018-03-14 15:24:00 +0000193 default:
194 /* Do nothing in default case */
195 break;
Fu Weic2f78442017-05-27 21:21:42 +0800196 }
197
198 return err;
199}
200
201/*******************************************************************************
202 * This function can be used by the platforms to update/use image
203 * information for given `image_id`.
204 ******************************************************************************/
205int bl2_plat_handle_post_image_load(unsigned int image_id)
206{
207 return qemu_bl2_handle_post_image_load(image_id);
208}
Jens Wiklander52c798e2015-12-07 14:37:10 +0100209
Etienne Carriere911de8c2018-02-02 13:23:22 +0100210uintptr_t plat_get_ns_image_entrypoint(void)
Jens Wiklander52c798e2015-12-07 14:37:10 +0100211{
212 return NS_IMAGE_OFFSET;
213}