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