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__)
42 #include <sys/param.h>
44 #include <sys/eventhandler.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/memrange.h>
49 #include <sys/systm.h>
54 #include <machine/clock.h>
55 #include <machine/cpu.h>
56 #include <machine/intr_machdep.h>
58 #include <machine/pcb.h>
59 #include <machine/specialreg.h>
60 #include <machine/md_var.h>
63 #include <x86/apicreg.h>
64 #include <x86/apicvar.h>
67 #include <machine/smp.h>
68 #include <machine/vmparam.h>
71 #include <contrib/dev/acpica/include/acpi.h>
73 #include <dev/acpica/acpivar.h>
75 #include "acpi_wakecode.h"
76 #include "acpi_wakedata.h"
78 /* Make sure the code is less than a page and leave room for the stack. */
79 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
81 extern int acpi_resume_beep;
82 extern int acpi_reset_video;
85 extern struct susppcb **susppcbs;
86 static cpuset_t suspcpus;
88 static struct susppcb **susppcbs;
91 static void *acpi_alloc_wakeup_handler(void **);
92 static void acpi_stop_beep(void *);
95 static int acpi_wakeup_ap(struct acpi_softc *, int);
96 static void acpi_wakeup_cpus(struct acpi_softc *);
100 #define ACPI_WAKEPAGES 4
102 #define ACPI_WAKEPAGES 1
105 #define WAKECODE_FIXUP(offset, type, val) do { \
107 addr = (type *)(sc->acpi_wakeaddr + (offset)); \
112 acpi_stop_beep(void *arg)
115 if (acpi_resume_beep != 0)
116 timer_spkr_release();
121 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
124 int vector = (sc->acpi_wakephys >> 12) & 0xff;
125 int apic_id = cpu_apic_ids[cpu];
128 pcb = &susppcbs[cpu]->sp_pcb;
129 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
130 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
131 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
133 ipi_startup(apic_id, vector);
135 /* Wait up to 5 seconds for it to resume. */
136 for (ms = 0; ms < 5000; ms++) {
137 if (!CPU_ISSET(cpu, &suspended_cpus))
138 return (1); /* return SUCCESS */
141 return (0); /* return FAILURE */
144 #define WARMBOOT_TARGET 0
145 #define WARMBOOT_OFF (KERNBASE + 0x0467)
146 #define WARMBOOT_SEG (KERNBASE + 0x0469)
148 #define CMOS_REG (0x70)
149 #define CMOS_DATA (0x71)
150 #define BIOS_RESET (0x0f)
151 #define BIOS_WARM (0x0a)
154 acpi_wakeup_cpus(struct acpi_softc *sc)
156 uint32_t mpbioswarmvec;
160 /* save the current value of the warm-start vector */
161 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
162 outb(CMOS_REG, BIOS_RESET);
163 mpbiosreason = inb(CMOS_DATA);
165 /* setup a vector to our boot code */
166 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
167 *((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
168 outb(CMOS_REG, BIOS_RESET);
169 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
171 /* Wake up each AP. */
172 for (cpu = 1; cpu < mp_ncpus; cpu++) {
173 if (!CPU_ISSET(cpu, &suspcpus))
175 if (acpi_wakeup_ap(sc, cpu) == 0) {
176 /* restore the warmstart vector */
177 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
178 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
179 cpu, cpu_apic_ids[cpu]);
183 /* restore the warmstart vector */
184 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
186 outb(CMOS_REG, BIOS_RESET);
187 outb(CMOS_DATA, mpbiosreason);
192 acpi_sleep_machdep(struct acpi_softc *sc, int state)
197 if (sc->acpi_wakeaddr == 0ul)
198 return (-1); /* couldn't alloc wake memory */
202 CPU_CLR(PCPU_GET(cpuid), &suspcpus);
205 if (acpi_resume_beep != 0)
206 timer_spkr_acquire();
208 AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
212 pcb = &susppcbs[0]->sp_pcb;
215 fpususpend(susppcbs[0]->sp_fpususpend);
216 #elif defined(DEV_NPX)
217 npxsuspend(susppcbs[0]->sp_fpususpend);
220 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
221 device_printf(sc->acpi_dev, "Failed to suspend APs\n");
222 return (0); /* couldn't sleep */
226 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
227 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
230 WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
232 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
233 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
234 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
236 /* Call ACPICA to enter the desired sleep state */
237 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
238 status = AcpiEnterSleepStateS4bios();
240 status = AcpiEnterSleepState(state);
241 if (ACPI_FAILURE(status)) {
242 device_printf(sc->acpi_dev,
243 "AcpiEnterSleepState failed - %s\n",
244 AcpiFormatException(status));
245 return (0); /* couldn't sleep */
252 fpuresume(susppcbs[0]->sp_fpususpend);
253 #elif defined(DEV_NPX)
254 npxresume(susppcbs[0]->sp_fpususpend);
258 return (1); /* wakeup successfully */
262 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
266 if (sleep_result == -1)
267 return (sleep_result);
270 /* Wakeup MD procedures in interrupt disabled context */
271 if (sleep_result == 1) {
274 PCPU_SET(switchtime, 0);
275 PCPU_SET(switchticks, ticks);
280 if (!CPU_EMPTY(&suspcpus))
281 acpi_wakeup_cpus(sc);
286 if (!CPU_EMPTY(&suspcpus))
287 restart_cpus(suspcpus);
291 if (vmm_resume_p != NULL)
294 intr_resume(/*suspend_cancelled*/false);
296 AcpiSetFirmwareWakingVector(0, 0);
298 /* Wakeup MD procedures in interrupt enabled context */
299 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
300 mem_range_softc.mr_op->reinit != NULL)
301 mem_range_softc.mr_op->reinit(&mem_range_softc);
304 return (sleep_result);
308 acpi_alloc_wakeup_handler(void *wakepages[ACPI_WAKEPAGES])
312 memset(wakepages, 0, ACPI_WAKEPAGES * sizeof(*wakepages));
315 * Specify the region for our wakeup code. We want it in the low 1 MB
316 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
317 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
318 * and ROM area (0xa0000 and above). The temporary page tables must be
321 for (i = 0; i < ACPI_WAKEPAGES; i++) {
322 wakepages[i] = contigmalloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT,
323 0x500, 0xa0000, PAGE_SIZE, 0ul);
324 if (wakepages[i] == NULL) {
325 printf("%s: can't alloc wake memory\n", __func__);
329 if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
330 EVENTHANDLER_PRI_LAST) == NULL) {
331 printf("%s: can't register event handler\n", __func__);
334 susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
335 for (i = 0; i < mp_ncpus; i++) {
336 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
337 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
343 for (i = 0; i < ACPI_WAKEPAGES; i++)
344 if (wakepages[i] != NULL)
345 contigfree(wakepages[i], PAGE_SIZE, M_DEVBUF);
350 acpi_install_wakeup_handler(struct acpi_softc *sc)
352 static void *wakeaddr;
353 void *wakepages[ACPI_WAKEPAGES];
355 uint64_t *pt4, *pt3, *pt2;
356 vm_paddr_t pt4pa, pt3pa, pt2pa;
360 if (wakeaddr != NULL)
363 if (acpi_alloc_wakeup_handler(wakepages) == NULL)
366 wakeaddr = wakepages[0];
367 sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
368 sc->acpi_wakephys = vtophys(wakeaddr);
374 pt4pa = vtophys(pt4);
375 pt3pa = vtophys(pt3);
376 pt2pa = vtophys(pt2);
379 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
381 /* Patch GDT base address, ljmp targets. */
382 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
383 sc->acpi_wakephys + bootgdt);
384 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
385 sc->acpi_wakephys + wakeup_32);
387 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
388 sc->acpi_wakephys + wakeup_64);
389 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, pt4pa);
392 /* Save pointers to some global data. */
393 WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
395 #if defined(PAE) || defined(PAE_TABLES)
396 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt));
398 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir));
401 #else /* __amd64__ */
402 /* Create the initial 1GB replicated page tables */
403 for (i = 0; i < 512; i++) {
405 * Each slot of the level 4 pages points
406 * to the same level 3 page
408 pt4[i] = (uint64_t)pt3pa;
409 pt4[i] |= PG_V | PG_RW | PG_U;
412 * Each slot of the level 3 pages points
413 * to the same level 2 page
415 pt3[i] = (uint64_t)pt2pa;
416 pt3[i] |= PG_V | PG_RW | PG_U;
418 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
419 pt2[i] = i * (2 * 1024 * 1024);
420 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
422 #endif /* !__amd64__ */
425 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
426 (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);