2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * Copyright (c) 2003 Peter Wemm
5 * Copyright (c) 2008-2009 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>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/memrange.h>
40 #include <sys/types.h>
45 #include <machine/intr_machdep.h>
46 #include <machine/pcb.h>
47 #include <machine/pmap.h>
48 #include <machine/specialreg.h>
49 #include <machine/vmparam.h>
52 #include <machine/apicreg.h>
53 #include <machine/smp.h>
56 #include <contrib/dev/acpica/include/acpi.h>
58 #include <dev/acpica/acpivar.h>
60 #include "acpi_wakecode.h"
61 #include "acpi_wakedata.h"
63 /* Make sure the code is less than a page and leave room for the stack. */
64 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
67 #error this file needs sys/cdefs.h as a prerequisite
70 extern int acpi_resume_beep;
71 extern int acpi_reset_video;
74 extern struct xpcb *stopxpcbs;
76 static struct xpcb *stopxpcbs;
79 int acpi_restorecpu(struct xpcb *, vm_offset_t);
80 int acpi_savecpu(struct xpcb *);
82 static void acpi_alloc_wakeup_handler(void);
83 static void acpi_stop_beep(void *);
86 static int acpi_wakeup_ap(struct acpi_softc *, int);
87 static void acpi_wakeup_cpus(struct acpi_softc *, cpumask_t);
90 #define WAKECODE_VADDR(sc) ((sc)->acpi_wakeaddr + (3 * PAGE_SIZE))
91 #define WAKECODE_PADDR(sc) ((sc)->acpi_wakephys + (3 * PAGE_SIZE))
92 #define WAKECODE_FIXUP(offset, type, val) do { \
94 addr = (type *)(WAKECODE_VADDR(sc) + offset); \
98 /* Turn off bits 1&2 of the PIT, stopping the beep. */
100 acpi_stop_beep(void *arg)
102 outb(0x61, inb(0x61) & ~0x3);
107 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
109 int vector = (WAKECODE_PADDR(sc) >> 12) & 0xff;
110 int apic_id = cpu_apic_ids[cpu];
113 WAKECODE_FIXUP(wakeup_xpcb, struct xpcb *, &stopxpcbs[cpu]);
114 WAKECODE_FIXUP(wakeup_gdt, uint16_t, stopxpcbs[cpu].xpcb_gdt.rd_limit);
115 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
116 stopxpcbs[cpu].xpcb_gdt.rd_base);
117 WAKECODE_FIXUP(wakeup_cpu, int, cpu);
119 /* do an INIT IPI: assert RESET */
120 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
121 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id);
123 /* wait for pending status end */
126 /* do an INIT IPI: deassert RESET */
127 lapic_ipi_raw(APIC_DEST_ALLESELF | APIC_TRIGMOD_LEVEL |
128 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, 0);
130 /* wait for pending status end */
131 DELAY(10000); /* wait ~10mS */
135 * next we do a STARTUP IPI: the previous INIT IPI might still be
136 * latched, (P5 bug) this 1st STARTUP would then terminate
137 * immediately, and the previously started INIT IPI would continue. OR
138 * the previous INIT IPI has already run. and this STARTUP IPI will
139 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
143 /* do a STARTUP IPI */
144 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
145 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
148 DELAY(200); /* wait ~200uS */
151 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
152 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
153 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
154 * recognized after hardware RESET or INIT IPI.
157 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
158 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
161 DELAY(200); /* wait ~200uS */
163 /* Wait up to 5 seconds for it to start. */
164 for (ms = 0; ms < 5000; ms++) {
165 if (*(int *)(WAKECODE_VADDR(sc) + wakeup_cpu) == 0)
166 return (1); /* return SUCCESS */
169 return (0); /* return FAILURE */
172 #define WARMBOOT_TARGET 0
173 #define WARMBOOT_OFF (KERNBASE + 0x0467)
174 #define WARMBOOT_SEG (KERNBASE + 0x0469)
176 #define CMOS_REG (0x70)
177 #define CMOS_DATA (0x71)
178 #define BIOS_RESET (0x0f)
179 #define BIOS_WARM (0x0a)
182 acpi_wakeup_cpus(struct acpi_softc *sc, cpumask_t wakeup_cpus)
184 uint32_t mpbioswarmvec;
189 /* save the current value of the warm-start vector */
190 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
191 outb(CMOS_REG, BIOS_RESET);
192 mpbiosreason = inb(CMOS_DATA);
194 /* setup a vector to our boot code */
195 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
196 *((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4;
197 outb(CMOS_REG, BIOS_RESET);
198 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
200 /* Wake up each AP. */
201 for (cpu = 1; cpu < mp_ncpus; cpu++) {
203 if ((wakeup_cpus & map) != map)
205 if (acpi_wakeup_ap(sc, cpu) == 0) {
206 /* restore the warmstart vector */
207 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
208 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
209 cpu, cpu_apic_ids[cpu]);
213 /* restore the warmstart vector */
214 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
216 outb(CMOS_REG, BIOS_RESET);
217 outb(CMOS_DATA, mpbiosreason);
222 acpi_sleep_machdep(struct acpi_softc *sc, int state)
224 struct savefpu *stopfpu;
226 cpumask_t wakeup_cpus;
234 if (sc->acpi_wakeaddr == 0ul)
238 wakeup_cpus = PCPU_GET(other_cpus);
241 AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc));
247 * Temporarily switch to the kernel pmap because it provides
248 * an identity mapping (setup at boot) for the low physical
249 * memory region containing the wakeup code.
254 stopfpu = &stopxpcbs[0].xpcb_pcb.pcb_save;
255 if (acpi_savecpu(&stopxpcbs[0])) {
256 fpugetregs(curthread, stopfpu);
259 if (wakeup_cpus != 0 && suspend_cpus(wakeup_cpus) == 0) {
260 device_printf(sc->acpi_dev,
261 "Failed to suspend APs: CPU mask = 0x%jx\n",
262 (uintmax_t)(wakeup_cpus & ~stopped_cpus));
267 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
268 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
270 WAKECODE_FIXUP(wakeup_xpcb, struct xpcb *, &stopxpcbs[0]);
271 WAKECODE_FIXUP(wakeup_gdt, uint16_t,
272 stopxpcbs[0].xpcb_gdt.rd_limit);
273 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
274 stopxpcbs[0].xpcb_gdt.rd_base);
275 WAKECODE_FIXUP(wakeup_cpu, int, 0);
277 /* Call ACPICA to enter the desired sleep state */
278 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
279 status = AcpiEnterSleepStateS4bios();
281 status = AcpiEnterSleepState(state);
283 if (status != AE_OK) {
284 device_printf(sc->acpi_dev,
285 "AcpiEnterSleepState failed - %s\n",
286 AcpiFormatException(status));
293 fpusetregs(curthread, stopfpu);
295 if (wakeup_cpus != 0)
296 acpi_wakeup_cpus(sc, wakeup_cpus);
298 acpi_resync_clock(sc);
304 if (wakeup_cpus != 0)
305 restart_cpus(wakeup_cpus);
312 AcpiSetFirmwareWakingVector(0);
314 if (ret == 0 && mem_range_softc.mr_op != NULL &&
315 mem_range_softc.mr_op->reinit != NULL)
316 mem_range_softc.mr_op->reinit(&mem_range_softc);
318 /* If we beeped, turn it off after a delay. */
319 if (acpi_resume_beep)
320 timeout(acpi_stop_beep, NULL, 3 * hz);
325 static vm_offset_t acpi_wakeaddr;
328 acpi_alloc_wakeup_handler(void)
336 * Specify the region for our wakeup code. We want it in the low 1 MB
337 * region, excluding video memory and above (0xa0000). We ask for
338 * it to be page-aligned, just to be safe.
340 wakeaddr = contigmalloc(4 * PAGE_SIZE, M_DEVBUF, M_NOWAIT, 0, 0x9ffff,
342 if (wakeaddr == NULL) {
343 printf("%s: can't alloc wake memory\n", __func__);
346 stopxpcbs = malloc(mp_ncpus * sizeof(*stopxpcbs), M_DEVBUF, M_NOWAIT);
347 if (stopxpcbs == NULL) {
348 contigfree(wakeaddr, 4 * PAGE_SIZE, M_DEVBUF);
349 printf("%s: can't alloc CPU state memory\n", __func__);
352 acpi_wakeaddr = (vm_offset_t)wakeaddr;
355 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0);
358 acpi_install_wakeup_handler(struct acpi_softc *sc)
360 uint64_t *pt4, *pt3, *pt2;
363 if (acpi_wakeaddr == 0ul)
366 sc->acpi_wakeaddr = acpi_wakeaddr;
367 sc->acpi_wakephys = vtophys(acpi_wakeaddr);
369 bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode));
371 /* Patch GDT base address, ljmp targets and page table base address. */
372 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
373 WAKECODE_PADDR(sc) + bootgdt);
374 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
375 WAKECODE_PADDR(sc) + wakeup_32);
376 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
377 WAKECODE_PADDR(sc) + wakeup_64);
378 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys);
380 /* Save pointers to some global data. */
381 WAKECODE_FIXUP(wakeup_retaddr, void *, acpi_restorecpu);
382 WAKECODE_FIXUP(wakeup_kpml4, uint64_t, KPML4phys);
383 WAKECODE_FIXUP(wakeup_ctx, vm_offset_t,
384 WAKECODE_VADDR(sc) + wakeup_ctx);
385 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER));
386 WAKECODE_FIXUP(wakeup_pat, uint64_t, rdmsr(MSR_PAT));
387 WAKECODE_FIXUP(wakeup_star, uint64_t, rdmsr(MSR_STAR));
388 WAKECODE_FIXUP(wakeup_lstar, uint64_t, rdmsr(MSR_LSTAR));
389 WAKECODE_FIXUP(wakeup_cstar, uint64_t, rdmsr(MSR_CSTAR));
390 WAKECODE_FIXUP(wakeup_sfmask, uint64_t, rdmsr(MSR_SF_MASK));
392 /* Build temporary page tables below realmode code. */
393 pt4 = (uint64_t *)acpi_wakeaddr;
394 pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t);
395 pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t);
397 /* Create the initial 1GB replicated page tables */
398 for (i = 0; i < 512; i++) {
400 * Each slot of the level 4 pages points
401 * to the same level 3 page
403 pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE);
404 pt4[i] |= PG_V | PG_RW | PG_U;
407 * Each slot of the level 3 pages points
408 * to the same level 2 page
410 pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE));
411 pt3[i] |= PG_V | PG_RW | PG_U;
413 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
414 pt2[i] = i * (2 * 1024 * 1024);
415 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
419 device_printf(sc->acpi_dev, "wakeup code va %p pa %p\n",
420 (void *)sc->acpi_wakeaddr, (void *)sc->acpi_wakephys);