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 #include <sys/param.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/memrange.h>
44 #include <machine/clock.h>
45 #include <machine/intr_machdep.h>
47 #include <machine/pcb.h>
48 #include <machine/pmap.h>
49 #include <machine/specialreg.h>
50 #include <machine/md_var.h>
53 #include <x86/apicreg.h>
54 #include <machine/smp.h>
55 #include <machine/vmparam.h>
58 #include <contrib/dev/acpica/include/acpi.h>
60 #include <dev/acpica/acpivar.h>
62 #include "acpi_wakecode.h"
63 #include "acpi_wakedata.h"
65 /* Make sure the code is less than a page and leave room for the stack. */
66 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
68 extern int acpi_resume_beep;
69 extern int acpi_reset_video;
72 extern struct pcb **susppcbs;
73 static cpuset_t suspcpus;
75 static struct pcb **susppcbs;
78 static void *acpi_alloc_wakeup_handler(void);
79 static void acpi_stop_beep(void *);
82 static int acpi_wakeup_ap(struct acpi_softc *, int);
83 static void acpi_wakeup_cpus(struct acpi_softc *);
87 #define ACPI_PAGETABLES 3
89 #define ACPI_PAGETABLES 0
92 #define WAKECODE_VADDR(sc) \
93 ((sc)->acpi_wakeaddr + (ACPI_PAGETABLES * PAGE_SIZE))
94 #define WAKECODE_PADDR(sc) \
95 ((sc)->acpi_wakephys + (ACPI_PAGETABLES * PAGE_SIZE))
96 #define WAKECODE_FIXUP(offset, type, val) do { \
98 addr = (type *)(WAKECODE_VADDR(sc) + offset); \
103 acpi_stop_beep(void *arg)
106 if (acpi_resume_beep != 0)
107 timer_spkr_release();
112 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
114 int vector = (WAKECODE_PADDR(sc) >> 12) & 0xff;
115 int apic_id = cpu_apic_ids[cpu];
118 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[cpu]);
119 WAKECODE_FIXUP(wakeup_gdt, uint16_t, susppcbs[cpu]->pcb_gdt.rd_limit);
120 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
121 susppcbs[cpu]->pcb_gdt.rd_base);
123 ipi_startup(apic_id, vector);
125 /* Wait up to 5 seconds for it to resume. */
126 for (ms = 0; ms < 5000; ms++) {
127 if (!CPU_ISSET(cpu, &suspended_cpus))
128 return (1); /* return SUCCESS */
131 return (0); /* return FAILURE */
134 #define WARMBOOT_TARGET 0
135 #define WARMBOOT_OFF (KERNBASE + 0x0467)
136 #define WARMBOOT_SEG (KERNBASE + 0x0469)
138 #define CMOS_REG (0x70)
139 #define CMOS_DATA (0x71)
140 #define BIOS_RESET (0x0f)
141 #define BIOS_WARM (0x0a)
144 acpi_wakeup_cpus(struct acpi_softc *sc)
146 uint32_t mpbioswarmvec;
150 /* save the current value of the warm-start vector */
151 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
152 outb(CMOS_REG, BIOS_RESET);
153 mpbiosreason = inb(CMOS_DATA);
155 /* setup a vector to our boot code */
156 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
157 *((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4;
158 outb(CMOS_REG, BIOS_RESET);
159 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
161 /* Wake up each AP. */
162 for (cpu = 1; cpu < mp_ncpus; cpu++) {
163 if (!CPU_ISSET(cpu, &suspcpus))
165 if (acpi_wakeup_ap(sc, cpu) == 0) {
166 /* restore the warmstart vector */
167 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
168 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
169 cpu, cpu_apic_ids[cpu]);
173 /* restore the warmstart vector */
174 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
176 outb(CMOS_REG, BIOS_RESET);
177 outb(CMOS_DATA, mpbiosreason);
182 acpi_sleep_machdep(struct acpi_softc *sc, int state)
186 if (sc->acpi_wakeaddr == 0ul)
187 return (-1); /* couldn't alloc wake memory */
191 CPU_CLR(PCPU_GET(cpuid), &suspcpus);
194 if (acpi_resume_beep != 0)
195 timer_spkr_acquire();
197 AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc));
201 if (savectx(susppcbs[0])) {
203 ctx_fpusave(susppcbs[0]->pcb_fpususpend);
206 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
207 device_printf(sc->acpi_dev, "Failed to suspend APs\n");
208 return (0); /* couldn't sleep */
212 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
213 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
216 WAKECODE_FIXUP(wakeup_cr4, register_t, susppcbs[0]->pcb_cr4);
218 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[0]);
219 WAKECODE_FIXUP(wakeup_gdt, uint16_t,
220 susppcbs[0]->pcb_gdt.rd_limit);
221 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
222 susppcbs[0]->pcb_gdt.rd_base);
224 /* Call ACPICA to enter the desired sleep state */
225 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
226 status = AcpiEnterSleepStateS4bios();
228 status = AcpiEnterSleepState(state);
229 if (ACPI_FAILURE(status)) {
230 device_printf(sc->acpi_dev,
231 "AcpiEnterSleepState failed - %s\n",
232 AcpiFormatException(status));
233 return (0); /* couldn't sleep */
240 return (1); /* wakeup successfully */
244 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
248 if (sleep_result == -1)
249 return (sleep_result);
252 /* Wakeup MD procedures in interrupt disabled context */
253 if (sleep_result == 1) {
256 PCPU_SET(switchtime, 0);
257 PCPU_SET(switchticks, ticks);
259 if (!CPU_EMPTY(&suspcpus))
260 acpi_wakeup_cpus(sc);
265 if (!CPU_EMPTY(&suspcpus))
266 restart_cpus(suspcpus);
269 intr_resume(/*suspend_cancelled*/false);
271 AcpiSetFirmwareWakingVector(0);
273 /* Wakeup MD procedures in interrupt enabled context */
274 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
275 mem_range_softc.mr_op->reinit != NULL)
276 mem_range_softc.mr_op->reinit(&mem_range_softc);
279 return (sleep_result);
283 acpi_alloc_wakeup_handler(void)
289 * Specify the region for our wakeup code. We want it in the low 1 MB
290 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
291 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
292 * and ROM area (0xa0000 and above). The temporary page tables must be
295 wakeaddr = contigmalloc((ACPI_PAGETABLES + 1) * PAGE_SIZE, M_DEVBUF,
296 M_WAITOK, 0x500, 0xa0000, PAGE_SIZE, 0ul);
297 if (wakeaddr == NULL) {
298 printf("%s: can't alloc wake memory\n", __func__);
301 if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
302 EVENTHANDLER_PRI_LAST) == NULL) {
303 printf("%s: can't register event handler\n", __func__);
304 contigfree(wakeaddr, (ACPI_PAGETABLES + 1) * PAGE_SIZE,
308 susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
309 for (i = 0; i < mp_ncpus; i++) {
310 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
312 susppcbs[i]->pcb_fpususpend = alloc_fpusave(M_WAITOK);
320 acpi_install_wakeup_handler(struct acpi_softc *sc)
322 static void *wakeaddr = NULL;
324 uint64_t *pt4, *pt3, *pt2;
328 if (wakeaddr != NULL)
331 wakeaddr = acpi_alloc_wakeup_handler();
332 if (wakeaddr == NULL)
335 sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
336 sc->acpi_wakephys = vtophys(wakeaddr);
338 bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode));
340 /* Patch GDT base address, ljmp targets. */
341 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
342 WAKECODE_PADDR(sc) + bootgdt);
343 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
344 WAKECODE_PADDR(sc) + wakeup_32);
346 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
347 WAKECODE_PADDR(sc) + wakeup_64);
348 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys);
351 /* Save pointers to some global data. */
352 WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
355 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt));
357 WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir));
361 /* Build temporary page tables below realmode code. */
363 pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t);
364 pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t);
366 /* Create the initial 1GB replicated page tables */
367 for (i = 0; i < 512; i++) {
369 * Each slot of the level 4 pages points
370 * to the same level 3 page
372 pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE);
373 pt4[i] |= PG_V | PG_RW | PG_U;
376 * Each slot of the level 3 pages points
377 * to the same level 2 page
379 pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE));
380 pt3[i] |= PG_V | PG_RW | PG_U;
382 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
383 pt2[i] = i * (2 * 1024 * 1024);
384 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
389 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
390 (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);