1 /* $NetBSD: fdt_machdep.c,v 1.68 2020/03/08 08:26:54 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2015-2017 Jared McNeill <jmcneill@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.68 2020/03/08 08:26:54 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 #include "opt_efi.h" 40 41 #include "ukbd.h" 42 #include "wsdisplay.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/bus.h> 47 #include <sys/atomic.h> 48 #include <sys/cpu.h> 49 #include <sys/device.h> 50 #include <sys/exec.h> 51 #include <sys/kernel.h> 52 #include <sys/kmem.h> 53 #include <sys/ksyms.h> 54 #include <sys/msgbuf.h> 55 #include <sys/proc.h> 56 #include <sys/reboot.h> 57 #include <sys/termios.h> 58 #include <sys/bootblock.h> 59 #include <sys/disklabel.h> 60 #include <sys/vnode.h> 61 #include <sys/kauth.h> 62 #include <sys/fcntl.h> 63 #include <sys/uuid.h> 64 #include <sys/disk.h> 65 #include <sys/md5.h> 66 #include <sys/pserialize.h> 67 #include <sys/rnd.h> 68 69 #include <net/if.h> 70 #include <net/if_dl.h> 71 72 #include <dev/cons.h> 73 #include <uvm/uvm_extern.h> 74 75 #include <sys/conf.h> 76 77 #include <machine/db_machdep.h> 78 #include <ddb/db_sym.h> 79 #include <ddb/db_extern.h> 80 81 #include <machine/bootconfig.h> 82 #include <arm/armreg.h> 83 84 #include <arm/cpufunc.h> 85 86 #include <evbarm/include/autoconf.h> 87 #include <evbarm/fdt/machdep.h> 88 #include <evbarm/fdt/platform.h> 89 #include <evbarm/fdt/fdt_memory.h> 90 91 #include <arm/fdt/arm_fdtvar.h> 92 #include <dev/fdt/fdt_private.h> 93 94 #ifdef EFI_RUNTIME 95 #include <arm/arm/efi_runtime.h> 96 #endif 97 98 #if NUKBD > 0 99 #include <dev/usb/ukbdvar.h> 100 #endif 101 #if NWSDISPLAY > 0 102 #include <dev/wscons/wsdisplayvar.h> 103 #endif 104 105 #ifdef MEMORY_DISK_DYNAMIC 106 #include <dev/md.h> 107 #endif 108 109 #ifndef FDT_MAX_BOOT_STRING 110 #define FDT_MAX_BOOT_STRING 1024 111 #endif 112 113 BootConfig bootconfig; 114 char bootargs[FDT_MAX_BOOT_STRING] = ""; 115 char *boot_args = NULL; 116 117 /* filled in before cleaning bss. keep in .data */ 118 u_long uboot_args[4] __attribute__((__section__(".data"))); 119 const uint8_t *fdt_addr_r __attribute__((__section__(".data"))); 120 121 static uint64_t initrd_start, initrd_end; 122 static uint64_t rndseed_start, rndseed_end; 123 124 #include <libfdt.h> 125 #include <dev/fdt/fdtvar.h> 126 #define FDT_BUF_SIZE (512*1024) 127 static uint8_t fdt_data[FDT_BUF_SIZE]; 128 129 extern char KERNEL_BASE_phys[]; 130 #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys) 131 132 static void fdt_update_stdout_path(void); 133 static void fdt_device_register(device_t, void *); 134 static void fdt_device_register_post_config(device_t, void *); 135 static void fdt_cpu_rootconf(void); 136 static void fdt_reset(void); 137 static void fdt_powerdown(void); 138 139 static void 140 earlyconsputc(dev_t dev, int c) 141 { 142 uartputc(c); 143 } 144 145 static int 146 earlyconsgetc(dev_t dev) 147 { 148 return 0; 149 } 150 151 static struct consdev earlycons = { 152 .cn_putc = earlyconsputc, 153 .cn_getc = earlyconsgetc, 154 .cn_pollc = nullcnpollc, 155 }; 156 157 #ifdef VERBOSE_INIT_ARM 158 #define VPRINTF(...) printf(__VA_ARGS__) 159 #else 160 #define VPRINTF(...) __nothing 161 #endif 162 163 /* 164 * Get all of physical memory, including holes. 165 */ 166 static void 167 fdt_get_memory(uint64_t *pstart, uint64_t *pend) 168 { 169 const int memory = OF_finddevice("/memory"); 170 uint64_t cur_addr, cur_size; 171 int index; 172 173 /* Assume the first entry is the start of memory */ 174 if (fdtbus_get_reg64(memory, 0, &cur_addr, &cur_size) != 0) 175 panic("Cannot determine memory size"); 176 177 *pstart = cur_addr; 178 *pend = cur_addr + cur_size; 179 180 VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", 181 0, *pstart, *pend - *pstart); 182 183 for (index = 1; 184 fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0; 185 index++) { 186 VPRINTF("FDT /memory [%d] @ 0x%" PRIx64 " size 0x%" PRIx64 "\n", 187 index, cur_addr, cur_size); 188 189 if (cur_addr + cur_size > *pend) 190 *pend = cur_addr + cur_size; 191 } 192 } 193 194 void 195 fdt_add_reserved_memory_range(uint64_t addr, uint64_t size) 196 { 197 fdt_memory_remove_range(addr, size); 198 } 199 200 /* 201 * Exclude memory ranges from memory config from the device tree 202 */ 203 static void 204 fdt_add_reserved_memory(uint64_t min_addr, uint64_t max_addr) 205 { 206 uint64_t lstart = 0, lend = 0; 207 uint64_t addr, size; 208 int index, error; 209 210 const int num = fdt_num_mem_rsv(fdtbus_get_data()); 211 for (index = 0; index <= num; index++) { 212 error = fdt_get_mem_rsv(fdtbus_get_data(), index, 213 &addr, &size); 214 if (error != 0) 215 continue; 216 if (lstart <= addr && addr <= lend) { 217 size -= (lend - addr); 218 addr = lend; 219 } 220 if (size == 0) 221 continue; 222 if (addr + size <= min_addr) 223 continue; 224 if (addr >= max_addr) 225 continue; 226 if (addr < min_addr) { 227 size -= (min_addr - addr); 228 addr = min_addr; 229 } 230 if (addr + size > max_addr) 231 size = max_addr - addr; 232 fdt_add_reserved_memory_range(addr, size); 233 lstart = addr; 234 lend = addr + size; 235 } 236 } 237 238 static void 239 fdt_add_dram_blocks(const struct fdt_memory *m, void *arg) 240 { 241 BootConfig *bc = arg; 242 243 VPRINTF(" %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1); 244 bc->dram[bc->dramblocks].address = m->start; 245 bc->dram[bc->dramblocks].pages = 246 (m->end - m->start) / PAGE_SIZE; 247 bc->dramblocks++; 248 } 249 250 #define MAX_PHYSMEM 64 251 static int nfdt_physmem = 0; 252 static struct boot_physmem fdt_physmem[MAX_PHYSMEM]; 253 254 static void 255 fdt_add_boot_physmem(const struct fdt_memory *m, void *arg) 256 { 257 const paddr_t saddr = round_page(m->start); 258 const paddr_t eaddr = trunc_page(m->end); 259 260 VPRINTF(" %" PRIx64 " - %" PRIx64, m->start, m->end - 1); 261 if (saddr >= eaddr) { 262 VPRINTF(" skipped\n"); 263 return; 264 } 265 VPRINTF("\n"); 266 267 struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++]; 268 269 KASSERT(nfdt_physmem <= MAX_PHYSMEM); 270 271 bp->bp_start = atop(saddr); 272 bp->bp_pages = atop(eaddr) - bp->bp_start; 273 bp->bp_freelist = VM_FREELIST_DEFAULT; 274 275 #ifdef PMAP_NEED_ALLOC_POOLPAGE 276 const uint64_t memory_size = *(uint64_t *)arg; 277 if (atop(memory_size) > bp->bp_pages) { 278 arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP; 279 bp->bp_freelist = VM_FREELIST_DIRECTMAP; 280 } 281 #endif 282 } 283 284 /* 285 * Define usable memory regions. 286 */ 287 static void 288 fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end) 289 { 290 const int memory = OF_finddevice("/memory"); 291 BootConfig *bc = &bootconfig; 292 uint64_t addr, size; 293 int index; 294 295 for (index = 0; 296 fdtbus_get_reg64(memory, index, &addr, &size) == 0; 297 index++) { 298 if (addr >= mem_end || size == 0) 299 continue; 300 if (addr + size > mem_end) 301 size = mem_end - addr; 302 303 fdt_memory_add_range(addr, size); 304 } 305 306 fdt_add_reserved_memory(mem_start, mem_end); 307 308 const uint64_t initrd_size = initrd_end - initrd_start; 309 if (initrd_size > 0) 310 fdt_memory_remove_range(initrd_start, initrd_size); 311 312 const uint64_t rndseed_size = rndseed_end - rndseed_start; 313 if (rndseed_size > 0) 314 fdt_memory_remove_range(rndseed_start, rndseed_size); 315 316 const int framebuffer = OF_finddevice("/chosen/framebuffer"); 317 if (framebuffer >= 0) { 318 for (index = 0; 319 fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0; 320 index++) { 321 fdt_add_reserved_memory_range(addr, size); 322 } 323 } 324 325 VPRINTF("Usable memory:\n"); 326 bc->dramblocks = 0; 327 fdt_memory_foreach(fdt_add_dram_blocks, bc); 328 } 329 330 static void 331 fdt_probe_initrd(uint64_t *pstart, uint64_t *pend) 332 { 333 *pstart = *pend = 0; 334 335 #ifdef MEMORY_DISK_DYNAMIC 336 const int chosen = OF_finddevice("/chosen"); 337 if (chosen < 0) 338 return; 339 340 int len; 341 const void *start_data = fdtbus_get_prop(chosen, 342 "linux,initrd-start", &len); 343 const void *end_data = fdtbus_get_prop(chosen, 344 "linux,initrd-end", NULL); 345 if (start_data == NULL || end_data == NULL) 346 return; 347 348 switch (len) { 349 case 4: 350 *pstart = be32dec(start_data); 351 *pend = be32dec(end_data); 352 break; 353 case 8: 354 *pstart = be64dec(start_data); 355 *pend = be64dec(end_data); 356 break; 357 default: 358 printf("Unsupported len %d for /chosen/initrd-start\n", len); 359 return; 360 } 361 #endif 362 } 363 364 static void 365 fdt_setup_initrd(void) 366 { 367 #ifdef MEMORY_DISK_DYNAMIC 368 const uint64_t initrd_size = initrd_end - initrd_start; 369 paddr_t startpa = trunc_page(initrd_start); 370 paddr_t endpa = round_page(initrd_end); 371 paddr_t pa; 372 vaddr_t va; 373 void *md_start; 374 375 if (initrd_size == 0) 376 return; 377 378 va = uvm_km_alloc(kernel_map, initrd_size, 0, 379 UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 380 if (va == 0) { 381 printf("Failed to allocate VA for initrd\n"); 382 return; 383 } 384 385 md_start = (void *)va; 386 387 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) 388 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0); 389 pmap_update(pmap_kernel()); 390 391 md_root_setconf(md_start, initrd_size); 392 #endif 393 } 394 395 static void 396 fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend) 397 { 398 int chosen, len; 399 const void *start_data, *end_data; 400 401 *pstart = *pend = 0; 402 chosen = OF_finddevice("/chosen"); 403 if (chosen < 0) 404 return; 405 406 start_data = fdtbus_get_prop(chosen, "netbsd,rndseed-start", &len); 407 end_data = fdtbus_get_prop(chosen, "netbsd,rndseed-end", NULL); 408 if (start_data == NULL || end_data == NULL) 409 return; 410 411 switch (len) { 412 case 4: 413 *pstart = be32dec(start_data); 414 *pend = be32dec(end_data); 415 break; 416 case 8: 417 *pstart = be64dec(start_data); 418 *pend = be64dec(end_data); 419 break; 420 default: 421 printf("Unsupported len %d for /chosen/rndseed-start\n", len); 422 return; 423 } 424 } 425 426 static void 427 fdt_setup_rndseed(void) 428 { 429 const uint64_t rndseed_size = rndseed_end - rndseed_start; 430 const paddr_t startpa = trunc_page(rndseed_start); 431 const paddr_t endpa = round_page(rndseed_end); 432 paddr_t pa; 433 vaddr_t va; 434 void *rndseed; 435 436 if (rndseed_size == 0) 437 return; 438 439 va = uvm_km_alloc(kernel_map, endpa - startpa, 0, 440 UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 441 if (va == 0) { 442 printf("Failed to allocate VA for rndseed\n"); 443 return; 444 } 445 rndseed = (void *)va; 446 447 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) 448 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0); 449 pmap_update(pmap_kernel()); 450 451 rnd_seed(rndseed, rndseed_size); 452 } 453 454 #ifdef EFI_RUNTIME 455 static void 456 fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type) 457 { 458 int len; 459 460 const int chosen_off = fdt_path_offset(fdt_data, "/chosen"); 461 if (chosen_off < 0) 462 return; 463 464 const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len); 465 if (map == NULL) 466 return; 467 468 while (len >= 24) { 469 const paddr_t pa = be64toh(map[0]); 470 const vaddr_t va = be64toh(map[1]); 471 const uint64_t sz = be64toh(map[2]); 472 VPRINTF("%s: %s %lx-%lx (%lx-%lx)\n", __func__, prop, pa, pa+sz-1, va, va+sz-1); 473 arm_efirt_md_map_range(va, pa, sz, type); 474 map += 3; 475 len -= 24; 476 } 477 } 478 #endif 479 480 vaddr_t 481 initarm(void *arg) 482 { 483 const struct arm_platform *plat; 484 uint64_t memory_start, memory_end; 485 486 /* set temporally to work printf()/panic() even before consinit() */ 487 cn_tab = &earlycons; 488 489 /* Load FDT */ 490 int error = fdt_check_header(fdt_addr_r); 491 if (error == 0) { 492 /* If the DTB is too big, try to pack it in place first. */ 493 if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data)) 494 (void)fdt_pack(__UNCONST(fdt_addr_r)); 495 error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data)); 496 if (error != 0) 497 panic("fdt_move failed: %s", fdt_strerror(error)); 498 fdtbus_init(fdt_data); 499 } else { 500 panic("fdt_check_header failed: %s", fdt_strerror(error)); 501 } 502 503 /* Lookup platform specific backend */ 504 plat = arm_fdt_platform(); 505 if (plat == NULL) 506 panic("Kernel does not support this device"); 507 508 /* Early console may be available, announce ourselves. */ 509 VPRINTF("FDT<%p>\n", fdt_addr_r); 510 511 const int chosen = OF_finddevice("/chosen"); 512 if (chosen >= 0) 513 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs)); 514 boot_args = bootargs; 515 516 /* Heads up ... Setup the CPU / MMU / TLB functions. */ 517 VPRINTF("cpufunc\n"); 518 if (set_cpufuncs()) 519 panic("cpu not recognized!"); 520 521 /* 522 * Memory is still identity/flat mapped this point so using ttbr for 523 * l1pt VA is fine 524 */ 525 526 VPRINTF("devmap\n"); 527 extern char ARM_BOOTSTRAP_LxPT[]; 528 pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->ap_devmap()); 529 530 VPRINTF("bootstrap\n"); 531 plat->ap_bootstrap(); 532 533 /* 534 * If stdout-path is specified on the command line, override the 535 * value in /chosen/stdout-path before initializing console. 536 */ 537 VPRINTF("stdout\n"); 538 fdt_update_stdout_path(); 539 540 /* 541 * Done making changes to the FDT. 542 */ 543 fdt_pack(fdt_data); 544 545 VPRINTF("consinit "); 546 consinit(); 547 VPRINTF("ok\n"); 548 549 VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n", 550 uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]); 551 552 cpu_reset_address = fdt_reset; 553 cpu_powerdown_address = fdt_powerdown; 554 evbarm_device_register = fdt_device_register; 555 evbarm_device_register_post_config = fdt_device_register_post_config; 556 evbarm_cpu_rootconf = fdt_cpu_rootconf; 557 558 /* Talk to the user */ 559 printf("NetBSD/evbarm (fdt) booting ...\n"); 560 561 #ifdef BOOT_ARGS 562 char mi_bootargs[] = BOOT_ARGS; 563 parse_mi_bootargs(mi_bootargs); 564 #endif 565 566 fdt_get_memory(&memory_start, &memory_end); 567 568 #if !defined(_LP64) 569 /* Cannot map memory above 4GB */ 570 if (memory_end >= 0x100000000ULL) 571 memory_end = 0x100000000ULL - PAGE_SIZE; 572 573 #endif 574 uint64_t memory_size = memory_end - memory_start; 575 576 VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %" 577 PRIx64 ")\n", __func__, memory_start, memory_end, memory_size); 578 579 /* Parse ramdisk info */ 580 fdt_probe_initrd(&initrd_start, &initrd_end); 581 582 /* Parse rndseed */ 583 fdt_probe_rndseed(&rndseed_start, &rndseed_end); 584 585 /* 586 * Populate bootconfig structure for the benefit of 587 * dodumpsys 588 */ 589 VPRINTF("%s: fdt_build_bootconfig\n", __func__); 590 fdt_build_bootconfig(memory_start, memory_end); 591 592 #ifdef EFI_RUNTIME 593 fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE); 594 fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA); 595 fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO); 596 #endif 597 598 /* Perform PT build and VM init */ 599 cpu_kernel_vm_init(memory_start, memory_size); 600 601 VPRINTF("bootargs: %s\n", bootargs); 602 603 parse_mi_bootargs(boot_args); 604 605 VPRINTF("Memory regions:\n"); 606 fdt_memory_foreach(fdt_add_boot_physmem, &memory_size); 607 608 vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem, 609 nfdt_physmem); 610 611 /* 612 * initarm_common flushes cache if required before AP start 613 */ 614 error = 0; 615 if ((boothowto & RB_MD1) == 0) { 616 VPRINTF("mpstart\n"); 617 if (plat->ap_mpstart) 618 error = plat->ap_mpstart(); 619 } 620 621 if (error) 622 return sp; 623 /* 624 * Now we have APs started the pages used for stacks and L1PT can 625 * be given to uvm 626 */ 627 extern char const __start__init_memory[]; 628 extern char const __stop__init_memory[] __weak; 629 630 if (__start__init_memory != __stop__init_memory) { 631 const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory); 632 const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory); 633 const paddr_t spg = atop(spa); 634 const paddr_t epg = atop(epa); 635 636 VPRINTF(" start %08lx end %08lx... " 637 "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT); 638 639 uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT); 640 641 } 642 643 return sp; 644 } 645 646 static void 647 fdt_update_stdout_path(void) 648 { 649 char *stdout_path, *ep; 650 int stdout_path_len; 651 char buf[256]; 652 653 const int chosen_off = fdt_path_offset(fdt_data, "/chosen"); 654 if (chosen_off == -1) 655 return; 656 657 if (get_bootconf_option(boot_args, "stdout-path", 658 BOOTOPT_TYPE_STRING, &stdout_path) == 0) 659 return; 660 661 ep = strchr(stdout_path, ' '); 662 stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path); 663 if (stdout_path_len >= sizeof(buf)) 664 return; 665 666 strncpy(buf, stdout_path, stdout_path_len); 667 buf[stdout_path_len] = '\0'; 668 fdt_setprop(fdt_data, chosen_off, "stdout-path", 669 buf, stdout_path_len + 1); 670 } 671 672 void 673 consinit(void) 674 { 675 static bool initialized = false; 676 const struct arm_platform *plat = arm_fdt_platform(); 677 const struct fdt_console *cons = fdtbus_get_console(); 678 struct fdt_attach_args faa; 679 u_int uart_freq = 0; 680 681 if (initialized || cons == NULL) 682 return; 683 684 plat->ap_init_attach_args(&faa); 685 faa.faa_phandle = fdtbus_get_stdout_phandle(); 686 687 if (plat->ap_uart_freq != NULL) 688 uart_freq = plat->ap_uart_freq(); 689 690 cons->consinit(&faa, uart_freq); 691 692 initialized = true; 693 } 694 695 void 696 cpu_startup_hook(void) 697 { 698 699 fdtbus_intr_init(); 700 701 fdt_setup_rndseed(); 702 } 703 704 void 705 delay(u_int us) 706 { 707 const struct arm_platform *plat = arm_fdt_platform(); 708 709 plat->ap_delay(us); 710 } 711 712 static void 713 fdt_detect_root_device(device_t dev) 714 { 715 struct mbr_sector mbr; 716 uint8_t buf[DEV_BSIZE]; 717 uint8_t hash[16]; 718 const uint8_t *rhash; 719 char rootarg[64]; 720 struct vnode *vp; 721 MD5_CTX md5ctx; 722 int error, len; 723 size_t resid; 724 u_int part; 725 726 const int chosen = OF_finddevice("/chosen"); 727 if (chosen < 0) 728 return; 729 730 if (of_hasprop(chosen, "netbsd,mbr") && 731 of_hasprop(chosen, "netbsd,partition")) { 732 733 /* 734 * The bootloader has passed in a partition index and MD5 hash 735 * of the MBR sector. Read the MBR of this device, calculate the 736 * hash, and compare it with the value passed in. 737 */ 738 rhash = fdtbus_get_prop(chosen, "netbsd,mbr", &len); 739 if (rhash == NULL || len != 16) 740 return; 741 of_getprop_uint32(chosen, "netbsd,partition", &part); 742 if (part >= MAXPARTITIONS) 743 return; 744 745 vp = opendisk(dev); 746 if (!vp) 747 return; 748 error = vn_rdwr(UIO_READ, vp, buf, sizeof(buf), 0, UIO_SYSSPACE, 749 0, NOCRED, &resid, NULL); 750 VOP_CLOSE(vp, FREAD, NOCRED); 751 vput(vp); 752 753 if (error != 0) 754 return; 755 756 memcpy(&mbr, buf, sizeof(mbr)); 757 MD5Init(&md5ctx); 758 MD5Update(&md5ctx, (void *)&mbr, sizeof(mbr)); 759 MD5Final(hash, &md5ctx); 760 761 if (memcmp(rhash, hash, 16) != 0) 762 return; 763 764 snprintf(rootarg, sizeof(rootarg), " root=%s%c", device_xname(dev), part + 'a'); 765 strcat(boot_args, rootarg); 766 } 767 768 if (of_hasprop(chosen, "netbsd,gpt-guid")) { 769 char guidbuf[UUID_STR_LEN]; 770 const struct uuid *guid = fdtbus_get_prop(chosen, "netbsd,gpt-guid", &len); 771 if (guid == NULL || len != 16) 772 return; 773 774 uuid_snprintf(guidbuf, sizeof(guidbuf), guid); 775 snprintf(rootarg, sizeof(rootarg), " root=wedge:%s", guidbuf); 776 strcat(boot_args, rootarg); 777 } 778 779 if (of_hasprop(chosen, "netbsd,gpt-label")) { 780 const char *label = fdtbus_get_string(chosen, "netbsd,gpt-label"); 781 if (label == NULL || *label == '\0') 782 return; 783 784 device_t dv = dkwedge_find_by_wname(label); 785 if (dv != NULL) 786 booted_device = dv; 787 } 788 789 if (of_hasprop(chosen, "netbsd,booted-mac-address")) { 790 const uint8_t *macaddr = fdtbus_get_prop(chosen, "netbsd,booted-mac-address", &len); 791 if (macaddr == NULL || len != 6) 792 return; 793 int s = pserialize_read_enter(); 794 struct ifnet *ifp; 795 IFNET_READER_FOREACH(ifp) { 796 if (memcmp(macaddr, CLLADDR(ifp->if_sadl), len) == 0) { 797 device_t dv = device_find_by_xname(ifp->if_xname); 798 if (dv != NULL) 799 booted_device = dv; 800 break; 801 } 802 } 803 pserialize_read_exit(s); 804 } 805 } 806 807 static void 808 fdt_device_register(device_t self, void *aux) 809 { 810 const struct arm_platform *plat = arm_fdt_platform(); 811 812 if (device_is_a(self, "armfdt")) 813 fdt_setup_initrd(); 814 815 if (plat && plat->ap_device_register) 816 plat->ap_device_register(self, aux); 817 } 818 819 static void 820 fdt_device_register_post_config(device_t self, void *aux) 821 { 822 #if NUKBD > 0 && NWSDISPLAY > 0 823 if (device_is_a(self, "wsdisplay")) { 824 struct wsdisplay_softc *sc = device_private(self); 825 if (wsdisplay_isconsole(sc)) 826 ukbd_cnattach(); 827 } 828 #endif 829 } 830 831 static void 832 fdt_cpu_rootconf(void) 833 { 834 device_t dev; 835 deviter_t di; 836 char *ptr; 837 838 for (dev = deviter_first(&di, 0); dev; dev = deviter_next(&di)) { 839 if (device_class(dev) != DV_DISK) 840 continue; 841 842 if (get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0) 843 break; 844 845 if (device_is_a(dev, "ld") || device_is_a(dev, "sd") || device_is_a(dev, "wd")) 846 fdt_detect_root_device(dev); 847 } 848 deviter_release(&di); 849 } 850 851 static void 852 fdt_reset(void) 853 { 854 const struct arm_platform *plat = arm_fdt_platform(); 855 856 fdtbus_power_reset(); 857 858 if (plat && plat->ap_reset) 859 plat->ap_reset(); 860 } 861 862 static void 863 fdt_powerdown(void) 864 { 865 fdtbus_power_poweroff(); 866 } 867