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