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