blob: 5f3070809308fb61102cf8e46751da04017389dd [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
David Wang0ba499f2016-03-07 11:02:57 +08002 * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <arch_helpers.h>
32#include <arm_def.h>
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010033#include <assert.h>
Dan Handley9df48042015-03-19 18:58:55 +000034#include <bl_common.h>
35#include <console.h>
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010036#include <debug.h>
37#include <desc_image_load.h>
Dan Handley9df48042015-03-19 18:58:55 +000038#include <plat_arm.h>
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010039#include <platform_def.h>
Dan Handley9df48042015-03-19 18:58:55 +000040#include <string.h>
41
Dan Handley9df48042015-03-19 18:58:55 +000042/* Data structure which holds the extents of the trusted SRAM for BL2 */
43static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
44
Yatharth Kocharf9a0f162016-09-13 17:07:57 +010045/* Weak definitions may be overridden in specific ARM standard platform */
46#pragma weak bl2_early_platform_setup
47#pragma weak bl2_platform_setup
48#pragma weak bl2_plat_arch_setup
49#pragma weak bl2_plat_sec_mem_layout
50
51#if LOAD_IMAGE_V2
52
53#pragma weak bl2_plat_handle_post_image_load
54
55#else /* LOAD_IMAGE_V2 */
Dan Handley9df48042015-03-19 18:58:55 +000056
57/*******************************************************************************
58 * This structure represents the superset of information that is passed to
Juan Castillo7d199412015-12-14 09:35:25 +000059 * BL31, e.g. while passing control to it from BL2, bl31_params
Dan Handley9df48042015-03-19 18:58:55 +000060 * and other platform specific params
61 ******************************************************************************/
62typedef struct bl2_to_bl31_params_mem {
63 bl31_params_t bl31_params;
64 image_info_t bl31_image_info;
65 image_info_t bl32_image_info;
66 image_info_t bl33_image_info;
67 entry_point_info_t bl33_ep_info;
68 entry_point_info_t bl32_ep_info;
69 entry_point_info_t bl31_ep_info;
70} bl2_to_bl31_params_mem_t;
71
72
73static bl2_to_bl31_params_mem_t bl31_params_mem;
74
75
76/* Weak definitions may be overridden in specific ARM standard platform */
Dan Handley9df48042015-03-19 18:58:55 +000077#pragma weak bl2_plat_get_bl31_params
78#pragma weak bl2_plat_get_bl31_ep_info
79#pragma weak bl2_plat_flush_bl31_params
80#pragma weak bl2_plat_set_bl31_ep_info
Juan Castilloa72b6472015-12-10 15:49:17 +000081#pragma weak bl2_plat_get_scp_bl2_meminfo
Dan Handley9df48042015-03-19 18:58:55 +000082#pragma weak bl2_plat_get_bl32_meminfo
83#pragma weak bl2_plat_set_bl32_ep_info
84#pragma weak bl2_plat_get_bl33_meminfo
85#pragma weak bl2_plat_set_bl33_ep_info
86
David Wang0ba499f2016-03-07 11:02:57 +080087#if ARM_BL31_IN_DRAM
88meminfo_t *bl2_plat_sec_mem_layout(void)
89{
90 static meminfo_t bl2_dram_layout
91 __aligned(CACHE_WRITEBACK_GRANULE) = {
92 .total_base = BL31_BASE,
93 .total_size = (ARM_AP_TZC_DRAM1_BASE +
94 ARM_AP_TZC_DRAM1_SIZE) - BL31_BASE,
95 .free_base = BL31_BASE,
96 .free_size = (ARM_AP_TZC_DRAM1_BASE +
97 ARM_AP_TZC_DRAM1_SIZE) - BL31_BASE
98 };
Dan Handley9df48042015-03-19 18:58:55 +000099
David Wang0ba499f2016-03-07 11:02:57 +0800100 return &bl2_dram_layout;
101}
102#else
Dan Handley9df48042015-03-19 18:58:55 +0000103meminfo_t *bl2_plat_sec_mem_layout(void)
104{
105 return &bl2_tzram_layout;
106}
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100107#endif /* ARM_BL31_IN_DRAM */
Dan Handley9df48042015-03-19 18:58:55 +0000108
109/*******************************************************************************
110 * This function assigns a pointer to the memory that the platform has kept
111 * aside to pass platform specific and trusted firmware related information
112 * to BL31. This memory is allocated by allocating memory to
113 * bl2_to_bl31_params_mem_t structure which is a superset of all the
114 * structure whose information is passed to BL31
115 * NOTE: This function should be called only once and should be done
116 * before generating params to BL31
117 ******************************************************************************/
118bl31_params_t *bl2_plat_get_bl31_params(void)
119{
120 bl31_params_t *bl2_to_bl31_params;
121
122 /*
123 * Initialise the memory for all the arguments that needs to
Juan Castillo7d199412015-12-14 09:35:25 +0000124 * be passed to BL31
Dan Handley9df48042015-03-19 18:58:55 +0000125 */
126 memset(&bl31_params_mem, 0, sizeof(bl2_to_bl31_params_mem_t));
127
128 /* Assign memory for TF related information */
129 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
130 SET_PARAM_HEAD(bl2_to_bl31_params, PARAM_BL31, VERSION_1, 0);
131
Juan Castillo7d199412015-12-14 09:35:25 +0000132 /* Fill BL31 related information */
Dan Handley9df48042015-03-19 18:58:55 +0000133 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
134 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, PARAM_IMAGE_BINARY,
135 VERSION_1, 0);
136
Juan Castillo7d199412015-12-14 09:35:25 +0000137 /* Fill BL32 related information if it exists */
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100138#ifdef BL32_BASE
Dan Handley9df48042015-03-19 18:58:55 +0000139 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
140 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, PARAM_EP,
141 VERSION_1, 0);
142 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
143 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, PARAM_IMAGE_BINARY,
144 VERSION_1, 0);
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100145#endif /* BL32_BASE */
Dan Handley9df48042015-03-19 18:58:55 +0000146
Juan Castillo7d199412015-12-14 09:35:25 +0000147 /* Fill BL33 related information */
Dan Handley9df48042015-03-19 18:58:55 +0000148 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
149 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_ep_info,
150 PARAM_EP, VERSION_1, 0);
151
Juan Castillo7d199412015-12-14 09:35:25 +0000152 /* BL33 expects to receive the primary CPU MPID (through x0) */
Dan Handley9df48042015-03-19 18:58:55 +0000153 bl2_to_bl31_params->bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
154
155 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
156 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, PARAM_IMAGE_BINARY,
157 VERSION_1, 0);
158
159 return bl2_to_bl31_params;
160}
161
162/* Flush the TF params and the TF plat params */
163void bl2_plat_flush_bl31_params(void)
164{
165 flush_dcache_range((unsigned long)&bl31_params_mem,
166 sizeof(bl2_to_bl31_params_mem_t));
167}
168
169/*******************************************************************************
170 * This function returns a pointer to the shared memory that the platform
171 * has kept to point to entry point information of BL31 to BL2
172 ******************************************************************************/
173struct entry_point_info *bl2_plat_get_bl31_ep_info(void)
174{
175#if DEBUG
176 bl31_params_mem.bl31_ep_info.args.arg1 = ARM_BL31_PLAT_PARAM_VAL;
177#endif
178
179 return &bl31_params_mem.bl31_ep_info;
180}
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100181#endif /* LOAD_IMAGE_V2 */
Dan Handley9df48042015-03-19 18:58:55 +0000182
183/*******************************************************************************
184 * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
185 * in x0. This memory layout is sitting at the base of the free trusted SRAM.
186 * Copy it to a safe location before its reclaimed by later BL2 functionality.
187 ******************************************************************************/
188void arm_bl2_early_platform_setup(meminfo_t *mem_layout)
189{
190 /* Initialize the console to provide early debug support */
191 console_init(PLAT_ARM_BOOT_UART_BASE, PLAT_ARM_BOOT_UART_CLK_IN_HZ,
192 ARM_CONSOLE_BAUDRATE);
193
194 /* Setup the BL2 memory layout */
195 bl2_tzram_layout = *mem_layout;
196
197 /* Initialise the IO layer and register platform IO devices */
198 plat_arm_io_setup();
199}
200
201void bl2_early_platform_setup(meminfo_t *mem_layout)
202{
203 arm_bl2_early_platform_setup(mem_layout);
204}
205
206/*
207 * Perform ARM standard platform setup.
208 */
209void arm_bl2_platform_setup(void)
210{
211 /* Initialize the secure environment */
212 plat_arm_security_setup();
213}
214
215void bl2_platform_setup(void)
216{
217 arm_bl2_platform_setup();
218}
219
220/*******************************************************************************
221 * Perform the very early platform specific architectural setup here. At the
222 * moment this is only initializes the mmu in a quick and dirty way.
223 ******************************************************************************/
224void arm_bl2_plat_arch_setup(void)
225{
Sandrine Bailleux4a1267a2016-05-18 16:11:47 +0100226 arm_setup_page_tables(bl2_tzram_layout.total_base,
Dan Handley9df48042015-03-19 18:58:55 +0000227 bl2_tzram_layout.total_size,
Sandrine Bailleuxecdc4d32016-07-08 14:38:16 +0100228 BL_CODE_BASE,
Masahiro Yamada51bef612017-01-18 02:10:08 +0900229 BL_CODE_END,
Sandrine Bailleuxecdc4d32016-07-08 14:38:16 +0100230 BL_RO_DATA_BASE,
Masahiro Yamada51bef612017-01-18 02:10:08 +0900231 BL_RO_DATA_END
Dan Handley9df48042015-03-19 18:58:55 +0000232#if USE_COHERENT_MEM
Masahiro Yamada0fac5af2016-12-28 16:11:41 +0900233 , BL_COHERENT_RAM_BASE,
234 BL_COHERENT_RAM_END
Dan Handley9df48042015-03-19 18:58:55 +0000235#endif
236 );
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100237
238#ifdef AARCH32
239 enable_mmu_secure(0);
240#else
Sandrine Bailleux4a1267a2016-05-18 16:11:47 +0100241 enable_mmu_el1(0);
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100242#endif
Dan Handley9df48042015-03-19 18:58:55 +0000243}
244
245void bl2_plat_arch_setup(void)
246{
247 arm_bl2_plat_arch_setup();
248}
249
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100250#if LOAD_IMAGE_V2
251/*******************************************************************************
252 * This function can be used by the platforms to update/use image
253 * information for given `image_id`.
254 ******************************************************************************/
255int bl2_plat_handle_post_image_load(unsigned int image_id)
256{
257 int err = 0;
258 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
259 assert(bl_mem_params);
260
261 switch (image_id) {
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100262#ifdef AARCH64
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100263 case BL32_IMAGE_ID:
264 bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
265 break;
Yatharth Kochara5f77d32016-07-04 11:26:14 +0100266#endif
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100267
268 case BL33_IMAGE_ID:
269 /* BL33 expects to receive the primary CPU MPID (through r0) */
270 bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
271 bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
272 break;
273
274#ifdef SCP_BL2_BASE
275 case SCP_BL2_IMAGE_ID:
276 /* The subsequent handling of SCP_BL2 is platform specific */
277 err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
278 if (err) {
279 WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
280 }
281 break;
282#endif
283 }
284
285 return err;
286}
287
288#else /* LOAD_IMAGE_V2 */
289
Dan Handley9df48042015-03-19 18:58:55 +0000290/*******************************************************************************
Juan Castilloa72b6472015-12-10 15:49:17 +0000291 * Populate the extents of memory available for loading SCP_BL2 (if used),
Dan Handley9df48042015-03-19 18:58:55 +0000292 * i.e. anywhere in trusted RAM as long as it doesn't overwrite BL2.
293 ******************************************************************************/
Juan Castilloa72b6472015-12-10 15:49:17 +0000294void bl2_plat_get_scp_bl2_meminfo(meminfo_t *scp_bl2_meminfo)
Dan Handley9df48042015-03-19 18:58:55 +0000295{
Juan Castilloa72b6472015-12-10 15:49:17 +0000296 *scp_bl2_meminfo = bl2_tzram_layout;
Dan Handley9df48042015-03-19 18:58:55 +0000297}
298
299/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000300 * Before calling this function BL31 is loaded in memory and its entrypoint
Dan Handley9df48042015-03-19 18:58:55 +0000301 * is set by load_image. This is a placeholder for the platform to change
Juan Castillo7d199412015-12-14 09:35:25 +0000302 * the entrypoint of BL31 and set SPSR and security state.
Dan Handley9df48042015-03-19 18:58:55 +0000303 * On ARM standard platforms we only set the security state of the entrypoint
304 ******************************************************************************/
305void bl2_plat_set_bl31_ep_info(image_info_t *bl31_image_info,
306 entry_point_info_t *bl31_ep_info)
307{
308 SET_SECURITY_STATE(bl31_ep_info->h.attr, SECURE);
309 bl31_ep_info->spsr = SPSR_64(MODE_EL3, MODE_SP_ELX,
310 DISABLE_ALL_EXCEPTIONS);
311}
312
313
314/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000315 * Before calling this function BL32 is loaded in memory and its entrypoint
Dan Handley9df48042015-03-19 18:58:55 +0000316 * is set by load_image. This is a placeholder for the platform to change
Juan Castillo7d199412015-12-14 09:35:25 +0000317 * the entrypoint of BL32 and set SPSR and security state.
Dan Handley9df48042015-03-19 18:58:55 +0000318 * On ARM standard platforms we only set the security state of the entrypoint
319 ******************************************************************************/
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100320#ifdef BL32_BASE
Dan Handley9df48042015-03-19 18:58:55 +0000321void bl2_plat_set_bl32_ep_info(image_info_t *bl32_image_info,
322 entry_point_info_t *bl32_ep_info)
323{
324 SET_SECURITY_STATE(bl32_ep_info->h.attr, SECURE);
325 bl32_ep_info->spsr = arm_get_spsr_for_bl32_entry();
326}
327
328/*******************************************************************************
Dan Handley9df48042015-03-19 18:58:55 +0000329 * Populate the extents of memory available for loading BL32
330 ******************************************************************************/
331void bl2_plat_get_bl32_meminfo(meminfo_t *bl32_meminfo)
332{
333 /*
334 * Populate the extents of memory available for loading BL32.
335 */
336 bl32_meminfo->total_base = BL32_BASE;
337 bl32_meminfo->free_base = BL32_BASE;
338 bl32_meminfo->total_size =
339 (TSP_SEC_MEM_BASE + TSP_SEC_MEM_SIZE) - BL32_BASE;
340 bl32_meminfo->free_size =
341 (TSP_SEC_MEM_BASE + TSP_SEC_MEM_SIZE) - BL32_BASE;
342}
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100343#endif /* BL32_BASE */
Dan Handley9df48042015-03-19 18:58:55 +0000344
Antonio Nino Diaze4fa3702016-04-05 11:38:49 +0100345/*******************************************************************************
346 * Before calling this function BL33 is loaded in memory and its entrypoint
347 * is set by load_image. This is a placeholder for the platform to change
348 * the entrypoint of BL33 and set SPSR and security state.
349 * On ARM standard platforms we only set the security state of the entrypoint
350 ******************************************************************************/
351void bl2_plat_set_bl33_ep_info(image_info_t *image,
352 entry_point_info_t *bl33_ep_info)
353{
354 SET_SECURITY_STATE(bl33_ep_info->h.attr, NON_SECURE);
355 bl33_ep_info->spsr = arm_get_spsr_for_bl33_entry();
356}
Dan Handley9df48042015-03-19 18:58:55 +0000357
358/*******************************************************************************
359 * Populate the extents of memory available for loading BL33
360 ******************************************************************************/
361void bl2_plat_get_bl33_meminfo(meminfo_t *bl33_meminfo)
362{
363 bl33_meminfo->total_base = ARM_NS_DRAM1_BASE;
364 bl33_meminfo->total_size = ARM_NS_DRAM1_SIZE;
365 bl33_meminfo->free_base = ARM_NS_DRAM1_BASE;
366 bl33_meminfo->free_size = ARM_NS_DRAM1_SIZE;
367}
Yatharth Kocharf9a0f162016-09-13 17:07:57 +0100368
369#endif /* LOAD_IMAGE_V2 */