xref: /freebsd/sys/amd64/acpica/acpi_wakeup.c (revision d1bdc2821fcd416ab9b238580386eb605a6128d0)
12b6eec53SKonstantin Belousov /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
332b6eec53SKonstantin Belousov #include <sys/bus.h>
342b6eec53SKonstantin Belousov #include <sys/eventhandler.h>
352b6eec53SKonstantin Belousov #include <sys/kernel.h>
362b6eec53SKonstantin Belousov #include <sys/malloc.h>
372b6eec53SKonstantin Belousov #include <sys/memrange.h>
382b6eec53SKonstantin Belousov #include <sys/smp.h>
392b6eec53SKonstantin Belousov #include <sys/systm.h>
402b6eec53SKonstantin Belousov #include <sys/cons.h>
412b6eec53SKonstantin Belousov 
422b6eec53SKonstantin Belousov #include <vm/vm.h>
432b6eec53SKonstantin Belousov #include <vm/pmap.h>
441c56781cSKonstantin Belousov #include <vm/vm_page.h>
452b6eec53SKonstantin Belousov 
462b6eec53SKonstantin Belousov #include <machine/clock.h>
472b6eec53SKonstantin Belousov #include <machine/cpu.h>
482b6eec53SKonstantin Belousov #include <machine/intr_machdep.h>
492b6eec53SKonstantin Belousov #include <machine/md_var.h>
502b6eec53SKonstantin Belousov #include <x86/mca.h>
512b6eec53SKonstantin Belousov #include <machine/pcb.h>
522b6eec53SKonstantin Belousov #include <machine/specialreg.h>
532b6eec53SKonstantin Belousov #include <x86/ucode.h>
542b6eec53SKonstantin Belousov 
552b6eec53SKonstantin Belousov #include <x86/apicreg.h>
562b6eec53SKonstantin Belousov #include <x86/apicvar.h>
572b6eec53SKonstantin Belousov #ifdef SMP
582b6eec53SKonstantin Belousov #include <machine/smp.h>
592b6eec53SKonstantin Belousov #include <machine/vmparam.h>
602b6eec53SKonstantin Belousov #endif
612b6eec53SKonstantin Belousov 
622b6eec53SKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h>
632b6eec53SKonstantin Belousov 
642b6eec53SKonstantin Belousov #include <dev/acpica/acpivar.h>
652b6eec53SKonstantin Belousov 
662b6eec53SKonstantin Belousov #include "acpi_wakecode.h"
672b6eec53SKonstantin Belousov #include "acpi_wakedata.h"
682b6eec53SKonstantin Belousov 
692b6eec53SKonstantin Belousov /* Make sure the code is less than a page and leave room for the stack. */
702b6eec53SKonstantin Belousov CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
712b6eec53SKonstantin Belousov 
722b6eec53SKonstantin Belousov extern int		acpi_resume_beep;
732b6eec53SKonstantin Belousov extern int		acpi_reset_video;
742b6eec53SKonstantin Belousov extern int		acpi_susp_bounce;
752b6eec53SKonstantin Belousov 
762b6eec53SKonstantin Belousov #ifdef SMP
772b6eec53SKonstantin Belousov extern struct susppcb	**susppcbs;
782b6eec53SKonstantin Belousov static cpuset_t		suspcpus;
792b6eec53SKonstantin Belousov #else
802b6eec53SKonstantin Belousov static struct susppcb	**susppcbs;
812b6eec53SKonstantin Belousov #endif
822b6eec53SKonstantin Belousov 
832b6eec53SKonstantin Belousov static void		acpi_stop_beep(void *);
842b6eec53SKonstantin Belousov 
852b6eec53SKonstantin Belousov #ifdef SMP
862b6eec53SKonstantin Belousov static int		acpi_wakeup_ap(struct acpi_softc *, int);
872b6eec53SKonstantin Belousov static void		acpi_wakeup_cpus(struct acpi_softc *);
882b6eec53SKonstantin Belousov #endif
892b6eec53SKonstantin Belousov 
901c56781cSKonstantin Belousov #define	ACPI_WAKEPT_PAGES	7
912b6eec53SKonstantin Belousov 
922b6eec53SKonstantin Belousov #define	WAKECODE_FIXUP(offset, type, val)	do {	\
932b6eec53SKonstantin Belousov 	type	*addr;					\
942b6eec53SKonstantin Belousov 	addr = (type *)(sc->acpi_wakeaddr + (offset));	\
952b6eec53SKonstantin Belousov 	*addr = val;					\
962b6eec53SKonstantin Belousov } while (0)
972b6eec53SKonstantin Belousov 
982b6eec53SKonstantin Belousov static void
992b6eec53SKonstantin Belousov acpi_stop_beep(void *arg)
1002b6eec53SKonstantin Belousov {
1012b6eec53SKonstantin Belousov 
1022b6eec53SKonstantin Belousov 	if (acpi_resume_beep != 0)
1032b6eec53SKonstantin Belousov 		timer_spkr_release();
1042b6eec53SKonstantin Belousov }
1052b6eec53SKonstantin Belousov 
1062b6eec53SKonstantin Belousov #ifdef SMP
1072b6eec53SKonstantin Belousov static int
1082b6eec53SKonstantin Belousov acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
1092b6eec53SKonstantin Belousov {
1102b6eec53SKonstantin Belousov 	struct pcb *pcb;
1112b6eec53SKonstantin Belousov 	int		vector = (sc->acpi_wakephys >> 12) & 0xff;
1122b6eec53SKonstantin Belousov 	int		apic_id = cpu_apic_ids[cpu];
1132b6eec53SKonstantin Belousov 	int		ms;
1142b6eec53SKonstantin Belousov 
1152b6eec53SKonstantin Belousov 	pcb = &susppcbs[cpu]->sp_pcb;
1162b6eec53SKonstantin Belousov 	WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
1172b6eec53SKonstantin Belousov 	WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
1182b6eec53SKonstantin Belousov 	WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
1192b6eec53SKonstantin Belousov 
1202b6eec53SKonstantin Belousov 	ipi_startup(apic_id, vector);
1212b6eec53SKonstantin Belousov 
1222b6eec53SKonstantin Belousov 	/* Wait up to 5 seconds for it to resume. */
1232b6eec53SKonstantin Belousov 	for (ms = 0; ms < 5000; ms++) {
1242b6eec53SKonstantin Belousov 		if (!CPU_ISSET(cpu, &suspended_cpus))
1252b6eec53SKonstantin Belousov 			return (1);	/* return SUCCESS */
1262b6eec53SKonstantin Belousov 		DELAY(1000);
1272b6eec53SKonstantin Belousov 	}
1282b6eec53SKonstantin Belousov 	return (0);		/* return FAILURE */
1292b6eec53SKonstantin Belousov }
1302b6eec53SKonstantin Belousov 
1312b6eec53SKonstantin Belousov #define	WARMBOOT_TARGET		0
1322b6eec53SKonstantin Belousov #define	WARMBOOT_OFF		(KERNBASE + 0x0467)
1332b6eec53SKonstantin Belousov #define	WARMBOOT_SEG		(KERNBASE + 0x0469)
1342b6eec53SKonstantin Belousov 
1352b6eec53SKonstantin Belousov #define	CMOS_REG		(0x70)
1362b6eec53SKonstantin Belousov #define	CMOS_DATA		(0x71)
1372b6eec53SKonstantin Belousov #define	BIOS_RESET		(0x0f)
1382b6eec53SKonstantin Belousov #define	BIOS_WARM		(0x0a)
1392b6eec53SKonstantin Belousov 
1402b6eec53SKonstantin Belousov static void
1412b6eec53SKonstantin Belousov acpi_wakeup_cpus(struct acpi_softc *sc)
1422b6eec53SKonstantin Belousov {
1432b6eec53SKonstantin Belousov 	uint32_t	mpbioswarmvec;
1442b6eec53SKonstantin Belousov 	int		cpu;
1452b6eec53SKonstantin Belousov 	u_char		mpbiosreason;
1462b6eec53SKonstantin Belousov 
1472b6eec53SKonstantin Belousov 	if (!efi_boot) {
1482b6eec53SKonstantin Belousov 		/* save the current value of the warm-start vector */
1492b6eec53SKonstantin Belousov 		mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
1502b6eec53SKonstantin Belousov 		outb(CMOS_REG, BIOS_RESET);
1512b6eec53SKonstantin Belousov 		mpbiosreason = inb(CMOS_DATA);
1522b6eec53SKonstantin Belousov 
1532b6eec53SKonstantin Belousov 		/* setup a vector to our boot code */
1542b6eec53SKonstantin Belousov 		*((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
1552b6eec53SKonstantin Belousov 		*((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
1562b6eec53SKonstantin Belousov 		outb(CMOS_REG, BIOS_RESET);
1572b6eec53SKonstantin Belousov 		outb(CMOS_DATA, BIOS_WARM);	/* 'warm-start' */
1582b6eec53SKonstantin Belousov 	}
1592b6eec53SKonstantin Belousov 
1602b6eec53SKonstantin Belousov 	/* Wake up each AP. */
1612b6eec53SKonstantin Belousov 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
1622b6eec53SKonstantin Belousov 		if (!CPU_ISSET(cpu, &suspcpus))
1632b6eec53SKonstantin Belousov 			continue;
1642b6eec53SKonstantin Belousov 		if (acpi_wakeup_ap(sc, cpu) == 0) {
1652b6eec53SKonstantin Belousov 			/* restore the warmstart vector */
1662b6eec53SKonstantin Belousov 			*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
1672b6eec53SKonstantin Belousov 			panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
1682b6eec53SKonstantin Belousov 			    cpu, cpu_apic_ids[cpu]);
1692b6eec53SKonstantin Belousov 		}
1702b6eec53SKonstantin Belousov 	}
1712b6eec53SKonstantin Belousov 
1722b6eec53SKonstantin Belousov 	if (!efi_boot) {
1732b6eec53SKonstantin Belousov 		/* restore the warmstart vector */
1742b6eec53SKonstantin Belousov 		*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
1752b6eec53SKonstantin Belousov 
1762b6eec53SKonstantin Belousov 		outb(CMOS_REG, BIOS_RESET);
1772b6eec53SKonstantin Belousov 		outb(CMOS_DATA, mpbiosreason);
1782b6eec53SKonstantin Belousov 	}
1792b6eec53SKonstantin Belousov }
1802b6eec53SKonstantin Belousov #endif
1812b6eec53SKonstantin Belousov 
1822b6eec53SKonstantin Belousov int
1832b6eec53SKonstantin Belousov acpi_sleep_machdep(struct acpi_softc *sc, int state)
1842b6eec53SKonstantin Belousov {
1852b6eec53SKonstantin Belousov 	ACPI_STATUS	status;
1862b6eec53SKonstantin Belousov 	struct pcb	*pcb;
1872b6eec53SKonstantin Belousov 	struct pcpu *pc;
1882b6eec53SKonstantin Belousov 	int i;
1892b6eec53SKonstantin Belousov 
1902b6eec53SKonstantin Belousov 	if (sc->acpi_wakeaddr == 0ul)
1912b6eec53SKonstantin Belousov 		return (-1);	/* couldn't alloc wake memory */
1922b6eec53SKonstantin Belousov 
1932b6eec53SKonstantin Belousov #ifdef SMP
1942b6eec53SKonstantin Belousov 	suspcpus = all_cpus;
1952b6eec53SKonstantin Belousov 	CPU_CLR(PCPU_GET(cpuid), &suspcpus);
1962b6eec53SKonstantin Belousov #endif
1972b6eec53SKonstantin Belousov 
1982b6eec53SKonstantin Belousov 	if (acpi_resume_beep != 0)
1992b6eec53SKonstantin Belousov 		timer_spkr_acquire();
2002b6eec53SKonstantin Belousov 
2012b6eec53SKonstantin Belousov 	AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
2022b6eec53SKonstantin Belousov 
2032b6eec53SKonstantin Belousov 	intr_suspend();
2042b6eec53SKonstantin Belousov 
2052b6eec53SKonstantin Belousov 	pcb = &susppcbs[0]->sp_pcb;
2062b6eec53SKonstantin Belousov 	if (savectx(pcb)) {
2072b6eec53SKonstantin Belousov 		fpususpend(susppcbs[0]->sp_fpususpend);
2082b6eec53SKonstantin Belousov #ifdef SMP
2092b6eec53SKonstantin Belousov 		if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
2102b6eec53SKonstantin Belousov 			device_printf(sc->acpi_dev, "Failed to suspend APs\n");
2112b6eec53SKonstantin Belousov 			return (0);	/* couldn't sleep */
2122b6eec53SKonstantin Belousov 		}
2132b6eec53SKonstantin Belousov #endif
2142b6eec53SKonstantin Belousov 		hw_ibrs_ibpb_active = 0;
2152b6eec53SKonstantin Belousov 		hw_ssb_active = 0;
2162b6eec53SKonstantin Belousov 		cpu_stdext_feature3 = 0;
2172b6eec53SKonstantin Belousov 		CPU_FOREACH(i) {
2182b6eec53SKonstantin Belousov 			pc = pcpu_find(i);
2192b6eec53SKonstantin Belousov 			pc->pc_ibpb_set = 0;
2202b6eec53SKonstantin Belousov 		}
2212b6eec53SKonstantin Belousov 
2222b6eec53SKonstantin Belousov 		WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
2232b6eec53SKonstantin Belousov 		WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
2242b6eec53SKonstantin Belousov 
2252b6eec53SKonstantin Belousov 		WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER) &
2262b6eec53SKonstantin Belousov 		    ~(EFER_LMA));
2272b6eec53SKonstantin Belousov 		WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
2282b6eec53SKonstantin Belousov 		WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
2292b6eec53SKonstantin Belousov 		WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
2302b6eec53SKonstantin Belousov 
2312b6eec53SKonstantin Belousov 		/* Call ACPICA to enter the desired sleep state */
2322b6eec53SKonstantin Belousov 		if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
2332b6eec53SKonstantin Belousov 			status = AcpiEnterSleepStateS4bios();
2342b6eec53SKonstantin Belousov 		else
2352b6eec53SKonstantin Belousov 			status = AcpiEnterSleepState(state);
2362b6eec53SKonstantin Belousov 		if (ACPI_FAILURE(status)) {
2372b6eec53SKonstantin Belousov 			device_printf(sc->acpi_dev,
2382b6eec53SKonstantin Belousov 			    "AcpiEnterSleepState failed - %s\n",
2392b6eec53SKonstantin Belousov 			    AcpiFormatException(status));
2402b6eec53SKonstantin Belousov 			return (0);	/* couldn't sleep */
2412b6eec53SKonstantin Belousov 		}
2422b6eec53SKonstantin Belousov 
2432b6eec53SKonstantin Belousov 		if (acpi_susp_bounce)
2442b6eec53SKonstantin Belousov 			resumectx(pcb);
2452b6eec53SKonstantin Belousov 
2462b6eec53SKonstantin Belousov 		for (;;)
2472b6eec53SKonstantin Belousov 			ia32_pause();
2482b6eec53SKonstantin Belousov 	} else {
2492b6eec53SKonstantin Belousov 		/*
250ff6d6094SKonstantin Belousov 		 * Re-initialize console hardware as soon as possible.
2512b6eec53SKonstantin Belousov 		 * No console output (e.g. printf) is allowed before
2522b6eec53SKonstantin Belousov 		 * this point.
2532b6eec53SKonstantin Belousov 		 */
2542b6eec53SKonstantin Belousov 		cnresume();
2552b6eec53SKonstantin Belousov 		fpuresume(susppcbs[0]->sp_fpususpend);
2562b6eec53SKonstantin Belousov 	}
2572b6eec53SKonstantin Belousov 
2582b6eec53SKonstantin Belousov 	return (1);	/* wakeup successfully */
2592b6eec53SKonstantin Belousov }
2602b6eec53SKonstantin Belousov 
2612b6eec53SKonstantin Belousov int
2622b6eec53SKonstantin Belousov acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
2632b6eec53SKonstantin Belousov     int intr_enabled)
2642b6eec53SKonstantin Belousov {
2652b6eec53SKonstantin Belousov 
2662b6eec53SKonstantin Belousov 	if (sleep_result == -1)
2672b6eec53SKonstantin Belousov 		return (sleep_result);
2682b6eec53SKonstantin Belousov 
2692b6eec53SKonstantin Belousov 	if (!intr_enabled) {
2702b6eec53SKonstantin Belousov 		/* Wakeup MD procedures in interrupt disabled context */
2712b6eec53SKonstantin Belousov 		if (sleep_result == 1) {
2722b6eec53SKonstantin Belousov 			ucode_reload();
2732b6eec53SKonstantin Belousov 			pmap_init_pat();
2742b6eec53SKonstantin Belousov 			initializecpu();
2752b6eec53SKonstantin Belousov 			PCPU_SET(switchtime, 0);
2762b6eec53SKonstantin Belousov 			PCPU_SET(switchticks, ticks);
2772b6eec53SKonstantin Belousov 			lapic_xapic_mode();
2782b6eec53SKonstantin Belousov #ifdef SMP
2792b6eec53SKonstantin Belousov 			if (!CPU_EMPTY(&suspcpus))
2802b6eec53SKonstantin Belousov 				acpi_wakeup_cpus(sc);
2812b6eec53SKonstantin Belousov #endif
2822b6eec53SKonstantin Belousov 		}
2832b6eec53SKonstantin Belousov 
2842b6eec53SKonstantin Belousov #ifdef SMP
2852b6eec53SKonstantin Belousov 		if (!CPU_EMPTY(&suspcpus))
2862b6eec53SKonstantin Belousov 			resume_cpus(suspcpus);
2872b6eec53SKonstantin Belousov #endif
28802904a06SKonstantin Belousov 
28902904a06SKonstantin Belousov 		/*
29002904a06SKonstantin Belousov 		 * Re-read cpu_stdext_feature3, which was zeroed-out
2912b4b3789SKonstantin Belousov 		 * in acpi_sleep_machdep(), after the microcode was
2922b4b3789SKonstantin Belousov 		 * reloaded.  Then recalculate the active mitigation
29302904a06SKonstantin Belousov 		 * knobs that depend on the microcode and
294cd137909SKonstantin Belousov 		 * cpu_stdext_feature3.  Do it after LAPICs are woken,
295cd137909SKonstantin Belousov 		 * so that IPIs work.
29602904a06SKonstantin Belousov 		 */
29702904a06SKonstantin Belousov 		identify_cpu_ext_features();
29802904a06SKonstantin Belousov 
2992b6eec53SKonstantin Belousov 		mca_resume();
3002b6eec53SKonstantin Belousov 		if (vmm_resume_p != NULL)
3012b6eec53SKonstantin Belousov 			vmm_resume_p();
3022b6eec53SKonstantin Belousov 		intr_resume(/*suspend_cancelled*/false);
3032b6eec53SKonstantin Belousov 
304cd137909SKonstantin Belousov 		hw_ibrs_recalculate(true);
305cd137909SKonstantin Belousov 		amd64_syscall_ret_flush_l1d_recalc();
306cd137909SKonstantin Belousov 		hw_ssb_recalculate(true);
307cd137909SKonstantin Belousov 		x86_rngds_mitg_recalculate(true);
308ebaea1bcSOlivier Certner 		zenbleed_check_and_apply(true);
309cd137909SKonstantin Belousov 
3102b6eec53SKonstantin Belousov 		AcpiSetFirmwareWakingVector(0, 0);
3112b6eec53SKonstantin Belousov 	} else {
3122b6eec53SKonstantin Belousov 		/* Wakeup MD procedures in interrupt enabled context */
3132b6eec53SKonstantin Belousov 		if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
3142b6eec53SKonstantin Belousov 		    mem_range_softc.mr_op->reinit != NULL)
3152b6eec53SKonstantin Belousov 			mem_range_softc.mr_op->reinit(&mem_range_softc);
3162b6eec53SKonstantin Belousov 	}
3172b6eec53SKonstantin Belousov 
3182b6eec53SKonstantin Belousov 	return (sleep_result);
3192b6eec53SKonstantin Belousov }
3202b6eec53SKonstantin Belousov 
3211c56781cSKonstantin Belousov static void
3221c56781cSKonstantin Belousov acpi_alloc_wakeup_handler(void **wakeaddr,
3231c56781cSKonstantin Belousov     void *wakept_pages[ACPI_WAKEPT_PAGES])
3242b6eec53SKonstantin Belousov {
3251c56781cSKonstantin Belousov 	vm_page_t wakept_m[ACPI_WAKEPT_PAGES];
3262b6eec53SKonstantin Belousov 	int i;
3272b6eec53SKonstantin Belousov 
3281c56781cSKonstantin Belousov 	*wakeaddr = NULL;
3291c56781cSKonstantin Belousov 	memset(wakept_pages, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_pages));
3301c56781cSKonstantin Belousov 	memset(wakept_m, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_m));
3312b6eec53SKonstantin Belousov 
3322b6eec53SKonstantin Belousov 	/*
3331c56781cSKonstantin Belousov 	 * Specify the region for our wakeup code.  We want it in the
3341c56781cSKonstantin Belousov 	 * low 1 MB region, excluding real mode IVT (0-0x3ff), BDA
3351c56781cSKonstantin Belousov 	 * (0x400-0x4ff), EBDA (less than 128KB, below 0xa0000, must
3361c56781cSKonstantin Belousov 	 * be excluded by SMAP and DSDT), and ROM area (0xa0000 and
3371c56781cSKonstantin Belousov 	 * above).
3382b6eec53SKonstantin Belousov 	 */
3391c56781cSKonstantin Belousov 	*wakeaddr = contigmalloc(PAGE_SIZE, M_DEVBUF,
3402b6eec53SKonstantin Belousov 	    M_NOWAIT, 0x500, 0xa0000, PAGE_SIZE, 0ul);
3411c56781cSKonstantin Belousov 	if (*wakeaddr == NULL) {
3422b6eec53SKonstantin Belousov 		printf("%s: can't alloc wake memory\n", __func__);
3432b6eec53SKonstantin Belousov 		goto freepages;
3442b6eec53SKonstantin Belousov 	}
3451c56781cSKonstantin Belousov 
3461c56781cSKonstantin Belousov 	for (i = 0; i < ACPI_WAKEPT_PAGES - (la57 ? 0 : 1); i++) {
3471c56781cSKonstantin Belousov 		wakept_m[i] = pmap_page_alloc_below_4g(true);
3481c56781cSKonstantin Belousov 		wakept_pages[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
3491c56781cSKonstantin Belousov 		    wakept_m[i]));
3502b6eec53SKonstantin Belousov 	}
3512b6eec53SKonstantin Belousov 	if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
3522b6eec53SKonstantin Belousov 	    EVENTHANDLER_PRI_LAST) == NULL) {
3532b6eec53SKonstantin Belousov 		printf("%s: can't register event handler\n", __func__);
3542b6eec53SKonstantin Belousov 		goto freepages;
3552b6eec53SKonstantin Belousov 	}
3562b6eec53SKonstantin Belousov 	susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
3572b6eec53SKonstantin Belousov 	for (i = 0; i < mp_ncpus; i++) {
3582b6eec53SKonstantin Belousov 		susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
3592b6eec53SKonstantin Belousov 		susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
3602b6eec53SKonstantin Belousov 	}
3611c56781cSKonstantin Belousov 	return;
3622b6eec53SKonstantin Belousov 
3632b6eec53SKonstantin Belousov freepages:
364*d1bdc282SBjoern A. Zeeb 	free(*wakeaddr, M_DEVBUF);
3651c56781cSKonstantin Belousov 	for (i = 0; i < ACPI_WAKEPT_PAGES; i++) {
3661c56781cSKonstantin Belousov 		if (wakept_m[i] != NULL)
3671c56781cSKonstantin Belousov 			vm_page_free(wakept_m[i]);
3681c56781cSKonstantin Belousov 	}
3691c56781cSKonstantin Belousov 	*wakeaddr = NULL;
3702b6eec53SKonstantin Belousov }
3712b6eec53SKonstantin Belousov 
3722b6eec53SKonstantin Belousov void
3732b6eec53SKonstantin Belousov acpi_install_wakeup_handler(struct acpi_softc *sc)
3742b6eec53SKonstantin Belousov {
3752b6eec53SKonstantin Belousov 	static void *wakeaddr;
3761c56781cSKonstantin Belousov 	void *wakept_pages[ACPI_WAKEPT_PAGES];
3772b6eec53SKonstantin Belousov 	uint64_t *pt5, *pt4, *pt3, *pt2_0, *pt2_1, *pt2_2, *pt2_3;
3782b6eec53SKonstantin Belousov 	vm_paddr_t pt5pa, pt4pa, pt3pa, pt2_0pa, pt2_1pa, pt2_2pa, pt2_3pa;
3792b6eec53SKonstantin Belousov 	int i;
3802b6eec53SKonstantin Belousov 
3812b6eec53SKonstantin Belousov 	if (wakeaddr != NULL)
3822b6eec53SKonstantin Belousov 		return;
3831c56781cSKonstantin Belousov 	acpi_alloc_wakeup_handler(&wakeaddr, wakept_pages);
3841c56781cSKonstantin Belousov 	if (wakeaddr == NULL)
3852b6eec53SKonstantin Belousov 		return;
3862b6eec53SKonstantin Belousov 
3872b6eec53SKonstantin Belousov 	sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
3882b6eec53SKonstantin Belousov 	sc->acpi_wakephys = vtophys(wakeaddr);
3892b6eec53SKonstantin Belousov 
3902b6eec53SKonstantin Belousov 	if (la57) {
3911c56781cSKonstantin Belousov 		pt5 = wakept_pages[6];
3922b6eec53SKonstantin Belousov 		pt5pa = vtophys(pt5);
3932b6eec53SKonstantin Belousov 	}
3941c56781cSKonstantin Belousov 	pt4 = wakept_pages[0];
3951c56781cSKonstantin Belousov 	pt3 = wakept_pages[1];
3961c56781cSKonstantin Belousov 	pt2_0 = wakept_pages[2];
3971c56781cSKonstantin Belousov 	pt2_1 = wakept_pages[3];
3981c56781cSKonstantin Belousov 	pt2_2 = wakept_pages[4];
3991c56781cSKonstantin Belousov 	pt2_3 = wakept_pages[5];
4002b6eec53SKonstantin Belousov 	pt4pa = vtophys(pt4);
4012b6eec53SKonstantin Belousov 	pt3pa = vtophys(pt3);
4022b6eec53SKonstantin Belousov 	pt2_0pa = vtophys(pt2_0);
4032b6eec53SKonstantin Belousov 	pt2_1pa = vtophys(pt2_1);
4042b6eec53SKonstantin Belousov 	pt2_2pa = vtophys(pt2_2);
4052b6eec53SKonstantin Belousov 	pt2_3pa = vtophys(pt2_3);
4062b6eec53SKonstantin Belousov 
4072b6eec53SKonstantin Belousov 	bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
4082b6eec53SKonstantin Belousov 
4092b6eec53SKonstantin Belousov 	/* Patch GDT base address, ljmp targets. */
4102b6eec53SKonstantin Belousov 	WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
4112b6eec53SKonstantin Belousov 	    sc->acpi_wakephys + bootgdt);
4122b6eec53SKonstantin Belousov 	WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
4132b6eec53SKonstantin Belousov 	    sc->acpi_wakephys + wakeup_32);
4142b6eec53SKonstantin Belousov 	WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
4152b6eec53SKonstantin Belousov 	    sc->acpi_wakephys + wakeup_64);
4162b6eec53SKonstantin Belousov 	WAKECODE_FIXUP(wakeup_pagetables, uint32_t, la57 ? (pt5pa | 0x1) :
4172b6eec53SKonstantin Belousov 	    pt4pa);
4182b6eec53SKonstantin Belousov 
4192b6eec53SKonstantin Belousov 	/* Save pointers to some global data. */
4202b6eec53SKonstantin Belousov 	WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
4212b6eec53SKonstantin Belousov 	/* Create 1:1 mapping for the low 4G */
4222b6eec53SKonstantin Belousov 	if (la57) {
4232b6eec53SKonstantin Belousov 		bcopy(kernel_pmap->pm_pmltop, pt5, PAGE_SIZE);
4242b6eec53SKonstantin Belousov 		pt5[0] = (uint64_t)pt4pa;
4252b6eec53SKonstantin Belousov 		pt5[0] |= PG_V | PG_RW | PG_U;
4262b6eec53SKonstantin Belousov 	} else {
4272b6eec53SKonstantin Belousov 		bcopy(kernel_pmap->pm_pmltop, pt4, PAGE_SIZE);
4282b6eec53SKonstantin Belousov 	}
4292b6eec53SKonstantin Belousov 
4302b6eec53SKonstantin Belousov 	pt4[0] = (uint64_t)pt3pa;
4312b6eec53SKonstantin Belousov 	pt4[0] |= PG_V | PG_RW | PG_U;
4322b6eec53SKonstantin Belousov 
4332b6eec53SKonstantin Belousov 	pt3[0] = (uint64_t)pt2_0pa;
4342b6eec53SKonstantin Belousov 	pt3[0] |= PG_V | PG_RW | PG_U;
4352b6eec53SKonstantin Belousov 	pt3[1] = (uint64_t)pt2_1pa;
4362b6eec53SKonstantin Belousov 	pt3[1] |= PG_V | PG_RW | PG_U;
4372b6eec53SKonstantin Belousov 	pt3[2] = (uint64_t)pt2_2pa;
4382b6eec53SKonstantin Belousov 	pt3[2] |= PG_V | PG_RW | PG_U;
4392b6eec53SKonstantin Belousov 	pt3[3] = (uint64_t)pt2_3pa;
4402b6eec53SKonstantin Belousov 	pt3[3] |= PG_V | PG_RW | PG_U;
4412b6eec53SKonstantin Belousov 
4422b6eec53SKonstantin Belousov 	for (i = 0; i < NPDEPG; i++) {
4432b6eec53SKonstantin Belousov 		pt2_0[i] = (pd_entry_t)i * NBPDR;
4442b6eec53SKonstantin Belousov 		pt2_0[i] |= PG_V | PG_RW | PG_PS | PG_U;
4452b6eec53SKonstantin Belousov 	}
4462b6eec53SKonstantin Belousov 	for (i = 0; i < NPDEPG; i++) {
4472b6eec53SKonstantin Belousov 		pt2_1[i] = (pd_entry_t)NBPDP + i * NBPDR;
4482b6eec53SKonstantin Belousov 		pt2_1[i] |= PG_V | PG_RW | PG_PS | PG_U;
4492b6eec53SKonstantin Belousov 	}
4502b6eec53SKonstantin Belousov 	for (i = 0; i < NPDEPG; i++) {
4512b6eec53SKonstantin Belousov 		pt2_2[i] = (pd_entry_t)2 * NBPDP + i * NBPDR;
4522b6eec53SKonstantin Belousov 		pt2_2[i] |= PG_V | PG_RW | PG_PS | PG_U;
4532b6eec53SKonstantin Belousov 	}
4542b6eec53SKonstantin Belousov 	for (i = 0; i < NPDEPG; i++) {
4552b6eec53SKonstantin Belousov 		pt2_3[i] = (pd_entry_t)3 * NBPDP + i * NBPDR;
4562b6eec53SKonstantin Belousov 		pt2_3[i] |= PG_V | PG_RW | PG_PS | PG_U;
4572b6eec53SKonstantin Belousov 	}
4582b6eec53SKonstantin Belousov 
4592b6eec53SKonstantin Belousov 	if (bootverbose)
4602b6eec53SKonstantin Belousov 		device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
4612b6eec53SKonstantin Belousov 		    (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
4622b6eec53SKonstantin Belousov }
463