Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.45
      1  1.45     skrll /* $NetBSD: fdt_machdep.c,v 1.45 2018/10/18 17:34:10 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.45     skrll __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.45 2018/10/18 17:34:10 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.1  jmcneill 
     40  1.14  jmcneill #include "ukbd.h"
     41  1.39    bouyer #include "wsdisplay.h"
     42  1.14  jmcneill 
     43   1.1  jmcneill #include <sys/param.h>
     44   1.1  jmcneill #include <sys/systm.h>
     45   1.1  jmcneill #include <sys/bus.h>
     46   1.1  jmcneill #include <sys/atomic.h>
     47   1.1  jmcneill #include <sys/cpu.h>
     48   1.1  jmcneill #include <sys/device.h>
     49   1.1  jmcneill #include <sys/exec.h>
     50   1.1  jmcneill #include <sys/kernel.h>
     51   1.1  jmcneill #include <sys/kmem.h>
     52   1.1  jmcneill #include <sys/ksyms.h>
     53   1.1  jmcneill #include <sys/msgbuf.h>
     54   1.1  jmcneill #include <sys/proc.h>
     55   1.1  jmcneill #include <sys/reboot.h>
     56   1.1  jmcneill #include <sys/termios.h>
     57   1.8  jmcneill #include <sys/extent.h>
     58  1.34  jmcneill #include <sys/bootblock.h>
     59  1.34  jmcneill #include <sys/disklabel.h>
     60  1.34  jmcneill #include <sys/vnode.h>
     61  1.34  jmcneill #include <sys/kauth.h>
     62  1.34  jmcneill #include <sys/fcntl.h>
     63  1.34  jmcneill #include <sys/md5.h>
     64   1.1  jmcneill 
     65  1.23       ryo #include <dev/cons.h>
     66   1.1  jmcneill #include <uvm/uvm_extern.h>
     67   1.1  jmcneill 
     68   1.1  jmcneill #include <sys/conf.h>
     69   1.1  jmcneill 
     70   1.1  jmcneill #include <machine/db_machdep.h>
     71   1.1  jmcneill #include <ddb/db_sym.h>
     72   1.1  jmcneill #include <ddb/db_extern.h>
     73   1.1  jmcneill 
     74   1.1  jmcneill #include <machine/bootconfig.h>
     75   1.1  jmcneill #include <arm/armreg.h>
     76   1.1  jmcneill 
     77  1.21       ryo #include <arm/cpufunc.h>
     78   1.1  jmcneill 
     79   1.1  jmcneill #include <evbarm/include/autoconf.h>
     80  1.30     skrll #include <evbarm/fdt/machdep.h>
     81   1.1  jmcneill #include <evbarm/fdt/platform.h>
     82   1.1  jmcneill 
     83   1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     84   1.1  jmcneill 
     85  1.14  jmcneill #if NUKBD > 0
     86  1.14  jmcneill #include <dev/usb/ukbdvar.h>
     87  1.14  jmcneill #endif
     88  1.39    bouyer #if NWSDISPLAY > 0
     89  1.39    bouyer #include <dev/wscons/wsdisplayvar.h>
     90  1.39    bouyer #endif
     91  1.14  jmcneill 
     92   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
     93   1.8  jmcneill #include <dev/md.h>
     94   1.8  jmcneill #endif
     95   1.8  jmcneill 
     96   1.1  jmcneill #ifndef FDT_MAX_BOOT_STRING
     97   1.1  jmcneill #define FDT_MAX_BOOT_STRING 1024
     98   1.1  jmcneill #endif
     99   1.1  jmcneill 
    100   1.1  jmcneill BootConfig bootconfig;
    101   1.1  jmcneill char bootargs[FDT_MAX_BOOT_STRING] = "";
    102   1.1  jmcneill char *boot_args = NULL;
    103  1.26  christos 
    104  1.26  christos /* filled in before cleaning bss. keep in .data */
    105  1.27  christos u_long uboot_args[4] __attribute__((__section__(".data")));
    106  1.28     skrll const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
    107   1.1  jmcneill 
    108   1.8  jmcneill static char fdt_memory_ext_storage[EXTENT_FIXED_STORAGE_SIZE(DRAM_BLOCKS)];
    109   1.8  jmcneill static struct extent *fdt_memory_ext;
    110   1.8  jmcneill 
    111   1.8  jmcneill static uint64_t initrd_start, initrd_end;
    112   1.8  jmcneill 
    113   1.1  jmcneill #include <libfdt.h>
    114   1.1  jmcneill #include <dev/fdt/fdtvar.h>
    115  1.40  jmcneill #define FDT_BUF_SIZE	(512*1024)
    116   1.1  jmcneill static uint8_t fdt_data[FDT_BUF_SIZE];
    117   1.1  jmcneill 
    118   1.1  jmcneill extern char KERNEL_BASE_phys[];
    119   1.1  jmcneill #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
    120   1.1  jmcneill 
    121   1.5  jmcneill static void fdt_update_stdout_path(void);
    122   1.1  jmcneill static void fdt_device_register(device_t, void *);
    123  1.39    bouyer static void fdt_device_register_post_config(device_t, void *);
    124  1.34  jmcneill static void fdt_cpu_rootconf(void);
    125   1.1  jmcneill static void fdt_reset(void);
    126   1.1  jmcneill static void fdt_powerdown(void);
    127   1.1  jmcneill 
    128  1.23       ryo static dev_type_cnputc(earlyconsputc);
    129  1.23       ryo static dev_type_cngetc(earlyconsgetc);
    130  1.23       ryo 
    131  1.23       ryo static struct consdev earlycons = {
    132  1.23       ryo 	.cn_putc = earlyconsputc,
    133  1.23       ryo 	.cn_getc = earlyconsgetc,
    134  1.23       ryo 	.cn_pollc = nullcnpollc,
    135  1.23       ryo };
    136  1.23       ryo 
    137   1.1  jmcneill static void
    138   1.1  jmcneill fdt_putchar(char c)
    139   1.1  jmcneill {
    140  1.42       ryo #ifdef EARLYCONS
    141   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    142  1.32     skrll 	if (plat && plat->ap_early_putchar) {
    143  1.32     skrll 		plat->ap_early_putchar(c);
    144  1.44     skrll 	} else {
    145  1.44     skrll 		uartputc(c);
    146  1.23       ryo 	}
    147  1.23       ryo #endif
    148   1.1  jmcneill }
    149   1.1  jmcneill 
    150   1.1  jmcneill static void
    151  1.23       ryo earlyconsputc(dev_t dev, int c)
    152   1.1  jmcneill {
    153  1.23       ryo 	fdt_putchar(c);
    154   1.1  jmcneill }
    155   1.1  jmcneill 
    156  1.23       ryo static int
    157  1.23       ryo earlyconsgetc(dev_t dev)
    158   1.1  jmcneill {
    159  1.23       ryo 	return 0;	/* XXX */
    160  1.23       ryo }
    161   1.1  jmcneill 
    162  1.23       ryo #ifdef VERBOSE_INIT_ARM
    163  1.29     skrll #define VPRINTF(...)	printf(__VA_ARGS__)
    164   1.1  jmcneill #else
    165  1.43     skrll #define VPRINTF(...)	__nothing
    166   1.1  jmcneill #endif
    167   1.1  jmcneill 
    168   1.7  jmcneill /*
    169  1.21       ryo  * ARM: Get the first physically contiguous region of memory.
    170  1.21       ryo  * ARM64: Get all of physical memory, including holes.
    171   1.7  jmcneill  */
    172   1.7  jmcneill static void
    173  1.21       ryo fdt_get_memory(uint64_t *pstart, uint64_t *pend)
    174   1.7  jmcneill {
    175   1.7  jmcneill 	const int memory = OF_finddevice("/memory");
    176   1.7  jmcneill 	uint64_t cur_addr, cur_size;
    177   1.7  jmcneill 	int index;
    178   1.7  jmcneill 
    179   1.7  jmcneill 	/* Assume the first entry is the start of memory */
    180  1.21       ryo 	if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0)
    181   1.7  jmcneill 		panic("Cannot determine memory size");
    182   1.7  jmcneill 
    183  1.21       ryo 	*pstart = cur_addr;
    184  1.21       ryo 	*pend = cur_addr + cur_size;
    185  1.21       ryo 
    186  1.29     skrll 	VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    187  1.21       ryo 	    0, *pstart, *pend - *pstart);
    188   1.7  jmcneill 
    189   1.7  jmcneill 	for (index = 1;
    190   1.7  jmcneill 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
    191   1.7  jmcneill 	     index++) {
    192  1.29     skrll 		VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    193   1.7  jmcneill 		    index, cur_addr, cur_size);
    194  1.21       ryo 
    195  1.21       ryo #ifdef __aarch64__
    196  1.21       ryo 		if (cur_addr + cur_size > *pend)
    197  1.21       ryo 			*pend = cur_addr + cur_size;
    198  1.21       ryo #else
    199  1.21       ryo 		/* If subsequent entries follow the previous, append them. */
    200  1.21       ryo 		if (*pend == cur_addr)
    201  1.21       ryo 			*pend = cur_addr + cur_size;
    202  1.21       ryo #endif
    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.44     skrll 	uint64_t start = addr;
    210  1.44     skrll 	uint64_t end = addr + size;
    211   1.8  jmcneill 
    212  1.18    martin 	int error = extent_free(fdt_memory_ext, start,
    213  1.18    martin 	     end - start, EX_NOWAIT);
    214   1.8  jmcneill 	if (error != 0)
    215  1.25  christos 		printf("MEM ERROR: res %" PRIx64 "-%" PRIx64 " failed: %d\n",
    216  1.18    martin 		    start, end, error);
    217  1.11  jmcneill 	else
    218  1.29     skrll 		VPRINTF("MEM: res %" PRIx64 "-%" PRIx64 "\n", start, end);
    219   1.8  jmcneill }
    220   1.8  jmcneill 
    221   1.8  jmcneill /*
    222  1.11  jmcneill  * Exclude memory ranges from memory config from the device tree
    223   1.8  jmcneill  */
    224   1.8  jmcneill static void
    225  1.33  jmcneill fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr)
    226   1.8  jmcneill {
    227  1.44     skrll 	uint64_t lstart = 0, lend = 0;
    228   1.8  jmcneill 	uint64_t addr, size;
    229  1.11  jmcneill 	int index, error;
    230   1.8  jmcneill 
    231  1.11  jmcneill 	const int num = fdt_num_mem_rsv(fdtbus_get_data());
    232  1.11  jmcneill 	for (index = 0; index <= num; index++) {
    233  1.11  jmcneill 		error = fdt_get_mem_rsv(fdtbus_get_data(), index,
    234  1.11  jmcneill 		    &addr, &size);
    235  1.44     skrll 		if (error != 0)
    236  1.44     skrll 			continue;
    237  1.44     skrll 		if (lstart <= addr && addr <= lend) {
    238  1.44     skrll 			size -= (lend - addr);
    239  1.44     skrll 			addr = lend;
    240  1.44     skrll 		}
    241  1.44     skrll 		if (size == 0)
    242  1.11  jmcneill 			continue;
    243  1.33  jmcneill 		if (addr + size <= min_addr)
    244  1.33  jmcneill 			continue;
    245   1.8  jmcneill 		if (addr >= max_addr)
    246   1.8  jmcneill 			continue;
    247  1.33  jmcneill 		if (addr < min_addr) {
    248  1.33  jmcneill 			size -= (min_addr - addr);
    249  1.33  jmcneill 			addr = min_addr;
    250  1.33  jmcneill 		}
    251   1.8  jmcneill 		if (addr + size > max_addr)
    252   1.8  jmcneill 			size = max_addr - addr;
    253   1.8  jmcneill 		fdt_add_reserved_memory_range(addr, size);
    254  1.44     skrll 		lstart = addr;
    255  1.44     skrll 		lend = addr + size;
    256   1.8  jmcneill 	}
    257   1.8  jmcneill }
    258   1.8  jmcneill 
    259   1.8  jmcneill /*
    260   1.8  jmcneill  * Define usable memory regions.
    261   1.8  jmcneill  */
    262   1.8  jmcneill static void
    263  1.21       ryo fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
    264   1.8  jmcneill {
    265   1.8  jmcneill 	const int memory = OF_finddevice("/memory");
    266   1.8  jmcneill 	BootConfig *bc = &bootconfig;
    267   1.8  jmcneill 	struct extent_region *er;
    268   1.8  jmcneill 	uint64_t addr, size;
    269  1.11  jmcneill 	int index, error;
    270  1.15     skrll 
    271  1.21       ryo 	fdt_memory_ext = extent_create("FDT Memory", mem_start, mem_end,
    272  1.13  jmcneill 	    fdt_memory_ext_storage, sizeof(fdt_memory_ext_storage), EX_EARLY);
    273   1.8  jmcneill 
    274   1.8  jmcneill 	for (index = 0;
    275   1.8  jmcneill 	     fdtbus_get_reg64(memory, index, &addr, &size) == 0;
    276   1.8  jmcneill 	     index++) {
    277  1.21       ryo 		if (addr >= mem_end || size == 0)
    278   1.8  jmcneill 			continue;
    279  1.21       ryo 		if (addr + size > mem_end)
    280  1.21       ryo 			size = mem_end - addr;
    281   1.8  jmcneill 
    282   1.8  jmcneill 		error = extent_alloc_region(fdt_memory_ext, addr, size,
    283   1.8  jmcneill 		    EX_NOWAIT);
    284   1.8  jmcneill 		if (error != 0)
    285  1.25  christos 			printf("MEM ERROR: add %" PRIx64 "-%" PRIx64 " failed: %d\n",
    286  1.18    martin 			    addr, addr + size, error);
    287  1.29     skrll 		VPRINTF("MEM: add %" PRIx64 "-%" PRIx64 "\n", addr, addr + size);
    288   1.8  jmcneill 	}
    289   1.8  jmcneill 
    290  1.33  jmcneill 	fdt_add_reserved_memory(mem_start, mem_end);
    291   1.8  jmcneill 
    292   1.8  jmcneill 	const uint64_t initrd_size = initrd_end - initrd_start;
    293   1.8  jmcneill 	if (initrd_size > 0)
    294   1.8  jmcneill 		fdt_add_reserved_memory_range(initrd_start, initrd_size);
    295   1.8  jmcneill 
    296  1.29     skrll 	VPRINTF("Usable memory:\n");
    297   1.8  jmcneill 	bc->dramblocks = 0;
    298   1.8  jmcneill 	LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
    299  1.29     skrll 		VPRINTF("  %lx - %lx\n", er->er_start, er->er_end);
    300   1.8  jmcneill 		bc->dram[bc->dramblocks].address = er->er_start;
    301   1.8  jmcneill 		bc->dram[bc->dramblocks].pages =
    302   1.8  jmcneill 		    (er->er_end - er->er_start) / PAGE_SIZE;
    303   1.8  jmcneill 		bc->dramblocks++;
    304   1.8  jmcneill 	}
    305   1.8  jmcneill }
    306   1.8  jmcneill 
    307   1.8  jmcneill static void
    308   1.8  jmcneill fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
    309   1.8  jmcneill {
    310   1.8  jmcneill 	*pstart = *pend = 0;
    311   1.8  jmcneill 
    312   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
    313   1.8  jmcneill 	const int chosen = OF_finddevice("/chosen");
    314   1.8  jmcneill 	if (chosen < 0)
    315   1.8  jmcneill 		return;
    316   1.8  jmcneill 
    317   1.8  jmcneill 	int len;
    318   1.8  jmcneill 	const void *start_data = fdtbus_get_prop(chosen,
    319   1.8  jmcneill 	    "linux,initrd-start", &len);
    320   1.8  jmcneill 	const void *end_data = fdtbus_get_prop(chosen,
    321   1.8  jmcneill 	    "linux,initrd-end", NULL);
    322   1.8  jmcneill 	if (start_data == NULL || end_data == NULL)
    323   1.8  jmcneill 		return;
    324   1.8  jmcneill 
    325   1.8  jmcneill 	switch (len) {
    326   1.8  jmcneill 	case 4:
    327   1.8  jmcneill 		*pstart = be32dec(start_data);
    328   1.8  jmcneill 		*pend = be32dec(end_data);
    329   1.8  jmcneill 		break;
    330   1.8  jmcneill 	case 8:
    331   1.8  jmcneill 		*pstart = be64dec(start_data);
    332   1.8  jmcneill 		*pend = be64dec(end_data);
    333   1.8  jmcneill 		break;
    334   1.8  jmcneill 	default:
    335   1.8  jmcneill 		printf("Unsupported len %d for /chosen/initrd-start\n", len);
    336   1.8  jmcneill 		return;
    337   1.8  jmcneill 	}
    338   1.8  jmcneill #endif
    339   1.8  jmcneill }
    340   1.8  jmcneill 
    341   1.8  jmcneill static void
    342   1.8  jmcneill fdt_setup_initrd(void)
    343   1.8  jmcneill {
    344   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
    345   1.8  jmcneill 	const uint64_t initrd_size = initrd_end - initrd_start;
    346   1.8  jmcneill 	paddr_t startpa = trunc_page(initrd_start);
    347   1.8  jmcneill 	paddr_t endpa = round_page(initrd_end);
    348   1.8  jmcneill 	paddr_t pa;
    349   1.8  jmcneill 	vaddr_t va;
    350   1.8  jmcneill 	void *md_start;
    351   1.8  jmcneill 
    352   1.8  jmcneill 	if (initrd_size == 0)
    353   1.8  jmcneill 		return;
    354   1.8  jmcneill 
    355   1.8  jmcneill 	va = uvm_km_alloc(kernel_map, initrd_size, 0,
    356   1.8  jmcneill 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    357   1.8  jmcneill 	if (va == 0) {
    358   1.8  jmcneill 		printf("Failed to allocate VA for initrd\n");
    359   1.8  jmcneill 		return;
    360   1.8  jmcneill 	}
    361   1.8  jmcneill 
    362   1.8  jmcneill 	md_start = (void *)va;
    363   1.8  jmcneill 
    364   1.8  jmcneill 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
    365   1.8  jmcneill 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
    366   1.8  jmcneill 	pmap_update(pmap_kernel());
    367   1.8  jmcneill 
    368   1.8  jmcneill 	md_root_setconf(md_start, initrd_size);
    369   1.8  jmcneill #endif
    370   1.8  jmcneill }
    371   1.8  jmcneill 
    372  1.21       ryo u_int initarm(void *arg);
    373  1.21       ryo 
    374   1.1  jmcneill u_int
    375   1.1  jmcneill initarm(void *arg)
    376   1.1  jmcneill {
    377   1.1  jmcneill 	const struct arm_platform *plat;
    378  1.21       ryo 	uint64_t memory_start, memory_end;
    379   1.1  jmcneill 
    380  1.23       ryo 	/* set temporally to work printf()/panic() even before consinit() */
    381  1.23       ryo 	cn_tab = &earlycons;
    382  1.23       ryo 
    383   1.1  jmcneill 	/* Load FDT */
    384   1.1  jmcneill 	int error = fdt_check_header(fdt_addr_r);
    385   1.1  jmcneill 	if (error == 0) {
    386  1.41  jmcneill 		/* If the DTB is too big, try to pack it in place first. */
    387  1.41  jmcneill 		if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
    388  1.41  jmcneill 			(void)fdt_pack(__UNCONST(fdt_addr_r));
    389  1.38  jmcneill 		error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
    390   1.1  jmcneill 		if (error != 0)
    391   1.1  jmcneill 			panic("fdt_move failed: %s", fdt_strerror(error));
    392   1.1  jmcneill 		fdtbus_set_data(fdt_data);
    393   1.1  jmcneill 	} else {
    394   1.1  jmcneill 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    395   1.1  jmcneill 	}
    396   1.1  jmcneill 
    397   1.1  jmcneill 	/* Lookup platform specific backend */
    398   1.1  jmcneill 	plat = arm_fdt_platform();
    399   1.1  jmcneill 	if (plat == NULL)
    400   1.1  jmcneill 		panic("Kernel does not support this device");
    401   1.1  jmcneill 
    402   1.1  jmcneill 	/* Early console may be available, announce ourselves. */
    403  1.29     skrll 	VPRINTF("FDT<%p>\n", fdt_addr_r);
    404   1.1  jmcneill 
    405   1.6  jmcneill 	const int chosen = OF_finddevice("/chosen");
    406   1.6  jmcneill 	if (chosen >= 0)
    407   1.6  jmcneill 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    408   1.6  jmcneill 	boot_args = bootargs;
    409   1.6  jmcneill 
    410   1.1  jmcneill 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    411  1.29     skrll 	VPRINTF("cpufunc\n");
    412   1.1  jmcneill 	if (set_cpufuncs())
    413   1.1  jmcneill 		panic("cpu not recognized!");
    414   1.1  jmcneill 
    415  1.44     skrll 	/*
    416  1.44     skrll 	 * Memory is still identity/flat mapped this point so using ttbr for
    417  1.44     skrll 	 * l1pt VA is fine
    418  1.44     skrll 	 */
    419  1.44     skrll 
    420  1.44     skrll 	VPRINTF("devmap\n");
    421  1.44     skrll 	extern char ARM_BOOTSTRAP_LxPT[];
    422  1.44     skrll 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap());
    423  1.44     skrll 
    424  1.29     skrll 	VPRINTF("bootstrap\n");
    425  1.32     skrll 	plat->ap_bootstrap();
    426  1.16     skrll 
    427   1.5  jmcneill 	/*
    428   1.5  jmcneill 	 * If stdout-path is specified on the command line, override the
    429   1.5  jmcneill 	 * value in /chosen/stdout-path before initializing console.
    430   1.5  jmcneill 	 */
    431  1.44     skrll 	VPRINTF("stdout\n");
    432   1.5  jmcneill 	fdt_update_stdout_path();
    433   1.5  jmcneill 
    434  1.38  jmcneill 	/*
    435  1.38  jmcneill 	 * Done making changes to the FDT.
    436  1.38  jmcneill 	 */
    437  1.38  jmcneill 	fdt_pack(fdt_data);
    438  1.38  jmcneill 
    439  1.29     skrll 	VPRINTF("consinit ");
    440   1.1  jmcneill 	consinit();
    441  1.29     skrll 	VPRINTF("ok\n");
    442   1.1  jmcneill 
    443  1.29     skrll 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
    444   1.1  jmcneill 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    445   1.1  jmcneill 
    446   1.1  jmcneill 	cpu_reset_address = fdt_reset;
    447   1.1  jmcneill 	cpu_powerdown_address = fdt_powerdown;
    448   1.1  jmcneill 	evbarm_device_register = fdt_device_register;
    449  1.39    bouyer 	evbarm_device_register_post_config = fdt_device_register_post_config;
    450  1.34  jmcneill 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
    451   1.1  jmcneill 
    452   1.1  jmcneill 	/* Talk to the user */
    453  1.45     skrll 	printf("NetBSD/evbarm (fdt) booting ...\n");
    454   1.1  jmcneill 
    455   1.1  jmcneill #ifdef BOOT_ARGS
    456   1.1  jmcneill 	char mi_bootargs[] = BOOT_ARGS;
    457   1.1  jmcneill 	parse_mi_bootargs(mi_bootargs);
    458   1.1  jmcneill #endif
    459   1.1  jmcneill 
    460  1.21       ryo 	fdt_get_memory(&memory_start, &memory_end);
    461   1.1  jmcneill 
    462   1.1  jmcneill #if !defined(_LP64)
    463   1.1  jmcneill 	/* Cannot map memory above 4GB */
    464  1.21       ryo 	if (memory_end >= 0x100000000ULL)
    465  1.21       ryo 		memory_end = 0x100000000ULL - PAGE_SIZE;
    466  1.21       ryo 
    467  1.31     skrll #endif
    468  1.21       ryo 	uint64_t memory_size = memory_end - memory_start;
    469   1.1  jmcneill 
    470  1.44     skrll 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
    471  1.44     skrll 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
    472  1.44     skrll 
    473   1.8  jmcneill 	/* Parse ramdisk info */
    474   1.8  jmcneill 	fdt_probe_initrd(&initrd_start, &initrd_end);
    475   1.8  jmcneill 
    476  1.16     skrll 	/*
    477  1.16     skrll 	 * Populate bootconfig structure for the benefit of
    478  1.16     skrll 	 * dodumpsys
    479  1.16     skrll 	 */
    480  1.44     skrll 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
    481  1.21       ryo 	fdt_build_bootconfig(memory_start, memory_end);
    482  1.21       ryo 
    483  1.31     skrll 	/* Perform PT build and VM init */
    484  1.31     skrll 	cpu_kernel_vm_init(memory_start, memory_size);
    485   1.1  jmcneill 
    486  1.29     skrll 	VPRINTF("bootargs: %s\n", bootargs);
    487   1.1  jmcneill 
    488   1.1  jmcneill 	parse_mi_bootargs(boot_args);
    489   1.1  jmcneill 
    490  1.35  jmcneill 	#define MAX_PHYSMEM 64
    491  1.16     skrll 	static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
    492  1.16     skrll 	int nfdt_physmem = 0;
    493  1.16     skrll 	struct extent_region *er;
    494  1.16     skrll 
    495  1.44     skrll 	VPRINTF("Memory regions :\n");
    496  1.16     skrll 	LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
    497  1.29     skrll 		VPRINTF("  %lx - %lx\n", er->er_start, er->er_end);
    498  1.16     skrll 		struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
    499  1.16     skrll 
    500  1.17  jmcneill 		KASSERT(nfdt_physmem <= MAX_PHYSMEM);
    501  1.44     skrll 
    502  1.44     skrll 		bp->bp_start = atop(round_page(er->er_start));
    503  1.44     skrll 		bp->bp_pages = atop(trunc_page(er->er_end + 1)) - bp->bp_start;
    504  1.16     skrll 		bp->bp_freelist = VM_FREELIST_DEFAULT;
    505  1.16     skrll 
    506  1.21       ryo #ifdef _LP64
    507  1.21       ryo 		if (er->er_end > 0x100000000)
    508  1.21       ryo 			bp->bp_freelist = VM_FREELIST_HIGHMEM;
    509  1.21       ryo #endif
    510  1.21       ryo 
    511   1.1  jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
    512  1.16     skrll 		if (atop(memory_size) > bp->bp_pages) {
    513  1.16     skrll 			arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
    514  1.16     skrll 			bp->bp_freelist = VM_FREELIST_DIRECTMAP;
    515  1.16     skrll 		}
    516  1.16     skrll #endif
    517   1.1  jmcneill 	}
    518   1.1  jmcneill 
    519  1.44     skrll 	u_int sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
    520  1.16     skrll 	     nfdt_physmem);
    521  1.44     skrll 
    522  1.44     skrll 	VPRINTF("mpstart\n");
    523  1.44     skrll 	if (plat->ap_mpstart)
    524  1.44     skrll 		plat->ap_mpstart();
    525  1.44     skrll 
    526  1.44     skrll 	/*
    527  1.44     skrll 	 * Now we have APs started the pages used for stacks and L1PT can
    528  1.44     skrll 	 * be given to uvm
    529  1.44     skrll 	 */
    530  1.44     skrll 	extern char __start__init_memory[], __stop__init_memory[];
    531  1.44     skrll 	if (__start__init_memory != __stop__init_memory) {
    532  1.44     skrll 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
    533  1.44     skrll 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
    534  1.44     skrll 		const paddr_t spg = atop(spa);
    535  1.44     skrll 		const paddr_t epg = atop(epa);
    536  1.44     skrll 
    537  1.44     skrll 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
    538  1.44     skrll 
    539  1.44     skrll 		VPRINTF("           start %08lx  end %08lx", ptoa(spa), ptoa(epa));
    540  1.44     skrll 	}
    541  1.44     skrll 
    542  1.44     skrll 	return sp;
    543   1.1  jmcneill }
    544   1.1  jmcneill 
    545   1.5  jmcneill static void
    546   1.5  jmcneill fdt_update_stdout_path(void)
    547   1.5  jmcneill {
    548   1.5  jmcneill 	char *stdout_path, *ep;
    549   1.5  jmcneill 	int stdout_path_len;
    550   1.5  jmcneill 	char buf[256];
    551   1.5  jmcneill 
    552   1.5  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    553   1.5  jmcneill 	if (chosen_off == -1)
    554   1.5  jmcneill 		return;
    555   1.5  jmcneill 
    556   1.5  jmcneill 	if (get_bootconf_option(boot_args, "stdout-path",
    557   1.5  jmcneill 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    558   1.5  jmcneill 		return;
    559   1.5  jmcneill 
    560   1.5  jmcneill 	ep = strchr(stdout_path, ' ');
    561   1.5  jmcneill 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    562   1.5  jmcneill 	if (stdout_path_len >= sizeof(buf))
    563   1.5  jmcneill 		return;
    564   1.5  jmcneill 
    565   1.5  jmcneill 	strncpy(buf, stdout_path, stdout_path_len);
    566   1.5  jmcneill 	buf[stdout_path_len] = '\0';
    567   1.5  jmcneill 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    568   1.5  jmcneill 	    buf, stdout_path_len + 1);
    569   1.5  jmcneill }
    570   1.5  jmcneill 
    571   1.1  jmcneill void
    572   1.1  jmcneill consinit(void)
    573   1.1  jmcneill {
    574   1.1  jmcneill 	static bool initialized = false;
    575   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    576   1.1  jmcneill 	const struct fdt_console *cons = fdtbus_get_console();
    577   1.1  jmcneill 	struct fdt_attach_args faa;
    578   1.4  jmcneill 	u_int uart_freq = 0;
    579   1.1  jmcneill 
    580   1.1  jmcneill 	if (initialized || cons == NULL)
    581   1.1  jmcneill 		return;
    582   1.1  jmcneill 
    583  1.32     skrll 	plat->ap_init_attach_args(&faa);
    584   1.1  jmcneill 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    585   1.1  jmcneill 
    586  1.32     skrll 	if (plat->ap_uart_freq != NULL)
    587  1.32     skrll 		uart_freq = plat->ap_uart_freq();
    588   1.4  jmcneill 
    589   1.4  jmcneill 	cons->consinit(&faa, uart_freq);
    590   1.1  jmcneill 
    591   1.1  jmcneill 	initialized = true;
    592   1.1  jmcneill }
    593   1.1  jmcneill 
    594   1.3  jmcneill void
    595   1.3  jmcneill delay(u_int us)
    596   1.3  jmcneill {
    597   1.3  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    598   1.3  jmcneill 
    599  1.32     skrll 	plat->ap_delay(us);
    600   1.3  jmcneill }
    601   1.3  jmcneill 
    602   1.1  jmcneill static void
    603  1.34  jmcneill fdt_detect_root_device(device_t dev)
    604  1.34  jmcneill {
    605  1.34  jmcneill 	struct mbr_sector mbr;
    606  1.34  jmcneill 	uint8_t buf[DEV_BSIZE];
    607  1.34  jmcneill 	uint8_t hash[16];
    608  1.34  jmcneill 	const uint8_t *rhash;
    609  1.34  jmcneill 	char rootarg[32];
    610  1.34  jmcneill 	struct vnode *vp;
    611  1.34  jmcneill 	MD5_CTX md5ctx;
    612  1.34  jmcneill 	int error, len;
    613  1.34  jmcneill 	size_t resid;
    614  1.34  jmcneill 	u_int part;
    615  1.34  jmcneill 
    616  1.34  jmcneill 	const int chosen = OF_finddevice("/chosen");
    617  1.34  jmcneill 	if (chosen < 0)
    618  1.34  jmcneill 		return;
    619  1.34  jmcneill 
    620  1.34  jmcneill 	if (of_hasprop(chosen, "netbsd,mbr") &&
    621  1.34  jmcneill 	    of_hasprop(chosen, "netbsd,partition")) {
    622  1.34  jmcneill 
    623  1.34  jmcneill 		/*
    624  1.34  jmcneill 		 * The bootloader has passed in a partition index and MD5 hash
    625  1.34  jmcneill 		 * of the MBR sector. Read the MBR of this device, calculate the
    626  1.34  jmcneill 		 * hash, and compare it with the value passed in.
    627  1.34  jmcneill 		 */
    628  1.34  jmcneill 		rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len);
    629  1.34  jmcneill 		if (rhash == NULL || len != 16)
    630  1.34  jmcneill 			return;
    631  1.34  jmcneill 		of_getprop_uint32(chosen, "netbsd,partition", &part);
    632  1.34  jmcneill 		if (part >= MAXPARTITIONS)
    633  1.34  jmcneill 			return;
    634  1.34  jmcneill 
    635  1.34  jmcneill 		vp = opendisk(dev);
    636  1.34  jmcneill 		if (!vp)
    637  1.34  jmcneill 			return;
    638  1.34  jmcneill 		error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE,
    639  1.34  jmcneill 		    0, NOCRED, &resid, NULL);
    640  1.34  jmcneill 		VOP_CLOSE(vp, FREAD, NOCRED);
    641  1.34  jmcneill 		vput(vp);
    642  1.34  jmcneill 
    643  1.34  jmcneill 		if (error != 0)
    644  1.34  jmcneill 			return;
    645  1.34  jmcneill 
    646  1.34  jmcneill 		memcpy(&mbr, buf, sizeof(mbr));
    647  1.34  jmcneill 		MD5Init(&md5ctx);
    648  1.34  jmcneill 		MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr));
    649  1.34  jmcneill 		MD5Final(hash, &md5ctx);
    650  1.34  jmcneill 
    651  1.34  jmcneill 		if (memcmp(rhash, hash, 16) != 0)
    652  1.34  jmcneill 			return;
    653  1.34  jmcneill 
    654  1.34  jmcneill 		snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a');
    655  1.34  jmcneill 		strcat(boot_args, rootarg);
    656  1.34  jmcneill 	}
    657  1.34  jmcneill }
    658  1.34  jmcneill 
    659  1.34  jmcneill static void
    660   1.1  jmcneill fdt_device_register(device_t self, void *aux)
    661   1.1  jmcneill {
    662   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    663   1.1  jmcneill 
    664   1.8  jmcneill 	if (device_is_a(self, "armfdt"))
    665   1.8  jmcneill 		fdt_setup_initrd();
    666   1.8  jmcneill 
    667  1.32     skrll 	if (plat && plat->ap_device_register)
    668  1.32     skrll 		plat->ap_device_register(self, aux);
    669   1.1  jmcneill }
    670   1.1  jmcneill 
    671   1.1  jmcneill static void
    672  1.39    bouyer fdt_device_register_post_config(device_t self, void *aux)
    673  1.39    bouyer {
    674  1.39    bouyer #if NUKBD > 0 && NWSDISPLAY > 0
    675  1.39    bouyer 	if (device_is_a(self, "wsdisplay")) {
    676  1.39    bouyer 		struct wsdisplay_softc *sc = device_private(self);
    677  1.39    bouyer 		if (wsdisplay_isconsole(sc))
    678  1.39    bouyer 			ukbd_cnattach();
    679  1.39    bouyer 	}
    680  1.39    bouyer #endif
    681  1.39    bouyer }
    682  1.39    bouyer 
    683  1.39    bouyer static void
    684  1.34  jmcneill fdt_cpu_rootconf(void)
    685  1.34  jmcneill {
    686  1.34  jmcneill 	device_t dev;
    687  1.34  jmcneill 	deviter_t di;
    688  1.34  jmcneill 	char *ptr;
    689  1.34  jmcneill 
    690  1.34  jmcneill 	for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) {
    691  1.34  jmcneill 		if (device_class(dev) != DV_DISK)
    692  1.34  jmcneill 			continue;
    693  1.34  jmcneill 
    694  1.34  jmcneill 		if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0)
    695  1.34  jmcneill 			break;
    696  1.34  jmcneill 
    697  1.36  jakllsch 		if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd"))
    698  1.34  jmcneill 			fdt_detect_root_device(dev);
    699  1.34  jmcneill 	}
    700  1.34  jmcneill 	deviter_release(&di);
    701  1.34  jmcneill }
    702  1.34  jmcneill 
    703  1.34  jmcneill static void
    704   1.1  jmcneill fdt_reset(void)
    705   1.1  jmcneill {
    706   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    707   1.1  jmcneill 
    708   1.1  jmcneill 	fdtbus_power_reset();
    709   1.1  jmcneill 
    710  1.32     skrll 	if (plat && plat->ap_reset)
    711  1.32     skrll 		plat->ap_reset();
    712   1.1  jmcneill }
    713   1.1  jmcneill 
    714   1.1  jmcneill static void
    715   1.1  jmcneill fdt_powerdown(void)
    716   1.1  jmcneill {
    717   1.1  jmcneill 	fdtbus_power_poweroff();
    718   1.1  jmcneill }
    719