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