xref: /freebsd/sys/amd64/acpica/acpi_wakeup.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*-
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-2010 Jung-uk Kim <jkim@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/memrange.h>
38 #include <sys/smp.h>
39 
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 
43 #include <machine/intr_machdep.h>
44 #include <x86/mca.h>
45 #include <machine/pcb.h>
46 #include <machine/pmap.h>
47 #include <machine/specialreg.h>
48 
49 #ifdef SMP
50 #include <x86/apicreg.h>
51 #include <machine/smp.h>
52 #include <machine/vmparam.h>
53 #endif
54 
55 #include <contrib/dev/acpica/include/acpi.h>
56 
57 #include <dev/acpica/acpivar.h>
58 
59 #include "acpi_wakecode.h"
60 #include "acpi_wakedata.h"
61 
62 /* Make sure the code is less than a page and leave room for the stack. */
63 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
64 
65 extern int		acpi_resume_beep;
66 extern int		acpi_reset_video;
67 
68 #ifdef SMP
69 extern struct pcb	**susppcbs;
70 #else
71 static struct pcb	**susppcbs;
72 #endif
73 
74 int			acpi_restorecpu(vm_offset_t, struct pcb *);
75 
76 static void		*acpi_alloc_wakeup_handler(void);
77 static void		acpi_stop_beep(void *);
78 
79 #ifdef SMP
80 static int		acpi_wakeup_ap(struct acpi_softc *, int);
81 static void		acpi_wakeup_cpus(struct acpi_softc *, const cpuset_t *);
82 #endif
83 
84 #define	WAKECODE_VADDR(sc)	((sc)->acpi_wakeaddr + (3 * PAGE_SIZE))
85 #define	WAKECODE_PADDR(sc)	((sc)->acpi_wakephys + (3 * PAGE_SIZE))
86 #define	WAKECODE_FIXUP(offset, type, val) do	{	\
87 	type	*addr;					\
88 	addr = (type *)(WAKECODE_VADDR(sc) + offset);	\
89 	*addr = val;					\
90 } while (0)
91 
92 /* Turn off bits 1&2 of the PIT, stopping the beep. */
93 static void
94 acpi_stop_beep(void *arg)
95 {
96 	outb(0x61, inb(0x61) & ~0x3);
97 }
98 
99 #ifdef SMP
100 static int
101 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
102 {
103 	int		vector = (WAKECODE_PADDR(sc) >> 12) & 0xff;
104 	int		apic_id = cpu_apic_ids[cpu];
105 	int		ms;
106 
107 	WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[cpu]);
108 	WAKECODE_FIXUP(wakeup_gdt, uint16_t, susppcbs[cpu]->pcb_gdt.rd_limit);
109 	WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
110 	    susppcbs[cpu]->pcb_gdt.rd_base);
111 	WAKECODE_FIXUP(wakeup_cpu, int, cpu);
112 
113 	/* do an INIT IPI: assert RESET */
114 	lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
115 	    APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id);
116 
117 	/* wait for pending status end */
118 	lapic_ipi_wait(-1);
119 
120 	/* do an INIT IPI: deassert RESET */
121 	lapic_ipi_raw(APIC_DEST_ALLESELF | APIC_TRIGMOD_LEVEL |
122 	    APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, 0);
123 
124 	/* wait for pending status end */
125 	DELAY(10000);		/* wait ~10mS */
126 	lapic_ipi_wait(-1);
127 
128 	/*
129 	 * next we do a STARTUP IPI: the previous INIT IPI might still be
130 	 * latched, (P5 bug) this 1st STARTUP would then terminate
131 	 * immediately, and the previously started INIT IPI would continue. OR
132 	 * the previous INIT IPI has already run. and this STARTUP IPI will
133 	 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
134 	 * will run.
135 	 */
136 
137 	/* do a STARTUP IPI */
138 	lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
139 	    APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
140 	    vector, apic_id);
141 	lapic_ipi_wait(-1);
142 	DELAY(200);		/* wait ~200uS */
143 
144 	/*
145 	 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
146 	 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
147 	 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
148 	 * recognized after hardware RESET or INIT IPI.
149 	 */
150 
151 	lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
152 	    APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
153 	    vector, apic_id);
154 	lapic_ipi_wait(-1);
155 	DELAY(200);		/* wait ~200uS */
156 
157 	/* Wait up to 5 seconds for it to start. */
158 	for (ms = 0; ms < 5000; ms++) {
159 		if (*(int *)(WAKECODE_VADDR(sc) + wakeup_cpu) == 0)
160 			return (1);	/* return SUCCESS */
161 		DELAY(1000);
162 	}
163 	return (0);		/* return FAILURE */
164 }
165 
166 #define	WARMBOOT_TARGET		0
167 #define	WARMBOOT_OFF		(KERNBASE + 0x0467)
168 #define	WARMBOOT_SEG		(KERNBASE + 0x0469)
169 
170 #define	CMOS_REG		(0x70)
171 #define	CMOS_DATA		(0x71)
172 #define	BIOS_RESET		(0x0f)
173 #define	BIOS_WARM		(0x0a)
174 
175 static void
176 acpi_wakeup_cpus(struct acpi_softc *sc, const cpuset_t *wakeup_cpus)
177 {
178 	uint32_t	mpbioswarmvec;
179 	int		cpu;
180 	u_char		mpbiosreason;
181 
182 	/* save the current value of the warm-start vector */
183 	mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
184 	outb(CMOS_REG, BIOS_RESET);
185 	mpbiosreason = inb(CMOS_DATA);
186 
187 	/* setup a vector to our boot code */
188 	*((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
189 	*((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4;
190 	outb(CMOS_REG, BIOS_RESET);
191 	outb(CMOS_DATA, BIOS_WARM);	/* 'warm-start' */
192 
193 	/* Wake up each AP. */
194 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
195 		if (!CPU_ISSET(cpu, wakeup_cpus))
196 			continue;
197 		if (acpi_wakeup_ap(sc, cpu) == 0) {
198 			/* restore the warmstart vector */
199 			*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
200 			panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
201 			    cpu, cpu_apic_ids[cpu]);
202 		}
203 	}
204 
205 	/* restore the warmstart vector */
206 	*(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
207 
208 	outb(CMOS_REG, BIOS_RESET);
209 	outb(CMOS_DATA, mpbiosreason);
210 }
211 #endif
212 
213 int
214 acpi_sleep_machdep(struct acpi_softc *sc, int state)
215 {
216 #ifdef SMP
217 	cpuset_t	wakeup_cpus;
218 #endif
219 	register_t	cr3, rf;
220 	ACPI_STATUS	status;
221 	int		ret;
222 
223 	ret = -1;
224 
225 	if (sc->acpi_wakeaddr == 0ul)
226 		return (ret);
227 
228 #ifdef SMP
229 	wakeup_cpus = all_cpus;
230 	CPU_CLR(PCPU_GET(cpuid), &wakeup_cpus);
231 #endif
232 
233 	AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc));
234 
235 	rf = intr_disable();
236 	intr_suspend();
237 
238 	/*
239 	 * Temporarily switch to the kernel pmap because it provides
240 	 * an identity mapping (setup at boot) for the low physical
241 	 * memory region containing the wakeup code.
242 	 */
243 	cr3 = rcr3();
244 	load_cr3(KPML4phys);
245 
246 	if (savectx(susppcbs[0])) {
247 #ifdef SMP
248 		if (!CPU_EMPTY(&wakeup_cpus) &&
249 		    suspend_cpus(wakeup_cpus) == 0) {
250 			device_printf(sc->acpi_dev, "Failed to suspend APs\n");
251 			goto out;
252 		}
253 #endif
254 
255 		WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
256 		WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
257 
258 		WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[0]);
259 		WAKECODE_FIXUP(wakeup_gdt, uint16_t,
260 		    susppcbs[0]->pcb_gdt.rd_limit);
261 		WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
262 		    susppcbs[0]->pcb_gdt.rd_base);
263 		WAKECODE_FIXUP(wakeup_cpu, int, 0);
264 
265 		/* Call ACPICA to enter the desired sleep state */
266 		if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
267 			status = AcpiEnterSleepStateS4bios();
268 		else
269 			status = AcpiEnterSleepState(state);
270 
271 		if (status != AE_OK) {
272 			device_printf(sc->acpi_dev,
273 			    "AcpiEnterSleepState failed - %s\n",
274 			    AcpiFormatException(status));
275 			goto out;
276 		}
277 
278 		for (;;)
279 			ia32_pause();
280 	} else {
281 		pmap_init_pat();
282 		PCPU_SET(switchtime, 0);
283 		PCPU_SET(switchticks, ticks);
284 #ifdef SMP
285 		if (!CPU_EMPTY(&wakeup_cpus))
286 			acpi_wakeup_cpus(sc, &wakeup_cpus);
287 #endif
288 		acpi_resync_clock(sc);
289 		ret = 0;
290 	}
291 
292 out:
293 #ifdef SMP
294 	if (!CPU_EMPTY(&wakeup_cpus))
295 		restart_cpus(wakeup_cpus);
296 #endif
297 
298 	load_cr3(cr3);
299 	mca_resume();
300 	intr_resume();
301 	intr_restore(rf);
302 
303 	AcpiSetFirmwareWakingVector(0);
304 
305 	if (ret == 0 && mem_range_softc.mr_op != NULL &&
306 	    mem_range_softc.mr_op->reinit != NULL)
307 		mem_range_softc.mr_op->reinit(&mem_range_softc);
308 
309 	/* If we beeped, turn it off after a delay. */
310 	if (acpi_resume_beep)
311 		timeout(acpi_stop_beep, NULL, 3 * hz);
312 
313 	return (ret);
314 }
315 
316 static void *
317 acpi_alloc_wakeup_handler(void)
318 {
319 	void		*wakeaddr;
320 	int		i;
321 
322 	/*
323 	 * Specify the region for our wakeup code.  We want it in the low 1 MB
324 	 * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
325 	 * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
326 	 * and ROM area (0xa0000 and above).  The temporary page tables must be
327 	 * page-aligned.
328 	 */
329 	wakeaddr = contigmalloc(4 * PAGE_SIZE, M_DEVBUF, M_NOWAIT, 0x500,
330 	    0xa0000, PAGE_SIZE, 0ul);
331 	if (wakeaddr == NULL) {
332 		printf("%s: can't alloc wake memory\n", __func__);
333 		return (NULL);
334 	}
335 	susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
336 	for (i = 0; i < mp_ncpus; i++)
337 		susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
338 
339 	return (wakeaddr);
340 }
341 
342 void
343 acpi_install_wakeup_handler(struct acpi_softc *sc)
344 {
345 	static void	*wakeaddr = NULL;
346 	uint64_t	*pt4, *pt3, *pt2;
347 	int		i;
348 
349 	if (wakeaddr != NULL)
350 		return;
351 
352 	wakeaddr = acpi_alloc_wakeup_handler();
353 	if (wakeaddr == NULL)
354 		return;
355 
356 	sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
357 	sc->acpi_wakephys = vtophys(wakeaddr);
358 
359 	bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode));
360 
361 	/* Patch GDT base address, ljmp targets and page table base address. */
362 	WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
363 	    WAKECODE_PADDR(sc) + bootgdt);
364 	WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
365 	    WAKECODE_PADDR(sc) + wakeup_32);
366 	WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
367 	    WAKECODE_PADDR(sc) + wakeup_64);
368 	WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys);
369 
370 	/* Save pointers to some global data. */
371 	WAKECODE_FIXUP(wakeup_retaddr, void *, acpi_restorecpu);
372 	WAKECODE_FIXUP(wakeup_kpml4, uint64_t, KPML4phys);
373 	WAKECODE_FIXUP(wakeup_ctx, vm_offset_t,
374 	    WAKECODE_VADDR(sc) + wakeup_ctx);
375 	WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER));
376 	WAKECODE_FIXUP(wakeup_star, uint64_t, rdmsr(MSR_STAR));
377 	WAKECODE_FIXUP(wakeup_lstar, uint64_t, rdmsr(MSR_LSTAR));
378 	WAKECODE_FIXUP(wakeup_cstar, uint64_t, rdmsr(MSR_CSTAR));
379 	WAKECODE_FIXUP(wakeup_sfmask, uint64_t, rdmsr(MSR_SF_MASK));
380 
381 	/* Build temporary page tables below realmode code. */
382 	pt4 = wakeaddr;
383 	pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t);
384 	pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t);
385 
386 	/* Create the initial 1GB replicated page tables */
387 	for (i = 0; i < 512; i++) {
388 		/*
389 		 * Each slot of the level 4 pages points
390 		 * to the same level 3 page
391 		 */
392 		pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE);
393 		pt4[i] |= PG_V | PG_RW | PG_U;
394 
395 		/*
396 		 * Each slot of the level 3 pages points
397 		 * to the same level 2 page
398 		 */
399 		pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE));
400 		pt3[i] |= PG_V | PG_RW | PG_U;
401 
402 		/* The level 2 page slots are mapped with 2MB pages for 1GB. */
403 		pt2[i] = i * (2 * 1024 * 1024);
404 		pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
405 	}
406 
407 	if (bootverbose)
408 		device_printf(sc->acpi_dev, "wakeup code va %p pa %p\n",
409 		    (void *)sc->acpi_wakeaddr, (void *)sc->acpi_wakephys);
410 }
411