xref: /rk3399_ARM-atf/plat/rpi/rpi3/rpi3_bl31_setup.c (revision 10107707196d67731de57126b846169c5b29aac0)
1 /*
2  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <libfdt.h>
10 
11 #include <platform_def.h>
12 
13 #include <common/bl_common.h>
14 #include <lib/xlat_tables/xlat_mmu_helpers.h>
15 #include <lib/xlat_tables/xlat_tables_defs.h>
16 #include <plat/common/platform.h>
17 
18 #include <rpi_shared.h>
19 
20 /*
21  * Placeholder variables for copying the arguments that have been passed to
22  * BL31 from BL2.
23  */
24 static entry_point_info_t bl32_image_ep_info;
25 static entry_point_info_t bl33_image_ep_info;
26 
27 /*******************************************************************************
28  * Return a pointer to the 'entry_point_info' structure of the next image for
29  * the security state specified. BL33 corresponds to the non-secure image type
30  * while BL32 corresponds to the secure image type. A NULL pointer is returned
31  * if the image does not exist.
32  ******************************************************************************/
33 entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
34 {
35 	entry_point_info_t *next_image_info;
36 
37 	assert(sec_state_is_valid(type) != 0);
38 
39 	next_image_info = (type == NON_SECURE)
40 			? &bl33_image_ep_info : &bl32_image_ep_info;
41 
42 	/* None of the images can have 0x0 as the entrypoint. */
43 	if (next_image_info->pc) {
44 		return next_image_info;
45 	} else {
46 		return NULL;
47 	}
48 }
49 
50 /*******************************************************************************
51  * Perform any BL31 early platform setup. Here is an opportunity to copy
52  * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
53  * they are lost (potentially). This needs to be done before the MMU is
54  * initialized so that the memory layout can be used while creating page
55  * tables. BL2 has flushed this information to memory, so we are guaranteed
56  * to pick up good data.
57  ******************************************************************************/
58 void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
59 				u_register_t arg2, u_register_t arg3)
60 
61 {
62 	/* Initialize the console to provide early debug support */
63 	rpi3_console_init();
64 
65 	/*
66 	 * In debug builds, a special value is passed in 'arg1' to verify
67 	 * platform parameters from BL2 to BL31. Not used in release builds.
68 	 */
69 	assert(arg1 == RPI3_BL31_PLAT_PARAM_VAL);
70 
71 	/* Check that params passed from BL2 are not NULL. */
72 	bl_params_t *params_from_bl2 = (bl_params_t *) arg0;
73 
74 	assert(params_from_bl2 != NULL);
75 	assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
76 	assert(params_from_bl2->h.version >= VERSION_2);
77 
78 	bl_params_node_t *bl_params = params_from_bl2->head;
79 
80 	/*
81 	 * Copy BL33 and BL32 (if present), entry point information.
82 	 * They are stored in Secure RAM, in BL2's address space.
83 	 */
84 	while (bl_params) {
85 		if (bl_params->image_id == BL32_IMAGE_ID) {
86 			bl32_image_ep_info = *bl_params->ep_info;
87 		}
88 
89 		if (bl_params->image_id == BL33_IMAGE_ID) {
90 			bl33_image_ep_info = *bl_params->ep_info;
91 		}
92 
93 		bl_params = bl_params->next_params_info;
94 	}
95 
96 	if (bl33_image_ep_info.pc == 0) {
97 		panic();
98 	}
99 
100 #if RPI3_DIRECT_LINUX_BOOT
101 # if RPI3_BL33_IN_AARCH32
102 	/*
103 	 * According to the file ``Documentation/arm/Booting`` of the Linux
104 	 * kernel tree, Linux expects:
105 	 * r0 = 0
106 	 * r1 = machine type number, optional in DT-only platforms (~0 if so)
107 	 * r2 = Physical address of the device tree blob
108 	 */
109 	VERBOSE("rpi3: Preparing to boot 32-bit Linux kernel\n");
110 	bl33_image_ep_info.args.arg0 = 0U;
111 	bl33_image_ep_info.args.arg1 = ~0U;
112 	bl33_image_ep_info.args.arg2 = (u_register_t) RPI3_PRELOADED_DTB_BASE;
113 # else
114 	/*
115 	 * According to the file ``Documentation/arm64/booting.txt`` of the
116 	 * Linux kernel tree, Linux expects the physical address of the device
117 	 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
118 	 * must be 0.
119 	 */
120 	VERBOSE("rpi3: Preparing to boot 64-bit Linux kernel\n");
121 	bl33_image_ep_info.args.arg0 = (u_register_t) RPI3_PRELOADED_DTB_BASE;
122 	bl33_image_ep_info.args.arg1 = 0ULL;
123 	bl33_image_ep_info.args.arg2 = 0ULL;
124 	bl33_image_ep_info.args.arg3 = 0ULL;
125 # endif /* RPI3_BL33_IN_AARCH32 */
126 #endif /* RPI3_DIRECT_LINUX_BOOT */
127 }
128 
129 void bl31_plat_arch_setup(void)
130 {
131 	rpi3_setup_page_tables(BL31_BASE, BL31_END - BL31_BASE,
132 			       BL_CODE_BASE, BL_CODE_END,
133 			       BL_RO_DATA_BASE, BL_RO_DATA_END
134 #if USE_COHERENT_MEM
135 			       , BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END
136 #endif
137 			      );
138 
139 	enable_mmu_el3(0);
140 }
141 
142 #ifdef RPI3_PRELOADED_DTB_BASE
143 /*
144  * Add information to the device tree (if any) about the reserved DRAM used by
145  * the Trusted Firmware.
146  */
147 static void rpi3_dtb_add_mem_rsv(void)
148 {
149 	int i, regions, rc;
150 	uint64_t addr, size;
151 	void *dtb = (void *)RPI3_PRELOADED_DTB_BASE;
152 
153 	INFO("rpi3: Checking DTB...\n");
154 
155 	/* Return if no device tree is detected */
156 	if (fdt_check_header(dtb) != 0)
157 		return;
158 
159 	regions = fdt_num_mem_rsv(dtb);
160 
161 	VERBOSE("rpi3: Found %d mem reserve region(s)\n", regions);
162 
163 	/* We expect to find one reserved region that we can modify */
164 	if (regions < 1)
165 		return;
166 
167 	/*
168 	 * Look for the region that corresponds to the default boot firmware. It
169 	 * starts at address 0, and it is not needed when the default firmware
170 	 * is replaced by this port of the Trusted Firmware.
171 	 */
172 	for (i = 0; i < regions; i++) {
173 		if (fdt_get_mem_rsv(dtb, i, &addr, &size) != 0)
174 			continue;
175 
176 		if (addr != 0x0)
177 			continue;
178 
179 		VERBOSE("rpi3: Firmware mem reserve region found\n");
180 
181 		rc = fdt_del_mem_rsv(dtb, i);
182 		if (rc != 0) {
183 			INFO("rpi3: Can't remove mem reserve region (%d)\n", rc);
184 		}
185 
186 		break;
187 	}
188 
189 	if (i == regions) {
190 		VERBOSE("rpi3: Firmware mem reserve region not found\n");
191 	}
192 
193 	/*
194 	 * Reserve all SRAM. As said in the documentation, this isn't actually
195 	 * secure memory, so it is needed to tell BL33 that this is a reserved
196 	 * memory region. It doesn't guarantee it won't use it, though.
197 	 */
198 	rc = fdt_add_mem_rsv(dtb, SEC_SRAM_BASE, SEC_SRAM_SIZE);
199 	if (rc != 0) {
200 		WARN("rpi3: Can't add mem reserve region (%d)\n", rc);
201 	}
202 
203 	INFO("rpi3: Reserved 0x%llx - 0x%llx in DTB\n", SEC_SRAM_BASE,
204 	     SEC_SRAM_BASE + SEC_SRAM_SIZE);
205 }
206 #endif
207 
208 void bl31_platform_setup(void)
209 {
210 #ifdef RPI3_PRELOADED_DTB_BASE
211 	/* Only modify a DTB if we know where to look for it */
212 	rpi3_dtb_add_mem_rsv();
213 #endif
214 }
215