xref: /freebsd/sys/i386/acpica/acpi_wakeup.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
5  * Copyright (c) 2001-2012 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
6  * Copyright (c) 2003 Peter Wemm
7  * Copyright (c) 2008-2012 Jung-uk Kim <jkim@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_apic.h"
36 
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/memrange.h>
43 #include <sys/smp.h>
44 #include <sys/systm.h>
45 #include <sys/cons.h>
46 
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 
50 #include <machine/clock.h>
51 #include <machine/cpu.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/md_var.h>
54 #include <x86/mca.h>
55 #include <machine/pcb.h>
56 #include <machine/specialreg.h>
57 #include <x86/ucode.h>
58 
59 #ifdef DEV_APIC
60 #include <x86/apicreg.h>
61 #include <x86/apicvar.h>
62 #endif
63 #ifdef SMP
64 #include <machine/smp.h>
65 #include <machine/vmparam.h>
66 #endif
67 
68 #include <contrib/dev/acpica/include/acpi.h>
69 
70 #include <dev/acpica/acpivar.h>
71 
72 #include "acpi_wakecode.h"
73 #include "acpi_wakedata.h"
74 
75 /* Make sure the code is less than a page and leave room for the stack. */
76 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
77 
78 extern int		acpi_resume_beep;
79 extern int		acpi_reset_video;
80 extern int		acpi_susp_bounce;
81 
82 #ifdef SMP
83 extern struct susppcb	**susppcbs;
84 static cpuset_t		suspcpus;
85 #else
86 static struct susppcb	**susppcbs;
87 #endif
88 
89 static void		acpi_stop_beep(void *);
90 
91 #ifdef SMP
92 static int		acpi_wakeup_ap(struct acpi_softc *, int);
93 static void		acpi_wakeup_cpus(struct acpi_softc *);
94 #endif
95 
96 #define	ACPI_WAKEPAGES	1
97 
98 #define	WAKECODE_FIXUP(offset, type, val)	do {	\
99 	type	*addr;					\
100 	addr = (type *)(sc->acpi_wakeaddr + (offset));	\
101 	*addr = val;					\
102 } while (0)
103 
104 static void
105 acpi_stop_beep(void *arg)
106 {
107 
108 	if (acpi_resume_beep != 0)
109 		timer_spkr_release();
110 }
111 
112 #ifdef SMP
113 static int
114 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
115 {
116 	struct pcb *pcb;
117 	int		vector = (sc->acpi_wakephys >> 12) & 0xff;
118 	int		apic_id = cpu_apic_ids[cpu];
119 	int		ms;
120 
121 	pcb = &susppcbs[cpu]->sp_pcb;
122 	WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
123 	WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
124 	WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
125 
126 	ipi_startup(apic_id, vector);
127 
128 	/* Wait up to 5 seconds for it to resume. */
129 	for (ms = 0; ms < 5000; ms++) {
130 		if (!CPU_ISSET(cpu, &suspended_cpus))
131 			return (1);	/* return SUCCESS */
132 		DELAY(1000);
133 	}
134 	return (0);		/* return FAILURE */
135 }
136 
137 #define	WARMBOOT_TARGET		0
138 #define	WARMBOOT_OFF		(PMAP_MAP_LOW + 0x0467)
139 #define	WARMBOOT_SEG		(PMAP_MAP_LOW + 0x0469)
140 
141 #define	CMOS_REG		(0x70)
142 #define	CMOS_DATA		(0x71)
143 #define	BIOS_RESET		(0x0f)
144 #define	BIOS_WARM		(0x0a)
145 
146 static void
147 acpi_wakeup_cpus(struct acpi_softc *sc)
148 {
149 	uint32_t	mpbioswarmvec;
150 	int		cpu;
151 	u_char		mpbiosreason;
152 
153 	/* save the current value of the warm-start vector */
154 	mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
155 	outb(CMOS_REG, BIOS_RESET);
156 	mpbiosreason = inb(CMOS_DATA);
157 
158 	/* setup a vector to our boot code */
159 	*((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
160 	*((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
161 	outb(CMOS_REG, BIOS_RESET);
162 	outb(CMOS_DATA, BIOS_WARM);	/* 'warm-start' */
163 
164 	/* Wake up each AP. */
165 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
166 		if (!CPU_ISSET(cpu, &suspcpus))
167 			continue;
168 		if (acpi_wakeup_ap(sc, cpu) == 0) {
169 			/* restore the warmstart vector */
170 			*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
171 			panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
172 			    cpu, cpu_apic_ids[cpu]);
173 		}
174 	}
175 
176 	/*
177 	 * Remove the identity mapping of low memory for all CPUs and sync
178 	 * the TLB for the BSP.  The APs are now spinning in
179 	 * cpususpend_handler() and we will release them soon.  Then each
180 	 * will invalidate its TLB.
181 	 */
182 	pmap_remap_lowptdi(false);
183 
184 	/* restore the warmstart vector */
185 	*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
186 
187 	outb(CMOS_REG, BIOS_RESET);
188 	outb(CMOS_DATA, mpbiosreason);
189 }
190 #endif
191 
192 int
193 acpi_sleep_machdep(struct acpi_softc *sc, int state)
194 {
195 	ACPI_STATUS	status;
196 	struct pcb	*pcb;
197 
198 	if (sc->acpi_wakeaddr == 0ul)
199 		return (-1);	/* couldn't alloc wake memory */
200 
201 #ifdef SMP
202 	suspcpus = all_cpus;
203 	CPU_CLR(PCPU_GET(cpuid), &suspcpus);
204 #endif
205 
206 	if (acpi_resume_beep != 0)
207 		timer_spkr_acquire();
208 
209 	AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
210 
211 	intr_suspend();
212 
213 	pcb = &susppcbs[0]->sp_pcb;
214 	if (savectx(pcb)) {
215 		npxsuspend(susppcbs[0]->sp_fpususpend);
216 #ifdef SMP
217 		if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
218 			device_printf(sc->acpi_dev, "Failed to suspend APs\n");
219 			return (0);	/* couldn't sleep */
220 		}
221 #endif
222 
223 		WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
224 		WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
225 
226 		if ((amd_feature & AMDID_NX) != 0)
227 			WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER));
228 		WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
229 		WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
230 		WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
231 		WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
232 
233 		/*
234 		 * Map some low memory with virt == phys for ACPI wakecode
235 		 * to use to jump to high memory after enabling paging. This
236 		 * is the same as for similar jump in locore, except the
237 		 * jump is a single instruction, and we know its address
238 		 * more precisely so only need a single PTD, and we have to
239 		 * be careful to use the kernel map (PTD[0] is for curthread
240 		 * which may be a user thread in deprecated APIs).
241 		 */
242 		pmap_remap_lowptdi(true);
243 
244 		/* Call ACPICA to enter the desired sleep state */
245 		if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
246 			status = AcpiEnterSleepStateS4bios();
247 		else
248 			status = AcpiEnterSleepState(state);
249 		if (ACPI_FAILURE(status)) {
250 			device_printf(sc->acpi_dev,
251 			    "AcpiEnterSleepState failed - %s\n",
252 			    AcpiFormatException(status));
253 			return (0);	/* couldn't sleep */
254 		}
255 
256 		if (acpi_susp_bounce)
257 			resumectx(pcb);
258 
259 		for (;;)
260 			ia32_pause();
261 	} else {
262 		/*
263 		 * Re-initialize console hardware as soon as possibe.
264 		 * No console output (e.g. printf) is allowed before
265 		 * this point.
266 		 */
267 		cnresume();
268 		npxresume(susppcbs[0]->sp_fpususpend);
269 	}
270 
271 	return (1);	/* wakeup successfully */
272 }
273 
274 int
275 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
276     int intr_enabled)
277 {
278 
279 	if (sleep_result == -1)
280 		return (sleep_result);
281 
282 	if (!intr_enabled) {
283 		/* Wakeup MD procedures in interrupt disabled context */
284 		if (sleep_result == 1) {
285 			ucode_reload();
286 			pmap_init_pat();
287 			initializecpu();
288 			PCPU_SET(switchtime, 0);
289 			PCPU_SET(switchticks, ticks);
290 #ifdef DEV_APIC
291 			lapic_xapic_mode();
292 #endif
293 #ifdef SMP
294 			if (!CPU_EMPTY(&suspcpus))
295 				acpi_wakeup_cpus(sc);
296 #endif
297 		}
298 
299 #ifdef SMP
300 		if (!CPU_EMPTY(&suspcpus))
301 			resume_cpus(suspcpus);
302 #endif
303 		mca_resume();
304 		intr_resume(/*suspend_cancelled*/false);
305 
306 		AcpiSetFirmwareWakingVector(0, 0);
307 	} else {
308 		/* Wakeup MD procedures in interrupt enabled context */
309 		if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
310 		    mem_range_softc.mr_op->reinit != NULL)
311 			mem_range_softc.mr_op->reinit(&mem_range_softc);
312 	}
313 
314 	return (sleep_result);
315 }
316 
317 static void *
318 acpi_alloc_wakeup_handler(void *wakepages[ACPI_WAKEPAGES])
319 {
320 	int		i;
321 
322 	memset(wakepages, 0, ACPI_WAKEPAGES * sizeof(*wakepages));
323 
324 	/*
325 	 * Specify the region for our wakeup code.  We want it in the low 1 MB
326 	 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
327 	 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
328 	 * and ROM area (0xa0000 and above).  The temporary page tables must be
329 	 * page-aligned.
330 	 */
331 	for (i = 0; i < ACPI_WAKEPAGES; i++) {
332 		wakepages[i] = contigmalloc(PAGE_SIZE, M_DEVBUF,
333 		    M_NOWAIT | M_EXEC, 0x500, 0xa0000, PAGE_SIZE, 0ul);
334 		if (wakepages[i] == NULL) {
335 			printf("%s: can't alloc wake memory\n", __func__);
336 			goto freepages;
337 		}
338 	}
339 	if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
340 	    EVENTHANDLER_PRI_LAST) == NULL) {
341 		printf("%s: can't register event handler\n", __func__);
342 		goto freepages;
343 	}
344 	susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
345 	for (i = 0; i < mp_ncpus; i++) {
346 		susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
347 		susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
348 	}
349 
350 	return (wakepages);
351 
352 freepages:
353 	for (i = 0; i < ACPI_WAKEPAGES; i++)
354 		if (wakepages[i] != NULL)
355 			contigfree(wakepages[i], PAGE_SIZE, M_DEVBUF);
356 	return (NULL);
357 }
358 
359 void
360 acpi_install_wakeup_handler(struct acpi_softc *sc)
361 {
362 	static void *wakeaddr;
363 	void *wakepages[ACPI_WAKEPAGES];
364 
365 	if (wakeaddr != NULL)
366 		return;
367 
368 	if (acpi_alloc_wakeup_handler(wakepages) == NULL)
369 		return;
370 
371 	wakeaddr = wakepages[0];
372 	sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
373 	sc->acpi_wakephys = vtophys(wakeaddr);
374 
375 	bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
376 
377 	/* Patch GDT base address, ljmp targets. */
378 	WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
379 	    sc->acpi_wakephys + bootgdt);
380 	WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
381 	    sc->acpi_wakephys + wakeup_32);
382 
383 	/* Save pointers to some global data. */
384 	WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
385 	WAKECODE_FIXUP(wakeup_cr3, register_t, pmap_get_kcr3());
386 
387 	if (bootverbose)
388 		device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
389 		    (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
390 }
391