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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #if defined(__amd64__)
39 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/memrange.h>
46 #include <sys/systm.h>
51 #include <machine/clock.h>
52 #include <machine/cpu.h>
53 #include <machine/intr_machdep.h>
55 #include <machine/pcb.h>
56 #include <machine/specialreg.h>
57 #include <machine/md_var.h>
60 #include <x86/apicreg.h>
61 #include <x86/apicvar.h>
64 #include <machine/smp.h>
65 #include <machine/vmparam.h>
68 #include <contrib/dev/acpica/include/acpi.h>
70 #include <dev/acpica/acpivar.h>
72 #include "acpi_wakecode.h"
73 #include "acpi_wakedata.h"
75 /* Make sure the code is less than a page and leave room for the stack. */
76 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
78 extern int acpi_resume_beep;
79 extern int acpi_reset_video;
82 extern struct susppcb **susppcbs;
83 static cpuset_t suspcpus;
85 static struct susppcb **susppcbs;
88 static void *acpi_alloc_wakeup_handler(void **);
89 static void acpi_stop_beep(void *);
92 static int acpi_wakeup_ap(struct acpi_softc *, int);
93 static void acpi_wakeup_cpus(struct acpi_softc *);
97 #define ACPI_WAKEPAGES 4
99 #define ACPI_WAKEPAGES 1
102 #define WAKECODE_FIXUP(offset, type, val) do { \
104 addr = (type *)(sc->acpi_wakeaddr + (offset)); \
109 acpi_stop_beep(void *arg)
112 if (acpi_resume_beep != 0)
113 timer_spkr_release();
118 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
121 int vector = (sc->acpi_wakephys >> 12) & 0xff;
122 int apic_id = cpu_apic_ids[cpu];
125 pcb = &susppcbs[cpu]->sp_pcb;
126 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
127 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
128 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
130 ipi_startup(apic_id, vector);
132 /* Wait up to 5 seconds for it to resume. */
133 for (ms = 0; ms < 5000; ms++) {
134 if (!CPU_ISSET(cpu, &suspended_cpus))
135 return (1); /* return SUCCESS */
138 return (0); /* return FAILURE */
141 #define WARMBOOT_TARGET 0
142 #define WARMBOOT_OFF (KERNBASE + 0x0467)
143 #define WARMBOOT_SEG (KERNBASE + 0x0469)
145 #define CMOS_REG (0x70)
146 #define CMOS_DATA (0x71)
147 #define BIOS_RESET (0x0f)
148 #define BIOS_WARM (0x0a)
151 acpi_wakeup_cpus(struct acpi_softc *sc)
153 uint32_t mpbioswarmvec;
157 /* save the current value of the warm-start vector */
158 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
159 outb(CMOS_REG, BIOS_RESET);
160 mpbiosreason = inb(CMOS_DATA);
162 /* setup a vector to our boot code */
163 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
164 *((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
165 outb(CMOS_REG, BIOS_RESET);
166 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
168 /* Wake up each AP. */
169 for (cpu = 1; cpu < mp_ncpus; cpu++) {
170 if (!CPU_ISSET(cpu, &suspcpus))
172 if (acpi_wakeup_ap(sc, cpu) == 0) {
173 /* restore the warmstart vector */
174 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
175 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
176 cpu, cpu_apic_ids[cpu]);
180 /* restore the warmstart vector */
181 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
183 outb(CMOS_REG, BIOS_RESET);
184 outb(CMOS_DATA, mpbiosreason);
189 acpi_sleep_machdep(struct acpi_softc *sc, int state)
194 if (sc->acpi_wakeaddr == 0ul)
195 return (-1); /* couldn't alloc wake memory */
199 CPU_CLR(PCPU_GET(cpuid), &suspcpus);
202 if (acpi_resume_beep != 0)
203 timer_spkr_acquire();
205 AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
209 pcb = &susppcbs[0]->sp_pcb;
212 fpususpend(susppcbs[0]->sp_fpususpend);
214 npxsuspend(susppcbs[0]->sp_fpususpend);
217 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
218 device_printf(sc->acpi_dev, "Failed to suspend APs\n");
219 return (0); /* couldn't sleep */
223 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
224 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
227 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER));
229 WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
231 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
232 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
233 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
235 /* Call ACPICA to enter the desired sleep state */
236 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
237 status = AcpiEnterSleepStateS4bios();
239 status = AcpiEnterSleepState(state);
240 if (ACPI_FAILURE(status)) {
241 device_printf(sc->acpi_dev,
242 "AcpiEnterSleepState failed - %s\n",
243 AcpiFormatException(status));
244 return (0); /* couldn't sleep */
251 fpuresume(susppcbs[0]->sp_fpususpend);
253 npxresume(susppcbs[0]->sp_fpususpend);
257 return (1); /* wakeup successfully */
261 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
265 if (sleep_result == -1)
266 return (sleep_result);
269 /* Wakeup MD procedures in interrupt disabled context */
270 if (sleep_result == 1) {
273 PCPU_SET(switchtime, 0);
274 PCPU_SET(switchticks, ticks);
279 if (!CPU_EMPTY(&suspcpus))
280 acpi_wakeup_cpus(sc);
285 if (!CPU_EMPTY(&suspcpus))
286 restart_cpus(suspcpus);
290 if (vmm_resume_p != NULL)
293 intr_resume(/*suspend_cancelled*/false);
295 AcpiSetFirmwareWakingVector(0, 0);
297 /* Wakeup MD procedures in interrupt enabled context */
298 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
299 mem_range_softc.mr_op->reinit != NULL)
300 mem_range_softc.mr_op->reinit(&mem_range_softc);
303 return (sleep_result);
307 acpi_alloc_wakeup_handler(void *wakepages[ACPI_WAKEPAGES])
311 memset(wakepages, 0, ACPI_WAKEPAGES * sizeof(*wakepages));
314 * Specify the region for our wakeup code. We want it in the low 1 MB
315 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
316 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
317 * and ROM area (0xa0000 and above). The temporary page tables must be
320 for (i = 0; i < ACPI_WAKEPAGES; i++) {
321 wakepages[i] = contigmalloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT,
322 0x500, 0xa0000, PAGE_SIZE, 0ul);
323 if (wakepages[i] == NULL) {
324 printf("%s: can't alloc wake memory\n", __func__);
328 if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
329 EVENTHANDLER_PRI_LAST) == NULL) {
330 printf("%s: can't register event handler\n", __func__);
333 susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
334 for (i = 0; i < mp_ncpus; i++) {
335 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
336 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
342 for (i = 0; i < ACPI_WAKEPAGES; i++)
343 if (wakepages[i] != NULL)
344 contigfree(wakepages[i], PAGE_SIZE, M_DEVBUF);
349 acpi_install_wakeup_handler(struct acpi_softc *sc)
351 static void *wakeaddr;
352 void *wakepages[ACPI_WAKEPAGES];
354 uint64_t *pt4, *pt3, *pt2;
355 vm_paddr_t pt4pa, pt3pa, pt2pa;
359 if (wakeaddr != NULL)
362 if (acpi_alloc_wakeup_handler(wakepages) == NULL)
365 wakeaddr = wakepages[0];
366 sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
367 sc->acpi_wakephys = vtophys(wakeaddr);
373 pt4pa = vtophys(pt4);
374 pt3pa = vtophys(pt3);
375 pt2pa = vtophys(pt2);
378 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
380 /* Patch GDT base address, ljmp targets. */
381 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
382 sc->acpi_wakephys + bootgdt);
383 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
384 sc->acpi_wakephys + wakeup_32);
386 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
387 sc->acpi_wakephys + wakeup_64);
388 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, pt4pa);
391 /* Save pointers to some global data. */
392 WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
394 #if defined(PAE) || defined(PAE_TABLES)
395 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt));
397 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir));
400 #else /* __amd64__ */
401 /* Create the initial 1GB replicated page tables */
402 for (i = 0; i < 512; i++) {
404 * Each slot of the level 4 pages points
405 * to the same level 3 page
407 pt4[i] = (uint64_t)pt3pa;
408 pt4[i] |= PG_V | PG_RW | PG_U;
411 * Each slot of the level 3 pages points
412 * to the same level 2 page
414 pt3[i] = (uint64_t)pt2pa;
415 pt3[i] |= PG_V | PG_RW | PG_U;
417 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
418 pt2[i] = i * (2 * 1024 * 1024);
419 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
421 #endif /* !__amd64__ */
424 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
425 (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);