xref: /arm-trusted-firmware/plat/brcm/common/brcm_bl31_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2020, 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/arm/sp804_delay_timer.h>
14*91f16700Schasinglulu #include <lib/utils.h>
15*91f16700Schasinglulu #include <plat/common/platform.h>
16*91f16700Schasinglulu 
17*91f16700Schasinglulu #include <bcm_console.h>
18*91f16700Schasinglulu #include <plat_brcm.h>
19*91f16700Schasinglulu #include <platform_def.h>
20*91f16700Schasinglulu 
21*91f16700Schasinglulu #ifdef BL33_SHARED_DDR_BASE
22*91f16700Schasinglulu struct bl33_info *bl33_info = (struct bl33_info *)BL33_SHARED_DDR_BASE;
23*91f16700Schasinglulu #endif
24*91f16700Schasinglulu 
25*91f16700Schasinglulu /*
26*91f16700Schasinglulu  * Placeholder variables for copying the arguments that have been passed to
27*91f16700Schasinglulu  * BL31 from BL2.
28*91f16700Schasinglulu  */
29*91f16700Schasinglulu static entry_point_info_t bl32_image_ep_info;
30*91f16700Schasinglulu static entry_point_info_t bl33_image_ep_info;
31*91f16700Schasinglulu 
32*91f16700Schasinglulu /* Weak definitions may be overridden in specific BRCM platform */
33*91f16700Schasinglulu #pragma weak plat_bcm_bl31_early_platform_setup
34*91f16700Schasinglulu #pragma weak plat_brcm_pwrc_setup
35*91f16700Schasinglulu #pragma weak plat_brcm_security_setup
36*91f16700Schasinglulu 
37*91f16700Schasinglulu void plat_brcm_security_setup(void)
38*91f16700Schasinglulu {
39*91f16700Schasinglulu 
40*91f16700Schasinglulu }
41*91f16700Schasinglulu 
42*91f16700Schasinglulu void plat_brcm_pwrc_setup(void)
43*91f16700Schasinglulu {
44*91f16700Schasinglulu 
45*91f16700Schasinglulu }
46*91f16700Schasinglulu 
47*91f16700Schasinglulu void plat_bcm_bl31_early_platform_setup(void *from_bl2,
48*91f16700Schasinglulu 				   bl_params_t *plat_params_from_bl2)
49*91f16700Schasinglulu {
50*91f16700Schasinglulu 
51*91f16700Schasinglulu }
52*91f16700Schasinglulu 
53*91f16700Schasinglulu /*******************************************************************************
54*91f16700Schasinglulu  * Return a pointer to the 'entry_point_info' structure of the next image for
55*91f16700Schasinglulu  * the security state specified. BL33 corresponds to the non-secure image type
56*91f16700Schasinglulu  * while BL32 corresponds to the secure image type. A NULL pointer is returned
57*91f16700Schasinglulu  * if the image does not exist.
58*91f16700Schasinglulu  ******************************************************************************/
59*91f16700Schasinglulu struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type)
60*91f16700Schasinglulu {
61*91f16700Schasinglulu 	entry_point_info_t *next_image_info;
62*91f16700Schasinglulu 
63*91f16700Schasinglulu 	assert(sec_state_is_valid(type));
64*91f16700Schasinglulu 	next_image_info = (type == NON_SECURE)
65*91f16700Schasinglulu 			? &bl33_image_ep_info : &bl32_image_ep_info;
66*91f16700Schasinglulu 	/*
67*91f16700Schasinglulu 	 * None of the images on the ARM development platforms can have 0x0
68*91f16700Schasinglulu 	 * as the entrypoint
69*91f16700Schasinglulu 	 */
70*91f16700Schasinglulu 	if (next_image_info->pc)
71*91f16700Schasinglulu 		return next_image_info;
72*91f16700Schasinglulu 	else
73*91f16700Schasinglulu 		return NULL;
74*91f16700Schasinglulu }
75*91f16700Schasinglulu 
76*91f16700Schasinglulu /*******************************************************************************
77*91f16700Schasinglulu  * Perform any BL31 early platform setup common to ARM standard platforms.
78*91f16700Schasinglulu  * Here is an opportunity to copy parameters passed by the calling EL (S-EL1
79*91f16700Schasinglulu  * in BL2 & EL3 in BL1) before they are lost (potentially). This needs to be
80*91f16700Schasinglulu  * done before the MMU is initialized so that the memory layout can be used
81*91f16700Schasinglulu  * while creating page tables. BL2 has flushed this information to memory, so
82*91f16700Schasinglulu  * we are guaranteed to pick up good data.
83*91f16700Schasinglulu  ******************************************************************************/
84*91f16700Schasinglulu void __init brcm_bl31_early_platform_setup(void *from_bl2,
85*91f16700Schasinglulu 					  uintptr_t soc_fw_config,
86*91f16700Schasinglulu 					  uintptr_t hw_config,
87*91f16700Schasinglulu 					  void *plat_params_from_bl2)
88*91f16700Schasinglulu {
89*91f16700Schasinglulu 	/* Initialize the console to provide early debug support */
90*91f16700Schasinglulu 	bcm_console_boot_init();
91*91f16700Schasinglulu 
92*91f16700Schasinglulu 	/* Initialize delay timer driver using SP804 dual timer 0 */
93*91f16700Schasinglulu 	sp804_timer_init(SP804_TIMER0_BASE,
94*91f16700Schasinglulu 			 SP804_TIMER0_CLKMULT, SP804_TIMER0_CLKDIV);
95*91f16700Schasinglulu 
96*91f16700Schasinglulu #if RESET_TO_BL31
97*91f16700Schasinglulu 	/* There are no parameters from BL2 if BL31 is a reset vector */
98*91f16700Schasinglulu 	assert(from_bl2 == NULL);
99*91f16700Schasinglulu 	assert(plat_params_from_bl2 == NULL);
100*91f16700Schasinglulu 
101*91f16700Schasinglulu # ifdef BL32_BASE
102*91f16700Schasinglulu 	/* Populate entry point information for BL32 */
103*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl32_image_ep_info,
104*91f16700Schasinglulu 		       PARAM_EP,
105*91f16700Schasinglulu 		       VERSION_1,
106*91f16700Schasinglulu 		       0);
107*91f16700Schasinglulu 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
108*91f16700Schasinglulu 	bl32_image_ep_info.pc = BL32_BASE;
109*91f16700Schasinglulu 	bl32_image_ep_info.spsr = brcm_get_spsr_for_bl32_entry();
110*91f16700Schasinglulu # endif /* BL32_BASE */
111*91f16700Schasinglulu 
112*91f16700Schasinglulu 	/* Populate entry point information for BL33 */
113*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl33_image_ep_info,
114*91f16700Schasinglulu 		       PARAM_EP,
115*91f16700Schasinglulu 		       VERSION_1,
116*91f16700Schasinglulu 		       0);
117*91f16700Schasinglulu 	/*
118*91f16700Schasinglulu 	 * Tell BL31 where the non-trusted software image
119*91f16700Schasinglulu 	 * is located and the entry state information
120*91f16700Schasinglulu 	 */
121*91f16700Schasinglulu 	bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
122*91f16700Schasinglulu 
123*91f16700Schasinglulu 	bl33_image_ep_info.spsr = brcm_get_spsr_for_bl33_entry();
124*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
125*91f16700Schasinglulu 
126*91f16700Schasinglulu # if ARM_LINUX_KERNEL_AS_BL33
127*91f16700Schasinglulu 	/*
128*91f16700Schasinglulu 	 * According to the file ``Documentation/arm64/booting.txt`` of the
129*91f16700Schasinglulu 	 * Linux kernel tree, Linux expects the physical address of the device
130*91f16700Schasinglulu 	 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
131*91f16700Schasinglulu 	 * must be 0.
132*91f16700Schasinglulu 	 */
133*91f16700Schasinglulu 	bl33_image_ep_info.args.arg0 = (u_register_t)PRELOADED_DTB_BASE;
134*91f16700Schasinglulu 	bl33_image_ep_info.args.arg1 = 0U;
135*91f16700Schasinglulu 	bl33_image_ep_info.args.arg2 = 0U;
136*91f16700Schasinglulu 	bl33_image_ep_info.args.arg3 = 0U;
137*91f16700Schasinglulu # endif
138*91f16700Schasinglulu 
139*91f16700Schasinglulu #else /* RESET_TO_BL31 */
140*91f16700Schasinglulu 
141*91f16700Schasinglulu 	/*
142*91f16700Schasinglulu 	 * In debug builds, we pass a special value in 'plat_params_from_bl2'
143*91f16700Schasinglulu 	 * to verify platform parameters from BL2 to BL31.
144*91f16700Schasinglulu 	 * In release builds, it's not used.
145*91f16700Schasinglulu 	 */
146*91f16700Schasinglulu 	assert(((unsigned long long)plat_params_from_bl2) ==
147*91f16700Schasinglulu 		BRCM_BL31_PLAT_PARAM_VAL);
148*91f16700Schasinglulu 
149*91f16700Schasinglulu 	/*
150*91f16700Schasinglulu 	 * Check params passed from BL2 should not be NULL
151*91f16700Schasinglulu 	 */
152*91f16700Schasinglulu 	bl_params_t *params_from_bl2 = (bl_params_t *)from_bl2;
153*91f16700Schasinglulu 
154*91f16700Schasinglulu 	assert(params_from_bl2 != NULL);
155*91f16700Schasinglulu 	assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
156*91f16700Schasinglulu 	assert(params_from_bl2->h.version >= VERSION_2);
157*91f16700Schasinglulu 
158*91f16700Schasinglulu 	bl_params_node_t *bl_params = params_from_bl2->head;
159*91f16700Schasinglulu 
160*91f16700Schasinglulu 	/*
161*91f16700Schasinglulu 	 * Copy BL33 and BL32 (if present), entry point information.
162*91f16700Schasinglulu 	 * They are stored in Secure RAM, in BL2's address space.
163*91f16700Schasinglulu 	 */
164*91f16700Schasinglulu 	while (bl_params != NULL) {
165*91f16700Schasinglulu 		if (bl_params->image_id == BL32_IMAGE_ID &&
166*91f16700Schasinglulu 		    bl_params->image_info->h.attr != IMAGE_ATTRIB_SKIP_LOADING)
167*91f16700Schasinglulu 			bl32_image_ep_info = *bl_params->ep_info;
168*91f16700Schasinglulu 
169*91f16700Schasinglulu 		if (bl_params->image_id == BL33_IMAGE_ID)
170*91f16700Schasinglulu 			bl33_image_ep_info = *bl_params->ep_info;
171*91f16700Schasinglulu 
172*91f16700Schasinglulu 		bl_params = bl_params->next_params_info;
173*91f16700Schasinglulu 	}
174*91f16700Schasinglulu 
175*91f16700Schasinglulu 	if (bl33_image_ep_info.pc == 0U)
176*91f16700Schasinglulu 		panic();
177*91f16700Schasinglulu #endif /* RESET_TO_BL31 */
178*91f16700Schasinglulu 
179*91f16700Schasinglulu #ifdef BL33_SHARED_DDR_BASE
180*91f16700Schasinglulu 	/* Pass information to BL33 thorugh x0 */
181*91f16700Schasinglulu 	bl33_image_ep_info.args.arg0 = (u_register_t)BL33_SHARED_DDR_BASE;
182*91f16700Schasinglulu 	bl33_image_ep_info.args.arg1 = 0ULL;
183*91f16700Schasinglulu 	bl33_image_ep_info.args.arg2 = 0ULL;
184*91f16700Schasinglulu 	bl33_image_ep_info.args.arg3 = 0ULL;
185*91f16700Schasinglulu #endif
186*91f16700Schasinglulu }
187*91f16700Schasinglulu 
188*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
189*91f16700Schasinglulu 		u_register_t arg2, u_register_t arg3)
190*91f16700Schasinglulu {
191*91f16700Schasinglulu #ifdef BL31_LOG_LEVEL
192*91f16700Schasinglulu 	SET_LOG_LEVEL(BL31_LOG_LEVEL);
193*91f16700Schasinglulu #endif
194*91f16700Schasinglulu 
195*91f16700Schasinglulu 	brcm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
196*91f16700Schasinglulu 
197*91f16700Schasinglulu 	plat_bcm_bl31_early_platform_setup((void *)arg0, (void *)arg3);
198*91f16700Schasinglulu 
199*91f16700Schasinglulu #ifdef DRIVER_CC_ENABLE
200*91f16700Schasinglulu 	/*
201*91f16700Schasinglulu 	 * Initialize Interconnect for this cluster during cold boot.
202*91f16700Schasinglulu 	 * No need for locks as no other CPU is active.
203*91f16700Schasinglulu 	 */
204*91f16700Schasinglulu 	plat_brcm_interconnect_init();
205*91f16700Schasinglulu 
206*91f16700Schasinglulu 	/*
207*91f16700Schasinglulu 	 * Enable Interconnect coherency for the primary CPU's cluster.
208*91f16700Schasinglulu 	 * Earlier bootloader stages might already do this (e.g. Trusted
209*91f16700Schasinglulu 	 * Firmware's BL1 does it) but we can't assume so. There is no harm in
210*91f16700Schasinglulu 	 * executing this code twice anyway.
211*91f16700Schasinglulu 	 * Platform specific PSCI code will enable coherency for other
212*91f16700Schasinglulu 	 * clusters.
213*91f16700Schasinglulu 	 */
214*91f16700Schasinglulu 	plat_brcm_interconnect_enter_coherency();
215*91f16700Schasinglulu #endif
216*91f16700Schasinglulu }
217*91f16700Schasinglulu 
218*91f16700Schasinglulu /*******************************************************************************
219*91f16700Schasinglulu  * Perform any BL31 platform setup common to ARM standard platforms
220*91f16700Schasinglulu  ******************************************************************************/
221*91f16700Schasinglulu void brcm_bl31_platform_setup(void)
222*91f16700Schasinglulu {
223*91f16700Schasinglulu 	/* Initialize the GIC driver, cpu and distributor interfaces */
224*91f16700Schasinglulu 	plat_brcm_gic_driver_init();
225*91f16700Schasinglulu 	plat_brcm_gic_init();
226*91f16700Schasinglulu 
227*91f16700Schasinglulu 	/* Initialize power controller before setting up topology */
228*91f16700Schasinglulu 	plat_brcm_pwrc_setup();
229*91f16700Schasinglulu }
230*91f16700Schasinglulu 
231*91f16700Schasinglulu /*******************************************************************************
232*91f16700Schasinglulu  * Perform any BL31 platform runtime setup prior to BL31 exit common to ARM
233*91f16700Schasinglulu  * standard platforms
234*91f16700Schasinglulu  * Perform BL31 platform setup
235*91f16700Schasinglulu  ******************************************************************************/
236*91f16700Schasinglulu void brcm_bl31_plat_runtime_setup(void)
237*91f16700Schasinglulu {
238*91f16700Schasinglulu 	console_switch_state(CONSOLE_FLAG_RUNTIME);
239*91f16700Schasinglulu 
240*91f16700Schasinglulu 	/* Initialize the runtime console */
241*91f16700Schasinglulu 	bcm_console_runtime_init();
242*91f16700Schasinglulu }
243*91f16700Schasinglulu 
244*91f16700Schasinglulu void bl31_platform_setup(void)
245*91f16700Schasinglulu {
246*91f16700Schasinglulu 	brcm_bl31_platform_setup();
247*91f16700Schasinglulu 
248*91f16700Schasinglulu 	/* Initialize the secure environment */
249*91f16700Schasinglulu 	plat_brcm_security_setup();
250*91f16700Schasinglulu }
251*91f16700Schasinglulu 
252*91f16700Schasinglulu void bl31_plat_runtime_setup(void)
253*91f16700Schasinglulu {
254*91f16700Schasinglulu 	brcm_bl31_plat_runtime_setup();
255*91f16700Schasinglulu }
256*91f16700Schasinglulu 
257*91f16700Schasinglulu /*******************************************************************************
258*91f16700Schasinglulu  * Perform the very early platform specific architectural setup shared between
259*91f16700Schasinglulu  * ARM standard platforms. This only does basic initialization. Later
260*91f16700Schasinglulu  * architectural setup (bl31_arch_setup()) does not do anything platform
261*91f16700Schasinglulu  * specific.
262*91f16700Schasinglulu  ******************************************************************************/
263*91f16700Schasinglulu void __init brcm_bl31_plat_arch_setup(void)
264*91f16700Schasinglulu {
265*91f16700Schasinglulu #ifndef MMU_DISABLED
266*91f16700Schasinglulu 	const mmap_region_t bl_regions[] = {
267*91f16700Schasinglulu 		MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
268*91f16700Schasinglulu 				MT_MEMORY | MT_RW | MT_SECURE),
269*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
270*91f16700Schasinglulu 				MT_CODE | MT_SECURE),
271*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_RO_DATA_BASE,
272*91f16700Schasinglulu 				BL_RO_DATA_END - BL_RO_DATA_BASE,
273*91f16700Schasinglulu 				MT_RO_DATA | MT_SECURE),
274*91f16700Schasinglulu #if USE_COHERENT_MEM
275*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
276*91f16700Schasinglulu 				BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
277*91f16700Schasinglulu 				MT_DEVICE | MT_RW | MT_SECURE),
278*91f16700Schasinglulu #endif
279*91f16700Schasinglulu 		{0}
280*91f16700Schasinglulu 	};
281*91f16700Schasinglulu 
282*91f16700Schasinglulu 	setup_page_tables(bl_regions, plat_brcm_get_mmap());
283*91f16700Schasinglulu 
284*91f16700Schasinglulu 	enable_mmu_el3(0);
285*91f16700Schasinglulu #endif
286*91f16700Schasinglulu }
287*91f16700Schasinglulu 
288*91f16700Schasinglulu void __init bl31_plat_arch_setup(void)
289*91f16700Schasinglulu {
290*91f16700Schasinglulu 	brcm_bl31_plat_arch_setup();
291*91f16700Schasinglulu }
292