xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_setup.c (revision 402b3cf8766fe2cb4ae462f7ee7761d08a1ba56c)
1b4315306SDan Handley /*
21461ad9fSAlexei Fedorov  * Copyright (c) 2015-2019, 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>
1409d40e0eSAntonio Nino Diaz #include <lib/utils.h>
1509d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h>
16bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1809d40e0eSAntonio Nino Diaz 
19b4315306SDan Handley /* Weak definitions may be overridden in specific ARM standard platform */
20b4315306SDan Handley #pragma weak bl1_early_platform_setup
21b4315306SDan Handley #pragma weak bl1_plat_arch_setup
22b4315306SDan Handley #pragma weak bl1_plat_sec_mem_layout
2307570d59SYatharth Kochar #pragma weak bl1_plat_prepare_exit
244da6f6cdSSathees Balya #pragma weak bl1_plat_get_next_image_id
254da6f6cdSSathees Balya #pragma weak plat_arm_bl1_fwu_needed
26b4315306SDan Handley 
27d323af9eSDaniel Boulby #define MAP_BL1_TOTAL		MAP_REGION_FLAT(			\
28d323af9eSDaniel Boulby 					bl1_tzram_layout.total_base,	\
29d323af9eSDaniel Boulby 					bl1_tzram_layout.total_size,	\
30d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
312ecaafd2SDaniel Boulby /*
322ecaafd2SDaniel Boulby  * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section
332ecaafd2SDaniel Boulby  * otherwise one region is defined containing both
342ecaafd2SDaniel Boulby  */
352ecaafd2SDaniel Boulby #if SEPARATE_CODE_AND_RODATA
362ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
37d323af9eSDaniel Boulby 					BL_CODE_BASE,			\
38d323af9eSDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
392ecaafd2SDaniel Boulby 					MT_CODE | MT_SECURE),		\
402ecaafd2SDaniel Boulby 				MAP_REGION_FLAT(			\
41d323af9eSDaniel Boulby 					BL1_RO_DATA_BASE,		\
42d323af9eSDaniel Boulby 					BL1_RO_DATA_END			\
43d323af9eSDaniel Boulby 						- BL_RO_DATA_BASE,	\
44d323af9eSDaniel Boulby 					MT_RO_DATA | MT_SECURE)
452ecaafd2SDaniel Boulby #else
462ecaafd2SDaniel Boulby #define MAP_BL1_RO		MAP_REGION_FLAT(			\
472ecaafd2SDaniel Boulby 					BL_CODE_BASE,			\
482ecaafd2SDaniel Boulby 					BL1_CODE_END - BL_CODE_BASE,	\
492ecaafd2SDaniel Boulby 					MT_CODE | MT_SECURE)
502ecaafd2SDaniel Boulby #endif
51b4315306SDan Handley 
52b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL1*/
53b4315306SDan Handley static meminfo_t bl1_tzram_layout;
54b4315306SDan Handley 
556c77e749SSandrine Bailleux struct meminfo *bl1_plat_sec_mem_layout(void)
56b4315306SDan Handley {
57b4315306SDan Handley 	return &bl1_tzram_layout;
58b4315306SDan Handley }
59b4315306SDan Handley 
60b4315306SDan Handley /*******************************************************************************
61b4315306SDan Handley  * BL1 specific platform actions shared between ARM standard platforms.
62b4315306SDan Handley  ******************************************************************************/
63b4315306SDan Handley void arm_bl1_early_platform_setup(void)
64b4315306SDan Handley {
65b4315306SDan Handley 
667b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
677b4c1405SJuan Castillo 	/* Enable watchdog */
68b0c97dafSAditya Angadi 	plat_arm_secure_wdt_start();
697b4c1405SJuan Castillo #endif
707b4c1405SJuan Castillo 
71b4315306SDan Handley 	/* Initialize the console to provide early debug support */
7288a0523eSAntonio Nino Diaz 	arm_console_boot_init();
73b4315306SDan Handley 
74b4315306SDan Handley 	/* Allow BL1 to see the whole Trusted RAM */
75b4315306SDan Handley 	bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
76b4315306SDan Handley 	bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
77b4315306SDan Handley }
78b4315306SDan Handley 
79b4315306SDan Handley void bl1_early_platform_setup(void)
80b4315306SDan Handley {
81b4315306SDan Handley 	arm_bl1_early_platform_setup();
82b4315306SDan Handley 
83b4315306SDan Handley 	/*
846355f234SVikram Kanigiri 	 * Initialize Interconnect for this cluster during cold boot.
85b4315306SDan Handley 	 * No need for locks as no other CPU is active.
86b4315306SDan Handley 	 */
876355f234SVikram Kanigiri 	plat_arm_interconnect_init();
88b4315306SDan Handley 	/*
896355f234SVikram Kanigiri 	 * Enable Interconnect coherency for the primary CPU's cluster.
90b4315306SDan Handley 	 */
916355f234SVikram Kanigiri 	plat_arm_interconnect_enter_coherency();
92b4315306SDan Handley }
93b4315306SDan Handley 
94b4315306SDan Handley /******************************************************************************
95b4315306SDan Handley  * Perform the very early platform specific architecture setup shared between
96b4315306SDan Handley  * ARM standard platforms. This only does basic initialization. Later
97b4315306SDan Handley  * architectural setup (bl1_arch_setup()) does not do anything platform
98b4315306SDan Handley  * specific.
99b4315306SDan Handley  *****************************************************************************/
100b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
101b4315306SDan Handley {
102943bb7f8SSoby Mathew #if USE_COHERENT_MEM && !ARM_CRYPTOCELL_INTEG
103943bb7f8SSoby Mathew 	/*
104943bb7f8SSoby Mathew 	 * Ensure ARM platforms don't use coherent memory in BL1 unless
105943bb7f8SSoby Mathew 	 * cryptocell integration is enabled.
106943bb7f8SSoby Mathew 	 */
107d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
108b4315306SDan Handley #endif
109d323af9eSDaniel Boulby 
110d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
111d323af9eSDaniel Boulby 		MAP_BL1_TOTAL,
1122ecaafd2SDaniel Boulby 		MAP_BL1_RO,
1131eb735d7SRoberto Vargas #if USE_ROMLIB
1141eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1151eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1161eb735d7SRoberto Vargas #endif
117943bb7f8SSoby Mathew #if ARM_CRYPTOCELL_INTEG
118943bb7f8SSoby Mathew 		ARM_MAP_BL_COHERENT_RAM,
119943bb7f8SSoby Mathew #endif
120d323af9eSDaniel Boulby 		{0}
121d323af9eSDaniel Boulby 	};
122d323af9eSDaniel Boulby 
1230916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
124*402b3cf8SJulius Werner #ifdef __aarch64__
125b5fa6563SSandrine Bailleux 	enable_mmu_el3(0);
126*402b3cf8SJulius Werner #else
127*402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
128*402b3cf8SJulius Werner #endif /* __aarch64__ */
1291eb735d7SRoberto Vargas 
1301eb735d7SRoberto Vargas 	arm_setup_romlib();
131b4315306SDan Handley }
132b4315306SDan Handley 
133b4315306SDan Handley void bl1_plat_arch_setup(void)
134b4315306SDan Handley {
135b4315306SDan Handley 	arm_bl1_plat_arch_setup();
136b4315306SDan Handley }
137b4315306SDan Handley 
138b4315306SDan Handley /*
139b4315306SDan Handley  * Perform the platform specific architecture setup shared between
140b4315306SDan Handley  * ARM standard platforms.
141b4315306SDan Handley  */
142b4315306SDan Handley void arm_bl1_platform_setup(void)
143b4315306SDan Handley {
144b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
145b4315306SDan Handley 	plat_arm_io_setup();
146c228956aSSoby Mathew 	arm_load_tb_fw_config();
147ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT
148ba597da7SJohn Tsichritzis 	/* Share the Mbed TLS heap info with other images */
149ba597da7SJohn Tsichritzis 	arm_bl1_set_mbedtls_heap();
150ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */
15160e19f57SAntonio Nino Diaz 
1523208edcdSSoby Mathew 	/*
1533208edcdSSoby Mathew 	 * Allow access to the System counter timer module and program
1543208edcdSSoby Mathew 	 * counter frequency for non secure images during FWU
1553208edcdSSoby Mathew 	 */
1566393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE
1573208edcdSSoby Mathew 	arm_configure_sys_timer();
1586393c787SUsama Arif #endif
1598f73663bSUsama Arif #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
1603208edcdSSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
1618f73663bSUsama Arif #endif
162b4315306SDan Handley }
163b4315306SDan Handley 
1644c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
1654c117f6cSSandrine Bailleux {
1667b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
1677b4c1405SJuan Castillo 	/* Disable watchdog before leaving BL1 */
168b0c97dafSAditya Angadi 	plat_arm_secure_wdt_stop();
1697b4c1405SJuan Castillo #endif
1707b4c1405SJuan Castillo 
1714c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
1724c117f6cSSandrine Bailleux 	/*
1734c117f6cSSandrine Bailleux 	 * Program the EL3 payload's entry point address into the CPUs mailbox
1744c117f6cSSandrine Bailleux 	 * in order to release secondary CPUs from their holding pen and make
1754c117f6cSSandrine Bailleux 	 * them jump there.
1764c117f6cSSandrine Bailleux 	 */
1772a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(ep_info->pc);
1784c117f6cSSandrine Bailleux 	dsbsy();
1794c117f6cSSandrine Bailleux 	sev();
1804c117f6cSSandrine Bailleux #endif
1814c117f6cSSandrine Bailleux }
1827b56928aSSoby Mathew 
1834da6f6cdSSathees Balya /*
1844da6f6cdSSathees Balya  * On Arm platforms, the FWU process is triggered when the FIP image has
1854da6f6cdSSathees Balya  * been tampered with.
1864da6f6cdSSathees Balya  */
1874da6f6cdSSathees Balya int plat_arm_bl1_fwu_needed(void)
1884da6f6cdSSathees Balya {
1894da6f6cdSSathees Balya 	return (arm_io_is_toc_valid() != 1);
1904da6f6cdSSathees Balya }
1914da6f6cdSSathees Balya 
1927b56928aSSoby Mathew /*******************************************************************************
1937b56928aSSoby Mathew  * The following function checks if Firmware update is needed,
1947b56928aSSoby Mathew  * by checking if TOC in FIP image is valid or not.
1957b56928aSSoby Mathew  ******************************************************************************/
1967b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
1977b56928aSSoby Mathew {
1984da6f6cdSSathees Balya 	if (plat_arm_bl1_fwu_needed() != 0)
1997b56928aSSoby Mathew 		return NS_BL1U_IMAGE_ID;
2007b56928aSSoby Mathew 
2017b56928aSSoby Mathew 	return BL2_IMAGE_ID;
2027b56928aSSoby Mathew }
203