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 131 #ifdef __powerpc64__ 132 #include "mmu_oea64.h" 133 #endif 134 135 #ifndef __powerpc64__ 136 struct bat battable[16]; 137 #endif 138 139 #ifndef __powerpc64__ 140 /* Bits for running on 64-bit systems in 32-bit mode. */ 141 extern void *testppc64, *testppc64size; 142 extern void *restorebridge, *restorebridgesize; 143 extern void *rfid_patch, *rfi_patch1, *rfi_patch2; 144 extern void *trapcode64; 145 146 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; 147 #endif 148 149 extern void *rstcode, *rstcodeend; 150 extern void *trapcode, *trapcodeend; 151 extern void *hypertrapcode, *hypertrapcodeend; 152 extern void *generictrap, *generictrap64; 153 extern void *alitrap, *aliend; 154 extern void *dsitrap, *dsiend; 155 extern void *decrint, *decrsize; 156 extern void *extint, *extsize; 157 extern void *dblow, *dbend; 158 extern void *imisstrap, *imisssize; 159 extern void *dlmisstrap, *dlmisssize; 160 extern void *dsmisstrap, *dsmisssize; 161 162 extern void *ap_pcpu; 163 extern void __restartkernel(vm_offset_t, vm_offset_t, vm_offset_t, void *, uint32_t, register_t offset, register_t msr); 164 165 void aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, 166 void *mdp, uint32_t mdp_cookie); 167 void aim_cpu_init(vm_offset_t toc); 168 169 void 170 aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp, 171 uint32_t mdp_cookie) 172 { 173 register_t scratch; 174 175 /* 176 * If running from an FDT, make sure we are in real mode to avoid 177 * tromping on firmware page tables. Everything in the kernel assumes 178 * 1:1 mappings out of firmware, so this won't break anything not 179 * already broken. This doesn't work if there is live OF, since OF 180 * may internally use non-1:1 mappings. 181 */ 182 if (ofentry == 0) 183 mtmsr(mfmsr() & ~(PSL_IR | PSL_DR)); 184 185 #ifdef __powerpc64__ 186 /* 187 * If in real mode, relocate to high memory so that the kernel 188 * can execute from the direct map. 189 */ 190 if (!(mfmsr() & PSL_DR) && 191 (vm_offset_t)&aim_early_init < DMAP_BASE_ADDRESS) 192 __restartkernel(fdt, 0, ofentry, mdp, mdp_cookie, 193 DMAP_BASE_ADDRESS, mfmsr()); 194 #endif 195 196 /* Various very early CPU fix ups */ 197 switch (mfpvr() >> 16) { 198 /* 199 * PowerPC 970 CPUs have a misfeature requested by Apple that 200 * makes them pretend they have a 32-byte cacheline. Turn this 201 * off before we measure the cacheline size. 202 */ 203 case IBM970: 204 case IBM970FX: 205 case IBM970MP: 206 case IBM970GX: 207 scratch = mfspr(SPR_HID5); 208 scratch &= ~HID5_970_DCBZ_SIZE_HI; 209 mtspr(SPR_HID5, scratch); 210 break; 211 #ifdef __powerpc64__ 212 case IBMPOWER7: 213 case IBMPOWER7PLUS: 214 case IBMPOWER8: 215 case IBMPOWER8E: 216 case IBMPOWER9: 217 /* XXX: get from ibm,slb-size in device tree */ 218 n_slbs = 32; 219 break; 220 #endif 221 } 222 } 223 224 void 225 aim_cpu_init(vm_offset_t toc) 226 { 227 size_t trap_offset, trapsize; 228 vm_offset_t trap; 229 register_t msr; 230 uint8_t *cache_check; 231 int cacheline_warn; 232 #ifndef __powerpc64__ 233 register_t scratch; 234 int ppc64; 235 #endif 236 237 trap_offset = 0; 238 cacheline_warn = 0; 239 240 /* General setup for AIM CPUs */ 241 psl_kernset = PSL_EE | PSL_ME | PSL_IR | PSL_DR | PSL_RI; 242 243 #ifdef __powerpc64__ 244 psl_kernset |= PSL_SF; 245 if (mfmsr() & PSL_HV) 246 psl_kernset |= PSL_HV; 247 #endif 248 psl_userset = psl_kernset | PSL_PR; 249 #ifdef __powerpc64__ 250 psl_userset32 = psl_userset & ~PSL_SF; 251 #endif 252 253 /* Bits that users aren't allowed to change */ 254 psl_userstatic = ~(PSL_VEC | PSL_FP | PSL_FE0 | PSL_FE1); 255 /* 256 * Mask bits from the SRR1 that aren't really the MSR: 257 * Bits 1-4, 10-15 (ppc32), 33-36, 42-47 (ppc64) 258 */ 259 psl_userstatic &= ~0x783f0000UL; 260 261 /* 262 * Initialize the interrupt tables and figure out our cache line 263 * size and whether or not we need the 64-bit bridge code. 264 */ 265 266 /* 267 * Disable translation in case the vector area hasn't been 268 * mapped (G5). Note that no OFW calls can be made until 269 * translation is re-enabled. 270 */ 271 272 msr = mfmsr(); 273 mtmsr((msr & ~(PSL_IR | PSL_DR)) | PSL_RI); 274 275 /* 276 * Measure the cacheline size using dcbz 277 * 278 * Use EXC_PGM as a playground. We are about to overwrite it 279 * anyway, we know it exists, and we know it is cache-aligned. 280 */ 281 282 cache_check = (void *)EXC_PGM; 283 284 for (cacheline_size = 0; cacheline_size < 0x100; cacheline_size++) 285 cache_check[cacheline_size] = 0xff; 286 287 __asm __volatile("dcbz 0,%0":: "r" (cache_check) : "memory"); 288 289 /* Find the first byte dcbz did not zero to get the cache line size */ 290 for (cacheline_size = 0; cacheline_size < 0x100 && 291 cache_check[cacheline_size] == 0; cacheline_size++); 292 293 /* Work around psim bug */ 294 if (cacheline_size == 0) { 295 cacheline_warn = 1; 296 cacheline_size = 32; 297 } 298 299 #ifndef __powerpc64__ 300 /* 301 * Figure out whether we need to use the 64 bit PMAP. This works by 302 * executing an instruction that is only legal on 64-bit PPC (mtmsrd), 303 * and setting ppc64 = 0 if that causes a trap. 304 */ 305 306 ppc64 = 1; 307 308 bcopy(&testppc64, (void *)EXC_PGM, (size_t)&testppc64size); 309 __syncicache((void *)EXC_PGM, (size_t)&testppc64size); 310 311 __asm __volatile("\ 312 mfmsr %0; \ 313 mtsprg2 %1; \ 314 \ 315 mtmsrd %0; \ 316 mfsprg2 %1;" 317 : "=r"(scratch), "=r"(ppc64)); 318 319 if (ppc64) 320 cpu_features |= PPC_FEATURE_64; 321 322 /* 323 * Now copy restorebridge into all the handlers, if necessary, 324 * and set up the trap tables. 325 */ 326 327 if (cpu_features & PPC_FEATURE_64) { 328 /* Patch the two instances of rfi -> rfid */ 329 bcopy(&rfid_patch,&rfi_patch1,4); 330 #ifdef KDB 331 /* rfi_patch2 is at the end of dbleave */ 332 bcopy(&rfid_patch,&rfi_patch2,4); 333 #endif 334 } 335 #else /* powerpc64 */ 336 cpu_features |= PPC_FEATURE_64; 337 #endif 338 339 trapsize = (size_t)&trapcodeend - (size_t)&trapcode; 340 341 /* 342 * Copy generic handler into every possible trap. Special cases will get 343 * different ones in a minute. 344 */ 345 for (trap = EXC_RST; trap < EXC_LAST; trap += 0x20) 346 bcopy(&trapcode, (void *)trap, trapsize); 347 348 #ifndef __powerpc64__ 349 if (cpu_features & PPC_FEATURE_64) { 350 /* 351 * Copy a code snippet to restore 32-bit bridge mode 352 * to the top of every non-generic trap handler 353 */ 354 355 trap_offset += (size_t)&restorebridgesize; 356 bcopy(&restorebridge, (void *)EXC_RST, trap_offset); 357 bcopy(&restorebridge, (void *)EXC_DSI, trap_offset); 358 bcopy(&restorebridge, (void *)EXC_ALI, trap_offset); 359 bcopy(&restorebridge, (void *)EXC_PGM, trap_offset); 360 bcopy(&restorebridge, (void *)EXC_MCHK, trap_offset); 361 bcopy(&restorebridge, (void *)EXC_TRC, trap_offset); 362 bcopy(&restorebridge, (void *)EXC_BPT, trap_offset); 363 } 364 #else 365 trapsize = (size_t)&hypertrapcodeend - (size_t)&hypertrapcode; 366 bcopy(&hypertrapcode, (void *)(EXC_HEA + trap_offset), trapsize); 367 bcopy(&hypertrapcode, (void *)(EXC_HMI + trap_offset), trapsize); 368 bcopy(&hypertrapcode, (void *)(EXC_HVI + trap_offset), trapsize); 369 bcopy(&hypertrapcode, (void *)(EXC_SOFT_PATCH + trap_offset), trapsize); 370 #endif 371 372 bcopy(&rstcode, (void *)(EXC_RST + trap_offset), (size_t)&rstcodeend - 373 (size_t)&rstcode); 374 375 #ifdef KDB 376 bcopy(&dblow, (void *)(EXC_MCHK + trap_offset), (size_t)&dbend - 377 (size_t)&dblow); 378 bcopy(&dblow, (void *)(EXC_PGM + trap_offset), (size_t)&dbend - 379 (size_t)&dblow); 380 bcopy(&dblow, (void *)(EXC_TRC + trap_offset), (size_t)&dbend - 381 (size_t)&dblow); 382 bcopy(&dblow, (void *)(EXC_BPT + trap_offset), (size_t)&dbend - 383 (size_t)&dblow); 384 #endif 385 bcopy(&alitrap, (void *)(EXC_ALI + trap_offset), (size_t)&aliend - 386 (size_t)&alitrap); 387 bcopy(&dsitrap, (void *)(EXC_DSI + trap_offset), (size_t)&dsiend - 388 (size_t)&dsitrap); 389 390 #ifdef __powerpc64__ 391 /* Set TOC base so that the interrupt code can get at it */ 392 *((void **)TRAP_GENTRAP) = &generictrap; 393 *((register_t *)TRAP_TOCBASE) = toc; 394 #else 395 /* Set branch address for trap code */ 396 if (cpu_features & PPC_FEATURE_64) 397 *((void **)TRAP_GENTRAP) = &generictrap64; 398 else 399 *((void **)TRAP_GENTRAP) = &generictrap; 400 *((void **)TRAP_TOCBASE) = _GLOBAL_OFFSET_TABLE_; 401 402 /* G2-specific TLB miss helper handlers */ 403 bcopy(&imisstrap, (void *)EXC_IMISS, (size_t)&imisssize); 404 bcopy(&dlmisstrap, (void *)EXC_DLMISS, (size_t)&dlmisssize); 405 bcopy(&dsmisstrap, (void *)EXC_DSMISS, (size_t)&dsmisssize); 406 #endif 407 __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD); 408 409 /* 410 * Restore MSR 411 */ 412 mtmsr(msr); 413 414 /* Warn if cachline size was not determined */ 415 if (cacheline_warn == 1) { 416 printf("WARNING: cacheline size undetermined, setting to 32\n"); 417 } 418 419 /* 420 * Initialise virtual memory. Use BUS_PROBE_GENERIC priority 421 * in case the platform module had a better idea of what we 422 * should do. 423 */ 424 if (cpu_features & PPC_FEATURE_64) 425 pmap_mmu_install(MMU_TYPE_G5, BUS_PROBE_GENERIC); 426 else 427 pmap_mmu_install(MMU_TYPE_OEA, BUS_PROBE_GENERIC); 428 } 429 430 /* 431 * Shutdown the CPU as much as possible. 432 */ 433 void 434 cpu_halt(void) 435 { 436 437 OF_exit(); 438 } 439 440 int 441 ptrace_single_step(struct thread *td) 442 { 443 struct trapframe *tf; 444 445 tf = td->td_frame; 446 tf->srr1 |= PSL_SE; 447 448 return (0); 449 } 450 451 int 452 ptrace_clear_single_step(struct thread *td) 453 { 454 struct trapframe *tf; 455 456 tf = td->td_frame; 457 tf->srr1 &= ~PSL_SE; 458 459 return (0); 460 } 461 462 void 463 kdb_cpu_clear_singlestep(void) 464 { 465 466 kdb_frame->srr1 &= ~PSL_SE; 467 } 468 469 void 470 kdb_cpu_set_singlestep(void) 471 { 472 473 kdb_frame->srr1 |= PSL_SE; 474 } 475 476 /* 477 * Initialise a struct pcpu. 478 */ 479 void 480 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz) 481 { 482 #ifdef __powerpc64__ 483 /* Copy the SLB contents from the current CPU */ 484 memcpy(pcpu->pc_aim.slb, PCPU_GET(aim.slb), sizeof(pcpu->pc_aim.slb)); 485 #endif 486 } 487 488 #ifndef __powerpc64__ 489 uint64_t 490 va_to_vsid(pmap_t pm, vm_offset_t va) 491 { 492 return ((pm->pm_sr[(uintptr_t)va >> ADDR_SR_SHFT]) & SR_VSID_MASK); 493 } 494 495 #endif 496 497 /* 498 * These functions need to provide addresses that both (a) work in real mode 499 * (or whatever mode/circumstances the kernel is in in early boot (now)) and 500 * (b) can still, in principle, work once the kernel is going. Because these 501 * rely on existing mappings/real mode, unmap is a no-op. 502 */ 503 vm_offset_t 504 pmap_early_io_map(vm_paddr_t pa, vm_size_t size) 505 { 506 KASSERT(!pmap_bootstrapped, ("Not available after PMAP started!")); 507 508 /* 509 * If we have the MMU up in early boot, assume it is 1:1. Otherwise, 510 * try to get the address in a memory region compatible with the 511 * direct map for efficiency later. 512 */ 513 if (mfmsr() & PSL_DR) 514 return (pa); 515 else 516 return (DMAP_BASE_ADDRESS + pa); 517 } 518 519 void 520 pmap_early_io_unmap(vm_offset_t va, vm_size_t size) 521 { 522 523 KASSERT(!pmap_bootstrapped, ("Not available after PMAP started!")); 524 } 525 526 /* From p3-53 of the MPC7450 RISC Microprocessor Family Reference Manual */ 527 void 528 flush_disable_caches(void) 529 { 530 register_t msr; 531 register_t msscr0; 532 register_t cache_reg; 533 volatile uint32_t *memp; 534 uint32_t temp; 535 int i; 536 int x; 537 538 msr = mfmsr(); 539 powerpc_sync(); 540 mtmsr(msr & ~(PSL_EE | PSL_DR)); 541 msscr0 = mfspr(SPR_MSSCR0); 542 msscr0 &= ~MSSCR0_L2PFE; 543 mtspr(SPR_MSSCR0, msscr0); 544 powerpc_sync(); 545 isync(); 546 __asm__ __volatile__("dssall; sync"); 547 powerpc_sync(); 548 isync(); 549 __asm__ __volatile__("dcbf 0,%0" :: "r"(0)); 550 __asm__ __volatile__("dcbf 0,%0" :: "r"(0)); 551 __asm__ __volatile__("dcbf 0,%0" :: "r"(0)); 552 553 /* Lock the L1 Data cache. */ 554 mtspr(SPR_LDSTCR, mfspr(SPR_LDSTCR) | 0xFF); 555 powerpc_sync(); 556 isync(); 557 558 mtspr(SPR_LDSTCR, 0); 559 560 /* 561 * Perform this in two stages: Flush the cache starting in RAM, then do it 562 * from ROM. 563 */ 564 memp = (volatile uint32_t *)0x00000000; 565 for (i = 0; i < 128 * 1024; i++) { 566 temp = *memp; 567 __asm__ __volatile__("dcbf 0,%0" :: "r"(memp)); 568 memp += 32/sizeof(*memp); 569 } 570 571 memp = (volatile uint32_t *)0xfff00000; 572 x = 0xfe; 573 574 for (; x != 0xff;) { 575 mtspr(SPR_LDSTCR, x); 576 for (i = 0; i < 128; i++) { 577 temp = *memp; 578 __asm__ __volatile__("dcbf 0,%0" :: "r"(memp)); 579 memp += 32/sizeof(*memp); 580 } 581 x = ((x << 1) | 1) & 0xff; 582 } 583 mtspr(SPR_LDSTCR, 0); 584 585 cache_reg = mfspr(SPR_L2CR); 586 if (cache_reg & L2CR_L2E) { 587 cache_reg &= ~(L2CR_L2IO_7450 | L2CR_L2DO_7450); 588 mtspr(SPR_L2CR, cache_reg); 589 powerpc_sync(); 590 mtspr(SPR_L2CR, cache_reg | L2CR_L2HWF); 591 while (mfspr(SPR_L2CR) & L2CR_L2HWF) 592 ; /* Busy wait for cache to flush */ 593 powerpc_sync(); 594 cache_reg &= ~L2CR_L2E; 595 mtspr(SPR_L2CR, cache_reg); 596 powerpc_sync(); 597 mtspr(SPR_L2CR, cache_reg | L2CR_L2I); 598 powerpc_sync(); 599 while (mfspr(SPR_L2CR) & L2CR_L2I) 600 ; /* Busy wait for L2 cache invalidate */ 601 powerpc_sync(); 602 } 603 604 cache_reg = mfspr(SPR_L3CR); 605 if (cache_reg & L3CR_L3E) { 606 cache_reg &= ~(L3CR_L3IO | L3CR_L3DO); 607 mtspr(SPR_L3CR, cache_reg); 608 powerpc_sync(); 609 mtspr(SPR_L3CR, cache_reg | L3CR_L3HWF); 610 while (mfspr(SPR_L3CR) & L3CR_L3HWF) 611 ; /* Busy wait for cache to flush */ 612 powerpc_sync(); 613 cache_reg &= ~L3CR_L3E; 614 mtspr(SPR_L3CR, cache_reg); 615 powerpc_sync(); 616 mtspr(SPR_L3CR, cache_reg | L3CR_L3I); 617 powerpc_sync(); 618 while (mfspr(SPR_L3CR) & L3CR_L3I) 619 ; /* Busy wait for L3 cache invalidate */ 620 powerpc_sync(); 621 } 622 623 mtspr(SPR_HID0, mfspr(SPR_HID0) & ~HID0_DCE); 624 powerpc_sync(); 625 isync(); 626 627 mtmsr(msr); 628 } 629 630 void 631 cpu_sleep() 632 { 633 static u_quad_t timebase = 0; 634 static register_t sprgs[4]; 635 static register_t srrs[2]; 636 637 jmp_buf resetjb; 638 struct thread *fputd; 639 struct thread *vectd; 640 register_t hid0; 641 register_t msr; 642 register_t saved_msr; 643 644 ap_pcpu = pcpup; 645 646 PCPU_SET(restore, &resetjb); 647 648 saved_msr = mfmsr(); 649 fputd = PCPU_GET(fputhread); 650 vectd = PCPU_GET(vecthread); 651 if (fputd != NULL) 652 save_fpu(fputd); 653 if (vectd != NULL) 654 save_vec(vectd); 655 if (setjmp(resetjb) == 0) { 656 sprgs[0] = mfspr(SPR_SPRG0); 657 sprgs[1] = mfspr(SPR_SPRG1); 658 sprgs[2] = mfspr(SPR_SPRG2); 659 sprgs[3] = mfspr(SPR_SPRG3); 660 srrs[0] = mfspr(SPR_SRR0); 661 srrs[1] = mfspr(SPR_SRR1); 662 timebase = mftb(); 663 powerpc_sync(); 664 flush_disable_caches(); 665 hid0 = mfspr(SPR_HID0); 666 hid0 = (hid0 & ~(HID0_DOZE | HID0_NAP)) | HID0_SLEEP; 667 powerpc_sync(); 668 isync(); 669 msr = mfmsr() | PSL_POW; 670 mtspr(SPR_HID0, hid0); 671 powerpc_sync(); 672 673 while (1) 674 mtmsr(msr); 675 } 676 platform_smp_timebase_sync(timebase, 0); 677 PCPU_SET(curthread, curthread); 678 PCPU_SET(curpcb, curthread->td_pcb); 679 pmap_activate(curthread); 680 powerpc_sync(); 681 mtspr(SPR_SPRG0, sprgs[0]); 682 mtspr(SPR_SPRG1, sprgs[1]); 683 mtspr(SPR_SPRG2, sprgs[2]); 684 mtspr(SPR_SPRG3, sprgs[3]); 685 mtspr(SPR_SRR0, srrs[0]); 686 mtspr(SPR_SRR1, srrs[1]); 687 mtmsr(saved_msr); 688 if (fputd == curthread) 689 enable_fpu(curthread); 690 if (vectd == curthread) 691 enable_vec(curthread); 692 powerpc_sync(); 693 } 694 695