xref: /rk3399_ARM-atf/plat/arm/common/arm_bl2_setup.c (revision f19dc624a17c9df6aa444e33568b1f70ff4e9341)
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 
12deb4b3a6SZelalem 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*f19dc624Sjohpow01 #if ENABLE_RME
22*f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h>
23*f19dc624Sjohpow01 #endif /* ENABLE_RME */
2409d40e0eSAntonio Nino Diaz #ifdef SPD_opteed
2509d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h>
2609d40e0eSAntonio Nino Diaz #endif
2709d40e0eSAntonio Nino Diaz #include <lib/utils.h>
28*f19dc624Sjohpow01 #if ENABLE_RME
29deb4b3a6SZelalem Aweke #include <plat/arm/common/arm_pas_def.h>
30*f19dc624Sjohpow01 #endif /* ENABLE_RME */
31bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
3209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
3309d40e0eSAntonio Nino Diaz 
34b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL2 */
35b4315306SDan Handley static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
36b4315306SDan Handley 
37a07c101aSManish V Badarkhe /* Base address of fw_config received from BL1 */
38d74c6b83SJimmy Brisson static uintptr_t config_base;
39a07c101aSManish V Badarkhe 
40caf4eca1SSoby Mathew /*
4104e06973SManish V Badarkhe  * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is
42c099cd39SSoby Mathew  * for `meminfo_t` data structure and fw_configs passed from BL1.
43caf4eca1SSoby Mathew  */
4404e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows);
45caf4eca1SSoby Mathew 
46a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */
470c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2
48a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup
49a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup
50a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout
517b4e1fbbSAlexei Fedorov #if MEASURED_BOOT
527b4e1fbbSAlexei Fedorov #pragma weak bl2_plat_get_hash
537b4e1fbbSAlexei Fedorov #endif
54a8aa7fecSYatharth Kochar 
554bb72c47SZelalem Aweke #if ENABLE_RME
564bb72c47SZelalem Aweke #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
574bb72c47SZelalem Aweke 					bl2_tzram_layout.total_base,	\
584bb72c47SZelalem Aweke 					bl2_tzram_layout.total_size,	\
594bb72c47SZelalem Aweke 					MT_MEMORY | MT_RW | MT_ROOT)
604bb72c47SZelalem Aweke #else
61d323af9eSDaniel Boulby #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
62d323af9eSDaniel Boulby 					bl2_tzram_layout.total_base,	\
63d323af9eSDaniel Boulby 					bl2_tzram_layout.total_size,	\
64d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
654bb72c47SZelalem Aweke #endif /* ENABLE_RME */
664a581b06SDimitris Papastamos 
67490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load
684a581b06SDimitris Papastamos 
69b4315306SDan Handley /*******************************************************************************
70b4315306SDan Handley  * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
71b4315306SDan Handley  * in x0. This memory layout is sitting at the base of the free trusted SRAM.
72b4315306SDan Handley  * Copy it to a safe location before its reclaimed by later BL2 functionality.
73b4315306SDan Handley  ******************************************************************************/
7404e06973SManish V Badarkhe void arm_bl2_early_platform_setup(uintptr_t fw_config,
756c77e749SSandrine Bailleux 				  struct meminfo *mem_layout)
76b4315306SDan Handley {
77b4315306SDan Handley 	/* Initialize the console to provide early debug support */
7888a0523eSAntonio Nino Diaz 	arm_console_boot_init();
79b4315306SDan Handley 
80b4315306SDan Handley 	/* Setup the BL2 memory layout */
81b4315306SDan Handley 	bl2_tzram_layout = *mem_layout;
82b4315306SDan Handley 
83d74c6b83SJimmy Brisson 	config_base = fw_config;
849814bfc1SLouis Mayencourt 
85b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
86b4315306SDan Handley 	plat_arm_io_setup();
87ef1daa42SManish V Badarkhe 
88ef1daa42SManish V Badarkhe 	/* Load partition table */
89ef1daa42SManish V Badarkhe #if ARM_GPT_SUPPORT
90ef1daa42SManish V Badarkhe 	partition_init(GPT_IMAGE_ID);
91ef1daa42SManish V Badarkhe #endif /* ARM_GPT_SUPPORT */
92ef1daa42SManish V Badarkhe 
93b4315306SDan Handley }
94b4315306SDan Handley 
950c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
96b4315306SDan Handley {
97cab0b5b0SSoby Mathew 	arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
98cab0b5b0SSoby Mathew 
9918e279ebSSoby Mathew 	generic_delay_timer_init();
100b4315306SDan Handley }
101b4315306SDan Handley 
102b4315306SDan Handley /*
1036e79f9fdSSoby Mathew  * Perform  BL2 preload setup. Currently we initialise the dynamic
1046e79f9fdSSoby Mathew  * configuration here.
105b4315306SDan Handley  */
1066e79f9fdSSoby Mathew void bl2_plat_preload_setup(void)
107b4315306SDan Handley {
108cab0b5b0SSoby Mathew 	arm_bl2_dyn_cfg_init();
109ef1daa42SManish V Badarkhe 
1102f1177b2SManish V Badarkhe #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT
1112f1177b2SManish V Badarkhe 	/* Always use the FIP from bank 0 */
1122f1177b2SManish V Badarkhe 	arm_set_fip_addr(0U);
1132f1177b2SManish V Badarkhe #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */
1146e79f9fdSSoby Mathew }
115cab0b5b0SSoby Mathew 
1166e79f9fdSSoby Mathew /*
1176e79f9fdSSoby Mathew  * Perform ARM standard platform setup.
1186e79f9fdSSoby Mathew  */
1196e79f9fdSSoby Mathew void arm_bl2_platform_setup(void)
1206e79f9fdSSoby Mathew {
121deb4b3a6SZelalem Aweke #if !ENABLE_RME
122b4315306SDan Handley 	/* Initialize the secure environment */
123b4315306SDan Handley 	plat_arm_security_setup();
124deb4b3a6SZelalem Aweke #endif
125f145403cSRoberto Vargas 
126f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
127638b034cSRoberto Vargas 	arm_nor_psci_do_static_mem_protect();
128f145403cSRoberto Vargas #endif
129b4315306SDan Handley }
130b4315306SDan Handley 
131b4315306SDan Handley void bl2_platform_setup(void)
132b4315306SDan Handley {
133b4315306SDan Handley 	arm_bl2_platform_setup();
134b4315306SDan Handley }
135b4315306SDan Handley 
136deb4b3a6SZelalem Aweke #if ENABLE_RME
137*f19dc624Sjohpow01 
138deb4b3a6SZelalem Aweke static void arm_bl2_plat_gpt_setup(void)
139deb4b3a6SZelalem Aweke {
140deb4b3a6SZelalem Aweke 	/*
141deb4b3a6SZelalem Aweke 	 * The GPT library might modify the gpt regions structure to optimize
142deb4b3a6SZelalem Aweke 	 * the layout, so the array cannot be constant.
143deb4b3a6SZelalem Aweke 	 */
144deb4b3a6SZelalem Aweke 	pas_region_t pas_regions[] = {
145deb4b3a6SZelalem Aweke 		ARM_PAS_KERNEL,
146*f19dc624Sjohpow01 		ARM_PAS_SECURE,
147deb4b3a6SZelalem Aweke 		ARM_PAS_REALM,
148deb4b3a6SZelalem Aweke 		ARM_PAS_EL3_DRAM,
149deb4b3a6SZelalem Aweke 		ARM_PAS_GPTS
150deb4b3a6SZelalem Aweke 	};
151deb4b3a6SZelalem Aweke 
152*f19dc624Sjohpow01 	/* Initialize entire protected space to GPT_GPI_ANY. */
153*f19dc624Sjohpow01 	if (gpt_init_l0_tables(GPCCR_PPS_4GB, ARM_L0_GPT_ADDR_BASE,
154*f19dc624Sjohpow01 		ARM_L0_GPT_SIZE) < 0) {
155*f19dc624Sjohpow01 		ERROR("gpt_init_l0_tables() failed!\n");
156deb4b3a6SZelalem Aweke 		panic();
157deb4b3a6SZelalem Aweke 	}
158deb4b3a6SZelalem Aweke 
159*f19dc624Sjohpow01 	/* Carve out defined PAS ranges. */
160*f19dc624Sjohpow01 	if (gpt_init_pas_l1_tables(GPCCR_PGS_4K,
161*f19dc624Sjohpow01 				   ARM_L1_GPT_ADDR_BASE,
162*f19dc624Sjohpow01 				   ARM_L1_GPT_SIZE,
163*f19dc624Sjohpow01 				   pas_regions,
164*f19dc624Sjohpow01 				   (unsigned int)(sizeof(pas_regions) /
165*f19dc624Sjohpow01 				   sizeof(pas_region_t))) < 0) {
166*f19dc624Sjohpow01 		ERROR("gpt_init_pas_l1_tables() failed!\n");
167*f19dc624Sjohpow01 		panic();
168deb4b3a6SZelalem Aweke 	}
169*f19dc624Sjohpow01 
170*f19dc624Sjohpow01 	INFO("Enabling Granule Protection Checks\n");
171*f19dc624Sjohpow01 	if (gpt_enable() < 0) {
172*f19dc624Sjohpow01 		ERROR("gpt_enable() failed!\n");
173*f19dc624Sjohpow01 		panic();
174*f19dc624Sjohpow01 	}
175*f19dc624Sjohpow01 }
176*f19dc624Sjohpow01 
177deb4b3a6SZelalem Aweke #endif /* ENABLE_RME */
178deb4b3a6SZelalem Aweke 
179b4315306SDan Handley /*******************************************************************************
180deb4b3a6SZelalem Aweke  * Perform the very early platform specific architectural setup here.
181deb4b3a6SZelalem Aweke  * When RME is enabled the secure environment is initialised before
182deb4b3a6SZelalem Aweke  * initialising and enabling Granule Protection.
183deb4b3a6SZelalem Aweke  * This function initialises the MMU in a quick and dirty way.
184b4315306SDan Handley  ******************************************************************************/
185b4315306SDan Handley void arm_bl2_plat_arch_setup(void)
186b4315306SDan Handley {
187943bb7f8SSoby Mathew #if USE_COHERENT_MEM && !ARM_CRYPTOCELL_INTEG
188943bb7f8SSoby Mathew 	/*
189943bb7f8SSoby Mathew 	 * Ensure ARM platforms don't use coherent memory in BL2 unless
190943bb7f8SSoby Mathew 	 * cryptocell integration is enabled.
191943bb7f8SSoby Mathew 	 */
192d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
193b4315306SDan Handley #endif
194d323af9eSDaniel Boulby 
195d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
196d323af9eSDaniel Boulby 		MAP_BL2_TOTAL,
1972ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
1981eb735d7SRoberto Vargas #if USE_ROMLIB
1991eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
2001eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
2011eb735d7SRoberto Vargas #endif
202943bb7f8SSoby Mathew #if ARM_CRYPTOCELL_INTEG
203943bb7f8SSoby Mathew 		ARM_MAP_BL_COHERENT_RAM,
204943bb7f8SSoby Mathew #endif
205a07c101aSManish V Badarkhe 		ARM_MAP_BL_CONFIG_REGION,
206c8720729SZelalem Aweke #if ENABLE_RME
207c8720729SZelalem Aweke 		ARM_MAP_L0_GPT_REGION,
208c8720729SZelalem Aweke #endif
209d323af9eSDaniel Boulby 		{0}
210d323af9eSDaniel Boulby 	};
211d323af9eSDaniel Boulby 
212deb4b3a6SZelalem Aweke #if ENABLE_RME
213deb4b3a6SZelalem Aweke 	/* Initialise the secure environment */
214deb4b3a6SZelalem Aweke 	plat_arm_security_setup();
215deb4b3a6SZelalem Aweke #endif
2160916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
2176fe8aa2fSYatharth Kochar 
218402b3cf8SJulius Werner #ifdef __aarch64__
219deb4b3a6SZelalem Aweke #if ENABLE_RME
220deb4b3a6SZelalem Aweke 	/* BL2 runs in EL3 when RME enabled. */
221deb4b3a6SZelalem Aweke 	assert(get_armv9_2_feat_rme_support() != 0U);
222deb4b3a6SZelalem Aweke 	enable_mmu_el3(0);
223*f19dc624Sjohpow01 
224*f19dc624Sjohpow01 	/* Initialise and enable granule protection after MMU. */
225*f19dc624Sjohpow01 	arm_bl2_plat_gpt_setup();
226deb4b3a6SZelalem Aweke #else
227b5fa6563SSandrine Bailleux 	enable_mmu_el1(0);
228deb4b3a6SZelalem Aweke #endif
229402b3cf8SJulius Werner #else
230402b3cf8SJulius Werner 	enable_mmu_svc_mon(0);
2316fe8aa2fSYatharth Kochar #endif
2321eb735d7SRoberto Vargas 
2331eb735d7SRoberto Vargas 	arm_setup_romlib();
234b4315306SDan Handley }
235b4315306SDan Handley 
236b4315306SDan Handley void bl2_plat_arch_setup(void)
237b4315306SDan Handley {
238a07c101aSManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
239a07c101aSManish V Badarkhe 
240b4315306SDan Handley 	arm_bl2_plat_arch_setup();
241a07c101aSManish V Badarkhe 
242a07c101aSManish V Badarkhe 	/* Fill the properties struct with the info from the config dtb */
243d74c6b83SJimmy Brisson 	fconf_populate("FW_CONFIG", config_base);
244a07c101aSManish V Badarkhe 
245a07c101aSManish V Badarkhe 	/* TB_FW_CONFIG was also loaded by BL1 */
246a07c101aSManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
247a07c101aSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
248a07c101aSManish V Badarkhe 
249a07c101aSManish V Badarkhe 	fconf_populate("TB_FW", tb_fw_config_info->config_addr);
250b4315306SDan Handley }
251b4315306SDan Handley 
25207570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id)
253a8aa7fecSYatharth Kochar {
254a8aa7fecSYatharth Kochar 	int err = 0;
255a8aa7fecSYatharth Kochar 	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
25654661cd2SSummer Qin #ifdef SPD_opteed
25754661cd2SSummer Qin 	bl_mem_params_node_t *pager_mem_params = NULL;
25854661cd2SSummer Qin 	bl_mem_params_node_t *paged_mem_params = NULL;
25954661cd2SSummer Qin #endif
260466bb285SZelalem 	assert(bl_mem_params != NULL);
261a8aa7fecSYatharth Kochar 
262a8aa7fecSYatharth Kochar 	switch (image_id) {
263402b3cf8SJulius Werner #ifdef __aarch64__
264a8aa7fecSYatharth Kochar 	case BL32_IMAGE_ID:
26554661cd2SSummer Qin #ifdef SPD_opteed
26654661cd2SSummer Qin 		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
26754661cd2SSummer Qin 		assert(pager_mem_params);
26854661cd2SSummer Qin 
26954661cd2SSummer Qin 		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
27054661cd2SSummer Qin 		assert(paged_mem_params);
27154661cd2SSummer Qin 
27254661cd2SSummer Qin 		err = parse_optee_header(&bl_mem_params->ep_info,
27354661cd2SSummer Qin 				&pager_mem_params->image_info,
27454661cd2SSummer Qin 				&paged_mem_params->image_info);
27554661cd2SSummer Qin 		if (err != 0) {
27654661cd2SSummer Qin 			WARN("OPTEE header parse error.\n");
27754661cd2SSummer Qin 		}
27854661cd2SSummer Qin #endif
279a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
280a8aa7fecSYatharth Kochar 		break;
2816fe8aa2fSYatharth Kochar #endif
282a8aa7fecSYatharth Kochar 
283a8aa7fecSYatharth Kochar 	case BL33_IMAGE_ID:
284a8aa7fecSYatharth Kochar 		/* BL33 expects to receive the primary CPU MPID (through r0) */
285a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
286a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
287a8aa7fecSYatharth Kochar 		break;
288a8aa7fecSYatharth Kochar 
289a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE
290a8aa7fecSYatharth Kochar 	case SCP_BL2_IMAGE_ID:
291a8aa7fecSYatharth Kochar 		/* The subsequent handling of SCP_BL2 is platform specific */
292a8aa7fecSYatharth Kochar 		err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
293a8aa7fecSYatharth Kochar 		if (err) {
294a8aa7fecSYatharth Kochar 			WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
295a8aa7fecSYatharth Kochar 		}
296a8aa7fecSYatharth Kochar 		break;
297a8aa7fecSYatharth Kochar #endif
298649c48f5SJonathan Wright 	default:
299649c48f5SJonathan Wright 		/* Do nothing in default case */
300649c48f5SJonathan Wright 		break;
301a8aa7fecSYatharth Kochar 	}
302a8aa7fecSYatharth Kochar 
303a8aa7fecSYatharth Kochar 	return err;
304a8aa7fecSYatharth Kochar }
305a8aa7fecSYatharth Kochar 
30607570d59SYatharth Kochar /*******************************************************************************
30707570d59SYatharth Kochar  * This function can be used by the platforms to update/use image
30807570d59SYatharth Kochar  * information for given `image_id`.
30907570d59SYatharth Kochar  ******************************************************************************/
310490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
31107570d59SYatharth Kochar {
31246789a7cSBalint Dobszay #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD
313cb3b5344SManish Pandey 	/* For Secure Partitions we don't need post processing */
314cb3b5344SManish Pandey 	if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) &&
315cb3b5344SManish Pandey 		(image_id < MAX_NUMBER_IDS)) {
316cb3b5344SManish Pandey 		return 0;
317cb3b5344SManish Pandey 	}
318cb3b5344SManish Pandey #endif
31907570d59SYatharth Kochar 	return arm_bl2_handle_post_image_load(image_id);
32007570d59SYatharth Kochar }
32107570d59SYatharth Kochar 
322490eeb04SDaniel Boulby int bl2_plat_handle_post_image_load(unsigned int image_id)
323490eeb04SDaniel Boulby {
324490eeb04SDaniel Boulby 	return arm_bl2_plat_handle_post_image_load(image_id);
325490eeb04SDaniel Boulby }
3267b4e1fbbSAlexei Fedorov 
3277b4e1fbbSAlexei Fedorov #if MEASURED_BOOT
3287b4e1fbbSAlexei Fedorov /* Read TCG_DIGEST_SIZE bytes of BL2 hash data */
3297b4e1fbbSAlexei Fedorov void bl2_plat_get_hash(void *data)
3307b4e1fbbSAlexei Fedorov {
3317b4e1fbbSAlexei Fedorov 	arm_bl2_get_hash(data);
3327b4e1fbbSAlexei Fedorov }
3337b4e1fbbSAlexei Fedorov #endif
334