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/param.h>
33 #include <sys/bus.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/memrange.h>
38 #include <sys/smp.h>
39 #include <sys/systm.h>
40 #include <sys/cons.h>
41
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 #include <vm/vm_page.h>
45
46 #include <machine/clock.h>
47 #include <machine/cpu.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/md_var.h>
50 #include <x86/mca.h>
51 #include <machine/pcb.h>
52 #include <machine/specialreg.h>
53 #include <x86/ucode.h>
54
55 #include <x86/apicreg.h>
56 #include <x86/apicvar.h>
57 #ifdef SMP
58 #include <machine/smp.h>
59 #include <machine/vmparam.h>
60 #endif
61
62 #include <contrib/dev/acpica/include/acpi.h>
63
64 #include <dev/acpica/acpivar.h>
65
66 #include "acpi_wakecode.h"
67 #include "acpi_wakedata.h"
68
69 /* Make sure the code is less than a page and leave room for the stack. */
70 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
71
72 extern int acpi_resume_beep;
73 extern int acpi_reset_video;
74 extern int acpi_susp_bounce;
75
76 #ifdef SMP
77 extern struct susppcb **susppcbs;
78 static cpuset_t suspcpus;
79 #else
80 static struct susppcb **susppcbs;
81 #endif
82
83 static void acpi_stop_beep(void *);
84
85 #ifdef SMP
86 static int acpi_wakeup_ap(struct acpi_softc *, int);
87 static void acpi_wakeup_cpus(struct acpi_softc *);
88 #endif
89
90 #define ACPI_WAKEPT_PAGES 7
91
92 #define WAKECODE_FIXUP(offset, type, val) do { \
93 type *addr; \
94 addr = (type *)(sc->acpi_wakeaddr + (offset)); \
95 *addr = val; \
96 } while (0)
97
98 static void
acpi_stop_beep(void * arg)99 acpi_stop_beep(void *arg)
100 {
101
102 if (acpi_resume_beep != 0)
103 timer_spkr_release();
104 }
105
106 #ifdef SMP
107 static int
acpi_wakeup_ap(struct acpi_softc * sc,int cpu)108 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
109 {
110 struct pcb *pcb;
111 int vector = (sc->acpi_wakephys >> 12) & 0xff;
112 int apic_id = cpu_apic_ids[cpu];
113 int ms;
114
115 pcb = &susppcbs[cpu]->sp_pcb;
116 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
117 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
118 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
119
120 ipi_startup(apic_id, vector);
121
122 /* Wait up to 5 seconds for it to resume. */
123 for (ms = 0; ms < 5000; ms++) {
124 if (!CPU_ISSET(cpu, &suspended_cpus))
125 return (1); /* return SUCCESS */
126 DELAY(1000);
127 }
128 return (0); /* return FAILURE */
129 }
130
131 #define WARMBOOT_TARGET 0
132 #define WARMBOOT_OFF (KERNBASE + 0x0467)
133 #define WARMBOOT_SEG (KERNBASE + 0x0469)
134
135 #define CMOS_REG (0x70)
136 #define CMOS_DATA (0x71)
137 #define BIOS_RESET (0x0f)
138 #define BIOS_WARM (0x0a)
139
140 static void
acpi_wakeup_cpus(struct acpi_softc * sc)141 acpi_wakeup_cpus(struct acpi_softc *sc)
142 {
143 uint32_t mpbioswarmvec;
144 int cpu;
145 u_char mpbiosreason;
146
147 if (!efi_boot) {
148 /* save the current value of the warm-start vector */
149 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
150 outb(CMOS_REG, BIOS_RESET);
151 mpbiosreason = inb(CMOS_DATA);
152
153 /* setup a vector to our boot code */
154 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
155 *((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
156 outb(CMOS_REG, BIOS_RESET);
157 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
158 }
159
160 /* Wake up each AP. */
161 for (cpu = 1; cpu < mp_ncpus; cpu++) {
162 if (!CPU_ISSET(cpu, &suspcpus))
163 continue;
164 if (acpi_wakeup_ap(sc, cpu) == 0) {
165 /* restore the warmstart vector */
166 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
167 panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
168 cpu, cpu_apic_ids[cpu]);
169 }
170 }
171
172 if (!efi_boot) {
173 /* restore the warmstart vector */
174 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
175
176 outb(CMOS_REG, BIOS_RESET);
177 outb(CMOS_DATA, mpbiosreason);
178 }
179 }
180 #endif
181
182 int
acpi_sleep_machdep(struct acpi_softc * sc,int state)183 acpi_sleep_machdep(struct acpi_softc *sc, int state)
184 {
185 ACPI_STATUS status;
186 struct pcb *pcb;
187 struct pcpu *pc;
188 int i;
189
190 if (sc->acpi_wakeaddr == 0ul)
191 return (-1); /* couldn't alloc wake memory */
192
193 #ifdef SMP
194 suspcpus = all_cpus;
195 CPU_CLR(PCPU_GET(cpuid), &suspcpus);
196 #endif
197
198 if (acpi_resume_beep != 0)
199 timer_spkr_acquire();
200
201 AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
202
203 intr_suspend();
204
205 if (vmm_suspend_p != NULL)
206 vmm_suspend_p();
207
208 pcb = &susppcbs[0]->sp_pcb;
209 if (savectx(pcb)) {
210 fpususpend(susppcbs[0]->sp_fpususpend);
211 #ifdef SMP
212 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
213 device_printf(sc->acpi_dev, "Failed to suspend APs\n");
214 return (0); /* couldn't sleep */
215 }
216 #endif
217 hw_ibrs_ibpb_active = 0;
218 hw_ssb_active = 0;
219 cpu_stdext_feature3 = 0;
220 CPU_FOREACH(i) {
221 pc = pcpu_find(i);
222 pc->pc_ibpb_set = 0;
223 }
224
225 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
226 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
227
228 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER) &
229 ~(EFER_LMA));
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 /* Call ACPICA to enter the desired sleep state */
235 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
236 status = AcpiEnterSleepStateS4bios();
237 else
238 status = AcpiEnterSleepState(state);
239 if (ACPI_FAILURE(status)) {
240 device_printf(sc->acpi_dev,
241 "AcpiEnterSleepState failed - %s\n",
242 AcpiFormatException(status));
243 return (0); /* couldn't sleep */
244 }
245
246 if (acpi_susp_bounce)
247 resumectx(pcb);
248
249 for (;;)
250 ia32_pause();
251 } else {
252 /*
253 * Re-initialize console hardware as soon as possible.
254 * No console output (e.g. printf) is allowed before
255 * this point.
256 */
257 cnresume();
258 fpuresume(susppcbs[0]->sp_fpususpend);
259 }
260
261 return (1); /* wakeup successfully */
262 }
263
264 int
acpi_wakeup_machdep(struct acpi_softc * sc,int state,int sleep_result,int intr_enabled)265 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
266 int intr_enabled)
267 {
268
269 if (sleep_result == -1)
270 return (sleep_result);
271
272 if (!intr_enabled) {
273 /* Wakeup MD procedures in interrupt disabled context */
274 if (sleep_result == 1) {
275 ucode_reload();
276 pmap_init_pat();
277 initializecpu();
278 PCPU_SET(switchtime, 0);
279 PCPU_SET(switchticks, ticks);
280 lapic_xapic_mode();
281 #ifdef SMP
282 if (!CPU_EMPTY(&suspcpus))
283 acpi_wakeup_cpus(sc);
284 #endif
285 }
286
287 #ifdef SMP
288 if (!CPU_EMPTY(&suspcpus))
289 resume_cpus(suspcpus);
290 #endif
291
292 /*
293 * Re-read cpu_stdext_feature3, which was zeroed-out
294 * in acpi_sleep_machdep(), after the microcode was
295 * reloaded. Then recalculate the active mitigation
296 * knobs that depend on the microcode and
297 * cpu_stdext_feature3. Do it after LAPICs are woken,
298 * so that IPIs work.
299 */
300 identify_cpu_ext_features();
301
302 mca_resume();
303 if (vmm_resume_p != NULL)
304 vmm_resume_p();
305 intr_resume(/*suspend_cancelled*/false);
306
307 hw_ibrs_recalculate(true);
308 amd64_syscall_ret_flush_l1d_recalc();
309 hw_ssb_recalculate(true);
310 x86_rngds_mitg_recalculate(true);
311 zenbleed_check_and_apply(true);
312
313 AcpiSetFirmwareWakingVector(0, 0);
314 } else {
315 /* Wakeup MD procedures in interrupt enabled context */
316 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
317 mem_range_softc.mr_op->reinit != NULL)
318 mem_range_softc.mr_op->reinit(&mem_range_softc);
319 }
320
321 return (sleep_result);
322 }
323
324 static void
acpi_alloc_wakeup_handler(void ** wakeaddr,void * wakept_pages[ACPI_WAKEPT_PAGES])325 acpi_alloc_wakeup_handler(void **wakeaddr,
326 void *wakept_pages[ACPI_WAKEPT_PAGES])
327 {
328 vm_page_t wakept_m[ACPI_WAKEPT_PAGES];
329 int i;
330
331 *wakeaddr = NULL;
332 memset(wakept_pages, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_pages));
333 memset(wakept_m, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_m));
334
335 /*
336 * Specify the region for our wakeup code. We want it in the
337 * low 1 MB region, excluding real mode IVT (0-0x3ff), BDA
338 * (0x400-0x4ff), EBDA (less than 128KB, below 0xa0000, must
339 * be excluded by SMAP and DSDT), and ROM area (0xa0000 and
340 * above).
341 */
342 *wakeaddr = contigmalloc(PAGE_SIZE, M_DEVBUF,
343 M_NOWAIT, 0x500, 0xa0000, PAGE_SIZE, 0ul);
344 if (*wakeaddr == NULL) {
345 printf("%s: can't alloc wake memory\n", __func__);
346 goto freepages;
347 }
348
349 for (i = 0; i < ACPI_WAKEPT_PAGES - (la57 ? 0 : 1); i++) {
350 wakept_m[i] = pmap_page_alloc_below_4g(true);
351 wakept_pages[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
352 wakept_m[i]));
353 }
354 if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
355 EVENTHANDLER_PRI_LAST) == NULL) {
356 printf("%s: can't register event handler\n", __func__);
357 goto freepages;
358 }
359 susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
360 for (i = 0; i < mp_ncpus; i++) {
361 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
362 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
363 }
364 return;
365
366 freepages:
367 free(*wakeaddr, M_DEVBUF);
368 for (i = 0; i < ACPI_WAKEPT_PAGES; i++) {
369 if (wakept_m[i] != NULL)
370 vm_page_free(wakept_m[i]);
371 }
372 *wakeaddr = NULL;
373 }
374
375 void
acpi_install_wakeup_handler(struct acpi_softc * sc)376 acpi_install_wakeup_handler(struct acpi_softc *sc)
377 {
378 static void *wakeaddr;
379 void *wakept_pages[ACPI_WAKEPT_PAGES];
380 uint64_t *pt5, *pt4, *pt3, *pt2_0, *pt2_1, *pt2_2, *pt2_3;
381 vm_paddr_t pt5pa, pt4pa, pt3pa, pt2_0pa, pt2_1pa, pt2_2pa, pt2_3pa;
382 int i;
383
384 if (wakeaddr != NULL)
385 return;
386 acpi_alloc_wakeup_handler(&wakeaddr, wakept_pages);
387 if (wakeaddr == NULL)
388 return;
389
390 sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
391 sc->acpi_wakephys = vtophys(wakeaddr);
392
393 if (la57) {
394 pt5 = wakept_pages[6];
395 pt5pa = vtophys(pt5);
396 }
397 pt4 = wakept_pages[0];
398 pt3 = wakept_pages[1];
399 pt2_0 = wakept_pages[2];
400 pt2_1 = wakept_pages[3];
401 pt2_2 = wakept_pages[4];
402 pt2_3 = wakept_pages[5];
403 pt4pa = vtophys(pt4);
404 pt3pa = vtophys(pt3);
405 pt2_0pa = vtophys(pt2_0);
406 pt2_1pa = vtophys(pt2_1);
407 pt2_2pa = vtophys(pt2_2);
408 pt2_3pa = vtophys(pt2_3);
409
410 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
411
412 /* Patch GDT base address, ljmp targets. */
413 WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
414 sc->acpi_wakephys + bootgdt);
415 WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
416 sc->acpi_wakephys + wakeup_32);
417 WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
418 sc->acpi_wakephys + wakeup_64);
419 WAKECODE_FIXUP(wakeup_pagetables, uint32_t, la57 ? (pt5pa | 0x1) :
420 pt4pa);
421
422 /* Save pointers to some global data. */
423 WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
424 /* Create 1:1 mapping for the low 4G */
425 if (la57) {
426 bcopy(kernel_pmap->pm_pmltop, pt5, PAGE_SIZE);
427 pt5[0] = (uint64_t)pt4pa;
428 pt5[0] |= PG_V | PG_RW | PG_U;
429 } else {
430 bcopy(kernel_pmap->pm_pmltop, pt4, PAGE_SIZE);
431 }
432
433 pt4[0] = (uint64_t)pt3pa;
434 pt4[0] |= PG_V | PG_RW | PG_U;
435
436 pt3[0] = (uint64_t)pt2_0pa;
437 pt3[0] |= PG_V | PG_RW | PG_U;
438 pt3[1] = (uint64_t)pt2_1pa;
439 pt3[1] |= PG_V | PG_RW | PG_U;
440 pt3[2] = (uint64_t)pt2_2pa;
441 pt3[2] |= PG_V | PG_RW | PG_U;
442 pt3[3] = (uint64_t)pt2_3pa;
443 pt3[3] |= PG_V | PG_RW | PG_U;
444
445 for (i = 0; i < NPDEPG; i++) {
446 pt2_0[i] = (pd_entry_t)i * NBPDR;
447 pt2_0[i] |= PG_V | PG_RW | PG_PS | PG_U;
448 }
449 for (i = 0; i < NPDEPG; i++) {
450 pt2_1[i] = (pd_entry_t)NBPDP + i * NBPDR;
451 pt2_1[i] |= PG_V | PG_RW | PG_PS | PG_U;
452 }
453 for (i = 0; i < NPDEPG; i++) {
454 pt2_2[i] = (pd_entry_t)2 * NBPDP + i * NBPDR;
455 pt2_2[i] |= PG_V | PG_RW | PG_PS | PG_U;
456 }
457 for (i = 0; i < NPDEPG; i++) {
458 pt2_3[i] = (pd_entry_t)3 * NBPDP + i * NBPDR;
459 pt2_3[i] |= PG_V | PG_RW | PG_PS | PG_U;
460 }
461
462 if (bootverbose)
463 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
464 (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
465 }
466