xref: /arm-trusted-firmware/plat/ax/lmt/bl31_plat_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /* SPDX-License-Identifier: BSD-3-Clause */
2*91f16700Schasinglulu /*
3*91f16700Schasinglulu  * Copyright (C) 2024, Charleye <wangkart@aliyun.com>
4*91f16700Schasinglulu  * All rights reserved.
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <platform_def.h>
10*91f16700Schasinglulu #include <lmt_def.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu #include <common/bl_common.h>
13*91f16700Schasinglulu #include <common/debug.h>
14*91f16700Schasinglulu #include <common/desc_image_load.h>
15*91f16700Schasinglulu #include <drivers/console.h>
16*91f16700Schasinglulu #include <drivers/generic_delay_timer.h>
17*91f16700Schasinglulu #include <drivers/ti/uart/uart_16550.h>
18*91f16700Schasinglulu #include <lib/mmio.h>
19*91f16700Schasinglulu #include <plat/common/platform.h>
20*91f16700Schasinglulu #include <plat/arm/common/plat_arm.h>
21*91f16700Schasinglulu 
22*91f16700Schasinglulu static entry_point_info_t bl32_image_ep_info;
23*91f16700Schasinglulu static entry_point_info_t bl33_image_ep_info;
24*91f16700Schasinglulu 
25*91f16700Schasinglulu /*
26*91f16700Schasinglulu  * Table of regions to map using the MMU.
27*91f16700Schasinglulu  * This doesn't include TZRAM as the 'mem_layout' argument passed to
28*91f16700Schasinglulu  * configure_mmu_elx() will give the available subset of that,
29*91f16700Schasinglulu  */
30*91f16700Schasinglulu const mmap_region_t plat_lmt_mmap[] = {
31*91f16700Schasinglulu 	MAP_REGION_FLAT(DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW | MT_NS),
32*91f16700Schasinglulu 	MAP_REGION_FLAT(GICD_BASE, 0x8000, MT_DEVICE | MT_RW | MT_SECURE),
33*91f16700Schasinglulu 	MAP_REGION_FLAT(LMT_PMU_BASE, 0x1000, MT_DEVICE | MT_RW | MT_SECURE),
34*91f16700Schasinglulu 	{ 0 }
35*91f16700Schasinglulu };
36*91f16700Schasinglulu 
37*91f16700Schasinglulu const mmap_region_t *plat_lmt_get_mmap(void)
38*91f16700Schasinglulu {
39*91f16700Schasinglulu 	return plat_lmt_mmap;
40*91f16700Schasinglulu }
41*91f16700Schasinglulu 
42*91f16700Schasinglulu unsigned int plat_get_syscnt_freq2(void)
43*91f16700Schasinglulu {
44*91f16700Schasinglulu 	return LMT_OSC24M_CLK_IN_HZ;
45*91f16700Schasinglulu }
46*91f16700Schasinglulu 
47*91f16700Schasinglulu /*******************************************************************************
48*91f16700Schasinglulu  * Return a pointer to the 'entry_point_info' structure of the next image for
49*91f16700Schasinglulu  * the security state specified. BL33 corresponds to the non-secure image type
50*91f16700Schasinglulu  * while BL32 corresponds to the secure image type. A NULL pointer is returned
51*91f16700Schasinglulu  * if the image does not exist.
52*91f16700Schasinglulu  ******************************************************************************/
53*91f16700Schasinglulu entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
54*91f16700Schasinglulu {
55*91f16700Schasinglulu 	entry_point_info_t *next_image_info;
56*91f16700Schasinglulu 
57*91f16700Schasinglulu 	//assert(sec_state_is_valid(type));
58*91f16700Schasinglulu 	next_image_info = (type == NON_SECURE) ? \
59*91f16700Schasinglulu 						&bl33_image_ep_info : &bl32_image_ep_info;
60*91f16700Schasinglulu 
61*91f16700Schasinglulu 	/* None of the images on this platform can have 0x0 as the entrypoint */
62*91f16700Schasinglulu 	if (next_image_info->pc)
63*91f16700Schasinglulu 		return next_image_info;
64*91f16700Schasinglulu 	else
65*91f16700Schasinglulu 		return NULL;
66*91f16700Schasinglulu }
67*91f16700Schasinglulu 
68*91f16700Schasinglulu /*
69*91f16700Schasinglulu  * Set the build time defaults,if we can't find any config data.
70*91f16700Schasinglulu  */
71*91f16700Schasinglulu static inline void bl31_set_default_config(void)
72*91f16700Schasinglulu {
73*91f16700Schasinglulu 	bl32_image_ep_info.pc = (uintptr_t)BL32_BASE;
74*91f16700Schasinglulu 	bl32_image_ep_info.spsr = (uint32_t)arm_get_spsr_for_bl32_entry();
75*91f16700Schasinglulu 	bl33_image_ep_info.pc = (uintptr_t)plat_get_ns_image_entrypoint();
76*91f16700Schasinglulu 	bl33_image_ep_info.spsr = (uint32_t)SPSR_64(MODE_EL2, MODE_SP_ELX,
77*91f16700Schasinglulu 						    DISABLE_ALL_EXCEPTIONS);
78*91f16700Schasinglulu }
79*91f16700Schasinglulu 
80*91f16700Schasinglulu static void lmt_print_platform_name(void)
81*91f16700Schasinglulu {
82*91f16700Schasinglulu 	NOTICE("ATF running on %s\n", PLATFORM_NAME);
83*91f16700Schasinglulu }
84*91f16700Schasinglulu 
85*91f16700Schasinglulu void lmt_config_setup(void)
86*91f16700Schasinglulu {
87*91f16700Schasinglulu 	lmt_print_platform_name();
88*91f16700Schasinglulu 
89*91f16700Schasinglulu 	generic_delay_timer_init();
90*91f16700Schasinglulu }
91*91f16700Schasinglulu 
92*91f16700Schasinglulu /*******************************************************************************
93*91f16700Schasinglulu  * Perform any BL3-1 early platform setup. Here is an opportunity to copy
94*91f16700Schasinglulu  * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before they
95*91f16700Schasinglulu  * are lost (potentially). This needs to be done before the MMU is initialized
96*91f16700Schasinglulu  * so that the memory layout can be used while creating page tables.
97*91f16700Schasinglulu  * BL2 has flushed this information to memory, so we are guaranteed to pick up
98*91f16700Schasinglulu  * good data.
99*91f16700Schasinglulu  ******************************************************************************/
100*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
101*91f16700Schasinglulu 				u_register_t arg2, u_register_t arg3)
102*91f16700Schasinglulu {
103*91f16700Schasinglulu 	lmt_console_init();
104*91f16700Schasinglulu 
105*91f16700Schasinglulu 	/* Initialize the platform config for future decision making */
106*91f16700Schasinglulu 	lmt_config_setup();
107*91f16700Schasinglulu 	/* There are no parameters from BL2 if BL31 is a reset vector */
108*91f16700Schasinglulu 	assert(arg0 == 0U);
109*91f16700Schasinglulu 	assert(arg1 == 0U);
110*91f16700Schasinglulu 
111*91f16700Schasinglulu 	/*
112*91f16700Schasinglulu 	 * Do initial security configuration to allow DRAM/device access. On
113*91f16700Schasinglulu 	 * Base Sigi only DRAM security is programmable (via TrustZone), but
114*91f16700Schasinglulu 	 * other platforms might have more programmable security devices
115*91f16700Schasinglulu 	 * present.
116*91f16700Schasinglulu 	 */
117*91f16700Schasinglulu 
118*91f16700Schasinglulu 	/* Populate common information for BL32 and BL33 */
119*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
120*91f16700Schasinglulu 	SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
121*91f16700Schasinglulu 	SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
122*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
123*91f16700Schasinglulu 
124*91f16700Schasinglulu 	bl31_set_default_config();
125*91f16700Schasinglulu 
126*91f16700Schasinglulu 	VERBOSE("BL31: early platform setup\n");
127*91f16700Schasinglulu 	NOTICE("BL31: Secure code at 0x%08lx\n", bl32_image_ep_info.pc);
128*91f16700Schasinglulu 	NOTICE("BL31: Non secure code at 0x%08lx\n", bl33_image_ep_info.pc);
129*91f16700Schasinglulu }
130*91f16700Schasinglulu 
131*91f16700Schasinglulu /*******************************************************************************
132*91f16700Schasinglulu  * Perform any BL3-1 platform setup code
133*91f16700Schasinglulu  ******************************************************************************/
134*91f16700Schasinglulu void bl31_platform_setup(void)
135*91f16700Schasinglulu {
136*91f16700Schasinglulu 	/* Initialize the gic cpu and distributor interfaces */
137*91f16700Schasinglulu 	plat_lmt_gic_init();
138*91f16700Schasinglulu }
139*91f16700Schasinglulu 
140*91f16700Schasinglulu /*******************************************************************************
141*91f16700Schasinglulu  * Perform the very early platform specific architectural setup here. At the
142*91f16700Schasinglulu  * moment this is only intializes the mmu in a quick and dirty way.
143*91f16700Schasinglulu  ******************************************************************************/
144*91f16700Schasinglulu void bl31_plat_arch_setup(void)
145*91f16700Schasinglulu {
146*91f16700Schasinglulu 	const mmap_region_t bl_regions[] = {
147*91f16700Schasinglulu 		MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
148*91f16700Schasinglulu 				MT_MEMORY | MT_RW | MT_SECURE),
149*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
150*91f16700Schasinglulu 				MT_CODE | MT_SECURE),
151*91f16700Schasinglulu 		MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE, MT_RO_DATA| MT_SECURE),
152*91f16700Schasinglulu 		MAP_REGION_FLAT(SHARED_RAM_BASE, SHARED_RAM_SIZE, MT_MEMORY | MT_RW | MT_SECURE),
153*91f16700Schasinglulu 		{0}
154*91f16700Schasinglulu 	};
155*91f16700Schasinglulu 	setup_page_tables(bl_regions, plat_lmt_get_mmap());
156*91f16700Schasinglulu 	enable_mmu_el3(0);
157*91f16700Schasinglulu }
158