xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_setup.c (revision 2948d1f81904f02034a0d12faf9b8c7f34b05795)
1b4315306SDan Handley /*
29c11ed7eSHarrison 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>
179c11ed7eSHarrison Mutai #if TRANSFER_LIST
189c11ed7eSHarrison Mutai #include <lib/transfer_list.h>
199c11ed7eSHarrison 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 
67d5705719SHarrison Mutai struct transfer_list_header *secure_tl;
689c11ed7eSHarrison Mutai 
696c77e749SSandrine Bailleux struct meminfo *bl1_plat_sec_mem_layout(void)
70b4315306SDan Handley {
71b4315306SDan Handley 	return &bl1_tzram_layout;
72b4315306SDan Handley }
73b4315306SDan Handley 
74b4315306SDan Handley /*******************************************************************************
75b4315306SDan Handley  * BL1 specific platform actions shared between ARM standard platforms.
76b4315306SDan Handley  ******************************************************************************/
77b4315306SDan Handley void arm_bl1_early_platform_setup(void)
78b4315306SDan Handley {
79b4315306SDan Handley 
807b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
817b4c1405SJuan Castillo 	/* Enable watchdog */
82b0c97dafSAditya Angadi 	plat_arm_secure_wdt_start();
837b4c1405SJuan Castillo #endif
847b4c1405SJuan Castillo 
85b4315306SDan Handley 	/* Initialize the console to provide early debug support */
8688a0523eSAntonio Nino Diaz 	arm_console_boot_init();
87b4315306SDan Handley 
88b4315306SDan Handley 	/* Allow BL1 to see the whole Trusted RAM */
89b4315306SDan Handley 	bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
90b4315306SDan Handley 	bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
91d5705719SHarrison Mutai 
92d5705719SHarrison Mutai #if TRANSFER_LIST
93*2948d1f8SHarrison Mutai 	secure_tl = transfer_list_init((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
94d5705719SHarrison Mutai 					 PLAT_ARM_FW_HANDOFF_SIZE);
95d5705719SHarrison Mutai 	assert(secure_tl != NULL);
96d5705719SHarrison Mutai #endif
97b4315306SDan Handley }
98b4315306SDan Handley 
99b4315306SDan Handley void bl1_early_platform_setup(void)
100b4315306SDan Handley {
101b4315306SDan Handley 	arm_bl1_early_platform_setup();
102b4315306SDan Handley 
103b4315306SDan Handley 	/*
1046355f234SVikram Kanigiri 	 * Initialize Interconnect for this cluster during cold boot.
105b4315306SDan Handley 	 * No need for locks as no other CPU is active.
106b4315306SDan Handley 	 */
1076355f234SVikram Kanigiri 	plat_arm_interconnect_init();
108b4315306SDan Handley 	/*
1096355f234SVikram Kanigiri 	 * Enable Interconnect coherency for the primary CPU's cluster.
110b4315306SDan Handley 	 */
1116355f234SVikram Kanigiri 	plat_arm_interconnect_enter_coherency();
112b4315306SDan Handley }
113b4315306SDan Handley 
114b4315306SDan Handley /******************************************************************************
115b4315306SDan Handley  * Perform the very early platform specific architecture setup shared between
116b4315306SDan Handley  * ARM standard platforms. This only does basic initialization. Later
117b4315306SDan Handley  * architectural setup (bl1_arch_setup()) does not do anything platform
118b4315306SDan Handley  * specific.
119b4315306SDan Handley  *****************************************************************************/
120b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
121b4315306SDan Handley {
122b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM
123b65dfe40SSandrine Bailleux 	/* Ensure ARM platforms don't use coherent memory in BL1. */
124d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
125b4315306SDan Handley #endif
126d323af9eSDaniel Boulby 
127d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
128d323af9eSDaniel Boulby 		MAP_BL1_TOTAL,
1292ecaafd2SDaniel Boulby 		MAP_BL1_RO,
1301eb735d7SRoberto Vargas #if USE_ROMLIB
1311eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1321eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1331eb735d7SRoberto Vargas #endif
134d323af9eSDaniel Boulby 		{0}
135d323af9eSDaniel Boulby 	};
136d323af9eSDaniel Boulby 
1370916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
138402b3cf8SJulius Werner #ifdef __aarch64__
139b5fa6563SSandrine Bailleux 	enable_mmu_el3(0);
140402b3cf8SJulius Werner #else
141402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
142402b3cf8SJulius Werner #endif /* __aarch64__ */
1431eb735d7SRoberto Vargas 
1441eb735d7SRoberto Vargas 	arm_setup_romlib();
145b4315306SDan Handley }
146b4315306SDan Handley 
147b4315306SDan Handley void bl1_plat_arch_setup(void)
148b4315306SDan Handley {
149b4315306SDan Handley 	arm_bl1_plat_arch_setup();
150b4315306SDan Handley }
151b4315306SDan Handley 
152b4315306SDan Handley /*
153b4315306SDan Handley  * Perform the platform specific architecture setup shared between
154b4315306SDan Handley  * ARM standard platforms.
155b4315306SDan Handley  */
156b4315306SDan Handley void arm_bl1_platform_setup(void)
157b4315306SDan Handley {
1589c11ed7eSHarrison Mutai 	const struct dyn_cfg_dtb_info_t *config_info __unused;
1599c11ed7eSHarrison Mutai 	uint32_t fw_config_max_size __unused;
1609c11ed7eSHarrison Mutai 	image_info_t config_image_info __unused;
1619c11ed7eSHarrison Mutai 	struct transfer_list_entry *te __unused;
1629c11ed7eSHarrison Mutai 
16382869675SManish V Badarkhe 	image_desc_t *desc;
1649c11ed7eSHarrison Mutai 
165ada4e59dSHarrison Mutai 	int err __unused = 1;
16682869675SManish V Badarkhe 
167b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
168b4315306SDan Handley 	plat_arm_io_setup();
1693b5ea741SLouis Mayencourt 
17082869675SManish V Badarkhe 	/* Check if we need FWU before further processing */
171a249a9d9SManish V Badarkhe 	is_fwu_needed = plat_arm_bl1_fwu_needed();
172a249a9d9SManish V Badarkhe 	if (is_fwu_needed) {
17382869675SManish V Badarkhe 		ERROR("Skip platform setup as FWU detected\n");
17482869675SManish V Badarkhe 		return;
17582869675SManish V Badarkhe 	}
17682869675SManish V Badarkhe 
1779c11ed7eSHarrison Mutai #if TRANSFER_LIST
178ada4e59dSHarrison Mutai #if CRYPTO_SUPPORT
179ada4e59dSHarrison Mutai 	te = transfer_list_add(secure_tl, TL_TAG_MBEDTLS_HEAP_INFO,
180ada4e59dSHarrison Mutai 			       sizeof(struct crypto_heap_info), NULL);
1819c11ed7eSHarrison Mutai 	assert(te != NULL);
1829c11ed7eSHarrison Mutai 
183ada4e59dSHarrison Mutai 	struct crypto_heap_info *heap_info =
184ada4e59dSHarrison Mutai 		(struct crypto_heap_info *)transfer_list_entry_data(te);
185ada4e59dSHarrison Mutai 	arm_get_mbedtls_heap(&heap_info->addr, &heap_info->size);
186ada4e59dSHarrison Mutai #endif /* CRYPTO_SUPPORT */
187ada4e59dSHarrison Mutai 
188ada4e59dSHarrison Mutai 	desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
189ada4e59dSHarrison Mutai 
1909c11ed7eSHarrison Mutai 	/*
191ada4e59dSHarrison Mutai 	 * The event log might have been updated prior to this, make sure we have an
192ada4e59dSHarrison Mutai 	 * up to date tl before setting the handoff arguments.
1939c11ed7eSHarrison Mutai 	 */
1949c11ed7eSHarrison Mutai 	transfer_list_update_checksum(secure_tl);
195ada4e59dSHarrison Mutai 	transfer_list_set_handoff_args(secure_tl, &desc->ep_info);
1969c11ed7eSHarrison Mutai #else
19782869675SManish V Badarkhe 	/* Set global DTB info for fixed fw_config information */
19882869675SManish V Badarkhe 	fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE;
199046cb19bSManish V Badarkhe 	set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, FW_CONFIG_ID);
20082869675SManish V Badarkhe 
20182869675SManish V Badarkhe 	/* Fill the device tree information struct with the info from the config dtb */
20282869675SManish V Badarkhe 	err = fconf_load_config(FW_CONFIG_ID);
20382869675SManish V Badarkhe 	if (err < 0) {
20482869675SManish V Badarkhe 		ERROR("Loading of FW_CONFIG failed %d\n", err);
20582869675SManish V Badarkhe 		plat_error_handler(err);
20682869675SManish V Badarkhe 	}
20782869675SManish V Badarkhe 
20882869675SManish V Badarkhe 	/*
20982869675SManish V Badarkhe 	 * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing
21082869675SManish V Badarkhe 	 * is successful then load TB_FW_CONFIG device tree.
21182869675SManish V Badarkhe 	 */
2129c11ed7eSHarrison Mutai 	config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID);
2139c11ed7eSHarrison Mutai 	if (config_info != NULL) {
2149c11ed7eSHarrison Mutai 		err = fconf_populate_dtb_registry(config_info->config_addr);
21582869675SManish V Badarkhe 		if (err < 0) {
21682869675SManish V Badarkhe 			ERROR("Parsing of FW_CONFIG failed %d\n", err);
21782869675SManish V Badarkhe 			plat_error_handler(err);
21882869675SManish V Badarkhe 		}
2199c11ed7eSHarrison Mutai 
22082869675SManish V Badarkhe 		/* load TB_FW_CONFIG */
22182869675SManish V Badarkhe 		err = fconf_load_config(TB_FW_CONFIG_ID);
22282869675SManish V Badarkhe 		if (err < 0) {
22382869675SManish V Badarkhe 			ERROR("Loading of TB_FW_CONFIG failed %d\n", err);
22482869675SManish V Badarkhe 			plat_error_handler(err);
22582869675SManish V Badarkhe 		}
22682869675SManish V Badarkhe 	} else {
22782869675SManish V Badarkhe 		ERROR("Invalid FW_CONFIG address\n");
22882869675SManish V Badarkhe 		plat_error_handler(err);
22982869675SManish V Badarkhe 	}
23082869675SManish V Badarkhe 
23182869675SManish V Badarkhe 	desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
2329c11ed7eSHarrison Mutai 
2339c11ed7eSHarrison Mutai 	/* The BL2 ep_info arg0 is modified to point to FW_CONFIG */
23482869675SManish V Badarkhe 	assert(desc != NULL);
2359c11ed7eSHarrison Mutai 	desc->ep_info.args.arg0 = config_info->config_addr;
2363b5ea741SLouis Mayencourt 
23788c51c3fSManish V Badarkhe #if CRYPTO_SUPPORT
238ba597da7SJohn Tsichritzis 	/* Share the Mbed TLS heap info with other images */
239ba597da7SJohn Tsichritzis 	arm_bl1_set_mbedtls_heap();
24088c51c3fSManish V Badarkhe #endif /* CRYPTO_SUPPORT */
241ada4e59dSHarrison Mutai #endif /* TRANSFER_LIST */
24260e19f57SAntonio Nino Diaz 
2433208edcdSSoby Mathew 	/*
2443208edcdSSoby Mathew 	 * Allow access to the System counter timer module and program
2453208edcdSSoby Mathew 	 * counter frequency for non secure images during FWU
2463208edcdSSoby Mathew 	 */
2476393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE
2483208edcdSSoby Mathew 	arm_configure_sys_timer();
2496393c787SUsama Arif #endif
2508f73663bSUsama Arif #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
2513208edcdSSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
2528f73663bSUsama Arif #endif
253b4315306SDan Handley }
254b4315306SDan Handley 
2554c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
2564c117f6cSSandrine Bailleux {
2577b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
2587b4c1405SJuan Castillo 	/* Disable watchdog before leaving BL1 */
259b0c97dafSAditya Angadi 	plat_arm_secure_wdt_stop();
2607b4c1405SJuan Castillo #endif
2617b4c1405SJuan Castillo 
2624c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
2634c117f6cSSandrine Bailleux 	/*
2644c117f6cSSandrine Bailleux 	 * Program the EL3 payload's entry point address into the CPUs mailbox
2654c117f6cSSandrine Bailleux 	 * in order to release secondary CPUs from their holding pen and make
2664c117f6cSSandrine Bailleux 	 * them jump there.
2674c117f6cSSandrine Bailleux 	 */
2682a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(ep_info->pc);
2694c117f6cSSandrine Bailleux 	dsbsy();
2704c117f6cSSandrine Bailleux 	sev();
2714c117f6cSSandrine Bailleux #endif
2724c117f6cSSandrine Bailleux }
2737b56928aSSoby Mathew 
2744da6f6cdSSathees Balya /*
2754da6f6cdSSathees Balya  * On Arm platforms, the FWU process is triggered when the FIP image has
2764da6f6cdSSathees Balya  * been tampered with.
2774da6f6cdSSathees Balya  */
278d6dcbcadSLouis Mayencourt bool plat_arm_bl1_fwu_needed(void)
2794da6f6cdSSathees Balya {
280d6dcbcadSLouis Mayencourt 	return !arm_io_is_toc_valid();
2814da6f6cdSSathees Balya }
2824da6f6cdSSathees Balya 
2837b56928aSSoby Mathew /*******************************************************************************
2847b56928aSSoby Mathew  * The following function checks if Firmware update is needed,
2857b56928aSSoby Mathew  * by checking if TOC in FIP image is valid or not.
2867b56928aSSoby Mathew  ******************************************************************************/
2877b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
2887b56928aSSoby Mathew {
289a249a9d9SManish V Badarkhe 	return  is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID;
2907b56928aSSoby Mathew }
2919c11ed7eSHarrison Mutai 
2929c11ed7eSHarrison Mutai // Use the default implementation of this function when Firmware Handoff is
2939c11ed7eSHarrison Mutai // disabled to avoid duplicating its logic.
2949c11ed7eSHarrison Mutai #if TRANSFER_LIST
2959c11ed7eSHarrison Mutai int bl1_plat_handle_post_image_load(unsigned int image_id)
2969c11ed7eSHarrison Mutai {
2979c11ed7eSHarrison Mutai 	image_desc_t *image_desc __unused;
2989c11ed7eSHarrison Mutai 
2999c11ed7eSHarrison Mutai 	assert(image_id == BL2_IMAGE_ID);
3009c11ed7eSHarrison Mutai 	struct transfer_list_entry *te;
3019c11ed7eSHarrison Mutai 
3029c11ed7eSHarrison Mutai 	/* Convey this information to BL2 via its TL. */
3039c11ed7eSHarrison Mutai 	te = transfer_list_add(secure_tl, TL_TAG_SRAM_LAYOUT64,
3049c11ed7eSHarrison Mutai 			       sizeof(meminfo_t), NULL);
3059c11ed7eSHarrison Mutai 	assert(te != NULL);
3069c11ed7eSHarrison Mutai 
3079c11ed7eSHarrison Mutai 	bl1_plat_calc_bl2_layout(&bl1_tzram_layout,
3089c11ed7eSHarrison Mutai 				 (meminfo_t *)transfer_list_entry_data(te));
3099c11ed7eSHarrison Mutai 
3109c11ed7eSHarrison Mutai 	transfer_list_update_checksum(secure_tl);
3119c11ed7eSHarrison Mutai 
3129c11ed7eSHarrison Mutai 	/**
3139c11ed7eSHarrison Mutai 	 * Before exiting make sure the contents of the TL are flushed in case there's no
3149c11ed7eSHarrison Mutai 	 * support for hardware cache coherency.
3159c11ed7eSHarrison Mutai 	 */
3169c11ed7eSHarrison Mutai 	flush_dcache_range((uintptr_t)secure_tl, secure_tl->size);
3179c11ed7eSHarrison Mutai 	return 0;
3189c11ed7eSHarrison Mutai }
3199c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST*/
320