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