12b6eec53SKonstantin Belousov /*- 22b6eec53SKonstantin Belousov * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 32b6eec53SKonstantin Belousov * 42b6eec53SKonstantin Belousov * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org> 52b6eec53SKonstantin Belousov * Copyright (c) 2001-2012 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 62b6eec53SKonstantin Belousov * Copyright (c) 2003 Peter Wemm 72b6eec53SKonstantin Belousov * Copyright (c) 2008-2012 Jung-uk Kim <jkim@FreeBSD.org> 82b6eec53SKonstantin Belousov * All rights reserved. 92b6eec53SKonstantin Belousov * 102b6eec53SKonstantin Belousov * Redistribution and use in source and binary forms, with or without 112b6eec53SKonstantin Belousov * modification, are permitted provided that the following conditions 122b6eec53SKonstantin Belousov * are met: 132b6eec53SKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 142b6eec53SKonstantin Belousov * notice, this list of conditions and the following disclaimer. 152b6eec53SKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 162b6eec53SKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 172b6eec53SKonstantin Belousov * documentation and/or other materials provided with the distribution. 182b6eec53SKonstantin Belousov * 192b6eec53SKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 202b6eec53SKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 212b6eec53SKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 222b6eec53SKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 232b6eec53SKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 242b6eec53SKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 252b6eec53SKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 262b6eec53SKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 272b6eec53SKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 282b6eec53SKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 292b6eec53SKonstantin Belousov * SUCH DAMAGE. 302b6eec53SKonstantin Belousov */ 312b6eec53SKonstantin Belousov 322b6eec53SKonstantin Belousov #include <sys/cdefs.h> 332b6eec53SKonstantin Belousov __FBSDID("$FreeBSD$"); 342b6eec53SKonstantin Belousov 352b6eec53SKonstantin Belousov #include <sys/param.h> 362b6eec53SKonstantin Belousov #include <sys/bus.h> 372b6eec53SKonstantin Belousov #include <sys/eventhandler.h> 382b6eec53SKonstantin Belousov #include <sys/kernel.h> 392b6eec53SKonstantin Belousov #include <sys/malloc.h> 402b6eec53SKonstantin Belousov #include <sys/memrange.h> 412b6eec53SKonstantin Belousov #include <sys/smp.h> 422b6eec53SKonstantin Belousov #include <sys/systm.h> 432b6eec53SKonstantin Belousov #include <sys/cons.h> 442b6eec53SKonstantin Belousov 452b6eec53SKonstantin Belousov #include <vm/vm.h> 462b6eec53SKonstantin Belousov #include <vm/pmap.h> 471c56781cSKonstantin Belousov #include <vm/vm_page.h> 482b6eec53SKonstantin Belousov 492b6eec53SKonstantin Belousov #include <machine/clock.h> 502b6eec53SKonstantin Belousov #include <machine/cpu.h> 512b6eec53SKonstantin Belousov #include <machine/intr_machdep.h> 522b6eec53SKonstantin Belousov #include <machine/md_var.h> 532b6eec53SKonstantin Belousov #include <x86/mca.h> 542b6eec53SKonstantin Belousov #include <machine/pcb.h> 552b6eec53SKonstantin Belousov #include <machine/specialreg.h> 562b6eec53SKonstantin Belousov #include <x86/ucode.h> 572b6eec53SKonstantin Belousov 582b6eec53SKonstantin Belousov #include <x86/apicreg.h> 592b6eec53SKonstantin Belousov #include <x86/apicvar.h> 602b6eec53SKonstantin Belousov #ifdef SMP 612b6eec53SKonstantin Belousov #include <machine/smp.h> 622b6eec53SKonstantin Belousov #include <machine/vmparam.h> 632b6eec53SKonstantin Belousov #endif 642b6eec53SKonstantin Belousov 652b6eec53SKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h> 662b6eec53SKonstantin Belousov 672b6eec53SKonstantin Belousov #include <dev/acpica/acpivar.h> 682b6eec53SKonstantin Belousov 692b6eec53SKonstantin Belousov #include "acpi_wakecode.h" 702b6eec53SKonstantin Belousov #include "acpi_wakedata.h" 712b6eec53SKonstantin Belousov 722b6eec53SKonstantin Belousov /* Make sure the code is less than a page and leave room for the stack. */ 732b6eec53SKonstantin Belousov CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024); 742b6eec53SKonstantin Belousov 752b6eec53SKonstantin Belousov extern int acpi_resume_beep; 762b6eec53SKonstantin Belousov extern int acpi_reset_video; 772b6eec53SKonstantin Belousov extern int acpi_susp_bounce; 782b6eec53SKonstantin Belousov 792b6eec53SKonstantin Belousov #ifdef SMP 802b6eec53SKonstantin Belousov extern struct susppcb **susppcbs; 812b6eec53SKonstantin Belousov static cpuset_t suspcpus; 822b6eec53SKonstantin Belousov #else 832b6eec53SKonstantin Belousov static struct susppcb **susppcbs; 842b6eec53SKonstantin Belousov #endif 852b6eec53SKonstantin Belousov 862b6eec53SKonstantin Belousov static void acpi_stop_beep(void *); 872b6eec53SKonstantin Belousov 882b6eec53SKonstantin Belousov #ifdef SMP 892b6eec53SKonstantin Belousov static int acpi_wakeup_ap(struct acpi_softc *, int); 902b6eec53SKonstantin Belousov static void acpi_wakeup_cpus(struct acpi_softc *); 912b6eec53SKonstantin Belousov #endif 922b6eec53SKonstantin Belousov 931c56781cSKonstantin Belousov #define ACPI_WAKEPT_PAGES 7 942b6eec53SKonstantin Belousov 952b6eec53SKonstantin Belousov #define WAKECODE_FIXUP(offset, type, val) do { \ 962b6eec53SKonstantin Belousov type *addr; \ 972b6eec53SKonstantin Belousov addr = (type *)(sc->acpi_wakeaddr + (offset)); \ 982b6eec53SKonstantin Belousov *addr = val; \ 992b6eec53SKonstantin Belousov } while (0) 1002b6eec53SKonstantin Belousov 1012b6eec53SKonstantin Belousov static void 1022b6eec53SKonstantin Belousov acpi_stop_beep(void *arg) 1032b6eec53SKonstantin Belousov { 1042b6eec53SKonstantin Belousov 1052b6eec53SKonstantin Belousov if (acpi_resume_beep != 0) 1062b6eec53SKonstantin Belousov timer_spkr_release(); 1072b6eec53SKonstantin Belousov } 1082b6eec53SKonstantin Belousov 1092b6eec53SKonstantin Belousov #ifdef SMP 1102b6eec53SKonstantin Belousov static int 1112b6eec53SKonstantin Belousov acpi_wakeup_ap(struct acpi_softc *sc, int cpu) 1122b6eec53SKonstantin Belousov { 1132b6eec53SKonstantin Belousov struct pcb *pcb; 1142b6eec53SKonstantin Belousov int vector = (sc->acpi_wakephys >> 12) & 0xff; 1152b6eec53SKonstantin Belousov int apic_id = cpu_apic_ids[cpu]; 1162b6eec53SKonstantin Belousov int ms; 1172b6eec53SKonstantin Belousov 1182b6eec53SKonstantin Belousov pcb = &susppcbs[cpu]->sp_pcb; 1192b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb); 1202b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit); 1212b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base); 1222b6eec53SKonstantin Belousov 1232b6eec53SKonstantin Belousov ipi_startup(apic_id, vector); 1242b6eec53SKonstantin Belousov 1252b6eec53SKonstantin Belousov /* Wait up to 5 seconds for it to resume. */ 1262b6eec53SKonstantin Belousov for (ms = 0; ms < 5000; ms++) { 1272b6eec53SKonstantin Belousov if (!CPU_ISSET(cpu, &suspended_cpus)) 1282b6eec53SKonstantin Belousov return (1); /* return SUCCESS */ 1292b6eec53SKonstantin Belousov DELAY(1000); 1302b6eec53SKonstantin Belousov } 1312b6eec53SKonstantin Belousov return (0); /* return FAILURE */ 1322b6eec53SKonstantin Belousov } 1332b6eec53SKonstantin Belousov 1342b6eec53SKonstantin Belousov #define WARMBOOT_TARGET 0 1352b6eec53SKonstantin Belousov #define WARMBOOT_OFF (KERNBASE + 0x0467) 1362b6eec53SKonstantin Belousov #define WARMBOOT_SEG (KERNBASE + 0x0469) 1372b6eec53SKonstantin Belousov 1382b6eec53SKonstantin Belousov #define CMOS_REG (0x70) 1392b6eec53SKonstantin Belousov #define CMOS_DATA (0x71) 1402b6eec53SKonstantin Belousov #define BIOS_RESET (0x0f) 1412b6eec53SKonstantin Belousov #define BIOS_WARM (0x0a) 1422b6eec53SKonstantin Belousov 1432b6eec53SKonstantin Belousov static void 1442b6eec53SKonstantin Belousov acpi_wakeup_cpus(struct acpi_softc *sc) 1452b6eec53SKonstantin Belousov { 1462b6eec53SKonstantin Belousov uint32_t mpbioswarmvec; 1472b6eec53SKonstantin Belousov int cpu; 1482b6eec53SKonstantin Belousov u_char mpbiosreason; 1492b6eec53SKonstantin Belousov 1502b6eec53SKonstantin Belousov if (!efi_boot) { 1512b6eec53SKonstantin Belousov /* save the current value of the warm-start vector */ 1522b6eec53SKonstantin Belousov mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF); 1532b6eec53SKonstantin Belousov outb(CMOS_REG, BIOS_RESET); 1542b6eec53SKonstantin Belousov mpbiosreason = inb(CMOS_DATA); 1552b6eec53SKonstantin Belousov 1562b6eec53SKonstantin Belousov /* setup a vector to our boot code */ 1572b6eec53SKonstantin Belousov *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET; 1582b6eec53SKonstantin Belousov *((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4; 1592b6eec53SKonstantin Belousov outb(CMOS_REG, BIOS_RESET); 1602b6eec53SKonstantin Belousov outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ 1612b6eec53SKonstantin Belousov } 1622b6eec53SKonstantin Belousov 1632b6eec53SKonstantin Belousov /* Wake up each AP. */ 1642b6eec53SKonstantin Belousov for (cpu = 1; cpu < mp_ncpus; cpu++) { 1652b6eec53SKonstantin Belousov if (!CPU_ISSET(cpu, &suspcpus)) 1662b6eec53SKonstantin Belousov continue; 1672b6eec53SKonstantin Belousov if (acpi_wakeup_ap(sc, cpu) == 0) { 1682b6eec53SKonstantin Belousov /* restore the warmstart vector */ 1692b6eec53SKonstantin Belousov *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec; 1702b6eec53SKonstantin Belousov panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)", 1712b6eec53SKonstantin Belousov cpu, cpu_apic_ids[cpu]); 1722b6eec53SKonstantin Belousov } 1732b6eec53SKonstantin Belousov } 1742b6eec53SKonstantin Belousov 1752b6eec53SKonstantin Belousov if (!efi_boot) { 1762b6eec53SKonstantin Belousov /* restore the warmstart vector */ 1772b6eec53SKonstantin Belousov *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec; 1782b6eec53SKonstantin Belousov 1792b6eec53SKonstantin Belousov outb(CMOS_REG, BIOS_RESET); 1802b6eec53SKonstantin Belousov outb(CMOS_DATA, mpbiosreason); 1812b6eec53SKonstantin Belousov } 1822b6eec53SKonstantin Belousov } 1832b6eec53SKonstantin Belousov #endif 1842b6eec53SKonstantin Belousov 1852b6eec53SKonstantin Belousov int 1862b6eec53SKonstantin Belousov acpi_sleep_machdep(struct acpi_softc *sc, int state) 1872b6eec53SKonstantin Belousov { 1882b6eec53SKonstantin Belousov ACPI_STATUS status; 1892b6eec53SKonstantin Belousov struct pcb *pcb; 1902b6eec53SKonstantin Belousov struct pcpu *pc; 1912b6eec53SKonstantin Belousov int i; 1922b6eec53SKonstantin Belousov 1932b6eec53SKonstantin Belousov if (sc->acpi_wakeaddr == 0ul) 1942b6eec53SKonstantin Belousov return (-1); /* couldn't alloc wake memory */ 1952b6eec53SKonstantin Belousov 1962b6eec53SKonstantin Belousov #ifdef SMP 1972b6eec53SKonstantin Belousov suspcpus = all_cpus; 1982b6eec53SKonstantin Belousov CPU_CLR(PCPU_GET(cpuid), &suspcpus); 1992b6eec53SKonstantin Belousov #endif 2002b6eec53SKonstantin Belousov 2012b6eec53SKonstantin Belousov if (acpi_resume_beep != 0) 2022b6eec53SKonstantin Belousov timer_spkr_acquire(); 2032b6eec53SKonstantin Belousov 2042b6eec53SKonstantin Belousov AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0); 2052b6eec53SKonstantin Belousov 2062b6eec53SKonstantin Belousov intr_suspend(); 2072b6eec53SKonstantin Belousov 2082b6eec53SKonstantin Belousov pcb = &susppcbs[0]->sp_pcb; 2092b6eec53SKonstantin Belousov if (savectx(pcb)) { 2102b6eec53SKonstantin Belousov fpususpend(susppcbs[0]->sp_fpususpend); 2112b6eec53SKonstantin Belousov #ifdef SMP 2122b6eec53SKonstantin Belousov if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) { 2132b6eec53SKonstantin Belousov device_printf(sc->acpi_dev, "Failed to suspend APs\n"); 2142b6eec53SKonstantin Belousov return (0); /* couldn't sleep */ 2152b6eec53SKonstantin Belousov } 2162b6eec53SKonstantin Belousov #endif 2172b6eec53SKonstantin Belousov hw_ibrs_ibpb_active = 0; 2182b6eec53SKonstantin Belousov hw_ssb_active = 0; 2192b6eec53SKonstantin Belousov cpu_stdext_feature3 = 0; 2202b6eec53SKonstantin Belousov CPU_FOREACH(i) { 2212b6eec53SKonstantin Belousov pc = pcpu_find(i); 2222b6eec53SKonstantin Belousov pc->pc_ibpb_set = 0; 2232b6eec53SKonstantin Belousov } 2242b6eec53SKonstantin Belousov 2252b6eec53SKonstantin Belousov WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0)); 2262b6eec53SKonstantin Belousov WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0)); 2272b6eec53SKonstantin Belousov 2282b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER) & 2292b6eec53SKonstantin Belousov ~(EFER_LMA)); 2302b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb); 2312b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit); 2322b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base); 2332b6eec53SKonstantin Belousov 2342b6eec53SKonstantin Belousov /* Call ACPICA to enter the desired sleep state */ 2352b6eec53SKonstantin Belousov if (state == ACPI_STATE_S4 && sc->acpi_s4bios) 2362b6eec53SKonstantin Belousov status = AcpiEnterSleepStateS4bios(); 2372b6eec53SKonstantin Belousov else 2382b6eec53SKonstantin Belousov status = AcpiEnterSleepState(state); 2392b6eec53SKonstantin Belousov if (ACPI_FAILURE(status)) { 2402b6eec53SKonstantin Belousov device_printf(sc->acpi_dev, 2412b6eec53SKonstantin Belousov "AcpiEnterSleepState failed - %s\n", 2422b6eec53SKonstantin Belousov AcpiFormatException(status)); 2432b6eec53SKonstantin Belousov return (0); /* couldn't sleep */ 2442b6eec53SKonstantin Belousov } 2452b6eec53SKonstantin Belousov 2462b6eec53SKonstantin Belousov if (acpi_susp_bounce) 2472b6eec53SKonstantin Belousov resumectx(pcb); 2482b6eec53SKonstantin Belousov 2492b6eec53SKonstantin Belousov for (;;) 2502b6eec53SKonstantin Belousov ia32_pause(); 2512b6eec53SKonstantin Belousov } else { 2522b6eec53SKonstantin Belousov /* 253*ff6d6094SKonstantin Belousov * Re-initialize console hardware as soon as possible. 2542b6eec53SKonstantin Belousov * No console output (e.g. printf) is allowed before 2552b6eec53SKonstantin Belousov * this point. 2562b6eec53SKonstantin Belousov */ 2572b6eec53SKonstantin Belousov cnresume(); 2582b6eec53SKonstantin Belousov fpuresume(susppcbs[0]->sp_fpususpend); 2592b6eec53SKonstantin Belousov } 2602b6eec53SKonstantin Belousov 2612b6eec53SKonstantin Belousov return (1); /* wakeup successfully */ 2622b6eec53SKonstantin Belousov } 2632b6eec53SKonstantin Belousov 2642b6eec53SKonstantin Belousov int 2652b6eec53SKonstantin Belousov acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result, 2662b6eec53SKonstantin Belousov int intr_enabled) 2672b6eec53SKonstantin Belousov { 2682b6eec53SKonstantin Belousov 2692b6eec53SKonstantin Belousov if (sleep_result == -1) 2702b6eec53SKonstantin Belousov return (sleep_result); 2712b6eec53SKonstantin Belousov 2722b6eec53SKonstantin Belousov if (!intr_enabled) { 2732b6eec53SKonstantin Belousov /* Wakeup MD procedures in interrupt disabled context */ 2742b6eec53SKonstantin Belousov if (sleep_result == 1) { 2752b6eec53SKonstantin Belousov ucode_reload(); 2762b6eec53SKonstantin Belousov pmap_init_pat(); 2772b6eec53SKonstantin Belousov initializecpu(); 2782b6eec53SKonstantin Belousov PCPU_SET(switchtime, 0); 2792b6eec53SKonstantin Belousov PCPU_SET(switchticks, ticks); 2802b6eec53SKonstantin Belousov lapic_xapic_mode(); 2812b6eec53SKonstantin Belousov #ifdef SMP 2822b6eec53SKonstantin Belousov if (!CPU_EMPTY(&suspcpus)) 2832b6eec53SKonstantin Belousov acpi_wakeup_cpus(sc); 2842b6eec53SKonstantin Belousov #endif 2852b6eec53SKonstantin Belousov } 2862b6eec53SKonstantin Belousov 2872b6eec53SKonstantin Belousov #ifdef SMP 2882b6eec53SKonstantin Belousov if (!CPU_EMPTY(&suspcpus)) 2892b6eec53SKonstantin Belousov resume_cpus(suspcpus); 2902b6eec53SKonstantin Belousov #endif 2912b6eec53SKonstantin Belousov mca_resume(); 2922b6eec53SKonstantin Belousov if (vmm_resume_p != NULL) 2932b6eec53SKonstantin Belousov vmm_resume_p(); 2942b6eec53SKonstantin Belousov intr_resume(/*suspend_cancelled*/false); 2952b6eec53SKonstantin Belousov 2962b6eec53SKonstantin Belousov AcpiSetFirmwareWakingVector(0, 0); 2972b6eec53SKonstantin Belousov } else { 2982b6eec53SKonstantin Belousov /* Wakeup MD procedures in interrupt enabled context */ 2992b6eec53SKonstantin Belousov if (sleep_result == 1 && mem_range_softc.mr_op != NULL && 3002b6eec53SKonstantin Belousov mem_range_softc.mr_op->reinit != NULL) 3012b6eec53SKonstantin Belousov mem_range_softc.mr_op->reinit(&mem_range_softc); 3022b6eec53SKonstantin Belousov } 3032b6eec53SKonstantin Belousov 3042b6eec53SKonstantin Belousov return (sleep_result); 3052b6eec53SKonstantin Belousov } 3062b6eec53SKonstantin Belousov 3071c56781cSKonstantin Belousov static void 3081c56781cSKonstantin Belousov acpi_alloc_wakeup_handler(void **wakeaddr, 3091c56781cSKonstantin Belousov void *wakept_pages[ACPI_WAKEPT_PAGES]) 3102b6eec53SKonstantin Belousov { 3111c56781cSKonstantin Belousov vm_page_t wakept_m[ACPI_WAKEPT_PAGES]; 3122b6eec53SKonstantin Belousov int i; 3132b6eec53SKonstantin Belousov 3141c56781cSKonstantin Belousov *wakeaddr = NULL; 3151c56781cSKonstantin Belousov memset(wakept_pages, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_pages)); 3161c56781cSKonstantin Belousov memset(wakept_m, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_m)); 3172b6eec53SKonstantin Belousov 3182b6eec53SKonstantin Belousov /* 3191c56781cSKonstantin Belousov * Specify the region for our wakeup code. We want it in the 3201c56781cSKonstantin Belousov * low 1 MB region, excluding real mode IVT (0-0x3ff), BDA 3211c56781cSKonstantin Belousov * (0x400-0x4ff), EBDA (less than 128KB, below 0xa0000, must 3221c56781cSKonstantin Belousov * be excluded by SMAP and DSDT), and ROM area (0xa0000 and 3231c56781cSKonstantin Belousov * above). 3242b6eec53SKonstantin Belousov */ 3251c56781cSKonstantin Belousov *wakeaddr = contigmalloc(PAGE_SIZE, M_DEVBUF, 3262b6eec53SKonstantin Belousov M_NOWAIT, 0x500, 0xa0000, PAGE_SIZE, 0ul); 3271c56781cSKonstantin Belousov if (*wakeaddr == NULL) { 3282b6eec53SKonstantin Belousov printf("%s: can't alloc wake memory\n", __func__); 3292b6eec53SKonstantin Belousov goto freepages; 3302b6eec53SKonstantin Belousov } 3311c56781cSKonstantin Belousov 3321c56781cSKonstantin Belousov for (i = 0; i < ACPI_WAKEPT_PAGES - (la57 ? 0 : 1); i++) { 3331c56781cSKonstantin Belousov wakept_m[i] = pmap_page_alloc_below_4g(true); 3341c56781cSKonstantin Belousov wakept_pages[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS( 3351c56781cSKonstantin Belousov wakept_m[i])); 3362b6eec53SKonstantin Belousov } 3372b6eec53SKonstantin Belousov if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL, 3382b6eec53SKonstantin Belousov EVENTHANDLER_PRI_LAST) == NULL) { 3392b6eec53SKonstantin Belousov printf("%s: can't register event handler\n", __func__); 3402b6eec53SKonstantin Belousov goto freepages; 3412b6eec53SKonstantin Belousov } 3422b6eec53SKonstantin Belousov susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK); 3432b6eec53SKonstantin Belousov for (i = 0; i < mp_ncpus; i++) { 3442b6eec53SKonstantin Belousov susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK); 3452b6eec53SKonstantin Belousov susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK); 3462b6eec53SKonstantin Belousov } 3471c56781cSKonstantin Belousov return; 3482b6eec53SKonstantin Belousov 3492b6eec53SKonstantin Belousov freepages: 3501c56781cSKonstantin Belousov if (*wakeaddr != NULL) 3511c56781cSKonstantin Belousov contigfree(*wakeaddr, PAGE_SIZE, M_DEVBUF); 3521c56781cSKonstantin Belousov for (i = 0; i < ACPI_WAKEPT_PAGES; i++) { 3531c56781cSKonstantin Belousov if (wakept_m[i] != NULL) 3541c56781cSKonstantin Belousov vm_page_free(wakept_m[i]); 3551c56781cSKonstantin Belousov } 3561c56781cSKonstantin Belousov *wakeaddr = NULL; 3572b6eec53SKonstantin Belousov } 3582b6eec53SKonstantin Belousov 3592b6eec53SKonstantin Belousov void 3602b6eec53SKonstantin Belousov acpi_install_wakeup_handler(struct acpi_softc *sc) 3612b6eec53SKonstantin Belousov { 3622b6eec53SKonstantin Belousov static void *wakeaddr; 3631c56781cSKonstantin Belousov void *wakept_pages[ACPI_WAKEPT_PAGES]; 3642b6eec53SKonstantin Belousov uint64_t *pt5, *pt4, *pt3, *pt2_0, *pt2_1, *pt2_2, *pt2_3; 3652b6eec53SKonstantin Belousov vm_paddr_t pt5pa, pt4pa, pt3pa, pt2_0pa, pt2_1pa, pt2_2pa, pt2_3pa; 3662b6eec53SKonstantin Belousov int i; 3672b6eec53SKonstantin Belousov 3682b6eec53SKonstantin Belousov if (wakeaddr != NULL) 3692b6eec53SKonstantin Belousov return; 3701c56781cSKonstantin Belousov acpi_alloc_wakeup_handler(&wakeaddr, wakept_pages); 3711c56781cSKonstantin Belousov if (wakeaddr == NULL) 3722b6eec53SKonstantin Belousov return; 3732b6eec53SKonstantin Belousov 3742b6eec53SKonstantin Belousov sc->acpi_wakeaddr = (vm_offset_t)wakeaddr; 3752b6eec53SKonstantin Belousov sc->acpi_wakephys = vtophys(wakeaddr); 3762b6eec53SKonstantin Belousov 3772b6eec53SKonstantin Belousov if (la57) { 3781c56781cSKonstantin Belousov pt5 = wakept_pages[6]; 3792b6eec53SKonstantin Belousov pt5pa = vtophys(pt5); 3802b6eec53SKonstantin Belousov } 3811c56781cSKonstantin Belousov pt4 = wakept_pages[0]; 3821c56781cSKonstantin Belousov pt3 = wakept_pages[1]; 3831c56781cSKonstantin Belousov pt2_0 = wakept_pages[2]; 3841c56781cSKonstantin Belousov pt2_1 = wakept_pages[3]; 3851c56781cSKonstantin Belousov pt2_2 = wakept_pages[4]; 3861c56781cSKonstantin Belousov pt2_3 = wakept_pages[5]; 3872b6eec53SKonstantin Belousov pt4pa = vtophys(pt4); 3882b6eec53SKonstantin Belousov pt3pa = vtophys(pt3); 3892b6eec53SKonstantin Belousov pt2_0pa = vtophys(pt2_0); 3902b6eec53SKonstantin Belousov pt2_1pa = vtophys(pt2_1); 3912b6eec53SKonstantin Belousov pt2_2pa = vtophys(pt2_2); 3922b6eec53SKonstantin Belousov pt2_3pa = vtophys(pt2_3); 3932b6eec53SKonstantin Belousov 3942b6eec53SKonstantin Belousov bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); 3952b6eec53SKonstantin Belousov 3962b6eec53SKonstantin Belousov /* Patch GDT base address, ljmp targets. */ 3972b6eec53SKonstantin Belousov WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t, 3982b6eec53SKonstantin Belousov sc->acpi_wakephys + bootgdt); 3992b6eec53SKonstantin Belousov WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t, 4002b6eec53SKonstantin Belousov sc->acpi_wakephys + wakeup_32); 4012b6eec53SKonstantin Belousov WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t, 4022b6eec53SKonstantin Belousov sc->acpi_wakephys + wakeup_64); 4032b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_pagetables, uint32_t, la57 ? (pt5pa | 0x1) : 4042b6eec53SKonstantin Belousov pt4pa); 4052b6eec53SKonstantin Belousov 4062b6eec53SKonstantin Belousov /* Save pointers to some global data. */ 4072b6eec53SKonstantin Belousov WAKECODE_FIXUP(wakeup_ret, void *, resumectx); 4082b6eec53SKonstantin Belousov /* Create 1:1 mapping for the low 4G */ 4092b6eec53SKonstantin Belousov if (la57) { 4102b6eec53SKonstantin Belousov bcopy(kernel_pmap->pm_pmltop, pt5, PAGE_SIZE); 4112b6eec53SKonstantin Belousov pt5[0] = (uint64_t)pt4pa; 4122b6eec53SKonstantin Belousov pt5[0] |= PG_V | PG_RW | PG_U; 4132b6eec53SKonstantin Belousov } else { 4142b6eec53SKonstantin Belousov bcopy(kernel_pmap->pm_pmltop, pt4, PAGE_SIZE); 4152b6eec53SKonstantin Belousov } 4162b6eec53SKonstantin Belousov 4172b6eec53SKonstantin Belousov pt4[0] = (uint64_t)pt3pa; 4182b6eec53SKonstantin Belousov pt4[0] |= PG_V | PG_RW | PG_U; 4192b6eec53SKonstantin Belousov 4202b6eec53SKonstantin Belousov pt3[0] = (uint64_t)pt2_0pa; 4212b6eec53SKonstantin Belousov pt3[0] |= PG_V | PG_RW | PG_U; 4222b6eec53SKonstantin Belousov pt3[1] = (uint64_t)pt2_1pa; 4232b6eec53SKonstantin Belousov pt3[1] |= PG_V | PG_RW | PG_U; 4242b6eec53SKonstantin Belousov pt3[2] = (uint64_t)pt2_2pa; 4252b6eec53SKonstantin Belousov pt3[2] |= PG_V | PG_RW | PG_U; 4262b6eec53SKonstantin Belousov pt3[3] = (uint64_t)pt2_3pa; 4272b6eec53SKonstantin Belousov pt3[3] |= PG_V | PG_RW | PG_U; 4282b6eec53SKonstantin Belousov 4292b6eec53SKonstantin Belousov for (i = 0; i < NPDEPG; i++) { 4302b6eec53SKonstantin Belousov pt2_0[i] = (pd_entry_t)i * NBPDR; 4312b6eec53SKonstantin Belousov pt2_0[i] |= PG_V | PG_RW | PG_PS | PG_U; 4322b6eec53SKonstantin Belousov } 4332b6eec53SKonstantin Belousov for (i = 0; i < NPDEPG; i++) { 4342b6eec53SKonstantin Belousov pt2_1[i] = (pd_entry_t)NBPDP + i * NBPDR; 4352b6eec53SKonstantin Belousov pt2_1[i] |= PG_V | PG_RW | PG_PS | PG_U; 4362b6eec53SKonstantin Belousov } 4372b6eec53SKonstantin Belousov for (i = 0; i < NPDEPG; i++) { 4382b6eec53SKonstantin Belousov pt2_2[i] = (pd_entry_t)2 * NBPDP + i * NBPDR; 4392b6eec53SKonstantin Belousov pt2_2[i] |= PG_V | PG_RW | PG_PS | PG_U; 4402b6eec53SKonstantin Belousov } 4412b6eec53SKonstantin Belousov for (i = 0; i < NPDEPG; i++) { 4422b6eec53SKonstantin Belousov pt2_3[i] = (pd_entry_t)3 * NBPDP + i * NBPDR; 4432b6eec53SKonstantin Belousov pt2_3[i] |= PG_V | PG_RW | PG_PS | PG_U; 4442b6eec53SKonstantin Belousov } 4452b6eec53SKonstantin Belousov 4462b6eec53SKonstantin Belousov if (bootverbose) 4472b6eec53SKonstantin Belousov device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n", 4482b6eec53SKonstantin Belousov (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys); 4492b6eec53SKonstantin Belousov } 450