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