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