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