xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_setup.c (revision 5fb061e761ee98d6ba1938d87efcc26a29ef0a87)
1b4315306SDan Handley /*
2*5fb061e7SGary Morrison  * Copyright (c) 2015-2021, 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>
143b5ea741SLouis Mayencourt #include <lib/fconf/fconf.h>
1582869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
1609d40e0eSAntonio Nino Diaz #include <lib/utils.h>
1709d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h>
18bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
1909d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2009d40e0eSAntonio Nino Diaz 
21b4315306SDan Handley /* Weak definitions may be overridden in specific ARM standard platform */
22b4315306SDan Handley #pragma weak bl1_early_platform_setup
23b4315306SDan Handley #pragma weak bl1_plat_arch_setup
24b4315306SDan Handley #pragma weak bl1_plat_sec_mem_layout
25*5fb061e7SGary Morrison #pragma weak arm_bl1_early_platform_setup
2607570d59SYatharth Kochar #pragma weak bl1_plat_prepare_exit
274da6f6cdSSathees Balya #pragma weak bl1_plat_get_next_image_id
284da6f6cdSSathees Balya #pragma weak plat_arm_bl1_fwu_needed
29*5fb061e7SGary Morrison #pragma weak arm_bl1_plat_arch_setup
30b4315306SDan Handley 
31d323af9eSDaniel Boulby #define MAP_BL1_TOTAL		MAP_REGION_FLAT(			\
32d323af9eSDaniel Boulby 					bl1_tzram_layout.total_base,	\
33d323af9eSDaniel Boulby 					bl1_tzram_layout.total_size,	\
34d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
352ecaafd2SDaniel Boulby /*
362ecaafd2SDaniel Boulby  * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section
372ecaafd2SDaniel Boulby  * otherwise one region is defined containing both
382ecaafd2SDaniel Boulby  */
392ecaafd2SDaniel Boulby #if SEPARATE_CODE_AND_RODATA
402ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
41d323af9eSDaniel Boulby 					BL_CODE_BASE,			\
42d323af9eSDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
432ecaafd2SDaniel Boulby 					MT_CODE | MT_SECURE),		\
442ecaafd2SDaniel Boulby 				MAP_REGION_FLAT(			\
45d323af9eSDaniel Boulby 					BL1_RO_DATA_BASE,		\
46d323af9eSDaniel Boulby 					BL1_RO_DATA_END			\
47d323af9eSDaniel Boulby 						- BL_RO_DATA_BASE,	\
48d323af9eSDaniel Boulby 					MT_RO_DATA | MT_SECURE)
492ecaafd2SDaniel Boulby #else
502ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
512ecaafd2SDaniel Boulby 					BL_CODE_BASE,			\
522ecaafd2SDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
532ecaafd2SDaniel Boulby 					MT_CODE | MT_SECURE)
542ecaafd2SDaniel Boulby #endif
55b4315306SDan Handley 
56b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL1*/
57b4315306SDan Handley static meminfo_t bl1_tzram_layout;
58b4315306SDan Handley 
59a249a9d9SManish V Badarkhe /* Boolean variable to hold condition whether firmware update needed or not */
60a249a9d9SManish V Badarkhe static bool is_fwu_needed;
61a249a9d9SManish V Badarkhe 
626c77e749SSandrine Bailleux struct meminfo *bl1_plat_sec_mem_layout(void)
63b4315306SDan Handley {
64b4315306SDan Handley 	return &bl1_tzram_layout;
65b4315306SDan Handley }
66b4315306SDan Handley 
67b4315306SDan Handley /*******************************************************************************
68b4315306SDan Handley  * BL1 specific platform actions shared between ARM standard platforms.
69b4315306SDan Handley  ******************************************************************************/
70b4315306SDan Handley void arm_bl1_early_platform_setup(void)
71b4315306SDan Handley {
72b4315306SDan Handley 
737b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
747b4c1405SJuan Castillo 	/* Enable watchdog */
75b0c97dafSAditya Angadi 	plat_arm_secure_wdt_start();
767b4c1405SJuan Castillo #endif
777b4c1405SJuan Castillo 
78b4315306SDan Handley 	/* Initialize the console to provide early debug support */
7988a0523eSAntonio Nino Diaz 	arm_console_boot_init();
80b4315306SDan Handley 
81b4315306SDan Handley 	/* Allow BL1 to see the whole Trusted RAM */
82b4315306SDan Handley 	bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
83b4315306SDan Handley 	bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
84b4315306SDan Handley }
85b4315306SDan Handley 
86b4315306SDan Handley void bl1_early_platform_setup(void)
87b4315306SDan Handley {
88b4315306SDan Handley 	arm_bl1_early_platform_setup();
89b4315306SDan Handley 
90b4315306SDan Handley 	/*
916355f234SVikram Kanigiri 	 * Initialize Interconnect for this cluster during cold boot.
92b4315306SDan Handley 	 * No need for locks as no other CPU is active.
93b4315306SDan Handley 	 */
946355f234SVikram Kanigiri 	plat_arm_interconnect_init();
95b4315306SDan Handley 	/*
966355f234SVikram Kanigiri 	 * Enable Interconnect coherency for the primary CPU's cluster.
97b4315306SDan Handley 	 */
986355f234SVikram Kanigiri 	plat_arm_interconnect_enter_coherency();
99b4315306SDan Handley }
100b4315306SDan Handley 
101b4315306SDan Handley /******************************************************************************
102b4315306SDan Handley  * Perform the very early platform specific architecture setup shared between
103b4315306SDan Handley  * ARM standard platforms. This only does basic initialization. Later
104b4315306SDan Handley  * architectural setup (bl1_arch_setup()) does not do anything platform
105b4315306SDan Handley  * specific.
106b4315306SDan Handley  *****************************************************************************/
107b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
108b4315306SDan Handley {
109943bb7f8SSoby Mathew #if USE_COHERENT_MEM && !ARM_CRYPTOCELL_INTEG
110943bb7f8SSoby Mathew 	/*
111943bb7f8SSoby Mathew 	 * Ensure ARM platforms don't use coherent memory in BL1 unless
112943bb7f8SSoby Mathew 	 * cryptocell integration is enabled.
113943bb7f8SSoby Mathew 	 */
114d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
115b4315306SDan Handley #endif
116d323af9eSDaniel Boulby 
117d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
118d323af9eSDaniel Boulby 		MAP_BL1_TOTAL,
1192ecaafd2SDaniel Boulby 		MAP_BL1_RO,
1201eb735d7SRoberto Vargas #if USE_ROMLIB
1211eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1221eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1231eb735d7SRoberto Vargas #endif
124943bb7f8SSoby Mathew #if ARM_CRYPTOCELL_INTEG
125943bb7f8SSoby Mathew 		ARM_MAP_BL_COHERENT_RAM,
126943bb7f8SSoby Mathew #endif
127d323af9eSDaniel Boulby 		{0}
128d323af9eSDaniel Boulby 	};
129d323af9eSDaniel Boulby 
1300916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
131402b3cf8SJulius Werner #ifdef __aarch64__
132b5fa6563SSandrine Bailleux 	enable_mmu_el3(0);
133402b3cf8SJulius Werner #else
134402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
135402b3cf8SJulius Werner #endif /* __aarch64__ */
1361eb735d7SRoberto Vargas 
1371eb735d7SRoberto Vargas 	arm_setup_romlib();
138b4315306SDan Handley }
139b4315306SDan Handley 
140b4315306SDan Handley void bl1_plat_arch_setup(void)
141b4315306SDan Handley {
142b4315306SDan Handley 	arm_bl1_plat_arch_setup();
143b4315306SDan Handley }
144b4315306SDan Handley 
145b4315306SDan Handley /*
146b4315306SDan Handley  * Perform the platform specific architecture setup shared between
147b4315306SDan Handley  * ARM standard platforms.
148b4315306SDan Handley  */
149b4315306SDan Handley void arm_bl1_platform_setup(void)
150b4315306SDan Handley {
15182869675SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *fw_config_info;
15282869675SManish V Badarkhe 	image_desc_t *desc;
15382869675SManish V Badarkhe 	uint32_t fw_config_max_size;
15482869675SManish V Badarkhe 	int err = -1;
15582869675SManish V Badarkhe 
156b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
157b4315306SDan Handley 	plat_arm_io_setup();
1583b5ea741SLouis Mayencourt 
15982869675SManish V Badarkhe 	/* Check if we need FWU before further processing */
160a249a9d9SManish V Badarkhe 	is_fwu_needed = plat_arm_bl1_fwu_needed();
161a249a9d9SManish V Badarkhe 	if (is_fwu_needed) {
16282869675SManish V Badarkhe 		ERROR("Skip platform setup as FWU detected\n");
16382869675SManish V Badarkhe 		return;
16482869675SManish V Badarkhe 	}
16582869675SManish V Badarkhe 
16682869675SManish V Badarkhe 	/* Set global DTB info for fixed fw_config information */
16782869675SManish V Badarkhe 	fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE;
168f4417189SManish V Badarkhe 	set_config_info(ARM_FW_CONFIG_BASE, fw_config_max_size, FW_CONFIG_ID);
16982869675SManish V Badarkhe 
17082869675SManish V Badarkhe 	/* Fill the device tree information struct with the info from the config dtb */
17182869675SManish V Badarkhe 	err = fconf_load_config(FW_CONFIG_ID);
17282869675SManish V Badarkhe 	if (err < 0) {
17382869675SManish V Badarkhe 		ERROR("Loading of FW_CONFIG failed %d\n", err);
17482869675SManish V Badarkhe 		plat_error_handler(err);
17582869675SManish V Badarkhe 	}
17682869675SManish V Badarkhe 
17782869675SManish V Badarkhe 	/*
17882869675SManish V Badarkhe 	 * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing
17982869675SManish V Badarkhe 	 * is successful then load TB_FW_CONFIG device tree.
18082869675SManish V Badarkhe 	 */
18182869675SManish V Badarkhe 	fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID);
18282869675SManish V Badarkhe 	if (fw_config_info != NULL) {
18382869675SManish V Badarkhe 		err = fconf_populate_dtb_registry(fw_config_info->config_addr);
18482869675SManish V Badarkhe 		if (err < 0) {
18582869675SManish V Badarkhe 			ERROR("Parsing of FW_CONFIG failed %d\n", err);
18682869675SManish V Badarkhe 			plat_error_handler(err);
18782869675SManish V Badarkhe 		}
18882869675SManish V Badarkhe 		/* load TB_FW_CONFIG */
18982869675SManish V Badarkhe 		err = fconf_load_config(TB_FW_CONFIG_ID);
19082869675SManish V Badarkhe 		if (err < 0) {
19182869675SManish V Badarkhe 			ERROR("Loading of TB_FW_CONFIG failed %d\n", err);
19282869675SManish V Badarkhe 			plat_error_handler(err);
19382869675SManish V Badarkhe 		}
19482869675SManish V Badarkhe 	} else {
19582869675SManish V Badarkhe 		ERROR("Invalid FW_CONFIG address\n");
19682869675SManish V Badarkhe 		plat_error_handler(err);
19782869675SManish V Badarkhe 	}
19882869675SManish V Badarkhe 
19982869675SManish V Badarkhe 	/* The BL2 ep_info arg0 is modified to point to FW_CONFIG */
20082869675SManish V Badarkhe 	desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
20182869675SManish V Badarkhe 	assert(desc != NULL);
20282869675SManish V Badarkhe 	desc->ep_info.args.arg0 = fw_config_info->config_addr;
2033b5ea741SLouis Mayencourt 
204ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT
205ba597da7SJohn Tsichritzis 	/* Share the Mbed TLS heap info with other images */
206ba597da7SJohn Tsichritzis 	arm_bl1_set_mbedtls_heap();
207ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */
20860e19f57SAntonio Nino Diaz 
2093208edcdSSoby Mathew 	/*
2103208edcdSSoby Mathew 	 * Allow access to the System counter timer module and program
2113208edcdSSoby Mathew 	 * counter frequency for non secure images during FWU
2123208edcdSSoby Mathew 	 */
2136393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE
2143208edcdSSoby Mathew 	arm_configure_sys_timer();
2156393c787SUsama Arif #endif
2168f73663bSUsama Arif #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
2173208edcdSSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
2188f73663bSUsama Arif #endif
219b4315306SDan Handley }
220b4315306SDan Handley 
2214c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
2224c117f6cSSandrine Bailleux {
2237b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
2247b4c1405SJuan Castillo 	/* Disable watchdog before leaving BL1 */
225b0c97dafSAditya Angadi 	plat_arm_secure_wdt_stop();
2267b4c1405SJuan Castillo #endif
2277b4c1405SJuan Castillo 
2284c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
2294c117f6cSSandrine Bailleux 	/*
2304c117f6cSSandrine Bailleux 	 * Program the EL3 payload's entry point address into the CPUs mailbox
2314c117f6cSSandrine Bailleux 	 * in order to release secondary CPUs from their holding pen and make
2324c117f6cSSandrine Bailleux 	 * them jump there.
2334c117f6cSSandrine Bailleux 	 */
2342a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(ep_info->pc);
2354c117f6cSSandrine Bailleux 	dsbsy();
2364c117f6cSSandrine Bailleux 	sev();
2374c117f6cSSandrine Bailleux #endif
2384c117f6cSSandrine Bailleux }
2397b56928aSSoby Mathew 
2404da6f6cdSSathees Balya /*
2414da6f6cdSSathees Balya  * On Arm platforms, the FWU process is triggered when the FIP image has
2424da6f6cdSSathees Balya  * been tampered with.
2434da6f6cdSSathees Balya  */
244d6dcbcadSLouis Mayencourt bool plat_arm_bl1_fwu_needed(void)
2454da6f6cdSSathees Balya {
246d6dcbcadSLouis Mayencourt 	return !arm_io_is_toc_valid();
2474da6f6cdSSathees Balya }
2484da6f6cdSSathees Balya 
2497b56928aSSoby Mathew /*******************************************************************************
2507b56928aSSoby Mathew  * The following function checks if Firmware update is needed,
2517b56928aSSoby Mathew  * by checking if TOC in FIP image is valid or not.
2527b56928aSSoby Mathew  ******************************************************************************/
2537b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
2547b56928aSSoby Mathew {
255a249a9d9SManish V Badarkhe 	return  is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID;
2567b56928aSSoby Mathew }
257