xref: /rk3399_ARM-atf/plat/xilinx/versal_net/bl31_versal_net_setup.c (revision a53a95076242a0a7b0d3a50bbbc3b6b9b3b202fb)
11d333e69SMichal Simek /*
201907f3fSHarrison Mutai  * Copyright (c) 2018-2025, Arm Limited and Contributors. All rights reserved.
31d333e69SMichal Simek  * Copyright (c) 2018-2022, Xilinx, Inc. All rights reserved.
410510c98SAmit Nagal  * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
51d333e69SMichal Simek  *
61d333e69SMichal Simek  * SPDX-License-Identifier: BSD-3-Clause
71d333e69SMichal Simek  */
81d333e69SMichal Simek 
91d333e69SMichal Simek #include <assert.h>
101d333e69SMichal Simek #include <errno.h>
111d333e69SMichal Simek 
121d333e69SMichal Simek #include <bl31/bl31.h>
131d333e69SMichal Simek #include <common/bl_common.h>
141d333e69SMichal Simek #include <common/debug.h>
151d333e69SMichal Simek #include <lib/mmio.h>
161d333e69SMichal Simek #include <lib/xlat_tables/xlat_tables_v2.h>
171d333e69SMichal Simek #include <plat/common/platform.h>
181d333e69SMichal Simek #include <plat_arm.h>
19a467e813SPrasad Kummari #include <plat_console.h>
2007625d9dSPrasad Kummari #include <plat_clkfunc.h>
211d333e69SMichal Simek 
2246a08aabSAmit Nagal #include <plat_fdt.h>
231d333e69SMichal Simek #include <plat_private.h>
241d333e69SMichal Simek #include <plat_startup.h>
25a36ac40cSAkshay Belsare #include <pm_api_sys.h>
26a36ac40cSAkshay Belsare #include <pm_client.h>
27a36ac40cSAkshay Belsare #include <pm_ipi.h>
281d333e69SMichal Simek #include <versal_net_def.h>
291d333e69SMichal Simek 
301d333e69SMichal Simek static entry_point_info_t bl32_image_ep_info;
311d333e69SMichal Simek static entry_point_info_t bl33_image_ep_info;
321d333e69SMichal Simek 
3375170704SBoyan Karatotev static const uintptr_t gicr_base_addrs[2] = {
3475170704SBoyan Karatotev 	PLAT_ARM_GICR_BASE,	/* GICR Base address of the primary CPU */
3575170704SBoyan Karatotev 	0U			/* Zero Termination */
3675170704SBoyan Karatotev };
3775170704SBoyan Karatotev 
381d333e69SMichal Simek /*
391d333e69SMichal Simek  * Return a pointer to the 'entry_point_info' structure of the next image for
401d333e69SMichal Simek  * the security state specified. BL33 corresponds to the non-secure image type
411d333e69SMichal Simek  * while BL32 corresponds to the secure image type. A NULL pointer is returned
421d333e69SMichal Simek  * if the image does not exist.
431d333e69SMichal Simek  */
bl31_plat_get_next_image_ep_info(uint32_t type)441d333e69SMichal Simek entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
451d333e69SMichal Simek {
461d333e69SMichal Simek 	assert(sec_state_is_valid(type));
471d333e69SMichal Simek 
481d333e69SMichal Simek 	if (type == NON_SECURE) {
491d333e69SMichal Simek 		return &bl33_image_ep_info;
501d333e69SMichal Simek 	}
511d333e69SMichal Simek 
521d333e69SMichal Simek 	return &bl32_image_ep_info;
531d333e69SMichal Simek }
541d333e69SMichal Simek 
551d333e69SMichal Simek /*
561d333e69SMichal Simek  * Set the build time defaults,if we can't find any config data.
571d333e69SMichal Simek  */
bl31_set_default_config(void)581d333e69SMichal Simek static inline void bl31_set_default_config(void)
591d333e69SMichal Simek {
601d333e69SMichal Simek 	bl32_image_ep_info.pc = BL32_BASE;
6101907f3fSHarrison Mutai 	bl32_image_ep_info.spsr = arm_get_spsr(BL32_IMAGE_ID);
621d333e69SMichal Simek 	bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
63d51c8e4cSMaheedhar Bollapalli 	bl33_image_ep_info.spsr = (uint32_t)SPSR_64(MODE_EL2, MODE_SP_ELX,
641d333e69SMichal Simek 					DISABLE_ALL_EXCEPTIONS);
651d333e69SMichal Simek }
661d333e69SMichal Simek 
67c6f62027SAmit Nagal /* Define read and write function for clusterbusqos register */
DEFINE_RENAME_SYSREG_RW_FUNCS(cluster_bus_qos,S3_0_C15_C4_4)68c6f62027SAmit Nagal DEFINE_RENAME_SYSREG_RW_FUNCS(cluster_bus_qos, S3_0_C15_C4_4)
69c6f62027SAmit Nagal 
70c6f62027SAmit Nagal static void versal_net_setup_qos(void)
71c6f62027SAmit Nagal {
72c6f62027SAmit Nagal 	int ret;
73c6f62027SAmit Nagal 
74c6f62027SAmit Nagal 	ret = read_cluster_bus_qos();
75c6f62027SAmit Nagal 	INFO("BL31: default cluster bus qos: 0x%x\n", ret);
76c6f62027SAmit Nagal 	write_cluster_bus_qos(0);
77c6f62027SAmit Nagal 	ret = read_cluster_bus_qos();
78c6f62027SAmit Nagal 	INFO("BL31: cluster bus qos written: 0x%x\n", ret);
79c6f62027SAmit Nagal }
80c6f62027SAmit Nagal 
811d333e69SMichal Simek /*
821d333e69SMichal Simek  * Perform any BL31 specific platform actions. Here is an opportunity to copy
831d333e69SMichal Simek  * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
841d333e69SMichal Simek  * are lost (potentially). This needs to be done before the MMU is initialized
851d333e69SMichal Simek  * so that the memory layout can be used while creating page tables.
861d333e69SMichal Simek  */
bl31_early_platform_setup2(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)871d333e69SMichal Simek void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
881d333e69SMichal Simek 				u_register_t arg2, u_register_t arg3)
891d333e69SMichal Simek {
9006f63f4bSMaheedhar Bollapalli 	(void)arg0;
9106f63f4bSMaheedhar Bollapalli 	(void)arg1;
9206f63f4bSMaheedhar Bollapalli 	(void)arg2;
9306f63f4bSMaheedhar Bollapalli 	(void)arg3;
9406f63f4bSMaheedhar Bollapalli 
95a36ac40cSAkshay Belsare #if !(TFA_NO_PM)
96a36ac40cSAkshay Belsare 	uint64_t tfa_handoff_addr, buff[HANDOFF_PARAMS_MAX_SIZE] = {0};
97a36ac40cSAkshay Belsare 	uint32_t payload[PAYLOAD_ARG_CNT], max_size = HANDOFF_PARAMS_MAX_SIZE;
98a36ac40cSAkshay Belsare 	enum pm_ret_status ret_status;
9905d0cb4fSMaheedhar Bollapalli #if DEBUG
100*3ef5820cSSaivardhan Thatikonda 	uint32_t boot_mode[RET_PAYLOAD_ARG_CNT] = {0};
10105d0cb4fSMaheedhar Bollapalli #endif
102a36ac40cSAkshay Belsare #endif /* !(TFA_NO_PM) */
1031d333e69SMichal Simek 
1041d333e69SMichal Simek 	board_detection();
1051d333e69SMichal Simek 
1061d333e69SMichal Simek 	switch (platform_id) {
1071d333e69SMichal Simek 	case VERSAL_NET_SPP:
1081d333e69SMichal Simek 		cpu_clock = 1000000;
1091d333e69SMichal Simek 		break;
1101d333e69SMichal Simek 	case VERSAL_NET_EMU:
1111d333e69SMichal Simek 		cpu_clock = 3660000;
1121d333e69SMichal Simek 		break;
1131d333e69SMichal Simek 	case VERSAL_NET_QEMU:
1141d333e69SMichal Simek 		/* Random values now */
1151d333e69SMichal Simek 		cpu_clock = 100000000;
1161d333e69SMichal Simek 		break;
1171d333e69SMichal Simek 	case VERSAL_NET_SILICON:
118faa22d48SMichal Simek 		cpu_clock = 100000000;
119faa22d48SMichal Simek 		break;
1201d333e69SMichal Simek 	default:
1211d333e69SMichal Simek 		panic();
1221d333e69SMichal Simek 	}
1231d333e69SMichal Simek 
12407625d9dSPrasad Kummari 	syscnt_freq_config_setup();
12507625d9dSPrasad Kummari 
12607625d9dSPrasad Kummari 	set_cnt_freq();
12707625d9dSPrasad Kummari 
128e14ae4b3SSaivardhan Thatikonda 	/* Initialize the platform config for future decision making */
129e14ae4b3SSaivardhan Thatikonda 	versal_net_config_setup();
130e14ae4b3SSaivardhan Thatikonda 
131a467e813SPrasad Kummari 	setup_console();
1321d333e69SMichal Simek 
133d6760c4dSAkshay Belsare 	NOTICE("TF-A running on %s %d.%d\n", board_name_decode(),
1341d333e69SMichal Simek 	       platform_version / 10U, platform_version % 10U);
1351d333e69SMichal Simek 
136c6f62027SAmit Nagal 	versal_net_setup_qos();
137c6f62027SAmit Nagal 
1381d333e69SMichal Simek 
1391d333e69SMichal Simek 	/*
1401d333e69SMichal Simek 	 * Do initial security configuration to allow DRAM/device access. On
1411d333e69SMichal Simek 	 * Base VERSAL_NET only DRAM security is programmable (via TrustZone), but
1421d333e69SMichal Simek 	 * other platforms might have more programmable security devices
1431d333e69SMichal Simek 	 * present.
1441d333e69SMichal Simek 	 */
1451d333e69SMichal Simek 
1461d333e69SMichal Simek 	/* Populate common information for BL32 and BL33 */
1471d333e69SMichal Simek 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
1481d333e69SMichal Simek 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
1491d333e69SMichal Simek 	SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
1501d333e69SMichal Simek 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
151a36ac40cSAkshay Belsare #if !(TFA_NO_PM)
1523cbe0ae5SMaheedhar Bollapalli 	PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, 1U, PM_LOAD_GET_HANDOFF_PARAMS,
153a36ac40cSAkshay Belsare 			 (uintptr_t)buff >> 32U, (uintptr_t)buff, max_size);
154a36ac40cSAkshay Belsare 
155a36ac40cSAkshay Belsare 	ret_status = pm_ipi_send_sync(primary_proc, payload, NULL, 0);
156a36ac40cSAkshay Belsare 	if (ret_status == PM_RET_SUCCESS) {
157a36ac40cSAkshay Belsare 		enum xbl_handoff xbl_ret;
158a36ac40cSAkshay Belsare 
159a36ac40cSAkshay Belsare 		tfa_handoff_addr = (uintptr_t)&buff;
160a36ac40cSAkshay Belsare 
161a36ac40cSAkshay Belsare 		xbl_ret = xbl_handover(&bl32_image_ep_info, &bl33_image_ep_info,
162a36ac40cSAkshay Belsare 				       tfa_handoff_addr);
16305d0cb4fSMaheedhar Bollapalli 		if (xbl_ret == XBL_HANDOFF_SUCCESS) {
16405d0cb4fSMaheedhar Bollapalli 			goto success;
165a36ac40cSAkshay Belsare 		}
16605d0cb4fSMaheedhar Bollapalli #if DEBUG
167*3ef5820cSSaivardhan Thatikonda 		get_boot_mode(boot_mode);
168*3ef5820cSSaivardhan Thatikonda 		if ((xbl_ret != XBL_HANDOFF_SUCCESS) && (boot_mode[1] == JTAG_MODE)) {
1691d333e69SMichal Simek 			bl31_set_default_config();
17005d0cb4fSMaheedhar Bollapalli 			goto success;
171a36ac40cSAkshay Belsare 		}
17205d0cb4fSMaheedhar Bollapalli #endif
17305d0cb4fSMaheedhar Bollapalli 	} else {
17405d0cb4fSMaheedhar Bollapalli 		bl31_set_default_config();
17505d0cb4fSMaheedhar Bollapalli 		goto success;
17605d0cb4fSMaheedhar Bollapalli 	}
17705d0cb4fSMaheedhar Bollapalli 
17805d0cb4fSMaheedhar Bollapalli 	ERROR("PLM to TF-A handover failed or not in default boot mode\n");
17905d0cb4fSMaheedhar Bollapalli 	panic();
18005d0cb4fSMaheedhar Bollapalli 
18105d0cb4fSMaheedhar Bollapalli success:
18205d0cb4fSMaheedhar Bollapalli 	INFO("BL31: PLM to TF-A handover success or default config is set\n");
18305d0cb4fSMaheedhar Bollapalli 
184a36ac40cSAkshay Belsare #else
185a36ac40cSAkshay Belsare 	bl31_set_default_config();
186a36ac40cSAkshay Belsare #endif /* !(TFA_NO_PM) */
1871d333e69SMichal Simek 
1881d333e69SMichal Simek 	NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
1891d333e69SMichal Simek 	NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
1901d333e69SMichal Simek }
1911d333e69SMichal Simek 
1920654ab7fSJay Buddhabhatti static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
1930654ab7fSJay Buddhabhatti 
request_intr_type_el3(uint32_t id,interrupt_type_handler_t handler)1940654ab7fSJay Buddhabhatti int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
1950654ab7fSJay Buddhabhatti {
1960654ab7fSJay Buddhabhatti 	static uint32_t index;
1970654ab7fSJay Buddhabhatti 	uint32_t i;
1985003a332SMaheedhar Bollapalli 	int32_t ret = 0;
1990654ab7fSJay Buddhabhatti 
2000654ab7fSJay Buddhabhatti 	/* Validate 'handler' and 'id' parameters */
201a4ddd24fSMaheedhar Bollapalli 	if ((handler == NULL) || (index >= MAX_INTR_EL3)) {
2025003a332SMaheedhar Bollapalli 		ret = -EINVAL;
2035003a332SMaheedhar Bollapalli 		goto exit_label;
2040654ab7fSJay Buddhabhatti 	}
2050654ab7fSJay Buddhabhatti 
2060654ab7fSJay Buddhabhatti 	/* Check if a handler has already been registered */
2070654ab7fSJay Buddhabhatti 	for (i = 0; i < index; i++) {
2080654ab7fSJay Buddhabhatti 		if (id == type_el3_interrupt_table[i].id) {
2095003a332SMaheedhar Bollapalli 			ret = -EALREADY;
2105003a332SMaheedhar Bollapalli 			goto exit_label;
2110654ab7fSJay Buddhabhatti 		}
2120654ab7fSJay Buddhabhatti 	}
2130654ab7fSJay Buddhabhatti 
2140654ab7fSJay Buddhabhatti 	type_el3_interrupt_table[index].id = id;
2150654ab7fSJay Buddhabhatti 	type_el3_interrupt_table[index].handler = handler;
2160654ab7fSJay Buddhabhatti 
2170654ab7fSJay Buddhabhatti 	index++;
2180654ab7fSJay Buddhabhatti 
2195003a332SMaheedhar Bollapalli exit_label:
2205003a332SMaheedhar Bollapalli 	return ret;
2210654ab7fSJay Buddhabhatti }
2220654ab7fSJay Buddhabhatti 
22310510c98SAmit Nagal #if SDEI_SUPPORT
rdo_el3_interrupt_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)22410510c98SAmit Nagal static int rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
22510510c98SAmit Nagal 				     void *handle, void *cookie)
22610510c98SAmit Nagal #else
2270654ab7fSJay Buddhabhatti static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
2280654ab7fSJay Buddhabhatti 					  void *handle, void *cookie)
22910510c98SAmit Nagal #endif
2300654ab7fSJay Buddhabhatti {
2310654ab7fSJay Buddhabhatti 	uint32_t intr_id;
2320654ab7fSJay Buddhabhatti 	uint32_t i;
2330654ab7fSJay Buddhabhatti 	interrupt_type_handler_t handler = NULL;
2340654ab7fSJay Buddhabhatti 
23510510c98SAmit Nagal #if SDEI_SUPPORT
23610510c98SAmit Nagal 	/* when SDEI_SUPPORT is enabled, ehf_el3_interrupt_handler
23710510c98SAmit Nagal 	 * reads the interrupt id prior to calling the
23810510c98SAmit Nagal 	 * rdo_el3_interrupt_handler and passes that id to the
23910510c98SAmit Nagal 	 * handler.
24010510c98SAmit Nagal 	 */
24110510c98SAmit Nagal 	intr_id = id;
24210510c98SAmit Nagal #else
2430654ab7fSJay Buddhabhatti 	intr_id = plat_ic_get_pending_interrupt_id();
24410510c98SAmit Nagal #endif
2450654ab7fSJay Buddhabhatti 
2460654ab7fSJay Buddhabhatti 	for (i = 0; i < MAX_INTR_EL3; i++) {
2470654ab7fSJay Buddhabhatti 		if (intr_id == type_el3_interrupt_table[i].id) {
2480654ab7fSJay Buddhabhatti 			handler = type_el3_interrupt_table[i].handler;
2490654ab7fSJay Buddhabhatti 		}
2500654ab7fSJay Buddhabhatti 	}
2510654ab7fSJay Buddhabhatti 
2520654ab7fSJay Buddhabhatti 	if (handler != NULL) {
253aa6df8ecSMaheedhar Bollapalli 		(void)handler(intr_id, flags, handle, cookie);
2540654ab7fSJay Buddhabhatti 	}
2550654ab7fSJay Buddhabhatti 
2560654ab7fSJay Buddhabhatti 	return 0;
2570654ab7fSJay Buddhabhatti }
2580654ab7fSJay Buddhabhatti 
bl31_platform_setup(void)2591d333e69SMichal Simek void bl31_platform_setup(void)
2601d333e69SMichal Simek {
26146a08aabSAmit Nagal 	prepare_dtb();
26275170704SBoyan Karatotev 
26375170704SBoyan Karatotev 	gic_set_gicr_frames(gicr_base_addrs);
2641d333e69SMichal Simek }
2651d333e69SMichal Simek 
bl31_plat_runtime_setup(void)2661d333e69SMichal Simek void bl31_plat_runtime_setup(void)
2671d333e69SMichal Simek {
26810510c98SAmit Nagal #if !SDEI_SUPPORT
2690654ab7fSJay Buddhabhatti 	uint64_t flags = 0;
2700654ab7fSJay Buddhabhatti 	int32_t rc;
2710654ab7fSJay Buddhabhatti 
2720654ab7fSJay Buddhabhatti 	set_interrupt_rm_flag(flags, NON_SECURE);
2730654ab7fSJay Buddhabhatti 	rc = register_interrupt_type_handler(INTR_TYPE_EL3,
2740654ab7fSJay Buddhabhatti 					     rdo_el3_interrupt_handler, flags);
2750654ab7fSJay Buddhabhatti 	if (rc != 0) {
2760654ab7fSJay Buddhabhatti 		panic();
2770654ab7fSJay Buddhabhatti 	}
27810510c98SAmit Nagal #else
27910510c98SAmit Nagal 	ehf_register_priority_handler(PLAT_IPI_PRI, rdo_el3_interrupt_handler);
28010510c98SAmit Nagal #endif
2811d333e69SMichal Simek }
2821d333e69SMichal Simek 
2831d333e69SMichal Simek /*
2841d333e69SMichal Simek  * Perform the very early platform specific architectural setup here.
2851d333e69SMichal Simek  */
bl31_plat_arch_setup(void)2861d333e69SMichal Simek void bl31_plat_arch_setup(void)
2871d333e69SMichal Simek {
2881d333e69SMichal Simek 	const mmap_region_t bl_regions[] = {
28946a08aabSAmit Nagal #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE))
29046a08aabSAmit Nagal 		MAP_REGION_FLAT(XILINX_OF_BOARD_DTB_ADDR, XILINX_OF_BOARD_DTB_MAX_SIZE,
29146a08aabSAmit Nagal 				MT_MEMORY | MT_RW | MT_NS),
29246a08aabSAmit Nagal #endif
2931d333e69SMichal Simek 		MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
2941d333e69SMichal Simek 			MT_MEMORY | MT_RW | MT_SECURE),
2951d333e69SMichal Simek 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
2961d333e69SMichal Simek 				MT_CODE | MT_SECURE),
2971d333e69SMichal Simek 		MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
2981d333e69SMichal Simek 				MT_RO_DATA | MT_SECURE),
2991d333e69SMichal Simek 		{0}
3001d333e69SMichal Simek 	};
3011d333e69SMichal Simek 
30251564354SPrasad Kummari 	setup_page_tables(bl_regions, plat_get_mmap());
3031d333e69SMichal Simek 	enable_mmu(0);
3041d333e69SMichal Simek }
305