xref: /arm-trusted-firmware/plat/xilinx/versal/bl31_versal_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  * Copyright (c) 2018-2022, Xilinx, Inc. All rights reserved.
4*91f16700Schasinglulu  * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
5*91f16700Schasinglulu  *
6*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
7*91f16700Schasinglulu  */
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <assert.h>
10*91f16700Schasinglulu #include <errno.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu #include <bl31/bl31.h>
13*91f16700Schasinglulu #include <common/bl_common.h>
14*91f16700Schasinglulu #include <common/debug.h>
15*91f16700Schasinglulu #include <lib/mmio.h>
16*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_v2.h>
17*91f16700Schasinglulu #include <plat/common/platform.h>
18*91f16700Schasinglulu #include <plat_arm.h>
19*91f16700Schasinglulu #include <plat_console.h>
20*91f16700Schasinglulu 
21*91f16700Schasinglulu #include <plat_fdt.h>
22*91f16700Schasinglulu #include <plat_private.h>
23*91f16700Schasinglulu #include <plat_startup.h>
24*91f16700Schasinglulu #include "pm_api_sys.h"
25*91f16700Schasinglulu #include "pm_client.h"
26*91f16700Schasinglulu #include <pm_ipi.h>
27*91f16700Schasinglulu #include <versal_def.h>
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 /*
33*91f16700Schasinglulu  * Return a pointer to the 'entry_point_info' structure of the next image for
34*91f16700Schasinglulu  * the security state specified. BL33 corresponds to the non-secure image type
35*91f16700Schasinglulu  * while BL32 corresponds to the secure image type. A NULL pointer is returned
36*91f16700Schasinglulu  * if the image does not exist.
37*91f16700Schasinglulu  */
38*91f16700Schasinglulu entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
39*91f16700Schasinglulu {
40*91f16700Schasinglulu 	assert(sec_state_is_valid(type));
41*91f16700Schasinglulu 
42*91f16700Schasinglulu 	if (type == NON_SECURE) {
43*91f16700Schasinglulu 		return &bl33_image_ep_info;
44*91f16700Schasinglulu 	}
45*91f16700Schasinglulu 
46*91f16700Schasinglulu 	return &bl32_image_ep_info;
47*91f16700Schasinglulu }
48*91f16700Schasinglulu 
49*91f16700Schasinglulu /*
50*91f16700Schasinglulu  * Set the build time defaults,if we can't find any config data.
51*91f16700Schasinglulu  */
52*91f16700Schasinglulu static inline void bl31_set_default_config(void)
53*91f16700Schasinglulu {
54*91f16700Schasinglulu 	bl32_image_ep_info.pc = (uintptr_t)BL32_BASE;
55*91f16700Schasinglulu 	bl32_image_ep_info.spsr = (uint32_t)arm_get_spsr_for_bl32_entry();
56*91f16700Schasinglulu 	bl33_image_ep_info.pc = (uintptr_t)plat_get_ns_image_entrypoint();
57*91f16700Schasinglulu 	bl33_image_ep_info.spsr = (uint32_t)SPSR_64(MODE_EL2, MODE_SP_ELX,
58*91f16700Schasinglulu 						    DISABLE_ALL_EXCEPTIONS);
59*91f16700Schasinglulu }
60*91f16700Schasinglulu 
61*91f16700Schasinglulu /*
62*91f16700Schasinglulu  * Perform any BL31 specific platform actions. Here is an opportunity to copy
63*91f16700Schasinglulu  * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
64*91f16700Schasinglulu  * are lost (potentially). This needs to be done before the MMU is initialized
65*91f16700Schasinglulu  * so that the memory layout can be used while creating page tables.
66*91f16700Schasinglulu  */
67*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
68*91f16700Schasinglulu 				u_register_t arg2, u_register_t arg3)
69*91f16700Schasinglulu {
70*91f16700Schasinglulu 	uint64_t tfa_handoff_addr;
71*91f16700Schasinglulu 	uint32_t payload[PAYLOAD_ARG_CNT], max_size = HANDOFF_PARAMS_MAX_SIZE;
72*91f16700Schasinglulu 	enum pm_ret_status ret_status;
73*91f16700Schasinglulu 	uint64_t addr[HANDOFF_PARAMS_MAX_SIZE];
74*91f16700Schasinglulu 
75*91f16700Schasinglulu 	setup_console();
76*91f16700Schasinglulu 
77*91f16700Schasinglulu 	/* Initialize the platform config for future decision making */
78*91f16700Schasinglulu 	versal_config_setup();
79*91f16700Schasinglulu 
80*91f16700Schasinglulu 	/* Get platform related information */
81*91f16700Schasinglulu 	board_detection();
82*91f16700Schasinglulu 
83*91f16700Schasinglulu 	/*
84*91f16700Schasinglulu 	 * Do initial security configuration to allow DRAM/device access. On
85*91f16700Schasinglulu 	 * Base VERSAL only DRAM security is programmable (via TrustZone), but
86*91f16700Schasinglulu 	 * other platforms might have more programmable security devices
87*91f16700Schasinglulu 	 * present.
88*91f16700Schasinglulu 	 */
89*91f16700Schasinglulu 
90*91f16700Schasinglulu 	/* Populate common information for BL32 and BL33 */
91*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
92*91f16700Schasinglulu 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
93*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
94*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
95*91f16700Schasinglulu 
96*91f16700Schasinglulu 	PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, 1, PM_LOAD_GET_HANDOFF_PARAMS,
97*91f16700Schasinglulu 			(uintptr_t)addr >> 32U, (uintptr_t)addr, max_size);
98*91f16700Schasinglulu 	ret_status = pm_ipi_send_sync(primary_proc, payload, NULL, 0);
99*91f16700Schasinglulu 	if (ret_status == PM_RET_SUCCESS) {
100*91f16700Schasinglulu 		INFO("BL31: GET_HANDOFF_PARAMS call success=%d\n", ret_status);
101*91f16700Schasinglulu 		tfa_handoff_addr = (uintptr_t)&addr;
102*91f16700Schasinglulu 	} else {
103*91f16700Schasinglulu 		ERROR("BL31: GET_HANDOFF_PARAMS Failed, read tfa_handoff_addr from reg\n");
104*91f16700Schasinglulu 		tfa_handoff_addr = mmio_read_32(PMC_GLOBAL_GLOB_GEN_STORAGE4);
105*91f16700Schasinglulu 	}
106*91f16700Schasinglulu 
107*91f16700Schasinglulu 	enum xbl_handoff ret = xbl_handover(&bl32_image_ep_info,
108*91f16700Schasinglulu 						  &bl33_image_ep_info,
109*91f16700Schasinglulu 						  tfa_handoff_addr);
110*91f16700Schasinglulu 	if (ret == XBL_HANDOFF_NO_STRUCT || ret == XBL_HANDOFF_INVAL_STRUCT) {
111*91f16700Schasinglulu 		bl31_set_default_config();
112*91f16700Schasinglulu 	} else if (ret == XBL_HANDOFF_TOO_MANY_PARTS) {
113*91f16700Schasinglulu 		ERROR("BL31: Error too many partitions %u\n", ret);
114*91f16700Schasinglulu 	} else if (ret != XBL_HANDOFF_SUCCESS) {
115*91f16700Schasinglulu 		panic();
116*91f16700Schasinglulu 	} else {
117*91f16700Schasinglulu 		INFO("BL31: PLM to TF-A handover success %u\n", ret);
118*91f16700Schasinglulu 
119*91f16700Schasinglulu 		/*
120*91f16700Schasinglulu 		 * The BL32 load address is indicated as 0x0 in the handoff
121*91f16700Schasinglulu 		 * parameters, which is different from the default/user-provided
122*91f16700Schasinglulu 		 * load address of 0x60000000 but the flags are correctly
123*91f16700Schasinglulu 		 * configured. Consequently, in this scenario, set the PC
124*91f16700Schasinglulu 		 * to the requested BL32_BASE address.
125*91f16700Schasinglulu 		 */
126*91f16700Schasinglulu 
127*91f16700Schasinglulu 		/* TODO: Remove the following check once this is fixed from PLM */
128*91f16700Schasinglulu 		if (bl32_image_ep_info.pc == 0 && bl32_image_ep_info.spsr != 0) {
129*91f16700Schasinglulu 			bl32_image_ep_info.pc = (uintptr_t)BL32_BASE;
130*91f16700Schasinglulu 		}
131*91f16700Schasinglulu 	}
132*91f16700Schasinglulu 
133*91f16700Schasinglulu 	NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
134*91f16700Schasinglulu 	NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
135*91f16700Schasinglulu }
136*91f16700Schasinglulu 
137*91f16700Schasinglulu static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
138*91f16700Schasinglulu 
139*91f16700Schasinglulu int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
140*91f16700Schasinglulu {
141*91f16700Schasinglulu 	static uint32_t index;
142*91f16700Schasinglulu 	uint32_t i;
143*91f16700Schasinglulu 
144*91f16700Schasinglulu 	/* Validate 'handler' and 'id' parameters */
145*91f16700Schasinglulu 	if (handler == NULL || index >= MAX_INTR_EL3) {
146*91f16700Schasinglulu 		return -EINVAL;
147*91f16700Schasinglulu 	}
148*91f16700Schasinglulu 
149*91f16700Schasinglulu 	/* Check if a handler has already been registered */
150*91f16700Schasinglulu 	for (i = 0; i < index; i++) {
151*91f16700Schasinglulu 		if (id == type_el3_interrupt_table[i].id) {
152*91f16700Schasinglulu 			return -EALREADY;
153*91f16700Schasinglulu 		}
154*91f16700Schasinglulu 	}
155*91f16700Schasinglulu 
156*91f16700Schasinglulu 	type_el3_interrupt_table[index].id = id;
157*91f16700Schasinglulu 	type_el3_interrupt_table[index].handler = handler;
158*91f16700Schasinglulu 
159*91f16700Schasinglulu 	index++;
160*91f16700Schasinglulu 
161*91f16700Schasinglulu 	return 0;
162*91f16700Schasinglulu }
163*91f16700Schasinglulu 
164*91f16700Schasinglulu static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
165*91f16700Schasinglulu 					  void *handle, void *cookie)
166*91f16700Schasinglulu {
167*91f16700Schasinglulu 	uint32_t intr_id;
168*91f16700Schasinglulu 	uint32_t i;
169*91f16700Schasinglulu 	interrupt_type_handler_t handler = NULL;
170*91f16700Schasinglulu 
171*91f16700Schasinglulu 	intr_id = plat_ic_get_pending_interrupt_id();
172*91f16700Schasinglulu 
173*91f16700Schasinglulu 	for (i = 0; i < MAX_INTR_EL3; i++) {
174*91f16700Schasinglulu 		if (intr_id == type_el3_interrupt_table[i].id) {
175*91f16700Schasinglulu 			handler = type_el3_interrupt_table[i].handler;
176*91f16700Schasinglulu 		}
177*91f16700Schasinglulu 	}
178*91f16700Schasinglulu 
179*91f16700Schasinglulu 	if (handler != NULL) {
180*91f16700Schasinglulu 		return handler(intr_id, flags, handle, cookie);
181*91f16700Schasinglulu 	}
182*91f16700Schasinglulu 
183*91f16700Schasinglulu 	return 0;
184*91f16700Schasinglulu }
185*91f16700Schasinglulu 
186*91f16700Schasinglulu void bl31_platform_setup(void)
187*91f16700Schasinglulu {
188*91f16700Schasinglulu 	prepare_dtb();
189*91f16700Schasinglulu 
190*91f16700Schasinglulu 	/* Initialize the gic cpu and distributor interfaces */
191*91f16700Schasinglulu 	plat_versal_gic_driver_init();
192*91f16700Schasinglulu 	plat_versal_gic_init();
193*91f16700Schasinglulu }
194*91f16700Schasinglulu 
195*91f16700Schasinglulu void bl31_plat_runtime_setup(void)
196*91f16700Schasinglulu {
197*91f16700Schasinglulu 	uint64_t flags = 0;
198*91f16700Schasinglulu 	int32_t rc;
199*91f16700Schasinglulu 
200*91f16700Schasinglulu 	set_interrupt_rm_flag(flags, NON_SECURE);
201*91f16700Schasinglulu 	rc = register_interrupt_type_handler(INTR_TYPE_EL3,
202*91f16700Schasinglulu 					     rdo_el3_interrupt_handler, flags);
203*91f16700Schasinglulu 	if (rc != 0) {
204*91f16700Schasinglulu 		panic();
205*91f16700Schasinglulu 	}
206*91f16700Schasinglulu 
207*91f16700Schasinglulu 	console_switch_state(CONSOLE_FLAG_RUNTIME);
208*91f16700Schasinglulu }
209*91f16700Schasinglulu 
210*91f16700Schasinglulu /*
211*91f16700Schasinglulu  * Perform the very early platform specific architectural setup here.
212*91f16700Schasinglulu  */
213*91f16700Schasinglulu void bl31_plat_arch_setup(void)
214*91f16700Schasinglulu {
215*91f16700Schasinglulu 	plat_arm_interconnect_init();
216*91f16700Schasinglulu 	plat_arm_interconnect_enter_coherency();
217*91f16700Schasinglulu 
218*91f16700Schasinglulu 	const mmap_region_t bl_regions[] = {
219*91f16700Schasinglulu #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE) && \
220*91f16700Schasinglulu 	(!defined(PLAT_XLAT_TABLES_DYNAMIC)))
221*91f16700Schasinglulu 		MAP_REGION_FLAT(XILINX_OF_BOARD_DTB_ADDR, XILINX_OF_BOARD_DTB_MAX_SIZE,
222*91f16700Schasinglulu 				MT_MEMORY | MT_RW | MT_NS),
223*91f16700Schasinglulu #endif
224*91f16700Schasinglulu 		MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
225*91f16700Schasinglulu 			MT_MEMORY | MT_RW | MT_SECURE),
226*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
227*91f16700Schasinglulu 				MT_CODE | MT_SECURE),
228*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
229*91f16700Schasinglulu 				MT_RO_DATA | MT_SECURE),
230*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
231*91f16700Schasinglulu 				BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
232*91f16700Schasinglulu 				MT_DEVICE | MT_RW | MT_SECURE),
233*91f16700Schasinglulu 		{0}
234*91f16700Schasinglulu 	};
235*91f16700Schasinglulu 
236*91f16700Schasinglulu 	setup_page_tables(bl_regions, plat_get_mmap());
237*91f16700Schasinglulu 	enable_mmu(0);
238*91f16700Schasinglulu }
239