blob: 632e84c2d3bb2d2c5da886b61f76c5cb5faea205 [file] [log] [blame]
Dan Handleyb4315302015-03-19 18:58:55 +00001/*
Salman Nabic864af92024-02-19 17:03:44 +00002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Dan Handleyb4315302015-03-19 18:58:55 +00003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handleyb4315302015-03-19 18:58:55 +00005 */
6
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00007#include <assert.h>
8
Dan Handleyb4315302015-03-19 18:58:55 +00009#include <arch.h>
10#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011#include <common/bl_common.h>
12#include <common/debug.h>
13#include <drivers/console.h>
Ambroise Vincent992f0912019-07-12 13:47:03 +010014#include <lib/debugfs.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000015#include <lib/extensions/ras.h>
Harrison Mutaia5566f62023-12-01 15:50:00 +000016#include <lib/fconf/fconf.h>
johpow01f19dc622021-06-16 17:57:28 -050017#include <lib/gpt_rme/gpt_rme.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000018#include <lib/mmio.h>
Harrison Mutaia5566f62023-12-01 15:50:00 +000019#if TRANSFER_LIST
20#include <lib/transfer_list.h>
21#endif
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000022#include <lib/xlat_tables/xlat_tables_compat.h>
Antonio Nino Diazbd9344f2019-01-25 14:30:04 +000023#include <plat/arm/common/plat_arm.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000024#include <plat/common/platform.h>
Antonio Nino Diaz234bc7f2019-01-15 14:19:50 +000025#include <platform_def.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000026
Harrison Mutaia5566f62023-12-01 15:50:00 +000027static struct transfer_list_header *secure_tl __unused;
Dan Handleyb4315302015-03-19 18:58:55 +000028/*
29 * Placeholder variables for copying the arguments that have been passed to
Juan Castillod1786372015-12-14 09:35:25 +000030 * BL31 from BL2.
Dan Handleyb4315302015-03-19 18:58:55 +000031 */
32static entry_point_info_t bl32_image_ep_info;
33static entry_point_info_t bl33_image_ep_info;
Zelalem Aweke9d870b72021-07-11 18:39:39 -050034#if ENABLE_RME
35static entry_point_info_t rmm_image_ep_info;
36#endif
Dan Handleyb4315302015-03-19 18:58:55 +000037
Soby Mathewfc922ca2018-10-14 08:13:44 +010038#if !RESET_TO_BL31
Soby Mathewc099cd32018-06-01 16:53:38 +010039/*
Manish V Badarkhe04e06972020-05-31 10:17:59 +010040 * Check that BL31_BASE is above ARM_FW_CONFIG_LIMIT. The reserved page
Soby Mathewc099cd32018-06-01 16:53:38 +010041 * is required for SOC_FW_CONFIG/TOS_FW_CONFIG passed from BL2.
42 */
Harrison Mutaia5566f62023-12-01 15:50:00 +000043#if TRANSFER_LIST
44CASSERT(BL31_BASE >= PLAT_ARM_EL3_FW_HANDOFF_LIMIT, assert_bl31_base_overflows);
45#else
Manish V Badarkhe04e06972020-05-31 10:17:59 +010046CASSERT(BL31_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl31_base_overflows);
Harrison Mutaia5566f62023-12-01 15:50:00 +000047#endif /* TRANSFER_LIST */
48#endif /* RESET_TO_BL31 */
Dan Handleyb4315302015-03-19 18:58:55 +000049
50/* Weak definitions may be overridden in specific ARM standard platform */
Soby Mathew0c306cc2018-01-10 15:59:31 +000051#pragma weak bl31_early_platform_setup2
Dan Handleyb4315302015-03-19 18:58:55 +000052#pragma weak bl31_platform_setup
53#pragma weak bl31_plat_arch_setup
54#pragma weak bl31_plat_get_next_image_ep_info
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -050055#pragma weak bl31_plat_runtime_setup
Dan Handleyb4315302015-03-19 18:58:55 +000056
Daniel Boulbycb4adb02018-09-18 11:52:49 +010057#define MAP_BL31_TOTAL MAP_REGION_FLAT( \
Soby Mathewfc922ca2018-10-14 08:13:44 +010058 BL31_START, \
59 BL31_END - BL31_START, \
Zelalem Aweke4bb72c42021-07-12 22:33:55 -050060 MT_MEMORY | MT_RW | EL3_PAS)
Daniel Boulbycb4adb02018-09-18 11:52:49 +010061#if RECLAIM_INIT_CODE
62IMPORT_SYM(unsigned long, __INIT_CODE_START__, BL_INIT_CODE_BASE);
Alexei Fedorovfa1fdb22020-07-21 17:07:45 +010063IMPORT_SYM(unsigned long, __INIT_CODE_END__, BL_CODE_END_UNALIGNED);
David Horstmann3ed56062020-10-14 15:17:49 +010064IMPORT_SYM(unsigned long, __STACKS_END__, BL_STACKS_END_UNALIGNED);
Alexei Fedorovfa1fdb22020-07-21 17:07:45 +010065
66#define BL_INIT_CODE_END ((BL_CODE_END_UNALIGNED + PAGE_SIZE - 1) & \
67 ~(PAGE_SIZE - 1))
David Horstmann3ed56062020-10-14 15:17:49 +010068#define BL_STACKS_END ((BL_STACKS_END_UNALIGNED + PAGE_SIZE - 1) & \
69 ~(PAGE_SIZE - 1))
Daniel Boulbycb4adb02018-09-18 11:52:49 +010070
71#define MAP_BL_INIT_CODE MAP_REGION_FLAT( \
72 BL_INIT_CODE_BASE, \
73 BL_INIT_CODE_END \
74 - BL_INIT_CODE_BASE, \
Zelalem Aweke4bb72c42021-07-12 22:33:55 -050075 MT_CODE | EL3_PAS)
Daniel Boulbycb4adb02018-09-18 11:52:49 +010076#endif
Dan Handleyb4315302015-03-19 18:58:55 +000077
Madhukar Pappireddy0c1f1972020-01-27 15:38:26 -060078#if SEPARATE_NOBITS_REGION
79#define MAP_BL31_NOBITS MAP_REGION_FLAT( \
80 BL31_NOBITS_BASE, \
81 BL31_NOBITS_LIMIT \
82 - BL31_NOBITS_BASE, \
Zelalem Aweke4bb72c42021-07-12 22:33:55 -050083 MT_MEMORY | MT_RW | EL3_PAS)
Madhukar Pappireddy0c1f1972020-01-27 15:38:26 -060084
85#endif
Dan Handleyb4315302015-03-19 18:58:55 +000086/*******************************************************************************
87 * Return a pointer to the 'entry_point_info' structure of the next image for the
Juan Castillod1786372015-12-14 09:35:25 +000088 * security state specified. BL33 corresponds to the non-secure image type
89 * while BL32 corresponds to the secure image type. A NULL pointer is returned
Dan Handleyb4315302015-03-19 18:58:55 +000090 * if the image does not exist.
91 ******************************************************************************/
Sandrine Bailleux6c77e742018-07-11 12:44:22 +020092struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type)
Dan Handleyb4315302015-03-19 18:58:55 +000093{
94 entry_point_info_t *next_image_info;
95
96 assert(sec_state_is_valid(type));
Zelalem Aweke9d870b72021-07-11 18:39:39 -050097 if (type == NON_SECURE) {
98 next_image_info = &bl33_image_ep_info;
99 }
100#if ENABLE_RME
101 else if (type == REALM) {
102 next_image_info = &rmm_image_ep_info;
103 }
104#endif
105 else {
106 next_image_info = &bl32_image_ep_info;
107 }
108
Dan Handleyb4315302015-03-19 18:58:55 +0000109 /*
110 * None of the images on the ARM development platforms can have 0x0
111 * as the entrypoint
112 */
113 if (next_image_info->pc)
114 return next_image_info;
115 else
116 return NULL;
117}
118
119/*******************************************************************************
Juan Castillod1786372015-12-14 09:35:25 +0000120 * Perform any BL31 early platform setup common to ARM standard platforms.
Dan Handleyb4315302015-03-19 18:58:55 +0000121 * Here is an opportunity to copy parameters passed by the calling EL (S-EL1
John Tsichritzisa6238322018-09-14 10:34:57 +0100122 * in BL2 & EL3 in BL1) before they are lost (potentially). This needs to be
Dan Handleyb4315302015-03-19 18:58:55 +0000123 * done before the MMU is initialized so that the memory layout can be used
124 * while creating page tables. BL2 has flushed this information to memory, so
125 * we are guaranteed to pick up good data.
126 ******************************************************************************/
Harrison Mutaia5566f62023-12-01 15:50:00 +0000127#if TRANSFER_LIST
128void __init arm_bl31_early_platform_setup(u_register_t arg0, u_register_t arg1,
129 u_register_t arg2, u_register_t arg3)
130{
Harrison Mutai1a0ebff2024-05-02 12:40:20 +0000131#if RESET_TO_BL31
132 /* Populate entry point information for BL33 */
133 SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
134 /*
135 * Tell BL31 where the non-trusted software image
136 * is located and the entry state information
137 */
138 bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
139
140 bl33_image_ep_info.spsr = arm_get_spsr_for_bl33_entry();
141 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
142
143 bl33_image_ep_info.args.arg0 =
144 FW_NS_HANDOFF_BASE + ARM_PRELOADED_DTB_OFFSET;
145 bl33_image_ep_info.args.arg1 = TRANSFER_LIST_SIGNATURE |
146 REGISTER_CONVENTION_VERSION_MASK;
147 bl33_image_ep_info.args.arg3 = FW_NS_HANDOFF_BASE;
148#else
Harrison Mutaia5566f62023-12-01 15:50:00 +0000149 struct transfer_list_entry *te = NULL;
150 struct entry_point_info *ep;
151
152 secure_tl = (struct transfer_list_header *)arg3;
153
154 /*
155 * Populate the global entry point structures used to execute subsequent
156 * images.
157 */
158 while ((te = transfer_list_next(secure_tl, te)) != NULL) {
159 ep = transfer_list_entry_data(te);
160
161 if (te->tag_id == TL_TAG_EXEC_EP_INFO64) {
162 switch (GET_SECURITY_STATE(ep->h.attr)) {
163 case NON_SECURE:
164 bl33_image_ep_info = *ep;
165 break;
166#if ENABLE_RME
167 case REALM:
168 rmm_image_ep_info = *ep;
169 break;
170#endif
171 case SECURE:
172 bl32_image_ep_info = *ep;
173 break;
174 default:
175 ERROR("Unrecognized Image Security State %lu\n",
176 GET_SECURITY_STATE(ep->h.attr));
177 panic();
178 }
179 }
180 }
Harrison Mutai1a0ebff2024-05-02 12:40:20 +0000181#endif /* RESET_TO_BL31 */
Harrison Mutaia5566f62023-12-01 15:50:00 +0000182}
183#else
Daniel Boulby4d010d02018-09-18 13:26:03 +0100184void __init arm_bl31_early_platform_setup(void *from_bl2, uintptr_t soc_fw_config,
Soby Mathew0c306cc2018-01-10 15:59:31 +0000185 uintptr_t hw_config, void *plat_params_from_bl2)
Dan Handleyb4315302015-03-19 18:58:55 +0000186{
187 /* Initialize the console to provide early debug support */
Antonio Nino Diaz88a05232018-06-19 09:29:36 +0100188 arm_console_boot_init();
Dan Handleyb4315302015-03-19 18:58:55 +0000189
190#if RESET_TO_BL31
Juan Castillod1786372015-12-14 09:35:25 +0000191 /* There are no parameters from BL2 if BL31 is a reset vector */
Dan Handleyb4315302015-03-19 18:58:55 +0000192 assert(from_bl2 == NULL);
193 assert(plat_params_from_bl2 == NULL);
194
Antonio Nino Diazb726c162018-05-11 11:15:10 +0100195# ifdef BL32_BASE
Juan Castillod1786372015-12-14 09:35:25 +0000196 /* Populate entry point information for BL32 */
Dan Handleyb4315302015-03-19 18:58:55 +0000197 SET_PARAM_HEAD(&bl32_image_ep_info,
198 PARAM_EP,
199 VERSION_1,
200 0);
201 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
202 bl32_image_ep_info.pc = BL32_BASE;
203 bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
Manish Pandeycc9cb292020-07-16 00:38:59 +0100204
205#if defined(SPD_spmd)
206 /* SPM (hafnium in secure world) expects SPM Core manifest base address
207 * in x0, which in !RESET_TO_BL31 case loaded after base of non shared
208 * SRAM(after 4KB offset of SRAM). But in RESET_TO_BL31 case all non
209 * shared SRAM is allocated to BL31, so to avoid overwriting of manifest
210 * keep it in the last page.
211 */
212 bl32_image_ep_info.args.arg0 = ARM_TRUSTED_SRAM_BASE +
213 PLAT_ARM_TRUSTED_SRAM_SIZE - PAGE_SIZE;
214#endif
215
Antonio Nino Diazb726c162018-05-11 11:15:10 +0100216# endif /* BL32_BASE */
Dan Handleyb4315302015-03-19 18:58:55 +0000217
Juan Castillod1786372015-12-14 09:35:25 +0000218 /* Populate entry point information for BL33 */
Dan Handleyb4315302015-03-19 18:58:55 +0000219 SET_PARAM_HEAD(&bl33_image_ep_info,
220 PARAM_EP,
221 VERSION_1,
222 0);
223 /*
Juan Castillod1786372015-12-14 09:35:25 +0000224 * Tell BL31 where the non-trusted software image
Dan Handleyb4315302015-03-19 18:58:55 +0000225 * is located and the entry state information
226 */
227 bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
Soby Mathew48ac1df2016-05-09 17:20:10 +0100228
Dan Handleyb4315302015-03-19 18:58:55 +0000229 bl33_image_ep_info.spsr = arm_get_spsr_for_bl33_entry();
230 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
231
Javier Almansa Sobrino8c980a42021-11-24 18:37:37 +0000232#if ENABLE_RME
233 /*
234 * Populate entry point information for RMM.
235 * Only PC needs to be set as other fields are determined by RMMD.
236 */
237 rmm_image_ep_info.pc = RMM_BASE;
238#endif /* ENABLE_RME */
239
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100240#else /* RESET_TO_BL31 */
241
Dan Handleyb4315302015-03-19 18:58:55 +0000242 /*
243 * In debug builds, we pass a special value in 'plat_params_from_bl2'
Juan Castillod1786372015-12-14 09:35:25 +0000244 * to verify platform parameters from BL2 to BL31.
Dan Handleyb4315302015-03-19 18:58:55 +0000245 * In release builds, it's not used.
246 */
247 assert(((unsigned long long)plat_params_from_bl2) ==
248 ARM_BL31_PLAT_PARAM_VAL);
249
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100250 /*
251 * Check params passed from BL2 should not be NULL,
252 */
253 bl_params_t *params_from_bl2 = (bl_params_t *)from_bl2;
254 assert(params_from_bl2 != NULL);
255 assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
256 assert(params_from_bl2->h.version >= VERSION_2);
257
258 bl_params_node_t *bl_params = params_from_bl2->head;
259
260 /*
Zelalem Aweke9d870b72021-07-11 18:39:39 -0500261 * Copy BL33, BL32 and RMM (if present), entry point information.
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100262 * They are stored in Secure RAM, in BL2's address space.
263 */
Antonio Nino Diazc9512bc2018-08-24 16:30:29 +0100264 while (bl_params != NULL) {
Zelalem Aweke9d870b72021-07-11 18:39:39 -0500265 if (bl_params->image_id == BL32_IMAGE_ID) {
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100266 bl32_image_ep_info = *bl_params->ep_info;
Manish V Badarkhea0ef1c02023-11-08 09:30:18 +0000267#if SPMC_AT_EL3
Nishant Sharma821b01f2023-10-13 11:22:08 +0100268 /*
269 * Populate the BL32 image base, size and max limit in
270 * the entry point information, since there is no
271 * platform function to retrieve them in generic
272 * code. We choose arg2, arg3 and arg4 since the generic
273 * code uses arg1 for stashing the SP manifest size. The
274 * SPMC setup uses these arguments to update SP manifest
275 * with actual SP's base address and it size.
276 */
277 bl32_image_ep_info.args.arg2 =
278 bl_params->image_info->image_base;
279 bl32_image_ep_info.args.arg3 =
280 bl_params->image_info->image_size;
281 bl32_image_ep_info.args.arg4 =
282 bl_params->image_info->image_base +
283 bl_params->image_info->image_max_size;
284#endif
Zelalem Aweke9d870b72021-07-11 18:39:39 -0500285 }
286#if ENABLE_RME
287 else if (bl_params->image_id == RMM_IMAGE_ID) {
288 rmm_image_ep_info = *bl_params->ep_info;
289 }
290#endif
291 else if (bl_params->image_id == BL33_IMAGE_ID) {
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100292 bl33_image_ep_info = *bl_params->ep_info;
Zelalem Aweke9d870b72021-07-11 18:39:39 -0500293 }
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100294
295 bl_params = bl_params->next_params_info;
296 }
297
Antonio Nino Diazc9512bc2018-08-24 16:30:29 +0100298 if (bl33_image_ep_info.pc == 0U)
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100299 panic();
Zelalem Aweke9d870b72021-07-11 18:39:39 -0500300#if ENABLE_RME
301 if (rmm_image_ep_info.pc == 0U)
302 panic();
303#endif
Yatharth Kochara8aa7fe2016-09-13 17:07:57 +0100304#endif /* RESET_TO_BL31 */
Andre Przywarac99b8c82021-02-08 17:40:17 +0000305
306# if ARM_LINUX_KERNEL_AS_BL33
307 /*
308 * According to the file ``Documentation/arm64/booting.txt`` of the
309 * Linux kernel tree, Linux expects the physical address of the device
310 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
311 * must be 0.
Olivier Deprez3221fce2021-10-20 15:17:07 +0200312 * Repurpose the option to load Hafnium hypervisor in the normal world.
313 * It expects its manifest address in x0. This is essentially the linux
314 * dts (passed to the primary VM) by adding 'hypervisor' and chosen
315 * nodes specifying the Hypervisor configuration.
Andre Przywarac99b8c82021-02-08 17:40:17 +0000316 */
Zelalem Aweke672d6692021-07-26 21:39:05 -0500317#if RESET_TO_BL31
Andre Przywarac99b8c82021-02-08 17:40:17 +0000318 bl33_image_ep_info.args.arg0 = (u_register_t)ARM_PRELOADED_DTB_BASE;
Zelalem Aweke672d6692021-07-26 21:39:05 -0500319#else
320 bl33_image_ep_info.args.arg0 = (u_register_t)hw_config;
321#endif
Andre Przywarac99b8c82021-02-08 17:40:17 +0000322 bl33_image_ep_info.args.arg1 = 0U;
323 bl33_image_ep_info.args.arg2 = 0U;
324 bl33_image_ep_info.args.arg3 = 0U;
325# endif
Dan Handleyb4315302015-03-19 18:58:55 +0000326}
Harrison Mutaia5566f62023-12-01 15:50:00 +0000327#endif
Dan Handleyb4315302015-03-19 18:58:55 +0000328
Soby Mathew0c306cc2018-01-10 15:59:31 +0000329void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
330 u_register_t arg2, u_register_t arg3)
Dan Handleyb4315302015-03-19 18:58:55 +0000331{
Harrison Mutaia5566f62023-12-01 15:50:00 +0000332#if TRANSFER_LIST
333 arm_bl31_early_platform_setup(arg0, arg1, arg2, arg3);
334#else
Soby Mathew0c306cc2018-01-10 15:59:31 +0000335 arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
Harrison Mutaia5566f62023-12-01 15:50:00 +0000336#endif
Dan Handleyb4315302015-03-19 18:58:55 +0000337
338 /*
Vikram Kanigiri6355f232016-02-15 11:54:14 +0000339 * Initialize Interconnect for this cluster during cold boot.
Dan Handleyb4315302015-03-19 18:58:55 +0000340 * No need for locks as no other CPU is active.
341 */
Vikram Kanigiri6355f232016-02-15 11:54:14 +0000342 plat_arm_interconnect_init();
Sandrine Bailleuxa6695272015-05-14 14:13:05 +0100343
Dan Handleyb4315302015-03-19 18:58:55 +0000344 /*
Vikram Kanigiri6355f232016-02-15 11:54:14 +0000345 * Enable Interconnect coherency for the primary CPU's cluster.
Sandrine Bailleuxa6695272015-05-14 14:13:05 +0100346 * Earlier bootloader stages might already do this (e.g. Trusted
347 * Firmware's BL1 does it) but we can't assume so. There is no harm in
348 * executing this code twice anyway.
Dan Handleyb4315302015-03-19 18:58:55 +0000349 * Platform specific PSCI code will enable coherency for other
350 * clusters.
351 */
Vikram Kanigiri6355f232016-02-15 11:54:14 +0000352 plat_arm_interconnect_enter_coherency();
Dan Handleyb4315302015-03-19 18:58:55 +0000353}
354
355/*******************************************************************************
Juan Castillod1786372015-12-14 09:35:25 +0000356 * Perform any BL31 platform setup common to ARM standard platforms
Dan Handleyb4315302015-03-19 18:58:55 +0000357 ******************************************************************************/
358void arm_bl31_platform_setup(void)
359{
Achin Gupta27573c52015-11-03 14:18:34 +0000360 /* Initialize the GIC driver, cpu and distributor interfaces */
361 plat_arm_gic_driver_init();
Dan Handleyb4315302015-03-19 18:58:55 +0000362 plat_arm_gic_init();
Dan Handleyb4315302015-03-19 18:58:55 +0000363
364#if RESET_TO_BL31
365 /*
366 * Do initial security configuration to allow DRAM/device access
367 * (if earlier BL has not already done so).
368 */
369 plat_arm_security_setup();
370
Roberto Vargas638b0342018-01-05 16:00:05 +0000371#if defined(PLAT_ARM_MEM_PROT_ADDR)
372 arm_nor_psci_do_dyn_mem_protect();
373#endif /* PLAT_ARM_MEM_PROT_ADDR */
374
Dan Handleyb4315302015-03-19 18:58:55 +0000375#endif /* RESET_TO_BL31 */
376
377 /* Enable and initialize the System level generic timer */
378 mmio_write_32(ARM_SYS_CNTCTL_BASE + CNTCR_OFF,
Antonio Nino Diazc9512bc2018-08-24 16:30:29 +0100379 CNTCR_FCREQ(0U) | CNTCR_EN);
Dan Handleyb4315302015-03-19 18:58:55 +0000380
381 /* Allow access to the System counter timer module */
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100382 arm_configure_sys_timer();
Dan Handleyb4315302015-03-19 18:58:55 +0000383
384 /* Initialize power controller before setting up topology */
385 plat_arm_pwrc_setup();
Jeenu Viswambharan0b9ce902018-02-06 12:21:39 +0000386
Manish Pandeyf87e54f2023-10-10 15:42:19 +0100387#if ENABLE_FEAT_RAS && FFH_SUPPORT
Jeenu Viswambharan0b9ce902018-02-06 12:21:39 +0000388 ras_init();
389#endif
Ambroise Vincent992f0912019-07-12 13:47:03 +0100390
391#if USE_DEBUGFS
392 debugfs_init();
393#endif /* USE_DEBUGFS */
Dan Handleyb4315302015-03-19 18:58:55 +0000394}
395
Soby Mathew080225d2015-12-09 11:38:43 +0000396/*******************************************************************************
Juan Castillod1786372015-12-14 09:35:25 +0000397 * Perform any BL31 platform runtime setup prior to BL31 exit common to ARM
Soby Mathew080225d2015-12-09 11:38:43 +0000398 * standard platforms
399 ******************************************************************************/
400void arm_bl31_plat_runtime_setup(void)
401{
402 /* Initialize the runtime console */
Antonio Nino Diaz88a05232018-06-19 09:29:36 +0100403 arm_console_runtime_init();
Petre-Ionut Tudor60e8f3c2019-11-07 15:18:03 +0000404
Daniel Boulbycb4adb02018-09-18 11:52:49 +0100405#if RECLAIM_INIT_CODE
406 arm_free_init_memory();
407#endif
Petre-Ionut Tudor60e8f3c2019-11-07 15:18:03 +0000408
409#if PLAT_RO_XLAT_TABLES
410 arm_xlat_make_tables_readonly();
411#endif
Soby Mathew080225d2015-12-09 11:38:43 +0000412}
413
Daniel Boulbycb4adb02018-09-18 11:52:49 +0100414#if RECLAIM_INIT_CODE
415/*
David Horstmann3ed56062020-10-14 15:17:49 +0100416 * Make memory for image boot time code RW to reclaim it as stack for the
417 * secondary cores, or RO where it cannot be reclaimed:
418 *
419 * |-------- INIT SECTION --------|
420 * -----------------------------------------
421 * | CORE 0 | CORE 1 | CORE 2 | EXTRA |
422 * | STACK | STACK | STACK | SPACE |
423 * -----------------------------------------
424 * <-------------------> <------>
425 * MAKE RW AND XN MAKE
426 * FOR STACKS RO AND XN
Daniel Boulbycb4adb02018-09-18 11:52:49 +0100427 */
428void arm_free_init_memory(void)
429{
David Horstmann3ed56062020-10-14 15:17:49 +0100430 int ret = 0;
431
432 if (BL_STACKS_END < BL_INIT_CODE_END) {
433 /* Reclaim some of the init section as stack if possible. */
434 if (BL_INIT_CODE_BASE < BL_STACKS_END) {
435 ret |= xlat_change_mem_attributes(BL_INIT_CODE_BASE,
436 BL_STACKS_END - BL_INIT_CODE_BASE,
437 MT_RW_DATA);
438 }
439 /* Make the rest of the init section read-only. */
440 ret |= xlat_change_mem_attributes(BL_STACKS_END,
441 BL_INIT_CODE_END - BL_STACKS_END,
442 MT_RO_DATA);
443 } else {
444 /* The stacks cover the init section, so reclaim it all. */
445 ret |= xlat_change_mem_attributes(BL_INIT_CODE_BASE,
Daniel Boulbycb4adb02018-09-18 11:52:49 +0100446 BL_INIT_CODE_END - BL_INIT_CODE_BASE,
447 MT_RW_DATA);
David Horstmann3ed56062020-10-14 15:17:49 +0100448 }
Daniel Boulbycb4adb02018-09-18 11:52:49 +0100449
450 if (ret != 0) {
451 ERROR("Could not reclaim initialization code");
452 panic();
453 }
454}
455#endif
456
Daniel Boulby4d010d02018-09-18 13:26:03 +0100457void __init bl31_platform_setup(void)
Dan Handleyb4315302015-03-19 18:58:55 +0000458{
459 arm_bl31_platform_setup();
460}
461
Soby Mathew080225d2015-12-09 11:38:43 +0000462void bl31_plat_runtime_setup(void)
463{
464 arm_bl31_plat_runtime_setup();
465}
466
Dan Handleyb4315302015-03-19 18:58:55 +0000467/*******************************************************************************
Sandrine Bailleuxb5fa6562016-05-18 16:11:47 +0100468 * Perform the very early platform specific architectural setup shared between
469 * ARM standard platforms. This only does basic initialization. Later
470 * architectural setup (bl31_arch_setup()) does not do anything platform
471 * specific.
Dan Handleyb4315302015-03-19 18:58:55 +0000472 ******************************************************************************/
Daniel Boulby4d010d02018-09-18 13:26:03 +0100473void __init arm_bl31_plat_arch_setup(void)
Dan Handleyb4315302015-03-19 18:58:55 +0000474{
Daniel Boulbyd323af92018-07-06 16:54:44 +0100475 const mmap_region_t bl_regions[] = {
476 MAP_BL31_TOTAL,
Zelalem Awekec8720722021-07-12 23:41:05 -0500477#if ENABLE_RME
478 ARM_MAP_L0_GPT_REGION,
479#endif
Daniel Boulbycb4adb02018-09-18 11:52:49 +0100480#if RECLAIM_INIT_CODE
481 MAP_BL_INIT_CODE,
482#endif
Madhukar Pappireddy0c1f1972020-01-27 15:38:26 -0600483#if SEPARATE_NOBITS_REGION
484 MAP_BL31_NOBITS,
485#endif
Daniel Boulby2ecaafd2018-07-16 14:09:15 +0100486 ARM_MAP_BL_RO,
Roberto Vargas1eb735d2018-05-23 09:27:06 +0100487#if USE_ROMLIB
488 ARM_MAP_ROMLIB_CODE,
489 ARM_MAP_ROMLIB_DATA,
490#endif
Dan Handleyb4315302015-03-19 18:58:55 +0000491#if USE_COHERENT_MEM
Daniel Boulbyd323af92018-07-06 16:54:44 +0100492 ARM_MAP_BL_COHERENT_RAM,
Dan Handleyb4315302015-03-19 18:58:55 +0000493#endif
Daniel Boulbyd323af92018-07-06 16:54:44 +0100494 {0}
495 };
496
Roberto Vargas0916c382018-10-19 16:44:18 +0100497 setup_page_tables(bl_regions, plat_arm_get_mmap());
Daniel Boulbyd323af92018-07-06 16:54:44 +0100498
Sandrine Bailleuxb5fa6562016-05-18 16:11:47 +0100499 enable_mmu_el3(0);
Roberto Vargas1eb735d2018-05-23 09:27:06 +0100500
johpow01f19dc622021-06-16 17:57:28 -0500501#if ENABLE_RME
502 /*
503 * Initialise Granule Protection library and enable GPC for the primary
504 * processor. The tables have already been initialized by a previous BL
505 * stage, so there is no need to provide any PAS here. This function
506 * sets up pointers to those tables.
507 */
508 if (gpt_runtime_init() < 0) {
509 ERROR("gpt_runtime_init() failed!\n");
510 panic();
511 }
512#endif /* ENABLE_RME */
513
Roberto Vargas1eb735d2018-05-23 09:27:06 +0100514 arm_setup_romlib();
Dan Handleyb4315302015-03-19 18:58:55 +0000515}
516
Daniel Boulby4d010d02018-09-18 13:26:03 +0100517void __init bl31_plat_arch_setup(void)
Dan Handleyb4315302015-03-19 18:58:55 +0000518{
Harrison Mutaia5566f62023-12-01 15:50:00 +0000519 struct transfer_list_entry *te __unused;
520
Dan Handleyb4315302015-03-19 18:58:55 +0000521 arm_bl31_plat_arch_setup();
Harrison Mutaia5566f62023-12-01 15:50:00 +0000522
Harrison Mutai1a0ebff2024-05-02 12:40:20 +0000523#if TRANSFER_LIST && !(RESET_TO_BL2 || RESET_TO_BL31)
Harrison Mutaia5566f62023-12-01 15:50:00 +0000524 te = transfer_list_find(secure_tl, TL_TAG_FDT);
525 assert(te != NULL);
526
527 /* Populate HW_CONFIG device tree with the mapped address */
528 fconf_populate("HW_CONFIG", (uintptr_t)transfer_list_entry_data(te));
529#endif
Dan Handleyb4315302015-03-19 18:58:55 +0000530}