xref: /rk3399_ARM-atf/plat/nvidia/tegra/common/tegra_bl31_setup.c (revision 322e7c3e003cdcf954fb1e82cb9184405e053d03)
1 /*
2  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <errno.h>
9 #include <stddef.h>
10 #include <string.h>
11 
12 #include <platform_def.h>
13 
14 #include <arch.h>
15 #include <arch_helpers.h>
16 #include <bl31/bl31.h>
17 #include <common/bl_common.h>
18 #include <common/debug.h>
19 #include <cortex_a53.h>
20 #include <cortex_a57.h>
21 #include <denver.h>
22 #include <drivers/console.h>
23 #include <lib/mmio.h>
24 #include <lib/utils.h>
25 #include <lib/utils_def.h>
26 #include <plat/common/platform.h>
27 
28 #include <memctrl.h>
29 #include <tegra_def.h>
30 #include <tegra_platform.h>
31 #include <tegra_private.h>
32 
33 /* length of Trusty's input parameters (in bytes) */
34 #define TRUSTY_PARAMS_LEN_BYTES	(4096*2)
35 
36 extern void memcpy16(void *dest, const void *src, unsigned int length);
37 
38 /*******************************************************************************
39  * Declarations of linker defined symbols which will help us find the layout
40  * of trusted SRAM
41  ******************************************************************************/
42 
43 IMPORT_SYM(unsigned long, __RW_START__,		BL31_RW_START);
44 IMPORT_SYM(unsigned long, __RW_END__,		BL31_RW_END);
45 IMPORT_SYM(unsigned long, __RODATA_START__,	BL31_RODATA_BASE);
46 IMPORT_SYM(unsigned long, __RODATA_END__,	BL31_RODATA_END);
47 IMPORT_SYM(unsigned long, __TEXT_START__,	TEXT_START);
48 IMPORT_SYM(unsigned long, __TEXT_END__,		TEXT_END);
49 
50 extern uint64_t tegra_bl31_phys_base;
51 extern uint64_t tegra_console_base;
52 
53 
54 static entry_point_info_t bl33_image_ep_info, bl32_image_ep_info;
55 static plat_params_from_bl2_t plat_bl31_params_from_bl2 = {
56 	.tzdram_size = (uint64_t)TZDRAM_SIZE
57 };
58 static unsigned long bl32_mem_size;
59 static unsigned long bl32_boot_params;
60 
61 /*******************************************************************************
62  * This variable holds the non-secure image entry address
63  ******************************************************************************/
64 extern uint64_t ns_image_entrypoint;
65 
66 /*******************************************************************************
67  * The following platform setup functions are weakly defined. They
68  * provide typical implementations that will be overridden by a SoC.
69  ******************************************************************************/
70 #pragma weak plat_early_platform_setup
71 #pragma weak plat_get_bl31_params
72 #pragma weak plat_get_bl31_plat_params
73 
74 void plat_early_platform_setup(void)
75 {
76 	; /* do nothing */
77 }
78 
79 struct tegra_bl31_params *plat_get_bl31_params(void)
80 {
81 	return NULL;
82 }
83 
84 plat_params_from_bl2_t *plat_get_bl31_plat_params(void)
85 {
86 	return NULL;
87 }
88 
89 /*******************************************************************************
90  * Return a pointer to the 'entry_point_info' structure of the next image for
91  * security state specified. BL33 corresponds to the non-secure image type
92  * while BL32 corresponds to the secure image type.
93  ******************************************************************************/
94 entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
95 {
96 	if (type == NON_SECURE)
97 		return &bl33_image_ep_info;
98 
99 	/* return BL32 entry point info if it is valid */
100 	if (type == SECURE && bl32_image_ep_info.pc)
101 		return &bl32_image_ep_info;
102 
103 	return NULL;
104 }
105 
106 /*******************************************************************************
107  * Return a pointer to the 'plat_params_from_bl2_t' structure. The BL2 image
108  * passes this platform specific information.
109  ******************************************************************************/
110 plat_params_from_bl2_t *bl31_get_plat_params(void)
111 {
112 	return &plat_bl31_params_from_bl2;
113 }
114 
115 /*******************************************************************************
116  * Perform any BL31 specific platform actions. Populate the BL33 and BL32 image
117  * info.
118  ******************************************************************************/
119 void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
120 				u_register_t arg2, u_register_t arg3)
121 {
122 	struct tegra_bl31_params *arg_from_bl2 = (struct tegra_bl31_params *) arg0;
123 	plat_params_from_bl2_t *plat_params = (plat_params_from_bl2_t *)arg1;
124 	image_info_t bl32_img_info = { {0} };
125 	uint64_t tzdram_start, tzdram_end, bl32_start, bl32_end;
126 	uint32_t console_clock;
127 
128 	/*
129 	 * For RESET_TO_BL31 systems, BL31 is the first bootloader to run so
130 	 * there's no argument to relay from a previous bootloader. Platforms
131 	 * might use custom ways to get arguments, so provide handlers which
132 	 * they can override.
133 	 */
134 	if (arg_from_bl2 == NULL)
135 		arg_from_bl2 = plat_get_bl31_params();
136 	if (plat_params == NULL)
137 		plat_params = plat_get_bl31_plat_params();
138 
139 	/*
140 	 * Copy BL3-3, BL3-2 entry point information.
141 	 * They are stored in Secure RAM, in BL2's address space.
142 	 */
143 	assert(arg_from_bl2);
144 	assert(arg_from_bl2->bl33_ep_info);
145 	bl33_image_ep_info = *arg_from_bl2->bl33_ep_info;
146 
147 	if (arg_from_bl2->bl32_ep_info) {
148 		bl32_image_ep_info = *arg_from_bl2->bl32_ep_info;
149 		bl32_mem_size = arg_from_bl2->bl32_ep_info->args.arg0;
150 		bl32_boot_params = arg_from_bl2->bl32_ep_info->args.arg2;
151 	}
152 
153 	/*
154 	 * Parse platform specific parameters - TZDRAM aperture base and size
155 	 */
156 	assert(plat_params);
157 	plat_bl31_params_from_bl2.tzdram_base = plat_params->tzdram_base;
158 	plat_bl31_params_from_bl2.tzdram_size = plat_params->tzdram_size;
159 	plat_bl31_params_from_bl2.uart_id = plat_params->uart_id;
160 
161 	/*
162 	 * It is very important that we run either from TZDRAM or TZSRAM base.
163 	 * Add an explicit check here.
164 	 */
165 	if ((plat_bl31_params_from_bl2.tzdram_base != BL31_BASE) &&
166 	    (TEGRA_TZRAM_BASE != BL31_BASE))
167 		panic();
168 
169 	/*
170 	 * Reference clock used by the FPGAs is a lot slower.
171 	 */
172 	if (tegra_platform_is_fpga() == 1U) {
173 		console_clock = TEGRA_BOOT_UART_CLK_13_MHZ;
174 	} else {
175 		console_clock = TEGRA_BOOT_UART_CLK_408_MHZ;
176 	}
177 
178 	/*
179 	 * Get the base address of the UART controller to be used for the
180 	 * console
181 	 */
182 	tegra_console_base = plat_get_console_from_id(plat_params->uart_id);
183 
184 	if (tegra_console_base != (uint64_t)0) {
185 		/*
186 		 * Configure the UART port to be used as the console
187 		 */
188 		console_init(tegra_console_base, console_clock,
189 			     TEGRA_CONSOLE_BAUDRATE);
190 	}
191 
192 	/*
193 	 * Initialize delay timer
194 	 */
195 	tegra_delay_timer_init();
196 
197 	/*
198 	 * Do initial security configuration to allow DRAM/device access.
199 	 */
200 	tegra_memctrl_tzdram_setup(plat_bl31_params_from_bl2.tzdram_base,
201 			plat_bl31_params_from_bl2.tzdram_size);
202 
203 	/*
204 	 * The previous bootloader might not have placed the BL32 image
205 	 * inside the TZDRAM. We check the BL32 image info to find out
206 	 * the base/PC values and relocate the image if necessary.
207 	 */
208 	if (arg_from_bl2->bl32_image_info) {
209 
210 		bl32_img_info = *arg_from_bl2->bl32_image_info;
211 
212 		/* Relocate BL32 if it resides outside of the TZDRAM */
213 		tzdram_start = plat_bl31_params_from_bl2.tzdram_base;
214 		tzdram_end = plat_bl31_params_from_bl2.tzdram_base +
215 				plat_bl31_params_from_bl2.tzdram_size;
216 		bl32_start = bl32_img_info.image_base;
217 		bl32_end = bl32_img_info.image_base + bl32_img_info.image_size;
218 
219 		assert(tzdram_end > tzdram_start);
220 		assert(bl32_end > bl32_start);
221 		assert(bl32_image_ep_info.pc > tzdram_start);
222 		assert(bl32_image_ep_info.pc < tzdram_end);
223 
224 		/* relocate BL32 */
225 		if (bl32_start >= tzdram_end || bl32_end <= tzdram_start) {
226 
227 			INFO("Relocate BL32 to TZDRAM\n");
228 
229 			memcpy16((void *)(uintptr_t)bl32_image_ep_info.pc,
230 				 (void *)(uintptr_t)bl32_start,
231 				 bl32_img_info.image_size);
232 
233 			/* clean up non-secure intermediate buffer */
234 			zeromem((void *)(uintptr_t)bl32_start,
235 				bl32_img_info.image_size);
236 		}
237 	}
238 
239 	/* Early platform setup for Tegra SoCs */
240 	plat_early_platform_setup();
241 
242 	INFO("BL3-1: Boot CPU: %s Processor [%lx]\n",
243 	     (((read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK)
244 	      == DENVER_IMPL) ? "Denver" : "ARM", read_mpidr());
245 }
246 
247 #ifdef SPD_trusty
248 void plat_trusty_set_boot_args(aapcs64_params_t *args)
249 {
250 	args->arg0 = bl32_mem_size;
251 	args->arg1 = bl32_boot_params;
252 	args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
253 }
254 #endif
255 
256 /*******************************************************************************
257  * Initialize the gic, configure the SCR.
258  ******************************************************************************/
259 void bl31_platform_setup(void)
260 {
261 	uint32_t tmp_reg;
262 
263 	/* Initialize the gic cpu and distributor interfaces */
264 	plat_gic_setup();
265 
266 	/*
267 	 * Setup secondary CPU POR infrastructure.
268 	 */
269 	plat_secondary_setup();
270 
271 	/*
272 	 * Initial Memory Controller configuration.
273 	 */
274 	tegra_memctrl_setup();
275 
276 	/*
277 	 * Set up the TZRAM memory aperture to allow only secure world
278 	 * access
279 	 */
280 	tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
281 
282 	/* Set the next EL to be AArch64 */
283 	tmp_reg = SCR_RES1_BITS | SCR_RW_BIT;
284 	write_scr(tmp_reg);
285 
286 	INFO("BL3-1: Tegra platform setup complete\n");
287 }
288 
289 /*******************************************************************************
290  * Perform any BL3-1 platform runtime setup prior to BL3-1 cold boot exit
291  ******************************************************************************/
292 void bl31_plat_runtime_setup(void)
293 {
294 	/*
295 	 * During boot, USB3 and flash media (SDMMC/SATA) devices need
296 	 * access to IRAM. Because these clients connect to the MC and
297 	 * do not have a direct path to the IRAM, the MC implements AHB
298 	 * redirection during boot to allow path to IRAM. In this mode
299 	 * accesses to a programmed memory address aperture are directed
300 	 * to the AHB bus, allowing access to the IRAM. This mode must be
301 	 * disabled before we jump to the non-secure world.
302 	 */
303 	tegra_memctrl_disable_ahb_redirection();
304 }
305 
306 /*******************************************************************************
307  * Perform the very early platform specific architectural setup here. At the
308  * moment this only intializes the mmu in a quick and dirty way.
309  ******************************************************************************/
310 void bl31_plat_arch_setup(void)
311 {
312 	unsigned long rw_start = BL31_RW_START;
313 	unsigned long rw_size = BL31_RW_END - BL31_RW_START;
314 	unsigned long rodata_start = BL31_RODATA_BASE;
315 	unsigned long rodata_size = BL31_RODATA_END - BL31_RODATA_BASE;
316 	unsigned long code_base = TEXT_START;
317 	unsigned long code_size = TEXT_END - TEXT_START;
318 	const mmap_region_t *plat_mmio_map = NULL;
319 #if USE_COHERENT_MEM
320 	unsigned long coh_start, coh_size;
321 #endif
322 	plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
323 
324 	/* add memory regions */
325 	mmap_add_region(rw_start, rw_start,
326 			rw_size,
327 			MT_MEMORY | MT_RW | MT_SECURE);
328 	mmap_add_region(rodata_start, rodata_start,
329 			rodata_size,
330 			MT_RO_DATA | MT_SECURE);
331 	mmap_add_region(code_base, code_base,
332 			code_size,
333 			MT_CODE | MT_SECURE);
334 
335 	/* map TZDRAM used by BL31 as coherent memory */
336 	if (TEGRA_TZRAM_BASE == tegra_bl31_phys_base) {
337 		mmap_add_region(params_from_bl2->tzdram_base,
338 				params_from_bl2->tzdram_base,
339 				BL31_SIZE,
340 				MT_DEVICE | MT_RW | MT_SECURE);
341 	}
342 
343 #if USE_COHERENT_MEM
344 	coh_start = total_base + (BL_COHERENT_RAM_BASE - BL31_RO_BASE);
345 	coh_size = BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE;
346 
347 	mmap_add_region(coh_start, coh_start,
348 			coh_size,
349 			MT_DEVICE | MT_RW | MT_SECURE);
350 #endif
351 
352 	/* map on-chip free running uS timer */
353 	mmap_add_region(page_align((uint64_t)TEGRA_TMRUS_BASE, 0),
354 			page_align((uint64_t)TEGRA_TMRUS_BASE, 0),
355 			(uint64_t)TEGRA_TMRUS_SIZE,
356 			MT_DEVICE | MT_RO | MT_SECURE);
357 
358 	/* add MMIO space */
359 	plat_mmio_map = plat_get_mmio_map();
360 	if (plat_mmio_map)
361 		mmap_add(plat_mmio_map);
362 	else
363 		WARN("MMIO map not available\n");
364 
365 	/* set up translation tables */
366 	init_xlat_tables();
367 
368 	/* enable the MMU */
369 	enable_mmu_el3(0);
370 
371 	INFO("BL3-1: Tegra: MMU enabled\n");
372 }
373 
374 /*******************************************************************************
375  * Check if the given NS DRAM range is valid
376  ******************************************************************************/
377 int bl31_check_ns_address(uint64_t base, uint64_t size_in_bytes)
378 {
379 	uint64_t end = base + size_in_bytes;
380 
381 	/*
382 	 * Check if the NS DRAM address is valid
383 	 */
384 	if ((base < TEGRA_DRAM_BASE) || (end > TEGRA_DRAM_END)) {
385 		ERROR("NS address is out-of-bounds!\n");
386 		return -EFAULT;
387 	}
388 
389 	/*
390 	 * TZDRAM aperture contains the BL31 and BL32 images, so we need
391 	 * to check if the NS DRAM range overlaps the TZDRAM aperture.
392 	 */
393 	if ((base < TZDRAM_END) && (end > tegra_bl31_phys_base)) {
394 		ERROR("NS address overlaps TZDRAM!\n");
395 		return -ENOTSUP;
396 	}
397 
398 	/* valid NS address */
399 	return 0;
400 }
401