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