xref: /rk3399_ARM-atf/plat/arm/common/arm_bl2_setup.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1b4315306SDan Handley /*
20c306cc0SSoby Mathew  * Copyright (c) 2015-2018, 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>
9*09d40e0eSAntonio Nino Diaz 
10*09d40e0eSAntonio Nino Diaz #include <platform_def.h>
11*09d40e0eSAntonio Nino Diaz 
12*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
13*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
14*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
15*09d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
16*09d40e0eSAntonio Nino Diaz #include <drivers/generic_delay_timer.h>
17*09d40e0eSAntonio Nino Diaz #ifdef SPD_opteed
18*09d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h>
19*09d40e0eSAntonio Nino Diaz #endif
20*09d40e0eSAntonio Nino Diaz #include <lib/utils.h>
21*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
22*09d40e0eSAntonio Nino Diaz 
23*09d40e0eSAntonio Nino Diaz #include <arm_def.h>
24*09d40e0eSAntonio Nino Diaz #include <plat_arm.h>
25b4315306SDan Handley 
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 /*
30c099cd39SSoby Mathew  * Check that BL2_BASE is above ARM_TB_FW_CONFIG_LIMIT. This reserved page is
31c099cd39SSoby Mathew  * for `meminfo_t` data structure and fw_configs passed from BL1.
32caf4eca1SSoby Mathew  */
33c099cd39SSoby Mathew CASSERT(BL2_BASE >= ARM_TB_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
40a8aa7fecSYatharth Kochar 
41d323af9eSDaniel Boulby #define MAP_BL2_TOTAL		MAP_REGION_FLAT(			\
42d323af9eSDaniel Boulby 					bl2_tzram_layout.total_base,	\
43d323af9eSDaniel Boulby 					bl2_tzram_layout.total_size,	\
44d323af9eSDaniel Boulby 					MT_MEMORY | MT_RW | MT_SECURE)
45d323af9eSDaniel Boulby 
464a581b06SDimitris Papastamos 
47490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load
484a581b06SDimitris Papastamos 
49b4315306SDan Handley /*******************************************************************************
50b4315306SDan Handley  * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
51b4315306SDan Handley  * in x0. This memory layout is sitting at the base of the free trusted SRAM.
52b4315306SDan Handley  * Copy it to a safe location before its reclaimed by later BL2 functionality.
53b4315306SDan Handley  ******************************************************************************/
546c77e749SSandrine Bailleux void arm_bl2_early_platform_setup(uintptr_t tb_fw_config,
556c77e749SSandrine Bailleux 				  struct meminfo *mem_layout)
56b4315306SDan Handley {
57b4315306SDan Handley 	/* Initialize the console to provide early debug support */
5888a0523eSAntonio Nino Diaz 	arm_console_boot_init();
59b4315306SDan Handley 
60b4315306SDan Handley 	/* Setup the BL2 memory layout */
61b4315306SDan Handley 	bl2_tzram_layout = *mem_layout;
62b4315306SDan Handley 
63b4315306SDan Handley 	/* Initialise the IO layer and register platform IO devices */
64b4315306SDan Handley 	plat_arm_io_setup();
65cab0b5b0SSoby Mathew 
66da5f2745SSoby Mathew 	if (tb_fw_config != 0U)
67cab0b5b0SSoby Mathew 		arm_bl2_set_tb_cfg_addr((void *)tb_fw_config);
68b4315306SDan Handley }
69b4315306SDan Handley 
700c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
71b4315306SDan Handley {
72cab0b5b0SSoby Mathew 	arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
73cab0b5b0SSoby Mathew 
7418e279ebSSoby Mathew 	generic_delay_timer_init();
75b4315306SDan Handley }
76b4315306SDan Handley 
77b4315306SDan Handley /*
786e79f9fdSSoby Mathew  * Perform  BL2 preload setup. Currently we initialise the dynamic
796e79f9fdSSoby Mathew  * configuration here.
80b4315306SDan Handley  */
816e79f9fdSSoby Mathew void bl2_plat_preload_setup(void)
82b4315306SDan Handley {
83cab0b5b0SSoby Mathew 	arm_bl2_dyn_cfg_init();
846e79f9fdSSoby Mathew }
85cab0b5b0SSoby Mathew 
866e79f9fdSSoby Mathew /*
876e79f9fdSSoby Mathew  * Perform ARM standard platform setup.
886e79f9fdSSoby Mathew  */
896e79f9fdSSoby Mathew void arm_bl2_platform_setup(void)
906e79f9fdSSoby Mathew {
91b4315306SDan Handley 	/* Initialize the secure environment */
92b4315306SDan Handley 	plat_arm_security_setup();
93f145403cSRoberto Vargas 
94f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
95638b034cSRoberto Vargas 	arm_nor_psci_do_static_mem_protect();
96f145403cSRoberto Vargas #endif
97b4315306SDan Handley }
98b4315306SDan Handley 
99b4315306SDan Handley void bl2_platform_setup(void)
100b4315306SDan Handley {
101b4315306SDan Handley 	arm_bl2_platform_setup();
102b4315306SDan Handley }
103b4315306SDan Handley 
104b4315306SDan Handley /*******************************************************************************
105b4315306SDan Handley  * Perform the very early platform specific architectural setup here. At the
106b4315306SDan Handley  * moment this is only initializes the mmu in a quick and dirty way.
107b4315306SDan Handley  ******************************************************************************/
108b4315306SDan Handley void arm_bl2_plat_arch_setup(void)
109b4315306SDan Handley {
110943bb7f8SSoby Mathew #if USE_COHERENT_MEM && !ARM_CRYPTOCELL_INTEG
111943bb7f8SSoby Mathew 	/*
112943bb7f8SSoby Mathew 	 * Ensure ARM platforms don't use coherent memory in BL2 unless
113943bb7f8SSoby Mathew 	 * cryptocell integration is enabled.
114943bb7f8SSoby Mathew 	 */
115d323af9eSDaniel Boulby 	assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
116b4315306SDan Handley #endif
117d323af9eSDaniel Boulby 
118d323af9eSDaniel Boulby 	const mmap_region_t bl_regions[] = {
119d323af9eSDaniel Boulby 		MAP_BL2_TOTAL,
1202ecaafd2SDaniel Boulby 		ARM_MAP_BL_RO,
1211eb735d7SRoberto Vargas #if USE_ROMLIB
1221eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_CODE,
1231eb735d7SRoberto Vargas 		ARM_MAP_ROMLIB_DATA,
1241eb735d7SRoberto Vargas #endif
125943bb7f8SSoby Mathew #if ARM_CRYPTOCELL_INTEG
126943bb7f8SSoby Mathew 		ARM_MAP_BL_COHERENT_RAM,
127943bb7f8SSoby Mathew #endif
128d323af9eSDaniel Boulby 		{0}
129d323af9eSDaniel Boulby 	};
130d323af9eSDaniel Boulby 
1310916c38dSRoberto Vargas 	setup_page_tables(bl_regions, plat_arm_get_mmap());
1326fe8aa2fSYatharth Kochar 
1336fe8aa2fSYatharth Kochar #ifdef AARCH32
1341e54cbb8SAntonio Nino Diaz 	enable_mmu_svc_mon(0);
1356fe8aa2fSYatharth Kochar #else
136b5fa6563SSandrine Bailleux 	enable_mmu_el1(0);
1376fe8aa2fSYatharth Kochar #endif
1381eb735d7SRoberto Vargas 
1391eb735d7SRoberto Vargas 	arm_setup_romlib();
140b4315306SDan Handley }
141b4315306SDan Handley 
142b4315306SDan Handley void bl2_plat_arch_setup(void)
143b4315306SDan Handley {
144b4315306SDan Handley 	arm_bl2_plat_arch_setup();
145b4315306SDan Handley }
146b4315306SDan Handley 
14707570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id)
148a8aa7fecSYatharth Kochar {
149a8aa7fecSYatharth Kochar 	int err = 0;
150a8aa7fecSYatharth Kochar 	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
15154661cd2SSummer Qin #ifdef SPD_opteed
15254661cd2SSummer Qin 	bl_mem_params_node_t *pager_mem_params = NULL;
15354661cd2SSummer Qin 	bl_mem_params_node_t *paged_mem_params = NULL;
15454661cd2SSummer Qin #endif
155a8aa7fecSYatharth Kochar 	assert(bl_mem_params);
156a8aa7fecSYatharth Kochar 
157a8aa7fecSYatharth Kochar 	switch (image_id) {
1586fe8aa2fSYatharth Kochar #ifdef AARCH64
159a8aa7fecSYatharth Kochar 	case BL32_IMAGE_ID:
16054661cd2SSummer Qin #ifdef SPD_opteed
16154661cd2SSummer Qin 		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
16254661cd2SSummer Qin 		assert(pager_mem_params);
16354661cd2SSummer Qin 
16454661cd2SSummer Qin 		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
16554661cd2SSummer Qin 		assert(paged_mem_params);
16654661cd2SSummer Qin 
16754661cd2SSummer Qin 		err = parse_optee_header(&bl_mem_params->ep_info,
16854661cd2SSummer Qin 				&pager_mem_params->image_info,
16954661cd2SSummer Qin 				&paged_mem_params->image_info);
17054661cd2SSummer Qin 		if (err != 0) {
17154661cd2SSummer Qin 			WARN("OPTEE header parse error.\n");
17254661cd2SSummer Qin 		}
17354661cd2SSummer Qin #endif
174a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
175a8aa7fecSYatharth Kochar 		break;
1766fe8aa2fSYatharth Kochar #endif
177a8aa7fecSYatharth Kochar 
178a8aa7fecSYatharth Kochar 	case BL33_IMAGE_ID:
179a8aa7fecSYatharth Kochar 		/* BL33 expects to receive the primary CPU MPID (through r0) */
180a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
181a8aa7fecSYatharth Kochar 		bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
182a8aa7fecSYatharth Kochar 		break;
183a8aa7fecSYatharth Kochar 
184a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE
185a8aa7fecSYatharth Kochar 	case SCP_BL2_IMAGE_ID:
186a8aa7fecSYatharth Kochar 		/* The subsequent handling of SCP_BL2 is platform specific */
187a8aa7fecSYatharth Kochar 		err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
188a8aa7fecSYatharth Kochar 		if (err) {
189a8aa7fecSYatharth Kochar 			WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
190a8aa7fecSYatharth Kochar 		}
191a8aa7fecSYatharth Kochar 		break;
192a8aa7fecSYatharth Kochar #endif
193649c48f5SJonathan Wright 	default:
194649c48f5SJonathan Wright 		/* Do nothing in default case */
195649c48f5SJonathan Wright 		break;
196a8aa7fecSYatharth Kochar 	}
197a8aa7fecSYatharth Kochar 
198a8aa7fecSYatharth Kochar 	return err;
199a8aa7fecSYatharth Kochar }
200a8aa7fecSYatharth Kochar 
20107570d59SYatharth Kochar /*******************************************************************************
20207570d59SYatharth Kochar  * This function can be used by the platforms to update/use image
20307570d59SYatharth Kochar  * information for given `image_id`.
20407570d59SYatharth Kochar  ******************************************************************************/
205490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
20607570d59SYatharth Kochar {
20707570d59SYatharth Kochar 	return arm_bl2_handle_post_image_load(image_id);
20807570d59SYatharth Kochar }
20907570d59SYatharth Kochar 
210490eeb04SDaniel Boulby int bl2_plat_handle_post_image_load(unsigned int image_id)
211490eeb04SDaniel Boulby {
212490eeb04SDaniel Boulby 	return arm_bl2_plat_handle_post_image_load(image_id);
213490eeb04SDaniel Boulby }
214