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