xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_setup.c (revision 9c11ed7e3e5536ad1fcb9190560e0368da9c5ab5)
1b4315306SDan Handley /*
2*9c11ed7eSHarrison Mutai  * 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 
7d323af9eSDaniel Boulby #include <assert.h>
809d40e0eSAntonio Nino Diaz 
94adb10c1SIsla Mitchell #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <arch.h>
1209d40e0eSAntonio Nino Diaz #include <bl1/bl1.h>
1309d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
14885e2683SClaus Pedersen #include <common/debug.h>
153b5ea741SLouis Mayencourt #include <lib/fconf/fconf.h>
1682869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
17*9c11ed7eSHarrison Mutai #if TRANSFER_LIST
18*9c11ed7eSHarrison Mutai #include <lib/transfer_list.h>
19*9c11ed7eSHarrison Mutai #endif
2009d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2109d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h>
22bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
2309d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2409d40e0eSAntonio Nino Diaz 
25b4315306SDan Handley /* Weak definitions may be overridden in specific ARM standard platform */
26b4315306SDan Handley #pragma weak bl1_early_platform_setup
27b4315306SDan Handley #pragma weak bl1_plat_arch_setup
28b4315306SDan Handley #pragma weak bl1_plat_sec_mem_layout
295fb061e7SGary Morrison #pragma weak arm_bl1_early_platform_setup
3007570d59SYatharth Kochar #pragma weak bl1_plat_prepare_exit
314da6f6cdSSathees Balya #pragma weak bl1_plat_get_next_image_id
324da6f6cdSSathees Balya #pragma weak plat_arm_bl1_fwu_needed
335fb061e7SGary Morrison #pragma weak arm_bl1_plat_arch_setup
34e31fb0faSlaurenw-arm #pragma weak arm_bl1_platform_setup
35b4315306SDan Handley 
36d323af9eSDaniel Boulby #define MAP_BL1_TOTAL		MAP_REGION_FLAT(			\
37d323af9eSDaniel Boulby 					bl1_tzram_layout.total_base,	\
38d323af9eSDaniel Boulby 					bl1_tzram_layout.total_size,	\
394bb72c47SZelalem Aweke 					MT_MEMORY | MT_RW | EL3_PAS)
402ecaafd2SDaniel Boulby /*
412ecaafd2SDaniel Boulby  * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section
422ecaafd2SDaniel Boulby  * otherwise one region is defined containing both
432ecaafd2SDaniel Boulby  */
442ecaafd2SDaniel Boulby #if SEPARATE_CODE_AND_RODATA
452ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
46d323af9eSDaniel Boulby 					BL_CODE_BASE,			\
47d323af9eSDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
484bb72c47SZelalem Aweke 					MT_CODE | EL3_PAS),		\
492ecaafd2SDaniel Boulby 				MAP_REGION_FLAT(			\
50d323af9eSDaniel Boulby 					BL1_RO_DATA_BASE,		\
51d323af9eSDaniel Boulby 					BL1_RO_DATA_END			\
52d323af9eSDaniel Boulby 						- BL_RO_DATA_BASE,	\
534bb72c47SZelalem Aweke 					MT_RO_DATA | EL3_PAS)
542ecaafd2SDaniel Boulby #else
552ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
562ecaafd2SDaniel Boulby 					BL_CODE_BASE,			\
572ecaafd2SDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
584bb72c47SZelalem Aweke 					MT_CODE | EL3_PAS)
592ecaafd2SDaniel Boulby #endif
60b4315306SDan Handley 
61b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL1*/
62b4315306SDan Handley static meminfo_t bl1_tzram_layout;
63b4315306SDan Handley 
64a249a9d9SManish V Badarkhe /* Boolean variable to hold condition whether firmware update needed or not */
65a249a9d9SManish V Badarkhe static bool is_fwu_needed;
66a249a9d9SManish V Badarkhe 
67*9c11ed7eSHarrison Mutai #if TRANSFER_LIST
68*9c11ed7eSHarrison Mutai static struct transfer_list_header *secure_tl;
69*9c11ed7eSHarrison Mutai #endif
70*9c11ed7eSHarrison Mutai 
716c77e749SSandrine Bailleux struct meminfo *bl1_plat_sec_mem_layout(void)
72b4315306SDan Handley {
73b4315306SDan Handley 	return &bl1_tzram_layout;
74b4315306SDan Handley }
75b4315306SDan Handley 
76b4315306SDan Handley /*******************************************************************************
77b4315306SDan Handley  * BL1 specific platform actions shared between ARM standard platforms.
78b4315306SDan Handley  ******************************************************************************/
79b4315306SDan Handley void arm_bl1_early_platform_setup(void)
80b4315306SDan Handley {
81b4315306SDan Handley 
827b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
837b4c1405SJuan Castillo 	/* Enable watchdog */
84b0c97dafSAditya Angadi 	plat_arm_secure_wdt_start();
857b4c1405SJuan Castillo #endif
867b4c1405SJuan Castillo 
87b4315306SDan Handley 	/* Initialize the console to provide early debug support */
8888a0523eSAntonio Nino Diaz 	arm_console_boot_init();
89b4315306SDan Handley 
90b4315306SDan Handley 	/* Allow BL1 to see the whole Trusted RAM */
91b4315306SDan Handley 	bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
92b4315306SDan Handley 	bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
93b4315306SDan Handley }
94b4315306SDan Handley 
95b4315306SDan Handley void bl1_early_platform_setup(void)
96b4315306SDan Handley {
97b4315306SDan Handley 	arm_bl1_early_platform_setup();
98b4315306SDan Handley 
99b4315306SDan Handley 	/*
1006355f234SVikram Kanigiri 	 * Initialize Interconnect for this cluster during cold boot.
101b4315306SDan Handley 	 * No need for locks as no other CPU is active.
102b4315306SDan Handley 	 */
1036355f234SVikram Kanigiri 	plat_arm_interconnect_init();
104b4315306SDan Handley 	/*
1056355f234SVikram Kanigiri 	 * Enable Interconnect coherency for the primary CPU's cluster.
106b4315306SDan Handley 	 */
1076355f234SVikram Kanigiri 	plat_arm_interconnect_enter_coherency();
108b4315306SDan Handley }
109b4315306SDan Handley 
110b4315306SDan Handley /******************************************************************************
111b4315306SDan Handley  * Perform the very early platform specific architecture setup shared between
112b4315306SDan Handley  * ARM standard platforms. This only does basic initialization. Later
113b4315306SDan Handley  * architectural setup (bl1_arch_setup()) does not do anything platform
114b4315306SDan Handley  * specific.
115b4315306SDan Handley  *****************************************************************************/
116b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
117b4315306SDan Handley {
118b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM
119b65dfe40SSandrine Bailleux 	/* Ensure ARM platforms don't use coherent memory in BL1. */
120d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
121b4315306SDan Handley #endif
122d323af9eSDaniel Boulby 
123d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
124d323af9eSDaniel Boulby 		MAP_BL1_TOTAL,
1252ecaafd2SDaniel Boulby 		MAP_BL1_RO,
1261eb735d7SRoberto Vargas #if USE_ROMLIB
1271eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1281eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1291eb735d7SRoberto Vargas #endif
130d323af9eSDaniel Boulby 		{0}
131d323af9eSDaniel Boulby 	};
132d323af9eSDaniel Boulby 
1330916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
134402b3cf8SJulius Werner #ifdef __aarch64__
135b5fa6563SSandrine Bailleux 	enable_mmu_el3(0);
136402b3cf8SJulius Werner #else
137402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
138402b3cf8SJulius Werner #endif /* __aarch64__ */
1391eb735d7SRoberto Vargas 
1401eb735d7SRoberto Vargas 	arm_setup_romlib();
141b4315306SDan Handley }
142b4315306SDan Handley 
143b4315306SDan Handley void bl1_plat_arch_setup(void)
144b4315306SDan Handley {
145b4315306SDan Handley 	arm_bl1_plat_arch_setup();
146b4315306SDan Handley }
147b4315306SDan Handley 
148b4315306SDan Handley /*
149b4315306SDan Handley  * Perform the platform specific architecture setup shared between
150b4315306SDan Handley  * ARM standard platforms.
151b4315306SDan Handley  */
152b4315306SDan Handley void arm_bl1_platform_setup(void)
153b4315306SDan Handley {
154*9c11ed7eSHarrison Mutai 	const struct dyn_cfg_dtb_info_t *config_info __unused;
155*9c11ed7eSHarrison Mutai 	uint32_t fw_config_max_size __unused;
156*9c11ed7eSHarrison Mutai 	image_info_t config_image_info __unused;
157*9c11ed7eSHarrison Mutai 	struct transfer_list_entry *te __unused;
158*9c11ed7eSHarrison Mutai 
15982869675SManish V Badarkhe 	image_desc_t *desc;
160*9c11ed7eSHarrison Mutai 
16182869675SManish V Badarkhe 	int err = -1;
16282869675SManish V Badarkhe 
163b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
164b4315306SDan Handley 	plat_arm_io_setup();
1653b5ea741SLouis Mayencourt 
16682869675SManish V Badarkhe 	/* Check if we need FWU before further processing */
167a249a9d9SManish V Badarkhe 	is_fwu_needed = plat_arm_bl1_fwu_needed();
168a249a9d9SManish V Badarkhe 	if (is_fwu_needed) {
16982869675SManish V Badarkhe 		ERROR("Skip platform setup as FWU detected\n");
17082869675SManish V Badarkhe 		return;
17182869675SManish V Badarkhe 	}
17282869675SManish V Badarkhe 
173*9c11ed7eSHarrison Mutai #if TRANSFER_LIST
174*9c11ed7eSHarrison Mutai 	secure_tl = transfer_list_init((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
175*9c11ed7eSHarrison Mutai 				       PLAT_ARM_FW_HANDOFF_SIZE);
176*9c11ed7eSHarrison Mutai 
177*9c11ed7eSHarrison Mutai 	if (secure_tl == NULL) {
178*9c11ed7eSHarrison Mutai 		ERROR("Secure transfer list initialisation failed!\n");
179*9c11ed7eSHarrison Mutai 		panic();
180*9c11ed7eSHarrison Mutai 	}
181*9c11ed7eSHarrison Mutai 
182*9c11ed7eSHarrison Mutai 	te = transfer_list_add(secure_tl, TL_TAG_TB_FW_CONFIG,
183*9c11ed7eSHarrison Mutai 			       ARM_TB_FW_CONFIG_MAX_SIZE, NULL);
184*9c11ed7eSHarrison Mutai 	assert(te != NULL);
185*9c11ed7eSHarrison Mutai 
186*9c11ed7eSHarrison Mutai 	/*
187*9c11ed7eSHarrison Mutai 	 * Set the load address of TB_FW_CONFIG in the data section of the TE just
188*9c11ed7eSHarrison Mutai 	 * allocated in the secure transfer list.
189*9c11ed7eSHarrison Mutai 	 */
190*9c11ed7eSHarrison Mutai 	SET_PARAM_HEAD(&config_image_info, PARAM_IMAGE_BINARY, VERSION_2, 0);
191*9c11ed7eSHarrison Mutai 	config_image_info.image_base = (uintptr_t)transfer_list_entry_data(te);
192*9c11ed7eSHarrison Mutai 	config_image_info.image_max_size = te->data_size;
193*9c11ed7eSHarrison Mutai 
194*9c11ed7eSHarrison Mutai 	VERBOSE("FCONF: Loading config with image ID: %u\n", TB_FW_CONFIG_ID);
195*9c11ed7eSHarrison Mutai 	err = load_auth_image(TB_FW_CONFIG_ID, &config_image_info);
196*9c11ed7eSHarrison Mutai 	if (err != 0) {
197*9c11ed7eSHarrison Mutai 		VERBOSE("Failed to load config %u\n", TB_FW_CONFIG_ID);
198*9c11ed7eSHarrison Mutai 		plat_error_handler(err);
199*9c11ed7eSHarrison Mutai 	}
200*9c11ed7eSHarrison Mutai 
201*9c11ed7eSHarrison Mutai 	transfer_list_update_checksum(secure_tl);
202*9c11ed7eSHarrison Mutai 	fconf_populate("TB_FW", (uintptr_t)transfer_list_entry_data(te));
203*9c11ed7eSHarrison Mutai #else
20482869675SManish V Badarkhe 	/* Set global DTB info for fixed fw_config information */
20582869675SManish V Badarkhe 	fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE;
206046cb19bSManish V Badarkhe 	set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, FW_CONFIG_ID);
20782869675SManish V Badarkhe 
20882869675SManish V Badarkhe 	/* Fill the device tree information struct with the info from the config dtb */
20982869675SManish V Badarkhe 	err = fconf_load_config(FW_CONFIG_ID);
21082869675SManish V Badarkhe 	if (err < 0) {
21182869675SManish V Badarkhe 		ERROR("Loading of FW_CONFIG failed %d\n", err);
21282869675SManish V Badarkhe 		plat_error_handler(err);
21382869675SManish V Badarkhe 	}
21482869675SManish V Badarkhe 
21582869675SManish V Badarkhe 	/*
21682869675SManish V Badarkhe 	 * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing
21782869675SManish V Badarkhe 	 * is successful then load TB_FW_CONFIG device tree.
21882869675SManish V Badarkhe 	 */
219*9c11ed7eSHarrison Mutai 	config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID);
220*9c11ed7eSHarrison Mutai 	if (config_info != NULL) {
221*9c11ed7eSHarrison Mutai 		err = fconf_populate_dtb_registry(config_info->config_addr);
22282869675SManish V Badarkhe 		if (err < 0) {
22382869675SManish V Badarkhe 			ERROR("Parsing of FW_CONFIG failed %d\n", err);
22482869675SManish V Badarkhe 			plat_error_handler(err);
22582869675SManish V Badarkhe 		}
226*9c11ed7eSHarrison Mutai 
22782869675SManish V Badarkhe 		/* load TB_FW_CONFIG */
22882869675SManish V Badarkhe 		err = fconf_load_config(TB_FW_CONFIG_ID);
22982869675SManish V Badarkhe 		if (err < 0) {
23082869675SManish V Badarkhe 			ERROR("Loading of TB_FW_CONFIG failed %d\n", err);
23182869675SManish V Badarkhe 			plat_error_handler(err);
23282869675SManish V Badarkhe 		}
23382869675SManish V Badarkhe 	} else {
23482869675SManish V Badarkhe 		ERROR("Invalid FW_CONFIG address\n");
23582869675SManish V Badarkhe 		plat_error_handler(err);
23682869675SManish V Badarkhe 	}
237*9c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */
23882869675SManish V Badarkhe 
23982869675SManish V Badarkhe 	desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
240*9c11ed7eSHarrison Mutai 
241*9c11ed7eSHarrison Mutai #if TRANSFER_LIST
242*9c11ed7eSHarrison Mutai 	transfer_list_set_handoff_args(secure_tl, &desc->ep_info);
243*9c11ed7eSHarrison Mutai #else
244*9c11ed7eSHarrison Mutai 	/* The BL2 ep_info arg0 is modified to point to FW_CONFIG */
24582869675SManish V Badarkhe 	assert(desc != NULL);
246*9c11ed7eSHarrison Mutai 	desc->ep_info.args.arg0 = config_info->config_addr;
247*9c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */
2483b5ea741SLouis Mayencourt 
24988c51c3fSManish V Badarkhe #if CRYPTO_SUPPORT
250ba597da7SJohn Tsichritzis 	/* Share the Mbed TLS heap info with other images */
251ba597da7SJohn Tsichritzis 	arm_bl1_set_mbedtls_heap();
25288c51c3fSManish V Badarkhe #endif /* CRYPTO_SUPPORT */
25360e19f57SAntonio Nino Diaz 
2543208edcdSSoby Mathew 	/*
2553208edcdSSoby Mathew 	 * Allow access to the System counter timer module and program
2563208edcdSSoby Mathew 	 * counter frequency for non secure images during FWU
2573208edcdSSoby Mathew 	 */
2586393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE
2593208edcdSSoby Mathew 	arm_configure_sys_timer();
2606393c787SUsama Arif #endif
2618f73663bSUsama Arif #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
2623208edcdSSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
2638f73663bSUsama Arif #endif
264b4315306SDan Handley }
265b4315306SDan Handley 
2664c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
2674c117f6cSSandrine Bailleux {
2687b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
2697b4c1405SJuan Castillo 	/* Disable watchdog before leaving BL1 */
270b0c97dafSAditya Angadi 	plat_arm_secure_wdt_stop();
2717b4c1405SJuan Castillo #endif
2727b4c1405SJuan Castillo 
2734c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
2744c117f6cSSandrine Bailleux 	/*
2754c117f6cSSandrine Bailleux 	 * Program the EL3 payload's entry point address into the CPUs mailbox
2764c117f6cSSandrine Bailleux 	 * in order to release secondary CPUs from their holding pen and make
2774c117f6cSSandrine Bailleux 	 * them jump there.
2784c117f6cSSandrine Bailleux 	 */
2792a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(ep_info->pc);
2804c117f6cSSandrine Bailleux 	dsbsy();
2814c117f6cSSandrine Bailleux 	sev();
2824c117f6cSSandrine Bailleux #endif
2834c117f6cSSandrine Bailleux }
2847b56928aSSoby Mathew 
2854da6f6cdSSathees Balya /*
2864da6f6cdSSathees Balya  * On Arm platforms, the FWU process is triggered when the FIP image has
2874da6f6cdSSathees Balya  * been tampered with.
2884da6f6cdSSathees Balya  */
289d6dcbcadSLouis Mayencourt bool plat_arm_bl1_fwu_needed(void)
2904da6f6cdSSathees Balya {
291d6dcbcadSLouis Mayencourt 	return !arm_io_is_toc_valid();
2924da6f6cdSSathees Balya }
2934da6f6cdSSathees Balya 
2947b56928aSSoby Mathew /*******************************************************************************
2957b56928aSSoby Mathew  * The following function checks if Firmware update is needed,
2967b56928aSSoby Mathew  * by checking if TOC in FIP image is valid or not.
2977b56928aSSoby Mathew  ******************************************************************************/
2987b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
2997b56928aSSoby Mathew {
300a249a9d9SManish V Badarkhe 	return  is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID;
3017b56928aSSoby Mathew }
302*9c11ed7eSHarrison Mutai 
303*9c11ed7eSHarrison Mutai // Use the default implementation of this function when Firmware Handoff is
304*9c11ed7eSHarrison Mutai // disabled to avoid duplicating its logic.
305*9c11ed7eSHarrison Mutai #if TRANSFER_LIST
306*9c11ed7eSHarrison Mutai int bl1_plat_handle_post_image_load(unsigned int image_id)
307*9c11ed7eSHarrison Mutai {
308*9c11ed7eSHarrison Mutai 	image_desc_t *image_desc __unused;
309*9c11ed7eSHarrison Mutai 
310*9c11ed7eSHarrison Mutai 	assert(image_id == BL2_IMAGE_ID);
311*9c11ed7eSHarrison Mutai 	struct transfer_list_entry *te;
312*9c11ed7eSHarrison Mutai 
313*9c11ed7eSHarrison Mutai 	/* Convey this information to BL2 via its TL. */
314*9c11ed7eSHarrison Mutai 	te = transfer_list_add(secure_tl, TL_TAG_SRAM_LAYOUT64,
315*9c11ed7eSHarrison Mutai 			       sizeof(meminfo_t), NULL);
316*9c11ed7eSHarrison Mutai 	assert(te != NULL);
317*9c11ed7eSHarrison Mutai 
318*9c11ed7eSHarrison Mutai 	bl1_plat_calc_bl2_layout(&bl1_tzram_layout,
319*9c11ed7eSHarrison Mutai 				 (meminfo_t *)transfer_list_entry_data(te));
320*9c11ed7eSHarrison Mutai 
321*9c11ed7eSHarrison Mutai 	transfer_list_update_checksum(secure_tl);
322*9c11ed7eSHarrison Mutai 
323*9c11ed7eSHarrison Mutai 	/**
324*9c11ed7eSHarrison Mutai 	 * Before exiting make sure the contents of the TL are flushed in case there's no
325*9c11ed7eSHarrison Mutai 	 * support for hardware cache coherency.
326*9c11ed7eSHarrison Mutai 	 */
327*9c11ed7eSHarrison Mutai 	flush_dcache_range((uintptr_t)secure_tl, secure_tl->size);
328*9c11ed7eSHarrison Mutai 	return 0;
329*9c11ed7eSHarrison Mutai }
330*9c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST*/
331