xref: /freebsd/sys/i386/acpica/acpi_wakeup.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_alloc_wakeup_handler(void **);
90 static void		acpi_stop_beep(void *);
91 
92 #ifdef SMP
93 static int		acpi_wakeup_ap(struct acpi_softc *, int);
94 static void		acpi_wakeup_cpus(struct acpi_softc *);
95 #endif
96 
97 #define	ACPI_WAKEPAGES	1
98 
99 #define	WAKECODE_FIXUP(offset, type, val)	do {	\
100 	type	*addr;					\
101 	addr = (type *)(sc->acpi_wakeaddr + (offset));	\
102 	*addr = val;					\
103 } while (0)
104 
105 static void
106 acpi_stop_beep(void *arg)
107 {
108 
109 	if (acpi_resume_beep != 0)
110 		timer_spkr_release();
111 }
112 
113 #ifdef SMP
114 static int
115 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
116 {
117 	struct pcb *pcb;
118 	int		vector = (sc->acpi_wakephys >> 12) & 0xff;
119 	int		apic_id = cpu_apic_ids[cpu];
120 	int		ms;
121 
122 	pcb = &susppcbs[cpu]->sp_pcb;
123 	WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
124 	WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
125 	WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
126 
127 	ipi_startup(apic_id, vector);
128 
129 	/* Wait up to 5 seconds for it to resume. */
130 	for (ms = 0; ms < 5000; ms++) {
131 		if (!CPU_ISSET(cpu, &suspended_cpus))
132 			return (1);	/* return SUCCESS */
133 		DELAY(1000);
134 	}
135 	return (0);		/* return FAILURE */
136 }
137 
138 #define	WARMBOOT_TARGET		0
139 #define	WARMBOOT_OFF		(PMAP_MAP_LOW + 0x0467)
140 #define	WARMBOOT_SEG		(PMAP_MAP_LOW + 0x0469)
141 
142 #define	CMOS_REG		(0x70)
143 #define	CMOS_DATA		(0x71)
144 #define	BIOS_RESET		(0x0f)
145 #define	BIOS_WARM		(0x0a)
146 
147 static void
148 acpi_wakeup_cpus(struct acpi_softc *sc)
149 {
150 	uint32_t	mpbioswarmvec;
151 	int		cpu;
152 	u_char		mpbiosreason;
153 
154 	/* save the current value of the warm-start vector */
155 	mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
156 	outb(CMOS_REG, BIOS_RESET);
157 	mpbiosreason = inb(CMOS_DATA);
158 
159 	/* setup a vector to our boot code */
160 	*((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
161 	*((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
162 	outb(CMOS_REG, BIOS_RESET);
163 	outb(CMOS_DATA, BIOS_WARM);	/* 'warm-start' */
164 
165 	/* Wake up each AP. */
166 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
167 		if (!CPU_ISSET(cpu, &suspcpus))
168 			continue;
169 		if (acpi_wakeup_ap(sc, cpu) == 0) {
170 			/* restore the warmstart vector */
171 			*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
172 			panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
173 			    cpu, cpu_apic_ids[cpu]);
174 		}
175 	}
176 
177 	/*
178 	 * Remove the identity mapping of low memory for all CPUs and sync
179 	 * the TLB for the BSP.  The APs are now spinning in
180 	 * cpususpend_handler() and we will release them soon.  Then each
181 	 * will invalidate its TLB.
182 	 */
183 	pmap_remap_lowptdi(false);
184 
185 	/* restore the warmstart vector */
186 	*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
187 
188 	outb(CMOS_REG, BIOS_RESET);
189 	outb(CMOS_DATA, mpbiosreason);
190 }
191 #endif
192 
193 int
194 acpi_sleep_machdep(struct acpi_softc *sc, int state)
195 {
196 	ACPI_STATUS	status;
197 	struct pcb	*pcb;
198 
199 	if (sc->acpi_wakeaddr == 0ul)
200 		return (-1);	/* couldn't alloc wake memory */
201 
202 #ifdef SMP
203 	suspcpus = all_cpus;
204 	CPU_CLR(PCPU_GET(cpuid), &suspcpus);
205 #endif
206 
207 	if (acpi_resume_beep != 0)
208 		timer_spkr_acquire();
209 
210 	AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
211 
212 	intr_suspend();
213 
214 	pcb = &susppcbs[0]->sp_pcb;
215 	if (savectx(pcb)) {
216 		npxsuspend(susppcbs[0]->sp_fpususpend);
217 #ifdef SMP
218 		if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
219 			device_printf(sc->acpi_dev, "Failed to suspend APs\n");
220 			return (0);	/* couldn't sleep */
221 		}
222 #endif
223 
224 		WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
225 		WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
226 
227 		if ((amd_feature & AMDID_NX) != 0)
228 			WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER));
229 		WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
230 		WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
231 		WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
232 		WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
233 
234 		/*
235 		 * Map some low memory with virt == phys for ACPI wakecode
236 		 * to use to jump to high memory after enabling paging. This
237 		 * is the same as for similar jump in locore, except the
238 		 * jump is a single instruction, and we know its address
239 		 * more precisely so only need a single PTD, and we have to
240 		 * be careful to use the kernel map (PTD[0] is for curthread
241 		 * which may be a user thread in deprecated APIs).
242 		 */
243 		pmap_remap_lowptdi(true);
244 
245 		/* Call ACPICA to enter the desired sleep state */
246 		if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
247 			status = AcpiEnterSleepStateS4bios();
248 		else
249 			status = AcpiEnterSleepState(state);
250 		if (ACPI_FAILURE(status)) {
251 			device_printf(sc->acpi_dev,
252 			    "AcpiEnterSleepState failed - %s\n",
253 			    AcpiFormatException(status));
254 			return (0);	/* couldn't sleep */
255 		}
256 
257 		if (acpi_susp_bounce)
258 			resumectx(pcb);
259 
260 		for (;;)
261 			ia32_pause();
262 	} else {
263 		/*
264 		 * Re-initialize console hardware as soon as possibe.
265 		 * No console output (e.g. printf) is allowed before
266 		 * this point.
267 		 */
268 		cnresume();
269 		npxresume(susppcbs[0]->sp_fpususpend);
270 	}
271 
272 	return (1);	/* wakeup successfully */
273 }
274 
275 int
276 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
277     int intr_enabled)
278 {
279 
280 	if (sleep_result == -1)
281 		return (sleep_result);
282 
283 	if (!intr_enabled) {
284 		/* Wakeup MD procedures in interrupt disabled context */
285 		if (sleep_result == 1) {
286 			ucode_reload();
287 			pmap_init_pat();
288 			initializecpu();
289 			PCPU_SET(switchtime, 0);
290 			PCPU_SET(switchticks, ticks);
291 #ifdef DEV_APIC
292 			lapic_xapic_mode();
293 #endif
294 #ifdef SMP
295 			if (!CPU_EMPTY(&suspcpus))
296 				acpi_wakeup_cpus(sc);
297 #endif
298 		}
299 
300 #ifdef SMP
301 		if (!CPU_EMPTY(&suspcpus))
302 			resume_cpus(suspcpus);
303 #endif
304 		mca_resume();
305 		intr_resume(/*suspend_cancelled*/false);
306 
307 		AcpiSetFirmwareWakingVector(0, 0);
308 	} else {
309 		/* Wakeup MD procedures in interrupt enabled context */
310 		if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
311 		    mem_range_softc.mr_op->reinit != NULL)
312 			mem_range_softc.mr_op->reinit(&mem_range_softc);
313 	}
314 
315 	return (sleep_result);
316 }
317 
318 static void *
319 acpi_alloc_wakeup_handler(void *wakepages[ACPI_WAKEPAGES])
320 {
321 	int		i;
322 
323 	memset(wakepages, 0, ACPI_WAKEPAGES * sizeof(*wakepages));
324 
325 	/*
326 	 * Specify the region for our wakeup code.  We want it in the low 1 MB
327 	 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
328 	 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
329 	 * and ROM area (0xa0000 and above).  The temporary page tables must be
330 	 * page-aligned.
331 	 */
332 	for (i = 0; i < ACPI_WAKEPAGES; i++) {
333 		wakepages[i] = contigmalloc(PAGE_SIZE, M_DEVBUF,
334 		    M_NOWAIT | M_EXEC, 0x500, 0xa0000, PAGE_SIZE, 0ul);
335 		if (wakepages[i] == NULL) {
336 			printf("%s: can't alloc wake memory\n", __func__);
337 			goto freepages;
338 		}
339 	}
340 	if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
341 	    EVENTHANDLER_PRI_LAST) == NULL) {
342 		printf("%s: can't register event handler\n", __func__);
343 		goto freepages;
344 	}
345 	susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
346 	for (i = 0; i < mp_ncpus; i++) {
347 		susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
348 		susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
349 	}
350 
351 	return (wakepages);
352 
353 freepages:
354 	for (i = 0; i < ACPI_WAKEPAGES; i++)
355 		if (wakepages[i] != NULL)
356 			contigfree(wakepages[i], PAGE_SIZE, M_DEVBUF);
357 	return (NULL);
358 }
359 
360 void
361 acpi_install_wakeup_handler(struct acpi_softc *sc)
362 {
363 	static void *wakeaddr;
364 	void *wakepages[ACPI_WAKEPAGES];
365 
366 	if (wakeaddr != NULL)
367 		return;
368 
369 	if (acpi_alloc_wakeup_handler(wakepages) == NULL)
370 		return;
371 
372 	wakeaddr = wakepages[0];
373 	sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
374 	sc->acpi_wakephys = vtophys(wakeaddr);
375 
376 	bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
377 
378 	/* Patch GDT base address, ljmp targets. */
379 	WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
380 	    sc->acpi_wakephys + bootgdt);
381 	WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
382 	    sc->acpi_wakephys + wakeup_32);
383 
384 	/* Save pointers to some global data. */
385 	WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
386 	WAKECODE_FIXUP(wakeup_cr3, register_t, pmap_get_kcr3());
387 
388 	if (bootverbose)
389 		device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
390 		    (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
391 }
392