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