1 /*- 2 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 3 * Copyright (C) 1995, 1996 TooLs GmbH. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by TooLs GmbH. 17 * 4. The name of TooLs GmbH may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /*- 32 * Copyright (C) 2001 Benno Rice 33 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 54 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $ 55 */ 56 57 #include <sys/cdefs.h> 58 __FBSDID("$FreeBSD$"); 59 60 #include "opt_ddb.h" 61 #include "opt_kstack_pages.h" 62 #include "opt_platform.h" 63 64 #include <sys/param.h> 65 #include <sys/proc.h> 66 #include <sys/systm.h> 67 #include <sys/bio.h> 68 #include <sys/buf.h> 69 #include <sys/bus.h> 70 #include <sys/cons.h> 71 #include <sys/cpu.h> 72 #include <sys/eventhandler.h> 73 #include <sys/exec.h> 74 #include <sys/imgact.h> 75 #include <sys/kdb.h> 76 #include <sys/kernel.h> 77 #include <sys/ktr.h> 78 #include <sys/linker.h> 79 #include <sys/lock.h> 80 #include <sys/malloc.h> 81 #include <sys/mbuf.h> 82 #include <sys/msgbuf.h> 83 #include <sys/mutex.h> 84 #include <sys/ptrace.h> 85 #include <sys/reboot.h> 86 #include <sys/rwlock.h> 87 #include <sys/signalvar.h> 88 #include <sys/syscallsubr.h> 89 #include <sys/sysctl.h> 90 #include <sys/sysent.h> 91 #include <sys/sysproto.h> 92 #include <sys/ucontext.h> 93 #include <sys/uio.h> 94 #include <sys/vmmeter.h> 95 #include <sys/vnode.h> 96 97 #include <net/netisr.h> 98 99 #include <vm/vm.h> 100 #include <vm/vm_extern.h> 101 #include <vm/vm_kern.h> 102 #include <vm/vm_page.h> 103 #include <vm/vm_map.h> 104 #include <vm/vm_object.h> 105 #include <vm/vm_pager.h> 106 107 #include <machine/altivec.h> 108 #ifndef __powerpc64__ 109 #include <machine/bat.h> 110 #endif 111 #include <machine/cpu.h> 112 #include <machine/elf.h> 113 #include <machine/fpu.h> 114 #include <machine/hid.h> 115 #include <machine/kdb.h> 116 #include <machine/md_var.h> 117 #include <machine/metadata.h> 118 #include <machine/mmuvar.h> 119 #include <machine/pcb.h> 120 #include <machine/reg.h> 121 #include <machine/sigframe.h> 122 #include <machine/spr.h> 123 #include <machine/trap.h> 124 #include <machine/vmparam.h> 125 #include <machine/ofw_machdep.h> 126 127 #include <ddb/ddb.h> 128 129 #include <dev/ofw/openfirm.h> 130 #include <dev/ofw/ofw_subr.h> 131 132 int cold = 1; 133 #ifdef __powerpc64__ 134 int cacheline_size = 128; 135 #else 136 int cacheline_size = 32; 137 #endif 138 int hw_direct_map = 1; 139 140 #ifdef BOOKE 141 extern vm_paddr_t kernload; 142 #endif 143 144 extern void *ap_pcpu; 145 146 struct pcpu __pcpu[MAXCPU]; 147 static char init_kenv[2048]; 148 149 static struct trapframe frame0; 150 151 char machine[] = "powerpc"; 152 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, ""); 153 154 static void cpu_startup(void *); 155 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL); 156 157 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size, 158 CTLFLAG_RD, &cacheline_size, 0, ""); 159 160 uintptr_t powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *, 161 uint32_t); 162 163 long Maxmem = 0; 164 long realmem = 0; 165 166 /* Default MSR values set in the AIM/Book-E early startup code */ 167 register_t psl_kernset; 168 register_t psl_userset; 169 register_t psl_userstatic; 170 #ifdef __powerpc64__ 171 register_t psl_userset32; 172 #endif 173 174 struct kva_md_info kmi; 175 176 static void 177 cpu_startup(void *dummy) 178 { 179 180 /* 181 * Initialise the decrementer-based clock. 182 */ 183 decr_init(); 184 185 /* 186 * Good {morning,afternoon,evening,night}. 187 */ 188 cpu_setup(PCPU_GET(cpuid)); 189 190 #ifdef PERFMON 191 perfmon_init(); 192 #endif 193 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)physmem), 194 ptoa((uintmax_t)physmem) / 1048576); 195 realmem = physmem; 196 197 if (bootverbose) 198 printf("available KVA = %zu (%zu MB)\n", 199 virtual_end - virtual_avail, 200 (virtual_end - virtual_avail) / 1048576); 201 202 /* 203 * Display any holes after the first chunk of extended memory. 204 */ 205 if (bootverbose) { 206 int indx; 207 208 printf("Physical memory chunk(s):\n"); 209 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 210 vm_paddr_t size1 = 211 phys_avail[indx + 1] - phys_avail[indx]; 212 213 #ifdef __powerpc64__ 214 printf("0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n", 215 #else 216 printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n", 217 #endif 218 (uintmax_t)phys_avail[indx], 219 (uintmax_t)phys_avail[indx + 1] - 1, 220 (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE); 221 } 222 } 223 224 vm_ksubmap_init(&kmi); 225 226 printf("avail memory = %ju (%ju MB)\n", 227 ptoa((uintmax_t)vm_free_count()), 228 ptoa((uintmax_t)vm_free_count()) / 1048576); 229 230 /* 231 * Set up buffers, so they can be used to read disk labels. 232 */ 233 bufinit(); 234 vm_pager_bufferinit(); 235 } 236 237 extern vm_offset_t __startkernel, __endkernel; 238 extern unsigned char __bss_start[]; 239 extern unsigned char __sbss_start[]; 240 extern unsigned char __sbss_end[]; 241 extern unsigned char _end[]; 242 243 void aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, 244 void *mdp, uint32_t mdp_cookie); 245 void aim_cpu_init(vm_offset_t toc); 246 void booke_cpu_init(void); 247 248 uintptr_t 249 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp, 250 uint32_t mdp_cookie) 251 { 252 struct pcpu *pc; 253 struct cpuref bsp; 254 vm_offset_t startkernel, endkernel; 255 char *env; 256 bool ofw_bootargs = false; 257 #ifdef DDB 258 vm_offset_t ksym_start; 259 vm_offset_t ksym_end; 260 #endif 261 262 /* First guess at start/end kernel positions */ 263 startkernel = __startkernel; 264 endkernel = __endkernel; 265 266 /* 267 * If the metadata pointer cookie is not set to the magic value, 268 * the number in mdp should be treated as nonsense. 269 */ 270 if (mdp_cookie != 0xfb5d104d) 271 mdp = NULL; 272 273 #if !defined(BOOKE) 274 /* 275 * On BOOKE the BSS is already cleared and some variables 276 * initialized. Do not wipe them out. 277 */ 278 bzero(__sbss_start, __sbss_end - __sbss_start); 279 bzero(__bss_start, _end - __bss_start); 280 #endif 281 282 cpu_feature_setup(); 283 284 #ifdef AIM 285 aim_early_init(fdt, toc, ofentry, mdp, mdp_cookie); 286 #endif 287 288 /* 289 * Parse metadata if present and fetch parameters. Must be done 290 * before console is inited so cninit gets the right value of 291 * boothowto. 292 */ 293 if (mdp != NULL) { 294 void *kmdp = NULL; 295 char *envp = NULL; 296 uintptr_t md_offset = 0; 297 vm_paddr_t kernelendphys; 298 299 #ifdef AIM 300 if ((uintptr_t)&powerpc_init > DMAP_BASE_ADDRESS) 301 md_offset = DMAP_BASE_ADDRESS; 302 #else /* BOOKE */ 303 md_offset = VM_MIN_KERNEL_ADDRESS - kernload; 304 #endif 305 306 preload_metadata = mdp; 307 if (md_offset > 0) { 308 preload_metadata += md_offset; 309 preload_bootstrap_relocate(md_offset); 310 } 311 kmdp = preload_search_by_type("elf kernel"); 312 if (kmdp != NULL) { 313 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 314 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 315 if (envp != NULL) 316 envp += md_offset; 317 init_static_kenv(envp, 0); 318 if (fdt == 0) { 319 fdt = MD_FETCH(kmdp, MODINFOMD_DTBP, uintptr_t); 320 if (fdt != 0) 321 fdt += md_offset; 322 } 323 kernelendphys = MD_FETCH(kmdp, MODINFOMD_KERNEND, 324 vm_offset_t); 325 if (kernelendphys != 0) 326 kernelendphys += md_offset; 327 endkernel = ulmax(endkernel, kernelendphys); 328 #ifdef DDB 329 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t); 330 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t); 331 db_fetch_ksymtab(ksym_start, ksym_end); 332 #endif 333 } 334 } else { 335 init_static_kenv(init_kenv, sizeof(init_kenv)); 336 ofw_bootargs = true; 337 } 338 /* Store boot environment state */ 339 OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry); 340 341 /* 342 * Init params/tunables that can be overridden by the loader 343 */ 344 init_param1(); 345 346 /* 347 * Start initializing proc0 and thread0. 348 */ 349 proc_linkup0(&proc0, &thread0); 350 thread0.td_frame = &frame0; 351 #ifdef __powerpc64__ 352 __asm __volatile("mr 13,%0" :: "r"(&thread0)); 353 #else 354 __asm __volatile("mr 2,%0" :: "r"(&thread0)); 355 #endif 356 357 /* 358 * Init mutexes, which we use heavily in PMAP 359 */ 360 mutex_init(); 361 362 /* 363 * Install the OF client interface 364 */ 365 OF_bootstrap(); 366 367 if (ofw_bootargs) 368 ofw_parse_bootargs(); 369 370 /* 371 * Initialize the console before printing anything. 372 */ 373 cninit(); 374 375 #ifdef AIM 376 aim_cpu_init(toc); 377 #else /* BOOKE */ 378 booke_cpu_init(); 379 380 /* Make sure the kernel icache is valid before we go too much further */ 381 __syncicache((caddr_t)startkernel, endkernel - startkernel); 382 #endif 383 384 /* 385 * Choose a platform module so we can get the physical memory map. 386 */ 387 388 platform_probe_and_attach(); 389 390 /* 391 * Set up per-cpu data for the BSP now that the platform can tell 392 * us which that is. 393 */ 394 if (platform_smp_get_bsp(&bsp) != 0) 395 bsp.cr_cpuid = 0; 396 pc = &__pcpu[bsp.cr_cpuid]; 397 __asm __volatile("mtsprg 0, %0" :: "r"(pc)); 398 pcpu_init(pc, bsp.cr_cpuid, sizeof(struct pcpu)); 399 pc->pc_curthread = &thread0; 400 thread0.td_oncpu = bsp.cr_cpuid; 401 pc->pc_cpuid = bsp.cr_cpuid; 402 pc->pc_hwref = bsp.cr_hwref; 403 404 /* 405 * Init KDB 406 */ 407 kdb_init(); 408 409 /* 410 * Bring up MMU 411 */ 412 pmap_bootstrap(startkernel, endkernel); 413 mtmsr(psl_kernset & ~PSL_EE); 414 415 /* 416 * Initialize params/tunables that are derived from memsize 417 */ 418 init_param2(physmem); 419 420 /* 421 * Grab booted kernel's name 422 */ 423 env = kern_getenv("kernelname"); 424 if (env != NULL) { 425 strlcpy(kernelname, env, sizeof(kernelname)); 426 freeenv(env); 427 } 428 429 /* 430 * Finish setting up thread0. 431 */ 432 thread0.td_pcb = (struct pcb *) 433 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE - 434 sizeof(struct pcb)) & ~15UL); 435 bzero((void *)thread0.td_pcb, sizeof(struct pcb)); 436 pc->pc_curpcb = thread0.td_pcb; 437 438 /* Initialise the message buffer. */ 439 msgbufinit(msgbufp, msgbufsize); 440 441 #ifdef KDB 442 if (boothowto & RB_KDB) 443 kdb_enter(KDB_WHY_BOOTFLAGS, 444 "Boot flags requested debugger"); 445 #endif 446 447 return (((uintptr_t)thread0.td_pcb - 448 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL); 449 } 450 451 /* 452 * Flush the D-cache for non-DMA I/O so that the I-cache can 453 * be made coherent later. 454 */ 455 void 456 cpu_flush_dcache(void *ptr, size_t len) 457 { 458 register_t addr, off; 459 460 /* 461 * Align the address to a cacheline and adjust the length 462 * accordingly. Then round the length to a multiple of the 463 * cacheline for easy looping. 464 */ 465 addr = (uintptr_t)ptr; 466 off = addr & (cacheline_size - 1); 467 addr -= off; 468 len = roundup2(len + off, cacheline_size); 469 470 while (len > 0) { 471 __asm __volatile ("dcbf 0,%0" :: "r"(addr)); 472 __asm __volatile ("sync"); 473 addr += cacheline_size; 474 len -= cacheline_size; 475 } 476 } 477 478 int 479 ptrace_set_pc(struct thread *td, unsigned long addr) 480 { 481 struct trapframe *tf; 482 483 tf = td->td_frame; 484 tf->srr0 = (register_t)addr; 485 486 return (0); 487 } 488 489 void 490 spinlock_enter(void) 491 { 492 struct thread *td; 493 register_t msr; 494 495 td = curthread; 496 if (td->td_md.md_spinlock_count == 0) { 497 __asm __volatile("or 2,2,2"); /* Set high thread priority */ 498 msr = intr_disable(); 499 td->td_md.md_spinlock_count = 1; 500 td->td_md.md_saved_msr = msr; 501 } else 502 td->td_md.md_spinlock_count++; 503 critical_enter(); 504 } 505 506 void 507 spinlock_exit(void) 508 { 509 struct thread *td; 510 register_t msr; 511 512 td = curthread; 513 critical_exit(); 514 msr = td->td_md.md_saved_msr; 515 td->td_md.md_spinlock_count--; 516 if (td->td_md.md_spinlock_count == 0) { 517 intr_restore(msr); 518 __asm __volatile("or 6,6,6"); /* Set normal thread priority */ 519 } 520 } 521 522 /* 523 * Simple ddb(4) command/hack to view any SPR on the running CPU. 524 * Uses a trivial asm function to perform the mfspr, and rewrites the mfspr 525 * instruction each time. 526 * XXX: Since it uses code modification, it won't work if the kernel code pages 527 * are marked RO. 528 */ 529 extern register_t get_spr(int); 530 531 #ifdef DDB 532 DB_SHOW_COMMAND(spr, db_show_spr) 533 { 534 register_t spr; 535 volatile uint32_t *p; 536 int sprno, saved_sprno; 537 538 if (!have_addr) 539 return; 540 541 saved_sprno = sprno = (intptr_t) addr; 542 sprno = ((sprno & 0x3e0) >> 5) | ((sprno & 0x1f) << 5); 543 p = (uint32_t *)(void *)&get_spr; 544 #if defined(_CALL_ELF) && _CALL_ELF == 2 545 /* Account for ELFv2 function prologue. */ 546 p += 2; 547 #endif 548 *p = (*p & ~0x001ff800) | (sprno << 11); 549 __syncicache(get_spr, cacheline_size); 550 spr = get_spr(sprno); 551 552 db_printf("SPR %d(%x): %lx\n", saved_sprno, saved_sprno, 553 (unsigned long)spr); 554 } 555 #endif 556 557 #undef bzero 558 void 559 bzero(void *buf, size_t len) 560 { 561 caddr_t p; 562 563 p = buf; 564 565 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) { 566 *p++ = 0; 567 len--; 568 } 569 570 while (len >= sizeof(u_long) * 8) { 571 *(u_long*) p = 0; 572 *((u_long*) p + 1) = 0; 573 *((u_long*) p + 2) = 0; 574 *((u_long*) p + 3) = 0; 575 len -= sizeof(u_long) * 8; 576 *((u_long*) p + 4) = 0; 577 *((u_long*) p + 5) = 0; 578 *((u_long*) p + 6) = 0; 579 *((u_long*) p + 7) = 0; 580 p += sizeof(u_long) * 8; 581 } 582 583 while (len >= sizeof(u_long)) { 584 *(u_long*) p = 0; 585 len -= sizeof(u_long); 586 p += sizeof(u_long); 587 } 588 589 while (len) { 590 *p++ = 0; 591 len--; 592 } 593 } 594