blob: 59157536b5ec14d1e1497a3ff09525c1b7ba1b3b [file] [log] [blame]
Antonio Nino Diazae6779e2017-11-06 14:49:04 +00001/*
Andre Przywara4ea3bd32019-07-09 14:32:11 +01002 * Copyright (c) 2015-2019, 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
Andre Przywara4ea3bd32019-07-09 14:32:11 +010018#include <rpi_shared.h>
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000019
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000020/*
21 * Placeholder variables for copying the arguments that have been passed to
22 * BL31 from BL2.
23 */
24static entry_point_info_t bl32_image_ep_info;
25static 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 ******************************************************************************/
33entry_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/*******************************************************************************
Andre Przywaraaa89ae42019-07-11 01:42:12 +010051 * Return entrypoint of BL33.
52 ******************************************************************************/
53uintptr_t plat_get_ns_image_entrypoint(void)
54{
55#ifdef PRELOADED_BL33_BASE
56 return PRELOADED_BL33_BASE;
57#else
58 return PLAT_RPI3_NS_IMAGE_OFFSET;
59#endif
60}
61
62/*******************************************************************************
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000063 * Perform any BL31 early platform setup. Here is an opportunity to copy
John Tsichritzisd653d332018-09-14 10:34:57 +010064 * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000065 * they are lost (potentially). This needs to be done before the MMU is
66 * initialized so that the memory layout can be used while creating page
67 * tables. BL2 has flushed this information to memory, so we are guaranteed
68 * to pick up good data.
69 ******************************************************************************/
Antonio Nino Diaz83d8c792018-08-17 14:25:08 +010070void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
71 u_register_t arg2, u_register_t arg3)
72
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000073{
74 /* Initialize the console to provide early debug support */
Andre Przywara57ccecc2020-03-10 12:33:16 +000075 rpi3_console_init();
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000076
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000077 /*
Antonio Nino Diaz83d8c792018-08-17 14:25:08 +010078 * In debug builds, a special value is passed in 'arg1' to verify
79 * platform parameters from BL2 to BL31. Not used in release builds.
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000080 */
Antonio Nino Diaz83d8c792018-08-17 14:25:08 +010081 assert(arg1 == RPI3_BL31_PLAT_PARAM_VAL);
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000082
Antonio Nino Diaz83d8c792018-08-17 14:25:08 +010083 /* Check that params passed from BL2 are not NULL. */
84 bl_params_t *params_from_bl2 = (bl_params_t *) arg0;
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000085
86 assert(params_from_bl2 != NULL);
87 assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
88 assert(params_from_bl2->h.version >= VERSION_2);
89
90 bl_params_node_t *bl_params = params_from_bl2->head;
91
92 /*
93 * Copy BL33 and BL32 (if present), entry point information.
94 * They are stored in Secure RAM, in BL2's address space.
95 */
96 while (bl_params) {
97 if (bl_params->image_id == BL32_IMAGE_ID) {
98 bl32_image_ep_info = *bl_params->ep_info;
99 }
100
101 if (bl_params->image_id == BL33_IMAGE_ID) {
102 bl33_image_ep_info = *bl_params->ep_info;
103 }
104
105 bl_params = bl_params->next_params_info;
106 }
107
108 if (bl33_image_ep_info.pc == 0) {
109 panic();
110 }
Antonio Nino Diazf8b36cc2018-07-15 12:32:32 +0100111
112#if RPI3_DIRECT_LINUX_BOOT
113# if RPI3_BL33_IN_AARCH32
114 /*
115 * According to the file ``Documentation/arm/Booting`` of the Linux
116 * kernel tree, Linux expects:
117 * r0 = 0
118 * r1 = machine type number, optional in DT-only platforms (~0 if so)
119 * r2 = Physical address of the device tree blob
120 */
121 VERBOSE("rpi3: Preparing to boot 32-bit Linux kernel\n");
122 bl33_image_ep_info.args.arg0 = 0U;
123 bl33_image_ep_info.args.arg1 = ~0U;
124 bl33_image_ep_info.args.arg2 = (u_register_t) RPI3_PRELOADED_DTB_BASE;
125# else
126 /*
127 * According to the file ``Documentation/arm64/booting.txt`` of the
128 * Linux kernel tree, Linux expects the physical address of the device
129 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
130 * must be 0.
131 */
132 VERBOSE("rpi3: Preparing to boot 64-bit Linux kernel\n");
133 bl33_image_ep_info.args.arg0 = (u_register_t) RPI3_PRELOADED_DTB_BASE;
134 bl33_image_ep_info.args.arg1 = 0ULL;
135 bl33_image_ep_info.args.arg2 = 0ULL;
136 bl33_image_ep_info.args.arg3 = 0ULL;
137# endif /* RPI3_BL33_IN_AARCH32 */
138#endif /* RPI3_DIRECT_LINUX_BOOT */
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000139}
140
141void bl31_plat_arch_setup(void)
142{
143 rpi3_setup_page_tables(BL31_BASE, BL31_END - BL31_BASE,
144 BL_CODE_BASE, BL_CODE_END,
145 BL_RO_DATA_BASE, BL_RO_DATA_END
146#if USE_COHERENT_MEM
147 , BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END
148#endif
149 );
150
151 enable_mmu_el3(0);
152}
153
Igor Opaniuk02016b22019-01-16 23:59:41 +0200154#ifdef RPI3_PRELOADED_DTB_BASE
Antonio Nino Diazf96582a2018-10-19 00:57:16 +0100155/*
156 * Add information to the device tree (if any) about the reserved DRAM used by
157 * the Trusted Firmware.
158 */
159static void rpi3_dtb_add_mem_rsv(void)
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000160{
Antonio Nino Diazf96582a2018-10-19 00:57:16 +0100161 int i, regions, rc;
162 uint64_t addr, size;
163 void *dtb = (void *)RPI3_PRELOADED_DTB_BASE;
164
165 INFO("rpi3: Checking DTB...\n");
166
167 /* Return if no device tree is detected */
168 if (fdt_check_header(dtb) != 0)
169 return;
170
171 regions = fdt_num_mem_rsv(dtb);
172
173 VERBOSE("rpi3: Found %d mem reserve region(s)\n", regions);
174
175 /* We expect to find one reserved region that we can modify */
176 if (regions < 1)
177 return;
178
179 /*
180 * Look for the region that corresponds to the default boot firmware. It
181 * starts at address 0, and it is not needed when the default firmware
182 * is replaced by this port of the Trusted Firmware.
183 */
184 for (i = 0; i < regions; i++) {
185 if (fdt_get_mem_rsv(dtb, i, &addr, &size) != 0)
186 continue;
187
188 if (addr != 0x0)
189 continue;
190
191 VERBOSE("rpi3: Firmware mem reserve region found\n");
192
193 rc = fdt_del_mem_rsv(dtb, i);
194 if (rc != 0) {
195 INFO("rpi3: Can't remove mem reserve region (%d)\n", rc);
196 }
197
198 break;
199 }
200
201 if (i == regions) {
202 VERBOSE("rpi3: Firmware mem reserve region not found\n");
203 }
204
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000205 /*
Antonio Nino Diazf96582a2018-10-19 00:57:16 +0100206 * Reserve all SRAM. As said in the documentation, this isn't actually
207 * secure memory, so it is needed to tell BL33 that this is a reserved
208 * memory region. It doesn't guarantee it won't use it, though.
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000209 */
Antonio Nino Diazf96582a2018-10-19 00:57:16 +0100210 rc = fdt_add_mem_rsv(dtb, SEC_SRAM_BASE, SEC_SRAM_SIZE);
211 if (rc != 0) {
212 WARN("rpi3: Can't add mem reserve region (%d)\n", rc);
213 }
214
215 INFO("rpi3: Reserved 0x%llx - 0x%llx in DTB\n", SEC_SRAM_BASE,
216 SEC_SRAM_BASE + SEC_SRAM_SIZE);
217}
Igor Opaniuk02016b22019-01-16 23:59:41 +0200218#endif
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000219
Antonio Nino Diazf96582a2018-10-19 00:57:16 +0100220void bl31_platform_setup(void)
221{
222#ifdef RPI3_PRELOADED_DTB_BASE
223 /* Only modify a DTB if we know where to look for it */
224 rpi3_dtb_add_mem_rsv();
225#endif
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000226}