1 /*- 2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org> 3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2003 Peter Wemm 5 * Copyright (c) 2008-2009 Jung-uk Kim <jkim@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/memrange.h> 39 #include <sys/smp.h> 40 #include <sys/types.h> 41 42 #include <vm/vm.h> 43 #include <vm/pmap.h> 44 45 #include <machine/intr_machdep.h> 46 #include <machine/pcb.h> 47 #include <machine/pmap.h> 48 #include <machine/specialreg.h> 49 #include <machine/vmparam.h> 50 51 #ifdef SMP 52 #include <machine/apicreg.h> 53 #include <machine/smp.h> 54 #endif 55 56 #include <contrib/dev/acpica/include/acpi.h> 57 58 #include <dev/acpica/acpivar.h> 59 60 #include "acpi_wakecode.h" 61 #include "acpi_wakedata.h" 62 63 /* Make sure the code is less than a page and leave room for the stack. */ 64 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024); 65 66 #ifndef _SYS_CDEFS_H_ 67 #error this file needs sys/cdefs.h as a prerequisite 68 #endif 69 70 extern int acpi_resume_beep; 71 extern int acpi_reset_video; 72 73 #ifdef SMP 74 extern struct xpcb *stopxpcbs; 75 #else 76 static struct xpcb *stopxpcbs; 77 #endif 78 79 int acpi_restorecpu(struct xpcb *, vm_offset_t); 80 int acpi_savecpu(struct xpcb *); 81 82 static void acpi_alloc_wakeup_handler(void); 83 static void acpi_stop_beep(void *); 84 85 #ifdef SMP 86 static int acpi_wakeup_ap(struct acpi_softc *, int); 87 static void acpi_wakeup_cpus(struct acpi_softc *, cpumask_t); 88 #endif 89 90 #define WAKECODE_VADDR(sc) ((sc)->acpi_wakeaddr + (3 * PAGE_SIZE)) 91 #define WAKECODE_PADDR(sc) ((sc)->acpi_wakephys + (3 * PAGE_SIZE)) 92 #define WAKECODE_FIXUP(offset, type, val) do { \ 93 type *addr; \ 94 addr = (type *)(WAKECODE_VADDR(sc) + offset); \ 95 *addr = val; \ 96 } while (0) 97 98 /* Turn off bits 1&2 of the PIT, stopping the beep. */ 99 static void 100 acpi_stop_beep(void *arg) 101 { 102 outb(0x61, inb(0x61) & ~0x3); 103 } 104 105 #ifdef SMP 106 static int 107 acpi_wakeup_ap(struct acpi_softc *sc, int cpu) 108 { 109 int vector = (WAKECODE_PADDR(sc) >> 12) & 0xff; 110 int apic_id = cpu_apic_ids[cpu]; 111 int ms; 112 113 WAKECODE_FIXUP(wakeup_xpcb, struct xpcb *, &stopxpcbs[cpu]); 114 WAKECODE_FIXUP(wakeup_gdt, uint16_t, stopxpcbs[cpu].xpcb_gdt.rd_limit); 115 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, 116 stopxpcbs[cpu].xpcb_gdt.rd_base); 117 WAKECODE_FIXUP(wakeup_cpu, int, cpu); 118 119 /* do an INIT IPI: assert RESET */ 120 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | 121 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id); 122 123 /* wait for pending status end */ 124 lapic_ipi_wait(-1); 125 126 /* do an INIT IPI: deassert RESET */ 127 lapic_ipi_raw(APIC_DEST_ALLESELF | APIC_TRIGMOD_LEVEL | 128 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, 0); 129 130 /* wait for pending status end */ 131 DELAY(10000); /* wait ~10mS */ 132 lapic_ipi_wait(-1); 133 134 /* 135 * next we do a STARTUP IPI: the previous INIT IPI might still be 136 * latched, (P5 bug) this 1st STARTUP would then terminate 137 * immediately, and the previously started INIT IPI would continue. OR 138 * the previous INIT IPI has already run. and this STARTUP IPI will 139 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI 140 * will run. 141 */ 142 143 /* do a STARTUP IPI */ 144 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | 145 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP | 146 vector, apic_id); 147 lapic_ipi_wait(-1); 148 DELAY(200); /* wait ~200uS */ 149 150 /* 151 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF 152 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR 153 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is 154 * recognized after hardware RESET or INIT IPI. 155 */ 156 157 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | 158 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP | 159 vector, apic_id); 160 lapic_ipi_wait(-1); 161 DELAY(200); /* wait ~200uS */ 162 163 /* Wait up to 5 seconds for it to start. */ 164 for (ms = 0; ms < 5000; ms++) { 165 if (*(int *)(WAKECODE_VADDR(sc) + wakeup_cpu) == 0) 166 return (1); /* return SUCCESS */ 167 DELAY(1000); 168 } 169 return (0); /* return FAILURE */ 170 } 171 172 #define WARMBOOT_TARGET 0 173 #define WARMBOOT_OFF (KERNBASE + 0x0467) 174 #define WARMBOOT_SEG (KERNBASE + 0x0469) 175 176 #define CMOS_REG (0x70) 177 #define CMOS_DATA (0x71) 178 #define BIOS_RESET (0x0f) 179 #define BIOS_WARM (0x0a) 180 181 static void 182 acpi_wakeup_cpus(struct acpi_softc *sc, cpumask_t wakeup_cpus) 183 { 184 uint32_t mpbioswarmvec; 185 cpumask_t map; 186 int cpu; 187 u_char mpbiosreason; 188 189 /* save the current value of the warm-start vector */ 190 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF); 191 outb(CMOS_REG, BIOS_RESET); 192 mpbiosreason = inb(CMOS_DATA); 193 194 /* setup a vector to our boot code */ 195 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET; 196 *((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4; 197 outb(CMOS_REG, BIOS_RESET); 198 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ 199 200 /* Wake up each AP. */ 201 for (cpu = 1; cpu < mp_ncpus; cpu++) { 202 map = 1ul << cpu; 203 if ((wakeup_cpus & map) != map) 204 continue; 205 if (acpi_wakeup_ap(sc, cpu) == 0) { 206 /* restore the warmstart vector */ 207 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec; 208 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)", 209 cpu, cpu_apic_ids[cpu]); 210 } 211 } 212 213 /* restore the warmstart vector */ 214 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec; 215 216 outb(CMOS_REG, BIOS_RESET); 217 outb(CMOS_DATA, mpbiosreason); 218 } 219 #endif 220 221 int 222 acpi_sleep_machdep(struct acpi_softc *sc, int state) 223 { 224 struct savefpu *stopfpu; 225 #ifdef SMP 226 cpumask_t wakeup_cpus; 227 #endif 228 register_t cr3, rf; 229 ACPI_STATUS status; 230 int ret; 231 232 ret = -1; 233 234 if (sc->acpi_wakeaddr == 0ul) 235 return (ret); 236 237 #ifdef SMP 238 wakeup_cpus = PCPU_GET(other_cpus); 239 #endif 240 241 AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc)); 242 243 rf = intr_disable(); 244 intr_suspend(); 245 246 /* 247 * Temporarily switch to the kernel pmap because it provides 248 * an identity mapping (setup at boot) for the low physical 249 * memory region containing the wakeup code. 250 */ 251 cr3 = rcr3(); 252 load_cr3(KPML4phys); 253 254 stopfpu = &stopxpcbs[0].xpcb_pcb.pcb_save; 255 if (acpi_savecpu(&stopxpcbs[0])) { 256 fpugetregs(curthread, stopfpu); 257 258 #ifdef SMP 259 if (wakeup_cpus != 0 && suspend_cpus(wakeup_cpus) == 0) { 260 device_printf(sc->acpi_dev, 261 "Failed to suspend APs: CPU mask = 0x%jx\n", 262 (uintmax_t)(wakeup_cpus & ~stopped_cpus)); 263 goto out; 264 } 265 #endif 266 267 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0)); 268 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0)); 269 270 WAKECODE_FIXUP(wakeup_xpcb, struct xpcb *, &stopxpcbs[0]); 271 WAKECODE_FIXUP(wakeup_gdt, uint16_t, 272 stopxpcbs[0].xpcb_gdt.rd_limit); 273 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, 274 stopxpcbs[0].xpcb_gdt.rd_base); 275 WAKECODE_FIXUP(wakeup_cpu, int, 0); 276 277 /* Call ACPICA to enter the desired sleep state */ 278 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) 279 status = AcpiEnterSleepStateS4bios(); 280 else 281 status = AcpiEnterSleepState(state); 282 283 if (status != AE_OK) { 284 device_printf(sc->acpi_dev, 285 "AcpiEnterSleepState failed - %s\n", 286 AcpiFormatException(status)); 287 goto out; 288 } 289 290 for (;;) 291 ia32_pause(); 292 } else { 293 fpusetregs(curthread, stopfpu); 294 #ifdef SMP 295 if (wakeup_cpus != 0) 296 acpi_wakeup_cpus(sc, wakeup_cpus); 297 #endif 298 acpi_resync_clock(sc); 299 ret = 0; 300 } 301 302 out: 303 #ifdef SMP 304 if (wakeup_cpus != 0) 305 restart_cpus(wakeup_cpus); 306 #endif 307 308 load_cr3(cr3); 309 intr_resume(); 310 intr_restore(rf); 311 312 AcpiSetFirmwareWakingVector(0); 313 314 if (ret == 0 && mem_range_softc.mr_op != NULL && 315 mem_range_softc.mr_op->reinit != NULL) 316 mem_range_softc.mr_op->reinit(&mem_range_softc); 317 318 /* If we beeped, turn it off after a delay. */ 319 if (acpi_resume_beep) 320 timeout(acpi_stop_beep, NULL, 3 * hz); 321 322 return (ret); 323 } 324 325 static vm_offset_t acpi_wakeaddr; 326 327 static void 328 acpi_alloc_wakeup_handler(void) 329 { 330 void *wakeaddr; 331 332 if (!cold) 333 return; 334 335 /* 336 * Specify the region for our wakeup code. We want it in the low 1 MB 337 * region, excluding video memory and above (0xa0000). We ask for 338 * it to be page-aligned, just to be safe. 339 */ 340 wakeaddr = contigmalloc(4 * PAGE_SIZE, M_DEVBUF, M_NOWAIT, 0, 0x9ffff, 341 PAGE_SIZE, 0ul); 342 if (wakeaddr == NULL) { 343 printf("%s: can't alloc wake memory\n", __func__); 344 return; 345 } 346 stopxpcbs = malloc(mp_ncpus * sizeof(*stopxpcbs), M_DEVBUF, M_NOWAIT); 347 if (stopxpcbs == NULL) { 348 contigfree(wakeaddr, 4 * PAGE_SIZE, M_DEVBUF); 349 printf("%s: can't alloc CPU state memory\n", __func__); 350 return; 351 } 352 acpi_wakeaddr = (vm_offset_t)wakeaddr; 353 } 354 355 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0); 356 357 void 358 acpi_install_wakeup_handler(struct acpi_softc *sc) 359 { 360 uint64_t *pt4, *pt3, *pt2; 361 int i; 362 363 if (acpi_wakeaddr == 0ul) 364 return; 365 366 sc->acpi_wakeaddr = acpi_wakeaddr; 367 sc->acpi_wakephys = vtophys(acpi_wakeaddr); 368 369 bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode)); 370 371 /* Patch GDT base address, ljmp targets and page table base address. */ 372 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t, 373 WAKECODE_PADDR(sc) + bootgdt); 374 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t, 375 WAKECODE_PADDR(sc) + wakeup_32); 376 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t, 377 WAKECODE_PADDR(sc) + wakeup_64); 378 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys); 379 380 /* Save pointers to some global data. */ 381 WAKECODE_FIXUP(wakeup_retaddr, void *, acpi_restorecpu); 382 WAKECODE_FIXUP(wakeup_kpml4, uint64_t, KPML4phys); 383 WAKECODE_FIXUP(wakeup_ctx, vm_offset_t, 384 WAKECODE_VADDR(sc) + wakeup_ctx); 385 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER)); 386 WAKECODE_FIXUP(wakeup_pat, uint64_t, rdmsr(MSR_PAT)); 387 WAKECODE_FIXUP(wakeup_star, uint64_t, rdmsr(MSR_STAR)); 388 WAKECODE_FIXUP(wakeup_lstar, uint64_t, rdmsr(MSR_LSTAR)); 389 WAKECODE_FIXUP(wakeup_cstar, uint64_t, rdmsr(MSR_CSTAR)); 390 WAKECODE_FIXUP(wakeup_sfmask, uint64_t, rdmsr(MSR_SF_MASK)); 391 392 /* Build temporary page tables below realmode code. */ 393 pt4 = (uint64_t *)acpi_wakeaddr; 394 pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t); 395 pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t); 396 397 /* Create the initial 1GB replicated page tables */ 398 for (i = 0; i < 512; i++) { 399 /* 400 * Each slot of the level 4 pages points 401 * to the same level 3 page 402 */ 403 pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE); 404 pt4[i] |= PG_V | PG_RW | PG_U; 405 406 /* 407 * Each slot of the level 3 pages points 408 * to the same level 2 page 409 */ 410 pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE)); 411 pt3[i] |= PG_V | PG_RW | PG_U; 412 413 /* The level 2 page slots are mapped with 2MB pages for 1GB. */ 414 pt2[i] = i * (2 * 1024 * 1024); 415 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U; 416 } 417 418 if (bootverbose) 419 device_printf(sc->acpi_dev, "wakeup code va %p pa %p\n", 420 (void *)sc->acpi_wakeaddr, (void *)sc->acpi_wakephys); 421 } 422