blob: b49665027165dfabb52e34cb3bbe6ed89786a071 [file] [log] [blame]
Varun Wadekar08438e22015-05-19 16:48:04 +05301/*
Joel Hutton9f85f9e2018-03-21 11:40:57 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Varun Wadekar08438e22015-05-19 16:48:04 +05303 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekar08438e22015-05-19 16:48:04 +05305 */
6
Varun Wadekar08438e22015-05-19 16:48:04 +05307#include <assert.h>
Varun Wadekar9a964512015-06-10 14:04:32 +05308#include <errno.h>
Varun Wadekar08438e22015-05-19 16:48:04 +05309#include <stddef.h>
Varun Wadekarda3849e2016-05-23 15:56:14 -070010#include <string.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011
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>
Varun Wadekar06b19d52015-12-30 15:06:41 -080029#include <tegra_def.h>
Varun Wadekar08438e22015-05-19 16:48:04 +053030#include <tegra_private.h>
31
Arve Hjønnevåg06ff2512018-02-21 17:36:44 -080032/* length of Trusty's input parameters (in bytes) */
33#define TRUSTY_PARAMS_LEN_BYTES (4096*2)
34
Antonio Nino Diaz93c78ed2018-08-16 16:52:57 +010035extern void memcpy16(void *dest, const void *src, unsigned int length);
Varun Wadekarda3849e2016-05-23 15:56:14 -070036
Varun Wadekar08438e22015-05-19 16:48:04 +053037/*******************************************************************************
38 * Declarations of linker defined symbols which will help us find the layout
39 * of trusted SRAM
40 ******************************************************************************/
Joel Hutton9f85f9e2018-03-21 11:40:57 +000041
42IMPORT_SYM(unsigned long, __RW_START__, BL31_RW_START);
43IMPORT_SYM(unsigned long, __RW_END__, BL31_RW_END);
44IMPORT_SYM(unsigned long, __RODATA_START__, BL31_RODATA_BASE);
45IMPORT_SYM(unsigned long, __RODATA_END__, BL31_RODATA_END);
46IMPORT_SYM(unsigned long, __TEXT_START__, TEXT_START);
47IMPORT_SYM(unsigned long, __TEXT_END__, TEXT_END);
Varun Wadekar08438e22015-05-19 16:48:04 +053048
Varun Wadekar08438e22015-05-19 16:48:04 +053049extern uint64_t tegra_bl31_phys_base;
Varun Wadekare1084212015-10-29 10:37:28 +053050extern uint64_t tegra_console_base;
Varun Wadekar08438e22015-05-19 16:48:04 +053051
Varun Wadekar08438e22015-05-19 16:48:04 +053052
Varun Wadekardc7fdad2015-06-05 12:57:27 +053053static entry_point_info_t bl33_image_ep_info, bl32_image_ep_info;
Varun Wadekar08438e22015-05-19 16:48:04 +053054static plat_params_from_bl2_t plat_bl31_params_from_bl2 = {
Varun Wadekar0bf1b022015-07-31 10:03:01 +053055 .tzdram_size = (uint64_t)TZDRAM_SIZE
Varun Wadekar08438e22015-05-19 16:48:04 +053056};
Arve Hjønnevåg06ff2512018-02-21 17:36:44 -080057static unsigned long bl32_mem_size;
58static unsigned long bl32_boot_params;
Varun Wadekar08438e22015-05-19 16:48:04 +053059
60/*******************************************************************************
61 * This variable holds the non-secure image entry address
62 ******************************************************************************/
63extern uint64_t ns_image_entrypoint;
64
65/*******************************************************************************
Varun Wadekar5ea0b022016-03-28 15:56:47 -070066 * The following platform setup functions are weakly defined. They
67 * provide typical implementations that will be overridden by a SoC.
68 ******************************************************************************/
69#pragma weak plat_early_platform_setup
Varun Wadekar8ab06d22016-05-23 11:41:07 -070070#pragma weak plat_get_bl31_params
71#pragma weak plat_get_bl31_plat_params
Varun Wadekar5ea0b022016-03-28 15:56:47 -070072
73void plat_early_platform_setup(void)
74{
75 ; /* do nothing */
76}
77
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +010078struct tegra_bl31_params *plat_get_bl31_params(void)
Varun Wadekar8ab06d22016-05-23 11:41:07 -070079{
80 return NULL;
81}
82
83plat_params_from_bl2_t *plat_get_bl31_plat_params(void)
84{
85 return NULL;
86}
87
Varun Wadekar5ea0b022016-03-28 15:56:47 -070088/*******************************************************************************
Varun Wadekar08438e22015-05-19 16:48:04 +053089 * Return a pointer to the 'entry_point_info' structure of the next image for
90 * security state specified. BL33 corresponds to the non-secure image type
91 * while BL32 corresponds to the secure image type.
92 ******************************************************************************/
93entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
94{
95 if (type == NON_SECURE)
96 return &bl33_image_ep_info;
97
Varun Wadekar4ce9a182016-06-06 10:46:28 -070098 /* return BL32 entry point info if it is valid */
99 if (type == SECURE && bl32_image_ep_info.pc)
Varun Wadekardc7fdad2015-06-05 12:57:27 +0530100 return &bl32_image_ep_info;
101
Varun Wadekar08438e22015-05-19 16:48:04 +0530102 return NULL;
103}
104
105/*******************************************************************************
106 * Return a pointer to the 'plat_params_from_bl2_t' structure. The BL2 image
107 * passes this platform specific information.
108 ******************************************************************************/
109plat_params_from_bl2_t *bl31_get_plat_params(void)
110{
111 return &plat_bl31_params_from_bl2;
112}
113
114/*******************************************************************************
115 * Perform any BL31 specific platform actions. Populate the BL33 and BL32 image
116 * info.
117 ******************************************************************************/
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100118void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
119 u_register_t arg2, u_register_t arg3)
Varun Wadekar08438e22015-05-19 16:48:04 +0530120{
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100121 struct tegra_bl31_params *arg_from_bl2 = (struct tegra_bl31_params *) arg0;
122 plat_params_from_bl2_t *plat_params = (plat_params_from_bl2_t *)arg1;
Varun Wadekarda3849e2016-05-23 15:56:14 -0700123 image_info_t bl32_img_info = { {0} };
124 uint64_t tzdram_start, tzdram_end, bl32_start, bl32_end;
Varun Wadekar08cefa92015-09-22 15:00:06 +0530125
Varun Wadekar08438e22015-05-19 16:48:04 +0530126 /*
Varun Wadekar8ab06d22016-05-23 11:41:07 -0700127 * For RESET_TO_BL31 systems, BL31 is the first bootloader to run so
128 * there's no argument to relay from a previous bootloader. Platforms
129 * might use custom ways to get arguments, so provide handlers which
130 * they can override.
131 */
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100132 if (arg_from_bl2 == NULL)
133 arg_from_bl2 = plat_get_bl31_params();
Varun Wadekar8ab06d22016-05-23 11:41:07 -0700134 if (plat_params == NULL)
135 plat_params = plat_get_bl31_plat_params();
136
137 /*
Varun Wadekardc7fdad2015-06-05 12:57:27 +0530138 * Copy BL3-3, BL3-2 entry point information.
Varun Wadekar08438e22015-05-19 16:48:04 +0530139 * They are stored in Secure RAM, in BL2's address space.
140 */
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100141 assert(arg_from_bl2);
142 assert(arg_from_bl2->bl33_ep_info);
143 bl33_image_ep_info = *arg_from_bl2->bl33_ep_info;
Varun Wadekar08cefa92015-09-22 15:00:06 +0530144
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100145 if (arg_from_bl2->bl32_ep_info) {
146 bl32_image_ep_info = *arg_from_bl2->bl32_ep_info;
147 bl32_mem_size = arg_from_bl2->bl32_ep_info->args.arg0;
148 bl32_boot_params = arg_from_bl2->bl32_ep_info->args.arg2;
Arve Hjønnevåg06ff2512018-02-21 17:36:44 -0800149 }
Varun Wadekar08438e22015-05-19 16:48:04 +0530150
151 /*
Varun Wadekare0d41582015-10-06 12:49:31 +0530152 * Parse platform specific parameters - TZDRAM aperture base and size
Varun Wadekar08438e22015-05-19 16:48:04 +0530153 */
Varun Wadekare0d41582015-10-06 12:49:31 +0530154 assert(plat_params);
155 plat_bl31_params_from_bl2.tzdram_base = plat_params->tzdram_base;
156 plat_bl31_params_from_bl2.tzdram_size = plat_params->tzdram_size;
Varun Wadekare1084212015-10-29 10:37:28 +0530157 plat_bl31_params_from_bl2.uart_id = plat_params->uart_id;
158
159 /*
Varun Wadekar939dcf22016-03-24 15:34:24 -0700160 * It is very important that we run either from TZDRAM or TZSRAM base.
161 * Add an explicit check here.
162 */
163 if ((plat_bl31_params_from_bl2.tzdram_base != BL31_BASE) &&
164 (TEGRA_TZRAM_BASE != BL31_BASE))
165 panic();
166
167 /*
Varun Wadekare1084212015-10-29 10:37:28 +0530168 * Get the base address of the UART controller to be used for the
169 * console
170 */
Varun Wadekare1084212015-10-29 10:37:28 +0530171 tegra_console_base = plat_get_console_from_id(plat_params->uart_id);
172
Damon Duan9b514f82016-11-07 19:37:50 +0800173 if (tegra_console_base != (uint64_t)0) {
174 /*
175 * Configure the UART port to be used as the console
176 */
177 console_init(tegra_console_base, TEGRA_BOOT_UART_CLK_IN_HZ,
178 TEGRA_CONSOLE_BAUDRATE);
Damon Duan9b514f82016-11-07 19:37:50 +0800179 }
Varun Wadekare1084212015-10-29 10:37:28 +0530180
Varun Wadekar08012f42016-06-04 22:08:50 -0700181 /*
Steven Kaod29d96f2016-10-21 14:16:59 +0800182 * Initialize delay timer
183 */
184 tegra_delay_timer_init();
185
186 /*
Varun Wadekar08012f42016-06-04 22:08:50 -0700187 * Do initial security configuration to allow DRAM/device access.
188 */
189 tegra_memctrl_tzdram_setup(plat_bl31_params_from_bl2.tzdram_base,
190 plat_bl31_params_from_bl2.tzdram_size);
191
Varun Wadekarda3849e2016-05-23 15:56:14 -0700192 /*
193 * The previous bootloader might not have placed the BL32 image
194 * inside the TZDRAM. We check the BL32 image info to find out
195 * the base/PC values and relocate the image if necessary.
196 */
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100197 if (arg_from_bl2->bl32_image_info) {
Varun Wadekarda3849e2016-05-23 15:56:14 -0700198
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100199 bl32_img_info = *arg_from_bl2->bl32_image_info;
Varun Wadekarda3849e2016-05-23 15:56:14 -0700200
201 /* Relocate BL32 if it resides outside of the TZDRAM */
202 tzdram_start = plat_bl31_params_from_bl2.tzdram_base;
203 tzdram_end = plat_bl31_params_from_bl2.tzdram_base +
204 plat_bl31_params_from_bl2.tzdram_size;
205 bl32_start = bl32_img_info.image_base;
206 bl32_end = bl32_img_info.image_base + bl32_img_info.image_size;
207
208 assert(tzdram_end > tzdram_start);
209 assert(bl32_end > bl32_start);
210 assert(bl32_image_ep_info.pc > tzdram_start);
211 assert(bl32_image_ep_info.pc < tzdram_end);
212
213 /* relocate BL32 */
214 if (bl32_start >= tzdram_end || bl32_end <= tzdram_start) {
215
216 INFO("Relocate BL32 to TZDRAM\n");
217
218 memcpy16((void *)(uintptr_t)bl32_image_ep_info.pc,
219 (void *)(uintptr_t)bl32_start,
220 bl32_img_info.image_size);
221
222 /* clean up non-secure intermediate buffer */
Antonio Nino Diazfdcc1122018-09-24 17:16:05 +0100223 zeromem((void *)(uintptr_t)bl32_start,
Varun Wadekarda3849e2016-05-23 15:56:14 -0700224 bl32_img_info.image_size);
225 }
226 }
227
Varun Wadekar5ea0b022016-03-28 15:56:47 -0700228 /* Early platform setup for Tegra SoCs */
229 plat_early_platform_setup();
230
Sandrine Bailleuxc426fd72018-06-21 11:41:43 +0200231 INFO("BL3-1: Boot CPU: %s Processor [%lx]\n",
232 (((read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK)
233 == DENVER_IMPL) ? "Denver" : "ARM", read_mpidr());
Varun Wadekar08438e22015-05-19 16:48:04 +0530234}
235
Arve Hjønnevåg06ff2512018-02-21 17:36:44 -0800236#ifdef SPD_trusty
237void plat_trusty_set_boot_args(aapcs64_params_t *args)
238{
239 args->arg0 = bl32_mem_size;
240 args->arg1 = bl32_boot_params;
241 args->arg2 = TRUSTY_PARAMS_LEN_BYTES;
242}
243#endif
244
Varun Wadekar08438e22015-05-19 16:48:04 +0530245/*******************************************************************************
246 * Initialize the gic, configure the SCR.
247 ******************************************************************************/
248void bl31_platform_setup(void)
249{
250 uint32_t tmp_reg;
251
Varun Wadekard3360302015-12-28 14:55:41 -0800252 /* Initialize the gic cpu and distributor interfaces */
253 plat_gic_setup();
254
Varun Wadekar08438e22015-05-19 16:48:04 +0530255 /*
256 * Setup secondary CPU POR infrastructure.
257 */
258 plat_secondary_setup();
259
260 /*
261 * Initial Memory Controller configuration.
262 */
263 tegra_memctrl_setup();
264
265 /*
Varun Wadekar06b19d52015-12-30 15:06:41 -0800266 * Set up the TZRAM memory aperture to allow only secure world
267 * access
268 */
269 tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
270
Varun Wadekar08438e22015-05-19 16:48:04 +0530271 /* Set the next EL to be AArch64 */
272 tmp_reg = SCR_RES1_BITS | SCR_RW_BIT;
273 write_scr(tmp_reg);
274
Varun Wadekar08cefa92015-09-22 15:00:06 +0530275 INFO("BL3-1: Tegra platform setup complete\n");
Varun Wadekar08438e22015-05-19 16:48:04 +0530276}
277
278/*******************************************************************************
Varun Wadekar25caa162016-01-08 17:48:42 -0800279 * Perform any BL3-1 platform runtime setup prior to BL3-1 cold boot exit
280 ******************************************************************************/
281void bl31_plat_runtime_setup(void)
282{
Varun Wadekar0c2276e2017-03-29 14:57:29 -0700283 /*
284 * During boot, USB3 and flash media (SDMMC/SATA) devices need
285 * access to IRAM. Because these clients connect to the MC and
286 * do not have a direct path to the IRAM, the MC implements AHB
287 * redirection during boot to allow path to IRAM. In this mode
288 * accesses to a programmed memory address aperture are directed
289 * to the AHB bus, allowing access to the IRAM. This mode must be
290 * disabled before we jump to the non-secure world.
291 */
292 tegra_memctrl_disable_ahb_redirection();
Varun Wadekar25caa162016-01-08 17:48:42 -0800293}
294
295/*******************************************************************************
Varun Wadekar08438e22015-05-19 16:48:04 +0530296 * Perform the very early platform specific architectural setup here. At the
297 * moment this only intializes the mmu in a quick and dirty way.
298 ******************************************************************************/
299void bl31_plat_arch_setup(void)
300{
Varun Wadekarbc0a0be2017-02-28 08:23:59 -0800301 unsigned long rw_start = BL31_RW_START;
302 unsigned long rw_size = BL31_RW_END - BL31_RW_START;
303 unsigned long rodata_start = BL31_RODATA_BASE;
304 unsigned long rodata_size = BL31_RODATA_END - BL31_RODATA_BASE;
Joel Hutton9f85f9e2018-03-21 11:40:57 +0000305 unsigned long code_base = TEXT_START;
306 unsigned long code_size = TEXT_END - TEXT_START;
Varun Wadekar08438e22015-05-19 16:48:04 +0530307 const mmap_region_t *plat_mmio_map = NULL;
Varun Wadekar08438e22015-05-19 16:48:04 +0530308#if USE_COHERENT_MEM
Varun Wadekar68e2a642015-07-08 12:57:50 +0530309 unsigned long coh_start, coh_size;
Varun Wadekar08438e22015-05-19 16:48:04 +0530310#endif
Varun Wadekar260ae462016-03-18 13:01:12 -0700311 plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
Varun Wadekar08438e22015-05-19 16:48:04 +0530312
313 /* add memory regions */
Varun Wadekarbc0a0be2017-02-28 08:23:59 -0800314 mmap_add_region(rw_start, rw_start,
315 rw_size,
Varun Wadekar08438e22015-05-19 16:48:04 +0530316 MT_MEMORY | MT_RW | MT_SECURE);
Varun Wadekarbc0a0be2017-02-28 08:23:59 -0800317 mmap_add_region(rodata_start, rodata_start,
318 rodata_size,
319 MT_RO_DATA | MT_SECURE);
320 mmap_add_region(code_base, code_base,
321 code_size,
322 MT_CODE | MT_SECURE);
Varun Wadekar68e2a642015-07-08 12:57:50 +0530323
Varun Wadekar260ae462016-03-18 13:01:12 -0700324 /* map TZDRAM used by BL31 as coherent memory */
325 if (TEGRA_TZRAM_BASE == tegra_bl31_phys_base) {
326 mmap_add_region(params_from_bl2->tzdram_base,
327 params_from_bl2->tzdram_base,
328 BL31_SIZE,
329 MT_DEVICE | MT_RW | MT_SECURE);
330 }
331
Varun Wadekar08438e22015-05-19 16:48:04 +0530332#if USE_COHERENT_MEM
Masahiro Yamada47497052016-12-28 16:11:41 +0900333 coh_start = total_base + (BL_COHERENT_RAM_BASE - BL31_RO_BASE);
334 coh_size = BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE;
Varun Wadekar68e2a642015-07-08 12:57:50 +0530335
Varun Wadekar08438e22015-05-19 16:48:04 +0530336 mmap_add_region(coh_start, coh_start,
337 coh_size,
338 MT_DEVICE | MT_RW | MT_SECURE);
339#endif
340
Steven Kaoe99eeec2016-12-23 16:05:13 +0800341 /* map on-chip free running uS timer */
342 mmap_add_region(page_align((uint64_t)TEGRA_TMRUS_BASE, 0),
343 page_align((uint64_t)TEGRA_TMRUS_BASE, 0),
344 (uint64_t)TEGRA_TMRUS_SIZE,
345 MT_DEVICE | MT_RO | MT_SECURE);
346
Varun Wadekar08438e22015-05-19 16:48:04 +0530347 /* add MMIO space */
348 plat_mmio_map = plat_get_mmio_map();
349 if (plat_mmio_map)
350 mmap_add(plat_mmio_map);
351 else
352 WARN("MMIO map not available\n");
353
354 /* set up translation tables */
355 init_xlat_tables();
356
357 /* enable the MMU */
358 enable_mmu_el3(0);
Varun Wadekar08cefa92015-09-22 15:00:06 +0530359
360 INFO("BL3-1: Tegra: MMU enabled\n");
Varun Wadekar08438e22015-05-19 16:48:04 +0530361}
Varun Wadekar9a964512015-06-10 14:04:32 +0530362
363/*******************************************************************************
364 * Check if the given NS DRAM range is valid
365 ******************************************************************************/
366int bl31_check_ns_address(uint64_t base, uint64_t size_in_bytes)
367{
Varun Wadekar14a1c0e2017-01-25 13:35:27 -0800368 uint64_t end = base + size_in_bytes;
Varun Wadekar9a964512015-06-10 14:04:32 +0530369
370 /*
371 * Check if the NS DRAM address is valid
372 */
Varun Wadekar14a1c0e2017-01-25 13:35:27 -0800373 if ((base < TEGRA_DRAM_BASE) || (end > TEGRA_DRAM_END)) {
Varun Wadekar9a964512015-06-10 14:04:32 +0530374 ERROR("NS address is out-of-bounds!\n");
375 return -EFAULT;
376 }
377
378 /*
379 * TZDRAM aperture contains the BL31 and BL32 images, so we need
380 * to check if the NS DRAM range overlaps the TZDRAM aperture.
381 */
382 if ((base < TZDRAM_END) && (end > tegra_bl31_phys_base)) {
383 ERROR("NS address overlaps TZDRAM!\n");
384 return -ENOTSUP;
385 }
386
387 /* valid NS address */
388 return 0;
389}