xref: /arm-trusted-firmware/plat/xilinx/zynqmp/bl31_zynqmp_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2013-2021, Arm Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
4*91f16700Schasinglulu  *
5*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <assert.h>
9*91f16700Schasinglulu #include <errno.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <bl31/bl31.h>
12*91f16700Schasinglulu #include <common/bl_common.h>
13*91f16700Schasinglulu #include <common/debug.h>
14*91f16700Schasinglulu #include <common/fdt_fixup.h>
15*91f16700Schasinglulu #include <common/fdt_wrappers.h>
16*91f16700Schasinglulu #include <lib/mmio.h>
17*91f16700Schasinglulu #include <libfdt.h>
18*91f16700Schasinglulu #include <plat/arm/common/plat_arm.h>
19*91f16700Schasinglulu #include <plat/common/platform.h>
20*91f16700Schasinglulu #include <plat_console.h>
21*91f16700Schasinglulu 
22*91f16700Schasinglulu #include <custom_svc.h>
23*91f16700Schasinglulu #include <plat_fdt.h>
24*91f16700Schasinglulu #include <plat_private.h>
25*91f16700Schasinglulu #include <plat_startup.h>
26*91f16700Schasinglulu #include <zynqmp_def.h>
27*91f16700Schasinglulu 
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 struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type)
39*91f16700Schasinglulu {
40*91f16700Schasinglulu 	entry_point_info_t *next_image_info;
41*91f16700Schasinglulu 
42*91f16700Schasinglulu 	assert(sec_state_is_valid(type));
43*91f16700Schasinglulu 	if (type == NON_SECURE) {
44*91f16700Schasinglulu 		next_image_info = &bl33_image_ep_info;
45*91f16700Schasinglulu 	} else {
46*91f16700Schasinglulu 		next_image_info = &bl32_image_ep_info;
47*91f16700Schasinglulu 	}
48*91f16700Schasinglulu 
49*91f16700Schasinglulu 	return next_image_info;
50*91f16700Schasinglulu }
51*91f16700Schasinglulu 
52*91f16700Schasinglulu /*
53*91f16700Schasinglulu  * Set the build time defaults. We want to do this when doing a JTAG boot
54*91f16700Schasinglulu  * or if we can't find any other config data.
55*91f16700Schasinglulu  */
56*91f16700Schasinglulu static inline void bl31_set_default_config(void)
57*91f16700Schasinglulu {
58*91f16700Schasinglulu 	bl32_image_ep_info.pc = BL32_BASE;
59*91f16700Schasinglulu 	bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
60*91f16700Schasinglulu 	bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
61*91f16700Schasinglulu 	bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
62*91f16700Schasinglulu 					  DISABLE_ALL_EXCEPTIONS);
63*91f16700Schasinglulu }
64*91f16700Schasinglulu 
65*91f16700Schasinglulu /*
66*91f16700Schasinglulu  * Perform any BL31 specific platform actions. Here is an opportunity to copy
67*91f16700Schasinglulu  * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before they
68*91f16700Schasinglulu  * are lost (potentially). This needs to be done before the MMU is initialized
69*91f16700Schasinglulu  * so that the memory layout can be used while creating page tables.
70*91f16700Schasinglulu  */
71*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
72*91f16700Schasinglulu 				u_register_t arg2, u_register_t arg3)
73*91f16700Schasinglulu {
74*91f16700Schasinglulu 	uint64_t tfa_handoff_addr;
75*91f16700Schasinglulu 
76*91f16700Schasinglulu 	setup_console();
77*91f16700Schasinglulu 
78*91f16700Schasinglulu 	/* Initialize the platform config for future decision making */
79*91f16700Schasinglulu 	zynqmp_config_setup();
80*91f16700Schasinglulu 
81*91f16700Schasinglulu 	/*
82*91f16700Schasinglulu 	 * Do initial security configuration to allow DRAM/device access. On
83*91f16700Schasinglulu 	 * Base ZYNQMP only DRAM security is programmable (via TrustZone), but
84*91f16700Schasinglulu 	 * other platforms might have more programmable security devices
85*91f16700Schasinglulu 	 * present.
86*91f16700Schasinglulu 	 */
87*91f16700Schasinglulu 
88*91f16700Schasinglulu 	/* Populate common information for BL32 and BL33 */
89*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
90*91f16700Schasinglulu 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
91*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
92*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
93*91f16700Schasinglulu 
94*91f16700Schasinglulu 	tfa_handoff_addr = mmio_read_32(PMU_GLOBAL_GEN_STORAGE6);
95*91f16700Schasinglulu 
96*91f16700Schasinglulu 	if (zynqmp_get_bootmode() == ZYNQMP_BOOTMODE_JTAG) {
97*91f16700Schasinglulu 		bl31_set_default_config();
98*91f16700Schasinglulu 	} else {
99*91f16700Schasinglulu 		/* use parameters from XBL */
100*91f16700Schasinglulu 		enum xbl_handoff ret = xbl_handover(&bl32_image_ep_info,
101*91f16700Schasinglulu 							  &bl33_image_ep_info,
102*91f16700Schasinglulu 							  tfa_handoff_addr);
103*91f16700Schasinglulu 		if (ret != XBL_HANDOFF_SUCCESS) {
104*91f16700Schasinglulu 			panic();
105*91f16700Schasinglulu 		}
106*91f16700Schasinglulu 	}
107*91f16700Schasinglulu 	if (bl32_image_ep_info.pc != 0) {
108*91f16700Schasinglulu 		NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
109*91f16700Schasinglulu 	}
110*91f16700Schasinglulu 	if (bl33_image_ep_info.pc != 0) {
111*91f16700Schasinglulu 		NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
112*91f16700Schasinglulu 	}
113*91f16700Schasinglulu 
114*91f16700Schasinglulu 	custom_early_setup();
115*91f16700Schasinglulu 
116*91f16700Schasinglulu }
117*91f16700Schasinglulu 
118*91f16700Schasinglulu #if ZYNQMP_WDT_RESTART
119*91f16700Schasinglulu static zynmp_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
120*91f16700Schasinglulu 
121*91f16700Schasinglulu int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
122*91f16700Schasinglulu {
123*91f16700Schasinglulu 	static uint32_t index;
124*91f16700Schasinglulu 	uint32_t i;
125*91f16700Schasinglulu 
126*91f16700Schasinglulu 	/* Validate 'handler' and 'id' parameters */
127*91f16700Schasinglulu 	if (!handler || index >= MAX_INTR_EL3) {
128*91f16700Schasinglulu 		return -EINVAL;
129*91f16700Schasinglulu 	}
130*91f16700Schasinglulu 
131*91f16700Schasinglulu 	/* Check if a handler has already been registered */
132*91f16700Schasinglulu 	for (i = 0; i < index; i++) {
133*91f16700Schasinglulu 		if (id == type_el3_interrupt_table[i].id) {
134*91f16700Schasinglulu 			return -EALREADY;
135*91f16700Schasinglulu 		}
136*91f16700Schasinglulu 	}
137*91f16700Schasinglulu 
138*91f16700Schasinglulu 	type_el3_interrupt_table[index].id = id;
139*91f16700Schasinglulu 	type_el3_interrupt_table[index].handler = handler;
140*91f16700Schasinglulu 
141*91f16700Schasinglulu 	index++;
142*91f16700Schasinglulu 
143*91f16700Schasinglulu 	return 0;
144*91f16700Schasinglulu }
145*91f16700Schasinglulu 
146*91f16700Schasinglulu static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
147*91f16700Schasinglulu 					  void *handle, void *cookie)
148*91f16700Schasinglulu {
149*91f16700Schasinglulu 	uint32_t intr_id;
150*91f16700Schasinglulu 	uint32_t i;
151*91f16700Schasinglulu 	interrupt_type_handler_t handler = NULL;
152*91f16700Schasinglulu 
153*91f16700Schasinglulu 	intr_id = plat_ic_get_pending_interrupt_id();
154*91f16700Schasinglulu 
155*91f16700Schasinglulu 	for (i = 0; i < MAX_INTR_EL3; i++) {
156*91f16700Schasinglulu 		if (intr_id == type_el3_interrupt_table[i].id) {
157*91f16700Schasinglulu 			handler = type_el3_interrupt_table[i].handler;
158*91f16700Schasinglulu 		}
159*91f16700Schasinglulu 	}
160*91f16700Schasinglulu 
161*91f16700Schasinglulu 	if (handler != NULL) {
162*91f16700Schasinglulu 		return handler(intr_id, flags, handle, cookie);
163*91f16700Schasinglulu 	}
164*91f16700Schasinglulu 
165*91f16700Schasinglulu 	return 0;
166*91f16700Schasinglulu }
167*91f16700Schasinglulu #endif
168*91f16700Schasinglulu 
169*91f16700Schasinglulu void bl31_platform_setup(void)
170*91f16700Schasinglulu {
171*91f16700Schasinglulu 	prepare_dtb();
172*91f16700Schasinglulu 
173*91f16700Schasinglulu 	/* Initialize the gic cpu and distributor interfaces */
174*91f16700Schasinglulu 	plat_arm_gic_driver_init();
175*91f16700Schasinglulu 	plat_arm_gic_init();
176*91f16700Schasinglulu }
177*91f16700Schasinglulu 
178*91f16700Schasinglulu void bl31_plat_runtime_setup(void)
179*91f16700Schasinglulu {
180*91f16700Schasinglulu #if ZYNQMP_WDT_RESTART
181*91f16700Schasinglulu 	uint64_t flags = 0;
182*91f16700Schasinglulu 	uint64_t rc;
183*91f16700Schasinglulu 
184*91f16700Schasinglulu 	set_interrupt_rm_flag(flags, NON_SECURE);
185*91f16700Schasinglulu 	rc = register_interrupt_type_handler(INTR_TYPE_EL3,
186*91f16700Schasinglulu 					     rdo_el3_interrupt_handler, flags);
187*91f16700Schasinglulu 	if (rc) {
188*91f16700Schasinglulu 		panic();
189*91f16700Schasinglulu 	}
190*91f16700Schasinglulu #endif
191*91f16700Schasinglulu 
192*91f16700Schasinglulu 	custom_runtime_setup();
193*91f16700Schasinglulu 
194*91f16700Schasinglulu 	console_switch_state(CONSOLE_FLAG_RUNTIME);
195*91f16700Schasinglulu }
196*91f16700Schasinglulu 
197*91f16700Schasinglulu /*
198*91f16700Schasinglulu  * Perform the very early platform specific architectural setup here.
199*91f16700Schasinglulu  */
200*91f16700Schasinglulu void bl31_plat_arch_setup(void)
201*91f16700Schasinglulu {
202*91f16700Schasinglulu 	plat_arm_interconnect_init();
203*91f16700Schasinglulu 	plat_arm_interconnect_enter_coherency();
204*91f16700Schasinglulu 
205*91f16700Schasinglulu 	const mmap_region_t bl_regions[] = {
206*91f16700Schasinglulu #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE))
207*91f16700Schasinglulu 		MAP_REGION_FLAT(XILINX_OF_BOARD_DTB_ADDR, XILINX_OF_BOARD_DTB_MAX_SIZE,
208*91f16700Schasinglulu 			MT_MEMORY | MT_RW | MT_NS),
209*91f16700Schasinglulu #endif
210*91f16700Schasinglulu 		MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
211*91f16700Schasinglulu 			MT_MEMORY | MT_RW | MT_SECURE),
212*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
213*91f16700Schasinglulu 				MT_CODE | MT_SECURE),
214*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
215*91f16700Schasinglulu 				MT_RO_DATA | MT_SECURE),
216*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
217*91f16700Schasinglulu 				BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
218*91f16700Schasinglulu 				MT_DEVICE | MT_RW | MT_SECURE),
219*91f16700Schasinglulu 		{0}
220*91f16700Schasinglulu 	};
221*91f16700Schasinglulu 
222*91f16700Schasinglulu 	custom_mmap_add();
223*91f16700Schasinglulu 
224*91f16700Schasinglulu 	setup_page_tables(bl_regions, plat_get_mmap());
225*91f16700Schasinglulu 	enable_mmu_el3(0);
226*91f16700Schasinglulu }
227