xref: /netbsd-src/sys/arch/evbarm/fdt/fdt_machdep.c (revision b7341c0e432b08a315d32c52c618eef902d0379d)
1 /* $NetBSD: fdt_machdep.c,v 1.108 2024/06/30 17:55:28 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.108 2024/06/30 17:55:28 jmcneill Exp $");
31 
32 #include "opt_arm_debug.h"
33 #include "opt_bootconfig.h"
34 #include "opt_cpuoptions.h"
35 #include "opt_ddb.h"
36 #include "opt_efi.h"
37 #include "opt_machdep.h"
38 #include "opt_multiprocessor.h"
39 
40 #include "genfb.h"
41 #include "ukbd.h"
42 #include "wsdisplay.h"
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 
47 #include <sys/atomic.h>
48 #include <sys/bootblock.h>
49 #include <sys/bus.h>
50 #include <sys/conf.h>
51 #include <sys/cpu.h>
52 #include <sys/device.h>
53 #include <sys/disk.h>
54 #include <sys/disklabel.h>
55 #include <sys/endian.h>
56 #include <sys/exec.h>
57 #include <sys/fcntl.h>
58 #include <sys/kauth.h>
59 #include <sys/kernel.h>
60 #include <sys/kmem.h>
61 #include <sys/ksyms.h>
62 #include <sys/md5.h>
63 #include <sys/msgbuf.h>
64 #include <sys/proc.h>
65 #include <sys/pserialize.h>
66 #include <sys/reboot.h>
67 #include <sys/systm.h>
68 #include <sys/termios.h>
69 #include <sys/vnode.h>
70 #include <sys/uuid.h>
71 
72 #include <net/if.h>
73 #include <net/if_dl.h>
74 
75 #include <dev/cons.h>
76 #include <uvm/uvm_extern.h>
77 
78 #include <machine/db_machdep.h>
79 #include <ddb/db_sym.h>
80 #include <ddb/db_extern.h>
81 
82 #include <machine/bootconfig.h>
83 #include <arm/armreg.h>
84 
85 #include <arm/cpufunc.h>
86 
87 #include <evbarm/include/autoconf.h>
88 #include <evbarm/fdt/machdep.h>
89 #include <evbarm/fdt/platform.h>
90 
91 #include <arm/fdt/arm_fdtvar.h>
92 
93 #include <dev/fdt/fdtvar.h>
94 #include <dev/fdt/fdt_boot.h>
95 #include <dev/fdt/fdt_private.h>
96 #include <dev/fdt/fdt_memory.h>
97 
98 #ifdef EFI_RUNTIME
99 #include <arm/arm/efi_runtime.h>
100 #endif
101 
102 #if NWSDISPLAY > 0 && NGENFB > 0
103 #include <arm/fdt/arm_simplefb.h>
104 #endif
105 
106 #if NUKBD > 0
107 #include <dev/usb/ukbdvar.h>
108 #endif
109 #if NWSDISPLAY > 0
110 #include <dev/wscons/wsdisplayvar.h>
111 #endif
112 
113 BootConfig bootconfig;
114 char *boot_args = NULL;
115 
116 /* filled in before cleaning bss. keep in .data */
117 u_long uboot_args[4] __attribute__((__section__(".data")));
118 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
119 
120 #include <libfdt.h>
121 #include <dev/fdt/fdtvar.h>
122 #define FDT_BUF_SIZE	(512*1024)
123 static uint8_t fdt_data[FDT_BUF_SIZE];
124 
125 extern char KERNEL_BASE_phys[];
126 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
127 
128 static void fdt_device_register(device_t, void *);
129 static void fdt_device_register_post_config(device_t, void *);
130 static void fdt_reset(void);
131 static void fdt_powerdown(void);
132 
133 #if BYTE_ORDER == BIG_ENDIAN
134 static void fdt_update_fb_format(void);
135 #endif
136 
137 static void
earlyconsputc(dev_t dev,int c)138 earlyconsputc(dev_t dev, int c)
139 {
140 	uartputc(c);
141 }
142 
143 static int
earlyconsgetc(dev_t dev)144 earlyconsgetc(dev_t dev)
145 {
146 	return -1;
147 }
148 
149 static struct consdev earlycons = {
150 	.cn_putc = earlyconsputc,
151 	.cn_getc = earlyconsgetc,
152 	.cn_pollc = nullcnpollc,
153 };
154 
155 #ifdef VERBOSE_INIT_ARM
156 #define VPRINTF(...)	printf(__VA_ARGS__)
157 #else
158 #define VPRINTF(...)	__nothing
159 #endif
160 
161 static void
fdt_add_dram_blocks(const struct fdt_memory * m,void * arg)162 fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
163 {
164 	BootConfig *bc = arg;
165 
166 	VPRINTF("  %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
167 	bc->dram[bc->dramblocks].address = m->start;
168 	bc->dram[bc->dramblocks].pages =
169 	    (m->end - m->start) / PAGE_SIZE;
170 	bc->dramblocks++;
171 }
172 
173 static int nfdt_physmem = 0;
174 static struct boot_physmem fdt_physmem[FDT_MEMORY_RANGES];
175 
176 static void
fdt_add_boot_physmem(const struct fdt_memory * m,void * arg)177 fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
178 {
179 	const paddr_t saddr = round_page(m->start);
180 	const paddr_t eaddr = trunc_page(m->end);
181 
182 	VPRINTF("  %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
183 	if (saddr >= eaddr) {
184 		VPRINTF(" skipped\n");
185 		return;
186 	}
187 	VPRINTF("\n");
188 
189 	struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
190 
191 	KASSERT(nfdt_physmem <= FDT_MEMORY_RANGES);
192 
193 	bp->bp_start = atop(saddr);
194 	bp->bp_pages = atop(eaddr) - bp->bp_start;
195 	bp->bp_freelist = VM_FREELIST_DEFAULT;
196 
197 #ifdef PMAP_NEED_ALLOC_POOLPAGE
198 	const uint64_t memory_size = *(uint64_t *)arg;
199 	if (atop(memory_size) > bp->bp_pages) {
200 		arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
201 		bp->bp_freelist = VM_FREELIST_DIRECTMAP;
202 	}
203 #endif
204 }
205 
206 
207 static void
fdt_print_memory(const struct fdt_memory * m,void * arg)208 fdt_print_memory(const struct fdt_memory *m, void *arg)
209 {
210 
211 	VPRINTF("FDT /memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
212 	    m->start, m->end - m->start);
213 }
214 
215 
216 /*
217  * Define usable memory regions.
218  */
219 static void
fdt_build_bootconfig(uint64_t mem_start,uint64_t mem_end)220 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
221 {
222 	BootConfig *bc = &bootconfig;
223 
224 	uint64_t addr, size;
225 	int index;
226 
227 	/* Reserve pages for ramdisk, rndseed, and firmware's RNG */
228 	fdt_reserve_initrd();
229 	fdt_reserve_rndseed();
230 	fdt_reserve_efirng();
231 
232 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
233 	if (framebuffer >= 0) {
234 		for (index = 0;
235 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
236 		     index++) {
237 			fdt_memory_remove_range(addr, size);
238 		}
239 	}
240 
241 	VPRINTF("Usable memory:\n");
242 	bc->dramblocks = 0;
243 	fdt_memory_foreach(fdt_add_dram_blocks, bc);
244 }
245 
246 
247 vaddr_t
initarm(void * arg)248 initarm(void *arg)
249 {
250 	const struct fdt_platform *plat;
251 	uint64_t memory_start, memory_end;
252 
253 	/* set temporally to work printf()/panic() even before consinit() */
254 	cn_tab = &earlycons;
255 
256 	/* Load FDT */
257 	int error = fdt_check_header(fdt_addr_r);
258 	if (error != 0)
259 		panic("fdt_check_header failed: %s", fdt_strerror(error));
260 
261 	/* If the DTB is too big, try to pack it in place first. */
262 	if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
263 		(void)fdt_pack(__UNCONST(fdt_addr_r));
264 
265 	error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
266 	if (error != 0)
267 		panic("fdt_move failed: %s", fdt_strerror(error));
268 
269 	fdtbus_init(fdt_data);
270 
271 	/* Lookup platform specific backend */
272 	plat = fdt_platform_find();
273 	if (plat == NULL)
274 		panic("Kernel does not support this device");
275 
276 	/* Early console may be available, announce ourselves. */
277 	VPRINTF("FDT<%p>\n", fdt_addr_r);
278 
279 	boot_args = fdt_get_bootargs();
280 
281 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
282 	VPRINTF("cpufunc\n");
283 	if (set_cpufuncs())
284 		panic("cpu not recognized!");
285 
286 	/*
287 	 * Memory is still identity/flat mapped this point so using ttbr for
288 	 * l1pt VA is fine
289 	 */
290 
291 	VPRINTF("devmap %p\n", plat->fp_devmap());
292 	extern char ARM_BOOTSTRAP_LxPT[];
293 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->fp_devmap());
294 
295 	VPRINTF("bootstrap\n");
296 	plat->fp_bootstrap();
297 
298 	/*
299 	 * If stdout-path is specified on the command line, override the
300 	 * value in /chosen/stdout-path before initializing console.
301 	 */
302 	VPRINTF("stdout\n");
303 	fdt_update_stdout_path(fdt_data, boot_args);
304 
305 #if BYTE_ORDER == BIG_ENDIAN
306 	/*
307 	 * Most boards are configured to little-endian mode initially, and
308 	 * switched to big-endian mode after kernel is loaded. In this case,
309 	 * framebuffer seems byte-swapped to CPU. Override FDT to let
310 	 * drivers know.
311 	 */
312 	VPRINTF("fb_format\n");
313 	fdt_update_fb_format();
314 #endif
315 
316 	/*
317 	 * Done making changes to the FDT.
318 	 */
319 	fdt_pack(fdt_data);
320 
321 	VPRINTF("consinit ");
322 	consinit();
323 	VPRINTF("ok\n");
324 
325 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
326 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
327 
328 	cpu_reset_address = fdt_reset;
329 	cpu_powerdown_address = fdt_powerdown;
330 	evbarm_device_register = fdt_device_register;
331 	evbarm_device_register_post_config = fdt_device_register_post_config;
332 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
333 
334 	/* Talk to the user */
335 	printf("NetBSD/evbarm (fdt) booting ...\n");
336 
337 #ifdef BOOT_ARGS
338 	char mi_bootargs[] = BOOT_ARGS;
339 	parse_mi_bootargs(mi_bootargs);
340 #endif
341 
342 	fdt_memory_get(&memory_start, &memory_end);
343 
344 	fdt_memory_foreach(fdt_print_memory, NULL);
345 
346 #if !defined(_LP64)
347 	/* Cannot map memory above 4GB (remove last page as well) */
348 	const uint64_t memory_limit = 0x100000000ULL - PAGE_SIZE;
349 	if (memory_end > memory_limit) {
350 		fdt_memory_remove_range(memory_limit , memory_end);
351 		memory_end = memory_limit;
352 	}
353 #endif
354 	uint64_t memory_size = memory_end - memory_start;
355 
356 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
357 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
358 
359 	/* Parse ramdisk info */
360 	fdt_probe_initrd();
361 
362 	/* Parse our on-disk rndseed and the firmware's RNG from EFI */
363 	fdt_probe_rndseed();
364 	fdt_probe_efirng();
365 
366 	fdt_memory_remove_reserved(memory_start, memory_end);
367 
368 	/*
369 	 * Populate bootconfig structure for the benefit of dodumpsys
370 	 */
371 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
372 	fdt_build_bootconfig(memory_start, memory_end);
373 
374 	/* Perform PT build and VM init */
375 	cpu_kernel_vm_init(memory_start, memory_size);
376 
377 	VPRINTF("bootargs: %s\n", boot_args);
378 
379 	parse_mi_bootargs(boot_args);
380 
381 	VPRINTF("Memory regions:\n");
382 
383 	/* Populate fdt_physmem / nfdt_physmem for initarm_common */
384 	fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
385 
386 	vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
387 	     nfdt_physmem);
388 
389 	/*
390 	 * initarm_common flushes cache if required before AP start
391 	 */
392 	error = 0;
393 	if ((boothowto & RB_MD1) == 0) {
394 		VPRINTF("mpstart\n");
395 		if (plat->fp_mpstart)
396 			error = plat->fp_mpstart();
397 	}
398 
399 	if (error)
400 		return sp;
401 
402 	/*
403 	 * Now we have APs started the pages used for stacks and L1PT can
404 	 * be given to uvm
405 	 */
406 	extern char const __start__init_memory[];
407 	extern char const __stop__init_memory[] __weak;
408 
409 	if (&__start__init_memory[0] != &__stop__init_memory[0]) {
410 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
411 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
412 		const paddr_t spg = atop(spa);
413 		const paddr_t epg = atop(epa);
414 
415 		VPRINTF("         start %08lx  end %08lx... "
416 		    "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
417 
418 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
419 	}
420 
421 	return sp;
422 }
423 
424 void
consinit(void)425 consinit(void)
426 {
427 	static bool initialized = false;
428 	const struct fdt_platform *plat = fdt_platform_find();
429 	const struct fdt_console *cons = fdtbus_get_console();
430 	struct fdt_attach_args faa;
431 	u_int uart_freq = 0;
432 
433 	if (initialized || cons == NULL)
434 		return;
435 
436 	plat->fp_init_attach_args(&faa);
437 	faa.faa_phandle = fdtbus_get_stdout_phandle();
438 
439 	if (plat->fp_uart_freq != NULL)
440 		uart_freq = plat->fp_uart_freq();
441 
442 	cons->consinit(&faa, uart_freq);
443 
444 	initialized = true;
445 }
446 
447 void
cpu_startup_hook(void)448 cpu_startup_hook(void)
449 {
450 #ifdef EFI_RUNTIME
451 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
452 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
453 	fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
454 #endif
455 
456 	fdtbus_intr_init();
457 
458 	fdt_setup_rndseed();
459 	fdt_setup_efirng();
460 }
461 
462 void
delay(u_int us)463 delay(u_int us)
464 {
465 	const struct fdt_platform *plat = fdt_platform_find();
466 
467 	plat->fp_delay(us);
468 }
469 static void
fdt_device_register(device_t self,void * aux)470 fdt_device_register(device_t self, void *aux)
471 {
472 	const struct fdt_platform *plat = fdt_platform_find();
473 
474 	if (device_is_a(self, "armfdt")) {
475 		fdt_setup_initrd();
476 
477 #if NWSDISPLAY > 0 && NGENFB > 0
478 		/*
479 		 * Setup framebuffer console, if present.
480 		 */
481 		arm_simplefb_preattach();
482 #endif
483 	}
484 
485 #if NWSDISPLAY > 0 && NGENFB > 0
486 	if (device_is_a(self, "genfb")) {
487 		prop_dictionary_t dict = device_properties(self);
488 		prop_dictionary_set_uint64(dict,
489 		    "simplefb-physaddr", arm_simplefb_physaddr());
490 	}
491 #endif
492 
493 	if (plat && plat->fp_device_register)
494 		plat->fp_device_register(self, aux);
495 }
496 
497 static void
fdt_device_register_post_config(device_t self,void * aux)498 fdt_device_register_post_config(device_t self, void *aux)
499 {
500 	const struct fdt_platform *plat = fdt_platform_find();
501 
502 	if (plat && plat->fp_device_register_post_config)
503 		plat->fp_device_register_post_config(self, aux);
504 
505 #if NUKBD > 0 && NWSDISPLAY > 0
506 	if (device_is_a(self, "wsdisplay")) {
507 		struct wsdisplay_softc *sc = device_private(self);
508 		if (wsdisplay_isconsole(sc))
509 			ukbd_cnattach();
510 	}
511 #endif
512 }
513 
514 static void
fdt_reset(void)515 fdt_reset(void)
516 {
517 	const struct fdt_platform *plat = fdt_platform_find();
518 
519 	fdtbus_power_reset();
520 
521 	if (plat && plat->fp_reset)
522 		plat->fp_reset();
523 }
524 
525 static void
fdt_powerdown(void)526 fdt_powerdown(void)
527 {
528 	fdtbus_power_poweroff();
529 }
530 
531 #if BYTE_ORDER == BIG_ENDIAN
532 static void
fdt_update_fb_format(void)533 fdt_update_fb_format(void)
534 {
535 	int off, len;
536 	const char *format, *replace;
537 
538 	off = fdt_path_offset(fdt_data, "/chosen");
539 	if (off < 0)
540 		return;
541 
542 	for (;;) {
543 		off = fdt_node_offset_by_compatible(fdt_data, off,
544 		    "simple-framebuffer");
545 		if (off < 0)
546 			return;
547 
548 		format = fdt_getprop(fdt_data, off, "format", &len);
549 		if (format == NULL)
550 			continue;
551 
552 		replace = NULL;
553 		if (strcmp(format, "a8b8g8r8") == 0)
554 			replace = "r8g8b8a8";
555 		else if (strcmp(format, "x8r8g8b8") == 0)
556 			replace = "b8g8r8x8";
557 		if (replace != NULL)
558 			fdt_setprop(fdt_data, off, "format", replace,
559 			    strlen(replace) + 1);
560 	}
561 }
562 #endif
563