blob: f0a7036efede6f3522ad35350136268540f243c3 [file] [log] [blame]
Varun Wadekarb316e242015-05-19 16:48:04 +05301/*
Joel Hutton5cc3bc82018-03-21 11:40:57 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Varun Wadekarb316e242015-05-19 16:48:04 +05303 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekarb316e242015-05-19 16:48:04 +05305 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
10#include <bl31.h>
11#include <bl_common.h>
12#include <console.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053013#include <cortex_a53.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010014#include <cortex_a57.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053015#include <debug.h>
Varun Wadekarbaf903e2015-09-22 15:00:06 +053016#include <denver.h>
Varun Wadekar7a269e22015-06-10 14:04:32 +053017#include <errno.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053018#include <memctrl.h>
19#include <mmio.h>
20#include <platform.h>
21#include <platform_def.h>
22#include <stddef.h>
Varun Wadekarb41a4142016-05-23 15:56:14 -070023#include <string.h>
Varun Wadekar0dc91812015-12-30 15:06:41 -080024#include <tegra_def.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053025#include <tegra_private.h>
Joel Hutton5cc3bc82018-03-21 11:40:57 +000026#include <utils_def.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053027
Arve Hjønnevåg8f539492018-02-21 17:36:44 -080028/* length of Trusty's input parameters (in bytes) */
29#define TRUSTY_PARAMS_LEN_BYTES (4096*2)
30
Varun Wadekarb41a4142016-05-23 15:56:14 -070031extern void zeromem16(void *mem, unsigned int length);
32
Varun Wadekarb316e242015-05-19 16:48:04 +053033/*******************************************************************************
34 * Declarations of linker defined symbols which will help us find the layout
35 * of trusted SRAM
36 ******************************************************************************/
Joel Hutton5cc3bc82018-03-21 11:40:57 +000037
38IMPORT_SYM(unsigned long, __RW_START__, BL31_RW_START);
39IMPORT_SYM(unsigned long, __RW_END__, BL31_RW_END);
40IMPORT_SYM(unsigned long, __RODATA_START__, BL31_RODATA_BASE);
41IMPORT_SYM(unsigned long, __RODATA_END__, BL31_RODATA_END);
42IMPORT_SYM(unsigned long, __TEXT_START__, TEXT_START);
43IMPORT_SYM(unsigned long, __TEXT_END__, TEXT_END);
Varun Wadekarb316e242015-05-19 16:48:04 +053044
Varun Wadekarb316e242015-05-19 16:48:04 +053045extern uint64_t tegra_bl31_phys_base;
Varun Wadekard2014c62015-10-29 10:37:28 +053046extern uint64_t tegra_console_base;
Varun Wadekarb316e242015-05-19 16:48:04 +053047
Varun Wadekarb316e242015-05-19 16:48:04 +053048
Varun Wadekar52a15982015-06-05 12:57:27 +053049static entry_point_info_t bl33_image_ep_info, bl32_image_ep_info;
Varun Wadekarb316e242015-05-19 16:48:04 +053050static plat_params_from_bl2_t plat_bl31_params_from_bl2 = {
Varun Wadekarc8bfe2e2015-07-31 10:03:01 +053051 .tzdram_size = (uint64_t)TZDRAM_SIZE
Varun Wadekarb316e242015-05-19 16:48:04 +053052};
Arve Hjønnevåg8f539492018-02-21 17:36:44 -080053static unsigned long bl32_mem_size;
54static unsigned long bl32_boot_params;
Varun Wadekarb316e242015-05-19 16:48:04 +053055
56/*******************************************************************************
57 * This variable holds the non-secure image entry address
58 ******************************************************************************/
59extern uint64_t ns_image_entrypoint;
60
61/*******************************************************************************
Varun Wadekar3f0a8ad2016-03-28 15:56:47 -070062 * The following platform setup functions are weakly defined. They
63 * provide typical implementations that will be overridden by a SoC.
64 ******************************************************************************/
65#pragma weak plat_early_platform_setup
Varun Wadekard22d4ad2016-05-23 11:41:07 -070066#pragma weak plat_get_bl31_params
67#pragma weak plat_get_bl31_plat_params
Varun Wadekar3f0a8ad2016-03-28 15:56:47 -070068
69void plat_early_platform_setup(void)
70{
71 ; /* do nothing */
72}
73
Varun Wadekard22d4ad2016-05-23 11:41:07 -070074bl31_params_t *plat_get_bl31_params(void)
75{
76 return NULL;
77}
78
79plat_params_from_bl2_t *plat_get_bl31_plat_params(void)
80{
81 return NULL;
82}
83
Varun Wadekar3f0a8ad2016-03-28 15:56:47 -070084/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +053085 * Return a pointer to the 'entry_point_info' structure of the next image for
86 * security state specified. BL33 corresponds to the non-secure image type
87 * while BL32 corresponds to the secure image type.
88 ******************************************************************************/
89entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
90{
91 if (type == NON_SECURE)
92 return &bl33_image_ep_info;
93
Varun Wadekar197a75f2016-06-06 10:46:28 -070094 /* return BL32 entry point info if it is valid */
95 if (type == SECURE && bl32_image_ep_info.pc)
Varun Wadekar52a15982015-06-05 12:57:27 +053096 return &bl32_image_ep_info;
97
Varun Wadekarb316e242015-05-19 16:48:04 +053098 return NULL;
99}
100
101/*******************************************************************************
102 * Return a pointer to the 'plat_params_from_bl2_t' structure. The BL2 image
103 * passes this platform specific information.
104 ******************************************************************************/
105plat_params_from_bl2_t *bl31_get_plat_params(void)
106{
107 return &plat_bl31_params_from_bl2;
108}
109
110/*******************************************************************************
111 * Perform any BL31 specific platform actions. Populate the BL33 and BL32 image
112 * info.
113 ******************************************************************************/
114void bl31_early_platform_setup(bl31_params_t *from_bl2,
115 void *plat_params_from_bl2)
116{
117 plat_params_from_bl2_t *plat_params =
118 (plat_params_from_bl2_t *)plat_params_from_bl2;
Varun Wadekarb41a4142016-05-23 15:56:14 -0700119 image_info_t bl32_img_info = { {0} };
120 uint64_t tzdram_start, tzdram_end, bl32_start, bl32_end;
Varun Wadekarbaf903e2015-09-22 15:00:06 +0530121
Varun Wadekarb316e242015-05-19 16:48:04 +0530122 /*
Varun Wadekard22d4ad2016-05-23 11:41:07 -0700123 * For RESET_TO_BL31 systems, BL31 is the first bootloader to run so
124 * there's no argument to relay from a previous bootloader. Platforms
125 * might use custom ways to get arguments, so provide handlers which
126 * they can override.
127 */
128 if (from_bl2 == NULL)
129 from_bl2 = plat_get_bl31_params();
130 if (plat_params == NULL)
131 plat_params = plat_get_bl31_plat_params();
132
133 /*
Varun Wadekar52a15982015-06-05 12:57:27 +0530134 * Copy BL3-3, BL3-2 entry point information.
Varun Wadekarb316e242015-05-19 16:48:04 +0530135 * They are stored in Secure RAM, in BL2's address space.
136 */
Varun Wadekard22d4ad2016-05-23 11:41:07 -0700137 assert(from_bl2);
Varun Wadekar6bb62462015-10-06 12:49:31 +0530138 assert(from_bl2->bl33_ep_info);
139 bl33_image_ep_info = *from_bl2->bl33_ep_info;
Varun Wadekarbaf903e2015-09-22 15:00:06 +0530140
Arve Hjønnevåg8f539492018-02-21 17:36:44 -0800141 if (from_bl2->bl32_ep_info) {
Varun Wadekarbaf903e2015-09-22 15:00:06 +0530142 bl32_image_ep_info = *from_bl2->bl32_ep_info;
Arve Hjønnevåg8f539492018-02-21 17:36:44 -0800143 bl32_mem_size = from_bl2->bl32_ep_info->args.arg0;
144 bl32_boot_params = from_bl2->bl32_ep_info->args.arg2;
145 }
Varun Wadekarb316e242015-05-19 16:48:04 +0530146
147 /*
Varun Wadekar6bb62462015-10-06 12:49:31 +0530148 * Parse platform specific parameters - TZDRAM aperture base and size
Varun Wadekarb316e242015-05-19 16:48:04 +0530149 */
Varun Wadekar6bb62462015-10-06 12:49:31 +0530150 assert(plat_params);
151 plat_bl31_params_from_bl2.tzdram_base = plat_params->tzdram_base;
152 plat_bl31_params_from_bl2.tzdram_size = plat_params->tzdram_size;
Varun Wadekard2014c62015-10-29 10:37:28 +0530153 plat_bl31_params_from_bl2.uart_id = plat_params->uart_id;
154
155 /*
Varun Wadekar1ec441e2016-03-24 15:34:24 -0700156 * It is very important that we run either from TZDRAM or TZSRAM base.
157 * Add an explicit check here.
158 */
159 if ((plat_bl31_params_from_bl2.tzdram_base != BL31_BASE) &&
160 (TEGRA_TZRAM_BASE != BL31_BASE))
161 panic();
162
163 /*
Varun Wadekard2014c62015-10-29 10:37:28 +0530164 * Get the base address of the UART controller to be used for the
165 * console
166 */
Varun Wadekard2014c62015-10-29 10:37:28 +0530167 tegra_console_base = plat_get_console_from_id(plat_params->uart_id);
168
Damon Duan777baa52016-11-07 19:37:50 +0800169 if (tegra_console_base != (uint64_t)0) {
170 /*
171 * Configure the UART port to be used as the console
172 */
173 console_init(tegra_console_base, TEGRA_BOOT_UART_CLK_IN_HZ,
174 TEGRA_CONSOLE_BAUDRATE);
Damon Duan777baa52016-11-07 19:37:50 +0800175 }
Varun Wadekard2014c62015-10-29 10:37:28 +0530176
Varun Wadekar5118b532016-06-04 22:08:50 -0700177 /*
Steven Kao27e64312016-10-21 14:16:59 +0800178 * Initialize delay timer
179 */
180 tegra_delay_timer_init();
181
182 /*
Varun Wadekar5118b532016-06-04 22:08:50 -0700183 * Do initial security configuration to allow DRAM/device access.
184 */
185 tegra_memctrl_tzdram_setup(plat_bl31_params_from_bl2.tzdram_base,
186 plat_bl31_params_from_bl2.tzdram_size);
187
Varun Wadekarb41a4142016-05-23 15:56:14 -0700188 /*
189 * The previous bootloader might not have placed the BL32 image
190 * inside the TZDRAM. We check the BL32 image info to find out
191 * the base/PC values and relocate the image if necessary.
192 */
193 if (from_bl2->bl32_image_info) {
194
195 bl32_img_info = *from_bl2->bl32_image_info;
196
197 /* Relocate BL32 if it resides outside of the TZDRAM */
198 tzdram_start = plat_bl31_params_from_bl2.tzdram_base;
199 tzdram_end = plat_bl31_params_from_bl2.tzdram_base +
200 plat_bl31_params_from_bl2.tzdram_size;
201 bl32_start = bl32_img_info.image_base;
202 bl32_end = bl32_img_info.image_base + bl32_img_info.image_size;
203
204 assert(tzdram_end > tzdram_start);
205 assert(bl32_end > bl32_start);
206 assert(bl32_image_ep_info.pc > tzdram_start);
207 assert(bl32_image_ep_info.pc < tzdram_end);
208
209 /* relocate BL32 */
210 if (bl32_start >= tzdram_end || bl32_end <= tzdram_start) {
211
212 INFO("Relocate BL32 to TZDRAM\n");
213
214 memcpy16((void *)(uintptr_t)bl32_image_ep_info.pc,
215 (void *)(uintptr_t)bl32_start,
216 bl32_img_info.image_size);
217
218 /* clean up non-secure intermediate buffer */
219 zeromem16((void *)(uintptr_t)bl32_start,
220 bl32_img_info.image_size);
221 }
222 }
223
Varun Wadekar3f0a8ad2016-03-28 15:56:47 -0700224 /* Early platform setup for Tegra SoCs */
225 plat_early_platform_setup();
226
Sandrine Bailleuxfff61b62018-06-21 11:41:43 +0200227 INFO("BL3-1: Boot CPU: %s Processor [%lx]\n",
228 (((read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK)
229 == DENVER_IMPL) ? "Denver" : "ARM", read_mpidr());
Varun Wadekarb316e242015-05-19 16:48:04 +0530230}
Arve Hjønnevåg8f539492018-02-21 17:36:44 -0800231
232#ifdef SPD_trusty
233void plat_trusty_set_boot_args(aapcs64_params_t *args)
234{
235 args->arg0 = bl32_mem_size;
236 args->arg1 = bl32_boot_params;
237 args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
238}
239#endif
Varun Wadekarb316e242015-05-19 16:48:04 +0530240
241/*******************************************************************************
242 * Initialize the gic, configure the SCR.
243 ******************************************************************************/
244void bl31_platform_setup(void)
245{
246 uint32_t tmp_reg;
247
Varun Wadekarb7b45752015-12-28 14:55:41 -0800248 /* Initialize the gic cpu and distributor interfaces */
249 plat_gic_setup();
250
Varun Wadekarb316e242015-05-19 16:48:04 +0530251 /*
252 * Setup secondary CPU POR infrastructure.
253 */
254 plat_secondary_setup();
255
256 /*
257 * Initial Memory Controller configuration.
258 */
259 tegra_memctrl_setup();
260
261 /*
Varun Wadekar0dc91812015-12-30 15:06:41 -0800262 * Set up the TZRAM memory aperture to allow only secure world
263 * access
264 */
265 tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
266
Varun Wadekarb316e242015-05-19 16:48:04 +0530267 /* Set the next EL to be AArch64 */
268 tmp_reg = SCR_RES1_BITS | SCR_RW_BIT;
269 write_scr(tmp_reg);
270
Varun Wadekarbaf903e2015-09-22 15:00:06 +0530271 INFO("BL3-1: Tegra platform setup complete\n");
Varun Wadekarb316e242015-05-19 16:48:04 +0530272}
273
274/*******************************************************************************
Varun Wadekar1dcffa92016-01-08 17:48:42 -0800275 * Perform any BL3-1 platform runtime setup prior to BL3-1 cold boot exit
276 ******************************************************************************/
277void bl31_plat_runtime_setup(void)
278{
Varun Wadekarc92050b2017-03-29 14:57:29 -0700279 /*
280 * During boot, USB3 and flash media (SDMMC/SATA) devices need
281 * access to IRAM. Because these clients connect to the MC and
282 * do not have a direct path to the IRAM, the MC implements AHB
283 * redirection during boot to allow path to IRAM. In this mode
284 * accesses to a programmed memory address aperture are directed
285 * to the AHB bus, allowing access to the IRAM. This mode must be
286 * disabled before we jump to the non-secure world.
287 */
288 tegra_memctrl_disable_ahb_redirection();
Varun Wadekar1dcffa92016-01-08 17:48:42 -0800289}
290
291/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530292 * Perform the very early platform specific architectural setup here. At the
293 * moment this only intializes the mmu in a quick and dirty way.
294 ******************************************************************************/
295void bl31_plat_arch_setup(void)
296{
Varun Wadekar3fb854f2017-02-28 08:23:59 -0800297 unsigned long rw_start = BL31_RW_START;
298 unsigned long rw_size = BL31_RW_END - BL31_RW_START;
299 unsigned long rodata_start = BL31_RODATA_BASE;
300 unsigned long rodata_size = BL31_RODATA_END - BL31_RODATA_BASE;
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000301 unsigned long code_base = TEXT_START;
302 unsigned long code_size = TEXT_END - TEXT_START;
Varun Wadekarb316e242015-05-19 16:48:04 +0530303 const mmap_region_t *plat_mmio_map = NULL;
Varun Wadekarb316e242015-05-19 16:48:04 +0530304#if USE_COHERENT_MEM
Varun Wadekar207cc732015-07-08 12:57:50 +0530305 unsigned long coh_start, coh_size;
Varun Wadekarb316e242015-05-19 16:48:04 +0530306#endif
Varun Wadekard1513632016-03-18 13:01:12 -0700307 plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
Varun Wadekarb316e242015-05-19 16:48:04 +0530308
309 /* add memory regions */
Varun Wadekar3fb854f2017-02-28 08:23:59 -0800310 mmap_add_region(rw_start, rw_start,
311 rw_size,
Varun Wadekarb316e242015-05-19 16:48:04 +0530312 MT_MEMORY | MT_RW | MT_SECURE);
Varun Wadekar3fb854f2017-02-28 08:23:59 -0800313 mmap_add_region(rodata_start, rodata_start,
314 rodata_size,
315 MT_RO_DATA | MT_SECURE);
316 mmap_add_region(code_base, code_base,
317 code_size,
318 MT_CODE | MT_SECURE);
Varun Wadekar207cc732015-07-08 12:57:50 +0530319
Varun Wadekard1513632016-03-18 13:01:12 -0700320 /* map TZDRAM used by BL31 as coherent memory */
321 if (TEGRA_TZRAM_BASE == tegra_bl31_phys_base) {
322 mmap_add_region(params_from_bl2->tzdram_base,
323 params_from_bl2->tzdram_base,
324 BL31_SIZE,
325 MT_DEVICE | MT_RW | MT_SECURE);
326 }
327
Varun Wadekarb316e242015-05-19 16:48:04 +0530328#if USE_COHERENT_MEM
Masahiro Yamada0fac5af2016-12-28 16:11:41 +0900329 coh_start = total_base + (BL_COHERENT_RAM_BASE - BL31_RO_BASE);
330 coh_size = BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE;
Varun Wadekar207cc732015-07-08 12:57:50 +0530331
Varun Wadekarb316e242015-05-19 16:48:04 +0530332 mmap_add_region(coh_start, coh_start,
333 coh_size,
334 MT_DEVICE | MT_RW | MT_SECURE);
335#endif
336
Steven Kao4d160ac2016-12-23 16:05:13 +0800337 /* map on-chip free running uS timer */
338 mmap_add_region(page_align((uint64_t)TEGRA_TMRUS_BASE, 0),
339 page_align((uint64_t)TEGRA_TMRUS_BASE, 0),
340 (uint64_t)TEGRA_TMRUS_SIZE,
341 MT_DEVICE | MT_RO | MT_SECURE);
342
Varun Wadekarb316e242015-05-19 16:48:04 +0530343 /* add MMIO space */
344 plat_mmio_map = plat_get_mmio_map();
345 if (plat_mmio_map)
346 mmap_add(plat_mmio_map);
347 else
348 WARN("MMIO map not available\n");
349
350 /* set up translation tables */
351 init_xlat_tables();
352
353 /* enable the MMU */
354 enable_mmu_el3(0);
Varun Wadekarbaf903e2015-09-22 15:00:06 +0530355
356 INFO("BL3-1: Tegra: MMU enabled\n");
Varun Wadekarb316e242015-05-19 16:48:04 +0530357}
Varun Wadekar7a269e22015-06-10 14:04:32 +0530358
359/*******************************************************************************
360 * Check if the given NS DRAM range is valid
361 ******************************************************************************/
362int bl31_check_ns_address(uint64_t base, uint64_t size_in_bytes)
363{
Varun Wadekar55902982017-01-25 13:35:27 -0800364 uint64_t end = base + size_in_bytes;
Varun Wadekar7a269e22015-06-10 14:04:32 +0530365
366 /*
367 * Check if the NS DRAM address is valid
368 */
Varun Wadekar55902982017-01-25 13:35:27 -0800369 if ((base < TEGRA_DRAM_BASE) || (end > TEGRA_DRAM_END)) {
Varun Wadekar7a269e22015-06-10 14:04:32 +0530370 ERROR("NS address is out-of-bounds!\n");
371 return -EFAULT;
372 }
373
374 /*
375 * TZDRAM aperture contains the BL31 and BL32 images, so we need
376 * to check if the NS DRAM range overlaps the TZDRAM aperture.
377 */
378 if ((base < TZDRAM_END) && (end > tegra_bl31_phys_base)) {
379 ERROR("NS address overlaps TZDRAM!\n");
380 return -ENOTSUP;
381 }
382
383 /* valid NS address */
384 return 0;
385}