xref: /rk3399_ARM-atf/plat/arm/common/arm_bl2_setup.c (revision deb4b3a63e3a52f2e9823865a1932f6289ccb7ac)
1b4315306SDan Handley /*
2ef1daa42SManish V Badarkhe  * 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 
7a8aa7fecSYatharth Kochar #include <assert.h>
8b4315306SDan Handley #include <string.h>
909d40e0eSAntonio Nino Diaz 
1009d40e0eSAntonio Nino Diaz #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz 
12*deb4b3a6SZelalem Aweke #include <arch_features.h>
1309d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1509d40e0eSAntonio Nino Diaz #include <common/debug.h>
1609d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
1709d40e0eSAntonio Nino Diaz #include <drivers/generic_delay_timer.h>
18ef1daa42SManish V Badarkhe #include <drivers/partition/partition.h>
199814bfc1SLouis Mayencourt #include <lib/fconf/fconf.h>
2082869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
21*deb4b3a6SZelalem Aweke #include <lib/gpt/gpt.h>
2209d40e0eSAntonio Nino Diaz #ifdef SPD_opteed
2309d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h>
2409d40e0eSAntonio Nino Diaz #endif
2509d40e0eSAntonio Nino Diaz #include <lib/utils.h>
26*deb4b3a6SZelalem Aweke #include <plat/arm/common/arm_pas_def.h>
27bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
2809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2909d40e0eSAntonio Nino Diaz 
30b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL2 */
31b4315306SDan Handley static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
32b4315306SDan Handley 
33a07c101aSManish V Badarkhe /* Base address of fw_config received from BL1 */
34d74c6b83SJimmy Brisson static uintptr_t config_base;
35a07c101aSManish V Badarkhe 
36caf4eca1SSoby Mathew /*
3704e06973SManish V Badarkhe  * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is
38c099cd39SSoby Mathew  * for `meminfo_t` data structure and fw_configs passed from BL1.
39caf4eca1SSoby Mathew  */
4004e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows);
41caf4eca1SSoby Mathew 
42a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */
430c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2
44a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup
45a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup
46a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout
477b4e1fbbSAlexei Fedorov #if MEASURED_BOOT
487b4e1fbbSAlexei Fedorov #pragma weak bl2_plat_get_hash
497b4e1fbbSAlexei Fedorov #endif
50a8aa7fecSYatharth Kochar 
514bb72c47SZelalem Aweke #if ENABLE_RME
524bb72c47SZelalem Aweke #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
534bb72c47SZelalem Aweke 					bl2_tzram_layout.total_base,	\
544bb72c47SZelalem Aweke 					bl2_tzram_layout.total_size,	\
554bb72c47SZelalem Aweke 					MT_MEMORY | MT_RW | MT_ROOT)
564bb72c47SZelalem Aweke #else
57d323af9eSDaniel Boulby #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
58d323af9eSDaniel Boulby 					bl2_tzram_layout.total_base,	\
59d323af9eSDaniel Boulby 					bl2_tzram_layout.total_size,	\
60d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
614bb72c47SZelalem Aweke #endif /* ENABLE_RME */
624a581b06SDimitris Papastamos 
63490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load
644a581b06SDimitris Papastamos 
65b4315306SDan Handley /*******************************************************************************
66b4315306SDan Handley  * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
67b4315306SDan Handley  * in x0. This memory layout is sitting at the base of the free trusted SRAM.
68b4315306SDan Handley  * Copy it to a safe location before its reclaimed by later BL2 functionality.
69b4315306SDan Handley  ******************************************************************************/
7004e06973SManish V Badarkhe void arm_bl2_early_platform_setup(uintptr_t fw_config,
716c77e749SSandrine Bailleux 				  struct meminfo *mem_layout)
72b4315306SDan Handley {
73b4315306SDan Handley 	/* Initialize the console to provide early debug support */
7488a0523eSAntonio Nino Diaz 	arm_console_boot_init();
75b4315306SDan Handley 
76b4315306SDan Handley 	/* Setup the BL2 memory layout */
77b4315306SDan Handley 	bl2_tzram_layout = *mem_layout;
78b4315306SDan Handley 
79d74c6b83SJimmy Brisson 	config_base = fw_config;
809814bfc1SLouis Mayencourt 
81b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
82b4315306SDan Handley 	plat_arm_io_setup();
83ef1daa42SManish V Badarkhe 
84ef1daa42SManish V Badarkhe 	/* Load partition table */
85ef1daa42SManish V Badarkhe #if ARM_GPT_SUPPORT
86ef1daa42SManish V Badarkhe 	partition_init(GPT_IMAGE_ID);
87ef1daa42SManish V Badarkhe #endif /* ARM_GPT_SUPPORT */
88ef1daa42SManish V Badarkhe 
89b4315306SDan Handley }
90b4315306SDan Handley 
910c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
92b4315306SDan Handley {
93cab0b5b0SSoby Mathew 	arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
94cab0b5b0SSoby Mathew 
9518e279ebSSoby Mathew 	generic_delay_timer_init();
96b4315306SDan Handley }
97b4315306SDan Handley 
98b4315306SDan Handley /*
996e79f9fdSSoby Mathew  * Perform  BL2 preload setup. Currently we initialise the dynamic
1006e79f9fdSSoby Mathew  * configuration here.
101b4315306SDan Handley  */
1026e79f9fdSSoby Mathew void bl2_plat_preload_setup(void)
103b4315306SDan Handley {
104cab0b5b0SSoby Mathew 	arm_bl2_dyn_cfg_init();
105ef1daa42SManish V Badarkhe 
1062f1177b2SManish V Badarkhe #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT
1072f1177b2SManish V Badarkhe 	/* Always use the FIP from bank 0 */
1082f1177b2SManish V Badarkhe 	arm_set_fip_addr(0U);
1092f1177b2SManish V Badarkhe #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */
1106e79f9fdSSoby Mathew }
111cab0b5b0SSoby Mathew 
1126e79f9fdSSoby Mathew /*
1136e79f9fdSSoby Mathew  * Perform ARM standard platform setup.
1146e79f9fdSSoby Mathew  */
1156e79f9fdSSoby Mathew void arm_bl2_platform_setup(void)
1166e79f9fdSSoby Mathew {
117*deb4b3a6SZelalem Aweke #if !ENABLE_RME
118b4315306SDan Handley 	/* Initialize the secure environment */
119b4315306SDan Handley 	plat_arm_security_setup();
120*deb4b3a6SZelalem Aweke #endif
121f145403cSRoberto Vargas 
122f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
123638b034cSRoberto Vargas 	arm_nor_psci_do_static_mem_protect();
124f145403cSRoberto Vargas #endif
125b4315306SDan Handley }
126b4315306SDan Handley 
127b4315306SDan Handley void bl2_platform_setup(void)
128b4315306SDan Handley {
129b4315306SDan Handley 	arm_bl2_platform_setup();
130b4315306SDan Handley }
131b4315306SDan Handley 
132*deb4b3a6SZelalem Aweke #if ENABLE_RME
133*deb4b3a6SZelalem Aweke static void arm_bl2_plat_gpt_setup(void)
134*deb4b3a6SZelalem Aweke {
135*deb4b3a6SZelalem Aweke 	/*
136*deb4b3a6SZelalem Aweke 	 * The GPT library might modify the gpt regions structure to optimize
137*deb4b3a6SZelalem Aweke 	 * the layout, so the array cannot be constant.
138*deb4b3a6SZelalem Aweke 	 */
139*deb4b3a6SZelalem Aweke 	pas_region_t pas_regions[] = {
140*deb4b3a6SZelalem Aweke 		ARM_PAS_GPI_ANY,
141*deb4b3a6SZelalem Aweke 		ARM_PAS_KERNEL,
142*deb4b3a6SZelalem Aweke 		ARM_PAS_TZC,
143*deb4b3a6SZelalem Aweke 		ARM_PAS_REALM,
144*deb4b3a6SZelalem Aweke 		ARM_PAS_EL3_DRAM,
145*deb4b3a6SZelalem Aweke 		ARM_PAS_GPTS
146*deb4b3a6SZelalem Aweke 	};
147*deb4b3a6SZelalem Aweke 
148*deb4b3a6SZelalem Aweke 	gpt_init_params_t gpt_params = {
149*deb4b3a6SZelalem Aweke 		PLATFORM_PGS,
150*deb4b3a6SZelalem Aweke 		PLATFORM_PPS,
151*deb4b3a6SZelalem Aweke 		PLATFORM_L0GPTSZ,
152*deb4b3a6SZelalem Aweke 		pas_regions,
153*deb4b3a6SZelalem Aweke 		(unsigned int)(sizeof(pas_regions)/sizeof(pas_region_t)),
154*deb4b3a6SZelalem Aweke 		ARM_L0_GPT_ADDR_BASE, ARM_L0_GPT_SIZE,
155*deb4b3a6SZelalem Aweke 		ARM_L1_GPT_ADDR_BASE, ARM_L1_GPT_SIZE
156*deb4b3a6SZelalem Aweke 	};
157*deb4b3a6SZelalem Aweke 
158*deb4b3a6SZelalem Aweke 	/* Initialise the global granule tables */
159*deb4b3a6SZelalem Aweke 	INFO("Enabling Granule Protection Checks\n");
160*deb4b3a6SZelalem Aweke 	if (gpt_init(&gpt_params) < 0) {
161*deb4b3a6SZelalem Aweke 		panic();
162*deb4b3a6SZelalem Aweke 	}
163*deb4b3a6SZelalem Aweke 
164*deb4b3a6SZelalem Aweke 	gpt_enable();
165*deb4b3a6SZelalem Aweke }
166*deb4b3a6SZelalem Aweke #endif /* ENABLE_RME */
167*deb4b3a6SZelalem Aweke 
168b4315306SDan Handley /*******************************************************************************
169*deb4b3a6SZelalem Aweke  * Perform the very early platform specific architectural setup here.
170*deb4b3a6SZelalem Aweke  * When RME is enabled the secure environment is initialised before
171*deb4b3a6SZelalem Aweke  * initialising and enabling Granule Protection.
172*deb4b3a6SZelalem Aweke  * This function initialises the MMU in a quick and dirty way.
173b4315306SDan Handley  ******************************************************************************/
174b4315306SDan Handley void arm_bl2_plat_arch_setup(void)
175b4315306SDan Handley {
176943bb7f8SSoby Mathew #if USE_COHERENT_MEM && !ARM_CRYPTOCELL_INTEG
177943bb7f8SSoby Mathew 	/*
178943bb7f8SSoby Mathew 	 * Ensure ARM platforms don't use coherent memory in BL2 unless
179943bb7f8SSoby Mathew 	 * cryptocell integration is enabled.
180943bb7f8SSoby Mathew 	 */
181d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
182b4315306SDan Handley #endif
183d323af9eSDaniel Boulby 
184d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
185d323af9eSDaniel Boulby 		MAP_BL2_TOTAL,
1862ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
1871eb735d7SRoberto Vargas #if USE_ROMLIB
1881eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1891eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1901eb735d7SRoberto Vargas #endif
191943bb7f8SSoby Mathew #if ARM_CRYPTOCELL_INTEG
192943bb7f8SSoby Mathew 		ARM_MAP_BL_COHERENT_RAM,
193943bb7f8SSoby Mathew #endif
194a07c101aSManish V Badarkhe 		ARM_MAP_BL_CONFIG_REGION,
195c8720729SZelalem Aweke #if ENABLE_RME
196c8720729SZelalem Aweke 		ARM_MAP_L0_GPT_REGION,
197c8720729SZelalem Aweke #endif
198d323af9eSDaniel Boulby 		{0}
199d323af9eSDaniel Boulby 	};
200d323af9eSDaniel Boulby 
201*deb4b3a6SZelalem Aweke #if ENABLE_RME
202*deb4b3a6SZelalem Aweke 	/* Initialise the secure environment */
203*deb4b3a6SZelalem Aweke 	plat_arm_security_setup();
204*deb4b3a6SZelalem Aweke 
205*deb4b3a6SZelalem Aweke 	/* Initialise and enable Granule Protection */
206*deb4b3a6SZelalem Aweke 	arm_bl2_plat_gpt_setup();
207*deb4b3a6SZelalem Aweke #endif
2080916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
2096fe8aa2fSYatharth Kochar 
210402b3cf8SJulius Werner #ifdef __aarch64__
211*deb4b3a6SZelalem Aweke #if ENABLE_RME
212*deb4b3a6SZelalem Aweke 	/* BL2 runs in EL3 when RME enabled. */
213*deb4b3a6SZelalem Aweke 	assert(get_armv9_2_feat_rme_support() != 0U);
214*deb4b3a6SZelalem Aweke 	enable_mmu_el3(0);
215*deb4b3a6SZelalem Aweke #else
216b5fa6563SSandrine Bailleux 	enable_mmu_el1(0);
217*deb4b3a6SZelalem Aweke #endif
218402b3cf8SJulius Werner #else
219402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
2206fe8aa2fSYatharth Kochar #endif
2211eb735d7SRoberto Vargas 
2221eb735d7SRoberto Vargas 	arm_setup_romlib();
223b4315306SDan Handley }
224b4315306SDan Handley 
225b4315306SDan Handley void bl2_plat_arch_setup(void)
226b4315306SDan Handley {
227a07c101aSManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
228a07c101aSManish V Badarkhe 
229b4315306SDan Handley 	arm_bl2_plat_arch_setup();
230a07c101aSManish V Badarkhe 
231a07c101aSManish V Badarkhe 	/* Fill the properties struct with the info from the config dtb */
232d74c6b83SJimmy Brisson 	fconf_populate("FW_CONFIG", config_base);
233a07c101aSManish V Badarkhe 
234a07c101aSManish V Badarkhe 	/* TB_FW_CONFIG was also loaded by BL1 */
235a07c101aSManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
236a07c101aSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
237a07c101aSManish V Badarkhe 
238a07c101aSManish V Badarkhe 	fconf_populate("TB_FW", tb_fw_config_info->config_addr);
239b4315306SDan Handley }
240b4315306SDan Handley 
24107570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id)
242a8aa7fecSYatharth Kochar {
243a8aa7fecSYatharth Kochar 	int err = 0;
244a8aa7fecSYatharth Kochar 	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
24554661cd2SSummer Qin #ifdef SPD_opteed
24654661cd2SSummer Qin 	bl_mem_params_node_t *pager_mem_params = NULL;
24754661cd2SSummer Qin 	bl_mem_params_node_t *paged_mem_params = NULL;
24854661cd2SSummer Qin #endif
249466bb285SZelalem 	assert(bl_mem_params != NULL);
250a8aa7fecSYatharth Kochar 
251a8aa7fecSYatharth Kochar 	switch (image_id) {
252402b3cf8SJulius Werner #ifdef __aarch64__
253a8aa7fecSYatharth Kochar 	case BL32_IMAGE_ID:
25454661cd2SSummer Qin #ifdef SPD_opteed
25554661cd2SSummer Qin 		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
25654661cd2SSummer Qin 		assert(pager_mem_params);
25754661cd2SSummer Qin 
25854661cd2SSummer Qin 		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
25954661cd2SSummer Qin 		assert(paged_mem_params);
26054661cd2SSummer Qin 
26154661cd2SSummer Qin 		err = parse_optee_header(&bl_mem_params->ep_info,
26254661cd2SSummer Qin 				&pager_mem_params->image_info,
26354661cd2SSummer Qin 				&paged_mem_params->image_info);
26454661cd2SSummer Qin 		if (err != 0) {
26554661cd2SSummer Qin 			WARN("OPTEE header parse error.\n");
26654661cd2SSummer Qin 		}
26754661cd2SSummer Qin #endif
268a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
269a8aa7fecSYatharth Kochar 		break;
2706fe8aa2fSYatharth Kochar #endif
271a8aa7fecSYatharth Kochar 
272a8aa7fecSYatharth Kochar 	case BL33_IMAGE_ID:
273a8aa7fecSYatharth Kochar 		/* BL33 expects to receive the primary CPU MPID (through r0) */
274a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
275a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
276a8aa7fecSYatharth Kochar 		break;
277a8aa7fecSYatharth Kochar 
278a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE
279a8aa7fecSYatharth Kochar 	case SCP_BL2_IMAGE_ID:
280a8aa7fecSYatharth Kochar 		/* The subsequent handling of SCP_BL2 is platform specific */
281a8aa7fecSYatharth Kochar 		err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
282a8aa7fecSYatharth Kochar 		if (err) {
283a8aa7fecSYatharth Kochar 			WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
284a8aa7fecSYatharth Kochar 		}
285a8aa7fecSYatharth Kochar 		break;
286a8aa7fecSYatharth Kochar #endif
287649c48f5SJonathan Wright 	default:
288649c48f5SJonathan Wright 		/* Do nothing in default case */
289649c48f5SJonathan Wright 		break;
290a8aa7fecSYatharth Kochar 	}
291a8aa7fecSYatharth Kochar 
292a8aa7fecSYatharth Kochar 	return err;
293a8aa7fecSYatharth Kochar }
294a8aa7fecSYatharth Kochar 
29507570d59SYatharth Kochar /*******************************************************************************
29607570d59SYatharth Kochar  * This function can be used by the platforms to update/use image
29707570d59SYatharth Kochar  * information for given `image_id`.
29807570d59SYatharth Kochar  ******************************************************************************/
299490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
30007570d59SYatharth Kochar {
30146789a7cSBalint Dobszay #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD
302cb3b5344SManish Pandey 	/* For Secure Partitions we don't need post processing */
303cb3b5344SManish Pandey 	if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) &&
304cb3b5344SManish Pandey 		(image_id < MAX_NUMBER_IDS)) {
305cb3b5344SManish Pandey 		return 0;
306cb3b5344SManish Pandey 	}
307cb3b5344SManish Pandey #endif
30807570d59SYatharth Kochar 	return arm_bl2_handle_post_image_load(image_id);
30907570d59SYatharth Kochar }
31007570d59SYatharth Kochar 
311490eeb04SDaniel Boulby int bl2_plat_handle_post_image_load(unsigned int image_id)
312490eeb04SDaniel Boulby {
313490eeb04SDaniel Boulby 	return arm_bl2_plat_handle_post_image_load(image_id);
314490eeb04SDaniel Boulby }
3157b4e1fbbSAlexei Fedorov 
3167b4e1fbbSAlexei Fedorov #if MEASURED_BOOT
3177b4e1fbbSAlexei Fedorov /* Read TCG_DIGEST_SIZE bytes of BL2 hash data */
3187b4e1fbbSAlexei Fedorov void bl2_plat_get_hash(void *data)
3197b4e1fbbSAlexei Fedorov {
3207b4e1fbbSAlexei Fedorov 	arm_bl2_get_hash(data);
3217b4e1fbbSAlexei Fedorov }
3227b4e1fbbSAlexei Fedorov #endif
323