xref: /arm-trusted-firmware/plat/rpi/rpi4/rpi4_bl31_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <inttypes.h>
9*91f16700Schasinglulu #include <stdint.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <libfdt.h>
12*91f16700Schasinglulu 
13*91f16700Schasinglulu #include <platform_def.h>
14*91f16700Schasinglulu #include <arch_helpers.h>
15*91f16700Schasinglulu #include <common/bl_common.h>
16*91f16700Schasinglulu #include <lib/mmio.h>
17*91f16700Schasinglulu #include <lib/xlat_tables/xlat_mmu_helpers.h>
18*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_defs.h>
19*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_v2.h>
20*91f16700Schasinglulu #include <plat/common/platform.h>
21*91f16700Schasinglulu #include <common/fdt_fixup.h>
22*91f16700Schasinglulu #include <common/fdt_wrappers.h>
23*91f16700Schasinglulu #include <libfdt.h>
24*91f16700Schasinglulu 
25*91f16700Schasinglulu #include <drivers/arm/gicv2.h>
26*91f16700Schasinglulu 
27*91f16700Schasinglulu #include <rpi_shared.h>
28*91f16700Schasinglulu 
29*91f16700Schasinglulu /*
30*91f16700Schasinglulu  * Fields at the beginning of armstub8.bin.
31*91f16700Schasinglulu  * While building the BL31 image, we put the stub magic into the binary.
32*91f16700Schasinglulu  * The GPU firmware detects this at boot time, clears that field as a
33*91f16700Schasinglulu  * confirmation and puts the kernel and DT address in the following words.
34*91f16700Schasinglulu  */
35*91f16700Schasinglulu extern uint32_t stub_magic;
36*91f16700Schasinglulu extern uint32_t dtb_ptr32;
37*91f16700Schasinglulu extern uint32_t kernel_entry32;
38*91f16700Schasinglulu 
39*91f16700Schasinglulu static const gicv2_driver_data_t rpi4_gic_data = {
40*91f16700Schasinglulu 	.gicd_base = RPI4_GIC_GICD_BASE,
41*91f16700Schasinglulu 	.gicc_base = RPI4_GIC_GICC_BASE,
42*91f16700Schasinglulu };
43*91f16700Schasinglulu 
44*91f16700Schasinglulu /*
45*91f16700Schasinglulu  * To be filled by the code below. At the moment BL32 is not supported.
46*91f16700Schasinglulu  * In the future these might be passed down from BL2.
47*91f16700Schasinglulu  */
48*91f16700Schasinglulu static entry_point_info_t bl32_image_ep_info;
49*91f16700Schasinglulu static entry_point_info_t bl33_image_ep_info;
50*91f16700Schasinglulu 
51*91f16700Schasinglulu /*******************************************************************************
52*91f16700Schasinglulu  * Return a pointer to the 'entry_point_info' structure of the next image for
53*91f16700Schasinglulu  * the security state specified. BL33 corresponds to the non-secure image type
54*91f16700Schasinglulu  * while BL32 corresponds to the secure image type. A NULL pointer is returned
55*91f16700Schasinglulu  * if the image does not exist.
56*91f16700Schasinglulu  ******************************************************************************/
57*91f16700Schasinglulu entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
58*91f16700Schasinglulu {
59*91f16700Schasinglulu 	entry_point_info_t *next_image_info;
60*91f16700Schasinglulu 
61*91f16700Schasinglulu 	assert(sec_state_is_valid(type) != 0);
62*91f16700Schasinglulu 
63*91f16700Schasinglulu 	next_image_info = (type == NON_SECURE)
64*91f16700Schasinglulu 			? &bl33_image_ep_info : &bl32_image_ep_info;
65*91f16700Schasinglulu 
66*91f16700Schasinglulu 	/* None of the images can have 0x0 as the entrypoint. */
67*91f16700Schasinglulu 	if (next_image_info->pc) {
68*91f16700Schasinglulu 		return next_image_info;
69*91f16700Schasinglulu 	} else {
70*91f16700Schasinglulu 		return NULL;
71*91f16700Schasinglulu 	}
72*91f16700Schasinglulu }
73*91f16700Schasinglulu 
74*91f16700Schasinglulu uintptr_t plat_get_ns_image_entrypoint(void)
75*91f16700Schasinglulu {
76*91f16700Schasinglulu #ifdef PRELOADED_BL33_BASE
77*91f16700Schasinglulu 	return PRELOADED_BL33_BASE;
78*91f16700Schasinglulu #else
79*91f16700Schasinglulu 	/* Cleared by the GPU if kernel address is valid. */
80*91f16700Schasinglulu 	if (stub_magic == 0)
81*91f16700Schasinglulu 		return kernel_entry32;
82*91f16700Schasinglulu 
83*91f16700Schasinglulu 	WARN("Stub magic failure, using default kernel address 0x80000\n");
84*91f16700Schasinglulu 	return 0x80000;
85*91f16700Schasinglulu #endif
86*91f16700Schasinglulu }
87*91f16700Schasinglulu 
88*91f16700Schasinglulu static uintptr_t rpi4_get_dtb_address(void)
89*91f16700Schasinglulu {
90*91f16700Schasinglulu #ifdef RPI3_PRELOADED_DTB_BASE
91*91f16700Schasinglulu 	return RPI3_PRELOADED_DTB_BASE;
92*91f16700Schasinglulu #else
93*91f16700Schasinglulu 	/* Cleared by the GPU if DTB address is valid. */
94*91f16700Schasinglulu 	if (stub_magic == 0)
95*91f16700Schasinglulu 		return dtb_ptr32;
96*91f16700Schasinglulu 
97*91f16700Schasinglulu 	WARN("Stub magic failure, DTB address unknown\n");
98*91f16700Schasinglulu 	return 0;
99*91f16700Schasinglulu #endif
100*91f16700Schasinglulu }
101*91f16700Schasinglulu 
102*91f16700Schasinglulu static void ldelay(register_t delay)
103*91f16700Schasinglulu {
104*91f16700Schasinglulu 	__asm__ volatile (
105*91f16700Schasinglulu 		"1:\tcbz %0, 2f\n\t"
106*91f16700Schasinglulu 		"sub %0, %0, #1\n\t"
107*91f16700Schasinglulu 		"b 1b\n"
108*91f16700Schasinglulu 		"2:"
109*91f16700Schasinglulu 		: "=&r" (delay) : "0" (delay)
110*91f16700Schasinglulu 	);
111*91f16700Schasinglulu }
112*91f16700Schasinglulu 
113*91f16700Schasinglulu /*******************************************************************************
114*91f16700Schasinglulu  * Perform any BL31 early platform setup. Here is an opportunity to copy
115*91f16700Schasinglulu  * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
116*91f16700Schasinglulu  * they are lost (potentially). This needs to be done before the MMU is
117*91f16700Schasinglulu  * initialized so that the memory layout can be used while creating page
118*91f16700Schasinglulu  * tables. BL2 has flushed this information to memory, so we are guaranteed
119*91f16700Schasinglulu  * to pick up good data.
120*91f16700Schasinglulu  ******************************************************************************/
121*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
122*91f16700Schasinglulu 				u_register_t arg2, u_register_t arg3)
123*91f16700Schasinglulu 
124*91f16700Schasinglulu {
125*91f16700Schasinglulu 	/*
126*91f16700Schasinglulu 	 * LOCAL_CONTROL:
127*91f16700Schasinglulu 	 * Bit 9 clear: Increment by 1 (vs. 2).
128*91f16700Schasinglulu 	 * Bit 8 clear: Timer source is 19.2MHz crystal (vs. APB).
129*91f16700Schasinglulu 	 */
130*91f16700Schasinglulu 	mmio_write_32(RPI4_LOCAL_CONTROL_BASE_ADDRESS, 0);
131*91f16700Schasinglulu 
132*91f16700Schasinglulu 	/* LOCAL_PRESCALER; divide-by (0x80000000 / register_val) == 1 */
133*91f16700Schasinglulu 	mmio_write_32(RPI4_LOCAL_CONTROL_PRESCALER, 0x80000000);
134*91f16700Schasinglulu 
135*91f16700Schasinglulu 	/* Early GPU firmware revisions need a little break here. */
136*91f16700Schasinglulu 	ldelay(100000);
137*91f16700Schasinglulu 
138*91f16700Schasinglulu 	/* Initialize the console to provide early debug support. */
139*91f16700Schasinglulu 	rpi3_console_init();
140*91f16700Schasinglulu 
141*91f16700Schasinglulu 	bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
142*91f16700Schasinglulu 	bl33_image_ep_info.spsr = rpi3_get_spsr_for_bl33_entry();
143*91f16700Schasinglulu 	SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
144*91f16700Schasinglulu 
145*91f16700Schasinglulu #if RPI3_DIRECT_LINUX_BOOT
146*91f16700Schasinglulu # if RPI3_BL33_IN_AARCH32
147*91f16700Schasinglulu 	/*
148*91f16700Schasinglulu 	 * According to the file ``Documentation/arm/Booting`` of the Linux
149*91f16700Schasinglulu 	 * kernel tree, Linux expects:
150*91f16700Schasinglulu 	 * r0 = 0
151*91f16700Schasinglulu 	 * r1 = machine type number, optional in DT-only platforms (~0 if so)
152*91f16700Schasinglulu 	 * r2 = Physical address of the device tree blob
153*91f16700Schasinglulu 	 */
154*91f16700Schasinglulu 	VERBOSE("rpi4: Preparing to boot 32-bit Linux kernel\n");
155*91f16700Schasinglulu 	bl33_image_ep_info.args.arg0 = 0U;
156*91f16700Schasinglulu 	bl33_image_ep_info.args.arg1 = ~0U;
157*91f16700Schasinglulu 	bl33_image_ep_info.args.arg2 = rpi4_get_dtb_address();
158*91f16700Schasinglulu # else
159*91f16700Schasinglulu 	/*
160*91f16700Schasinglulu 	 * According to the file ``Documentation/arm64/booting.txt`` of the
161*91f16700Schasinglulu 	 * Linux kernel tree, Linux expects the physical address of the device
162*91f16700Schasinglulu 	 * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
163*91f16700Schasinglulu 	 * must be 0.
164*91f16700Schasinglulu 	 */
165*91f16700Schasinglulu 	VERBOSE("rpi4: Preparing to boot 64-bit Linux kernel\n");
166*91f16700Schasinglulu 	bl33_image_ep_info.args.arg0 = rpi4_get_dtb_address();
167*91f16700Schasinglulu 	bl33_image_ep_info.args.arg1 = 0ULL;
168*91f16700Schasinglulu 	bl33_image_ep_info.args.arg2 = 0ULL;
169*91f16700Schasinglulu 	bl33_image_ep_info.args.arg3 = 0ULL;
170*91f16700Schasinglulu # endif /* RPI3_BL33_IN_AARCH32 */
171*91f16700Schasinglulu #endif /* RPI3_DIRECT_LINUX_BOOT */
172*91f16700Schasinglulu }
173*91f16700Schasinglulu 
174*91f16700Schasinglulu void bl31_plat_arch_setup(void)
175*91f16700Schasinglulu {
176*91f16700Schasinglulu 	/*
177*91f16700Schasinglulu 	 * Is the dtb_ptr32 pointer valid? If yes, map the DTB region.
178*91f16700Schasinglulu 	 * We map the 2MB region the DTB start address lives in, plus
179*91f16700Schasinglulu 	 * the next 2MB, to have enough room for expansion.
180*91f16700Schasinglulu 	 */
181*91f16700Schasinglulu 	if (stub_magic == 0) {
182*91f16700Schasinglulu 		unsigned long long dtb_region = dtb_ptr32;
183*91f16700Schasinglulu 
184*91f16700Schasinglulu 		dtb_region &= ~0x1fffff;	/* Align to 2 MB. */
185*91f16700Schasinglulu 		mmap_add_region(dtb_region, dtb_region, 4U << 20,
186*91f16700Schasinglulu 				MT_MEMORY | MT_RW | MT_NS);
187*91f16700Schasinglulu 	}
188*91f16700Schasinglulu 	/*
189*91f16700Schasinglulu 	 * Add the first page of memory, which holds the stub magic,
190*91f16700Schasinglulu 	 * the kernel and the DT address.
191*91f16700Schasinglulu 	 * This also holds the secondary CPU's entrypoints and mailboxes.
192*91f16700Schasinglulu 	 */
193*91f16700Schasinglulu 	mmap_add_region(0, 0, 4096, MT_NON_CACHEABLE | MT_RW | MT_SECURE);
194*91f16700Schasinglulu 
195*91f16700Schasinglulu 	rpi3_setup_page_tables(BL31_BASE, BL31_END - BL31_BASE,
196*91f16700Schasinglulu 			       BL_CODE_BASE, BL_CODE_END,
197*91f16700Schasinglulu 			       BL_RO_DATA_BASE, BL_RO_DATA_END
198*91f16700Schasinglulu #if USE_COHERENT_MEM
199*91f16700Schasinglulu 			       , BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END
200*91f16700Schasinglulu #endif
201*91f16700Schasinglulu 			      );
202*91f16700Schasinglulu 
203*91f16700Schasinglulu 	enable_mmu_el3(0);
204*91f16700Schasinglulu }
205*91f16700Schasinglulu 
206*91f16700Schasinglulu /*
207*91f16700Schasinglulu  * Remove the FDT /memreserve/ entry that covers the region at the very
208*91f16700Schasinglulu  * beginning of memory (if that exists). This is where the secondaries
209*91f16700Schasinglulu  * originally spin, but we pull them out there.
210*91f16700Schasinglulu  * Having overlapping /reserved-memory and /memreserve/ regions confuses
211*91f16700Schasinglulu  * the Linux kernel, so we need to get rid of this one.
212*91f16700Schasinglulu  */
213*91f16700Schasinglulu static void remove_spintable_memreserve(void *dtb)
214*91f16700Schasinglulu {
215*91f16700Schasinglulu 	uint64_t addr, size;
216*91f16700Schasinglulu 	int regions = fdt_num_mem_rsv(dtb);
217*91f16700Schasinglulu 	int i;
218*91f16700Schasinglulu 
219*91f16700Schasinglulu 	for (i = 0; i < regions; i++) {
220*91f16700Schasinglulu 		if (fdt_get_mem_rsv(dtb, i, &addr, &size) != 0) {
221*91f16700Schasinglulu 			return;
222*91f16700Schasinglulu 		}
223*91f16700Schasinglulu 		if (size == 0U) {
224*91f16700Schasinglulu 			return;
225*91f16700Schasinglulu 		}
226*91f16700Schasinglulu 		/* We only look for the region at the beginning of DRAM. */
227*91f16700Schasinglulu 		if (addr != 0U) {
228*91f16700Schasinglulu 			continue;
229*91f16700Schasinglulu 		}
230*91f16700Schasinglulu 		/*
231*91f16700Schasinglulu 		 * Currently the region in the existing DTs is exactly 4K
232*91f16700Schasinglulu 		 * in size. Should this value ever change, there is probably
233*91f16700Schasinglulu 		 * a reason for that, so inform the user about this.
234*91f16700Schasinglulu 		 */
235*91f16700Schasinglulu 		if (size == 4096U) {
236*91f16700Schasinglulu 			fdt_del_mem_rsv(dtb, i);
237*91f16700Schasinglulu 			return;
238*91f16700Schasinglulu 		}
239*91f16700Schasinglulu 		WARN("Keeping unknown /memreserve/ region at 0, size: %" PRId64 "\n",
240*91f16700Schasinglulu 		     size);
241*91f16700Schasinglulu 	}
242*91f16700Schasinglulu }
243*91f16700Schasinglulu 
244*91f16700Schasinglulu static void rpi4_prepare_dtb(void)
245*91f16700Schasinglulu {
246*91f16700Schasinglulu 	void *dtb = (void *)rpi4_get_dtb_address();
247*91f16700Schasinglulu 	uint32_t gic_int_prop[3];
248*91f16700Schasinglulu 	int ret, offs;
249*91f16700Schasinglulu 
250*91f16700Schasinglulu 	/* Return if no device tree is detected */
251*91f16700Schasinglulu 	if (fdt_check_header(dtb) != 0)
252*91f16700Schasinglulu 		return;
253*91f16700Schasinglulu 
254*91f16700Schasinglulu 	ret = fdt_open_into(dtb, dtb, 0x100000);
255*91f16700Schasinglulu 	if (ret < 0) {
256*91f16700Schasinglulu 		ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
257*91f16700Schasinglulu 		return;
258*91f16700Schasinglulu 	}
259*91f16700Schasinglulu 
260*91f16700Schasinglulu 	if (dt_add_psci_node(dtb)) {
261*91f16700Schasinglulu 		ERROR("Failed to add PSCI Device Tree node\n");
262*91f16700Schasinglulu 		return;
263*91f16700Schasinglulu 	}
264*91f16700Schasinglulu 
265*91f16700Schasinglulu 	if (dt_add_psci_cpu_enable_methods(dtb)) {
266*91f16700Schasinglulu 		ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
267*91f16700Schasinglulu 		return;
268*91f16700Schasinglulu 	}
269*91f16700Schasinglulu 
270*91f16700Schasinglulu 	/*
271*91f16700Schasinglulu 	 * Remove the original reserved region (used for the spintable), and
272*91f16700Schasinglulu 	 * replace it with a region describing the whole of Trusted Firmware.
273*91f16700Schasinglulu 	 */
274*91f16700Schasinglulu 	remove_spintable_memreserve(dtb);
275*91f16700Schasinglulu 	if (fdt_add_reserved_memory(dtb, "atf@0", 0, 0x80000))
276*91f16700Schasinglulu 		WARN("Failed to add reserved memory nodes to DT.\n");
277*91f16700Schasinglulu 
278*91f16700Schasinglulu 	offs = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-400");
279*91f16700Schasinglulu 	gic_int_prop[0] = cpu_to_fdt32(1);		// PPI
280*91f16700Schasinglulu 	gic_int_prop[1] = cpu_to_fdt32(9);		// PPI #9
281*91f16700Schasinglulu 	gic_int_prop[2] = cpu_to_fdt32(0x0f04);		// all cores, level high
282*91f16700Schasinglulu 	fdt_setprop(dtb, offs, "interrupts", gic_int_prop, 12);
283*91f16700Schasinglulu 
284*91f16700Schasinglulu 	offs = fdt_path_offset(dtb, "/chosen");
285*91f16700Schasinglulu 	fdt_setprop_string(dtb, offs, "stdout-path", "serial0");
286*91f16700Schasinglulu 
287*91f16700Schasinglulu 	ret = fdt_pack(dtb);
288*91f16700Schasinglulu 	if (ret < 0)
289*91f16700Schasinglulu 		ERROR("Failed to pack Device Tree at %p: error %d\n", dtb, ret);
290*91f16700Schasinglulu 
291*91f16700Schasinglulu 	clean_dcache_range((uintptr_t)dtb, fdt_blob_size(dtb));
292*91f16700Schasinglulu 	INFO("Changed device tree to advertise PSCI.\n");
293*91f16700Schasinglulu }
294*91f16700Schasinglulu 
295*91f16700Schasinglulu void bl31_platform_setup(void)
296*91f16700Schasinglulu {
297*91f16700Schasinglulu 	rpi4_prepare_dtb();
298*91f16700Schasinglulu 
299*91f16700Schasinglulu 	/* Configure the interrupt controller */
300*91f16700Schasinglulu 	gicv2_driver_init(&rpi4_gic_data);
301*91f16700Schasinglulu 	gicv2_distif_init();
302*91f16700Schasinglulu 	gicv2_pcpu_distif_init();
303*91f16700Schasinglulu 	gicv2_cpuif_enable();
304*91f16700Schasinglulu }
305