Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.7
      1 /* $NetBSD: fdt_machdep.c,v 1.7 2017/06/11 20:25:07 jmcneill 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.7 2017/06/11 20:25:07 jmcneill 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 <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/bus.h>
     42 #include <sys/atomic.h>
     43 #include <sys/cpu.h>
     44 #include <sys/device.h>
     45 #include <sys/exec.h>
     46 #include <sys/kernel.h>
     47 #include <sys/kmem.h>
     48 #include <sys/ksyms.h>
     49 #include <sys/msgbuf.h>
     50 #include <sys/proc.h>
     51 #include <sys/reboot.h>
     52 #include <sys/termios.h>
     53 
     54 #include <uvm/uvm_extern.h>
     55 
     56 #include <sys/conf.h>
     57 
     58 #include <machine/db_machdep.h>
     59 #include <ddb/db_sym.h>
     60 #include <ddb/db_extern.h>
     61 
     62 #include <machine/bootconfig.h>
     63 #include <arm/armreg.h>
     64 #include <arm/undefined.h>
     65 
     66 #include <arm/arm32/machdep.h>
     67 
     68 #include <evbarm/include/autoconf.h>
     69 #include <evbarm/fdt/platform.h>
     70 
     71 #include <arm/fdt/arm_fdtvar.h>
     72 
     73 #ifndef FDT_MAX_BOOT_STRING
     74 #define FDT_MAX_BOOT_STRING 1024
     75 #endif
     76 
     77 BootConfig bootconfig;
     78 char bootargs[FDT_MAX_BOOT_STRING] = "";
     79 char *boot_args = NULL;
     80 u_int uboot_args[4] = { 0 };	/* filled in by xxx_start.S (not in bss) */
     81 
     82 #include <libfdt.h>
     83 #include <dev/fdt/fdtvar.h>
     84 #define FDT_BUF_SIZE	(128*1024)
     85 static uint8_t fdt_data[FDT_BUF_SIZE];
     86 
     87 extern char KERNEL_BASE_phys[];
     88 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
     89 
     90 static void fdt_update_stdout_path(void);
     91 static void fdt_device_register(device_t, void *);
     92 static void fdt_reset(void);
     93 static void fdt_powerdown(void);
     94 
     95 #ifdef PMAP_NEED_ALLOC_POOLPAGE
     96 static struct boot_physmem bp_lowgig = {
     97 	.bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG,
     98 	.bp_freelist = VM_FREELIST_ISADMA,
     99 	.bp_flags = 0
    100 };
    101 #endif
    102 
    103 #ifdef VERBOSE_INIT_ARM
    104 static void
    105 fdt_putchar(char c)
    106 {
    107 	const struct arm_platform *plat = arm_fdt_platform();
    108 	if (plat && plat->early_putchar)
    109 		plat->early_putchar(c);
    110 }
    111 
    112 static void
    113 fdt_putstr(const char *s)
    114 {
    115 	for (const char *p = s; *p; p++)
    116 		fdt_putchar(*p);
    117 }
    118 
    119 static void
    120 fdt_printn(u_int n, int base)
    121 {
    122 	char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
    123 
    124 	p = buf;
    125 	do {
    126 		*p++ = hexdigits[n % base];
    127 	} while (n /= base);
    128 
    129 	do {
    130 		fdt_putchar(*--p);
    131 	} while (p > buf);
    132 }
    133 #define DPRINTF(...)		printf(__VA_ARGS__)
    134 #define DPRINT(x)		fdt_putstr(x)
    135 #define DPRINTN(x,b)		fdt_printn((x), (b))
    136 #else
    137 #define DPRINTF(...)
    138 #define DPRINT(x)
    139 #define DPRINTN(x,b)
    140 #endif
    141 
    142 /*
    143  * Get the first physically contiguous region of memory.
    144  */
    145 static void
    146 fdt_get_memory(uint64_t *paddr, uint64_t *psize)
    147 {
    148 	const int memory = OF_finddevice("/memory");
    149 	uint64_t cur_addr, cur_size;
    150 	int index;
    151 
    152 	/* Assume the first entry is the start of memory */
    153 	if (fdtbus_get_reg64(memory, 0, paddr, psize) != 0)
    154 		panic("Cannot determine memory size");
    155 
    156 	DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    157 	    0, *paddr, *psize);
    158 
    159 	/* If subsequent entries follow the previous one, append them. */
    160 	for (index = 1;
    161 	     fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
    162 	     index++) {
    163 		DPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    164 		    index, cur_addr, cur_size);
    165 		if (*paddr + *psize == cur_addr)
    166 			*psize += cur_size;
    167 	}
    168 }
    169 
    170 u_int
    171 initarm(void *arg)
    172 {
    173 	const struct arm_platform *plat;
    174 	uint64_t memory_addr, memory_size;
    175 	psize_t ram_size = 0;
    176 
    177 	/* Load FDT */
    178 	const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
    179 	int error = fdt_check_header(fdt_addr_r);
    180 	if (error == 0) {
    181 		error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
    182 		if (error != 0)
    183 			panic("fdt_move failed: %s", fdt_strerror(error));
    184 		fdtbus_set_data(fdt_data);
    185 	} else {
    186 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    187 	}
    188 
    189 	/* Lookup platform specific backend */
    190 	plat = arm_fdt_platform();
    191 	if (plat == NULL)
    192 		panic("Kernel does not support this device");
    193 
    194 	/* Early console may be available, announce ourselves. */
    195 	DPRINT("FDT<");
    196 	DPRINTN((uintptr_t)fdt_addr_r, 16);
    197 	DPRINT(">");
    198 
    199 	const int chosen = OF_finddevice("/chosen");
    200 	if (chosen >= 0)
    201 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    202 	boot_args = bootargs;
    203 
    204 	DPRINT(" devmap");
    205 	pmap_devmap_register(plat->devmap());
    206 
    207 	DPRINT(" bootstrap");
    208 	plat->bootstrap();
    209 
    210 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    211 	DPRINT(" cpufunc");
    212 	if (set_cpufuncs())
    213 		panic("cpu not recognized!");
    214 
    215 	/*
    216 	 * If stdout-path is specified on the command line, override the
    217 	 * value in /chosen/stdout-path before initializing console.
    218 	 */
    219 	fdt_update_stdout_path();
    220 
    221 	DPRINT(" consinit");
    222 	consinit();
    223 
    224 	DPRINTF(" ok\n");
    225 
    226 	DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
    227 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    228 
    229 	cpu_reset_address = fdt_reset;
    230 	cpu_powerdown_address = fdt_powerdown;
    231 	evbarm_device_register = fdt_device_register;
    232 
    233 	/* Talk to the user */
    234 	DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
    235 
    236 #ifdef BOOT_ARGS
    237 	char mi_bootargs[] = BOOT_ARGS;
    238 	parse_mi_bootargs(mi_bootargs);
    239 #endif
    240 
    241 	DPRINTF("KERNEL_BASE=0x%x, "
    242 		"KERNEL_VM_BASE=0x%x, "
    243 		"KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
    244 		"KERNEL_BASE_VOFFSET=0x%x\n",
    245 		KERNEL_BASE,
    246 		KERNEL_VM_BASE,
    247 		KERNEL_VM_BASE - KERNEL_BASE,
    248 		KERNEL_BASE_VOFFSET);
    249 
    250 	fdt_get_memory(&memory_addr, &memory_size);
    251 
    252 #if !defined(_LP64)
    253 	/* Cannot map memory above 4GB */
    254 	if (memory_addr + memory_size > 0x100000000)
    255 		memory_size = 0x100000000 - memory_addr;
    256 #endif
    257 
    258 	ram_size = (bus_size_t)memory_size;
    259 
    260 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
    261 	const bool mapallmem_p = true;
    262 #ifndef PMAP_NEED_ALLOC_POOLPAGE
    263 	if (ram_size > KERNEL_VM_BASE - KERNEL_BASE) {
    264 		DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
    265 		    __func__, (unsigned long) (ram_size >> 20),
    266 		    (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
    267 		ram_size = KERNEL_VM_BASE - KERNEL_BASE;
    268 	}
    269 #endif
    270 #else
    271 	const bool mapallmem_p = false;
    272 #endif
    273 
    274 	/* Fake bootconfig structure for the benefit of pmap.c. */
    275 	bootconfig.dramblocks = 1;
    276 	bootconfig.dram[0].address = (bus_addr_t)memory_addr;
    277 	bootconfig.dram[0].pages = ram_size / PAGE_SIZE;
    278 
    279 	arm32_bootmem_init(bootconfig.dram[0].address, ram_size,
    280 	    KERNEL_BASE_PHYS);
    281 	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
    282 	    plat->devmap(), mapallmem_p);
    283 
    284 	DPRINTF("bootargs: %s\n", bootargs);
    285 
    286 	parse_mi_bootargs(boot_args);
    287 
    288 #ifdef PMAP_NEED_ALLOC_POOLPAGE
    289 	bp_lowgig.bp_start = memory_addr / NBPG;
    290 	if (atop(ram_size) > bp_lowgig.bp_pages) {
    291 		arm_poolpage_vmfreelist = bp_lowgig.bp_freelist;
    292 		return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
    293 		    &bp_lowgig, 1);
    294 	}
    295 #endif
    296 
    297 	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
    298 
    299 }
    300 
    301 static void
    302 fdt_update_stdout_path(void)
    303 {
    304 	char *stdout_path, *ep;
    305 	int stdout_path_len;
    306 	char buf[256];
    307 
    308 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    309 	if (chosen_off == -1)
    310 		return;
    311 
    312 	if (get_bootconf_option(boot_args, "stdout-path",
    313 	    BOOTOPT_TYPE_STRING, &stdout_path) == 0)
    314 		return;
    315 
    316 	ep = strchr(stdout_path, ' ');
    317 	stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
    318 	if (stdout_path_len >= sizeof(buf))
    319 		return;
    320 
    321 	strncpy(buf, stdout_path, stdout_path_len);
    322 	buf[stdout_path_len] = '\0';
    323 	fdt_setprop(fdt_data, chosen_off, "stdout-path",
    324 	    buf, stdout_path_len + 1);
    325 }
    326 
    327 void
    328 consinit(void)
    329 {
    330 	static bool initialized = false;
    331 	const struct arm_platform *plat = arm_fdt_platform();
    332 	const struct fdt_console *cons = fdtbus_get_console();
    333 	struct fdt_attach_args faa;
    334 	u_int uart_freq = 0;
    335 
    336 	if (initialized || cons == NULL)
    337 		return;
    338 
    339 	plat->init_attach_args(&faa);
    340 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    341 
    342 	if (plat->uart_freq != NULL)
    343 		uart_freq = plat->uart_freq();
    344 
    345 	cons->consinit(&faa, uart_freq);
    346 
    347 	initialized = true;
    348 }
    349 
    350 void
    351 delay(u_int us)
    352 {
    353 	const struct arm_platform *plat = arm_fdt_platform();
    354 
    355 	plat->delay(us);
    356 }
    357 
    358 static void
    359 fdt_device_register(device_t self, void *aux)
    360 {
    361 	const struct arm_platform *plat = arm_fdt_platform();
    362 
    363 	if (plat && plat->device_register)
    364 		plat->device_register(self, aux);
    365 }
    366 
    367 static void
    368 fdt_reset(void)
    369 {
    370 	const struct arm_platform *plat = arm_fdt_platform();
    371 
    372 	fdtbus_power_reset();
    373 
    374 	if (plat && plat->reset)
    375 		plat->reset();
    376 }
    377 
    378 static void
    379 fdt_powerdown(void)
    380 {
    381 	fdtbus_power_poweroff();
    382 }
    383