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