blob: 1306f34bdd20170c0594b0151afd7300ebceef51 [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 Wiklander52c798e2015-12-07 14:37:10 +010012#include <libfdt.h>
13#include <platform_def.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +010014#include <string.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000015#include <utils.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010016#include "qemu_private.h"
Jens Wiklander52c798e2015-12-07 14:37:10 +010017
18/*
19 * The next 2 constants identify the extents of the code & RO data region.
20 * These addresses are used by the MMU setup code and therefore they must be
21 * page-aligned. It is the responsibility of the linker script to ensure that
22 * __RO_START__ and __RO_END__ linker symbols refer to page-aligned addresses.
23 */
24#define BL2_RO_BASE (unsigned long)(&__RO_START__)
25#define BL2_RO_LIMIT (unsigned long)(&__RO_END__)
26
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
30#if !LOAD_IMAGE_V2
Jens Wiklander52c798e2015-12-07 14:37:10 +010031/*******************************************************************************
32 * This structure represents the superset of information that is passed to
33 * BL3-1, e.g. while passing control to it from BL2, bl31_params
34 * and other platform specific params
35 ******************************************************************************/
36typedef struct bl2_to_bl31_params_mem {
37 bl31_params_t bl31_params;
38 image_info_t bl31_image_info;
39 image_info_t bl32_image_info;
40 image_info_t bl33_image_info;
41 entry_point_info_t bl33_ep_info;
42 entry_point_info_t bl32_ep_info;
43 entry_point_info_t bl31_ep_info;
44} bl2_to_bl31_params_mem_t;
45
46
47static bl2_to_bl31_params_mem_t bl31_params_mem;
48
Jens Wiklander52c798e2015-12-07 14:37:10 +010049
50meminfo_t *bl2_plat_sec_mem_layout(void)
51{
52 return &bl2_tzram_layout;
53}
54
55/*******************************************************************************
56 * This function assigns a pointer to the memory that the platform has kept
57 * aside to pass platform specific and trusted firmware related information
58 * to BL31. This memory is allocated by allocating memory to
59 * bl2_to_bl31_params_mem_t structure which is a superset of all the
60 * structure whose information is passed to BL31
61 * NOTE: This function should be called only once and should be done
62 * before generating params to BL31
63 ******************************************************************************/
64bl31_params_t *bl2_plat_get_bl31_params(void)
65{
66 bl31_params_t *bl2_to_bl31_params;
67
68 /*
69 * Initialise the memory for all the arguments that needs to
70 * be passed to BL3-1
71 */
Douglas Raillarda8954fc2017-01-26 15:54:44 +000072 zeromem(&bl31_params_mem, sizeof(bl2_to_bl31_params_mem_t));
Jens Wiklander52c798e2015-12-07 14:37:10 +010073
74 /* Assign memory for TF related information */
75 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
76 SET_PARAM_HEAD(bl2_to_bl31_params, PARAM_BL31, VERSION_1, 0);
77
78 /* Fill BL3-1 related information */
79 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
80 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, PARAM_IMAGE_BINARY,
81 VERSION_1, 0);
82
83 /* Fill BL3-2 related information */
84 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
85 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, PARAM_EP,
86 VERSION_1, 0);
87 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
88 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, PARAM_IMAGE_BINARY,
89 VERSION_1, 0);
90
91 /* Fill BL3-3 related information */
92 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
93 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_ep_info,
94 PARAM_EP, VERSION_1, 0);
95
96 /* BL3-3 expects to receive the primary CPU MPID (through x0) */
97 bl2_to_bl31_params->bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
98
99 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
100 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, PARAM_IMAGE_BINARY,
101 VERSION_1, 0);
102
103 return bl2_to_bl31_params;
104}
105
106/* Flush the TF params and the TF plat params */
107void bl2_plat_flush_bl31_params(void)
108{
109 flush_dcache_range((unsigned long)&bl31_params_mem,
110 sizeof(bl2_to_bl31_params_mem_t));
111}
112
113/*******************************************************************************
114 * This function returns a pointer to the shared memory that the platform
115 * has kept to point to entry point information of BL31 to BL2
116 ******************************************************************************/
117struct entry_point_info *bl2_plat_get_bl31_ep_info(void)
118{
119#if DEBUG
120 bl31_params_mem.bl31_ep_info.args.arg1 = QEMU_BL31_PLAT_PARAM_VAL;
121#endif
122
123 return &bl31_params_mem.bl31_ep_info;
124}
Fu Weic2f78442017-05-27 21:21:42 +0800125#endif /* !LOAD_IMAGE_V2 */
Jens Wiklander52c798e2015-12-07 14:37:10 +0100126
127
128
129void bl2_early_platform_setup(meminfo_t *mem_layout)
130{
131 /* Initialize the console to provide early debug support */
132 console_init(PLAT_QEMU_BOOT_UART_BASE, PLAT_QEMU_BOOT_UART_CLK_IN_HZ,
133 PLAT_QEMU_CONSOLE_BAUDRATE);
134
135 /* Setup the BL2 memory layout */
136 bl2_tzram_layout = *mem_layout;
137
138 plat_qemu_io_setup();
139}
140
141static void security_setup(void)
142{
143 /*
144 * This is where a TrustZone address space controller and other
145 * security related peripherals, would be configured.
146 */
147}
148
149static void update_dt(void)
150{
151 int ret;
152 void *fdt = (void *)(uintptr_t)PLAT_QEMU_DT_BASE;
153
154 ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
155 if (ret < 0) {
156 ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
157 return;
158 }
159
160 if (dt_add_psci_node(fdt)) {
161 ERROR("Failed to add PSCI Device Tree node\n");
162 return;
163 }
164
165 if (dt_add_psci_cpu_enable_methods(fdt)) {
166 ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
167 return;
168 }
169
170 ret = fdt_pack(fdt);
171 if (ret < 0)
172 ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret);
173}
174
175void bl2_platform_setup(void)
176{
177 security_setup();
178 update_dt();
179
180 /* TODO Initialize timer */
181}
182
183void bl2_plat_arch_setup(void)
184{
185 qemu_configure_mmu_el1(bl2_tzram_layout.total_base,
186 bl2_tzram_layout.total_size,
187 BL2_RO_BASE, BL2_RO_LIMIT,
Masahiro Yamada0fac5af2016-12-28 16:11:41 +0900188 BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END);
Jens Wiklander52c798e2015-12-07 14:37:10 +0100189}
190
191/*******************************************************************************
192 * Gets SPSR for BL32 entry
193 ******************************************************************************/
194static uint32_t qemu_get_spsr_for_bl32_entry(void)
195{
196 /*
197 * The Secure Payload Dispatcher service is responsible for
198 * setting the SPSR prior to entry into the BL3-2 image.
199 */
200 return 0;
201}
202
203/*******************************************************************************
204 * Gets SPSR for BL33 entry
205 ******************************************************************************/
206static uint32_t qemu_get_spsr_for_bl33_entry(void)
207{
Jens Wiklander52c798e2015-12-07 14:37:10 +0100208 unsigned int mode;
209 uint32_t spsr;
210
211 /* Figure out what mode we enter the non-secure world in */
Jeenu Viswambharan2a9b8822017-02-21 14:40:44 +0000212 mode = EL_IMPLEMENTED(2) ? MODE_EL2 : MODE_EL1;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100213
214 /*
215 * TODO: Consider the possibility of specifying the SPSR in
216 * the FIP ToC and allowing the platform to have a say as
217 * well.
218 */
219 spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
220 return spsr;
221}
222
Fu Weic2f78442017-05-27 21:21:42 +0800223#if LOAD_IMAGE_V2
224static int qemu_bl2_handle_post_image_load(unsigned int image_id)
225{
226 int err = 0;
227 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
228
229 assert(bl_mem_params);
230
231 switch (image_id) {
232# ifdef AARCH64
233 case BL32_IMAGE_ID:
234 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry();
235 break;
236# endif
237 case BL33_IMAGE_ID:
238 /* BL33 expects to receive the primary CPU MPID (through r0) */
239 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
240 bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry();
241 break;
242 }
243
244 return err;
245}
246
247/*******************************************************************************
248 * This function can be used by the platforms to update/use image
249 * information for given `image_id`.
250 ******************************************************************************/
251int bl2_plat_handle_post_image_load(unsigned int image_id)
252{
253 return qemu_bl2_handle_post_image_load(image_id);
254}
255
256#else /* LOAD_IMAGE_V2 */
257
Jens Wiklander52c798e2015-12-07 14:37:10 +0100258/*******************************************************************************
259 * Before calling this function BL3-1 is loaded in memory and its entrypoint
260 * is set by load_image. This is a placeholder for the platform to change
261 * the entrypoint of BL3-1 and set SPSR and security state.
262 * On ARM standard platforms we only set the security state of the entrypoint
263 ******************************************************************************/
264void bl2_plat_set_bl31_ep_info(image_info_t *bl31_image_info,
265 entry_point_info_t *bl31_ep_info)
266{
267 SET_SECURITY_STATE(bl31_ep_info->h.attr, SECURE);
268 bl31_ep_info->spsr = SPSR_64(MODE_EL3, MODE_SP_ELX,
269 DISABLE_ALL_EXCEPTIONS);
270}
271
272/*******************************************************************************
273 * Before calling this function BL3-2 is loaded in memory and its entrypoint
274 * is set by load_image. This is a placeholder for the platform to change
275 * the entrypoint of BL3-2 and set SPSR and security state.
276 * On ARM standard platforms we only set the security state of the entrypoint
277 ******************************************************************************/
278void bl2_plat_set_bl32_ep_info(image_info_t *bl32_image_info,
279 entry_point_info_t *bl32_ep_info)
280{
281 SET_SECURITY_STATE(bl32_ep_info->h.attr, SECURE);
282 bl32_ep_info->spsr = qemu_get_spsr_for_bl32_entry();
283}
284
285/*******************************************************************************
286 * Before calling this function BL3-3 is loaded in memory and its entrypoint
287 * is set by load_image. This is a placeholder for the platform to change
288 * the entrypoint of BL3-3 and set SPSR and security state.
289 * On ARM standard platforms we only set the security state of the entrypoint
290 ******************************************************************************/
291void bl2_plat_set_bl33_ep_info(image_info_t *image,
292 entry_point_info_t *bl33_ep_info)
293{
294
295 SET_SECURITY_STATE(bl33_ep_info->h.attr, NON_SECURE);
296 bl33_ep_info->spsr = qemu_get_spsr_for_bl33_entry();
297}
298
299/*******************************************************************************
300 * Populate the extents of memory available for loading BL32
301 ******************************************************************************/
302void bl2_plat_get_bl32_meminfo(meminfo_t *bl32_meminfo)
303{
304 /*
305 * Populate the extents of memory available for loading BL32.
306 */
307 bl32_meminfo->total_base = BL32_BASE;
308 bl32_meminfo->free_base = BL32_BASE;
309 bl32_meminfo->total_size = (BL32_MEM_BASE + BL32_MEM_SIZE) - BL32_BASE;
310 bl32_meminfo->free_size = (BL32_MEM_BASE + BL32_MEM_SIZE) - BL32_BASE;
311}
312
313/*******************************************************************************
314 * Populate the extents of memory available for loading BL33
315 ******************************************************************************/
316void bl2_plat_get_bl33_meminfo(meminfo_t *bl33_meminfo)
317{
318 bl33_meminfo->total_base = NS_DRAM0_BASE;
319 bl33_meminfo->total_size = NS_DRAM0_SIZE;
320 bl33_meminfo->free_base = NS_DRAM0_BASE;
321 bl33_meminfo->free_size = NS_DRAM0_SIZE;
322}
Fu Weic2f78442017-05-27 21:21:42 +0800323#endif /* !LOAD_IMAGE_V2 */
Jens Wiklander52c798e2015-12-07 14:37:10 +0100324
325unsigned long plat_get_ns_image_entrypoint(void)
326{
327 return NS_IMAGE_OFFSET;
328}