Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.17
      1  1.17  jmcneill /* $NetBSD: fdt_machdep.c,v 1.17 2017/12/13 00:22:24 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.17  jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.17 2017/12/13 00:22:24 jmcneill Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include "opt_machdep.h"
     33   1.1  jmcneill #include "opt_ddb.h"
     34   1.1  jmcneill #include "opt_md.h"
     35   1.1  jmcneill #include "opt_arm_debug.h"
     36   1.1  jmcneill #include "opt_multiprocessor.h"
     37   1.1  jmcneill #include "opt_cpuoptions.h"
     38   1.1  jmcneill 
     39  1.14  jmcneill #include "ukbd.h"
     40  1.14  jmcneill 
     41   1.1  jmcneill #include <sys/param.h>
     42   1.1  jmcneill #include <sys/systm.h>
     43   1.1  jmcneill #include <sys/bus.h>
     44   1.1  jmcneill #include <sys/atomic.h>
     45   1.1  jmcneill #include <sys/cpu.h>
     46   1.1  jmcneill #include <sys/device.h>
     47   1.1  jmcneill #include <sys/exec.h>
     48   1.1  jmcneill #include <sys/kernel.h>
     49   1.1  jmcneill #include <sys/kmem.h>
     50   1.1  jmcneill #include <sys/ksyms.h>
     51   1.1  jmcneill #include <sys/msgbuf.h>
     52   1.1  jmcneill #include <sys/proc.h>
     53   1.1  jmcneill #include <sys/reboot.h>
     54   1.1  jmcneill #include <sys/termios.h>
     55   1.8  jmcneill #include <sys/extent.h>
     56   1.1  jmcneill 
     57   1.1  jmcneill #include <uvm/uvm_extern.h>
     58   1.1  jmcneill 
     59   1.1  jmcneill #include <sys/conf.h>
     60   1.1  jmcneill 
     61   1.1  jmcneill #include <machine/db_machdep.h>
     62   1.1  jmcneill #include <ddb/db_sym.h>
     63   1.1  jmcneill #include <ddb/db_extern.h>
     64   1.1  jmcneill 
     65   1.1  jmcneill #include <machine/bootconfig.h>
     66   1.1  jmcneill #include <arm/armreg.h>
     67   1.1  jmcneill #include <arm/undefined.h>
     68   1.1  jmcneill 
     69   1.1  jmcneill #include <arm/arm32/machdep.h>
     70   1.1  jmcneill 
     71   1.1  jmcneill #include <evbarm/include/autoconf.h>
     72   1.1  jmcneill #include <evbarm/fdt/platform.h>
     73   1.1  jmcneill 
     74   1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     75   1.1  jmcneill 
     76  1.14  jmcneill #if NUKBD > 0
     77  1.14  jmcneill #include <dev/usb/ukbdvar.h>
     78  1.14  jmcneill #endif
     79  1.14  jmcneill 
     80   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
     81   1.8  jmcneill #include <dev/md.h>
     82   1.8  jmcneill #endif
     83   1.8  jmcneill 
     84   1.1  jmcneill #ifndef FDT_MAX_BOOT_STRING
     85   1.1  jmcneill #define FDT_MAX_BOOT_STRING 1024
     86   1.1  jmcneill #endif
     87   1.1  jmcneill 
     88   1.1  jmcneill BootConfig bootconfig;
     89   1.1  jmcneill char bootargs[FDT_MAX_BOOT_STRING] = "";
     90   1.1  jmcneill char *boot_args = NULL;
     91   1.1  jmcneill u_int uboot_args[4] = { 0 };	/* filled in by xxx_start.S (not in bss) */
     92   1.1  jmcneill 
     93   1.8  jmcneill static char fdt_memory_ext_storage[EXTENT_FIXED_STORAGE_SIZE(DRAM_BLOCKS)];
     94   1.8  jmcneill static struct extent *fdt_memory_ext;
     95   1.8  jmcneill 
     96   1.8  jmcneill static uint64_t initrd_start, initrd_end;
     97   1.8  jmcneill 
     98   1.1  jmcneill #include <libfdt.h>
     99   1.1  jmcneill #include <dev/fdt/fdtvar.h>
    100   1.1  jmcneill #define FDT_BUF_SIZE	(128*1024)
    101   1.1  jmcneill static uint8_t fdt_data[FDT_BUF_SIZE];
    102   1.1  jmcneill 
    103   1.1  jmcneill extern char KERNEL_BASE_phys[];
    104   1.1  jmcneill #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
    105   1.1  jmcneill 
    106   1.5  jmcneill static void fdt_update_stdout_path(void);
    107   1.1  jmcneill static void fdt_device_register(device_t, void *);
    108   1.1  jmcneill static void fdt_reset(void);
    109   1.1  jmcneill static void fdt_powerdown(void);
    110   1.1  jmcneill 
    111   1.1  jmcneill #ifdef VERBOSE_INIT_ARM
    112   1.1  jmcneill static void
    113   1.1  jmcneill fdt_putchar(char c)
    114   1.1  jmcneill {
    115   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    116   1.2  jmcneill 	if (plat && plat->early_putchar)
    117   1.1  jmcneill 		plat->early_putchar(c);
    118   1.1  jmcneill }
    119   1.1  jmcneill 
    120   1.1  jmcneill static void
    121   1.1  jmcneill fdt_putstr(const char *s)
    122   1.1  jmcneill {
    123   1.1  jmcneill 	for (const char *p = s; *p; p++)
    124   1.1  jmcneill 		fdt_putchar(*p);
    125   1.1  jmcneill }
    126   1.1  jmcneill 
    127   1.1  jmcneill static void
    128   1.1  jmcneill fdt_printn(u_int n, int base)
    129   1.1  jmcneill {
    130   1.1  jmcneill 	char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
    131   1.1  jmcneill 
    132   1.1  jmcneill 	p = buf;
    133   1.1  jmcneill 	do {
    134   1.1  jmcneill 		*p++ = hexdigits[n % base];
    135   1.1  jmcneill 	} while (n /= base);
    136   1.1  jmcneill 
    137   1.1  jmcneill 	do {
    138   1.1  jmcneill 		fdt_putchar(*--p);
    139   1.1  jmcneill 	} while (p > buf);
    140   1.1  jmcneill }
    141   1.1  jmcneill #define DPRINTF(...)		printf(__VA_ARGS__)
    142   1.1  jmcneill #define DPRINT(x)		fdt_putstr(x)
    143   1.1  jmcneill #define DPRINTN(x,b)		fdt_printn((x), (b))
    144   1.1  jmcneill #else
    145   1.1  jmcneill #define DPRINTF(...)
    146   1.1  jmcneill #define DPRINT(x)
    147   1.1  jmcneill #define DPRINTN(x,b)
    148   1.1  jmcneill #endif
    149   1.1  jmcneill 
    150   1.7  jmcneill /*
    151   1.7  jmcneill  * Get the first physically contiguous region of memory.
    152   1.7  jmcneill  */
    153   1.7  jmcneill static void
    154   1.7  jmcneill fdt_get_memory(uint64_t *paddr, uint64_t *psize)
    155   1.7  jmcneill {
    156   1.7  jmcneill 	const int memory = OF_finddevice("/memory");
    157   1.7  jmcneill 	uint64_t cur_addr, cur_size;
    158   1.7  jmcneill 	int index;
    159   1.7  jmcneill 
    160   1.7  jmcneill 	/* Assume the first entry is the start of memory */
    161   1.7  jmcneill 	if (fdtbus_get_reg64(memory, 0, paddr, psize) != 0)
    162   1.7  jmcneill 		panic("Cannot determine memory size");
    163   1.7  jmcneill 
    164   1.7  jmcneill 	DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    165   1.7  jmcneill 	    0, *paddr, *psize);
    166   1.7  jmcneill 
    167   1.7  jmcneill 	/* If subsequent entries follow the previous one, append them. */
    168   1.7  jmcneill 	for (index = 1;
    169   1.7  jmcneill 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
    170   1.7  jmcneill 	     index++) {
    171   1.7  jmcneill 		DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    172   1.7  jmcneill 		    index, cur_addr, cur_size);
    173   1.7  jmcneill 		if (*paddr + *psize == cur_addr)
    174   1.7  jmcneill 			*psize += cur_size;
    175   1.7  jmcneill 	}
    176   1.7  jmcneill }
    177   1.7  jmcneill 
    178  1.16     skrll void
    179   1.8  jmcneill fdt_add_reserved_memory_range(uint64_t addr, uint64_t size)
    180   1.8  jmcneill {
    181   1.8  jmcneill 	int error;
    182   1.8  jmcneill 
    183  1.11  jmcneill 	addr = trunc_page(addr);
    184  1.11  jmcneill 	size = round_page(size);
    185  1.11  jmcneill 
    186   1.8  jmcneill 	error = extent_free(fdt_memory_ext, addr, size, EX_NOWAIT);
    187   1.8  jmcneill 	if (error != 0)
    188  1.11  jmcneill 		printf("MEM ERROR: res %llx-%llx failed: %d\n",
    189  1.11  jmcneill 		    addr, addr + size, error);
    190  1.11  jmcneill 	else
    191  1.11  jmcneill 		DPRINTF("MEM: res %llx-%llx\n", addr, addr + size);
    192   1.8  jmcneill }
    193   1.8  jmcneill 
    194   1.8  jmcneill /*
    195  1.11  jmcneill  * Exclude memory ranges from memory config from the device tree
    196   1.8  jmcneill  */
    197   1.8  jmcneill static void
    198  1.11  jmcneill fdt_add_reserved_memory(uint64_t max_addr)
    199   1.8  jmcneill {
    200   1.8  jmcneill 	uint64_t addr, size;
    201  1.11  jmcneill 	int index, error;
    202   1.8  jmcneill 
    203  1.11  jmcneill 	const int num = fdt_num_mem_rsv(fdtbus_get_data());
    204  1.11  jmcneill 	for (index = 0; index <= num; index++) {
    205  1.11  jmcneill 		error = fdt_get_mem_rsv(fdtbus_get_data(), index,
    206  1.11  jmcneill 		    &addr, &size);
    207  1.11  jmcneill 		if (error != 0 || size == 0)
    208  1.11  jmcneill 			continue;
    209   1.8  jmcneill 		if (addr >= max_addr)
    210   1.8  jmcneill 			continue;
    211   1.8  jmcneill 		if (addr + size > max_addr)
    212   1.8  jmcneill 			size = max_addr - addr;
    213   1.8  jmcneill 		fdt_add_reserved_memory_range(addr, size);
    214   1.8  jmcneill 	}
    215   1.8  jmcneill }
    216   1.8  jmcneill 
    217   1.8  jmcneill /*
    218   1.8  jmcneill  * Define usable memory regions.
    219   1.8  jmcneill  */
    220   1.8  jmcneill static void
    221   1.8  jmcneill fdt_build_bootconfig(uint64_t mem_addr, uint64_t mem_size)
    222   1.8  jmcneill {
    223   1.8  jmcneill 	const int memory = OF_finddevice("/memory");
    224   1.8  jmcneill 	const uint64_t max_addr = mem_addr + mem_size;
    225   1.8  jmcneill 	BootConfig *bc = &bootconfig;
    226   1.8  jmcneill 	struct extent_region *er;
    227   1.8  jmcneill 	uint64_t addr, size;
    228  1.11  jmcneill 	int index, error;
    229  1.15     skrll 
    230   1.8  jmcneill 	fdt_memory_ext = extent_create("FDT Memory", mem_addr, max_addr,
    231  1.13  jmcneill 	    fdt_memory_ext_storage, sizeof(fdt_memory_ext_storage), EX_EARLY);
    232   1.8  jmcneill 
    233   1.8  jmcneill 	for (index = 0;
    234   1.8  jmcneill 	     fdtbus_get_reg64(memory, index, &addr, &size) == 0;
    235   1.8  jmcneill 	     index++) {
    236  1.12  jakllsch 		if (addr >= max_addr || size == 0)
    237   1.8  jmcneill 			continue;
    238   1.8  jmcneill 		if (addr + size > max_addr)
    239   1.8  jmcneill 			size = max_addr - addr;
    240   1.8  jmcneill 
    241   1.8  jmcneill 		error = extent_alloc_region(fdt_memory_ext, addr, size,
    242   1.8  jmcneill 		    EX_NOWAIT);
    243   1.8  jmcneill 		if (error != 0)
    244   1.8  jmcneill 			printf("MEM ERROR: add %llx-%llx failed: %d\n",
    245   1.8  jmcneill 			    addr, size, error);
    246   1.8  jmcneill 		DPRINTF("MEM: add %llx-%llx\n", addr, size);
    247   1.8  jmcneill 	}
    248   1.8  jmcneill 
    249  1.11  jmcneill 	fdt_add_reserved_memory(max_addr);
    250   1.8  jmcneill 
    251   1.8  jmcneill 	const uint64_t initrd_size = initrd_end - initrd_start;
    252   1.8  jmcneill 	if (initrd_size > 0)
    253   1.8  jmcneill 		fdt_add_reserved_memory_range(initrd_start, initrd_size);
    254   1.8  jmcneill 
    255   1.8  jmcneill 	DPRINTF("Usable memory:\n");
    256   1.8  jmcneill 	bc->dramblocks = 0;
    257   1.8  jmcneill 	LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
    258   1.8  jmcneill 		DPRINTF("  %lx - %lx\n", er->er_start, er->er_end);
    259   1.8  jmcneill 		bc->dram[bc->dramblocks].address = er->er_start;
    260   1.8  jmcneill 		bc->dram[bc->dramblocks].pages =
    261   1.8  jmcneill 		    (er->er_end - er->er_start) / PAGE_SIZE;
    262   1.8  jmcneill 		bc->dramblocks++;
    263   1.8  jmcneill 	}
    264   1.8  jmcneill }
    265   1.8  jmcneill 
    266   1.8  jmcneill static void
    267   1.8  jmcneill fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
    268   1.8  jmcneill {
    269   1.8  jmcneill 	*pstart = *pend = 0;
    270   1.8  jmcneill 
    271   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
    272   1.8  jmcneill 	const int chosen = OF_finddevice("/chosen");
    273   1.8  jmcneill 	if (chosen < 0)
    274   1.8  jmcneill 		return;
    275   1.8  jmcneill 
    276   1.8  jmcneill 	int len;
    277   1.8  jmcneill 	const void *start_data = fdtbus_get_prop(chosen,
    278   1.8  jmcneill 	    "linux,initrd-start", &len);
    279   1.8  jmcneill 	const void *end_data = fdtbus_get_prop(chosen,
    280   1.8  jmcneill 	    "linux,initrd-end", NULL);
    281   1.8  jmcneill 	if (start_data == NULL || end_data == NULL)
    282   1.8  jmcneill 		return;
    283   1.8  jmcneill 
    284   1.8  jmcneill 	switch (len) {
    285   1.8  jmcneill 	case 4:
    286   1.8  jmcneill 		*pstart = be32dec(start_data);
    287   1.8  jmcneill 		*pend = be32dec(end_data);
    288   1.8  jmcneill 		break;
    289   1.8  jmcneill 	case 8:
    290   1.8  jmcneill 		*pstart = be64dec(start_data);
    291   1.8  jmcneill 		*pend = be64dec(end_data);
    292   1.8  jmcneill 		break;
    293   1.8  jmcneill 	default:
    294   1.8  jmcneill 		printf("Unsupported len %d for /chosen/initrd-start\n", len);
    295   1.8  jmcneill 		return;
    296   1.8  jmcneill 	}
    297   1.8  jmcneill #endif
    298   1.8  jmcneill }
    299   1.8  jmcneill 
    300   1.8  jmcneill static void
    301   1.8  jmcneill fdt_setup_initrd(void)
    302   1.8  jmcneill {
    303   1.8  jmcneill #ifdef MEMORY_DISK_DYNAMIC
    304   1.8  jmcneill 	const uint64_t initrd_size = initrd_end - initrd_start;
    305   1.8  jmcneill 	paddr_t startpa = trunc_page(initrd_start);
    306   1.8  jmcneill 	paddr_t endpa = round_page(initrd_end);
    307   1.8  jmcneill 	paddr_t pa;
    308   1.8  jmcneill 	vaddr_t va;
    309   1.8  jmcneill 	void *md_start;
    310   1.8  jmcneill 
    311   1.8  jmcneill 	if (initrd_size == 0)
    312   1.8  jmcneill 		return;
    313   1.8  jmcneill 
    314   1.8  jmcneill 	va = uvm_km_alloc(kernel_map, initrd_size, 0,
    315   1.8  jmcneill 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    316   1.8  jmcneill 	if (va == 0) {
    317   1.8  jmcneill 		printf("Failed to allocate VA for initrd\n");
    318   1.8  jmcneill 		return;
    319   1.8  jmcneill 	}
    320   1.8  jmcneill 
    321   1.8  jmcneill 	md_start = (void *)va;
    322   1.8  jmcneill 
    323   1.8  jmcneill 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
    324   1.8  jmcneill 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
    325   1.8  jmcneill 	pmap_update(pmap_kernel());
    326   1.8  jmcneill 
    327   1.8  jmcneill 	md_root_setconf(md_start, initrd_size);
    328   1.8  jmcneill #endif
    329   1.8  jmcneill }
    330   1.8  jmcneill 
    331   1.1  jmcneill u_int
    332   1.1  jmcneill initarm(void *arg)
    333   1.1  jmcneill {
    334   1.1  jmcneill 	const struct arm_platform *plat;
    335   1.1  jmcneill 	uint64_t memory_addr, memory_size;
    336   1.1  jmcneill 
    337   1.1  jmcneill 	/* Load FDT */
    338   1.1  jmcneill 	const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
    339   1.1  jmcneill 	int error = fdt_check_header(fdt_addr_r);
    340   1.1  jmcneill 	if (error == 0) {
    341   1.1  jmcneill 		error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
    342   1.1  jmcneill 		if (error != 0)
    343   1.1  jmcneill 			panic("fdt_move failed: %s", fdt_strerror(error));
    344   1.1  jmcneill 		fdtbus_set_data(fdt_data);
    345   1.1  jmcneill 	} else {
    346   1.1  jmcneill 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    347   1.1  jmcneill 	}
    348   1.1  jmcneill 
    349   1.1  jmcneill 	/* Lookup platform specific backend */
    350   1.1  jmcneill 	plat = arm_fdt_platform();
    351   1.1  jmcneill 	if (plat == NULL)
    352   1.1  jmcneill 		panic("Kernel does not support this device");
    353   1.1  jmcneill 
    354   1.1  jmcneill 	/* Early console may be available, announce ourselves. */
    355   1.2  jmcneill 	DPRINT("FDT<");
    356   1.2  jmcneill 	DPRINTN((uintptr_t)fdt_addr_r, 16);
    357   1.2  jmcneill 	DPRINT(">");
    358   1.1  jmcneill 
    359   1.6  jmcneill 	const int chosen = OF_finddevice("/chosen");
    360   1.6  jmcneill 	if (chosen >= 0)
    361   1.6  jmcneill 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    362   1.6  jmcneill 	boot_args = bootargs;
    363   1.6  jmcneill 
    364   1.1  jmcneill 	DPRINT(" devmap");
    365   1.1  jmcneill 	pmap_devmap_register(plat->devmap());
    366   1.1  jmcneill 
    367   1.1  jmcneill 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    368   1.1  jmcneill 	DPRINT(" cpufunc");
    369   1.1  jmcneill 	if (set_cpufuncs())
    370   1.1  jmcneill 		panic("cpu not recognized!");
    371   1.1  jmcneill 
    372  1.16     skrll 	DPRINT(" bootstrap");
    373  1.16     skrll 	plat->bootstrap();
    374  1.16     skrll 
    375   1.5  jmcneill 	/*
    376   1.5  jmcneill 	 * If stdout-path is specified on the command line, override the
    377   1.5  jmcneill 	 * value in /chosen/stdout-path before initializing console.
    378   1.5  jmcneill 	 */
    379   1.5  jmcneill 	fdt_update_stdout_path();
    380   1.5  jmcneill 
    381   1.1  jmcneill 	DPRINT(" consinit");
    382   1.1  jmcneill 	consinit();
    383   1.1  jmcneill 
    384   1.1  jmcneill 	DPRINTF(" ok\n");
    385   1.1  jmcneill 
    386   1.1  jmcneill 	DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
    387   1.1  jmcneill 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    388   1.1  jmcneill 
    389   1.1  jmcneill 	cpu_reset_address = fdt_reset;
    390   1.1  jmcneill 	cpu_powerdown_address = fdt_powerdown;
    391   1.1  jmcneill 	evbarm_device_register = fdt_device_register;
    392   1.1  jmcneill 
    393   1.1  jmcneill 	/* Talk to the user */
    394   1.1  jmcneill 	DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
    395   1.1  jmcneill 
    396   1.1  jmcneill #ifdef BOOT_ARGS
    397   1.1  jmcneill 	char mi_bootargs[] = BOOT_ARGS;
    398   1.1  jmcneill 	parse_mi_bootargs(mi_bootargs);
    399   1.1  jmcneill #endif
    400   1.1  jmcneill 
    401   1.1  jmcneill 	DPRINTF("KERNEL_BASE=0x%x, "
    402   1.1  jmcneill 		"KERNEL_VM_BASE=0x%x, "
    403   1.1  jmcneill 		"KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
    404   1.1  jmcneill 		"KERNEL_BASE_VOFFSET=0x%x\n",
    405   1.1  jmcneill 		KERNEL_BASE,
    406   1.1  jmcneill 		KERNEL_VM_BASE,
    407   1.1  jmcneill 		KERNEL_VM_BASE - KERNEL_BASE,
    408   1.1  jmcneill 		KERNEL_BASE_VOFFSET);
    409   1.1  jmcneill 
    410   1.7  jmcneill 	fdt_get_memory(&memory_addr, &memory_size);
    411   1.1  jmcneill 
    412   1.1  jmcneill #if !defined(_LP64)
    413   1.1  jmcneill 	/* Cannot map memory above 4GB */
    414   1.9       chs 	if (memory_addr + memory_size >= 0x100000000)
    415   1.9       chs 		memory_size = 0x100000000 - memory_addr - PAGE_SIZE;
    416   1.1  jmcneill #endif
    417   1.1  jmcneill 
    418   1.1  jmcneill #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
    419   1.1  jmcneill 	const bool mapallmem_p = true;
    420   1.1  jmcneill #ifndef PMAP_NEED_ALLOC_POOLPAGE
    421  1.11  jmcneill 	if (memory_size > KERNEL_VM_BASE - KERNEL_BASE) {
    422   1.1  jmcneill 		DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
    423  1.15     skrll 		    __func__, (unsigned long) (memory_size >> 20),
    424   1.1  jmcneill 		    (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
    425  1.11  jmcneill 		memory_size = KERNEL_VM_BASE - KERNEL_BASE;
    426   1.1  jmcneill 	}
    427   1.1  jmcneill #endif
    428   1.1  jmcneill #else
    429   1.1  jmcneill 	const bool mapallmem_p = false;
    430   1.1  jmcneill #endif
    431   1.1  jmcneill 
    432   1.8  jmcneill 	/* Parse ramdisk info */
    433   1.8  jmcneill 	fdt_probe_initrd(&initrd_start, &initrd_end);
    434   1.8  jmcneill 
    435  1.16     skrll 	/*
    436  1.16     skrll 	 * Populate bootconfig structure for the benefit of
    437  1.16     skrll 	 * dodumpsys
    438  1.16     skrll 	 */
    439   1.8  jmcneill 	fdt_build_bootconfig(memory_addr, memory_size);
    440   1.1  jmcneill 
    441  1.16     skrll 	arm32_bootmem_init(memory_addr, memory_size, KERNEL_BASE_PHYS);
    442   1.1  jmcneill 	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
    443   1.1  jmcneill 	    plat->devmap(), mapallmem_p);
    444   1.1  jmcneill 
    445   1.1  jmcneill 	DPRINTF("bootargs: %s\n", bootargs);
    446   1.1  jmcneill 
    447   1.1  jmcneill 	parse_mi_bootargs(boot_args);
    448   1.1  jmcneill 
    449  1.17  jmcneill 	#define MAX_PHYSMEM 16
    450  1.16     skrll 	static struct boot_physmem fdt_physmem[MAX_PHYSMEM];
    451  1.16     skrll 	int nfdt_physmem = 0;
    452  1.16     skrll 	struct extent_region *er;
    453  1.16     skrll 
    454  1.16     skrll 	LIST_FOREACH(er, &fdt_memory_ext->ex_regions, er_link) {
    455  1.16     skrll 		DPRINTF("  %lx - %lx\n", er->er_start, er->er_end);
    456  1.16     skrll 		struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
    457  1.16     skrll 
    458  1.17  jmcneill 		KASSERT(nfdt_physmem <= MAX_PHYSMEM);
    459  1.16     skrll 		bp->bp_start = atop(er->er_start);
    460  1.16     skrll 		bp->bp_pages = atop(er->er_end - er->er_start);
    461  1.16     skrll 		bp->bp_freelist = VM_FREELIST_DEFAULT;
    462  1.16     skrll 
    463   1.1  jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
    464  1.16     skrll 		if (atop(memory_size) > bp->bp_pages) {
    465  1.16     skrll 			arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
    466  1.16     skrll 			bp->bp_freelist = VM_FREELIST_DIRECTMAP;
    467  1.16     skrll 		}
    468  1.16     skrll #endif
    469   1.1  jmcneill 	}
    470   1.1  jmcneill 
    471  1.16     skrll 	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
    472  1.16     skrll 	     nfdt_physmem);
    473   1.1  jmcneill }
    474   1.1  jmcneill 
    475   1.5  jmcneill static void
    476   1.5  jmcneill fdt_update_stdout_path(void)
    477   1.5  jmcneill {
    478   1.5  jmcneill 	char *stdout_path, *ep;
    479   1.5  jmcneill 	int stdout_path_len;
    480   1.5  jmcneill 	char buf[256];
    481   1.5  jmcneill 
    482   1.5  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    483   1.5  jmcneill 	if (chosen_off == -1)
    484   1.5  jmcneill 		return;
    485   1.5  jmcneill 
    486   1.5  jmcneill 	if (get_bootconf_option(boot_args, "stdout-path",
    487   1.5  jmcneill 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    488   1.5  jmcneill 		return;
    489   1.5  jmcneill 
    490   1.5  jmcneill 	ep = strchr(stdout_path, ' ');
    491   1.5  jmcneill 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    492   1.5  jmcneill 	if (stdout_path_len >= sizeof(buf))
    493   1.5  jmcneill 		return;
    494   1.5  jmcneill 
    495   1.5  jmcneill 	strncpy(buf, stdout_path, stdout_path_len);
    496   1.5  jmcneill 	buf[stdout_path_len] = '\0';
    497   1.5  jmcneill 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    498   1.5  jmcneill 	    buf, stdout_path_len + 1);
    499   1.5  jmcneill }
    500   1.5  jmcneill 
    501   1.1  jmcneill void
    502   1.1  jmcneill consinit(void)
    503   1.1  jmcneill {
    504   1.1  jmcneill 	static bool initialized = false;
    505   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    506   1.1  jmcneill 	const struct fdt_console *cons = fdtbus_get_console();
    507   1.1  jmcneill 	struct fdt_attach_args faa;
    508   1.4  jmcneill 	u_int uart_freq = 0;
    509   1.1  jmcneill 
    510   1.1  jmcneill 	if (initialized || cons == NULL)
    511   1.1  jmcneill 		return;
    512   1.1  jmcneill 
    513   1.1  jmcneill 	plat->init_attach_args(&faa);
    514   1.1  jmcneill 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    515   1.1  jmcneill 
    516   1.4  jmcneill 	if (plat->uart_freq != NULL)
    517   1.4  jmcneill 		uart_freq = plat->uart_freq();
    518   1.4  jmcneill 
    519   1.4  jmcneill 	cons->consinit(&faa, uart_freq);
    520   1.1  jmcneill 
    521  1.14  jmcneill #if NUKBD > 0
    522  1.14  jmcneill 	ukbd_cnattach();	/* allow USB keyboard to become console */
    523  1.14  jmcneill #endif
    524  1.14  jmcneill 
    525   1.1  jmcneill 	initialized = true;
    526   1.1  jmcneill }
    527   1.1  jmcneill 
    528   1.3  jmcneill void
    529   1.3  jmcneill delay(u_int us)
    530   1.3  jmcneill {
    531   1.3  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    532   1.3  jmcneill 
    533   1.3  jmcneill 	plat->delay(us);
    534   1.3  jmcneill }
    535   1.3  jmcneill 
    536   1.1  jmcneill static void
    537   1.1  jmcneill fdt_device_register(device_t self, void *aux)
    538   1.1  jmcneill {
    539   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    540   1.1  jmcneill 
    541   1.8  jmcneill 	if (device_is_a(self, "armfdt"))
    542   1.8  jmcneill 		fdt_setup_initrd();
    543   1.8  jmcneill 
    544   1.1  jmcneill 	if (plat && plat->device_register)
    545   1.1  jmcneill 		plat->device_register(self, aux);
    546   1.1  jmcneill }
    547   1.1  jmcneill 
    548   1.1  jmcneill static void
    549   1.1  jmcneill fdt_reset(void)
    550   1.1  jmcneill {
    551   1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    552   1.1  jmcneill 
    553   1.1  jmcneill 	fdtbus_power_reset();
    554   1.1  jmcneill 
    555   1.1  jmcneill 	if (plat && plat->reset)
    556   1.1  jmcneill 		plat->reset();
    557   1.1  jmcneill }
    558   1.1  jmcneill 
    559   1.1  jmcneill static void
    560   1.1  jmcneill fdt_powerdown(void)
    561   1.1  jmcneill {
    562   1.1  jmcneill 	fdtbus_power_poweroff();
    563   1.1  jmcneill }
    564