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