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