xref: /rk3399_ARM-atf/plat/arm/common/arm_bl2_setup.c (revision 7b4e1fbb8f5c2d0e18326223aa0a945d6e7c3a55)
1b4315306SDan Handley /*
2466bb285SZelalem  * Copyright (c) 2015-2020, 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 
1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1409d40e0eSAntonio Nino Diaz #include <common/debug.h>
1509d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
1609d40e0eSAntonio Nino Diaz #include <drivers/generic_delay_timer.h>
179814bfc1SLouis Mayencourt #include <lib/fconf/fconf.h>
1882869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
1909d40e0eSAntonio Nino Diaz #ifdef SPD_opteed
2009d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h>
2109d40e0eSAntonio Nino Diaz #endif
2209d40e0eSAntonio Nino Diaz #include <lib/utils.h>
23bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
2409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2509d40e0eSAntonio Nino Diaz 
26b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL2 */
27b4315306SDan Handley static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
28b4315306SDan Handley 
29caf4eca1SSoby Mathew /*
3004e06973SManish V Badarkhe  * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is
31c099cd39SSoby Mathew  * for `meminfo_t` data structure and fw_configs passed from BL1.
32caf4eca1SSoby Mathew  */
3304e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows);
34caf4eca1SSoby Mathew 
35a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */
360c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2
37a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup
38a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup
39a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout
40*7b4e1fbbSAlexei Fedorov #if MEASURED_BOOT
41*7b4e1fbbSAlexei Fedorov #pragma weak bl2_plat_get_hash
42*7b4e1fbbSAlexei Fedorov #endif
43a8aa7fecSYatharth Kochar 
44d323af9eSDaniel Boulby #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
45d323af9eSDaniel Boulby 					bl2_tzram_layout.total_base,	\
46d323af9eSDaniel Boulby 					bl2_tzram_layout.total_size,	\
47d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
48d323af9eSDaniel Boulby 
494a581b06SDimitris Papastamos 
50490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load
514a581b06SDimitris Papastamos 
52b4315306SDan Handley /*******************************************************************************
53b4315306SDan Handley  * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
54b4315306SDan Handley  * in x0. This memory layout is sitting at the base of the free trusted SRAM.
55b4315306SDan Handley  * Copy it to a safe location before its reclaimed by later BL2 functionality.
56b4315306SDan Handley  ******************************************************************************/
5704e06973SManish V Badarkhe void arm_bl2_early_platform_setup(uintptr_t fw_config,
586c77e749SSandrine Bailleux 				  struct meminfo *mem_layout)
59b4315306SDan Handley {
6082869675SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
61b4315306SDan Handley 	/* Initialize the console to provide early debug support */
6288a0523eSAntonio Nino Diaz 	arm_console_boot_init();
63b4315306SDan Handley 
64b4315306SDan Handley 	/* Setup the BL2 memory layout */
65b4315306SDan Handley 	bl2_tzram_layout = *mem_layout;
66b4315306SDan Handley 
679814bfc1SLouis Mayencourt 	/* Fill the properties struct with the info from the config dtb */
6882869675SManish V Badarkhe 	fconf_populate("FW_CONFIG", fw_config);
6982869675SManish V Badarkhe 
7082869675SManish V Badarkhe 	/* TB_FW_CONFIG was also loaded by BL1 */
7182869675SManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
721d60052eSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
731d60052eSManish V Badarkhe 
7482869675SManish V Badarkhe 	fconf_populate("TB_FW", tb_fw_config_info->config_addr);
759814bfc1SLouis Mayencourt 
76b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
77b4315306SDan Handley 	plat_arm_io_setup();
78b4315306SDan Handley }
79b4315306SDan Handley 
800c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
81b4315306SDan Handley {
82cab0b5b0SSoby Mathew 	arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
83cab0b5b0SSoby Mathew 
8418e279ebSSoby Mathew 	generic_delay_timer_init();
85b4315306SDan Handley }
86b4315306SDan Handley 
87b4315306SDan Handley /*
886e79f9fdSSoby Mathew  * Perform  BL2 preload setup. Currently we initialise the dynamic
896e79f9fdSSoby Mathew  * configuration here.
90b4315306SDan Handley  */
916e79f9fdSSoby Mathew void bl2_plat_preload_setup(void)
92b4315306SDan Handley {
93cab0b5b0SSoby Mathew 	arm_bl2_dyn_cfg_init();
946e79f9fdSSoby Mathew }
95cab0b5b0SSoby Mathew 
966e79f9fdSSoby Mathew /*
976e79f9fdSSoby Mathew  * Perform ARM standard platform setup.
986e79f9fdSSoby Mathew  */
996e79f9fdSSoby Mathew void arm_bl2_platform_setup(void)
1006e79f9fdSSoby Mathew {
101b4315306SDan Handley 	/* Initialize the secure environment */
102b4315306SDan Handley 	plat_arm_security_setup();
103f145403cSRoberto Vargas 
104f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
105638b034cSRoberto Vargas 	arm_nor_psci_do_static_mem_protect();
106f145403cSRoberto Vargas #endif
107b4315306SDan Handley }
108b4315306SDan Handley 
109b4315306SDan Handley void bl2_platform_setup(void)
110b4315306SDan Handley {
111b4315306SDan Handley 	arm_bl2_platform_setup();
112b4315306SDan Handley }
113b4315306SDan Handley 
114b4315306SDan Handley /*******************************************************************************
115b4315306SDan Handley  * Perform the very early platform specific architectural setup here. At the
116b4315306SDan Handley  * moment this is only initializes the mmu in a quick and dirty way.
117b4315306SDan Handley  ******************************************************************************/
118b4315306SDan Handley void arm_bl2_plat_arch_setup(void)
119b4315306SDan Handley {
120943bb7f8SSoby Mathew #if USE_COHERENT_MEM && !ARM_CRYPTOCELL_INTEG
121943bb7f8SSoby Mathew 	/*
122943bb7f8SSoby Mathew 	 * Ensure ARM platforms don't use coherent memory in BL2 unless
123943bb7f8SSoby Mathew 	 * cryptocell integration is enabled.
124943bb7f8SSoby Mathew 	 */
125d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
126b4315306SDan Handley #endif
127d323af9eSDaniel Boulby 
128d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
129d323af9eSDaniel Boulby 		MAP_BL2_TOTAL,
1302ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
1311eb735d7SRoberto Vargas #if USE_ROMLIB
1321eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1331eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1341eb735d7SRoberto Vargas #endif
135943bb7f8SSoby Mathew #if ARM_CRYPTOCELL_INTEG
136943bb7f8SSoby Mathew 		ARM_MAP_BL_COHERENT_RAM,
137943bb7f8SSoby Mathew #endif
138d323af9eSDaniel Boulby 		{0}
139d323af9eSDaniel Boulby 	};
140d323af9eSDaniel Boulby 
1410916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
1426fe8aa2fSYatharth Kochar 
143402b3cf8SJulius Werner #ifdef __aarch64__
144b5fa6563SSandrine Bailleux 	enable_mmu_el1(0);
145402b3cf8SJulius Werner #else
146402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
1476fe8aa2fSYatharth Kochar #endif
1481eb735d7SRoberto Vargas 
1491eb735d7SRoberto Vargas 	arm_setup_romlib();
150b4315306SDan Handley }
151b4315306SDan Handley 
152b4315306SDan Handley void bl2_plat_arch_setup(void)
153b4315306SDan Handley {
154b4315306SDan Handley 	arm_bl2_plat_arch_setup();
155b4315306SDan Handley }
156b4315306SDan Handley 
15707570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id)
158a8aa7fecSYatharth Kochar {
159a8aa7fecSYatharth Kochar 	int err = 0;
160a8aa7fecSYatharth Kochar 	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
16154661cd2SSummer Qin #ifdef SPD_opteed
16254661cd2SSummer Qin 	bl_mem_params_node_t *pager_mem_params = NULL;
16354661cd2SSummer Qin 	bl_mem_params_node_t *paged_mem_params = NULL;
16454661cd2SSummer Qin #endif
165466bb285SZelalem 	assert(bl_mem_params != NULL);
166a8aa7fecSYatharth Kochar 
167a8aa7fecSYatharth Kochar 	switch (image_id) {
168402b3cf8SJulius Werner #ifdef __aarch64__
169a8aa7fecSYatharth Kochar 	case BL32_IMAGE_ID:
17054661cd2SSummer Qin #ifdef SPD_opteed
17154661cd2SSummer Qin 		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
17254661cd2SSummer Qin 		assert(pager_mem_params);
17354661cd2SSummer Qin 
17454661cd2SSummer Qin 		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
17554661cd2SSummer Qin 		assert(paged_mem_params);
17654661cd2SSummer Qin 
17754661cd2SSummer Qin 		err = parse_optee_header(&bl_mem_params->ep_info,
17854661cd2SSummer Qin 				&pager_mem_params->image_info,
17954661cd2SSummer Qin 				&paged_mem_params->image_info);
18054661cd2SSummer Qin 		if (err != 0) {
18154661cd2SSummer Qin 			WARN("OPTEE header parse error.\n");
18254661cd2SSummer Qin 		}
18354661cd2SSummer Qin #endif
184a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
185a8aa7fecSYatharth Kochar 		break;
1866fe8aa2fSYatharth Kochar #endif
187a8aa7fecSYatharth Kochar 
188a8aa7fecSYatharth Kochar 	case BL33_IMAGE_ID:
189a8aa7fecSYatharth Kochar 		/* BL33 expects to receive the primary CPU MPID (through r0) */
190a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
191a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
192a8aa7fecSYatharth Kochar 		break;
193a8aa7fecSYatharth Kochar 
194a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE
195a8aa7fecSYatharth Kochar 	case SCP_BL2_IMAGE_ID:
196a8aa7fecSYatharth Kochar 		/* The subsequent handling of SCP_BL2 is platform specific */
197a8aa7fecSYatharth Kochar 		err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
198a8aa7fecSYatharth Kochar 		if (err) {
199a8aa7fecSYatharth Kochar 			WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
200a8aa7fecSYatharth Kochar 		}
201a8aa7fecSYatharth Kochar 		break;
202a8aa7fecSYatharth Kochar #endif
203649c48f5SJonathan Wright 	default:
204649c48f5SJonathan Wright 		/* Do nothing in default case */
205649c48f5SJonathan Wright 		break;
206a8aa7fecSYatharth Kochar 	}
207a8aa7fecSYatharth Kochar 
208a8aa7fecSYatharth Kochar 	return err;
209a8aa7fecSYatharth Kochar }
210a8aa7fecSYatharth Kochar 
21107570d59SYatharth Kochar /*******************************************************************************
21207570d59SYatharth Kochar  * This function can be used by the platforms to update/use image
21307570d59SYatharth Kochar  * information for given `image_id`.
21407570d59SYatharth Kochar  ******************************************************************************/
215490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
21607570d59SYatharth Kochar {
217c33ff198SOlivier Deprez #if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
218cb3b5344SManish Pandey 	/* For Secure Partitions we don't need post processing */
219cb3b5344SManish Pandey 	if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) &&
220cb3b5344SManish Pandey 		(image_id < MAX_NUMBER_IDS)) {
221cb3b5344SManish Pandey 		return 0;
222cb3b5344SManish Pandey 	}
223cb3b5344SManish Pandey #endif
22407570d59SYatharth Kochar 	return arm_bl2_handle_post_image_load(image_id);
22507570d59SYatharth Kochar }
22607570d59SYatharth Kochar 
227490eeb04SDaniel Boulby int bl2_plat_handle_post_image_load(unsigned int image_id)
228490eeb04SDaniel Boulby {
229490eeb04SDaniel Boulby 	return arm_bl2_plat_handle_post_image_load(image_id);
230490eeb04SDaniel Boulby }
231*7b4e1fbbSAlexei Fedorov 
232*7b4e1fbbSAlexei Fedorov #if MEASURED_BOOT
233*7b4e1fbbSAlexei Fedorov /* Read TCG_DIGEST_SIZE bytes of BL2 hash data */
234*7b4e1fbbSAlexei Fedorov void bl2_plat_get_hash(void *data)
235*7b4e1fbbSAlexei Fedorov {
236*7b4e1fbbSAlexei Fedorov 	arm_bl2_get_hash(data);
237*7b4e1fbbSAlexei Fedorov }
238*7b4e1fbbSAlexei Fedorov #endif
239