acpi_wakeup.c revision 236938
1/*- 2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org> 3 * Copyright (c) 2001-2012 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2003 Peter Wemm 5 * Copyright (c) 2008-2012 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: head/sys/x86/acpica/acpi_wakeup.c 236938 2012-06-12 00:14:54Z iwasaki $"); 32 33#include <sys/param.h> 34#include <sys/bus.h> 35#include <sys/eventhandler.h> 36#include <sys/kernel.h> 37#include <sys/malloc.h> 38#include <sys/memrange.h> 39#include <sys/smp.h> 40 41#include <vm/vm.h> 42#include <vm/pmap.h> 43 44#include <machine/clock.h> 45#include <machine/intr_machdep.h> 46#include <x86/mca.h> 47#include <machine/pcb.h> 48#include <machine/pmap.h> 49#include <machine/specialreg.h> 50#include <machine/md_var.h> 51 52#ifdef SMP 53#include <x86/apicreg.h> 54#include <machine/smp.h> 55#include <machine/vmparam.h> 56#endif 57 58#include <contrib/dev/acpica/include/acpi.h> 59 60#include <dev/acpica/acpivar.h> 61 62#include "acpi_wakecode.h" 63#include "acpi_wakedata.h" 64 65/* Make sure the code is less than a page and leave room for the stack. */ 66CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024); 67 68extern int acpi_resume_beep; 69extern int acpi_reset_video; 70 71#ifdef SMP 72extern struct pcb **susppcbs; 73static cpuset_t suspcpus; 74#else 75static struct pcb **susppcbs; 76#endif 77 78static void *acpi_alloc_wakeup_handler(void); 79static void acpi_stop_beep(void *); 80 81#ifdef SMP 82static int acpi_wakeup_ap(struct acpi_softc *, int); 83static void acpi_wakeup_cpus(struct acpi_softc *); 84#endif 85 86#ifdef __amd64__ 87#define ACPI_PAGETABLES 3 88#else 89#define ACPI_PAGETABLES 0 90#endif 91 92#define WAKECODE_VADDR(sc) ((sc)->acpi_wakeaddr + (ACPI_PAGETABLES * PAGE_SIZE)) 93#define WAKECODE_PADDR(sc) ((sc)->acpi_wakephys + (ACPI_PAGETABLES * PAGE_SIZE)) 94#define WAKECODE_FIXUP(offset, type, val) do { \ 95 type *addr; \ 96 addr = (type *)(WAKECODE_VADDR(sc) + offset); \ 97 *addr = val; \ 98} while (0) 99 100static void 101acpi_stop_beep(void *arg) 102{ 103 104 if (acpi_resume_beep != 0) 105 timer_spkr_release(); 106} 107 108#ifdef SMP 109static int 110acpi_wakeup_ap(struct acpi_softc *sc, int cpu) 111{ 112 int vector = (WAKECODE_PADDR(sc) >> 12) & 0xff; 113 int apic_id = cpu_apic_ids[cpu]; 114 int ms; 115 116 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[cpu]); 117 WAKECODE_FIXUP(wakeup_gdt, uint16_t, susppcbs[cpu]->pcb_gdt.rd_limit); 118 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, 119 susppcbs[cpu]->pcb_gdt.rd_base); 120 121 ipi_startup(apic_id, vector); 122 123 /* Wait up to 5 seconds for it to resume. */ 124 for (ms = 0; ms < 5000; ms++) { 125 if (!CPU_ISSET(cpu, &suspended_cpus)) 126 return (1); /* return SUCCESS */ 127 DELAY(1000); 128 } 129 return (0); /* return FAILURE */ 130} 131 132#define WARMBOOT_TARGET 0 133#define WARMBOOT_OFF (KERNBASE + 0x0467) 134#define WARMBOOT_SEG (KERNBASE + 0x0469) 135 136#define CMOS_REG (0x70) 137#define CMOS_DATA (0x71) 138#define BIOS_RESET (0x0f) 139#define BIOS_WARM (0x0a) 140 141static void 142acpi_wakeup_cpus(struct acpi_softc *sc) 143{ 144 uint32_t mpbioswarmvec; 145 int cpu; 146 u_char mpbiosreason; 147 148 /* save the current value of the warm-start vector */ 149 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF); 150 outb(CMOS_REG, BIOS_RESET); 151 mpbiosreason = inb(CMOS_DATA); 152 153 /* setup a vector to our boot code */ 154 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET; 155 *((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4; 156 outb(CMOS_REG, BIOS_RESET); 157 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ 158 159 /* Wake up each AP. */ 160 for (cpu = 1; cpu < mp_ncpus; cpu++) { 161 if (!CPU_ISSET(cpu, &suspcpus)) 162 continue; 163 if (acpi_wakeup_ap(sc, cpu) == 0) { 164 /* restore the warmstart vector */ 165 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec; 166 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)", 167 cpu, cpu_apic_ids[cpu]); 168 } 169 } 170 171 /* restore the warmstart vector */ 172 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec; 173 174 outb(CMOS_REG, BIOS_RESET); 175 outb(CMOS_DATA, mpbiosreason); 176} 177#endif 178 179int 180acpi_sleep_machdep(struct acpi_softc *sc, int state) 181{ 182 ACPI_STATUS status; 183 184 if (sc->acpi_wakeaddr == 0ul) 185 return (-1); /* couldn't alloc wake memory */ 186 187#ifdef SMP 188 suspcpus = all_cpus; 189 CPU_CLR(PCPU_GET(cpuid), &suspcpus); 190#endif 191 192 if (acpi_resume_beep != 0) 193 timer_spkr_acquire(); 194 195 AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc)); 196 197 intr_suspend(); 198 199 if (savectx(susppcbs[0])) { 200#ifdef __amd64__ 201 ctx_fpusave(susppcbs[0]->pcb_fpususpend); 202#endif 203#ifdef SMP 204 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) { 205 device_printf(sc->acpi_dev, "Failed to suspend APs\n"); 206 return (0); /* couldn't sleep */ 207 } 208#endif 209 210 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0)); 211 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0)); 212 213 WAKECODE_FIXUP(wakeup_cr4, register_t, susppcbs[0]->pcb_cr4); 214 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[0]); 215 WAKECODE_FIXUP(wakeup_gdt, uint16_t, 216 susppcbs[0]->pcb_gdt.rd_limit); 217 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, 218 susppcbs[0]->pcb_gdt.rd_base); 219 220 /* Call ACPICA to enter the desired sleep state */ 221 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) 222 status = AcpiEnterSleepStateS4bios(); 223 else 224 status = AcpiEnterSleepState(state, acpi_sleep_flags); 225 if (ACPI_FAILURE(status)) { 226 device_printf(sc->acpi_dev, 227 "AcpiEnterSleepState failed - %s\n", 228 AcpiFormatException(status)); 229 return (0); /* couldn't sleep */ 230 } 231 232 for (;;) 233 ia32_pause(); 234 } 235 236 return (1); /* wakeup successfully */ 237} 238 239int 240acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result, 241 int intr_enabled) 242{ 243 244 if (sleep_result == -1) 245 return (sleep_result); 246 247 if (!intr_enabled) { 248 /* Wakeup MD procedures in interrupt disabled context */ 249 if (sleep_result == 1) { 250 pmap_init_pat(); 251#if 0 252 load_cr3(susppcbs[0]->pcb_cr3); 253#endif 254 initializecpu(); 255 PCPU_SET(switchtime, 0); 256 PCPU_SET(switchticks, ticks); 257#ifdef SMP 258 if (!CPU_EMPTY(&suspcpus)) 259 acpi_wakeup_cpus(sc); 260#endif 261 } 262 263#ifdef SMP 264 if (!CPU_EMPTY(&suspcpus)) 265 restart_cpus(suspcpus); 266#endif 267 mca_resume(); 268 intr_resume(); 269 270 AcpiSetFirmwareWakingVector(0); 271 } else { 272 /* Wakeup MD procedures in interrupt enabled context */ 273 if (sleep_result == 1 && mem_range_softc.mr_op != NULL && 274 mem_range_softc.mr_op->reinit != NULL) 275 mem_range_softc.mr_op->reinit(&mem_range_softc); 276 } 277 278 return (sleep_result); 279} 280 281static void * 282acpi_alloc_wakeup_handler(void) 283{ 284 void *wakeaddr; 285 int i; 286 287 /* 288 * Specify the region for our wakeup code. We want it in the low 1 MB 289 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA 290 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT), 291 * and ROM area (0xa0000 and above). The temporary page tables must be 292 * page-aligned. 293 */ 294 wakeaddr = contigmalloc((ACPI_PAGETABLES + 1) * PAGE_SIZE, M_DEVBUF, 295 M_WAITOK, 0x500, 0xa0000, PAGE_SIZE, 0ul); 296 if (wakeaddr == NULL) { 297 printf("%s: can't alloc wake memory\n", __func__); 298 return (NULL); 299 } 300 if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL, 301 EVENTHANDLER_PRI_LAST) == NULL) { 302 printf("%s: can't register event handler\n", __func__); 303 contigfree(wakeaddr, (ACPI_PAGETABLES + 1) * PAGE_SIZE, M_DEVBUF); 304 return (NULL); 305 } 306 susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK); 307 for (i = 0; i < mp_ncpus; i++) { 308 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK); 309#ifdef __amd64__ 310 susppcbs[i]->pcb_fpususpend = alloc_fpusave(M_WAITOK); 311#endif 312 } 313 314 return (wakeaddr); 315} 316 317void 318acpi_install_wakeup_handler(struct acpi_softc *sc) 319{ 320 static void *wakeaddr = NULL; 321#ifdef __amd64__ 322 uint64_t *pt4, *pt3, *pt2; 323 int i; 324#endif 325 326 if (wakeaddr != NULL) 327 return; 328 329 wakeaddr = acpi_alloc_wakeup_handler(); 330 if (wakeaddr == NULL) 331 return; 332 333 sc->acpi_wakeaddr = (vm_offset_t)wakeaddr; 334 sc->acpi_wakephys = vtophys(wakeaddr); 335 336 bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode)); 337 338 /* Patch GDT base address, ljmp targets. */ 339 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t, 340 WAKECODE_PADDR(sc) + bootgdt); 341 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t, 342 WAKECODE_PADDR(sc) + wakeup_32); 343#ifdef __amd64__ 344 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t, 345 WAKECODE_PADDR(sc) + wakeup_64); 346 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys); 347#endif 348 349 /* Save pointers to some global data. */ 350 WAKECODE_FIXUP(wakeup_ret, void *, resumectx); 351#ifdef __amd64__ 352 WAKECODE_FIXUP(wakeup_cr3, uint64_t, KPML4phys); 353#else 354#ifdef PAE 355 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt)); 356#else 357 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir)); 358#endif 359#endif 360 361#ifdef __amd64__ 362 /* Build temporary page tables below realmode code. */ 363 pt4 = wakeaddr; 364 pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t); 365 pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t); 366 367 /* Create the initial 1GB replicated page tables */ 368 for (i = 0; i < 512; i++) { 369 /* 370 * Each slot of the level 4 pages points 371 * to the same level 3 page 372 */ 373 pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE); 374 pt4[i] |= PG_V | PG_RW | PG_U; 375 376 /* 377 * Each slot of the level 3 pages points 378 * to the same level 2 page 379 */ 380 pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE)); 381 pt3[i] |= PG_V | PG_RW | PG_U; 382 383 /* The level 2 page slots are mapped with 2MB pages for 1GB. */ 384 pt2[i] = i * (2 * 1024 * 1024); 385 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U; 386 } 387#endif 388 389 if (bootverbose) 390 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n", 391 (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys); 392} 393