blob: 0a49d81b21d124e2396774b313a77e21755c5259 [file] [log] [blame]
Andre Przywara6d471e12019-07-09 11:25:57 +01001/*
2 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8
9#include <libfdt.h>
10
11#include <platform_def.h>
Andre Przywara88c9e1d2019-07-11 01:45:39 +010012#include <arch_helpers.h>
Andre Przywara6d471e12019-07-09 11:25:57 +010013#include <common/bl_common.h>
14#include <lib/mmio.h>
15#include <lib/xlat_tables/xlat_mmu_helpers.h>
16#include <lib/xlat_tables/xlat_tables_defs.h>
Andre Przywara2d8e99a2019-07-10 18:09:18 +010017#include <lib/xlat_tables/xlat_tables_v2.h>
Andre Przywara6d471e12019-07-09 11:25:57 +010018#include <plat/common/platform.h>
Andre Przywara88c9e1d2019-07-11 01:45:39 +010019#include <common/fdt_fixup.h>
20#include <libfdt.h>
Andre Przywara6d471e12019-07-09 11:25:57 +010021
22#include <drivers/arm/gicv2.h>
23
24#include <rpi_shared.h>
25
Andre Przywara2d8e99a2019-07-10 18:09:18 +010026/*
27 * Fields at the beginning of armstub8.bin.
28 * While building the BL31 image, we put the stub magic into the binary.
29 * The GPU firmware detects this at boot time, clears that field as a
30 * confirmation and puts the kernel and DT address in the following words.
31 */
32extern uint32_t stub_magic;
Andre Przywaraaa89ae42019-07-11 01:42:12 +010033extern uint32_t dtb_ptr32;
34extern uint32_t kernel_entry32;
Andre Przywara2d8e99a2019-07-10 18:09:18 +010035
Andre Przywara6d471e12019-07-09 11:25:57 +010036static const gicv2_driver_data_t rpi4_gic_data = {
37 .gicd_base = RPI4_GIC_GICD_BASE,
38 .gicc_base = RPI4_GIC_GICC_BASE,
39};
40
41/*
42 * To be filled by the code below. At the moment BL32 is not supported.
43 * In the future these might be passed down from BL2.
44 */
45static entry_point_info_t bl32_image_ep_info;
46static entry_point_info_t bl33_image_ep_info;
47
48/*******************************************************************************
49 * Return a pointer to the 'entry_point_info' structure of the next image for
50 * the security state specified. BL33 corresponds to the non-secure image type
51 * while BL32 corresponds to the secure image type. A NULL pointer is returned
52 * if the image does not exist.
53 ******************************************************************************/
54entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
55{
56 entry_point_info_t *next_image_info;
57
58 assert(sec_state_is_valid(type) != 0);
59
60 next_image_info = (type == NON_SECURE)
61 ? &bl33_image_ep_info : &bl32_image_ep_info;
62
63 /* None of the images can have 0x0 as the entrypoint. */
64 if (next_image_info->pc) {
65 return next_image_info;
66 } else {
67 return NULL;
68 }
69}
70
Andre Przywaraaa89ae42019-07-11 01:42:12 +010071uintptr_t plat_get_ns_image_entrypoint(void)
72{
73#ifdef PRELOADED_BL33_BASE
74 return PRELOADED_BL33_BASE;
75#else
76 /* Cleared by the GPU if kernel address is valid. */
77 if (stub_magic == 0)
78 return kernel_entry32;
79
80 WARN("Stub magic failure, using default kernel address 0x80000\n");
81 return 0x80000;
82#endif
83}
84
85static uintptr_t rpi4_get_dtb_address(void)
86{
87#ifdef RPI3_PRELOADED_DTB_BASE
88 return RPI3_PRELOADED_DTB_BASE;
89#else
90 /* Cleared by the GPU if DTB address is valid. */
91 if (stub_magic == 0)
92 return dtb_ptr32;
93
94 WARN("Stub magic failure, DTB address unknown\n");
95 return 0;
96#endif
97}
98
Andre Przywara6d471e12019-07-09 11:25:57 +010099static void ldelay(register_t delay)
100{
101 __asm__ volatile (
102 "1:\tcbz %0, 2f\n\t"
103 "sub %0, %0, #1\n\t"
104 "b 1b\n"
105 "2:"
106 : "=&r" (delay) : "0" (delay)
107 );
108}
109
110/*******************************************************************************
111 * Perform any BL31 early platform setup. Here is an opportunity to copy
112 * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
113 * they are lost (potentially). This needs to be done before the MMU is
114 * initialized so that the memory layout can be used while creating page
115 * tables. BL2 has flushed this information to memory, so we are guaranteed
116 * to pick up good data.
117 ******************************************************************************/
118void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
119 u_register_t arg2, u_register_t arg3)
120
121{
Andre Przywara6d471e12019-07-09 11:25:57 +0100122 /*
123 * LOCAL_CONTROL:
124 * Bit 9 clear: Increment by 1 (vs. 2).
125 * Bit 8 clear: Timer source is 19.2MHz crystal (vs. APB).
126 */
127 mmio_write_32(RPI4_LOCAL_CONTROL_BASE_ADDRESS, 0);
128
129 /* LOCAL_PRESCALER; divide-by (0x80000000 / register_val) == 1 */
130 mmio_write_32(RPI4_LOCAL_CONTROL_PRESCALER, 0x80000000);
131
132 /* Early GPU firmware revisions need a little break here. */
133 ldelay(100000);
134
Andre Przywara57ccecc2020-03-10 12:33:16 +0000135 /* Initialize the console to provide early debug support. */
136 rpi3_console_init();
Andre Przywara6d471e12019-07-09 11:25:57 +0100137
Andre Przywara6d471e12019-07-09 11:25:57 +0100138 bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
Andre Przywaraaa89ae42019-07-11 01:42:12 +0100139 bl33_image_ep_info.spsr = rpi3_get_spsr_for_bl33_entry();
Andre Przywara6d471e12019-07-09 11:25:57 +0100140 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
141
Andre Przywaraaa89ae42019-07-11 01:42:12 +0100142#if RPI3_DIRECT_LINUX_BOOT
Andre Przywara6d471e12019-07-09 11:25:57 +0100143# if RPI3_BL33_IN_AARCH32
144 /*
145 * According to the file ``Documentation/arm/Booting`` of the Linux
146 * kernel tree, Linux expects:
147 * r0 = 0
148 * r1 = machine type number, optional in DT-only platforms (~0 if so)
149 * r2 = Physical address of the device tree blob
150 */
151 VERBOSE("rpi4: Preparing to boot 32-bit Linux kernel\n");
152 bl33_image_ep_info.args.arg0 = 0U;
153 bl33_image_ep_info.args.arg1 = ~0U;
Andre Przywaraaa89ae42019-07-11 01:42:12 +0100154 bl33_image_ep_info.args.arg2 = rpi4_get_dtb_address();
Andre Przywara6d471e12019-07-09 11:25:57 +0100155# else
156 /*
157 * According to the file ``Documentation/arm64/booting.txt`` of the
158 * Linux kernel tree, Linux expects the physical address of the device
159 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
160 * must be 0.
161 */
162 VERBOSE("rpi4: Preparing to boot 64-bit Linux kernel\n");
Andre Przywaraaa89ae42019-07-11 01:42:12 +0100163 bl33_image_ep_info.args.arg0 = rpi4_get_dtb_address();
Andre Przywara6d471e12019-07-09 11:25:57 +0100164 bl33_image_ep_info.args.arg1 = 0ULL;
165 bl33_image_ep_info.args.arg2 = 0ULL;
166 bl33_image_ep_info.args.arg3 = 0ULL;
167# endif /* RPI3_BL33_IN_AARCH32 */
168#endif /* RPI3_DIRECT_LINUX_BOOT */
169}
170
171void bl31_plat_arch_setup(void)
172{
Andre Przywara2d8e99a2019-07-10 18:09:18 +0100173 /*
Andre Przywara88c9e1d2019-07-11 01:45:39 +0100174 * Is the dtb_ptr32 pointer valid? If yes, map the DTB region.
175 * We map the 2MB region the DTB start address lives in, plus
176 * the next 2MB, to have enough room for expansion.
177 */
178 if (stub_magic == 0) {
179 unsigned long long dtb_region = dtb_ptr32;
180
181 dtb_region &= ~0x1fffff; /* Align to 2 MB. */
182 mmap_add_region(dtb_region, dtb_region, 4U << 20,
183 MT_MEMORY | MT_RW | MT_NS);
184 }
185 /*
Andre Przywara2d8e99a2019-07-10 18:09:18 +0100186 * Add the first page of memory, which holds the stub magic,
187 * the kernel and the DT address.
Andre Przywara8b83a512019-07-15 09:04:27 +0100188 * This also holds the secondary CPU's entrypoints and mailboxes.
Andre Przywara2d8e99a2019-07-10 18:09:18 +0100189 */
Andre Przywara8b83a512019-07-15 09:04:27 +0100190 mmap_add_region(0, 0, 4096, MT_NON_CACHEABLE | MT_RW | MT_SECURE);
Andre Przywara2d8e99a2019-07-10 18:09:18 +0100191
Andre Przywara6d471e12019-07-09 11:25:57 +0100192 rpi3_setup_page_tables(BL31_BASE, BL31_END - BL31_BASE,
193 BL_CODE_BASE, BL_CODE_END,
194 BL_RO_DATA_BASE, BL_RO_DATA_END
195#if USE_COHERENT_MEM
196 , BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END
197#endif
198 );
199
200 enable_mmu_el3(0);
201}
202
Andre Przywara88c9e1d2019-07-11 01:45:39 +0100203static uint32_t dtb_size(const void *dtb)
204{
205 const uint32_t *dtb_header = dtb;
206
207 return fdt32_to_cpu(dtb_header[1]);
208}
209
210static void rpi4_prepare_dtb(void)
211{
212 void *dtb = (void *)rpi4_get_dtb_address();
Andre Przywara88ac8b62019-07-21 01:45:31 +0100213 uint32_t gic_int_prop[3];
214 int ret, offs;
Andre Przywara88c9e1d2019-07-11 01:45:39 +0100215
216 /* Return if no device tree is detected */
217 if (fdt_check_header(dtb) != 0)
218 return;
219
220 ret = fdt_open_into(dtb, dtb, 0x100000);
221 if (ret < 0) {
222 ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
223 return;
224 }
225
226 if (dt_add_psci_node(dtb)) {
227 ERROR("Failed to add PSCI Device Tree node\n");
228 return;
229 }
230
231 if (dt_add_psci_cpu_enable_methods(dtb)) {
232 ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
233 return;
234 }
235
Andre Przywaraaf7df712019-07-22 00:04:40 +0100236 /* Reserve memory used by Trusted Firmware. */
237 if (fdt_add_reserved_memory(dtb, "atf@0", 0, 0x80000))
238 WARN("Failed to add reserved memory nodes to DT.\n");
239
Andre Przywara88ac8b62019-07-21 01:45:31 +0100240 offs = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-400");
241 gic_int_prop[0] = cpu_to_fdt32(1); // PPI
242 gic_int_prop[1] = cpu_to_fdt32(9); // PPI #9
243 gic_int_prop[2] = cpu_to_fdt32(0x0f04); // all cores, level high
244 fdt_setprop(dtb, offs, "interrupts", gic_int_prop, 12);
245
Andre Przywara48a52912019-07-15 18:07:51 +0100246 offs = fdt_path_offset(dtb, "/chosen");
247 fdt_setprop_string(dtb, offs, "stdout-path", "serial0");
248
Andre Przywara88c9e1d2019-07-11 01:45:39 +0100249 ret = fdt_pack(dtb);
250 if (ret < 0)
251 ERROR("Failed to pack Device Tree at %p: error %d\n", dtb, ret);
252
253 clean_dcache_range((uintptr_t)dtb, dtb_size(dtb));
254 INFO("Changed device tree to advertise PSCI.\n");
255}
256
Andre Przywara6d471e12019-07-09 11:25:57 +0100257void bl31_platform_setup(void)
258{
Andre Przywara88c9e1d2019-07-11 01:45:39 +0100259 rpi4_prepare_dtb();
260
Andre Przywara6d471e12019-07-09 11:25:57 +0100261 /* Configure the interrupt controller */
262 gicv2_driver_init(&rpi4_gic_data);
263 gicv2_distif_init();
264 gicv2_pcpu_distif_init();
265 gicv2_cpuif_enable();
266}