1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 2004 Olivier Houchard 7 * Copyright (c) 1994-1998 Mark Brinicombe. 8 * Copyright (c) 1994 Brini. 9 * All rights reserved. 10 * 11 * This code is derived from software written for Brini by Mark Brinicombe 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Mark Brinicombe 24 * for the NetBSD Project. 25 * 4. The name of the company nor the name of the author may be used to 26 * endorse or promote products derived from this software without specific 27 * prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 32 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * Machine dependent functions for kernel setup 42 * 43 * Created : 17/09/94 44 * Updated : 18/04/01 updated for new wscons 45 */ 46 47 #include "opt_ddb.h" 48 #include "opt_kstack_pages.h" 49 #include "opt_platform.h" 50 #include "opt_sched.h" 51 52 #include <sys/cdefs.h> 53 __FBSDID("$FreeBSD$"); 54 55 #include <sys/param.h> 56 #include <sys/buf.h> 57 #include <sys/bus.h> 58 #include <sys/cons.h> 59 #include <sys/cpu.h> 60 #include <sys/devmap.h> 61 #include <sys/efi.h> 62 #include <sys/imgact.h> 63 #include <sys/kdb.h> 64 #include <sys/kernel.h> 65 #include <sys/ktr.h> 66 #include <sys/linker.h> 67 #include <sys/msgbuf.h> 68 #include <sys/physmem.h> 69 #include <sys/reboot.h> 70 #include <sys/rwlock.h> 71 #include <sys/sched.h> 72 #include <sys/syscallsubr.h> 73 #include <sys/sysent.h> 74 #include <sys/sysproto.h> 75 #include <sys/vmmeter.h> 76 77 #include <vm/vm_object.h> 78 #include <vm/vm_page.h> 79 #include <vm/vm_pager.h> 80 81 #include <machine/asm.h> 82 #include <machine/debug_monitor.h> 83 #include <machine/machdep.h> 84 #include <machine/metadata.h> 85 #include <machine/pcb.h> 86 #include <machine/platform.h> 87 #include <machine/sysarch.h> 88 #include <machine/undefined.h> 89 #include <machine/vfp.h> 90 #include <machine/vmparam.h> 91 92 #ifdef FDT 93 #include <dev/fdt/fdt_common.h> 94 #include <machine/ofw_machdep.h> 95 #endif 96 97 #ifdef DEBUG 98 #define debugf(fmt, args...) printf(fmt, ##args) 99 #else 100 #define debugf(fmt, args...) 101 #endif 102 103 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 104 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) || \ 105 defined(COMPAT_FREEBSD9) 106 #error FreeBSD/arm doesn't provide compatibility with releases prior to 10 107 #endif 108 109 110 #if __ARM_ARCH < 6 111 #error FreeBSD requires ARMv6 or later 112 #endif 113 114 struct pcpu __pcpu[MAXCPU]; 115 struct pcpu *pcpup = &__pcpu[0]; 116 117 static struct trapframe proc0_tf; 118 uint32_t cpu_reset_address = 0; 119 int cold = 1; 120 vm_offset_t vector_page; 121 122 /* The address at which the kernel was loaded. Set early in initarm(). */ 123 vm_paddr_t arm_physmem_kernaddr; 124 125 extern int *end; 126 127 #ifdef FDT 128 vm_paddr_t pmap_pa; 129 vm_offset_t systempage; 130 vm_offset_t irqstack; 131 vm_offset_t undstack; 132 vm_offset_t abtstack; 133 #endif /* FDT */ 134 135 #ifdef PLATFORM 136 static delay_func *delay_impl; 137 static void *delay_arg; 138 #endif 139 140 struct kva_md_info kmi; 141 /* 142 * arm32_vector_init: 143 * 144 * Initialize the vector page, and select whether or not to 145 * relocate the vectors. 146 * 147 * NOTE: We expect the vector page to be mapped at its expected 148 * destination. 149 */ 150 151 extern unsigned int page0[], page0_data[]; 152 void 153 arm_vector_init(vm_offset_t va, int which) 154 { 155 unsigned int *vectors = (int *) va; 156 unsigned int *vectors_data = vectors + (page0_data - page0); 157 int vec; 158 159 /* 160 * Loop through the vectors we're taking over, and copy the 161 * vector's insn and data word. 162 */ 163 for (vec = 0; vec < ARM_NVEC; vec++) { 164 if ((which & (1 << vec)) == 0) { 165 /* Don't want to take over this vector. */ 166 continue; 167 } 168 vectors[vec] = page0[vec]; 169 vectors_data[vec] = page0_data[vec]; 170 } 171 172 /* Now sync the vectors. */ 173 icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int)); 174 175 vector_page = va; 176 } 177 178 static void 179 cpu_startup(void *dummy) 180 { 181 struct pcb *pcb = thread0.td_pcb; 182 const unsigned int mbyte = 1024 * 1024; 183 184 identify_arm_cpu(); 185 186 vm_ksubmap_init(&kmi); 187 188 /* 189 * Display the RAM layout. 190 */ 191 printf("real memory = %ju (%ju MB)\n", 192 (uintmax_t)arm32_ptob(realmem), 193 (uintmax_t)arm32_ptob(realmem) / mbyte); 194 printf("avail memory = %ju (%ju MB)\n", 195 (uintmax_t)arm32_ptob(vm_free_count()), 196 (uintmax_t)arm32_ptob(vm_free_count()) / mbyte); 197 if (bootverbose) { 198 physmem_print_tables(); 199 devmap_print_table(); 200 } 201 202 bufinit(); 203 vm_pager_bufferinit(); 204 pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack + 205 USPACE_SVC_STACK_TOP; 206 pmap_set_pcb_pagedir(kernel_pmap, pcb); 207 } 208 209 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL); 210 211 /* 212 * Flush the D-cache for non-DMA I/O so that the I-cache can 213 * be made coherent later. 214 */ 215 void 216 cpu_flush_dcache(void *ptr, size_t len) 217 { 218 219 dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len); 220 } 221 222 /* Get current clock frequency for the given cpu id. */ 223 int 224 cpu_est_clockrate(int cpu_id, uint64_t *rate) 225 { 226 struct pcpu *pc; 227 228 pc = pcpu_find(cpu_id); 229 if (pc == NULL || rate == NULL) 230 return (EINVAL); 231 232 if (pc->pc_clock == 0) 233 return (EOPNOTSUPP); 234 235 *rate = pc->pc_clock; 236 237 return (0); 238 } 239 240 void 241 cpu_idle(int busy) 242 { 243 244 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu); 245 spinlock_enter(); 246 if (!busy) 247 cpu_idleclock(); 248 if (!sched_runnable()) 249 cpu_sleep(0); 250 if (!busy) 251 cpu_activeclock(); 252 spinlock_exit(); 253 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu); 254 } 255 256 int 257 cpu_idle_wakeup(int cpu) 258 { 259 260 return (0); 261 } 262 263 void 264 cpu_initclocks(void) 265 { 266 267 #ifdef SMP 268 if (PCPU_GET(cpuid) == 0) 269 cpu_initclocks_bsp(); 270 else 271 cpu_initclocks_ap(); 272 #else 273 cpu_initclocks_bsp(); 274 #endif 275 } 276 277 #ifdef PLATFORM 278 void 279 arm_set_delay(delay_func *impl, void *arg) 280 { 281 282 KASSERT(impl != NULL, ("No DELAY implementation")); 283 delay_impl = impl; 284 delay_arg = arg; 285 } 286 287 void 288 DELAY(int usec) 289 { 290 291 TSENTER(); 292 delay_impl(usec, delay_arg); 293 TSEXIT(); 294 } 295 #endif 296 297 void 298 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 299 { 300 301 pcpu->pc_mpidr = 0xffffffff; 302 } 303 304 void 305 spinlock_enter(void) 306 { 307 struct thread *td; 308 register_t cspr; 309 310 td = curthread; 311 if (td->td_md.md_spinlock_count == 0) { 312 cspr = disable_interrupts(PSR_I | PSR_F); 313 td->td_md.md_spinlock_count = 1; 314 td->td_md.md_saved_cspr = cspr; 315 critical_enter(); 316 } else 317 td->td_md.md_spinlock_count++; 318 } 319 320 void 321 spinlock_exit(void) 322 { 323 struct thread *td; 324 register_t cspr; 325 326 td = curthread; 327 cspr = td->td_md.md_saved_cspr; 328 td->td_md.md_spinlock_count--; 329 if (td->td_md.md_spinlock_count == 0) { 330 critical_exit(); 331 restore_interrupts(cspr); 332 } 333 } 334 335 /* 336 * Construct a PCB from a trapframe. This is called from kdb_trap() where 337 * we want to start a backtrace from the function that caused us to enter 338 * the debugger. We have the context in the trapframe, but base the trace 339 * on the PCB. The PCB doesn't have to be perfect, as long as it contains 340 * enough for a backtrace. 341 */ 342 void 343 makectx(struct trapframe *tf, struct pcb *pcb) 344 { 345 pcb->pcb_regs.sf_r4 = tf->tf_r4; 346 pcb->pcb_regs.sf_r5 = tf->tf_r5; 347 pcb->pcb_regs.sf_r6 = tf->tf_r6; 348 pcb->pcb_regs.sf_r7 = tf->tf_r7; 349 pcb->pcb_regs.sf_r8 = tf->tf_r8; 350 pcb->pcb_regs.sf_r9 = tf->tf_r9; 351 pcb->pcb_regs.sf_r10 = tf->tf_r10; 352 pcb->pcb_regs.sf_r11 = tf->tf_r11; 353 pcb->pcb_regs.sf_r12 = tf->tf_r12; 354 pcb->pcb_regs.sf_pc = tf->tf_pc; 355 pcb->pcb_regs.sf_lr = tf->tf_usr_lr; 356 pcb->pcb_regs.sf_sp = tf->tf_usr_sp; 357 } 358 359 void 360 pcpu0_init(void) 361 { 362 set_curthread(&thread0); 363 pcpu_init(pcpup, 0, sizeof(struct pcpu)); 364 pcpup->pc_mpidr = cp15_mpidr_get() & 0xFFFFFF; 365 PCPU_SET(curthread, &thread0); 366 } 367 368 /* 369 * Initialize proc0 370 */ 371 void 372 init_proc0(vm_offset_t kstack) 373 { 374 proc_linkup0(&proc0, &thread0); 375 thread0.td_kstack = kstack; 376 thread0.td_kstack_pages = kstack_pages; 377 thread0.td_pcb = (struct pcb *)(thread0.td_kstack + 378 thread0.td_kstack_pages * PAGE_SIZE) - 1; 379 thread0.td_pcb->pcb_flags = 0; 380 thread0.td_pcb->pcb_fpflags = 0; 381 thread0.td_pcb->pcb_vfpcpu = -1; 382 thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN; 383 thread0.td_pcb->pcb_vfpsaved = &thread0.td_pcb->pcb_vfpstate; 384 thread0.td_frame = &proc0_tf; 385 pcpup->pc_curpcb = thread0.td_pcb; 386 } 387 388 void 389 set_stackptrs(int cpu) 390 { 391 392 set_stackptr(PSR_IRQ32_MODE, 393 irqstack + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1))); 394 set_stackptr(PSR_ABT32_MODE, 395 abtstack + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1))); 396 set_stackptr(PSR_UND32_MODE, 397 undstack + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1))); 398 } 399 400 static void 401 arm_kdb_init(void) 402 { 403 404 kdb_init(); 405 #ifdef KDB 406 if (boothowto & RB_KDB) 407 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger"); 408 #endif 409 } 410 411 #ifdef FDT 412 void * 413 initarm(struct arm_boot_params *abp) 414 { 415 struct mem_region mem_regions[FDT_MEM_REGIONS]; 416 vm_paddr_t lastaddr; 417 vm_offset_t dtbp, kernelstack, dpcpu; 418 char *env; 419 void *kmdp; 420 int err_devmap, mem_regions_sz; 421 phandle_t root; 422 char dts_version[255]; 423 #ifdef EFI 424 struct efi_map_header *efihdr; 425 #endif 426 427 /* get last allocated physical address */ 428 arm_physmem_kernaddr = abp->abp_physaddr; 429 lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr; 430 431 set_cpufuncs(); 432 cpuinfo_init(); 433 434 /* 435 * Find the dtb passed in by the boot loader. 436 */ 437 kmdp = preload_search_by_type("elf kernel"); 438 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t); 439 #if defined(FDT_DTB_STATIC) 440 /* 441 * In case the device tree blob was not retrieved (from metadata) try 442 * to use the statically embedded one. 443 */ 444 if (dtbp == (vm_offset_t)NULL) 445 dtbp = (vm_offset_t)&fdt_static_dtb; 446 #endif 447 448 if (OF_install(OFW_FDT, 0) == FALSE) 449 panic("Cannot install FDT"); 450 451 if (OF_init((void *)dtbp) != 0) 452 panic("OF_init failed with the found device tree"); 453 454 #if defined(LINUX_BOOT_ABI) 455 arm_parse_fdt_bootargs(); 456 #endif 457 458 #ifdef EFI 459 efihdr = (struct efi_map_header *)preload_search_info(kmdp, 460 MODINFO_METADATA | MODINFOMD_EFI_MAP); 461 if (efihdr != NULL) { 462 arm_add_efi_map_entries(efihdr, mem_regions, &mem_regions_sz); 463 } else 464 #endif 465 { 466 /* Grab physical memory regions information from device tree. */ 467 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0) 468 panic("Cannot get physical memory regions"); 469 } 470 physmem_hardware_regions(mem_regions, mem_regions_sz); 471 472 /* Grab reserved memory regions information from device tree. */ 473 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0) 474 physmem_exclude_regions(mem_regions, mem_regions_sz, 475 EXFLAG_NODUMP | EXFLAG_NOALLOC); 476 477 /* 478 * Set TEX remapping registers. 479 * Setup kernel page tables and switch to kernel L1 page table. 480 */ 481 pmap_set_tex(); 482 pmap_bootstrap_prepare(lastaddr); 483 484 /* 485 * If EARLY_PRINTF support is enabled, we need to re-establish the 486 * mapping after pmap_bootstrap_prepare() switches to new page tables. 487 * Note that we can only do the remapping if the VA is outside the 488 * kernel, now that we have real virtual (not VA=PA) mappings in effect. 489 * Early printf does not work between the time pmap_set_tex() does 490 * cp15_prrr_set() and this code remaps the VA. 491 */ 492 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE 493 pmap_preboot_map_attr(SOCDEV_PA, SOCDEV_VA, 1024 * 1024, 494 VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE); 495 #endif 496 497 /* 498 * Now that proper page tables are installed, call cpu_setup() to enable 499 * instruction and data caches and other chip-specific features. 500 */ 501 cpu_setup(); 502 503 /* Platform-specific initialisation */ 504 platform_probe_and_attach(); 505 pcpu0_init(); 506 507 /* Do basic tuning, hz etc */ 508 init_param1(); 509 510 /* 511 * Allocate a page for the system page mapped to 0xffff0000 512 * This page will just contain the system vectors and can be 513 * shared by all processes. 514 */ 515 systempage = pmap_preboot_get_pages(1); 516 517 /* Map the vector page. */ 518 pmap_preboot_map_pages(systempage, ARM_VECTORS_HIGH, 1); 519 if (virtual_end >= ARM_VECTORS_HIGH) 520 virtual_end = ARM_VECTORS_HIGH - 1; 521 522 /* Allocate dynamic per-cpu area. */ 523 dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE); 524 dpcpu_init((void *)dpcpu, 0); 525 526 /* Allocate stacks for all modes */ 527 irqstack = pmap_preboot_get_vpages(IRQ_STACK_SIZE * MAXCPU); 528 abtstack = pmap_preboot_get_vpages(ABT_STACK_SIZE * MAXCPU); 529 undstack = pmap_preboot_get_vpages(UND_STACK_SIZE * MAXCPU ); 530 kernelstack = pmap_preboot_get_vpages(kstack_pages); 531 532 /* Allocate message buffer. */ 533 msgbufp = (void *)pmap_preboot_get_vpages( 534 round_page(msgbufsize) / PAGE_SIZE); 535 536 /* 537 * Pages were allocated during the secondary bootstrap for the 538 * stacks for different CPU modes. 539 * We must now set the r13 registers in the different CPU modes to 540 * point to these stacks. 541 * Since the ARM stacks use STMFD etc. we must set r13 to the top end 542 * of the stack memory. 543 */ 544 set_stackptrs(0); 545 mutex_init(); 546 547 /* Establish static device mappings. */ 548 err_devmap = platform_devmap_init(); 549 devmap_bootstrap(0, NULL); 550 vm_max_kernel_address = platform_lastaddr(); 551 552 /* 553 * Only after the SOC registers block is mapped we can perform device 554 * tree fixups, as they may attempt to read parameters from hardware. 555 */ 556 OF_interpret("perform-fixup", 0); 557 platform_gpio_init(); 558 cninit(); 559 560 /* 561 * If we made a mapping for EARLY_PRINTF after pmap_bootstrap_prepare(), 562 * undo it now that the normal console printf works. 563 */ 564 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE 565 pmap_kremove(SOCDEV_VA); 566 #endif 567 568 debugf("initarm: console initialized\n"); 569 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp); 570 debugf(" boothowto = 0x%08x\n", boothowto); 571 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp); 572 debugf(" lastaddr1: 0x%08x\n", lastaddr); 573 arm_print_kenv(); 574 575 env = kern_getenv("kernelname"); 576 if (env != NULL) 577 strlcpy(kernelname, env, sizeof(kernelname)); 578 579 if (err_devmap != 0) 580 printf("WARNING: could not fully configure devmap, error=%d\n", 581 err_devmap); 582 583 platform_late_init(); 584 585 root = OF_finddevice("/"); 586 if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) { 587 if (strcmp(LINUX_DTS_VERSION, dts_version) != 0) 588 printf("WARNING: DTB version is %s while kernel expects %s, " 589 "please update the DTB in the ESP\n", 590 dts_version, 591 LINUX_DTS_VERSION); 592 } else { 593 printf("WARNING: Cannot find freebsd,dts-version property, " 594 "cannot check DTB compliance\n"); 595 } 596 597 /* 598 * We must now clean the cache again.... 599 * Cleaning may be done by reading new data to displace any 600 * dirty data in the cache. This will have happened in cpu_setttb() 601 * but since we are boot strapping the addresses used for the read 602 * may have just been remapped and thus the cache could be out 603 * of sync. A re-clean after the switch will cure this. 604 * After booting there are no gross relocations of the kernel thus 605 * this problem will not occur after initarm(). 606 */ 607 /* Set stack for exception handlers */ 608 undefined_init(); 609 init_proc0(kernelstack); 610 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL); 611 enable_interrupts(PSR_A); 612 pmap_bootstrap(0); 613 614 /* Exclude the kernel (and all the things we allocated which immediately 615 * follow the kernel) from the VM allocation pool but not from crash 616 * dumps. virtual_avail is a global variable which tracks the kva we've 617 * "allocated" while setting up pmaps. 618 * 619 * Prepare the list of physical memory available to the vm subsystem. 620 */ 621 physmem_exclude_region(abp->abp_physaddr, 622 pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC); 623 physmem_init_kernel_globals(); 624 625 init_param2(physmem); 626 /* Init message buffer. */ 627 msgbufinit(msgbufp, msgbufsize); 628 dbg_monitor_init(); 629 arm_kdb_init(); 630 /* Apply possible BP hardening. */ 631 cpuinfo_init_bp_hardening(); 632 return ((void *)STACKALIGN(thread0.td_pcb)); 633 634 } 635 #endif /* FDT */ 636