xref: /rk3399_ARM-atf/plat/arm/common/arm_bl2_setup.c (revision fe94a21a6815fc8623074e7184d87583f2f58940)
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>
22a5566f65SHarrison Mutai #if TRANSFER_LIST
23a5566f65SHarrison Mutai #include <lib/transfer_list.h>
24a5566f65SHarrison 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 */
369c11ed7eSHarrison Mutai static uintptr_t config_base __unused;
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  */
429c11ed7eSHarrison Mutai #if TRANSFER_LIST
439c11ed7eSHarrison Mutai CASSERT(BL2_BASE >= PLAT_ARM_EL3_FW_HANDOFF_BASE + PLAT_ARM_FW_HANDOFF_SIZE,
449c11ed7eSHarrison Mutai 	assert_bl2_base_overflows);
459c11ed7eSHarrison Mutai #else
4604e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows);
479c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */
48caf4eca1SSoby Mathew 
49a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */
500c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2
51a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup
52a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup
53a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout
54a8aa7fecSYatharth Kochar 
554bb72c47SZelalem Aweke #if ENABLE_RME
564bb72c47SZelalem Aweke #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
574bb72c47SZelalem Aweke 					bl2_tzram_layout.total_base,	\
584bb72c47SZelalem Aweke 					bl2_tzram_layout.total_size,	\
594bb72c47SZelalem Aweke 					MT_MEMORY | MT_RW | MT_ROOT)
604bb72c47SZelalem Aweke #else
61d323af9eSDaniel Boulby #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
62d323af9eSDaniel Boulby 					bl2_tzram_layout.total_base,	\
63d323af9eSDaniel Boulby 					bl2_tzram_layout.total_size,	\
64d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
654bb72c47SZelalem Aweke #endif /* ENABLE_RME */
664a581b06SDimitris Papastamos 
67490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load
684a581b06SDimitris Papastamos 
69a5566f65SHarrison Mutai static struct transfer_list_header *secure_tl __unused;
70a5566f65SHarrison Mutai static struct transfer_list_header *ns_tl __unused;
71a5566f65SHarrison Mutai 
72b4315306SDan Handley /*******************************************************************************
73b4315306SDan Handley  * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
74b4315306SDan Handley  * in x0. This memory layout is sitting at the base of the free trusted SRAM.
75b4315306SDan Handley  * Copy it to a safe location before its reclaimed by later BL2 functionality.
76b4315306SDan Handley  ******************************************************************************/
7704e06973SManish V Badarkhe void arm_bl2_early_platform_setup(uintptr_t fw_config,
786c77e749SSandrine Bailleux 				  struct meminfo *mem_layout)
79b4315306SDan Handley {
809c11ed7eSHarrison Mutai 	struct transfer_list_entry *te __unused;
8108ec77c7SGovindraj Raja 	int __maybe_unused ret;
8208ec77c7SGovindraj Raja 
83b4315306SDan Handley 	/* Initialize the console to provide early debug support */
8488a0523eSAntonio Nino Diaz 	arm_console_boot_init();
85b4315306SDan Handley 
869c11ed7eSHarrison Mutai #if TRANSFER_LIST
879c11ed7eSHarrison Mutai 	// TODO: modify the prototype of this function fw_config != bl2_tl
889c11ed7eSHarrison Mutai 	secure_tl = (struct transfer_list_header *)fw_config;
899c11ed7eSHarrison Mutai 
909c11ed7eSHarrison Mutai 	te = transfer_list_find(secure_tl, TL_TAG_SRAM_LAYOUT64);
919c11ed7eSHarrison Mutai 	assert(te != NULL);
929c11ed7eSHarrison Mutai 
939c11ed7eSHarrison Mutai 	bl2_tzram_layout = *(meminfo_t *)transfer_list_entry_data(te);
949c11ed7eSHarrison Mutai 	transfer_list_rem(secure_tl, te);
959c11ed7eSHarrison Mutai #else
969c11ed7eSHarrison Mutai 	config_base = fw_config;
979c11ed7eSHarrison Mutai 
98b4315306SDan Handley 	/* Setup the BL2 memory layout */
99b4315306SDan Handley 	bl2_tzram_layout = *mem_layout;
1009c11ed7eSHarrison Mutai #endif
1019814bfc1SLouis Mayencourt 
102b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
103b4315306SDan Handley 	plat_arm_io_setup();
104ef1daa42SManish V Badarkhe 
105ef1daa42SManish V Badarkhe 	/* Load partition table */
106ef1daa42SManish V Badarkhe #if ARM_GPT_SUPPORT
10708ec77c7SGovindraj Raja 	ret = gpt_partition_init();
10808ec77c7SGovindraj Raja 	if (ret != 0) {
10908ec77c7SGovindraj Raja 		ERROR("GPT partition initialisation failed!\n");
11008ec77c7SGovindraj Raja 		panic();
11108ec77c7SGovindraj Raja 	}
112ef1daa42SManish V Badarkhe 
11308ec77c7SGovindraj Raja #endif /* ARM_GPT_SUPPORT */
114b4315306SDan Handley }
115b4315306SDan Handley 
1160c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
117b4315306SDan Handley {
118cab0b5b0SSoby Mathew 	arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
119cab0b5b0SSoby Mathew 
12018e279ebSSoby Mathew 	generic_delay_timer_init();
121b4315306SDan Handley }
122b4315306SDan Handley 
123b4315306SDan Handley /*
1246e79f9fdSSoby Mathew  * Perform  BL2 preload setup. Currently we initialise the dynamic
1256e79f9fdSSoby Mathew  * configuration here.
126b4315306SDan Handley  */
1276e79f9fdSSoby Mathew void bl2_plat_preload_setup(void)
128b4315306SDan Handley {
129a5566f65SHarrison Mutai #if TRANSFER_LIST
130f019c801SHarrison Mutai /* Assume the secure TL hasn't been initialised if BL2 is running at EL3. */
131f019c801SHarrison Mutai #if RESET_TO_BL2
132f019c801SHarrison Mutai 	secure_tl = transfer_list_init((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
133f019c801SHarrison Mutai 				       PLAT_ARM_FW_HANDOFF_SIZE);
134f019c801SHarrison Mutai 
135f019c801SHarrison Mutai 	if (secure_tl == NULL) {
136f019c801SHarrison Mutai 		ERROR("Secure transfer list initialisation failed!\n");
137f019c801SHarrison Mutai 		panic();
138f019c801SHarrison Mutai 	}
139f019c801SHarrison Mutai #endif
140f019c801SHarrison Mutai 
141a5566f65SHarrison Mutai 	arm_transfer_list_dyn_cfg_init(secure_tl);
142a5566f65SHarrison Mutai #else
143cab0b5b0SSoby Mathew 	arm_bl2_dyn_cfg_init();
144a5566f65SHarrison Mutai #endif
145ef1daa42SManish V Badarkhe 
1462f1177b2SManish V Badarkhe #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT
1472f1177b2SManish V Badarkhe 	/* Always use the FIP from bank 0 */
1482f1177b2SManish V Badarkhe 	arm_set_fip_addr(0U);
1492f1177b2SManish V Badarkhe #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */
1506e79f9fdSSoby Mathew }
151cab0b5b0SSoby Mathew 
1526e79f9fdSSoby Mathew /*
1536e79f9fdSSoby Mathew  * Perform ARM standard platform setup.
1546e79f9fdSSoby Mathew  */
1556e79f9fdSSoby Mathew void arm_bl2_platform_setup(void)
1566e79f9fdSSoby Mathew {
157deb4b3a6SZelalem Aweke #if !ENABLE_RME
158b4315306SDan Handley 	/* Initialize the secure environment */
159b4315306SDan Handley 	plat_arm_security_setup();
160deb4b3a6SZelalem Aweke #endif
161f145403cSRoberto Vargas 
162f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
163638b034cSRoberto Vargas 	arm_nor_psci_do_static_mem_protect();
164f145403cSRoberto Vargas #endif
165b4315306SDan Handley }
166b4315306SDan Handley 
167b4315306SDan Handley void bl2_platform_setup(void)
168b4315306SDan Handley {
169b4315306SDan Handley 	arm_bl2_platform_setup();
170b4315306SDan Handley }
171b4315306SDan Handley 
172b4315306SDan Handley /*******************************************************************************
173deb4b3a6SZelalem Aweke  * Perform the very early platform specific architectural setup here.
174deb4b3a6SZelalem Aweke  * When RME is enabled the secure environment is initialised before
175deb4b3a6SZelalem Aweke  * initialising and enabling Granule Protection.
176deb4b3a6SZelalem Aweke  * This function initialises the MMU in a quick and dirty way.
177b4315306SDan Handley  ******************************************************************************/
178b4315306SDan Handley void arm_bl2_plat_arch_setup(void)
179b4315306SDan Handley {
180b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM
181b65dfe40SSandrine Bailleux 	/* Ensure ARM platforms don't use coherent memory in BL2. */
182d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
183b4315306SDan Handley #endif
184d323af9eSDaniel Boulby 
185d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
186d323af9eSDaniel Boulby 		MAP_BL2_TOTAL,
1872ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
1881eb735d7SRoberto Vargas #if USE_ROMLIB
1891eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1901eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1911eb735d7SRoberto Vargas #endif
1929c11ed7eSHarrison Mutai #if !TRANSFER_LIST
193a07c101aSManish V Badarkhe 		ARM_MAP_BL_CONFIG_REGION,
1949c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */
195c8720729SZelalem Aweke #if ENABLE_RME
196c8720729SZelalem Aweke 		ARM_MAP_L0_GPT_REGION,
197c8720729SZelalem Aweke #endif
198d323af9eSDaniel Boulby 		{ 0 }
199d323af9eSDaniel Boulby 	};
200d323af9eSDaniel Boulby 
201deb4b3a6SZelalem Aweke #if ENABLE_RME
202deb4b3a6SZelalem Aweke 	/* Initialise the secure environment */
203deb4b3a6SZelalem Aweke 	plat_arm_security_setup();
204deb4b3a6SZelalem Aweke #endif
2050916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
2066fe8aa2fSYatharth Kochar 
207402b3cf8SJulius Werner #ifdef __aarch64__
208deb4b3a6SZelalem Aweke #if ENABLE_RME
209deb4b3a6SZelalem Aweke 	/* BL2 runs in EL3 when RME enabled. */
210aaaf2cc3SSona Mathew 	assert(is_feat_rme_present());
211deb4b3a6SZelalem Aweke 	enable_mmu_el3(0);
212f19dc624Sjohpow01 
213f19dc624Sjohpow01 	/* Initialise and enable granule protection after MMU. */
214341df6afSRohit Mathew 	arm_gpt_setup();
215deb4b3a6SZelalem Aweke #else
216b5fa6563SSandrine Bailleux 	enable_mmu_el1(0);
217deb4b3a6SZelalem Aweke #endif
218402b3cf8SJulius Werner #else
219402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
2206fe8aa2fSYatharth Kochar #endif
2211eb735d7SRoberto Vargas 
2221eb735d7SRoberto Vargas 	arm_setup_romlib();
223b4315306SDan Handley }
224b4315306SDan Handley 
225b4315306SDan Handley void bl2_plat_arch_setup(void)
226b4315306SDan Handley {
2279c11ed7eSHarrison Mutai 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info __unused;
2289c11ed7eSHarrison Mutai 	struct transfer_list_entry *te __unused;
229b4315306SDan Handley 	arm_bl2_plat_arch_setup();
230a07c101aSManish V Badarkhe 
2319c11ed7eSHarrison Mutai #if TRANSFER_LIST
2329c11ed7eSHarrison Mutai 	te = transfer_list_find(secure_tl, TL_TAG_TB_FW_CONFIG);
2339c11ed7eSHarrison Mutai 	assert(te != NULL);
2349c11ed7eSHarrison Mutai 
2359c11ed7eSHarrison Mutai 	fconf_populate("TB_FW", (uintptr_t)transfer_list_entry_data(te));
2369c11ed7eSHarrison Mutai 	transfer_list_rem(secure_tl, te);
2379c11ed7eSHarrison Mutai #else
238a07c101aSManish V Badarkhe 	/* Fill the properties struct with the info from the config dtb */
239d74c6b83SJimmy Brisson 	fconf_populate("FW_CONFIG", config_base);
240a07c101aSManish V Badarkhe 
241a07c101aSManish V Badarkhe 	/* TB_FW_CONFIG was also loaded by BL1 */
242a07c101aSManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
243a07c101aSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
244a07c101aSManish V Badarkhe 
245a07c101aSManish V Badarkhe 	fconf_populate("TB_FW", tb_fw_config_info->config_addr);
2469c11ed7eSHarrison Mutai #endif
247b4315306SDan Handley }
248b4315306SDan Handley 
24907570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id)
250a8aa7fecSYatharth Kochar {
251a8aa7fecSYatharth Kochar 	int err = 0;
252a8aa7fecSYatharth Kochar 	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
25354661cd2SSummer Qin #ifdef SPD_opteed
25454661cd2SSummer Qin 	bl_mem_params_node_t *pager_mem_params = NULL;
25554661cd2SSummer Qin 	bl_mem_params_node_t *paged_mem_params = NULL;
25654661cd2SSummer Qin #endif
257466bb285SZelalem 	assert(bl_mem_params != NULL);
258a8aa7fecSYatharth Kochar 
259a8aa7fecSYatharth Kochar 	switch (image_id) {
260402b3cf8SJulius Werner #ifdef __aarch64__
261a8aa7fecSYatharth Kochar 	case BL32_IMAGE_ID:
26254661cd2SSummer Qin #ifdef SPD_opteed
26354661cd2SSummer Qin 		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
26454661cd2SSummer Qin 		assert(pager_mem_params);
26554661cd2SSummer Qin 
26654661cd2SSummer Qin 		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
26754661cd2SSummer Qin 		assert(paged_mem_params);
26854661cd2SSummer Qin 
26954661cd2SSummer Qin 		err = parse_optee_header(&bl_mem_params->ep_info,
27054661cd2SSummer Qin 				&pager_mem_params->image_info,
27154661cd2SSummer Qin 				&paged_mem_params->image_info);
27254661cd2SSummer Qin 		if (err != 0) {
27354661cd2SSummer Qin 			WARN("OPTEE header parse error.\n");
27454661cd2SSummer Qin 		}
27554661cd2SSummer Qin #endif
276a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
277a8aa7fecSYatharth Kochar 		break;
2786fe8aa2fSYatharth Kochar #endif
279a8aa7fecSYatharth Kochar 
280a8aa7fecSYatharth Kochar 	case BL33_IMAGE_ID:
281a8aa7fecSYatharth Kochar 		/* BL33 expects to receive the primary CPU MPID (through r0) */
282a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
283a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
284a8aa7fecSYatharth Kochar 		break;
285a8aa7fecSYatharth Kochar 
286a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE
287a8aa7fecSYatharth Kochar 	case SCP_BL2_IMAGE_ID:
288a8aa7fecSYatharth Kochar 		/* The subsequent handling of SCP_BL2 is platform specific */
289a8aa7fecSYatharth Kochar 		err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
290a8aa7fecSYatharth Kochar 		if (err) {
291a8aa7fecSYatharth Kochar 			WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
292a8aa7fecSYatharth Kochar 		}
293a8aa7fecSYatharth Kochar 		break;
294a8aa7fecSYatharth Kochar #endif
295649c48f5SJonathan Wright 	default:
296649c48f5SJonathan Wright 		/* Do nothing in default case */
297649c48f5SJonathan Wright 		break;
298a8aa7fecSYatharth Kochar 	}
299a8aa7fecSYatharth Kochar 
300a8aa7fecSYatharth Kochar 	return err;
301a8aa7fecSYatharth Kochar }
302a8aa7fecSYatharth Kochar 
30307570d59SYatharth Kochar /*******************************************************************************
30407570d59SYatharth Kochar  * This function can be used by the platforms to update/use image
30507570d59SYatharth Kochar  * information for given `image_id`.
30607570d59SYatharth Kochar  ******************************************************************************/
307490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
30807570d59SYatharth Kochar {
30946789a7cSBalint Dobszay #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD
310cb3b5344SManish Pandey 	/* For Secure Partitions we don't need post processing */
311cb3b5344SManish Pandey 	if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) &&
312cb3b5344SManish Pandey 		(image_id < MAX_NUMBER_IDS)) {
313cb3b5344SManish Pandey 		return 0;
314cb3b5344SManish Pandey 	}
315cb3b5344SManish Pandey #endif
316a5566f65SHarrison Mutai 
317a5566f65SHarrison Mutai #if TRANSFER_LIST
318a5566f65SHarrison Mutai 	if (image_id == HW_CONFIG_ID) {
319*fe94a21aSHarrison Mutai 		/* Refresh the now stale checksum following loading of HW_CONFIG into the TL. */
320*fe94a21aSHarrison Mutai 		transfer_list_update_checksum(secure_tl);
321a5566f65SHarrison Mutai 	}
322a5566f65SHarrison Mutai #endif /* TRANSFER_LIST */
323a5566f65SHarrison Mutai 
32407570d59SYatharth Kochar 	return arm_bl2_handle_post_image_load(image_id);
32507570d59SYatharth Kochar }
326a5566f65SHarrison Mutai 
327a5566f65SHarrison Mutai void arm_bl2_setup_next_ep_info(bl_mem_params_node_t *next_param_node)
328a5566f65SHarrison Mutai {
329cca1b72bSHarrison Mutai 	entry_point_info_t *ep __unused;
330cca1b72bSHarrison Mutai 	ep = transfer_list_set_handoff_args(secure_tl,
331cca1b72bSHarrison Mutai 					    &next_param_node->ep_info);
332cca1b72bSHarrison Mutai 	assert(ep != NULL);
333a5566f65SHarrison Mutai 
334*fe94a21aSHarrison Mutai 	arm_transfer_list_populate_ep_info(next_param_node, secure_tl);
335a5566f65SHarrison Mutai }
336