xref: /rk3399_ARM-atf/plat/arm/common/sp_min/arm_sp_min_setup.c (revision ddc1fcee5df039c3c4d0105831b2e0b5a6f1f566)
1181bbd41SSoby Mathew /*
289213498SHarrison Mutai  * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved.
3181bbd41SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5181bbd41SSoby Mathew  */
6181bbd41SSoby Mathew 
7181bbd41SSoby Mathew #include <assert.h>
809d40e0eSAntonio Nino Diaz 
9181bbd41SSoby Mathew #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <bl32/sp_min/platform_sp_min.h>
1209d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <drivers/console.h>
1509d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
16bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1809d40e0eSAntonio Nino Diaz 
19abdb953bSHarrison Mutai struct transfer_list_header *secure_tl;
20abdb953bSHarrison Mutai struct transfer_list_header *ns_tl __unused;
21abdb953bSHarrison Mutai 
22181bbd41SSoby Mathew static entry_point_info_t bl33_image_ep_info;
23181bbd41SSoby Mathew 
2475170704SBoyan Karatotev #if USE_GIC_DRIVER == 3
2575170704SBoyan Karatotev static const uintptr_t gicr_base_addrs[2] = {
2675170704SBoyan Karatotev 	PLAT_ARM_GICR_BASE,	/* GICR Base address of the primary CPU */
2775170704SBoyan Karatotev 	0U			/* Zero Termination */
2875170704SBoyan Karatotev };
2975170704SBoyan Karatotev #endif
3075170704SBoyan Karatotev 
31181bbd41SSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */
32181bbd41SSoby Mathew #pragma weak sp_min_platform_setup
33181bbd41SSoby Mathew #pragma weak sp_min_plat_arch_setup
340ed8c001SSoby Mathew #pragma weak plat_arm_sp_min_early_platform_setup
35181bbd41SSoby Mathew 
36d323af9eSDaniel Boulby #define MAP_BL_SP_MIN_TOTAL	MAP_REGION_FLAT(			\
37d323af9eSDaniel Boulby 					BL32_BASE,			\
38d323af9eSDaniel Boulby 					BL32_END - BL32_BASE,		\
39d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
40d323af9eSDaniel Boulby 
41abdb953bSHarrison Mutai #define MAP_EL3_FW_HANDOFF                            \
42abdb953bSHarrison Mutai 	MAP_REGION_FLAT(PLAT_ARM_EL3_FW_HANDOFF_BASE, \
43abdb953bSHarrison Mutai 			PLAT_ARM_FW_HANDOFF_SIZE, MT_MEMORY | MT_RW | EL3_PAS)
44abdb953bSHarrison Mutai 
45abdb953bSHarrison Mutai #define MAP_FW_NS_HANDOFF                                             \
46abdb953bSHarrison Mutai 	MAP_REGION_FLAT(FW_NS_HANDOFF_BASE, PLAT_ARM_FW_HANDOFF_SIZE, \
47abdb953bSHarrison Mutai 			MT_MEMORY | MT_RW | MT_NS)
48abdb953bSHarrison Mutai 
49c099cd39SSoby Mathew /*
5004e06973SManish V Badarkhe  * Check that BL32_BASE is above ARM_FW_CONFIG_LIMIT. The reserved page
51c099cd39SSoby Mathew  * is required for SOC_FW_CONFIG/TOS_FW_CONFIG passed from BL2.
52c099cd39SSoby Mathew  */
537285fd5fSManish Pandey #if !RESET_TO_SP_MIN
54abdb953bSHarrison Mutai #if TRANSFER_LIST
55abdb953bSHarrison Mutai CASSERT(BL32_BASE >= PLAT_ARM_EL3_FW_HANDOFF_LIMIT, assert_bl32_base_overflows);
56abdb953bSHarrison Mutai #else
5704e06973SManish V Badarkhe CASSERT(BL32_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl32_base_overflows);
587285fd5fSManish Pandey #endif
59abdb953bSHarrison Mutai #endif
60181bbd41SSoby Mathew 
61181bbd41SSoby Mathew /*******************************************************************************
62181bbd41SSoby Mathew  * Return a pointer to the 'entry_point_info' structure of the next image for the
63181bbd41SSoby Mathew  * security state specified. BL33 corresponds to the non-secure image type
64181bbd41SSoby Mathew  * while BL32 corresponds to the secure image type. A NULL pointer is returned
65181bbd41SSoby Mathew  * if the image does not exist.
66181bbd41SSoby Mathew  ******************************************************************************/
sp_min_plat_get_bl33_ep_info(void)67181bbd41SSoby Mathew entry_point_info_t *sp_min_plat_get_bl33_ep_info(void)
68181bbd41SSoby Mathew {
69181bbd41SSoby Mathew 	entry_point_info_t *next_image_info;
70181bbd41SSoby Mathew 
71181bbd41SSoby Mathew 	next_image_info = &bl33_image_ep_info;
72181bbd41SSoby Mathew 
73181bbd41SSoby Mathew 	/*
74181bbd41SSoby Mathew 	 * None of the images on the ARM development platforms can have 0x0
75181bbd41SSoby Mathew 	 * as the entrypoint
76181bbd41SSoby Mathew 	 */
77181bbd41SSoby Mathew 	if (next_image_info->pc)
78181bbd41SSoby Mathew 		return next_image_info;
79181bbd41SSoby Mathew 	else
80181bbd41SSoby Mathew 		return NULL;
81181bbd41SSoby Mathew }
82181bbd41SSoby Mathew 
83181bbd41SSoby Mathew /*******************************************************************************
840ed8c001SSoby Mathew  * Utility function to perform early platform setup.
85181bbd41SSoby Mathew  ******************************************************************************/
arm_sp_min_early_platform_setup(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)8689213498SHarrison Mutai void arm_sp_min_early_platform_setup(u_register_t arg0, u_register_t arg1,
8789213498SHarrison Mutai 			u_register_t arg2, u_register_t arg3)
88181bbd41SSoby Mathew {
89abdb953bSHarrison Mutai 	struct transfer_list_entry *te __unused;
90abdb953bSHarrison Mutai 
91181bbd41SSoby Mathew 	/* Initialize the console to provide early debug support */
927e2bbef9SDaniel Boulby 	arm_console_boot_init();
93181bbd41SSoby Mathew 
94abdb953bSHarrison Mutai #if TRANSFER_LIST
95abdb953bSHarrison Mutai 	secure_tl = (struct transfer_list_header *)arg3;
96abdb953bSHarrison Mutai 
97abdb953bSHarrison Mutai 	te = transfer_list_find(secure_tl, TL_TAG_EXEC_EP_INFO32);
98abdb953bSHarrison Mutai 	assert(te != NULL);
99abdb953bSHarrison Mutai 
100abdb953bSHarrison Mutai 	bl33_image_ep_info =
101abdb953bSHarrison Mutai 		*(struct entry_point_info *)transfer_list_entry_data(te);
102abdb953bSHarrison Mutai 	return;
103abdb953bSHarrison Mutai #endif /* TRANSFER_LIST */
104abdb953bSHarrison Mutai 
105d9915518SYatharth Kochar #if RESET_TO_SP_MIN
106181bbd41SSoby Mathew 	/* Populate entry point information for BL33 */
10701907f3fSHarrison Mutai 	SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
108181bbd41SSoby Mathew 	/*
109181bbd41SSoby Mathew 	 * Tell SP_MIN where the non-trusted software image
110181bbd41SSoby Mathew 	 * is located and the entry state information
111181bbd41SSoby Mathew 	 */
112181bbd41SSoby Mathew 	bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
11301907f3fSHarrison Mutai 	bl33_image_ep_info.spsr = arm_get_spsr(BL33_IMAGE_ID);
114181bbd41SSoby Mathew 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
115d9915518SYatharth Kochar 
116ed2c4f4aSManish Pandey #if ARM_LINUX_KERNEL_AS_BL33
117ed2c4f4aSManish Pandey 	/*
118ed2c4f4aSManish Pandey 	 * According to the file ``Documentation/arm/Booting`` of the Linux
119ed2c4f4aSManish Pandey 	 * kernel tree, Linux expects:
120ed2c4f4aSManish Pandey 	 * r0 = 0
121ed2c4f4aSManish Pandey 	 * r1 = machine type number, optional in DT-only platforms (~0 if so)
122ed2c4f4aSManish Pandey 	 * r2 = Physical address of the device tree blob
123ed2c4f4aSManish Pandey 	 */
124ed2c4f4aSManish Pandey 	bl33_image_ep_info.args.arg0 = 0U;
125ed2c4f4aSManish Pandey 	bl33_image_ep_info.args.arg1 = ~0U;
126ed2c4f4aSManish Pandey 	bl33_image_ep_info.args.arg2 = (u_register_t)ARM_PRELOADED_DTB_BASE;
127ed2c4f4aSManish Pandey # endif
128ed2c4f4aSManish Pandey 
129d9915518SYatharth Kochar #else /* RESET_TO_SP_MIN */
130d9915518SYatharth Kochar 
131d9915518SYatharth Kochar 	/*
132d9915518SYatharth Kochar 	 * Check params passed from BL2 should not be NULL,
133d9915518SYatharth Kochar 	 */
13489213498SHarrison Mutai 	bl_params_t *params_from_bl2 = (bl_params_t *)arg0;
135d9915518SYatharth Kochar 	assert(params_from_bl2 != NULL);
136d9915518SYatharth Kochar 	assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
137d9915518SYatharth Kochar 	assert(params_from_bl2->h.version >= VERSION_2);
138d9915518SYatharth Kochar 
139d9915518SYatharth Kochar 	bl_params_node_t *bl_params = params_from_bl2->head;
140d9915518SYatharth Kochar 
141d9915518SYatharth Kochar 	/*
142d9915518SYatharth Kochar 	 * Copy BL33 entry point information.
143d9915518SYatharth Kochar 	 * They are stored in Secure RAM, in BL2's address space.
144d9915518SYatharth Kochar 	 */
145d9915518SYatharth Kochar 	while (bl_params) {
146d9915518SYatharth Kochar 		if (bl_params->image_id == BL33_IMAGE_ID) {
147d9915518SYatharth Kochar 			bl33_image_ep_info = *bl_params->ep_info;
148d9915518SYatharth Kochar 			break;
149181bbd41SSoby Mathew 		}
150181bbd41SSoby Mathew 
151d9915518SYatharth Kochar 		bl_params = bl_params->next_params_info;
152d9915518SYatharth Kochar 	}
153d9915518SYatharth Kochar 
154d9915518SYatharth Kochar 	if (bl33_image_ep_info.pc == 0)
155d9915518SYatharth Kochar 		panic();
156d9915518SYatharth Kochar 
157d9915518SYatharth Kochar #endif /* RESET_TO_SP_MIN */
158d9915518SYatharth Kochar 
159d9915518SYatharth Kochar }
160d9915518SYatharth Kochar 
1610ed8c001SSoby Mathew /*******************************************************************************
1620ed8c001SSoby Mathew  * Default implementation for sp_min_platform_setup2() for ARM platforms
1630ed8c001SSoby Mathew  ******************************************************************************/
plat_arm_sp_min_early_platform_setup(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)1640ed8c001SSoby Mathew void plat_arm_sp_min_early_platform_setup(u_register_t arg0, u_register_t arg1,
1650c306cc0SSoby Mathew 			u_register_t arg2, u_register_t arg3)
166181bbd41SSoby Mathew {
16789213498SHarrison Mutai 	arm_sp_min_early_platform_setup(arg0, arg1, arg2, arg3);
168181bbd41SSoby Mathew 
169*36fbcf4dSAhmed Azeem #if !HW_ASSISTED_COHERENCY
170181bbd41SSoby Mathew 	/*
171181bbd41SSoby Mathew 	 * Initialize Interconnect for this cluster during cold boot.
172181bbd41SSoby Mathew 	 * No need for locks as no other CPU is active.
173181bbd41SSoby Mathew 	 */
174181bbd41SSoby Mathew 	plat_arm_interconnect_init();
175181bbd41SSoby Mathew 
176181bbd41SSoby Mathew 	/*
177181bbd41SSoby Mathew 	 * Enable Interconnect coherency for the primary CPU's cluster.
178181bbd41SSoby Mathew 	 * Earlier bootloader stages might already do this (e.g. Trusted
179181bbd41SSoby Mathew 	 * Firmware's BL1 does it) but we can't assume so. There is no harm in
180181bbd41SSoby Mathew 	 * executing this code twice anyway.
181181bbd41SSoby Mathew 	 * Platform specific PSCI code will enable coherency for other
182181bbd41SSoby Mathew 	 * clusters.
183181bbd41SSoby Mathew 	 */
184181bbd41SSoby Mathew 	plat_arm_interconnect_enter_coherency();
185*36fbcf4dSAhmed Azeem #endif
186181bbd41SSoby Mathew }
187181bbd41SSoby Mathew 
sp_min_early_platform_setup2(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)1880ed8c001SSoby Mathew void sp_min_early_platform_setup2(u_register_t arg0, u_register_t arg1,
1890ed8c001SSoby Mathew 			u_register_t arg2, u_register_t arg3)
1900ed8c001SSoby Mathew {
1910ed8c001SSoby Mathew 	plat_arm_sp_min_early_platform_setup(arg0, arg1, arg2, arg3);
1920ed8c001SSoby Mathew }
1930ed8c001SSoby Mathew 
194181bbd41SSoby Mathew /*******************************************************************************
19521568304SDimitris Papastamos  * Perform any SP_MIN platform runtime setup prior to SP_MIN exit.
19621568304SDimitris Papastamos  * Common to ARM standard platforms.
19721568304SDimitris Papastamos  ******************************************************************************/
arm_sp_min_plat_runtime_setup(void)19821568304SDimitris Papastamos void arm_sp_min_plat_runtime_setup(void)
19921568304SDimitris Papastamos {
20021568304SDimitris Papastamos 	/* Initialize the runtime console */
2017e2bbef9SDaniel Boulby 	arm_console_runtime_init();
20260e8f3cfSPetre-Ionut Tudor 
20360e8f3cfSPetre-Ionut Tudor #if PLAT_RO_XLAT_TABLES
20460e8f3cfSPetre-Ionut Tudor 	arm_xlat_make_tables_readonly();
20560e8f3cfSPetre-Ionut Tudor #endif
20621568304SDimitris Papastamos }
20721568304SDimitris Papastamos 
20821568304SDimitris Papastamos /*******************************************************************************
209181bbd41SSoby Mathew  * Perform platform specific setup for SP_MIN
210181bbd41SSoby Mathew  ******************************************************************************/
sp_min_platform_setup(void)211181bbd41SSoby Mathew void sp_min_platform_setup(void)
212181bbd41SSoby Mathew {
213abdb953bSHarrison Mutai 	struct transfer_list_entry *te __unused;
214abdb953bSHarrison Mutai 
215181bbd41SSoby Mathew 	/* Initialize the GIC driver, cpu and distributor interfaces */
2165d893410SBoyan Karatotev 	unsigned int core_pos = plat_my_core_pos();
2175d893410SBoyan Karatotev 
21875170704SBoyan Karatotev #if USE_GIC_DRIVER == 3
21975170704SBoyan Karatotev 	gic_set_gicr_frames(gicr_base_addrs);
22075170704SBoyan Karatotev #endif
2215d893410SBoyan Karatotev 	gic_init(core_pos);
2225d893410SBoyan Karatotev 	gic_pcpu_init(core_pos);
2235d893410SBoyan Karatotev 	gic_cpuif_enable(core_pos);
224181bbd41SSoby Mathew 
225abdb953bSHarrison Mutai #if TRANSFER_LIST
226abdb953bSHarrison Mutai 	ns_tl = transfer_list_ensure((void *)FW_NS_HANDOFF_BASE,
227abdb953bSHarrison Mutai 				       PLAT_ARM_FW_HANDOFF_SIZE);
228abdb953bSHarrison Mutai 	if (ns_tl == NULL) {
229abdb953bSHarrison Mutai 		ERROR("Non-secure transfer list initialisation failed!\n");
230abdb953bSHarrison Mutai 		panic();
231abdb953bSHarrison Mutai 	}
232abdb953bSHarrison Mutai 
233abdb953bSHarrison Mutai 	te = transfer_list_find(secure_tl, TL_TAG_FDT);
234abdb953bSHarrison Mutai 	if (te != NULL) {
235abdb953bSHarrison Mutai 		te = transfer_list_add(ns_tl, TL_TAG_FDT, te->data_size,
236abdb953bSHarrison Mutai 				  (void *)transfer_list_entry_data(te));
237abdb953bSHarrison Mutai 		if (te == NULL) {
238abdb953bSHarrison Mutai 			ERROR("Failed to relocate device tree into non-secure memory.\n");
239abdb953bSHarrison Mutai 			panic();
240abdb953bSHarrison Mutai 		}
241abdb953bSHarrison Mutai 	}
242abdb953bSHarrison Mutai 
243abdb953bSHarrison Mutai 	transfer_list_set_handoff_args(ns_tl, &bl33_image_ep_info);
244abdb953bSHarrison Mutai #endif
245abdb953bSHarrison Mutai 
246181bbd41SSoby Mathew 	/*
247181bbd41SSoby Mathew 	 * Do initial security configuration to allow DRAM/device access
248181bbd41SSoby Mathew 	 * (if earlier BL has not already done so).
249181bbd41SSoby Mathew 	 */
250ae0e09bbSManish V Badarkhe #if RESET_TO_SP_MIN && !JUNO_AARCH32_EL3_RUNTIME
251181bbd41SSoby Mathew 	plat_arm_security_setup();
252638b034cSRoberto Vargas 
253638b034cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
254638b034cSRoberto Vargas 	arm_nor_psci_do_dyn_mem_protect();
255638b034cSRoberto Vargas #endif /* PLAT_ARM_MEM_PROT_ADDR */
256638b034cSRoberto Vargas 
257d9915518SYatharth Kochar #endif
258181bbd41SSoby Mathew 
259181bbd41SSoby Mathew 	/* Enable and initialize the System level generic timer */
2606393c787SUsama Arif #ifdef ARM_SYS_CNTCTL_BASE
261181bbd41SSoby Mathew 	mmio_write_32(ARM_SYS_CNTCTL_BASE + CNTCR_OFF,
262c9512bcaSAntonio Nino Diaz 			CNTCR_FCREQ(0U) | CNTCR_EN);
2636393c787SUsama Arif #endif
2646393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE
265181bbd41SSoby Mathew 	/* Allow access to the System counter timer module */
266181bbd41SSoby Mathew 	arm_configure_sys_timer();
2676393c787SUsama Arif #endif
268181bbd41SSoby Mathew 	/* Initialize power controller before setting up topology */
269181bbd41SSoby Mathew 	plat_arm_pwrc_setup();
270181bbd41SSoby Mathew }
271181bbd41SSoby Mathew 
sp_min_plat_runtime_setup(void)27221568304SDimitris Papastamos void sp_min_plat_runtime_setup(void)
27321568304SDimitris Papastamos {
27421568304SDimitris Papastamos 	arm_sp_min_plat_runtime_setup();
27521568304SDimitris Papastamos }
27621568304SDimitris Papastamos 
277181bbd41SSoby Mathew /*******************************************************************************
278181bbd41SSoby Mathew  * Perform the very early platform specific architectural setup here. At the
279181bbd41SSoby Mathew  * moment this only initializes the MMU
280181bbd41SSoby Mathew  ******************************************************************************/
arm_sp_min_plat_arch_setup(void)28126d1e0c3SMadhukar Pappireddy void arm_sp_min_plat_arch_setup(void)
282181bbd41SSoby Mathew {
283d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
284d323af9eSDaniel Boulby 		MAP_BL_SP_MIN_TOTAL,
2852ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
286181bbd41SSoby Mathew #if USE_COHERENT_MEM
287d323af9eSDaniel Boulby 		ARM_MAP_BL_COHERENT_RAM,
288181bbd41SSoby Mathew #endif
289abdb953bSHarrison Mutai #if TRANSFER_LIST
290abdb953bSHarrison Mutai 		MAP_EL3_FW_HANDOFF,
291abdb953bSHarrison Mutai 		MAP_FW_NS_HANDOFF,
292abdb953bSHarrison Mutai #endif
293d323af9eSDaniel Boulby 		{0}
294d323af9eSDaniel Boulby 	};
295d323af9eSDaniel Boulby 
2960916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
297181bbd41SSoby Mathew 
2981e54cbb8SAntonio Nino Diaz 	enable_mmu_svc_mon(0);
299181bbd41SSoby Mathew }
30026d1e0c3SMadhukar Pappireddy 
sp_min_plat_arch_setup(void)30126d1e0c3SMadhukar Pappireddy void sp_min_plat_arch_setup(void)
30226d1e0c3SMadhukar Pappireddy {
30326d1e0c3SMadhukar Pappireddy 	arm_sp_min_plat_arch_setup();
30426d1e0c3SMadhukar Pappireddy }
305