Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.79
      1  1.79     skrll /* $NetBSD: fdt_machdep.c,v 1.79 2020/11/24 06:36:36 skrll 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.79     skrll __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.79 2020/11/24 06:36:36 skrll Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include "opt_machdep.h"
     33  1.21       ryo #include "opt_bootconfig.h"
     34   1.1  jmcneill #include "opt_ddb.h"
     35   1.1  jmcneill #include "opt_md.h"
     36   1.1  jmcneill #include "opt_arm_debug.h"
     37   1.1  jmcneill #include "opt_multiprocessor.h"
     38   1.1  jmcneill #include "opt_cpuoptions.h"
     39  1.46  jmcneill #include "opt_efi.h"
     40   1.1  jmcneill 
     41  1.75  jmcneill #include "genfb.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.1  jmcneill #include <sys/systm.h>
     47   1.1  jmcneill #include <sys/bus.h>
     48   1.1  jmcneill #include <sys/atomic.h>
     49   1.1  jmcneill #include <sys/cpu.h>
     50   1.1  jmcneill #include <sys/device.h>
     51  1.76       rin #include <sys/endian.h>
     52   1.1  jmcneill #include <sys/exec.h>
     53   1.1  jmcneill #include <sys/kernel.h>
     54   1.1  jmcneill #include <sys/kmem.h>
     55   1.1  jmcneill #include <sys/ksyms.h>
     56   1.1  jmcneill #include <sys/msgbuf.h>
     57   1.1  jmcneill #include <sys/proc.h>
     58   1.1  jmcneill #include <sys/reboot.h>
     59   1.1  jmcneill #include <sys/termios.h>
     60  1.34  jmcneill #include <sys/bootblock.h>
     61  1.34  jmcneill #include <sys/disklabel.h>
     62  1.34  jmcneill #include <sys/vnode.h>
     63  1.34  jmcneill #include <sys/kauth.h>
     64  1.34  jmcneill #include <sys/fcntl.h>
     65  1.53  jmcneill #include <sys/uuid.h>
     66  1.53  jmcneill #include <sys/disk.h>
     67  1.34  jmcneill #include <sys/md5.h>
     68  1.55  jmcneill #include <sys/pserialize.h>
     69  1.65  riastrad #include <sys/rnd.h>
     70  1.69  riastrad #include <sys/rndsource.h>
     71  1.55  jmcneill 
     72  1.55  jmcneill #include <net/if.h>
     73  1.55  jmcneill #include <net/if_dl.h>
     74   1.1  jmcneill 
     75  1.23       ryo #include <dev/cons.h>
     76   1.1  jmcneill #include <uvm/uvm_extern.h>
     77   1.1  jmcneill 
     78   1.1  jmcneill #include <sys/conf.h>
     79   1.1  jmcneill 
     80   1.1  jmcneill #include <machine/db_machdep.h>
     81   1.1  jmcneill #include <ddb/db_sym.h>
     82   1.1  jmcneill #include <ddb/db_extern.h>
     83   1.1  jmcneill 
     84   1.1  jmcneill #include <machine/bootconfig.h>
     85   1.1  jmcneill #include <arm/armreg.h>
     86   1.1  jmcneill 
     87  1.21       ryo #include <arm/cpufunc.h>
     88   1.1  jmcneill 
     89   1.1  jmcneill #include <evbarm/include/autoconf.h>
     90  1.30     skrll #include <evbarm/fdt/machdep.h>
     91   1.1  jmcneill #include <evbarm/fdt/platform.h>
     92  1.49  jmcneill #include <evbarm/fdt/fdt_memory.h>
     93   1.1  jmcneill 
     94   1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     95  1.68     skrll #include <dev/fdt/fdt_private.h>
     96   1.1  jmcneill 
     97  1.46  jmcneill #ifdef EFI_RUNTIME
     98  1.46  jmcneill #include <arm/arm/efi_runtime.h>
     99  1.46  jmcneill #endif
    100  1.46  jmcneill 
    101  1.75  jmcneill #if NWSDISPLAY > 0 && NGENFB > 0
    102  1.75  jmcneill #include <arm/fdt/arm_simplefb.h>
    103  1.75  jmcneill #endif
    104  1.75  jmcneill 
    105  1.14  jmcneill #if NUKBD > 0
    106  1.14  jmcneill #include <dev/usb/ukbdvar.h>
    107  1.14  jmcneill #endif
    108  1.39    bouyer #if NWSDISPLAY > 0
    109  1.39    bouyer #include <dev/wscons/wsdisplayvar.h>
    110  1.39    bouyer #endif
    111  1.14  jmcneill 
    112   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
    113   1.8  jmcneill #include <dev/md.h>
    114   1.8  jmcneill #endif
    115   1.8  jmcneill 
    116   1.1  jmcneill #ifndef FDT_MAX_BOOT_STRING
    117   1.1  jmcneill #define FDT_MAX_BOOT_STRING 1024
    118   1.1  jmcneill #endif
    119   1.1  jmcneill 
    120   1.1  jmcneill BootConfig bootconfig;
    121   1.1  jmcneill char bootargs[FDT_MAX_BOOT_STRING] = "";
    122   1.1  jmcneill char *boot_args = NULL;
    123  1.26  christos 
    124  1.26  christos /* filled in before cleaning bss. keep in .data */
    125  1.27  christos u_long uboot_args[4] __attribute__((__section__(".data")));
    126  1.28     skrll const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
    127   1.1  jmcneill 
    128   1.8  jmcneill static uint64_t initrd_start, initrd_end;
    129  1.69  riastrad static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */
    130  1.69  riastrad static uint64_t efirng_start, efirng_end;   /* firmware's EFI RNG output */
    131   1.8  jmcneill 
    132   1.1  jmcneill #include <libfdt.h>
    133   1.1  jmcneill #include <dev/fdt/fdtvar.h>
    134  1.40  jmcneill #define FDT_BUF_SIZE	(512*1024)
    135   1.1  jmcneill static uint8_t fdt_data[FDT_BUF_SIZE];
    136   1.1  jmcneill 
    137   1.1  jmcneill extern char KERNEL_BASE_phys[];
    138   1.1  jmcneill #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
    139   1.1  jmcneill 
    140   1.5  jmcneill static void fdt_update_stdout_path(void);
    141   1.1  jmcneill static void fdt_device_register(device_t, void *);
    142  1.39    bouyer static void fdt_device_register_post_config(device_t, void *);
    143  1.34  jmcneill static void fdt_cpu_rootconf(void);
    144   1.1  jmcneill static void fdt_reset(void);
    145   1.1  jmcneill static void fdt_powerdown(void);
    146   1.1  jmcneill 
    147  1.76       rin #if BYTE_ORDER == BIG_ENDIAN
    148  1.76       rin static void fdt_update_fb_format(void);
    149  1.76       rin #endif
    150  1.76       rin 
    151   1.1  jmcneill static void
    152  1.23       ryo earlyconsputc(dev_t dev, int c)
    153   1.1  jmcneill {
    154  1.48     skrll 	uartputc(c);
    155   1.1  jmcneill }
    156   1.1  jmcneill 
    157  1.23       ryo static int
    158  1.23       ryo earlyconsgetc(dev_t dev)
    159   1.1  jmcneill {
    160  1.54     skrll 	return 0;
    161  1.23       ryo }
    162   1.1  jmcneill 
    163  1.54     skrll static struct consdev earlycons = {
    164  1.54     skrll 	.cn_putc = earlyconsputc,
    165  1.54     skrll 	.cn_getc = earlyconsgetc,
    166  1.54     skrll 	.cn_pollc = nullcnpollc,
    167  1.54     skrll };
    168  1.54     skrll 
    169  1.23       ryo #ifdef VERBOSE_INIT_ARM
    170  1.29     skrll #define VPRINTF(...)	printf(__VA_ARGS__)
    171   1.1  jmcneill #else
    172  1.43     skrll #define VPRINTF(...)	__nothing
    173   1.1  jmcneill #endif
    174   1.1  jmcneill 
    175   1.7  jmcneill /*
    176  1.61  jmcneill  * Get all of physical memory, including holes.
    177   1.7  jmcneill  */
    178   1.7  jmcneill static void
    179  1.21       ryo fdt_get_memory(uint64_t *pstart, uint64_t *pend)
    180   1.7  jmcneill {
    181   1.7  jmcneill 	const int memory = OF_finddevice("/memory");
    182   1.7  jmcneill 	uint64_t cur_addr, cur_size;
    183   1.7  jmcneill 	int index;
    184   1.7  jmcneill 
    185   1.7  jmcneill 	/* Assume the first entry is the start of memory */
    186  1.21       ryo 	if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0)
    187   1.7  jmcneill 		panic("Cannot determine memory size");
    188   1.7  jmcneill 
    189  1.21       ryo 	*pstart = cur_addr;
    190  1.21       ryo 	*pend = cur_addr + cur_size;
    191  1.21       ryo 
    192  1.29     skrll 	VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    193  1.21       ryo 	    0, *pstart, *pend - *pstart);
    194   1.7  jmcneill 
    195   1.7  jmcneill 	for (index = 1;
    196   1.7  jmcneill 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
    197   1.7  jmcneill 	     index++) {
    198  1.29     skrll 		VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    199   1.7  jmcneill 		    index, cur_addr, cur_size);
    200  1.21       ryo 
    201  1.21       ryo 		if (cur_addr + cur_size > *pend)
    202  1.21       ryo 			*pend = cur_addr + cur_size;
    203   1.7  jmcneill 	}
    204   1.7  jmcneill }
    205   1.7  jmcneill 
    206  1.16     skrll void
    207   1.8  jmcneill fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
    208   1.8  jmcneill {
    209  1.49  jmcneill 	fdt_memory_remove_range(addr, size);
    210   1.8  jmcneill }
    211   1.8  jmcneill 
    212   1.8  jmcneill /*
    213  1.11  jmcneill  * Exclude memory ranges from memory config from the device tree
    214   1.8  jmcneill  */
    215   1.8  jmcneill static void
    216  1.33  jmcneill fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr)
    217   1.8  jmcneill {
    218  1.44     skrll 	uint64_t lstart = 0, lend = 0;
    219   1.8  jmcneill 	uint64_t addr, size;
    220  1.11  jmcneill 	int index, error;
    221   1.8  jmcneill 
    222  1.11  jmcneill 	const int num = fdt_num_mem_rsv(fdtbus_get_data());
    223  1.11  jmcneill 	for (index = 0; index <= num; index++) {
    224  1.11  jmcneill 		error = fdt_get_mem_rsv(fdtbus_get_data(), index,
    225  1.11  jmcneill 		    &addr, &size);
    226  1.44     skrll 		if (error != 0)
    227  1.44     skrll 			continue;
    228  1.44     skrll 		if (lstart <= addr && addr <= lend) {
    229  1.44     skrll 			size -= (lend - addr);
    230  1.44     skrll 			addr = lend;
    231  1.44     skrll 		}
    232  1.44     skrll 		if (size == 0)
    233  1.11  jmcneill 			continue;
    234  1.33  jmcneill 		if (addr + size <= min_addr)
    235  1.33  jmcneill 			continue;
    236   1.8  jmcneill 		if (addr >= max_addr)
    237   1.8  jmcneill 			continue;
    238  1.33  jmcneill 		if (addr < min_addr) {
    239  1.33  jmcneill 			size -= (min_addr - addr);
    240  1.33  jmcneill 			addr = min_addr;
    241  1.33  jmcneill 		}
    242   1.8  jmcneill 		if (addr + size > max_addr)
    243   1.8  jmcneill 			size = max_addr - addr;
    244   1.8  jmcneill 		fdt_add_reserved_memory_range(addr, size);
    245  1.44     skrll 		lstart = addr;
    246  1.44     skrll 		lend = addr + size;
    247   1.8  jmcneill 	}
    248   1.8  jmcneill }
    249   1.8  jmcneill 
    250  1.49  jmcneill static void
    251  1.49  jmcneill fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
    252  1.49  jmcneill {
    253  1.49  jmcneill 	BootConfig *bc = arg;
    254  1.49  jmcneill 
    255  1.52  jmcneill 	VPRINTF("  %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
    256  1.49  jmcneill 	bc->dram[bc->dramblocks].address = m->start;
    257  1.49  jmcneill 	bc->dram[bc->dramblocks].pages =
    258  1.49  jmcneill 	    (m->end - m->start) / PAGE_SIZE;
    259  1.49  jmcneill 	bc->dramblocks++;
    260  1.49  jmcneill }
    261  1.49  jmcneill 
    262  1.49  jmcneill #define MAX_PHYSMEM 64
    263  1.49  jmcneill static int nfdt_physmem = 0;
    264  1.49  jmcneill static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
    265  1.49  jmcneill 
    266  1.49  jmcneill static void
    267  1.49  jmcneill fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
    268  1.49  jmcneill {
    269  1.62     skrll 	const paddr_t saddr = round_page(m->start);
    270  1.62     skrll 	const paddr_t eaddr = trunc_page(m->end);
    271  1.62     skrll 
    272  1.62     skrll 	VPRINTF("  %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
    273  1.62     skrll 	if (saddr >= eaddr) {
    274  1.62     skrll 		VPRINTF(" skipped\n");
    275  1.62     skrll 		return;
    276  1.62     skrll 	}
    277  1.62     skrll 	VPRINTF("\n");
    278  1.62     skrll 
    279  1.49  jmcneill 	struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
    280  1.49  jmcneill 
    281  1.49  jmcneill 	KASSERT(nfdt_physmem <= MAX_PHYSMEM);
    282  1.49  jmcneill 
    283  1.62     skrll 	bp->bp_start = atop(saddr);
    284  1.62     skrll 	bp->bp_pages = atop(eaddr) - bp->bp_start;
    285  1.49  jmcneill 	bp->bp_freelist = VM_FREELIST_DEFAULT;
    286  1.49  jmcneill 
    287  1.49  jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
    288  1.49  jmcneill 	const uint64_t memory_size = *(uint64_t *)arg;
    289  1.49  jmcneill 	if (atop(memory_size) > bp->bp_pages) {
    290  1.49  jmcneill 		arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
    291  1.49  jmcneill 		bp->bp_freelist = VM_FREELIST_DIRECTMAP;
    292  1.49  jmcneill 	}
    293  1.49  jmcneill #endif
    294  1.49  jmcneill }
    295  1.49  jmcneill 
    296   1.8  jmcneill /*
    297   1.8  jmcneill  * Define usable memory regions.
    298   1.8  jmcneill  */
    299   1.8  jmcneill static void
    300  1.21       ryo fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
    301   1.8  jmcneill {
    302   1.8  jmcneill 	const int memory = OF_finddevice("/memory");
    303   1.8  jmcneill 	BootConfig *bc = &bootconfig;
    304   1.8  jmcneill 	uint64_t addr, size;
    305  1.49  jmcneill 	int index;
    306   1.8  jmcneill 
    307   1.8  jmcneill 	for (index = 0;
    308   1.8  jmcneill 	     fdtbus_get_reg64(memory, index, &addr, &size) == 0;
    309   1.8  jmcneill 	     index++) {
    310  1.21       ryo 		if (addr >= mem_end || size == 0)
    311   1.8  jmcneill 			continue;
    312  1.21       ryo 		if (addr + size > mem_end)
    313  1.21       ryo 			size = mem_end - addr;
    314   1.8  jmcneill 
    315  1.49  jmcneill 		fdt_memory_add_range(addr, size);
    316   1.8  jmcneill 	}
    317   1.8  jmcneill 
    318  1.33  jmcneill 	fdt_add_reserved_memory(mem_start, mem_end);
    319   1.8  jmcneill 
    320  1.73  jmcneill 	const uint64_t initrd_size =
    321  1.73  jmcneill 	    round_page(initrd_end) - trunc_page(initrd_start);
    322   1.8  jmcneill 	if (initrd_size > 0)
    323  1.73  jmcneill 		fdt_memory_remove_range(trunc_page(initrd_start), initrd_size);
    324   1.8  jmcneill 
    325  1.73  jmcneill 	const uint64_t rndseed_size =
    326  1.73  jmcneill 	    round_page(rndseed_end) - trunc_page(rndseed_start);
    327  1.65  riastrad 	if (rndseed_size > 0)
    328  1.73  jmcneill 		fdt_memory_remove_range(trunc_page(rndseed_start),
    329  1.73  jmcneill 		    rndseed_size);
    330  1.65  riastrad 
    331  1.73  jmcneill 	const uint64_t efirng_size =
    332  1.73  jmcneill 	    round_page(efirng_end) - trunc_page(efirng_start);
    333  1.69  riastrad 	if (efirng_size > 0)
    334  1.73  jmcneill 		fdt_memory_remove_range(trunc_page(efirng_start), efirng_size);
    335  1.69  riastrad 
    336  1.47  jmcneill 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
    337  1.47  jmcneill 	if (framebuffer >= 0) {
    338  1.47  jmcneill 		for (index = 0;
    339  1.47  jmcneill 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
    340  1.47  jmcneill 		     index++) {
    341  1.47  jmcneill 			fdt_add_reserved_memory_range(addr, size);
    342  1.47  jmcneill 		}
    343  1.47  jmcneill 	}
    344  1.47  jmcneill 
    345  1.29     skrll 	VPRINTF("Usable memory:\n");
    346   1.8  jmcneill 	bc->dramblocks = 0;
    347  1.49  jmcneill 	fdt_memory_foreach(fdt_add_dram_blocks, bc);
    348   1.8  jmcneill }
    349   1.8  jmcneill 
    350   1.8  jmcneill static void
    351  1.70  riastrad fdt_probe_range(const char *startname, const char *endname,
    352  1.70  riastrad     uint64_t *pstart, uint64_t *pend)
    353   1.8  jmcneill {
    354  1.70  riastrad 	int chosen, len;
    355  1.70  riastrad 	const void *start_data, *end_data;
    356  1.70  riastrad 
    357   1.8  jmcneill 	*pstart = *pend = 0;
    358   1.8  jmcneill 
    359  1.70  riastrad 	chosen = OF_finddevice("/chosen");
    360   1.8  jmcneill 	if (chosen < 0)
    361   1.8  jmcneill 		return;
    362   1.8  jmcneill 
    363  1.70  riastrad 	start_data = fdtbus_get_prop(chosen, startname, &len);
    364  1.70  riastrad 	end_data = fdtbus_get_prop(chosen, endname, NULL);
    365   1.8  jmcneill 	if (start_data == NULL || end_data == NULL)
    366   1.8  jmcneill 		return;
    367   1.8  jmcneill 
    368   1.8  jmcneill 	switch (len) {
    369   1.8  jmcneill 	case 4:
    370   1.8  jmcneill 		*pstart = be32dec(start_data);
    371   1.8  jmcneill 		*pend = be32dec(end_data);
    372   1.8  jmcneill 		break;
    373   1.8  jmcneill 	case 8:
    374   1.8  jmcneill 		*pstart = be64dec(start_data);
    375   1.8  jmcneill 		*pend = be64dec(end_data);
    376   1.8  jmcneill 		break;
    377   1.8  jmcneill 	default:
    378  1.70  riastrad 		printf("Unsupported len %d for /chosen `%s'\n",
    379  1.70  riastrad 		    len, startname);
    380   1.8  jmcneill 		return;
    381   1.8  jmcneill 	}
    382   1.8  jmcneill }
    383   1.8  jmcneill 
    384  1.70  riastrad static void *
    385  1.70  riastrad fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize,
    386  1.70  riastrad     const char *purpose)
    387   1.8  jmcneill {
    388  1.70  riastrad 	const paddr_t startpa = trunc_page(start);
    389  1.70  riastrad 	const paddr_t endpa = round_page(end);
    390   1.8  jmcneill 	paddr_t pa;
    391   1.8  jmcneill 	vaddr_t va;
    392  1.70  riastrad 	void *ptr;
    393   1.8  jmcneill 
    394  1.70  riastrad 	*psize = end - start;
    395  1.70  riastrad 	if (*psize == 0)
    396  1.70  riastrad 		return NULL;
    397   1.8  jmcneill 
    398  1.72     skrll 	const vaddr_t voff = start & PAGE_MASK;
    399  1.72     skrll 
    400  1.70  riastrad 	va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT);
    401   1.8  jmcneill 	if (va == 0) {
    402  1.70  riastrad 		printf("Failed to allocate VA for %s\n", purpose);
    403  1.70  riastrad 		return NULL;
    404   1.8  jmcneill 	}
    405  1.72     skrll 	ptr = (void *)(va + voff);
    406   1.8  jmcneill 
    407   1.8  jmcneill 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
    408   1.8  jmcneill 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
    409   1.8  jmcneill 	pmap_update(pmap_kernel());
    410   1.8  jmcneill 
    411  1.70  riastrad 	return ptr;
    412  1.70  riastrad }
    413  1.70  riastrad 
    414  1.70  riastrad static void
    415  1.71  riastrad fdt_unmap_range(void *ptr, uint64_t size)
    416  1.71  riastrad {
    417  1.71  riastrad 	const char *start = ptr, *end = start + size;
    418  1.71  riastrad 	const vaddr_t startva = trunc_page((vaddr_t)(uintptr_t)start);
    419  1.71  riastrad 	const vaddr_t endva = round_page((vaddr_t)(uintptr_t)end);
    420  1.71  riastrad 
    421  1.71  riastrad 	pmap_kremove(startva, endva - startva);
    422  1.71  riastrad 	pmap_update(pmap_kernel());
    423  1.71  riastrad }
    424  1.71  riastrad 
    425  1.71  riastrad static void
    426  1.70  riastrad fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
    427  1.70  riastrad {
    428  1.70  riastrad 	*pstart = *pend = 0;
    429  1.70  riastrad 
    430  1.70  riastrad #ifdef MEMORY_DISK_DYNAMIC
    431  1.70  riastrad 	fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend);
    432   1.8  jmcneill #endif
    433   1.8  jmcneill }
    434   1.8  jmcneill 
    435  1.65  riastrad static void
    436  1.70  riastrad fdt_setup_initrd(void)
    437  1.65  riastrad {
    438  1.70  riastrad #ifdef MEMORY_DISK_DYNAMIC
    439  1.70  riastrad 	void *md_start;
    440  1.70  riastrad 	uint64_t initrd_size;
    441  1.65  riastrad 
    442  1.70  riastrad 	md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size,
    443  1.70  riastrad 	    "initrd");
    444  1.70  riastrad 	if (md_start == NULL)
    445  1.65  riastrad 		return;
    446  1.70  riastrad 	md_root_setconf(md_start, initrd_size);
    447  1.70  riastrad #endif
    448  1.70  riastrad }
    449  1.65  riastrad 
    450  1.70  riastrad static void
    451  1.70  riastrad fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
    452  1.70  riastrad {
    453  1.65  riastrad 
    454  1.70  riastrad 	fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end",
    455  1.70  riastrad 	    pstart, pend);
    456  1.65  riastrad }
    457  1.65  riastrad 
    458  1.65  riastrad static void
    459  1.65  riastrad fdt_setup_rndseed(void)
    460  1.65  riastrad {
    461  1.70  riastrad 	uint64_t rndseed_size;
    462  1.65  riastrad 	void *rndseed;
    463  1.65  riastrad 
    464  1.70  riastrad 	rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size,
    465  1.70  riastrad 	    "rndseed");
    466  1.70  riastrad 	if (rndseed == NULL)
    467  1.65  riastrad 		return;
    468  1.65  riastrad 	rnd_seed(rndseed, rndseed_size);
    469  1.71  riastrad 	fdt_unmap_range(rndseed, rndseed_size);
    470  1.65  riastrad }
    471  1.65  riastrad 
    472  1.69  riastrad static void
    473  1.69  riastrad fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
    474  1.69  riastrad {
    475  1.69  riastrad 
    476  1.70  riastrad 	fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end",
    477  1.70  riastrad 	    pstart, pend);
    478  1.69  riastrad }
    479  1.69  riastrad 
    480  1.69  riastrad static struct krndsource efirng_source;
    481  1.69  riastrad 
    482  1.69  riastrad static void
    483  1.69  riastrad fdt_setup_efirng(void)
    484  1.69  riastrad {
    485  1.70  riastrad 	uint64_t efirng_size;
    486  1.69  riastrad 	void *efirng;
    487  1.69  riastrad 
    488  1.70  riastrad 	efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size,
    489  1.70  riastrad 	    "efirng");
    490  1.70  riastrad 	if (efirng == NULL)
    491  1.69  riastrad 		return;
    492  1.69  riastrad 
    493  1.69  riastrad 	rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
    494  1.69  riastrad 	    RND_FLAG_DEFAULT);
    495  1.69  riastrad 	rnd_add_data(&efirng_source, efirng, efirng_size, 0);
    496  1.69  riastrad 	explicit_memset(efirng, 0, efirng_size);
    497  1.71  riastrad 	fdt_unmap_range(efirng, efirng_size);
    498  1.69  riastrad }
    499  1.69  riastrad 
    500  1.46  jmcneill #ifdef EFI_RUNTIME
    501  1.46  jmcneill static void
    502  1.51  jmcneill fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type)
    503  1.46  jmcneill {
    504  1.46  jmcneill 	int len;
    505  1.46  jmcneill 
    506  1.46  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    507  1.46  jmcneill 	if (chosen_off < 0)
    508  1.46  jmcneill 		return;
    509  1.46  jmcneill 
    510  1.46  jmcneill 	const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
    511  1.46  jmcneill 	if (map == NULL)
    512  1.46  jmcneill 		return;
    513  1.46  jmcneill 
    514  1.46  jmcneill 	while (len >= 24) {
    515  1.46  jmcneill 		const paddr_t pa = be64toh(map[0]);
    516  1.46  jmcneill 		const vaddr_t va = be64toh(map[1]);
    517  1.46  jmcneill 		const uint64_t sz = be64toh(map[2]);
    518  1.51  jmcneill 		VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1);
    519  1.51  jmcneill 		arm_efirt_md_map_range(va, pa, sz, type);
    520  1.46  jmcneill 		map += 3;
    521  1.46  jmcneill 		len -= 24;
    522  1.46  jmcneill 	}
    523  1.46  jmcneill }
    524  1.46  jmcneill #endif
    525  1.46  jmcneill 
    526  1.64     skrll vaddr_t
    527   1.1  jmcneill initarm(void *arg)
    528   1.1  jmcneill {
    529   1.1  jmcneill 	const struct arm_platform *plat;
    530  1.21       ryo 	uint64_t memory_start, memory_end;
    531   1.1  jmcneill 
    532  1.23       ryo 	/* set temporally to work printf()/panic() even before consinit() */
    533  1.23       ryo 	cn_tab = &earlycons;
    534  1.23       ryo 
    535   1.1  jmcneill 	/* Load FDT */
    536   1.1  jmcneill 	int error = fdt_check_header(fdt_addr_r);
    537  1.79     skrll 	if (error != 0)
    538   1.1  jmcneill 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    539  1.79     skrll 
    540  1.79     skrll 	/* If the DTB is too big, try to pack it in place first. */
    541  1.79     skrll 	if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
    542  1.79     skrll 		(void)fdt_pack(__UNCONST(fdt_addr_r));
    543  1.79     skrll 	error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
    544  1.79     skrll 	if (error != 0)
    545  1.79     skrll 		panic("fdt_move failed: %s", fdt_strerror(error));
    546  1.79     skrll 
    547  1.79     skrll 	fdtbus_init(fdt_data);
    548   1.1  jmcneill 
    549   1.1  jmcneill 	/* Lookup platform specific backend */
    550   1.1  jmcneill 	plat = arm_fdt_platform();
    551   1.1  jmcneill 	if (plat == NULL)
    552   1.1  jmcneill 		panic("Kernel does not support this device");
    553   1.1  jmcneill 
    554   1.1  jmcneill 	/* Early console may be available, announce ourselves. */
    555  1.29     skrll 	VPRINTF("FDT<%p>\n", fdt_addr_r);
    556   1.1  jmcneill 
    557   1.6  jmcneill 	const int chosen = OF_finddevice("/chosen");
    558   1.6  jmcneill 	if (chosen >= 0)
    559   1.6  jmcneill 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    560   1.6  jmcneill 	boot_args = bootargs;
    561   1.6  jmcneill 
    562   1.1  jmcneill 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    563  1.29     skrll 	VPRINTF("cpufunc\n");
    564   1.1  jmcneill 	if (set_cpufuncs())
    565   1.1  jmcneill 		panic("cpu not recognized!");
    566   1.1  jmcneill 
    567  1.44     skrll 	/*
    568  1.44     skrll 	 * Memory is still identity/flat mapped this point so using ttbr for
    569  1.44     skrll 	 * l1pt VA is fine
    570  1.44     skrll 	 */
    571  1.44     skrll 
    572  1.78     skrll 	VPRINTF("devmap %p\n", plat->ap_devmap());
    573  1.44     skrll 	extern char ARM_BOOTSTRAP_LxPT[];
    574  1.44     skrll 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap());
    575  1.44     skrll 
    576  1.29     skrll 	VPRINTF("bootstrap\n");
    577  1.32     skrll 	plat->ap_bootstrap();
    578  1.16     skrll 
    579   1.5  jmcneill 	/*
    580   1.5  jmcneill 	 * If stdout-path is specified on the command line, override the
    581   1.5  jmcneill 	 * value in /chosen/stdout-path before initializing console.
    582   1.5  jmcneill 	 */
    583  1.44     skrll 	VPRINTF("stdout\n");
    584   1.5  jmcneill 	fdt_update_stdout_path();
    585   1.5  jmcneill 
    586  1.76       rin #if BYTE_ORDER == BIG_ENDIAN
    587  1.76       rin 	/*
    588  1.76       rin 	 * Most boards are configured to little-endian mode in initial, and
    589  1.76       rin 	 * switched to big-endian mode after kernel is loaded. In this case,
    590  1.76       rin 	 * framebuffer seems byte-swapped to CPU. Override FDT to let
    591  1.76       rin 	 * drivers know.
    592  1.76       rin 	 */
    593  1.76       rin 	VPRINTF("fb_format\n");
    594  1.76       rin 	fdt_update_fb_format();
    595  1.76       rin #endif
    596  1.76       rin 
    597  1.38  jmcneill 	/*
    598  1.38  jmcneill 	 * Done making changes to the FDT.
    599  1.38  jmcneill 	 */
    600  1.38  jmcneill 	fdt_pack(fdt_data);
    601  1.38  jmcneill 
    602  1.29     skrll 	VPRINTF("consinit ");
    603   1.1  jmcneill 	consinit();
    604  1.29     skrll 	VPRINTF("ok\n");
    605   1.1  jmcneill 
    606  1.29     skrll 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
    607   1.1  jmcneill 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    608   1.1  jmcneill 
    609   1.1  jmcneill 	cpu_reset_address = fdt_reset;
    610   1.1  jmcneill 	cpu_powerdown_address = fdt_powerdown;
    611   1.1  jmcneill 	evbarm_device_register = fdt_device_register;
    612  1.39    bouyer 	evbarm_device_register_post_config = fdt_device_register_post_config;
    613  1.34  jmcneill 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
    614   1.1  jmcneill 
    615   1.1  jmcneill 	/* Talk to the user */
    616  1.45     skrll 	printf("NetBSD/evbarm (fdt) booting ...\n");
    617   1.1  jmcneill 
    618   1.1  jmcneill #ifdef BOOT_ARGS
    619   1.1  jmcneill 	char mi_bootargs[] = BOOT_ARGS;
    620   1.1  jmcneill 	parse_mi_bootargs(mi_bootargs);
    621   1.1  jmcneill #endif
    622   1.1  jmcneill 
    623  1.21       ryo 	fdt_get_memory(&memory_start, &memory_end);
    624   1.1  jmcneill 
    625   1.1  jmcneill #if !defined(_LP64)
    626   1.1  jmcneill 	/* Cannot map memory above 4GB */
    627  1.21       ryo 	if (memory_end >= 0x100000000ULL)
    628  1.21       ryo 		memory_end = 0x100000000ULL - PAGE_SIZE;
    629  1.21       ryo 
    630  1.31     skrll #endif
    631  1.21       ryo 	uint64_t memory_size = memory_end - memory_start;
    632   1.1  jmcneill 
    633  1.44     skrll 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
    634  1.44     skrll 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
    635  1.44     skrll 
    636   1.8  jmcneill 	/* Parse ramdisk info */
    637   1.8  jmcneill 	fdt_probe_initrd(&initrd_start, &initrd_end);
    638   1.8  jmcneill 
    639  1.69  riastrad 	/* Parse our on-disk rndseed and the firmware's RNG from EFI */
    640  1.65  riastrad 	fdt_probe_rndseed(&rndseed_start, &rndseed_end);
    641  1.69  riastrad 	fdt_probe_efirng(&efirng_start, &efirng_end);
    642  1.65  riastrad 
    643  1.16     skrll 	/*
    644  1.16     skrll 	 * Populate bootconfig structure for the benefit of
    645  1.16     skrll 	 * dodumpsys
    646  1.16     skrll 	 */
    647  1.44     skrll 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
    648  1.21       ryo 	fdt_build_bootconfig(memory_start, memory_end);
    649  1.21       ryo 
    650  1.46  jmcneill #ifdef EFI_RUNTIME
    651  1.51  jmcneill 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
    652  1.51  jmcneill 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
    653  1.51  jmcneill 	fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
    654  1.46  jmcneill #endif
    655  1.46  jmcneill 
    656  1.31     skrll 	/* Perform PT build and VM init */
    657  1.31     skrll 	cpu_kernel_vm_init(memory_start, memory_size);
    658   1.1  jmcneill 
    659  1.29     skrll 	VPRINTF("bootargs: %s\n", bootargs);
    660   1.1  jmcneill 
    661   1.1  jmcneill 	parse_mi_bootargs(boot_args);
    662   1.1  jmcneill 
    663  1.49  jmcneill 	VPRINTF("Memory regions:\n");
    664  1.49  jmcneill 	fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
    665   1.1  jmcneill 
    666  1.64     skrll 	vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
    667  1.16     skrll 	     nfdt_physmem);
    668  1.44     skrll 
    669  1.59     skrll 	/*
    670  1.60     skrll 	 * initarm_common flushes cache if required before AP start
    671  1.59     skrll 	 */
    672  1.58     skrll 	error = 0;
    673  1.56       ryo 	if ((boothowto & RB_MD1) == 0) {
    674  1.56       ryo 		VPRINTF("mpstart\n");
    675  1.56       ryo 		if (plat->ap_mpstart)
    676  1.58     skrll 			error = plat->ap_mpstart();
    677  1.56       ryo 	}
    678  1.44     skrll 
    679  1.58     skrll 	if (error)
    680  1.58     skrll 		return sp;
    681  1.74     skrll 
    682  1.44     skrll 	/*
    683  1.44     skrll 	 * Now we have APs started the pages used for stacks and L1PT can
    684  1.44     skrll 	 * be given to uvm
    685  1.44     skrll 	 */
    686  1.57     skrll 	extern char const __start__init_memory[];
    687  1.57     skrll 	extern char const __stop__init_memory[] __weak;
    688  1.57     skrll 
    689  1.44     skrll 	if (__start__init_memory != __stop__init_memory) {
    690  1.44     skrll 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
    691  1.44     skrll 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
    692  1.44     skrll 		const paddr_t spg = atop(spa);
    693  1.44     skrll 		const paddr_t epg = atop(epa);
    694  1.44     skrll 
    695  1.63     skrll 		VPRINTF("         start %08lx  end %08lx... "
    696  1.63     skrll 		    "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
    697  1.63     skrll 
    698  1.44     skrll 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
    699  1.44     skrll 
    700  1.44     skrll 	}
    701  1.44     skrll 
    702  1.44     skrll 	return sp;
    703   1.1  jmcneill }
    704   1.1  jmcneill 
    705   1.5  jmcneill static void
    706   1.5  jmcneill fdt_update_stdout_path(void)
    707   1.5  jmcneill {
    708   1.5  jmcneill 	char *stdout_path, *ep;
    709   1.5  jmcneill 	int stdout_path_len;
    710   1.5  jmcneill 	char buf[256];
    711   1.5  jmcneill 
    712   1.5  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    713   1.5  jmcneill 	if (chosen_off == -1)
    714   1.5  jmcneill 		return;
    715   1.5  jmcneill 
    716   1.5  jmcneill 	if (get_bootconf_option(boot_args, "stdout-path",
    717   1.5  jmcneill 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    718   1.5  jmcneill 		return;
    719   1.5  jmcneill 
    720   1.5  jmcneill 	ep = strchr(stdout_path, ' ');
    721   1.5  jmcneill 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    722   1.5  jmcneill 	if (stdout_path_len >= sizeof(buf))
    723   1.5  jmcneill 		return;
    724   1.5  jmcneill 
    725   1.5  jmcneill 	strncpy(buf, stdout_path, stdout_path_len);
    726   1.5  jmcneill 	buf[stdout_path_len] = '\0';
    727   1.5  jmcneill 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    728   1.5  jmcneill 	    buf, stdout_path_len + 1);
    729   1.5  jmcneill }
    730   1.5  jmcneill 
    731   1.1  jmcneill void
    732   1.1  jmcneill consinit(void)
    733   1.1  jmcneill {
    734   1.1  jmcneill 	static bool initialized = false;
    735   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    736   1.1  jmcneill 	const struct fdt_console *cons = fdtbus_get_console();
    737   1.1  jmcneill 	struct fdt_attach_args faa;
    738   1.4  jmcneill 	u_int uart_freq = 0;
    739   1.1  jmcneill 
    740   1.1  jmcneill 	if (initialized || cons == NULL)
    741   1.1  jmcneill 		return;
    742   1.1  jmcneill 
    743  1.32     skrll 	plat->ap_init_attach_args(&faa);
    744   1.1  jmcneill 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    745   1.1  jmcneill 
    746  1.32     skrll 	if (plat->ap_uart_freq != NULL)
    747  1.32     skrll 		uart_freq = plat->ap_uart_freq();
    748   1.4  jmcneill 
    749   1.4  jmcneill 	cons->consinit(&faa, uart_freq);
    750   1.1  jmcneill 
    751   1.1  jmcneill 	initialized = true;
    752   1.1  jmcneill }
    753   1.1  jmcneill 
    754   1.3  jmcneill void
    755  1.65  riastrad cpu_startup_hook(void)
    756  1.65  riastrad {
    757  1.65  riastrad 
    758  1.68     skrll 	fdtbus_intr_init();
    759  1.68     skrll 
    760  1.65  riastrad 	fdt_setup_rndseed();
    761  1.69  riastrad 	fdt_setup_efirng();
    762  1.65  riastrad }
    763  1.65  riastrad 
    764  1.65  riastrad void
    765   1.3  jmcneill delay(u_int us)
    766   1.3  jmcneill {
    767   1.3  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    768   1.3  jmcneill 
    769  1.32     skrll 	plat->ap_delay(us);
    770   1.3  jmcneill }
    771   1.3  jmcneill 
    772   1.1  jmcneill static void
    773  1.34  jmcneill fdt_detect_root_device(device_t dev)
    774  1.34  jmcneill {
    775  1.34  jmcneill 	struct mbr_sector mbr;
    776  1.34  jmcneill 	uint8_t buf[DEV_BSIZE];
    777  1.34  jmcneill 	uint8_t hash[16];
    778  1.34  jmcneill 	const uint8_t *rhash;
    779  1.53  jmcneill 	char rootarg[64];
    780  1.34  jmcneill 	struct vnode *vp;
    781  1.34  jmcneill 	MD5_CTX md5ctx;
    782  1.34  jmcneill 	int error, len;
    783  1.34  jmcneill 	size_t resid;
    784  1.34  jmcneill 	u_int part;
    785  1.34  jmcneill 
    786  1.34  jmcneill 	const int chosen = OF_finddevice("/chosen");
    787  1.34  jmcneill 	if (chosen < 0)
    788  1.34  jmcneill 		return;
    789  1.34  jmcneill 
    790  1.34  jmcneill 	if (of_hasprop(chosen, "netbsd,mbr") &&
    791  1.34  jmcneill 	    of_hasprop(chosen, "netbsd,partition")) {
    792  1.34  jmcneill 
    793  1.34  jmcneill 		/*
    794  1.34  jmcneill 		 * The bootloader has passed in a partition index and MD5 hash
    795  1.34  jmcneill 		 * of the MBR sector. Read the MBR of this device, calculate the
    796  1.34  jmcneill 		 * hash, and compare it with the value passed in.
    797  1.34  jmcneill 		 */
    798  1.34  jmcneill 		rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
    799  1.34  jmcneill 		if (rhash == NULL || len != 16)
    800  1.34  jmcneill 			return;
    801  1.34  jmcneill 		of_getprop_uint32(chosen, "netbsd,partition", &part);
    802  1.34  jmcneill 		if (part >= MAXPARTITIONS)
    803  1.34  jmcneill 			return;
    804  1.34  jmcneill 
    805  1.34  jmcneill 		vp = opendisk(dev);
    806  1.34  jmcneill 		if (!vp)
    807  1.34  jmcneill 			return;
    808  1.34  jmcneill 		error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
    809  1.34  jmcneill 		    0, NOCRED, &resid, NULL);
    810  1.34  jmcneill 		VOP_CLOSE(vp, FREAD, NOCRED);
    811  1.34  jmcneill 		vput(vp);
    812  1.34  jmcneill 
    813  1.34  jmcneill 		if (error != 0)
    814  1.34  jmcneill 			return;
    815  1.34  jmcneill 
    816  1.34  jmcneill 		memcpy(&mbr, buf, sizeof(mbr));
    817  1.34  jmcneill 		MD5Init(&md5ctx);
    818  1.34  jmcneill 		MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
    819  1.34  jmcneill 		MD5Final(hash, &md5ctx);
    820  1.34  jmcneill 
    821  1.34  jmcneill 		if (memcmp(rhash, hash, 16) != 0)
    822  1.34  jmcneill 			return;
    823  1.34  jmcneill 
    824  1.34  jmcneill 		snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a');
    825  1.34  jmcneill 		strcat(boot_args, rootarg);
    826  1.34  jmcneill 	}
    827  1.53  jmcneill 
    828  1.53  jmcneill 	if (of_hasprop(chosen, "netbsd,gpt-guid")) {
    829  1.53  jmcneill 		char guidbuf[UUID_STR_LEN];
    830  1.53  jmcneill 		const struct uuid *guid = fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len);
    831  1.53  jmcneill 		if (guid == NULL || len != 16)
    832  1.53  jmcneill 			return;
    833  1.53  jmcneill 
    834  1.53  jmcneill 		uuid_snprintf(guidbuf, sizeof(guidbuf), guid);
    835  1.53  jmcneill 		snprintf(rootarg, sizeof(rootarg), " root=wedge:%s", guidbuf);
    836  1.53  jmcneill 		strcat(boot_args, rootarg);
    837  1.53  jmcneill 	}
    838  1.53  jmcneill 
    839  1.53  jmcneill 	if (of_hasprop(chosen, "netbsd,gpt-label")) {
    840  1.53  jmcneill 		const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label");
    841  1.53  jmcneill 		if (label == NULL || *label == '\0')
    842  1.53  jmcneill 			return;
    843  1.53  jmcneill 
    844  1.53  jmcneill 		device_t dv = dkwedge_find_by_wname(label);
    845  1.53  jmcneill 		if (dv != NULL)
    846  1.53  jmcneill 			booted_device = dv;
    847  1.53  jmcneill 	}
    848  1.55  jmcneill 
    849  1.55  jmcneill 	if (of_hasprop(chosen, "netbsd,booted-mac-address")) {
    850  1.55  jmcneill 		const uint8_t *macaddr = fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len);
    851  1.55  jmcneill 		if (macaddr == NULL || len != 6)
    852  1.55  jmcneill 			return;
    853  1.55  jmcneill 		int s = pserialize_read_enter();
    854  1.55  jmcneill 		struct ifnet *ifp;
    855  1.55  jmcneill 		IFNET_READER_FOREACH(ifp) {
    856  1.55  jmcneill 			if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) {
    857  1.55  jmcneill 				device_t dv = device_find_by_xname(ifp->if_xname);
    858  1.55  jmcneill 				if (dv != NULL)
    859  1.55  jmcneill 					booted_device = dv;
    860  1.55  jmcneill 				break;
    861  1.55  jmcneill 			}
    862  1.55  jmcneill 		}
    863  1.55  jmcneill 		pserialize_read_exit(s);
    864  1.55  jmcneill 	}
    865  1.34  jmcneill }
    866  1.34  jmcneill 
    867  1.34  jmcneill static void
    868   1.1  jmcneill fdt_device_register(device_t self, void *aux)
    869   1.1  jmcneill {
    870   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    871   1.1  jmcneill 
    872  1.75  jmcneill 	if (device_is_a(self, "armfdt")) {
    873   1.8  jmcneill 		fdt_setup_initrd();
    874   1.8  jmcneill 
    875  1.75  jmcneill #if NWSDISPLAY > 0 && NGENFB > 0
    876  1.75  jmcneill 		/*
    877  1.75  jmcneill 		 * Setup framebuffer console, if present.
    878  1.75  jmcneill 		 */
    879  1.75  jmcneill 		arm_simplefb_preattach();
    880  1.75  jmcneill #endif
    881  1.75  jmcneill 	}
    882  1.75  jmcneill 
    883  1.77  jmcneill #if NWSDISPLAY > 0 && NGENFB > 0
    884  1.77  jmcneill 	if (device_is_a(self, "genfb")) {
    885  1.77  jmcneill 		prop_dictionary_t dict = device_properties(self);
    886  1.77  jmcneill 		prop_dictionary_set_uint64(dict,
    887  1.77  jmcneill 		    "simplefb-physaddr", arm_simplefb_physaddr());
    888  1.77  jmcneill 	}
    889  1.77  jmcneill #endif
    890  1.77  jmcneill 
    891  1.32     skrll 	if (plat && plat->ap_device_register)
    892  1.32     skrll 		plat->ap_device_register(self, aux);
    893   1.1  jmcneill }
    894   1.1  jmcneill 
    895   1.1  jmcneill static void
    896  1.39    bouyer fdt_device_register_post_config(device_t self, void *aux)
    897  1.39    bouyer {
    898  1.39    bouyer #if NUKBD > 0 && NWSDISPLAY > 0
    899  1.39    bouyer 	if (device_is_a(self, "wsdisplay")) {
    900  1.39    bouyer 		struct wsdisplay_softc *sc = device_private(self);
    901  1.39    bouyer 		if (wsdisplay_isconsole(sc))
    902  1.39    bouyer 			ukbd_cnattach();
    903  1.39    bouyer 	}
    904  1.39    bouyer #endif
    905  1.39    bouyer }
    906  1.39    bouyer 
    907  1.39    bouyer static void
    908  1.34  jmcneill fdt_cpu_rootconf(void)
    909  1.34  jmcneill {
    910  1.34  jmcneill 	device_t dev;
    911  1.34  jmcneill 	deviter_t di;
    912  1.34  jmcneill 	char *ptr;
    913  1.34  jmcneill 
    914  1.34  jmcneill 	for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
    915  1.34  jmcneill 		if (device_class(dev) != DV_DISK)
    916  1.34  jmcneill 			continue;
    917  1.34  jmcneill 
    918  1.34  jmcneill 		if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0)
    919  1.34  jmcneill 			break;
    920  1.34  jmcneill 
    921  1.36  jakllsch 		if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd"))
    922  1.34  jmcneill 			fdt_detect_root_device(dev);
    923  1.34  jmcneill 	}
    924  1.34  jmcneill 	deviter_release(&di);
    925  1.34  jmcneill }
    926  1.34  jmcneill 
    927  1.34  jmcneill static void
    928   1.1  jmcneill fdt_reset(void)
    929   1.1  jmcneill {
    930   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    931   1.1  jmcneill 
    932   1.1  jmcneill 	fdtbus_power_reset();
    933   1.1  jmcneill 
    934  1.32     skrll 	if (plat && plat->ap_reset)
    935  1.32     skrll 		plat->ap_reset();
    936   1.1  jmcneill }
    937   1.1  jmcneill 
    938   1.1  jmcneill static void
    939   1.1  jmcneill fdt_powerdown(void)
    940   1.1  jmcneill {
    941   1.1  jmcneill 	fdtbus_power_poweroff();
    942   1.1  jmcneill }
    943  1.76       rin 
    944  1.76       rin #if BYTE_ORDER == BIG_ENDIAN
    945  1.76       rin static void
    946  1.76       rin fdt_update_fb_format(void)
    947  1.76       rin {
    948  1.76       rin 	int off, len;
    949  1.76       rin 	const char *format, *replace;
    950  1.76       rin 
    951  1.76       rin 	off = fdt_path_offset(fdt_data, "/chosen");
    952  1.76       rin 	if (off < 0)
    953  1.76       rin 		return;
    954  1.76       rin 
    955  1.76       rin 	for (;;) {
    956  1.76       rin 		off = fdt_node_offset_by_compatible(fdt_data, off,
    957  1.76       rin 		    "simple-framebuffer");
    958  1.76       rin 		if (off < 0)
    959  1.76       rin 			return;
    960  1.76       rin 
    961  1.76       rin 		format = fdt_getprop(fdt_data, off, "format", &len);
    962  1.76       rin 		if (format == NULL)
    963  1.76       rin 			continue;
    964  1.76       rin 
    965  1.76       rin 		replace = NULL;
    966  1.76       rin 		if (strcmp(format, "a8b8g8r8") == 0)
    967  1.76       rin 			replace = "r8g8b8a8";
    968  1.76       rin 		else if (strcmp(format, "x8r8g8b8") == 0)
    969  1.76       rin 			replace = "b8g8r8x8";
    970  1.76       rin 		if (replace != NULL)
    971  1.76       rin 			fdt_setprop(fdt_data, off, "format", replace,
    972  1.76       rin 			    strlen(replace) + 1);
    973  1.76       rin 	}
    974  1.76       rin }
    975  1.76       rin #endif
    976