1 /* $NetBSD: acpi_wakeup.c,v 1.41 2016/07/27 13:04:28 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 2002, 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Takuya SHIOZAKI. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: acpi_wakeup.c,v 1.41 2016/07/27 13:04:28 maxv Exp $"); 34 35 /*- 36 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org> 37 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * FreeBSD: src/sys/i386/acpica/acpi_wakeup.c,v 1.9 2002/01/10 03:26:46 wes Exp 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: acpi_wakeup.c,v 1.41 2016/07/27 13:04:28 maxv Exp $"); 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/bus.h> 71 #include <sys/cpu.h> 72 #include <sys/kcpuset.h> 73 #include <sys/sysctl.h> 74 75 #include <uvm/uvm_extern.h> 76 #include <uvm/uvm_page.h> 77 78 #ifdef __i386__ 79 #include "opt_mtrr.h" 80 #endif 81 #include "ioapic.h" 82 #include "lapic.h" 83 84 #if NLAPIC > 0 85 #include <machine/i82489var.h> 86 #endif 87 #if NIOAPIC > 0 88 #include <machine/i82093var.h> 89 #endif 90 #include <machine/i8259.h> 91 92 #include "acpica.h" 93 94 #include <dev/ic/i8253reg.h> 95 #include <dev/acpi/acpica.h> 96 #include <dev/acpi/acpivar.h> 97 #define ACPI_MACHDEP_PRIVATE 98 #include <machine/acpi_machdep.h> 99 #include <machine/cpu.h> 100 #include <machine/mtrr.h> 101 102 #include <x86/cpuvar.h> 103 #include <x86/x86/tsc.h> 104 #include <x86/fpu.h> 105 106 #include "opt_vga.h" 107 108 #include "acpi_wakecode.h" 109 110 /* Address is also hard-coded in acpi_wakecode.S */ 111 static paddr_t acpi_wakeup_paddr = 3 * PAGE_SIZE; 112 static vaddr_t acpi_wakeup_vaddr; 113 114 int acpi_md_vbios_reset = 1; /* Referenced by dev/pci/vga_pci.c */ 115 int acpi_md_vesa_modenum = 0; /* Referenced by arch/x86/x86/genfb_machdep.c */ 116 static int acpi_md_beep_on_reset = 0; 117 118 static int acpi_md_s4bios(void); 119 static int sysctl_md_acpi_vbios_reset(SYSCTLFN_ARGS); 120 static int sysctl_md_acpi_beep_on_reset(SYSCTLFN_ARGS); 121 122 /* Implemented in acpi_wakeup_low.S. */ 123 int acpi_md_sleep_prepare(int); 124 int acpi_md_sleep_exit(int); 125 126 /* Referenced by acpi_wakeup_low.S. */ 127 void acpi_md_sleep_enter(int); 128 129 #ifdef MULTIPROCESSOR 130 /* Referenced in ipifuncs.c. */ 131 void acpi_cpu_sleep(struct cpu_info *); 132 #endif 133 134 static void 135 acpi_md_sleep_patch(struct cpu_info *ci) 136 { 137 #define WAKECODE_FIXUP(offset, type, val) do { \ 138 type *addr; \ 139 addr = (type *)(acpi_wakeup_vaddr + offset); \ 140 *addr = val; \ 141 } while (0) 142 143 #define WAKECODE_BCOPY(offset, type, val) do { \ 144 void **addr; \ 145 addr = (void **)(acpi_wakeup_vaddr + offset); \ 146 memcpy(addr, &(val), sizeof(type)); \ 147 } while (0) 148 149 paddr_t tmp_pdir; 150 151 tmp_pdir = pmap_init_tmp_pgtbl(acpi_wakeup_paddr); 152 153 /* Execute Sleep */ 154 memcpy((void *)acpi_wakeup_vaddr, wakecode, sizeof(wakecode)); 155 156 if (CPU_IS_PRIMARY(ci)) { 157 WAKECODE_FIXUP(WAKEUP_vesa_modenum, uint16_t, acpi_md_vesa_modenum); 158 WAKECODE_FIXUP(WAKEUP_vbios_reset, uint8_t, acpi_md_vbios_reset); 159 WAKECODE_FIXUP(WAKEUP_beep_on_reset, uint8_t, acpi_md_beep_on_reset); 160 } else { 161 WAKECODE_FIXUP(WAKEUP_vesa_modenum, uint16_t, 0); 162 WAKECODE_FIXUP(WAKEUP_vbios_reset, uint8_t, 0); 163 WAKECODE_FIXUP(WAKEUP_beep_on_reset, uint8_t, 0); 164 } 165 166 #ifdef __i386__ 167 WAKECODE_FIXUP(WAKEUP_r_cr4, uint32_t, ci->ci_suspend_cr4); 168 #endif 169 WAKECODE_FIXUP(WAKEUP_efer, uint32_t, ci->ci_suspend_efer); 170 WAKECODE_FIXUP(WAKEUP_curcpu, void *, ci); 171 #ifdef __i386__ 172 WAKECODE_FIXUP(WAKEUP_r_cr3, uint32_t, tmp_pdir); 173 #else 174 WAKECODE_FIXUP(WAKEUP_r_cr3, uint64_t, tmp_pdir); 175 #endif 176 WAKECODE_FIXUP(WAKEUP_restorecpu, void *, acpi_md_sleep_exit); 177 #undef WAKECODE_FIXUP 178 #undef WAKECODE_BCOPY 179 } 180 181 static int 182 acpi_md_s4bios(void) 183 { 184 ACPI_TABLE_FACS *facs; 185 ACPI_STATUS rv; 186 187 rv = AcpiGetTable(ACPI_SIG_FACS, 0, (ACPI_TABLE_HEADER **)&facs); 188 189 if (ACPI_FAILURE(rv) || facs == NULL) 190 return 0; 191 192 if ((facs->Flags & ACPI_FACS_S4_BIOS_PRESENT) == 0) 193 return 0; 194 195 return 1; 196 } 197 198 void 199 acpi_md_sleep_enter(int state) 200 { 201 static int s4bios = -1; 202 struct cpu_info *ci; 203 ACPI_STATUS rv; 204 205 ci = curcpu(); 206 207 #ifdef MULTIPROCESSOR 208 if (!CPU_IS_PRIMARY(ci)) { 209 atomic_and_32(&ci->ci_flags, ~CPUF_RUNNING); 210 kcpuset_atomic_clear(kcpuset_running, cpu_index(ci)); 211 212 ACPI_FLUSH_CPU_CACHE(); 213 214 for (;;) 215 x86_hlt(); 216 } 217 #endif 218 219 acpi_md_sleep_patch(ci); 220 221 ACPI_FLUSH_CPU_CACHE(); 222 223 switch (state) { 224 225 case ACPI_STATE_S4: 226 227 if (s4bios < 0) 228 s4bios = acpi_md_s4bios(); 229 230 if (s4bios == 0) { 231 aprint_error("acpi0: S4 not supported\n"); 232 return; 233 } 234 235 rv = AcpiEnterSleepStateS4bios(); 236 break; 237 238 default: 239 rv = AcpiEnterSleepState(state); 240 break; 241 } 242 243 if (ACPI_FAILURE(rv)) { 244 aprint_error("acpi0: failed to enter S%d\n", state); 245 return; 246 } 247 248 for (;;) 249 x86_hlt(); 250 } 251 252 #ifdef MULTIPROCESSOR 253 void 254 acpi_cpu_sleep(struct cpu_info *ci) 255 { 256 KASSERT(!CPU_IS_PRIMARY(ci)); 257 KASSERT(ci == curcpu()); 258 259 x86_disable_intr(); 260 261 if (acpi_md_sleep_prepare(-1)) 262 return; 263 264 /* Execute Wakeup */ 265 cpu_init_msrs(ci, false); 266 fpuinit(ci); 267 268 #if NLAPIC > 0 269 lapic_enable(); 270 lapic_set_lvt(); 271 lapic_initclocks(); 272 #endif 273 274 atomic_or_32(&ci->ci_flags, CPUF_RUNNING); 275 kcpuset_atomic_set(kcpuset_running, cpu_index(ci)); 276 tsc_sync_ap(ci); 277 278 x86_enable_intr(); 279 } 280 #endif 281 282 int 283 acpi_md_sleep(int state) 284 { 285 int s, ret = 0; 286 #ifdef MULTIPROCESSOR 287 struct cpu_info *ci; 288 CPU_INFO_ITERATOR cii; 289 cpuid_t cid; 290 #endif 291 292 KASSERT(acpi_wakeup_paddr != 0); 293 KASSERT(sizeof(wakecode) <= PAGE_SIZE); 294 295 if (!CPU_IS_PRIMARY(curcpu())) { 296 printf("acpi0: WARNING: ignoring sleep from secondary CPU\n"); 297 return -1; 298 } 299 300 AcpiSetFirmwareWakingVector(acpi_wakeup_paddr, acpi_wakeup_paddr); 301 302 s = splhigh(); 303 fpusave_cpu(true); 304 x86_disable_intr(); 305 306 #ifdef MULTIPROCESSOR 307 /* Save and suspend Application Processors. */ 308 x86_broadcast_ipi(X86_IPI_ACPI_CPU_SLEEP); 309 cid = cpu_index(curcpu()); 310 while (kcpuset_isotherset(kcpuset_running, cid)) { 311 delay(1); 312 } 313 #endif 314 315 if (acpi_md_sleep_prepare(state)) 316 goto out; 317 318 /* Execute Wakeup */ 319 #ifndef __i386__ 320 cpu_init_msrs(&cpu_info_primary, false); 321 #endif 322 fpuinit(&cpu_info_primary); 323 i8259_reinit(); 324 #if NLAPIC > 0 325 lapic_enable(); 326 lapic_set_lvt(); 327 lapic_initclocks(); 328 #endif 329 #if NIOAPIC > 0 330 ioapic_reenable(); 331 #endif 332 333 initrtclock(TIMER_FREQ); 334 inittodr(time_second); 335 336 /* 337 * The BIOS should always re-enable the SCI upon 338 * resume from the S3 state. The following is a 339 * workaround for systems that fail to do this. 340 */ 341 (void)AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, 1); 342 343 /* 344 * Clear fixed events (see e.g. ACPI 3.0, p. 62). 345 * Also prevent GPEs from misfiring by disabling 346 * all GPEs before interrupts are enabled. The 347 * AcpiLeaveSleepState() function will enable 348 * and handle the general purpose events later. 349 */ 350 (void)AcpiClearEvent(ACPI_EVENT_PMTIMER); 351 (void)AcpiClearEvent(ACPI_EVENT_GLOBAL); 352 (void)AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 353 (void)AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 354 (void)AcpiClearEvent(ACPI_EVENT_RTC); 355 (void)AcpiHwDisableAllGpes(); 356 357 acpi_pci_link_resume(); 358 359 out: 360 361 #ifdef MULTIPROCESSOR 362 for (CPU_INFO_FOREACH(cii, ci)) { 363 if (CPU_IS_PRIMARY(ci)) 364 continue; 365 acpi_md_sleep_patch(ci); 366 367 CPU_STARTUP(ci, acpi_wakeup_paddr); 368 CPU_START_CLEANUP(ci); 369 370 while ((ci->ci_flags & CPUF_RUNNING) == 0) 371 x86_pause(); 372 373 tsc_sync_bp(ci); 374 } 375 #endif 376 377 x86_enable_intr(); 378 splx(s); 379 380 #ifdef MTRR 381 if (mtrr_funcs != NULL) 382 mtrr_commit(); 383 #endif 384 385 return (ret); 386 } 387 388 void 389 acpi_md_sleep_init(void) 390 { 391 /* Map ACPI wakecode */ 392 acpi_wakeup_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, 393 UVM_KMF_VAONLY); 394 if (acpi_wakeup_vaddr == 0) 395 panic("acpi: can't allocate address for wakecode.\n"); 396 397 pmap_kenter_pa(acpi_wakeup_vaddr, acpi_wakeup_paddr, 398 VM_PROT_READ | VM_PROT_WRITE, 0); 399 pmap_update(pmap_kernel()); 400 } 401 402 SYSCTL_SETUP(sysctl_md_acpi_setup, "ACPI x86 sysctl setup") 403 { 404 const struct sysctlnode *rnode; 405 int err; 406 407 err = sysctl_createv(clog, 0, NULL, &rnode, 408 CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi", NULL, 409 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 410 411 if (err != 0) 412 return; 413 414 err = sysctl_createv(clog, 0, &rnode, &rnode, 415 CTLFLAG_PERMANENT, CTLTYPE_NODE, 416 "sleep", SYSCTL_DESCR("ACPI sleep"), 417 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL); 418 419 if (err != 0) 420 return; 421 422 (void)sysctl_createv(NULL, 0, &rnode, NULL, 423 CTLFLAG_READWRITE, CTLTYPE_BOOL, "beep", 424 NULL, sysctl_md_acpi_beep_on_reset, 425 0, NULL, 0, CTL_CREATE, CTL_EOL); 426 427 (void)sysctl_createv(NULL, 0, &rnode, NULL, 428 CTLFLAG_READWRITE, CTLTYPE_INT, "vbios", 429 NULL, sysctl_md_acpi_vbios_reset, 430 0, NULL, 0, CTL_CREATE, CTL_EOL); 431 } 432 433 static int 434 sysctl_md_acpi_vbios_reset(SYSCTLFN_ARGS) 435 { 436 int error, t; 437 struct sysctlnode node; 438 439 node = *rnode; 440 t = acpi_md_vbios_reset; 441 node.sysctl_data = &t; 442 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 443 if (error || newp == NULL) 444 return error; 445 446 if (t < 0 || t > 2) 447 return EINVAL; 448 449 #ifndef VGA_POST 450 if (t == 2) { 451 aprint_error("WARNING: hw.acpi.sleep.vbios=2 " 452 "unsupported (no option VGA_POST in kernel config)\n"); 453 return EINVAL; 454 } 455 #endif 456 457 acpi_md_vbios_reset = t; 458 459 return 0; 460 } 461 462 static int 463 sysctl_md_acpi_beep_on_reset(SYSCTLFN_ARGS) 464 { 465 int error, t; 466 struct sysctlnode node; 467 468 node = *rnode; 469 t = acpi_md_beep_on_reset; 470 node.sysctl_data = &t; 471 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 472 if (error || newp == NULL) 473 return error; 474 475 if (t < 0 || t > 1) 476 return EINVAL; 477 478 acpi_md_beep_on_reset = t; 479 480 return 0; 481 } 482