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