xref: /rk3399_ARM-atf/plat/arm/common/arm_bl2_setup.c (revision a5566f65fd1be689ca5c63baa1f5b61b40960c8d)
1b4315306SDan Handley /*
286e4859aSRohit Mathew  * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5b4315306SDan Handley  */
6b4315306SDan Handley 
7a8aa7fecSYatharth Kochar #include <assert.h>
8b4315306SDan Handley #include <string.h>
909d40e0eSAntonio Nino Diaz 
1009d40e0eSAntonio Nino Diaz #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz 
12deb4b3a6SZelalem Aweke #include <arch_features.h>
1309d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1509d40e0eSAntonio Nino Diaz #include <common/debug.h>
1609d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
1709d40e0eSAntonio Nino Diaz #include <drivers/generic_delay_timer.h>
18ef1daa42SManish V Badarkhe #include <drivers/partition/partition.h>
199814bfc1SLouis Mayencourt #include <lib/fconf/fconf.h>
2082869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
21f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h>
22*a5566f65SHarrison Mutai #if TRANSFER_LIST
23*a5566f65SHarrison Mutai #include <lib/transfer_list.h>
24*a5566f65SHarrison Mutai #endif
2509d40e0eSAntonio Nino Diaz #ifdef SPD_opteed
2609d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h>
2709d40e0eSAntonio Nino Diaz #endif
2809d40e0eSAntonio Nino Diaz #include <lib/utils.h>
29bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
3009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
3109d40e0eSAntonio Nino Diaz 
32b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL2 */
33b4315306SDan Handley static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
34b4315306SDan Handley 
35a07c101aSManish V Badarkhe /* Base address of fw_config received from BL1 */
36d74c6b83SJimmy Brisson static uintptr_t config_base;
37a07c101aSManish V Badarkhe 
38caf4eca1SSoby Mathew /*
3904e06973SManish V Badarkhe  * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is
40c099cd39SSoby Mathew  * for `meminfo_t` data structure and fw_configs passed from BL1.
41caf4eca1SSoby Mathew  */
4204e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows);
43caf4eca1SSoby Mathew 
44a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */
450c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2
46a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup
47a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup
48a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout
49a8aa7fecSYatharth Kochar 
504bb72c47SZelalem Aweke #if ENABLE_RME
514bb72c47SZelalem Aweke #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
524bb72c47SZelalem Aweke 					bl2_tzram_layout.total_base,	\
534bb72c47SZelalem Aweke 					bl2_tzram_layout.total_size,	\
544bb72c47SZelalem Aweke 					MT_MEMORY | MT_RW | MT_ROOT)
554bb72c47SZelalem Aweke #else
56d323af9eSDaniel Boulby #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
57d323af9eSDaniel Boulby 					bl2_tzram_layout.total_base,	\
58d323af9eSDaniel Boulby 					bl2_tzram_layout.total_size,	\
59d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
604bb72c47SZelalem Aweke #endif /* ENABLE_RME */
614a581b06SDimitris Papastamos 
62490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load
634a581b06SDimitris Papastamos 
64*a5566f65SHarrison Mutai static struct transfer_list_header *secure_tl __unused;
65*a5566f65SHarrison Mutai static struct transfer_list_header *ns_tl __unused;
66*a5566f65SHarrison Mutai 
67b4315306SDan Handley /*******************************************************************************
68b4315306SDan Handley  * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
69b4315306SDan Handley  * in x0. This memory layout is sitting at the base of the free trusted SRAM.
70b4315306SDan Handley  * Copy it to a safe location before its reclaimed by later BL2 functionality.
71b4315306SDan Handley  ******************************************************************************/
7204e06973SManish V Badarkhe void arm_bl2_early_platform_setup(uintptr_t fw_config,
736c77e749SSandrine Bailleux 				  struct meminfo *mem_layout)
74b4315306SDan Handley {
7508ec77c7SGovindraj Raja 	int __maybe_unused ret;
7608ec77c7SGovindraj Raja 
77b4315306SDan Handley 	/* Initialize the console to provide early debug support */
7888a0523eSAntonio Nino Diaz 	arm_console_boot_init();
79b4315306SDan Handley 
80b4315306SDan Handley 	/* Setup the BL2 memory layout */
81b4315306SDan Handley 	bl2_tzram_layout = *mem_layout;
82b4315306SDan Handley 
83d74c6b83SJimmy Brisson 	config_base = fw_config;
849814bfc1SLouis Mayencourt 
85b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
86b4315306SDan Handley 	plat_arm_io_setup();
87ef1daa42SManish V Badarkhe 
88ef1daa42SManish V Badarkhe 	/* Load partition table */
89ef1daa42SManish V Badarkhe #if ARM_GPT_SUPPORT
9008ec77c7SGovindraj Raja 	ret = gpt_partition_init();
9108ec77c7SGovindraj Raja 	if (ret != 0) {
9208ec77c7SGovindraj Raja 		ERROR("GPT partition initialisation failed!\n");
9308ec77c7SGovindraj Raja 		panic();
9408ec77c7SGovindraj Raja 	}
95ef1daa42SManish V Badarkhe 
9608ec77c7SGovindraj Raja #endif /* ARM_GPT_SUPPORT */
97b4315306SDan Handley }
98b4315306SDan Handley 
990c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
100b4315306SDan Handley {
101cab0b5b0SSoby Mathew 	arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
102cab0b5b0SSoby Mathew 
10318e279ebSSoby Mathew 	generic_delay_timer_init();
104b4315306SDan Handley }
105b4315306SDan Handley 
106b4315306SDan Handley /*
1076e79f9fdSSoby Mathew  * Perform  BL2 preload setup. Currently we initialise the dynamic
1086e79f9fdSSoby Mathew  * configuration here.
109b4315306SDan Handley  */
1106e79f9fdSSoby Mathew void bl2_plat_preload_setup(void)
111b4315306SDan Handley {
112*a5566f65SHarrison Mutai #if TRANSFER_LIST
113*a5566f65SHarrison Mutai 	secure_tl = transfer_list_init((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
114*a5566f65SHarrison Mutai 				       PLAT_ARM_FW_HANDOFF_SIZE);
115*a5566f65SHarrison Mutai 	if (secure_tl == NULL) {
116*a5566f65SHarrison Mutai 		ERROR("Initialisation of secure transfer list failed!\n");
117*a5566f65SHarrison Mutai 		panic();
118*a5566f65SHarrison Mutai 	}
119*a5566f65SHarrison Mutai 
120*a5566f65SHarrison Mutai 	arm_transfer_list_dyn_cfg_init(secure_tl);
121*a5566f65SHarrison Mutai #else
122cab0b5b0SSoby Mathew 	arm_bl2_dyn_cfg_init();
123*a5566f65SHarrison Mutai #endif
124ef1daa42SManish V Badarkhe 
1252f1177b2SManish V Badarkhe #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT
1262f1177b2SManish V Badarkhe 	/* Always use the FIP from bank 0 */
1272f1177b2SManish V Badarkhe 	arm_set_fip_addr(0U);
1282f1177b2SManish V Badarkhe #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */
1296e79f9fdSSoby Mathew }
130cab0b5b0SSoby Mathew 
1316e79f9fdSSoby Mathew /*
1326e79f9fdSSoby Mathew  * Perform ARM standard platform setup.
1336e79f9fdSSoby Mathew  */
1346e79f9fdSSoby Mathew void arm_bl2_platform_setup(void)
1356e79f9fdSSoby Mathew {
136deb4b3a6SZelalem Aweke #if !ENABLE_RME
137b4315306SDan Handley 	/* Initialize the secure environment */
138b4315306SDan Handley 	plat_arm_security_setup();
139deb4b3a6SZelalem Aweke #endif
140f145403cSRoberto Vargas 
141f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
142638b034cSRoberto Vargas 	arm_nor_psci_do_static_mem_protect();
143f145403cSRoberto Vargas #endif
144*a5566f65SHarrison Mutai 
145*a5566f65SHarrison Mutai #if TRANSFER_LIST
146*a5566f65SHarrison Mutai 	ns_tl = transfer_list_init((void *)FW_NS_HANDOFF_BASE,
147*a5566f65SHarrison Mutai 				   PLAT_ARM_FW_HANDOFF_SIZE);
148*a5566f65SHarrison Mutai 
149*a5566f65SHarrison Mutai 	if (ns_tl == NULL) {
150*a5566f65SHarrison Mutai 		ERROR("Non-secure transfer list initialisation failed!");
151*a5566f65SHarrison Mutai 		panic();
152*a5566f65SHarrison Mutai 	}
153*a5566f65SHarrison Mutai #endif
154b4315306SDan Handley }
155b4315306SDan Handley 
156b4315306SDan Handley void bl2_platform_setup(void)
157b4315306SDan Handley {
158b4315306SDan Handley 	arm_bl2_platform_setup();
159b4315306SDan Handley }
160b4315306SDan Handley 
161b4315306SDan Handley /*******************************************************************************
162deb4b3a6SZelalem Aweke  * Perform the very early platform specific architectural setup here.
163deb4b3a6SZelalem Aweke  * When RME is enabled the secure environment is initialised before
164deb4b3a6SZelalem Aweke  * initialising and enabling Granule Protection.
165deb4b3a6SZelalem Aweke  * This function initialises the MMU in a quick and dirty way.
166b4315306SDan Handley  ******************************************************************************/
167b4315306SDan Handley void arm_bl2_plat_arch_setup(void)
168b4315306SDan Handley {
169b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM
170b65dfe40SSandrine Bailleux 	/* Ensure ARM platforms don't use coherent memory in BL2. */
171d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
172b4315306SDan Handley #endif
173d323af9eSDaniel Boulby 
174d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
175d323af9eSDaniel Boulby 		MAP_BL2_TOTAL,
1762ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
1771eb735d7SRoberto Vargas #if USE_ROMLIB
1781eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1791eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1801eb735d7SRoberto Vargas #endif
181a07c101aSManish V Badarkhe 		ARM_MAP_BL_CONFIG_REGION,
182c8720729SZelalem Aweke #if ENABLE_RME
183c8720729SZelalem Aweke 		ARM_MAP_L0_GPT_REGION,
184c8720729SZelalem Aweke #endif
185d323af9eSDaniel Boulby 		{0}
186d323af9eSDaniel Boulby 	};
187d323af9eSDaniel Boulby 
188deb4b3a6SZelalem Aweke #if ENABLE_RME
189deb4b3a6SZelalem Aweke 	/* Initialise the secure environment */
190deb4b3a6SZelalem Aweke 	plat_arm_security_setup();
191deb4b3a6SZelalem Aweke #endif
1920916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
1936fe8aa2fSYatharth Kochar 
194402b3cf8SJulius Werner #ifdef __aarch64__
195deb4b3a6SZelalem Aweke #if ENABLE_RME
196deb4b3a6SZelalem Aweke 	/* BL2 runs in EL3 when RME enabled. */
197deb4b3a6SZelalem Aweke 	assert(get_armv9_2_feat_rme_support() != 0U);
198deb4b3a6SZelalem Aweke 	enable_mmu_el3(0);
199f19dc624Sjohpow01 
200f19dc624Sjohpow01 	/* Initialise and enable granule protection after MMU. */
201341df6afSRohit Mathew 	arm_gpt_setup();
202deb4b3a6SZelalem Aweke #else
203b5fa6563SSandrine Bailleux 	enable_mmu_el1(0);
204deb4b3a6SZelalem Aweke #endif
205402b3cf8SJulius Werner #else
206402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
2076fe8aa2fSYatharth Kochar #endif
2081eb735d7SRoberto Vargas 
2091eb735d7SRoberto Vargas 	arm_setup_romlib();
210b4315306SDan Handley }
211b4315306SDan Handley 
212b4315306SDan Handley void bl2_plat_arch_setup(void)
213b4315306SDan Handley {
214a07c101aSManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
215a07c101aSManish V Badarkhe 
216b4315306SDan Handley 	arm_bl2_plat_arch_setup();
217a07c101aSManish V Badarkhe 
218a07c101aSManish V Badarkhe 	/* Fill the properties struct with the info from the config dtb */
219d74c6b83SJimmy Brisson 	fconf_populate("FW_CONFIG", config_base);
220a07c101aSManish V Badarkhe 
221a07c101aSManish V Badarkhe 	/* TB_FW_CONFIG was also loaded by BL1 */
222a07c101aSManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
223a07c101aSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
224a07c101aSManish V Badarkhe 
225a07c101aSManish V Badarkhe 	fconf_populate("TB_FW", tb_fw_config_info->config_addr);
226b4315306SDan Handley }
227b4315306SDan Handley 
22807570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id)
229a8aa7fecSYatharth Kochar {
230a8aa7fecSYatharth Kochar 	int err = 0;
231a8aa7fecSYatharth Kochar 	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
23254661cd2SSummer Qin #ifdef SPD_opteed
23354661cd2SSummer Qin 	bl_mem_params_node_t *pager_mem_params = NULL;
23454661cd2SSummer Qin 	bl_mem_params_node_t *paged_mem_params = NULL;
23554661cd2SSummer Qin #endif
236466bb285SZelalem 	assert(bl_mem_params != NULL);
237a8aa7fecSYatharth Kochar 
238a8aa7fecSYatharth Kochar 	switch (image_id) {
239402b3cf8SJulius Werner #ifdef __aarch64__
240a8aa7fecSYatharth Kochar 	case BL32_IMAGE_ID:
24154661cd2SSummer Qin #ifdef SPD_opteed
24254661cd2SSummer Qin 		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
24354661cd2SSummer Qin 		assert(pager_mem_params);
24454661cd2SSummer Qin 
24554661cd2SSummer Qin 		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
24654661cd2SSummer Qin 		assert(paged_mem_params);
24754661cd2SSummer Qin 
24854661cd2SSummer Qin 		err = parse_optee_header(&bl_mem_params->ep_info,
24954661cd2SSummer Qin 				&pager_mem_params->image_info,
25054661cd2SSummer Qin 				&paged_mem_params->image_info);
25154661cd2SSummer Qin 		if (err != 0) {
25254661cd2SSummer Qin 			WARN("OPTEE header parse error.\n");
25354661cd2SSummer Qin 		}
25454661cd2SSummer Qin #endif
255a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
256a8aa7fecSYatharth Kochar 		break;
2576fe8aa2fSYatharth Kochar #endif
258a8aa7fecSYatharth Kochar 
259a8aa7fecSYatharth Kochar 	case BL33_IMAGE_ID:
260a8aa7fecSYatharth Kochar 		/* BL33 expects to receive the primary CPU MPID (through r0) */
261a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
262a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
263a8aa7fecSYatharth Kochar 		break;
264a8aa7fecSYatharth Kochar 
265a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE
266a8aa7fecSYatharth Kochar 	case SCP_BL2_IMAGE_ID:
267a8aa7fecSYatharth Kochar 		/* The subsequent handling of SCP_BL2 is platform specific */
268a8aa7fecSYatharth Kochar 		err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
269a8aa7fecSYatharth Kochar 		if (err) {
270a8aa7fecSYatharth Kochar 			WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
271a8aa7fecSYatharth Kochar 		}
272a8aa7fecSYatharth Kochar 		break;
273a8aa7fecSYatharth Kochar #endif
274649c48f5SJonathan Wright 	default:
275649c48f5SJonathan Wright 		/* Do nothing in default case */
276649c48f5SJonathan Wright 		break;
277a8aa7fecSYatharth Kochar 	}
278a8aa7fecSYatharth Kochar 
279a8aa7fecSYatharth Kochar 	return err;
280a8aa7fecSYatharth Kochar }
281a8aa7fecSYatharth Kochar 
28207570d59SYatharth Kochar /*******************************************************************************
28307570d59SYatharth Kochar  * This function can be used by the platforms to update/use image
28407570d59SYatharth Kochar  * information for given `image_id`.
28507570d59SYatharth Kochar  ******************************************************************************/
286490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
28707570d59SYatharth Kochar {
28846789a7cSBalint Dobszay #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD
289cb3b5344SManish Pandey 	/* For Secure Partitions we don't need post processing */
290cb3b5344SManish Pandey 	if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) &&
291cb3b5344SManish Pandey 		(image_id < MAX_NUMBER_IDS)) {
292cb3b5344SManish Pandey 		return 0;
293cb3b5344SManish Pandey 	}
294cb3b5344SManish Pandey #endif
295*a5566f65SHarrison Mutai 
296*a5566f65SHarrison Mutai #if TRANSFER_LIST
297*a5566f65SHarrison Mutai 	if (image_id == HW_CONFIG_ID) {
298*a5566f65SHarrison Mutai 		arm_transfer_list_copy_hw_config(secure_tl, ns_tl);
299*a5566f65SHarrison Mutai 	}
300*a5566f65SHarrison Mutai #endif /* TRANSFER_LIST */
301*a5566f65SHarrison Mutai 
30207570d59SYatharth Kochar 	return arm_bl2_handle_post_image_load(image_id);
30307570d59SYatharth Kochar }
304*a5566f65SHarrison Mutai 
305*a5566f65SHarrison Mutai void arm_bl2_setup_next_ep_info(bl_mem_params_node_t *next_param_node)
306*a5566f65SHarrison Mutai {
307*a5566f65SHarrison Mutai 	assert(transfer_list_set_handoff_args(
308*a5566f65SHarrison Mutai 		       secure_tl, &next_param_node->ep_info) != NULL);
309*a5566f65SHarrison Mutai 
310*a5566f65SHarrison Mutai 	arm_transfer_list_populate_ep_info(next_param_node, secure_tl, ns_tl);
311*a5566f65SHarrison Mutai }
312