xref: /arm-trusted-firmware/plat/arm/common/arm_bl31_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <arch.h>
10*91f16700Schasinglulu #include <arch_helpers.h>
11*91f16700Schasinglulu #include <common/bl_common.h>
12*91f16700Schasinglulu #include <common/debug.h>
13*91f16700Schasinglulu #include <drivers/console.h>
14*91f16700Schasinglulu #include <lib/debugfs.h>
15*91f16700Schasinglulu #include <lib/extensions/ras.h>
16*91f16700Schasinglulu #include <lib/gpt_rme/gpt_rme.h>
17*91f16700Schasinglulu #include <lib/mmio.h>
18*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_compat.h>
19*91f16700Schasinglulu #include <plat/arm/common/plat_arm.h>
20*91f16700Schasinglulu #include <plat/common/platform.h>
21*91f16700Schasinglulu #include <platform_def.h>
22*91f16700Schasinglulu 
23*91f16700Schasinglulu /*
24*91f16700Schasinglulu  * Placeholder variables for copying the arguments that have been passed to
25*91f16700Schasinglulu  * BL31 from BL2.
26*91f16700Schasinglulu  */
27*91f16700Schasinglulu static entry_point_info_t bl32_image_ep_info;
28*91f16700Schasinglulu static entry_point_info_t bl33_image_ep_info;
29*91f16700Schasinglulu #if ENABLE_RME
30*91f16700Schasinglulu static entry_point_info_t rmm_image_ep_info;
31*91f16700Schasinglulu #endif
32*91f16700Schasinglulu 
33*91f16700Schasinglulu #if !RESET_TO_BL31
34*91f16700Schasinglulu /*
35*91f16700Schasinglulu  * Check that BL31_BASE is above ARM_FW_CONFIG_LIMIT. The reserved page
36*91f16700Schasinglulu  * is required for SOC_FW_CONFIG/TOS_FW_CONFIG passed from BL2.
37*91f16700Schasinglulu  */
38*91f16700Schasinglulu CASSERT(BL31_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl31_base_overflows);
39*91f16700Schasinglulu #endif
40*91f16700Schasinglulu 
41*91f16700Schasinglulu /* Weak definitions may be overridden in specific ARM standard platform */
42*91f16700Schasinglulu #pragma weak bl31_early_platform_setup2
43*91f16700Schasinglulu #pragma weak bl31_platform_setup
44*91f16700Schasinglulu #pragma weak bl31_plat_arch_setup
45*91f16700Schasinglulu #pragma weak bl31_plat_get_next_image_ep_info
46*91f16700Schasinglulu #pragma weak bl31_plat_runtime_setup
47*91f16700Schasinglulu 
48*91f16700Schasinglulu #define MAP_BL31_TOTAL		MAP_REGION_FLAT(			\
49*91f16700Schasinglulu 					BL31_START,			\
50*91f16700Schasinglulu 					BL31_END - BL31_START,		\
51*91f16700Schasinglulu 					MT_MEMORY | MT_RW | EL3_PAS)
52*91f16700Schasinglulu #if RECLAIM_INIT_CODE
53*91f16700Schasinglulu IMPORT_SYM(unsigned long, __INIT_CODE_START__, BL_INIT_CODE_BASE);
54*91f16700Schasinglulu IMPORT_SYM(unsigned long, __INIT_CODE_END__, BL_CODE_END_UNALIGNED);
55*91f16700Schasinglulu IMPORT_SYM(unsigned long, __STACKS_END__, BL_STACKS_END_UNALIGNED);
56*91f16700Schasinglulu 
57*91f16700Schasinglulu #define	BL_INIT_CODE_END	((BL_CODE_END_UNALIGNED + PAGE_SIZE - 1) & \
58*91f16700Schasinglulu 					~(PAGE_SIZE - 1))
59*91f16700Schasinglulu #define	BL_STACKS_END		((BL_STACKS_END_UNALIGNED + PAGE_SIZE - 1) & \
60*91f16700Schasinglulu 					~(PAGE_SIZE - 1))
61*91f16700Schasinglulu 
62*91f16700Schasinglulu #define MAP_BL_INIT_CODE	MAP_REGION_FLAT(			\
63*91f16700Schasinglulu 					BL_INIT_CODE_BASE,		\
64*91f16700Schasinglulu 					BL_INIT_CODE_END		\
65*91f16700Schasinglulu 						- BL_INIT_CODE_BASE,	\
66*91f16700Schasinglulu 					MT_CODE | EL3_PAS)
67*91f16700Schasinglulu #endif
68*91f16700Schasinglulu 
69*91f16700Schasinglulu #if SEPARATE_NOBITS_REGION
70*91f16700Schasinglulu #define MAP_BL31_NOBITS		MAP_REGION_FLAT(			\
71*91f16700Schasinglulu 					BL31_NOBITS_BASE,		\
72*91f16700Schasinglulu 					BL31_NOBITS_LIMIT 		\
73*91f16700Schasinglulu 						- BL31_NOBITS_BASE,	\
74*91f16700Schasinglulu 					MT_MEMORY | MT_RW | EL3_PAS)
75*91f16700Schasinglulu 
76*91f16700Schasinglulu #endif
77*91f16700Schasinglulu /*******************************************************************************
78*91f16700Schasinglulu  * Return a pointer to the 'entry_point_info' structure of the next image for the
79*91f16700Schasinglulu  * security state specified. BL33 corresponds to the non-secure image type
80*91f16700Schasinglulu  * while BL32 corresponds to the secure image type. A NULL pointer is returned
81*91f16700Schasinglulu  * if the image does not exist.
82*91f16700Schasinglulu  ******************************************************************************/
83*91f16700Schasinglulu struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type)
84*91f16700Schasinglulu {
85*91f16700Schasinglulu 	entry_point_info_t *next_image_info;
86*91f16700Schasinglulu 
87*91f16700Schasinglulu 	assert(sec_state_is_valid(type));
88*91f16700Schasinglulu 	if (type == NON_SECURE) {
89*91f16700Schasinglulu 		next_image_info = &bl33_image_ep_info;
90*91f16700Schasinglulu 	}
91*91f16700Schasinglulu #if ENABLE_RME
92*91f16700Schasinglulu 	else if (type == REALM) {
93*91f16700Schasinglulu 		next_image_info = &rmm_image_ep_info;
94*91f16700Schasinglulu 	}
95*91f16700Schasinglulu #endif
96*91f16700Schasinglulu 	else {
97*91f16700Schasinglulu 		next_image_info = &bl32_image_ep_info;
98*91f16700Schasinglulu 	}
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	/*
101*91f16700Schasinglulu 	 * None of the images on the ARM development platforms can have 0x0
102*91f16700Schasinglulu 	 * as the entrypoint
103*91f16700Schasinglulu 	 */
104*91f16700Schasinglulu 	if (next_image_info->pc)
105*91f16700Schasinglulu 		return next_image_info;
106*91f16700Schasinglulu 	else
107*91f16700Schasinglulu 		return NULL;
108*91f16700Schasinglulu }
109*91f16700Schasinglulu 
110*91f16700Schasinglulu /*******************************************************************************
111*91f16700Schasinglulu  * Perform any BL31 early platform setup common to ARM standard platforms.
112*91f16700Schasinglulu  * Here is an opportunity to copy parameters passed by the calling EL (S-EL1
113*91f16700Schasinglulu  * in BL2 & EL3 in BL1) before they are lost (potentially). This needs to be
114*91f16700Schasinglulu  * done before the MMU is initialized so that the memory layout can be used
115*91f16700Schasinglulu  * while creating page tables. BL2 has flushed this information to memory, so
116*91f16700Schasinglulu  * we are guaranteed to pick up good data.
117*91f16700Schasinglulu  ******************************************************************************/
118*91f16700Schasinglulu void __init arm_bl31_early_platform_setup(void *from_bl2, uintptr_t soc_fw_config,
119*91f16700Schasinglulu 				uintptr_t hw_config, void *plat_params_from_bl2)
120*91f16700Schasinglulu {
121*91f16700Schasinglulu 	/* Initialize the console to provide early debug support */
122*91f16700Schasinglulu 	arm_console_boot_init();
123*91f16700Schasinglulu 
124*91f16700Schasinglulu #if RESET_TO_BL31
125*91f16700Schasinglulu 	/* There are no parameters from BL2 if BL31 is a reset vector */
126*91f16700Schasinglulu 	assert(from_bl2 == NULL);
127*91f16700Schasinglulu 	assert(plat_params_from_bl2 == NULL);
128*91f16700Schasinglulu 
129*91f16700Schasinglulu # ifdef BL32_BASE
130*91f16700Schasinglulu 	/* Populate entry point information for BL32 */
131*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl32_image_ep_info,
132*91f16700Schasinglulu 				PARAM_EP,
133*91f16700Schasinglulu 				VERSION_1,
134*91f16700Schasinglulu 				0);
135*91f16700Schasinglulu 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
136*91f16700Schasinglulu 	bl32_image_ep_info.pc = BL32_BASE;
137*91f16700Schasinglulu 	bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
138*91f16700Schasinglulu 
139*91f16700Schasinglulu #if defined(SPD_spmd)
140*91f16700Schasinglulu 	/* SPM (hafnium in secure world) expects SPM Core manifest base address
141*91f16700Schasinglulu 	 * in x0, which in !RESET_TO_BL31 case loaded after base of non shared
142*91f16700Schasinglulu 	 * SRAM(after 4KB offset of SRAM). But in RESET_TO_BL31 case all non
143*91f16700Schasinglulu 	 * shared SRAM is allocated to BL31, so to avoid overwriting of manifest
144*91f16700Schasinglulu 	 * keep it in the last page.
145*91f16700Schasinglulu 	 */
146*91f16700Schasinglulu 	bl32_image_ep_info.args.arg0 = ARM_TRUSTED_SRAM_BASE +
147*91f16700Schasinglulu 				PLAT_ARM_TRUSTED_SRAM_SIZE - PAGE_SIZE;
148*91f16700Schasinglulu #endif
149*91f16700Schasinglulu 
150*91f16700Schasinglulu # endif /* BL32_BASE */
151*91f16700Schasinglulu 
152*91f16700Schasinglulu 	/* Populate entry point information for BL33 */
153*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl33_image_ep_info,
154*91f16700Schasinglulu 				PARAM_EP,
155*91f16700Schasinglulu 				VERSION_1,
156*91f16700Schasinglulu 				0);
157*91f16700Schasinglulu 	/*
158*91f16700Schasinglulu 	 * Tell BL31 where the non-trusted software image
159*91f16700Schasinglulu 	 * is located and the entry state information
160*91f16700Schasinglulu 	 */
161*91f16700Schasinglulu 	bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
162*91f16700Schasinglulu 
163*91f16700Schasinglulu 	bl33_image_ep_info.spsr = arm_get_spsr_for_bl33_entry();
164*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
165*91f16700Schasinglulu 
166*91f16700Schasinglulu #if ENABLE_RME
167*91f16700Schasinglulu 	/*
168*91f16700Schasinglulu 	 * Populate entry point information for RMM.
169*91f16700Schasinglulu 	 * Only PC needs to be set as other fields are determined by RMMD.
170*91f16700Schasinglulu 	 */
171*91f16700Schasinglulu 	rmm_image_ep_info.pc = RMM_BASE;
172*91f16700Schasinglulu #endif /* ENABLE_RME */
173*91f16700Schasinglulu 
174*91f16700Schasinglulu #else /* RESET_TO_BL31 */
175*91f16700Schasinglulu 
176*91f16700Schasinglulu 	/*
177*91f16700Schasinglulu 	 * In debug builds, we pass a special value in 'plat_params_from_bl2'
178*91f16700Schasinglulu 	 * to verify platform parameters from BL2 to BL31.
179*91f16700Schasinglulu 	 * In release builds, it's not used.
180*91f16700Schasinglulu 	 */
181*91f16700Schasinglulu 	assert(((unsigned long long)plat_params_from_bl2) ==
182*91f16700Schasinglulu 		ARM_BL31_PLAT_PARAM_VAL);
183*91f16700Schasinglulu 
184*91f16700Schasinglulu 	/*
185*91f16700Schasinglulu 	 * Check params passed from BL2 should not be NULL,
186*91f16700Schasinglulu 	 */
187*91f16700Schasinglulu 	bl_params_t *params_from_bl2 = (bl_params_t *)from_bl2;
188*91f16700Schasinglulu 	assert(params_from_bl2 != NULL);
189*91f16700Schasinglulu 	assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
190*91f16700Schasinglulu 	assert(params_from_bl2->h.version >= VERSION_2);
191*91f16700Schasinglulu 
192*91f16700Schasinglulu 	bl_params_node_t *bl_params = params_from_bl2->head;
193*91f16700Schasinglulu 
194*91f16700Schasinglulu 	/*
195*91f16700Schasinglulu 	 * Copy BL33, BL32 and RMM (if present), entry point information.
196*91f16700Schasinglulu 	 * They are stored in Secure RAM, in BL2's address space.
197*91f16700Schasinglulu 	 */
198*91f16700Schasinglulu 	while (bl_params != NULL) {
199*91f16700Schasinglulu 		if (bl_params->image_id == BL32_IMAGE_ID) {
200*91f16700Schasinglulu 			bl32_image_ep_info = *bl_params->ep_info;
201*91f16700Schasinglulu #if SPMC_AT_EL3
202*91f16700Schasinglulu 			/*
203*91f16700Schasinglulu 			 * Populate the BL32 image base, size and max limit in
204*91f16700Schasinglulu 			 * the entry point information, since there is no
205*91f16700Schasinglulu 			 * platform function to retrieve them in generic
206*91f16700Schasinglulu 			 * code. We choose arg2, arg3 and arg4 since the generic
207*91f16700Schasinglulu 			 * code uses arg1 for stashing the SP manifest size. The
208*91f16700Schasinglulu 			 * SPMC setup uses these arguments to update SP manifest
209*91f16700Schasinglulu 			 * with actual SP's base address and it size.
210*91f16700Schasinglulu 			 */
211*91f16700Schasinglulu 			bl32_image_ep_info.args.arg2 =
212*91f16700Schasinglulu 				bl_params->image_info->image_base;
213*91f16700Schasinglulu 			bl32_image_ep_info.args.arg3 =
214*91f16700Schasinglulu 				bl_params->image_info->image_size;
215*91f16700Schasinglulu 			bl32_image_ep_info.args.arg4 =
216*91f16700Schasinglulu 				bl_params->image_info->image_base +
217*91f16700Schasinglulu 				bl_params->image_info->image_max_size;
218*91f16700Schasinglulu #endif
219*91f16700Schasinglulu 		}
220*91f16700Schasinglulu #if ENABLE_RME
221*91f16700Schasinglulu 		else if (bl_params->image_id == RMM_IMAGE_ID) {
222*91f16700Schasinglulu 			rmm_image_ep_info = *bl_params->ep_info;
223*91f16700Schasinglulu 		}
224*91f16700Schasinglulu #endif
225*91f16700Schasinglulu 		else if (bl_params->image_id == BL33_IMAGE_ID) {
226*91f16700Schasinglulu 			bl33_image_ep_info = *bl_params->ep_info;
227*91f16700Schasinglulu 		}
228*91f16700Schasinglulu 
229*91f16700Schasinglulu 		bl_params = bl_params->next_params_info;
230*91f16700Schasinglulu 	}
231*91f16700Schasinglulu 
232*91f16700Schasinglulu 	if (bl33_image_ep_info.pc == 0U)
233*91f16700Schasinglulu 		panic();
234*91f16700Schasinglulu #if ENABLE_RME
235*91f16700Schasinglulu 	if (rmm_image_ep_info.pc == 0U)
236*91f16700Schasinglulu 		panic();
237*91f16700Schasinglulu #endif
238*91f16700Schasinglulu #endif /* RESET_TO_BL31 */
239*91f16700Schasinglulu 
240*91f16700Schasinglulu # if ARM_LINUX_KERNEL_AS_BL33
241*91f16700Schasinglulu 	/*
242*91f16700Schasinglulu 	 * According to the file ``Documentation/arm64/booting.txt`` of the
243*91f16700Schasinglulu 	 * Linux kernel tree, Linux expects the physical address of the device
244*91f16700Schasinglulu 	 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
245*91f16700Schasinglulu 	 * must be 0.
246*91f16700Schasinglulu 	 * Repurpose the option to load Hafnium hypervisor in the normal world.
247*91f16700Schasinglulu 	 * It expects its manifest address in x0. This is essentially the linux
248*91f16700Schasinglulu 	 * dts (passed to the primary VM) by adding 'hypervisor' and chosen
249*91f16700Schasinglulu 	 * nodes specifying the Hypervisor configuration.
250*91f16700Schasinglulu 	 */
251*91f16700Schasinglulu #if RESET_TO_BL31
252*91f16700Schasinglulu 	bl33_image_ep_info.args.arg0 = (u_register_t)ARM_PRELOADED_DTB_BASE;
253*91f16700Schasinglulu #else
254*91f16700Schasinglulu 	bl33_image_ep_info.args.arg0 = (u_register_t)hw_config;
255*91f16700Schasinglulu #endif
256*91f16700Schasinglulu 	bl33_image_ep_info.args.arg1 = 0U;
257*91f16700Schasinglulu 	bl33_image_ep_info.args.arg2 = 0U;
258*91f16700Schasinglulu 	bl33_image_ep_info.args.arg3 = 0U;
259*91f16700Schasinglulu # endif
260*91f16700Schasinglulu }
261*91f16700Schasinglulu 
262*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
263*91f16700Schasinglulu 		u_register_t arg2, u_register_t arg3)
264*91f16700Schasinglulu {
265*91f16700Schasinglulu 	arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
266*91f16700Schasinglulu 
267*91f16700Schasinglulu 	/*
268*91f16700Schasinglulu 	 * Initialize Interconnect for this cluster during cold boot.
269*91f16700Schasinglulu 	 * No need for locks as no other CPU is active.
270*91f16700Schasinglulu 	 */
271*91f16700Schasinglulu 	plat_arm_interconnect_init();
272*91f16700Schasinglulu 
273*91f16700Schasinglulu 	/*
274*91f16700Schasinglulu 	 * Enable Interconnect coherency for the primary CPU's cluster.
275*91f16700Schasinglulu 	 * Earlier bootloader stages might already do this (e.g. Trusted
276*91f16700Schasinglulu 	 * Firmware's BL1 does it) but we can't assume so. There is no harm in
277*91f16700Schasinglulu 	 * executing this code twice anyway.
278*91f16700Schasinglulu 	 * Platform specific PSCI code will enable coherency for other
279*91f16700Schasinglulu 	 * clusters.
280*91f16700Schasinglulu 	 */
281*91f16700Schasinglulu 	plat_arm_interconnect_enter_coherency();
282*91f16700Schasinglulu }
283*91f16700Schasinglulu 
284*91f16700Schasinglulu /*******************************************************************************
285*91f16700Schasinglulu  * Perform any BL31 platform setup common to ARM standard platforms
286*91f16700Schasinglulu  ******************************************************************************/
287*91f16700Schasinglulu void arm_bl31_platform_setup(void)
288*91f16700Schasinglulu {
289*91f16700Schasinglulu 	/* Initialize the GIC driver, cpu and distributor interfaces */
290*91f16700Schasinglulu 	plat_arm_gic_driver_init();
291*91f16700Schasinglulu 	plat_arm_gic_init();
292*91f16700Schasinglulu 
293*91f16700Schasinglulu #if RESET_TO_BL31
294*91f16700Schasinglulu 	/*
295*91f16700Schasinglulu 	 * Do initial security configuration to allow DRAM/device access
296*91f16700Schasinglulu 	 * (if earlier BL has not already done so).
297*91f16700Schasinglulu 	 */
298*91f16700Schasinglulu 	plat_arm_security_setup();
299*91f16700Schasinglulu 
300*91f16700Schasinglulu #if defined(PLAT_ARM_MEM_PROT_ADDR)
301*91f16700Schasinglulu 	arm_nor_psci_do_dyn_mem_protect();
302*91f16700Schasinglulu #endif /* PLAT_ARM_MEM_PROT_ADDR */
303*91f16700Schasinglulu 
304*91f16700Schasinglulu #endif /* RESET_TO_BL31 */
305*91f16700Schasinglulu 
306*91f16700Schasinglulu 	/* Enable and initialize the System level generic timer */
307*91f16700Schasinglulu 	mmio_write_32(ARM_SYS_CNTCTL_BASE + CNTCR_OFF,
308*91f16700Schasinglulu 			CNTCR_FCREQ(0U) | CNTCR_EN);
309*91f16700Schasinglulu 
310*91f16700Schasinglulu 	/* Allow access to the System counter timer module */
311*91f16700Schasinglulu 	arm_configure_sys_timer();
312*91f16700Schasinglulu 
313*91f16700Schasinglulu 	/* Initialize power controller before setting up topology */
314*91f16700Schasinglulu 	plat_arm_pwrc_setup();
315*91f16700Schasinglulu 
316*91f16700Schasinglulu #if ENABLE_FEAT_RAS && FFH_SUPPORT
317*91f16700Schasinglulu 	ras_init();
318*91f16700Schasinglulu #endif
319*91f16700Schasinglulu 
320*91f16700Schasinglulu #if USE_DEBUGFS
321*91f16700Schasinglulu 	debugfs_init();
322*91f16700Schasinglulu #endif /* USE_DEBUGFS */
323*91f16700Schasinglulu }
324*91f16700Schasinglulu 
325*91f16700Schasinglulu /*******************************************************************************
326*91f16700Schasinglulu  * Perform any BL31 platform runtime setup prior to BL31 exit common to ARM
327*91f16700Schasinglulu  * standard platforms
328*91f16700Schasinglulu  * Perform BL31 platform setup
329*91f16700Schasinglulu  ******************************************************************************/
330*91f16700Schasinglulu void arm_bl31_plat_runtime_setup(void)
331*91f16700Schasinglulu {
332*91f16700Schasinglulu 	console_switch_state(CONSOLE_FLAG_RUNTIME);
333*91f16700Schasinglulu 
334*91f16700Schasinglulu 	/* Initialize the runtime console */
335*91f16700Schasinglulu 	arm_console_runtime_init();
336*91f16700Schasinglulu 
337*91f16700Schasinglulu #if RECLAIM_INIT_CODE
338*91f16700Schasinglulu 	arm_free_init_memory();
339*91f16700Schasinglulu #endif
340*91f16700Schasinglulu 
341*91f16700Schasinglulu #if PLAT_RO_XLAT_TABLES
342*91f16700Schasinglulu 	arm_xlat_make_tables_readonly();
343*91f16700Schasinglulu #endif
344*91f16700Schasinglulu }
345*91f16700Schasinglulu 
346*91f16700Schasinglulu #if RECLAIM_INIT_CODE
347*91f16700Schasinglulu /*
348*91f16700Schasinglulu  * Make memory for image boot time code RW to reclaim it as stack for the
349*91f16700Schasinglulu  * secondary cores, or RO where it cannot be reclaimed:
350*91f16700Schasinglulu  *
351*91f16700Schasinglulu  *            |-------- INIT SECTION --------|
352*91f16700Schasinglulu  *  -----------------------------------------
353*91f16700Schasinglulu  * |  CORE 0  |  CORE 1  |  CORE 2  | EXTRA  |
354*91f16700Schasinglulu  * |  STACK   |  STACK   |  STACK   | SPACE  |
355*91f16700Schasinglulu  *  -----------------------------------------
356*91f16700Schasinglulu  *             <-------------------> <------>
357*91f16700Schasinglulu  *                MAKE RW AND XN       MAKE
358*91f16700Schasinglulu  *                  FOR STACKS       RO AND XN
359*91f16700Schasinglulu  */
360*91f16700Schasinglulu void arm_free_init_memory(void)
361*91f16700Schasinglulu {
362*91f16700Schasinglulu 	int ret = 0;
363*91f16700Schasinglulu 
364*91f16700Schasinglulu 	if (BL_STACKS_END < BL_INIT_CODE_END) {
365*91f16700Schasinglulu 		/* Reclaim some of the init section as stack if possible. */
366*91f16700Schasinglulu 		if (BL_INIT_CODE_BASE < BL_STACKS_END) {
367*91f16700Schasinglulu 			ret |= xlat_change_mem_attributes(BL_INIT_CODE_BASE,
368*91f16700Schasinglulu 					BL_STACKS_END - BL_INIT_CODE_BASE,
369*91f16700Schasinglulu 					MT_RW_DATA);
370*91f16700Schasinglulu 		}
371*91f16700Schasinglulu 		/* Make the rest of the init section read-only. */
372*91f16700Schasinglulu 		ret |= xlat_change_mem_attributes(BL_STACKS_END,
373*91f16700Schasinglulu 				BL_INIT_CODE_END - BL_STACKS_END,
374*91f16700Schasinglulu 				MT_RO_DATA);
375*91f16700Schasinglulu 	} else {
376*91f16700Schasinglulu 		/* The stacks cover the init section, so reclaim it all. */
377*91f16700Schasinglulu 		ret |= xlat_change_mem_attributes(BL_INIT_CODE_BASE,
378*91f16700Schasinglulu 				BL_INIT_CODE_END - BL_INIT_CODE_BASE,
379*91f16700Schasinglulu 				MT_RW_DATA);
380*91f16700Schasinglulu 	}
381*91f16700Schasinglulu 
382*91f16700Schasinglulu 	if (ret != 0) {
383*91f16700Schasinglulu 		ERROR("Could not reclaim initialization code");
384*91f16700Schasinglulu 		panic();
385*91f16700Schasinglulu 	}
386*91f16700Schasinglulu }
387*91f16700Schasinglulu #endif
388*91f16700Schasinglulu 
389*91f16700Schasinglulu void __init bl31_platform_setup(void)
390*91f16700Schasinglulu {
391*91f16700Schasinglulu 	arm_bl31_platform_setup();
392*91f16700Schasinglulu }
393*91f16700Schasinglulu 
394*91f16700Schasinglulu void bl31_plat_runtime_setup(void)
395*91f16700Schasinglulu {
396*91f16700Schasinglulu 	arm_bl31_plat_runtime_setup();
397*91f16700Schasinglulu }
398*91f16700Schasinglulu 
399*91f16700Schasinglulu /*******************************************************************************
400*91f16700Schasinglulu  * Perform the very early platform specific architectural setup shared between
401*91f16700Schasinglulu  * ARM standard platforms. This only does basic initialization. Later
402*91f16700Schasinglulu  * architectural setup (bl31_arch_setup()) does not do anything platform
403*91f16700Schasinglulu  * specific.
404*91f16700Schasinglulu  ******************************************************************************/
405*91f16700Schasinglulu void __init arm_bl31_plat_arch_setup(void)
406*91f16700Schasinglulu {
407*91f16700Schasinglulu 	const mmap_region_t bl_regions[] = {
408*91f16700Schasinglulu 		MAP_BL31_TOTAL,
409*91f16700Schasinglulu #if ENABLE_RME
410*91f16700Schasinglulu 		ARM_MAP_L0_GPT_REGION,
411*91f16700Schasinglulu #endif
412*91f16700Schasinglulu #if RECLAIM_INIT_CODE
413*91f16700Schasinglulu 		MAP_BL_INIT_CODE,
414*91f16700Schasinglulu #endif
415*91f16700Schasinglulu #if SEPARATE_NOBITS_REGION
416*91f16700Schasinglulu 		MAP_BL31_NOBITS,
417*91f16700Schasinglulu #endif
418*91f16700Schasinglulu 		ARM_MAP_BL_RO,
419*91f16700Schasinglulu #if USE_ROMLIB
420*91f16700Schasinglulu 		ARM_MAP_ROMLIB_CODE,
421*91f16700Schasinglulu 		ARM_MAP_ROMLIB_DATA,
422*91f16700Schasinglulu #endif
423*91f16700Schasinglulu #if USE_COHERENT_MEM
424*91f16700Schasinglulu 		ARM_MAP_BL_COHERENT_RAM,
425*91f16700Schasinglulu #endif
426*91f16700Schasinglulu 		{0}
427*91f16700Schasinglulu 	};
428*91f16700Schasinglulu 
429*91f16700Schasinglulu 	setup_page_tables(bl_regions, plat_arm_get_mmap());
430*91f16700Schasinglulu 
431*91f16700Schasinglulu 	enable_mmu_el3(0);
432*91f16700Schasinglulu 
433*91f16700Schasinglulu #if ENABLE_RME
434*91f16700Schasinglulu 	/*
435*91f16700Schasinglulu 	 * Initialise Granule Protection library and enable GPC for the primary
436*91f16700Schasinglulu 	 * processor. The tables have already been initialized by a previous BL
437*91f16700Schasinglulu 	 * stage, so there is no need to provide any PAS here. This function
438*91f16700Schasinglulu 	 * sets up pointers to those tables.
439*91f16700Schasinglulu 	 */
440*91f16700Schasinglulu 	if (gpt_runtime_init() < 0) {
441*91f16700Schasinglulu 		ERROR("gpt_runtime_init() failed!\n");
442*91f16700Schasinglulu 		panic();
443*91f16700Schasinglulu 	}
444*91f16700Schasinglulu #endif /* ENABLE_RME */
445*91f16700Schasinglulu 
446*91f16700Schasinglulu 	arm_setup_romlib();
447*91f16700Schasinglulu }
448*91f16700Schasinglulu 
449*91f16700Schasinglulu void __init bl31_plat_arch_setup(void)
450*91f16700Schasinglulu {
451*91f16700Schasinglulu 	arm_bl31_plat_arch_setup();
452*91f16700Schasinglulu }
453