blob: 9182bd12843fc19b8398e7e0fe3589733f937793 [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Douglas Raillarda8954fc2017-01-26 15:54:44 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handley9df48042015-03-19 18:58:55 +00005 */
6
7#include <arch_helpers.h>
8#include <arm_def.h>
Yatharth Kocharf9a0f162016-09-13 17:07:57 +01009#include <assert.h>
Dan Handley9df48042015-03-19 18:58:55 +000010#include <bl_common.h>
11#include <console.h>
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010012#include <debug.h>
13#include <desc_image_load.h>
Summer Qin9db8f2e2017-04-24 16:49:28 +010014#ifdef SPD_opteed
15#include <optee_utils.h>
16#endif
Dan Handley9df48042015-03-19 18:58:55 +000017#include <plat_arm.h>
dp-arm7f297ca2017-05-02 11:49:33 +010018#include <platform.h>
Isla Mitchelld2548792017-07-14 10:48:25 +010019#include <platform_def.h>
Dan Handley9df48042015-03-19 18:58:55 +000020#include <string.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000021#include <utils.h>
Dan Handley9df48042015-03-19 18:58:55 +000022
Dan Handley9df48042015-03-19 18:58:55 +000023/* Data structure which holds the extents of the trusted SRAM for BL2 */
24static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
25
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010026/* Weak definitions may be overridden in specific ARM standard platform */
27#pragma weak bl2_early_platform_setup
28#pragma weak bl2_platform_setup
29#pragma weak bl2_plat_arch_setup
30#pragma weak bl2_plat_sec_mem_layout
31
32#if LOAD_IMAGE_V2
33
34#pragma weak bl2_plat_handle_post_image_load
35
36#else /* LOAD_IMAGE_V2 */
Dan Handley9df48042015-03-19 18:58:55 +000037
38/*******************************************************************************
39 * This structure represents the superset of information that is passed to
Juan Castillo7d199412015-12-14 09:35:25 +000040 * BL31, e.g. while passing control to it from BL2, bl31_params
Dan Handley9df48042015-03-19 18:58:55 +000041 * and other platform specific params
42 ******************************************************************************/
43typedef struct bl2_to_bl31_params_mem {
44 bl31_params_t bl31_params;
45 image_info_t bl31_image_info;
46 image_info_t bl32_image_info;
47 image_info_t bl33_image_info;
48 entry_point_info_t bl33_ep_info;
49 entry_point_info_t bl32_ep_info;
50 entry_point_info_t bl31_ep_info;
51} bl2_to_bl31_params_mem_t;
52
53
54static bl2_to_bl31_params_mem_t bl31_params_mem;
55
56
57/* Weak definitions may be overridden in specific ARM standard platform */
Dan Handley9df48042015-03-19 18:58:55 +000058#pragma weak bl2_plat_get_bl31_params
59#pragma weak bl2_plat_get_bl31_ep_info
60#pragma weak bl2_plat_flush_bl31_params
61#pragma weak bl2_plat_set_bl31_ep_info
Juan Castilloa72b6472015-12-10 15:49:17 +000062#pragma weak bl2_plat_get_scp_bl2_meminfo
Dan Handley9df48042015-03-19 18:58:55 +000063#pragma weak bl2_plat_get_bl32_meminfo
64#pragma weak bl2_plat_set_bl32_ep_info
65#pragma weak bl2_plat_get_bl33_meminfo
66#pragma weak bl2_plat_set_bl33_ep_info
67
David Wang0ba499f2016-03-07 11:02:57 +080068#if ARM_BL31_IN_DRAM
69meminfo_t *bl2_plat_sec_mem_layout(void)
70{
71 static meminfo_t bl2_dram_layout
72 __aligned(CACHE_WRITEBACK_GRANULE) = {
73 .total_base = BL31_BASE,
74 .total_size = (ARM_AP_TZC_DRAM1_BASE +
75 ARM_AP_TZC_DRAM1_SIZE) - BL31_BASE,
76 .free_base = BL31_BASE,
77 .free_size = (ARM_AP_TZC_DRAM1_BASE +
78 ARM_AP_TZC_DRAM1_SIZE) - BL31_BASE
79 };
Dan Handley9df48042015-03-19 18:58:55 +000080
David Wang0ba499f2016-03-07 11:02:57 +080081 return &bl2_dram_layout;
82}
83#else
Dan Handley9df48042015-03-19 18:58:55 +000084meminfo_t *bl2_plat_sec_mem_layout(void)
85{
86 return &bl2_tzram_layout;
87}
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010088#endif /* ARM_BL31_IN_DRAM */
Dan Handley9df48042015-03-19 18:58:55 +000089
90/*******************************************************************************
91 * This function assigns a pointer to the memory that the platform has kept
92 * aside to pass platform specific and trusted firmware related information
93 * to BL31. This memory is allocated by allocating memory to
94 * bl2_to_bl31_params_mem_t structure which is a superset of all the
95 * structure whose information is passed to BL31
96 * NOTE: This function should be called only once and should be done
97 * before generating params to BL31
98 ******************************************************************************/
99bl31_params_t *bl2_plat_get_bl31_params(void)
100{
101 bl31_params_t *bl2_to_bl31_params;
102
103 /*
104 * Initialise the memory for all the arguments that needs to
Juan Castillo7d199412015-12-14 09:35:25 +0000105 * be passed to BL31
Dan Handley9df48042015-03-19 18:58:55 +0000106 */
Douglas Raillarda8954fc2017-01-26 15:54:44 +0000107 zeromem(&bl31_params_mem, sizeof(bl2_to_bl31_params_mem_t));
Dan Handley9df48042015-03-19 18:58:55 +0000108
109 /* Assign memory for TF related information */
110 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
111 SET_PARAM_HEAD(bl2_to_bl31_params, PARAM_BL31, VERSION_1, 0);
112
Juan Castillo7d199412015-12-14 09:35:25 +0000113 /* Fill BL31 related information */
Dan Handley9df48042015-03-19 18:58:55 +0000114 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
115 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, PARAM_IMAGE_BINARY,
116 VERSION_1, 0);
117
Juan Castillo7d199412015-12-14 09:35:25 +0000118 /* Fill BL32 related information if it exists */
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100119#ifdef BL32_BASE
Dan Handley9df48042015-03-19 18:58:55 +0000120 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
121 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, PARAM_EP,
122 VERSION_1, 0);
123 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
124 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, PARAM_IMAGE_BINARY,
125 VERSION_1, 0);
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100126#endif /* BL32_BASE */
Dan Handley9df48042015-03-19 18:58:55 +0000127
Juan Castillo7d199412015-12-14 09:35:25 +0000128 /* Fill BL33 related information */
Dan Handley9df48042015-03-19 18:58:55 +0000129 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
130 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_ep_info,
131 PARAM_EP, VERSION_1, 0);
132
Juan Castillo7d199412015-12-14 09:35:25 +0000133 /* BL33 expects to receive the primary CPU MPID (through x0) */
Dan Handley9df48042015-03-19 18:58:55 +0000134 bl2_to_bl31_params->bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
135
136 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
137 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, PARAM_IMAGE_BINARY,
138 VERSION_1, 0);
139
140 return bl2_to_bl31_params;
141}
142
143/* Flush the TF params and the TF plat params */
144void bl2_plat_flush_bl31_params(void)
145{
146 flush_dcache_range((unsigned long)&bl31_params_mem,
147 sizeof(bl2_to_bl31_params_mem_t));
148}
149
150/*******************************************************************************
151 * This function returns a pointer to the shared memory that the platform
152 * has kept to point to entry point information of BL31 to BL2
153 ******************************************************************************/
154struct entry_point_info *bl2_plat_get_bl31_ep_info(void)
155{
156#if DEBUG
157 bl31_params_mem.bl31_ep_info.args.arg1 = ARM_BL31_PLAT_PARAM_VAL;
158#endif
159
160 return &bl31_params_mem.bl31_ep_info;
161}
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100162#endif /* LOAD_IMAGE_V2 */
Dan Handley9df48042015-03-19 18:58:55 +0000163
164/*******************************************************************************
165 * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
166 * in x0. This memory layout is sitting at the base of the free trusted SRAM.
167 * Copy it to a safe location before its reclaimed by later BL2 functionality.
168 ******************************************************************************/
169void arm_bl2_early_platform_setup(meminfo_t *mem_layout)
170{
171 /* Initialize the console to provide early debug support */
172 console_init(PLAT_ARM_BOOT_UART_BASE, PLAT_ARM_BOOT_UART_CLK_IN_HZ,
173 ARM_CONSOLE_BAUDRATE);
174
175 /* Setup the BL2 memory layout */
176 bl2_tzram_layout = *mem_layout;
177
178 /* Initialise the IO layer and register platform IO devices */
179 plat_arm_io_setup();
180}
181
182void bl2_early_platform_setup(meminfo_t *mem_layout)
183{
184 arm_bl2_early_platform_setup(mem_layout);
185}
186
187/*
188 * Perform ARM standard platform setup.
189 */
190void arm_bl2_platform_setup(void)
191{
192 /* Initialize the secure environment */
193 plat_arm_security_setup();
194}
195
196void bl2_platform_setup(void)
197{
198 arm_bl2_platform_setup();
199}
200
201/*******************************************************************************
202 * Perform the very early platform specific architectural setup here. At the
203 * moment this is only initializes the mmu in a quick and dirty way.
204 ******************************************************************************/
205void arm_bl2_plat_arch_setup(void)
206{
Sandrine Bailleux4a1267a2016-05-18 16:11:47 +0100207 arm_setup_page_tables(bl2_tzram_layout.total_base,
Dan Handley9df48042015-03-19 18:58:55 +0000208 bl2_tzram_layout.total_size,
Sandrine Bailleuxecdc4d32016-07-08 14:38:16 +0100209 BL_CODE_BASE,
Masahiro Yamada51bef612017-01-18 02:10:08 +0900210 BL_CODE_END,
Sandrine Bailleuxecdc4d32016-07-08 14:38:16 +0100211 BL_RO_DATA_BASE,
Masahiro Yamada51bef612017-01-18 02:10:08 +0900212 BL_RO_DATA_END
Dan Handley9df48042015-03-19 18:58:55 +0000213#if USE_COHERENT_MEM
Masahiro Yamada0fac5af2016-12-28 16:11:41 +0900214 , BL_COHERENT_RAM_BASE,
215 BL_COHERENT_RAM_END
Dan Handley9df48042015-03-19 18:58:55 +0000216#endif
217 );
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100218
219#ifdef AARCH32
220 enable_mmu_secure(0);
221#else
Sandrine Bailleux4a1267a2016-05-18 16:11:47 +0100222 enable_mmu_el1(0);
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100223#endif
Dan Handley9df48042015-03-19 18:58:55 +0000224}
225
226void bl2_plat_arch_setup(void)
227{
228 arm_bl2_plat_arch_setup();
229}
230
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100231#if LOAD_IMAGE_V2
Yatharth Kocharede39cb2016-11-14 12:01:04 +0000232int arm_bl2_handle_post_image_load(unsigned int image_id)
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100233{
234 int err = 0;
235 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
Summer Qin9db8f2e2017-04-24 16:49:28 +0100236#ifdef SPD_opteed
237 bl_mem_params_node_t *pager_mem_params = NULL;
238 bl_mem_params_node_t *paged_mem_params = NULL;
239#endif
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100240 assert(bl_mem_params);
241
242 switch (image_id) {
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100243#ifdef AARCH64
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100244 case BL32_IMAGE_ID:
Summer Qin9db8f2e2017-04-24 16:49:28 +0100245#ifdef SPD_opteed
246 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
247 assert(pager_mem_params);
248
249 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
250 assert(paged_mem_params);
251
252 err = parse_optee_header(&bl_mem_params->ep_info,
253 &pager_mem_params->image_info,
254 &paged_mem_params->image_info);
255 if (err != 0) {
256 WARN("OPTEE header parse error.\n");
257 }
258#endif
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100259 bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
260 break;
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100261#endif
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100262
263 case BL33_IMAGE_ID:
264 /* BL33 expects to receive the primary CPU MPID (through r0) */
265 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
266 bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
267 break;
268
269#ifdef SCP_BL2_BASE
270 case SCP_BL2_IMAGE_ID:
271 /* The subsequent handling of SCP_BL2 is platform specific */
272 err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
273 if (err) {
274 WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
275 }
276 break;
277#endif
278 }
279
280 return err;
281}
282
Yatharth Kocharede39cb2016-11-14 12:01:04 +0000283/*******************************************************************************
284 * This function can be used by the platforms to update/use image
285 * information for given `image_id`.
286 ******************************************************************************/
287int bl2_plat_handle_post_image_load(unsigned int image_id)
288{
289 return arm_bl2_handle_post_image_load(image_id);
290}
291
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100292#else /* LOAD_IMAGE_V2 */
293
Dan Handley9df48042015-03-19 18:58:55 +0000294/*******************************************************************************
Juan Castilloa72b6472015-12-10 15:49:17 +0000295 * Populate the extents of memory available for loading SCP_BL2 (if used),
Dan Handley9df48042015-03-19 18:58:55 +0000296 * i.e. anywhere in trusted RAM as long as it doesn't overwrite BL2.
297 ******************************************************************************/
Juan Castilloa72b6472015-12-10 15:49:17 +0000298void bl2_plat_get_scp_bl2_meminfo(meminfo_t *scp_bl2_meminfo)
Dan Handley9df48042015-03-19 18:58:55 +0000299{
Juan Castilloa72b6472015-12-10 15:49:17 +0000300 *scp_bl2_meminfo = bl2_tzram_layout;
Dan Handley9df48042015-03-19 18:58:55 +0000301}
302
303/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000304 * Before calling this function BL31 is loaded in memory and its entrypoint
Dan Handley9df48042015-03-19 18:58:55 +0000305 * is set by load_image. This is a placeholder for the platform to change
Juan Castillo7d199412015-12-14 09:35:25 +0000306 * the entrypoint of BL31 and set SPSR and security state.
Dan Handley9df48042015-03-19 18:58:55 +0000307 * On ARM standard platforms we only set the security state of the entrypoint
308 ******************************************************************************/
309void bl2_plat_set_bl31_ep_info(image_info_t *bl31_image_info,
310 entry_point_info_t *bl31_ep_info)
311{
312 SET_SECURITY_STATE(bl31_ep_info->h.attr, SECURE);
313 bl31_ep_info->spsr = SPSR_64(MODE_EL3, MODE_SP_ELX,
314 DISABLE_ALL_EXCEPTIONS);
315}
316
317
318/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000319 * Before calling this function BL32 is loaded in memory and its entrypoint
Dan Handley9df48042015-03-19 18:58:55 +0000320 * is set by load_image. This is a placeholder for the platform to change
Juan Castillo7d199412015-12-14 09:35:25 +0000321 * the entrypoint of BL32 and set SPSR and security state.
Dan Handley9df48042015-03-19 18:58:55 +0000322 * On ARM standard platforms we only set the security state of the entrypoint
323 ******************************************************************************/
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100324#ifdef BL32_BASE
Dan Handley9df48042015-03-19 18:58:55 +0000325void bl2_plat_set_bl32_ep_info(image_info_t *bl32_image_info,
326 entry_point_info_t *bl32_ep_info)
327{
328 SET_SECURITY_STATE(bl32_ep_info->h.attr, SECURE);
329 bl32_ep_info->spsr = arm_get_spsr_for_bl32_entry();
330}
331
332/*******************************************************************************
Dan Handley9df48042015-03-19 18:58:55 +0000333 * Populate the extents of memory available for loading BL32
334 ******************************************************************************/
335void bl2_plat_get_bl32_meminfo(meminfo_t *bl32_meminfo)
336{
337 /*
338 * Populate the extents of memory available for loading BL32.
339 */
340 bl32_meminfo->total_base = BL32_BASE;
341 bl32_meminfo->free_base = BL32_BASE;
342 bl32_meminfo->total_size =
343 (TSP_SEC_MEM_BASE + TSP_SEC_MEM_SIZE) - BL32_BASE;
344 bl32_meminfo->free_size =
345 (TSP_SEC_MEM_BASE + TSP_SEC_MEM_SIZE) - BL32_BASE;
346}
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100347#endif /* BL32_BASE */
Dan Handley9df48042015-03-19 18:58:55 +0000348
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100349/*******************************************************************************
350 * Before calling this function BL33 is loaded in memory and its entrypoint
351 * is set by load_image. This is a placeholder for the platform to change
352 * the entrypoint of BL33 and set SPSR and security state.
353 * On ARM standard platforms we only set the security state of the entrypoint
354 ******************************************************************************/
355void bl2_plat_set_bl33_ep_info(image_info_t *image,
356 entry_point_info_t *bl33_ep_info)
357{
358 SET_SECURITY_STATE(bl33_ep_info->h.attr, NON_SECURE);
359 bl33_ep_info->spsr = arm_get_spsr_for_bl33_entry();
360}
Dan Handley9df48042015-03-19 18:58:55 +0000361
362/*******************************************************************************
363 * Populate the extents of memory available for loading BL33
364 ******************************************************************************/
365void bl2_plat_get_bl33_meminfo(meminfo_t *bl33_meminfo)
366{
367 bl33_meminfo->total_base = ARM_NS_DRAM1_BASE;
368 bl33_meminfo->total_size = ARM_NS_DRAM1_SIZE;
369 bl33_meminfo->free_base = ARM_NS_DRAM1_BASE;
370 bl33_meminfo->free_size = ARM_NS_DRAM1_SIZE;
371}
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100372
373#endif /* LOAD_IMAGE_V2 */