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