1 /* $NetBSD: acpi.c,v 1.306 2026/06/25 14:37:57 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum of By Noon Software, Inc. 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 /* 33 * Copyright (c) 2003 Wasabi Systems, Inc. 34 * All rights reserved. 35 * 36 * Written by Frank van der Linden for Wasabi Systems, Inc. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed for the NetBSD Project by 49 * Wasabi Systems, Inc. 50 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 51 * or promote products derived from this software without specific prior 52 * written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 64 * POSSIBILITY OF SUCH DAMAGE. 65 */ 66 67 /* 68 * Copyright 2001, 2003 Wasabi Systems, Inc. 69 * All rights reserved. 70 * 71 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. All advertising materials mentioning features or use of this software 82 * must display the following acknowledgement: 83 * This product includes software developed for the NetBSD Project by 84 * Wasabi Systems, Inc. 85 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 86 * or promote products derived from this software without specific prior 87 * written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 91 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 92 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 93 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 94 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 95 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 96 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 97 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 98 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 99 * POSSIBILITY OF SUCH DAMAGE. 100 */ 101 102 #include <sys/cdefs.h> 103 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.306 2026/06/25 14:37:57 riastradh Exp $"); 104 105 #include "pci.h" 106 #include "opt_acpi.h" 107 #include "opt_pcifixup.h" 108 109 #include <sys/param.h> 110 #include <sys/atomic.h> 111 #include <sys/device.h> 112 #include <sys/kernel.h> 113 #include <sys/kmem.h> 114 #include <sys/malloc.h> 115 #include <sys/module.h> 116 #include <sys/mutex.h> 117 #include <sys/rndsource.h> 118 #include <sys/sysctl.h> 119 #include <sys/systm.h> 120 #include <sys/timetc.h> 121 122 #include <dev/acpi/acpireg.h> 123 #include <dev/acpi/acpivar.h> 124 #include <dev/acpi/acpi_mcfg.h> 125 #include <dev/acpi/acpi_osd.h> 126 #include <dev/acpi/acpi_pci.h> 127 #include <dev/acpi/acpi_power.h> 128 #include <dev/acpi/acpi_timer.h> 129 #include <dev/acpi/acpi_wakedev.h> 130 131 #include <machine/acpi_machdep.h> 132 133 #include "ioconf.h" 134 135 #define _COMPONENT ACPI_BUS_COMPONENT 136 ACPI_MODULE_NAME ("acpi") 137 138 /* 139 * The acpi_active variable is set when the ACPI subsystem is active. 140 * Machine-dependent code may wish to skip other steps (such as attaching 141 * subsystems that ACPI supercedes) when ACPI is active. 142 */ 143 int acpi_active = 0; 144 int acpi_suspended = 0; 145 int acpi_force_load = 0; 146 int acpi_verbose_loaded = 0; 147 148 struct acpi_softc *acpi_softc = NULL; 149 static uint64_t acpi_root_pointer; 150 extern kmutex_t acpi_interrupt_list_mtx; 151 static ACPI_HANDLE acpi_scopes[4]; 152 ACPI_TABLE_HEADER *madt_header; 153 ACPI_TABLE_HEADER *gtdt_header; 154 155 /* 156 * This structure provides a context for the ACPI 157 * namespace walk performed in acpi_build_tree(). 158 */ 159 struct acpi_walkcontext { 160 struct acpi_softc *aw_sc; 161 struct acpi_devnode *aw_parent; 162 }; 163 164 /* 165 * Ignored HIDs. 166 */ 167 static const char * const acpi_ignored_ids[] = { 168 #if defined(i386) || defined(x86_64) 169 "ACPI0007", /* ACPI CPUs do not attach to acpi(4) */ 170 "PNP0000", /* AT interrupt controller is handled internally */ 171 "PNP0001", /* EISA interrupt controller is handled internally */ 172 "PNP0200", /* AT DMA controller is handled internally */ 173 "PNP0A??", /* PCI Busses are handled internally */ 174 "PNP0B00", /* AT RTC is handled internally */ 175 "PNP0C02", /* PnP motherboard resources */ 176 "PNP0C0F", /* ACPI PCI link devices are handled internally */ 177 #endif 178 #if defined(x86_64) 179 "PNP0C04", /* FPU is handled internally */ 180 #endif 181 #if defined(__aarch64__) 182 "ACPI0004", /* ACPI module devices are handled internally */ 183 "PNP0C0F", /* ACPI PCI link devices are handled internally */ 184 #endif 185 NULL 186 }; 187 188 /* 189 * Devices that should be attached early. 190 */ 191 static const char * const acpi_early_ids[] = { 192 "PNP0C09", /* acpiec(4) */ 193 NULL 194 }; 195 196 static int acpi_match(device_t, cfdata_t, void *); 197 static int acpi_submatch(device_t, cfdata_t, const int *, void *); 198 static void acpi_attach(device_t, device_t, void *); 199 static int acpi_detach(device_t, int); 200 static void acpi_childdet(device_t, device_t); 201 static bool acpi_suspend(device_t, const pmf_qual_t *); 202 static bool acpi_resume(device_t, const pmf_qual_t *); 203 204 static void acpi_build_tree(struct acpi_softc *); 205 static void acpi_find_deps(struct acpi_softc *); 206 static void acpi_config_tree(struct acpi_softc *); 207 static void acpi_config_dma(struct acpi_softc *); 208 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, uint32_t, 209 void *, void **); 210 static ACPI_STATUS acpi_make_devnode_post(ACPI_HANDLE, uint32_t, 211 void *, void **); 212 static void acpi_make_name(struct acpi_devnode *, uint32_t); 213 214 static int acpi_rescan(device_t, const char *, const int *); 215 static void acpi_rescan_early(struct acpi_softc *); 216 static void acpi_rescan_nodes(struct acpi_softc *); 217 static void acpi_rescan_capabilities(device_t); 218 static int acpi_print(void *aux, const char *); 219 220 static void acpi_notify_handler(ACPI_HANDLE, uint32_t, void *); 221 222 static void acpi_register_fixed_button(struct acpi_softc *, int); 223 static void acpi_deregister_fixed_button(struct acpi_softc *, int); 224 static uint32_t acpi_fixed_button_handler(void *); 225 static void acpi_fixed_button_pressed(void *); 226 227 static void acpi_sleep_init(struct acpi_softc *); 228 229 static int sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO); 230 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO); 231 static int sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO); 232 233 static bool acpi_is_scope(struct acpi_devnode *); 234 static ACPI_TABLE_HEADER *acpi_map_rsdt(void); 235 static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *); 236 237 void acpi_print_verbose_stub(struct acpi_softc *); 238 void acpi_print_dev_stub(const char *); 239 240 static void acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **); 241 ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE); 242 243 void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub; 244 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub; 245 246 bus_dma_tag_t acpi_default_dma_tag(struct acpi_softc *, struct acpi_devnode *); 247 bus_dma_tag_t acpi_default_dma64_tag(struct acpi_softc *, struct acpi_devnode *); 248 pci_chipset_tag_t acpi_default_pci_chipset_tag(struct acpi_softc *, int, int); 249 250 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc), 251 acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet); 252 253 /* 254 * Probe for ACPI support. 255 * 256 * This is called by the machine-dependent ACPI front-end. 257 * Note: this is not an autoconfiguration interface function. 258 */ 259 int 260 acpi_probe(void) 261 { 262 ACPI_TABLE_HEADER *rsdt; 263 ACPI_STATUS rv; 264 int quirks; 265 266 if (acpi_softc != NULL) 267 panic("%s: already probed", __func__); 268 269 mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE); 270 271 /* 272 * Start up ACPICA. 273 */ 274 AcpiGbl_EnableInterpreterSlack = true; 275 276 rv = AcpiInitializeSubsystem(); 277 278 if (ACPI_FAILURE(rv)) { 279 aprint_error("%s: failed to initialize subsystem\n", __func__); 280 return 0; 281 } 282 283 /* 284 * Allocate space for RSDT/XSDT and DSDT, 285 * but allow resizing if more tables exist. 286 */ 287 rv = AcpiInitializeTables(NULL, 2, true); 288 289 if (ACPI_FAILURE(rv)) { 290 aprint_error("%s: failed to initialize tables\n", __func__); 291 goto fail; 292 } 293 294 rv = AcpiLoadTables(); 295 296 if (ACPI_FAILURE(rv)) { 297 aprint_error("%s: failed to load tables\n", __func__); 298 goto fail; 299 } 300 301 rsdt = acpi_map_rsdt(); 302 303 if (rsdt == NULL) { 304 aprint_error("%s: failed to map RSDT\n", __func__); 305 goto fail; 306 } 307 308 quirks = acpi_find_quirks(); 309 310 if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_BROKEN) != 0) { 311 312 aprint_normal("ACPI: BIOS is listed as broken:\n"); 313 aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, " 314 "AslId <%4.4s,%08x>\n", rsdt->OemId, rsdt->OemTableId, 315 rsdt->OemRevision, rsdt->AslCompilerId, 316 rsdt->AslCompilerRevision); 317 aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n"); 318 319 acpi_unmap_rsdt(rsdt); 320 goto fail; 321 } 322 323 if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_OLDBIOS) != 0) { 324 325 aprint_normal("ACPI: BIOS is too old (%s). " 326 "Set acpi_force_load to use.\n", 327 pmf_get_platform("bios-date")); 328 329 acpi_unmap_rsdt(rsdt); 330 goto fail; 331 } 332 333 acpi_unmap_rsdt(rsdt); 334 335 rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE)); 336 337 if (ACPI_FAILURE(rv)) { 338 aprint_error("%s: failed to enable subsystem\n", __func__); 339 goto fail; 340 } 341 342 return 1; 343 344 fail: 345 (void)AcpiTerminate(); 346 347 return 0; 348 } 349 350 void 351 acpi_disable(void) 352 { 353 354 if (acpi_softc == NULL) 355 return; 356 357 KASSERT(acpi_active != 0); 358 359 if (AcpiGbl_FADT.SmiCommand != 0) 360 AcpiDisable(); 361 } 362 363 int 364 acpi_check(device_t parent, const char *ifattr) 365 { 366 return config_search(parent, NULL, 367 CFARGS(.submatch = acpi_submatch, 368 .iattr = ifattr)) != NULL; 369 } 370 371 int 372 acpi_reset(void) 373 { 374 struct acpi_softc *sc = acpi_softc; 375 ACPI_GENERIC_ADDRESS *ResetReg; 376 ACPI_PCI_ID PciId; 377 ACPI_STATUS status; 378 379 if (sc == NULL) 380 return ENXIO; 381 382 ResetReg = &AcpiGbl_FADT.ResetRegister; 383 384 /* Check if the reset register is supported */ 385 if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) || 386 !ResetReg->Address) { 387 return ENOENT; 388 } 389 390 switch (ResetReg->SpaceId) { 391 case ACPI_ADR_SPACE_PCI_CONFIG: 392 PciId.Segment = PciId.Bus = 0; 393 PciId.Device = ACPI_GAS_PCI_DEV(ResetReg->Address); 394 PciId.Function = ACPI_GAS_PCI_FUNC(ResetReg->Address); 395 status = AcpiOsWritePciConfiguration(&PciId, 396 ACPI_GAS_PCI_REGOFF(ResetReg->Address), 397 AcpiGbl_FADT.ResetValue, ResetReg->BitWidth); 398 break; 399 case ACPI_ADR_SPACE_SYSTEM_IO: 400 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 401 status = AcpiReset(); 402 break; 403 default: 404 status = AE_TYPE; 405 break; 406 } 407 408 return ACPI_FAILURE(status) ? EIO : 0; 409 } 410 411 /* 412 * Autoconfiguration. 413 */ 414 static int 415 acpi_match(device_t parent, cfdata_t match, void *aux) 416 { 417 /* 418 * XXX: Nada; MD code has called acpi_probe(). 419 */ 420 return 1; 421 } 422 423 static int 424 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 425 { 426 struct cfattach *ca; 427 428 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 429 430 return (ca == &acpi_ca); 431 } 432 433 static void 434 acpi_attach(device_t parent, device_t self, void *aux) 435 { 436 struct acpi_softc *sc = device_private(self); 437 struct acpibus_attach_args *aa = aux; 438 ACPI_TABLE_HEADER *rsdt, *hdr, *oem0; 439 ACPI_STATUS rv; 440 int i; 441 442 aprint_naive("\n"); 443 aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION); 444 445 if (acpi_softc != NULL) 446 panic("%s: already attached", __func__); 447 448 rsdt = acpi_map_rsdt(); 449 450 if (rsdt == NULL) 451 aprint_error_dev(self, "X/RSDT: Not found\n"); 452 else { 453 aprint_verbose_dev(self, 454 "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n", 455 rsdt->OemId, rsdt->OemTableId, 456 rsdt->OemRevision, 457 rsdt->AslCompilerId, rsdt->AslCompilerRevision); 458 } 459 460 acpi_unmap_rsdt(rsdt); 461 462 sc->sc_dev = self; 463 sc->sc_root = NULL; 464 465 sc->sc_sleepstate = ACPI_STATE_S0; 466 sc->sc_quirks = acpi_find_quirks(); 467 468 sysmon_power_settype("acpi"); 469 470 sc->sc_iot = aa->aa_iot; 471 sc->sc_memt = aa->aa_memt; 472 sc->sc_pciflags = aa->aa_pciflags; 473 sc->sc_ic = aa->aa_ic; 474 sc->sc_dmat = aa->aa_dmat; 475 sc->sc_dmat64 = aa->aa_dmat64; 476 477 SIMPLEQ_INIT(&sc->sc_head); 478 479 acpi_softc = sc; 480 481 if (pmf_device_register(self, acpi_suspend, acpi_resume) != true) 482 aprint_error_dev(self, "couldn't establish power handler\n"); 483 484 /* 485 * Bring ACPICA on-line. 486 */ 487 488 rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION); 489 490 if (ACPI_FAILURE(rv)) 491 goto fail; 492 493 /* 494 * Early initialization of acpiec(4) via ECDT. 495 */ 496 config_found(self, aa, NULL, 497 CFARGS(.iattr = "acpiecdtbus")); 498 499 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION); 500 501 if (ACPI_FAILURE(rv)) 502 goto fail; 503 504 /* 505 * Scan the namespace and build our device tree. 506 */ 507 acpi_build_tree(sc); 508 509 #if NPCI > 0 510 /* 511 * Probe MCFG table 512 */ 513 acpimcfg_probe(sc); 514 #endif 515 516 acpi_md_callback(sc); 517 518 /* 519 * Early initialization of the _PDC control method 520 * that may load additional SSDT tables dynamically. 521 */ 522 (void)acpi_md_pdc(); 523 524 /* 525 * Install global notify handlers. 526 */ 527 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT, 528 ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL); 529 530 if (ACPI_FAILURE(rv)) 531 goto fail; 532 533 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT, 534 ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL); 535 536 if (ACPI_FAILURE(rv)) 537 goto fail; 538 539 acpi_active = 1; 540 541 if (!AcpiGbl_ReducedHardware) { 542 /* Show SCI interrupt. */ 543 aprint_verbose_dev(self, "SCI interrupting at int %u\n", 544 AcpiGbl_FADT.SciInterrupt); 545 546 /* 547 * Install fixed-event handlers. 548 */ 549 acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON); 550 acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON); 551 } 552 553 /* 554 * Load drivers that operate on System Description Tables. 555 */ 556 for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; ++i) { 557 rv = AcpiGetTableByIndex(i, &hdr); 558 if (ACPI_FAILURE(rv)) { 559 continue; 560 } 561 config_found(sc->sc_dev, hdr, NULL, 562 CFARGS(.iattr = "acpisdtbus")); 563 AcpiPutTable(hdr); 564 } 565 566 acpitimer_init(sc); 567 acpi_config_tree(sc); 568 acpi_sleep_init(sc); 569 570 #ifdef ACPI_DEBUG 571 acpi_debug_init(); 572 #endif 573 574 /* 575 * Microsoft Hyper-V drops off entropy from the Windows system RNG 576 * in the OEM0 table of its guests. Consume and clear it as early 577 * as possible. 578 */ 579 rv = AcpiGetTable("OEM0", 1, &oem0); 580 if (ACPI_SUCCESS(rv) && oem0 != NULL) { 581 if (memcmp(oem0->OemId, "VRTUAL", 6) == 0 && 582 memcmp(oem0->OemTableId, "MICROSFT", 8) == 0) { 583 static struct krndsource hvrs; 584 uint8_t *hostrnd; 585 size_t hrlen; 586 587 memset(&hvrs, 0, sizeof(hvrs)); 588 rnd_attach_source(&hvrs, "Hyper-V OEM0", 589 RND_TYPE_UNKNOWN, RND_FLAG_DEFAULT); 590 hostrnd = (uint8_t *)oem0 + sizeof(ACPI_TABLE_HEADER); 591 hrlen = oem0->Length - sizeof(ACPI_TABLE_HEADER); 592 rnd_add_data(&hvrs, hostrnd, hrlen, hrlen * NBBY); 593 /* Now wipe it out - hide from /dev/acpi access */ 594 explicit_memset(hostrnd, 0, hrlen); 595 aprint_normal_dev(self, 596 "Hyper-V OEM0 entropy consumed and wiped\n"); 597 } 598 } 599 /* 600 * Print debug information. 601 */ 602 acpi_print_verbose(sc); 603 604 return; 605 606 fail: 607 aprint_error("%s: failed to initialize ACPI: %s\n", 608 __func__, AcpiFormatException(rv)); 609 } 610 611 /* 612 * XXX: This is incomplete. 613 */ 614 static int 615 acpi_detach(device_t self, int flags) 616 { 617 struct acpi_softc *sc = device_private(self); 618 ACPI_STATUS rv; 619 int rc; 620 621 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT, 622 ACPI_SYSTEM_NOTIFY, acpi_notify_handler); 623 624 if (ACPI_FAILURE(rv)) 625 return EBUSY; 626 627 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT, 628 ACPI_DEVICE_NOTIFY, acpi_notify_handler); 629 630 if (ACPI_FAILURE(rv)) 631 return EBUSY; 632 633 if ((rc = config_detach_children(self, flags)) != 0) 634 return rc; 635 636 if ((rc = acpitimer_detach()) != 0) 637 return rc; 638 639 if (!AcpiGbl_ReducedHardware) { 640 acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON); 641 acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON); 642 } 643 644 pmf_device_deregister(self); 645 646 acpi_softc = NULL; 647 648 return 0; 649 } 650 651 static void 652 acpi_childdet(device_t self, device_t child) 653 { 654 struct acpi_softc *sc = device_private(self); 655 struct acpi_devnode *ad; 656 657 if (sc->sc_apmbus == child) 658 sc->sc_apmbus = NULL; 659 660 if (sc->sc_hpet == child) 661 sc->sc_hpet = NULL; 662 663 if (sc->sc_wdrt == child) 664 sc->sc_wdrt = NULL; 665 666 if (sc->sc_apei == child) 667 sc->sc_apei = NULL; 668 669 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 670 671 if (ad->ad_device == child) 672 ad->ad_device = NULL; 673 } 674 } 675 676 static bool 677 acpi_suspend(device_t dv, const pmf_qual_t *qual) 678 { 679 680 acpi_suspended = 1; 681 682 return true; 683 } 684 685 static bool 686 acpi_resume(device_t dv, const pmf_qual_t *qual) 687 { 688 689 acpi_suspended = 0; 690 691 return true; 692 } 693 694 /* 695 * Namespace scan. 696 */ 697 static void 698 acpi_build_tree(struct acpi_softc *sc) 699 { 700 struct acpi_walkcontext awc; 701 702 /* 703 * Get the root scope handles. 704 */ 705 KASSERT(__arraycount(acpi_scopes) == 4); 706 707 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]); 708 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]); 709 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]); 710 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]); 711 712 /* 713 * Make the root node. 714 */ 715 awc.aw_sc = sc; 716 awc.aw_parent = NULL; 717 718 (void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL); 719 720 KASSERT(sc->sc_root == NULL); 721 KASSERT(awc.aw_parent != NULL); 722 723 sc->sc_root = awc.aw_parent; 724 725 /* 726 * Build the internal namespace. 727 */ 728 (void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX, 729 acpi_make_devnode, acpi_make_devnode_post, &awc, NULL); 730 731 /* 732 * Find device dependencies. 733 */ 734 acpi_find_deps(sc); 735 736 #if NPCI > 0 737 /* 738 * Scan the internal namespace. 739 */ 740 (void)acpi_pcidev_scan(sc->sc_root); 741 #endif 742 } 743 744 static void 745 acpi_add_dep(struct acpi_devnode *ad, struct acpi_devnode *depad) 746 { 747 struct acpi_devnodedep *dd; 748 749 dd = kmem_alloc(sizeof(*dd), KM_SLEEP); 750 dd->dd_node = depad; 751 SIMPLEQ_INSERT_TAIL(&ad->ad_deps, dd, dd_list); 752 } 753 754 static void 755 acpi_find_deps(struct acpi_softc *sc) 756 { 757 struct acpi_devnode *ad; 758 759 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 760 struct acpi_devnode *depad; 761 ACPI_OBJECT *obj; 762 ACPI_HANDLE _dep; 763 ACPI_BUFFER buf; 764 ACPI_STATUS rv; 765 u_int ref; 766 767 if (acpi_is_scope(ad) || 768 ad->ad_parent == NULL || 769 ad->ad_devinfo->Type != ACPI_TYPE_DEVICE) { 770 continue; 771 } 772 773 /* Add an implicit dependency on parent devices. */ 774 if (!acpi_is_scope(ad->ad_parent) && 775 ad->ad_parent->ad_devinfo->Type == ACPI_TYPE_DEVICE) { 776 acpi_add_dep(ad, ad->ad_parent); 777 } 778 779 rv = AcpiGetHandle(ad->ad_handle, "_DEP", &_dep); 780 if (ACPI_FAILURE(rv)) { 781 goto logit; 782 } 783 784 buf.Pointer = NULL; 785 buf.Length = ACPI_ALLOCATE_BUFFER; 786 rv = AcpiEvaluateObjectTyped(_dep, NULL, NULL, &buf, 787 ACPI_TYPE_PACKAGE); 788 if (ACPI_FAILURE(rv)) { 789 goto logit; 790 } 791 obj = buf.Pointer; 792 793 for (ref = 0; ref < obj->Package.Count; ref++) { 794 ACPI_OBJECT *robj = &obj->Package.Elements[ref]; 795 ACPI_HANDLE rhdl; 796 797 rv = acpi_eval_reference_handle(robj, &rhdl); 798 if (ACPI_FAILURE(rv)) { 799 continue; 800 } 801 802 depad = acpi_match_node(rhdl); 803 if (depad != NULL) { 804 acpi_add_dep(ad, depad); 805 } 806 } 807 808 ACPI_FREE(buf.Pointer); 809 810 logit: 811 if (!SIMPLEQ_EMPTY(&ad->ad_deps)) { 812 struct acpi_devnodedep *dd; 813 814 aprint_debug_dev(sc->sc_dev, "%s dependencies:", 815 ad->ad_name); 816 SIMPLEQ_FOREACH(dd, &ad->ad_deps, dd_list) { 817 aprint_debug(" %s", dd->dd_node->ad_name); 818 } 819 aprint_debug("\n"); 820 } 821 } 822 } 823 824 static void 825 acpi_config_tree(struct acpi_softc *sc) 826 { 827 /* 828 * Assign bus_dma resources 829 */ 830 acpi_config_dma(sc); 831 832 /* 833 * Configure all everything found "at acpi?". 834 */ 835 (void)acpi_rescan(sc->sc_dev, NULL, NULL); 836 837 /* 838 * Update GPE information. 839 * 840 * Note that this must be called after 841 * all GPE handlers have been installed. 842 */ 843 (void)AcpiUpdateAllGpes(); 844 845 /* 846 * Defer rest of the configuration. 847 */ 848 (void)config_defer(sc->sc_dev, acpi_rescan_capabilities); 849 } 850 851 // XXXNH? 852 static void 853 acpi_config_dma(struct acpi_softc *sc) 854 { 855 struct acpi_devnode *ad; 856 857 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 858 859 if (ad->ad_device != NULL) 860 continue; 861 862 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE) 863 continue; 864 865 ad->ad_dmat = acpi_get_dma_tag(sc, ad); 866 ad->ad_dmat64 = acpi_get_dma64_tag(sc, ad); 867 } 868 } 869 870 static ACPI_STATUS 871 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level, 872 void *context, void **status) 873 { 874 struct acpi_walkcontext *awc = context; 875 struct acpi_softc *sc = awc->aw_sc; 876 struct acpi_devnode *ad; 877 ACPI_DEVICE_INFO *devinfo; 878 ACPI_OBJECT_TYPE type; 879 ACPI_STATUS rv; 880 881 rv = AcpiGetObjectInfo(handle, &devinfo); 882 883 if (ACPI_FAILURE(rv)) 884 return AE_OK; /* Do not terminate the walk. */ 885 886 type = devinfo->Type; 887 888 switch (type) { 889 890 case ACPI_TYPE_DEVICE: 891 acpi_activate_device(handle, &devinfo); 892 /* FALLTHROUGH */ 893 894 case ACPI_TYPE_PROCESSOR: 895 case ACPI_TYPE_THERMAL: 896 case ACPI_TYPE_POWER: 897 898 ad = kmem_zalloc(sizeof(*ad), KM_SLEEP); 899 900 ad->ad_device = NULL; 901 ad->ad_notify = NULL; 902 ad->ad_pciinfo = NULL; 903 ad->ad_wakedev = NULL; 904 905 ad->ad_type = type; 906 ad->ad_handle = handle; 907 ad->ad_devinfo = devinfo; 908 909 ad->ad_root = sc->sc_dev; 910 ad->ad_parent = awc->aw_parent; 911 912 acpi_match_node_init(ad); 913 acpi_make_name(ad, devinfo->Name); 914 915 /* 916 * Identify wake GPEs from the _PRW. Note that 917 * AcpiUpdateAllGpes() must be called afterwards. 918 */ 919 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) 920 acpi_wakedev_init(ad); 921 922 SIMPLEQ_INIT(&ad->ad_child_head); 923 SIMPLEQ_INSERT_TAIL(&sc->sc_head, ad, ad_list); 924 SIMPLEQ_INIT(&ad->ad_deps); 925 926 if (ad->ad_parent != NULL) { 927 928 SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head, 929 ad, ad_child_list); 930 } 931 932 awc->aw_parent = ad; 933 break; 934 935 default: 936 ACPI_FREE(devinfo); 937 break; 938 } 939 940 return AE_OK; 941 } 942 943 static ACPI_STATUS 944 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level, 945 void *context, void **status) 946 { 947 struct acpi_walkcontext *awc = context; 948 949 KASSERT(awc != NULL); 950 KASSERT(awc->aw_parent != NULL); 951 952 if (handle == awc->aw_parent->ad_handle) 953 awc->aw_parent = awc->aw_parent->ad_parent; 954 955 return AE_OK; 956 } 957 958 static void 959 acpi_make_name(struct acpi_devnode *ad, uint32_t name) 960 { 961 ACPI_NAME_UNION *anu; 962 int clear, i; 963 964 anu = (ACPI_NAME_UNION *)&name; 965 ad->ad_name[4] = '\0'; 966 967 for (i = 3, clear = 0; i >= 0; i--) { 968 969 if (clear == 0 && anu->Ascii[i] == '_') 970 ad->ad_name[i] = '\0'; 971 else { 972 ad->ad_name[i] = anu->Ascii[i]; 973 clear = 1; 974 } 975 } 976 977 if (ad->ad_name[0] == '\0') 978 ad->ad_name[0] = '_'; 979 } 980 981 bus_dma_tag_t 982 acpi_default_dma_tag(struct acpi_softc *sc, struct acpi_devnode *ad) 983 { 984 return sc->sc_dmat; 985 } 986 __weak_alias(acpi_get_dma_tag,acpi_default_dma_tag); 987 988 bus_dma_tag_t 989 acpi_default_dma64_tag(struct acpi_softc *sc, struct acpi_devnode *ad) 990 { 991 return sc->sc_dmat64; 992 } 993 __weak_alias(acpi_get_dma64_tag,acpi_default_dma64_tag); 994 995 pci_chipset_tag_t 996 acpi_default_pci_chipset_tag(struct acpi_softc *sc, int seg, int bbn) 997 { 998 return NULL; 999 } 1000 __weak_alias(acpi_get_pci_chipset_tag,acpi_default_pci_chipset_tag); 1001 1002 /* 1003 * Device attachment. 1004 */ 1005 static int 1006 acpi_rescan(device_t self, const char *ifattr, const int *locators) 1007 { 1008 struct acpi_softc *sc = device_private(self); 1009 struct acpi_attach_args aa; 1010 1011 /* 1012 * Try to attach hpet(4) first via a specific table. 1013 */ 1014 aa.aa_memt = sc->sc_memt; 1015 1016 if (ifattr_match(ifattr, "acpihpetbus") && sc->sc_hpet == NULL) { 1017 sc->sc_hpet = config_found(sc->sc_dev, &aa, NULL, 1018 CFARGS(.iattr = "acpihpetbus")); 1019 } 1020 1021 /* 1022 * A two-pass scan for acpinodebus. 1023 */ 1024 if (ifattr_match(ifattr, "acpinodebus")) { 1025 acpi_rescan_early(sc); 1026 acpi_rescan_nodes(sc); 1027 } 1028 1029 /* 1030 * Attach APM emulation and acpiwdrt(4). 1031 */ 1032 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL) { 1033 sc->sc_apmbus = config_found(sc->sc_dev, NULL, NULL, 1034 CFARGS(.iattr = "acpiapmbus")); 1035 } 1036 1037 if (ifattr_match(ifattr, "acpiwdrtbus") && sc->sc_wdrt == NULL) { 1038 sc->sc_wdrt = config_found(sc->sc_dev, NULL, NULL, 1039 CFARGS(.iattr = "acpiwdrtbus")); 1040 } 1041 1042 if (ifattr_match(ifattr, "apeibus") && sc->sc_apei == NULL) { 1043 sc->sc_apei = config_found(sc->sc_dev, NULL, NULL, 1044 CFARGS(.iattr = "apeibus")); 1045 } 1046 1047 return 0; 1048 } 1049 1050 static void 1051 acpi_rescan_node(struct acpi_softc *sc, struct acpi_devnode *ad) 1052 { 1053 const char * const hpet_ids[] = { "PNP0103", NULL }; 1054 struct acpi_attach_args aa; 1055 struct acpi_devnodedep *dd; 1056 ACPI_DEVICE_INFO *di = ad->ad_devinfo; 1057 1058 if (ad->ad_scanned || ad->ad_device != NULL) { 1059 return; 1060 } 1061 1062 /* 1063 * Mark as scanned before checking dependencies to 1064 * break out of dependency cycles. 1065 */ 1066 ad->ad_scanned = true; 1067 1068 if (!acpi_device_present(ad->ad_handle)) { 1069 return; 1070 } 1071 1072 if (acpi_match_hid(di, acpi_ignored_ids) != 0) { 1073 return; 1074 } 1075 1076 if (acpi_match_hid(di, hpet_ids) != 0 && sc->sc_hpet != NULL) { 1077 return; 1078 } 1079 1080 /* Rescan dependencies first. */ 1081 SIMPLEQ_FOREACH(dd, &ad->ad_deps, dd_list) { 1082 if (!dd->dd_node->ad_scanned) { 1083 acpi_rescan_node(sc, dd->dd_node); 1084 } 1085 } 1086 1087 /* Dependency scanning may have claimed this device. */ 1088 if (ad->ad_device != NULL) { 1089 return; 1090 } 1091 1092 aa.aa_node = ad; 1093 aa.aa_iot = sc->sc_iot; 1094 aa.aa_memt = sc->sc_memt; 1095 if (ad->ad_pciinfo != NULL) { 1096 aa.aa_pc = ad->ad_pciinfo->ap_pc; 1097 aa.aa_pciflags = sc->sc_pciflags; 1098 } 1099 aa.aa_ic = sc->sc_ic; 1100 aa.aa_dmat = ad->ad_dmat; 1101 aa.aa_dmat64 = ad->ad_dmat64; 1102 1103 ad->ad_device = config_found(sc->sc_dev, &aa, acpi_print, 1104 CFARGS(.iattr = "acpinodebus", 1105 .devhandle = devhandle_from_acpi(devhandle_invalid(), 1106 ad->ad_handle))); 1107 } 1108 1109 static void 1110 acpi_rescan_early(struct acpi_softc *sc) 1111 { 1112 struct acpi_devnode *ad; 1113 1114 /* 1115 * First scan for devices such as acpiec(4) that 1116 * should be always attached before anything else. 1117 * We want these devices to attach regardless of 1118 * the device status and other restrictions. 1119 */ 1120 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 1121 1122 if (ad->ad_device != NULL) 1123 continue; 1124 1125 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE) 1126 continue; 1127 1128 if (acpi_match_hid(ad->ad_devinfo, acpi_early_ids) == 0) 1129 continue; 1130 1131 KASSERT(ad->ad_handle != NULL); 1132 1133 acpi_rescan_node(sc, ad); 1134 } 1135 } 1136 1137 static void 1138 acpi_rescan_nodes(struct acpi_softc *sc) 1139 { 1140 struct acpi_devnode *ad; 1141 ACPI_DEVICE_INFO *di; 1142 1143 /* Reset scan state. */ 1144 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 1145 ad->ad_scanned = false; 1146 } 1147 1148 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 1149 1150 if (ad->ad_device != NULL) 1151 continue; 1152 1153 /* 1154 * There is a bug in ACPICA: it defines the type 1155 * of the scopes incorrectly for its own reasons. 1156 */ 1157 if (acpi_is_scope(ad) != false) 1158 continue; 1159 1160 di = ad->ad_devinfo; 1161 1162 /* 1163 * We only attach devices which are present, enabled, and 1164 * functioning properly. However, if a device is enabled, 1165 * it is decoding resources and we should claim these, 1166 * if possible. This requires changes to bus_space(9). 1167 */ 1168 if (di->Type == ACPI_TYPE_DEVICE && 1169 !acpi_device_present(ad->ad_handle)) { 1170 continue; 1171 } 1172 1173 if (di->Type == ACPI_TYPE_POWER) 1174 continue; 1175 1176 if (di->Type == ACPI_TYPE_PROCESSOR) 1177 continue; 1178 1179 if (acpi_match_hid(di, acpi_early_ids) != 0) 1180 continue; 1181 1182 KASSERT(ad->ad_handle != NULL); 1183 1184 acpi_rescan_node(sc, ad); 1185 } 1186 } 1187 1188 static void 1189 acpi_rescan_capabilities(device_t self) 1190 { 1191 struct acpi_softc *sc = device_private(self); 1192 struct acpi_devnode *ad; 1193 ACPI_HANDLE tmp; 1194 ACPI_STATUS rv; 1195 1196 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 1197 1198 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE) 1199 continue; 1200 1201 /* 1202 * Scan power resource capabilities. 1203 * 1204 * If any power states are supported, 1205 * at least _PR0 and _PR3 must be present. 1206 */ 1207 rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp); 1208 1209 if (ACPI_SUCCESS(rv)) { 1210 ad->ad_flags |= ACPI_DEVICE_POWER; 1211 acpi_power_add(ad); 1212 } 1213 1214 /* 1215 * Scan wake-up capabilities. 1216 */ 1217 if (ad->ad_wakedev != NULL) { 1218 ad->ad_flags |= ACPI_DEVICE_WAKEUP; 1219 acpi_wakedev_add(ad); 1220 } 1221 1222 /* 1223 * Scan docking stations. 1224 */ 1225 rv = AcpiGetHandle(ad->ad_handle, "_DCK", &tmp); 1226 1227 if (ACPI_SUCCESS(rv)) 1228 ad->ad_flags |= ACPI_DEVICE_DOCK; 1229 1230 /* 1231 * Scan devices that are ejectable. 1232 */ 1233 rv = AcpiGetHandle(ad->ad_handle, "_EJ0", &tmp); 1234 1235 if (ACPI_SUCCESS(rv)) 1236 ad->ad_flags |= ACPI_DEVICE_EJECT; 1237 } 1238 } 1239 1240 static int 1241 acpi_print(void *aux, const char *pnp) 1242 { 1243 struct acpi_attach_args *aa = aux; 1244 struct acpi_devnode *ad; 1245 const char *hid, *uid; 1246 ACPI_DEVICE_INFO *di; 1247 1248 ad = aa->aa_node; 1249 di = ad->ad_devinfo; 1250 1251 hid = di->HardwareId.String; 1252 uid = di->UniqueId.String; 1253 1254 if (pnp != NULL) { 1255 1256 if (di->Type != ACPI_TYPE_DEVICE) { 1257 1258 aprint_normal("%s (ACPI Object Type '%s') at %s", 1259 ad->ad_name, AcpiUtGetTypeName(ad->ad_type), pnp); 1260 1261 return UNCONF; 1262 } 1263 1264 if ((di->Valid & ACPI_VALID_HID) == 0 || hid == NULL) 1265 return 0; 1266 1267 aprint_normal("%s (%s) ", ad->ad_name, hid); 1268 acpi_print_dev(hid); 1269 aprint_normal("at %s", pnp); 1270 1271 return UNCONF; 1272 } 1273 1274 aprint_normal(" (%s", ad->ad_name); 1275 1276 if ((di->Valid & ACPI_VALID_HID) != 0 && hid != NULL) { 1277 1278 aprint_normal(", %s", hid); 1279 1280 if ((di->Valid & ACPI_VALID_UID) != 0 && uid != NULL) { 1281 1282 if (uid[0] == '\0') 1283 uid = "<null>"; 1284 1285 aprint_normal("-%s", uid); 1286 } 1287 } 1288 1289 aprint_normal(")"); 1290 1291 return UNCONF; 1292 } 1293 1294 /* 1295 * Notify. 1296 */ 1297 static void 1298 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux) 1299 { 1300 struct acpi_softc *sc = acpi_softc; 1301 struct acpi_devnode *ad; 1302 ACPI_NOTIFY_HANDLER notify; 1303 1304 KASSERT(sc != NULL); 1305 KASSERT(aux == NULL); 1306 KASSERT(acpi_active != 0); 1307 1308 if (acpi_suspended != 0) 1309 return; 1310 1311 /* 1312 * System: 0x00 - 0x7F. 1313 * Device: 0x80 - 0xFF. 1314 */ 1315 switch (event) { 1316 1317 case ACPI_NOTIFY_BUS_CHECK: 1318 case ACPI_NOTIFY_DEVICE_CHECK: 1319 case ACPI_NOTIFY_DEVICE_WAKE: 1320 case ACPI_NOTIFY_EJECT_REQUEST: 1321 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT: 1322 case ACPI_NOTIFY_FREQUENCY_MISMATCH: 1323 case ACPI_NOTIFY_BUS_MODE_MISMATCH: 1324 case ACPI_NOTIFY_POWER_FAULT: 1325 case ACPI_NOTIFY_CAPABILITIES_CHECK: 1326 case ACPI_NOTIFY_DEVICE_PLD_CHECK: 1327 case ACPI_NOTIFY_RESERVED: 1328 case ACPI_NOTIFY_LOCALITY_UPDATE: 1329 break; 1330 } 1331 1332 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for " 1333 "%s (%p)\n", event, acpi_name(handle), handle)); 1334 1335 /* 1336 * We deliver notifications only to drivers 1337 * that have been successfully attached and 1338 * that have registered a handler with us. 1339 * The opaque pointer is always the device_t. 1340 */ 1341 SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) { 1342 1343 if (ad->ad_device == NULL) 1344 continue; 1345 1346 if ((notify = atomic_load_acquire(&ad->ad_notify)) == NULL) 1347 continue; 1348 1349 if (ad->ad_handle != handle) 1350 continue; 1351 1352 (*notify)(ad->ad_handle, event, ad->ad_device); 1353 1354 return; 1355 } 1356 1357 aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X " 1358 "for %s (%p)\n", event, acpi_name(handle), handle); 1359 } 1360 1361 bool 1362 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify) 1363 { 1364 struct acpi_softc *sc = acpi_softc; 1365 1366 KASSERT(sc != NULL); 1367 KASSERT(acpi_active != 0); 1368 1369 if (acpi_suspended != 0) 1370 goto fail; 1371 1372 if (ad == NULL || notify == NULL) 1373 goto fail; 1374 1375 KASSERTMSG(ad->ad_notify == NULL, 1376 "%s: ACPI node %s already has notify handler: %p", 1377 ad->ad_device ? device_xname(ad->ad_device) : "(unknown)", 1378 ad->ad_name, 1379 ad->ad_notify); 1380 atomic_store_release(&ad->ad_notify, notify); 1381 1382 return true; 1383 1384 fail: 1385 if (!ad) 1386 aprint_error_dev(sc->sc_dev, "failed to initialize ACPI\n"); 1387 else 1388 aprint_error_dev(sc->sc_dev, "failed to register notify " 1389 "handler for %s (%p)\n", ad->ad_name, ad->ad_handle); 1390 1391 return false; 1392 } 1393 1394 void 1395 acpi_deregister_notify(struct acpi_devnode *ad) 1396 { 1397 1398 atomic_store_relaxed(&ad->ad_notify, NULL); 1399 1400 /* Wait for any in-flight calls to the notifier to complete. */ 1401 AcpiOsWaitEventsComplete(); 1402 } 1403 1404 /* 1405 * Fixed buttons. 1406 */ 1407 static void 1408 acpi_register_fixed_button(struct acpi_softc *sc, int event) 1409 { 1410 struct sysmon_pswitch *smpsw; 1411 ACPI_STATUS rv; 1412 int type; 1413 1414 switch (event) { 1415 1416 case ACPI_EVENT_POWER_BUTTON: 1417 1418 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) 1419 return; 1420 1421 type = PSWITCH_TYPE_POWER; 1422 smpsw = &sc->sc_smpsw_power; 1423 break; 1424 1425 case ACPI_EVENT_SLEEP_BUTTON: 1426 1427 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) 1428 return; 1429 1430 type = PSWITCH_TYPE_SLEEP; 1431 smpsw = &sc->sc_smpsw_sleep; 1432 break; 1433 1434 default: 1435 rv = AE_TYPE; 1436 goto fail; 1437 } 1438 1439 smpsw->smpsw_type = type; 1440 smpsw->smpsw_name = device_xname(sc->sc_dev); 1441 1442 if (sysmon_pswitch_register(smpsw) != 0) { 1443 rv = AE_ERROR; 1444 goto fail; 1445 } 1446 1447 AcpiClearEvent(event); 1448 1449 rv = AcpiInstallFixedEventHandler(event, 1450 acpi_fixed_button_handler, smpsw); 1451 1452 if (ACPI_FAILURE(rv)) { 1453 sysmon_pswitch_unregister(smpsw); 1454 goto fail; 1455 } 1456 1457 aprint_normal_dev(sc->sc_dev, "fixed %s button present\n", 1458 (type != PSWITCH_TYPE_SLEEP) ? "power" : "sleep"); 1459 1460 return; 1461 1462 fail: 1463 aprint_error_dev(sc->sc_dev, "failed to register " 1464 "fixed event %d: %s\n", event, AcpiFormatException(rv)); 1465 } 1466 1467 static void 1468 acpi_deregister_fixed_button(struct acpi_softc *sc, int event) 1469 { 1470 struct sysmon_pswitch *smpsw; 1471 ACPI_STATUS rv; 1472 1473 switch (event) { 1474 1475 case ACPI_EVENT_POWER_BUTTON: 1476 smpsw = &sc->sc_smpsw_power; 1477 1478 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) { 1479 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER); 1480 return; 1481 } 1482 1483 break; 1484 1485 case ACPI_EVENT_SLEEP_BUTTON: 1486 smpsw = &sc->sc_smpsw_sleep; 1487 1488 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) { 1489 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP); 1490 return; 1491 } 1492 1493 break; 1494 1495 default: 1496 rv = AE_TYPE; 1497 goto fail; 1498 } 1499 1500 rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler); 1501 1502 if (ACPI_SUCCESS(rv)) { 1503 sysmon_pswitch_unregister(smpsw); 1504 return; 1505 } 1506 1507 fail: 1508 aprint_error_dev(sc->sc_dev, "failed to deregister " 1509 "fixed event: %s\n", AcpiFormatException(rv)); 1510 } 1511 1512 static uint32_t 1513 acpi_fixed_button_handler(void *context) 1514 { 1515 static const int handler = OSL_NOTIFY_HANDLER; 1516 struct sysmon_pswitch *smpsw = context; 1517 1518 (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw); 1519 1520 return ACPI_INTERRUPT_HANDLED; 1521 } 1522 1523 static void 1524 acpi_fixed_button_pressed(void *context) 1525 { 1526 struct sysmon_pswitch *smpsw = context; 1527 1528 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n", 1529 (smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ? 1530 "power" : "sleep")); 1531 1532 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED); 1533 } 1534 1535 /* 1536 * Sleep. 1537 */ 1538 static void 1539 acpi_sleep_init(struct acpi_softc *sc) 1540 { 1541 uint8_t a, b, i; 1542 ACPI_STATUS rv; 1543 1544 CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1); 1545 CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3); 1546 CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5); 1547 1548 /* 1549 * Evaluate supported sleep states. 1550 */ 1551 for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) { 1552 1553 rv = AcpiGetSleepTypeData(i, &a, &b); 1554 1555 if (ACPI_SUCCESS(rv)) 1556 sc->sc_sleepstates |= __BIT(i); 1557 } 1558 } 1559 1560 /* 1561 * Must be called with interrupts enabled. 1562 */ 1563 void 1564 acpi_enter_sleep_state(int state) 1565 { 1566 struct acpi_softc *sc = acpi_softc; 1567 ACPI_STATUS rv; 1568 1569 if (acpi_softc == NULL) 1570 return; 1571 1572 if (state == sc->sc_sleepstate) 1573 return; 1574 1575 if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5) 1576 return; 1577 1578 aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state); 1579 1580 switch (state) { 1581 1582 case ACPI_STATE_S0: 1583 sc->sc_sleepstate = ACPI_STATE_S0; 1584 return; 1585 1586 case ACPI_STATE_S1: 1587 case ACPI_STATE_S2: 1588 case ACPI_STATE_S3: 1589 case ACPI_STATE_S4: 1590 1591 if ((sc->sc_sleepstates & __BIT(state)) == 0) { 1592 aprint_error_dev(sc->sc_dev, "sleep state " 1593 "S%d is not available\n", state); 1594 return; 1595 } 1596 1597 /* 1598 * Evaluate the _TTS method. This should be done before 1599 * pmf_system_suspend(9) and the evaluation of _PTS. 1600 * We should also re-evaluate this once we return to 1601 * S0 or if we abort the sleep state transition in the 1602 * middle (see ACPI 3.0, section 7.3.6). In reality, 1603 * however, the _TTS method is seldom seen in the field. 1604 */ 1605 rv = acpi_eval_set_integer(NULL, "\\_TTS", state); 1606 1607 if (ACPI_SUCCESS(rv)) 1608 aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n"); 1609 1610 if (state != ACPI_STATE_S1 && 1611 pmf_system_suspend(PMF_Q_NONE) != true) { 1612 aprint_error_dev(sc->sc_dev, "aborting suspend\n"); 1613 break; 1614 } 1615 1616 /* 1617 * This will evaluate the _PTS and _SST methods, 1618 * but unlike the documentation claims, not _GTS, 1619 * which is evaluated in AcpiEnterSleepState(). 1620 * This must be called with interrupts enabled. 1621 */ 1622 rv = AcpiEnterSleepStatePrep(state); 1623 1624 if (ACPI_FAILURE(rv)) { 1625 aprint_error_dev(sc->sc_dev, "failed to prepare " 1626 "S%d: %s\n", state, AcpiFormatException(rv)); 1627 break; 1628 } 1629 1630 /* 1631 * After the _PTS method has been evaluated, we can 1632 * enable wake and evaluate _PSW (ACPI 4.0, p. 284). 1633 */ 1634 acpi_wakedev_commit(sc, state); 1635 1636 sc->sc_sleepstate = state; 1637 1638 if (state == ACPI_STATE_S1) { 1639 1640 /* 1641 * Before the transition to S1, CPU caches 1642 * must be flushed (see ACPI 4.0, 7.3.4.2). 1643 * 1644 * Note that interrupts must be off before 1645 * calling AcpiEnterSleepState(). Conversely, 1646 * AcpiLeaveSleepState() should always be 1647 * called with interrupts enabled. 1648 */ 1649 acpi_md_OsDisableInterrupt(); 1650 1651 ACPI_FLUSH_CPU_CACHE(); 1652 rv = AcpiEnterSleepState(state); 1653 1654 if (ACPI_FAILURE(rv)) 1655 aprint_error_dev(sc->sc_dev, "failed to " 1656 "enter S1: %s\n", AcpiFormatException(rv)); 1657 1658 /* 1659 * Clear fixed events and disable all GPEs before 1660 * interrupts are enabled. 1661 */ 1662 AcpiClearEvent(ACPI_EVENT_PMTIMER); 1663 AcpiClearEvent(ACPI_EVENT_GLOBAL); 1664 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 1665 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 1666 AcpiClearEvent(ACPI_EVENT_RTC); 1667 #if (!ACPI_REDUCED_HARDWARE) 1668 AcpiHwDisableAllGpes(); 1669 #endif 1670 1671 acpi_md_OsEnableInterrupt(); 1672 rv = AcpiLeaveSleepState(state); 1673 1674 } else { 1675 1676 (void)acpi_md_sleep(state); 1677 1678 if (state == ACPI_STATE_S4) 1679 AcpiEnable(); 1680 1681 (void)pmf_system_bus_resume(PMF_Q_NONE); 1682 (void)AcpiLeaveSleepState(state); 1683 #if (!ACPI_REDUCED_HARDWARE) 1684 (void)AcpiSetFirmwareWakingVector(0, 0); 1685 #endif 1686 (void)pmf_system_resume(PMF_Q_NONE); 1687 } 1688 1689 /* 1690 * No wake GPEs should be enabled at runtime. 1691 */ 1692 acpi_wakedev_commit(sc, ACPI_STATE_S0); 1693 break; 1694 1695 case ACPI_STATE_S5: 1696 1697 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5); 1698 1699 rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1700 1701 if (ACPI_FAILURE(rv)) { 1702 aprint_error_dev(sc->sc_dev, "failed to prepare " 1703 "S%d: %s\n", state, AcpiFormatException(rv)); 1704 break; 1705 } 1706 1707 (void)AcpiDisableAllGpes(); 1708 1709 DELAY(1000000); 1710 1711 sc->sc_sleepstate = state; 1712 acpi_md_OsDisableInterrupt(); 1713 1714 (void)AcpiEnterSleepState(ACPI_STATE_S5); 1715 1716 aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n"); 1717 1718 break; 1719 } 1720 1721 sc->sc_sleepstate = ACPI_STATE_S0; 1722 1723 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0); 1724 } 1725 1726 /* 1727 * Sysctl. 1728 */ 1729 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup") 1730 { 1731 const struct sysctlnode *rnode, *snode; 1732 int err; 1733 1734 err = sysctl_createv(clog, 0, NULL, &rnode, 1735 CTLFLAG_PERMANENT, CTLTYPE_NODE, 1736 "acpi", SYSCTL_DESCR("ACPI subsystem parameters"), 1737 NULL, 0, NULL, 0, 1738 CTL_HW, CTL_CREATE, CTL_EOL); 1739 1740 if (err != 0) 1741 return; 1742 1743 (void)sysctl_createv(NULL, 0, &rnode, NULL, 1744 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD, 1745 "root", SYSCTL_DESCR("ACPI root pointer"), 1746 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer), 1747 CTL_CREATE, CTL_EOL); 1748 1749 err = sysctl_createv(clog, 0, &rnode, &snode, 1750 CTLFLAG_PERMANENT, CTLTYPE_NODE, 1751 "sleep", SYSCTL_DESCR("ACPI sleep"), 1752 NULL, 0, NULL, 0, 1753 CTL_CREATE, CTL_EOL); 1754 1755 if (err != 0) 1756 return; 1757 1758 (void)sysctl_createv(NULL, 0, &snode, NULL, 1759 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 1760 "state", SYSCTL_DESCR("System sleep state"), 1761 sysctl_hw_acpi_sleepstate, 0, NULL, 0, 1762 CTL_CREATE, CTL_EOL); 1763 1764 (void)sysctl_createv(NULL, 0, &snode, NULL, 1765 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING, 1766 "states", SYSCTL_DESCR("Supported sleep states"), 1767 sysctl_hw_acpi_sleepstates, 0, NULL, 0, 1768 CTL_CREATE, CTL_EOL); 1769 1770 err = sysctl_createv(clog, 0, &rnode, &rnode, 1771 CTLFLAG_PERMANENT, CTLTYPE_NODE, 1772 "stat", SYSCTL_DESCR("ACPI statistics"), 1773 NULL, 0, NULL, 0, 1774 CTL_CREATE, CTL_EOL); 1775 1776 if (err != 0) 1777 return; 1778 1779 (void)sysctl_createv(clog, 0, &rnode, NULL, 1780 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD, 1781 "gpe", SYSCTL_DESCR("Number of dispatched GPEs"), 1782 NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount), 1783 CTL_CREATE, CTL_EOL); 1784 1785 (void)sysctl_createv(clog, 0, &rnode, NULL, 1786 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD, 1787 "sci", SYSCTL_DESCR("Number of SCI interrupts"), 1788 NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount), 1789 CTL_CREATE, CTL_EOL); 1790 1791 (void)sysctl_createv(clog, 0, &rnode, NULL, 1792 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD, 1793 "fixed", SYSCTL_DESCR("Number of fixed events"), 1794 sysctl_hw_acpi_fixedstats, 0, NULL, 0, 1795 CTL_CREATE, CTL_EOL); 1796 1797 (void)sysctl_createv(clog, 0, &rnode, NULL, 1798 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD, 1799 "method", SYSCTL_DESCR("Number of methods executed"), 1800 NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount), 1801 CTL_CREATE, CTL_EOL); 1802 1803 CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t)); 1804 CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t)); 1805 } 1806 1807 static int 1808 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS) 1809 { 1810 struct sysctlnode node; 1811 uint64_t t; 1812 int err, i; 1813 1814 for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++) 1815 t += AcpiFixedEventCount[i]; 1816 1817 node = *rnode; 1818 node.sysctl_data = &t; 1819 1820 err = sysctl_lookup(SYSCTLFN_CALL(&node)); 1821 1822 if (err || newp == NULL) 1823 return err; 1824 1825 return 0; 1826 } 1827 1828 static int 1829 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS) 1830 { 1831 struct acpi_softc *sc = acpi_softc; 1832 struct sysctlnode node; 1833 int err, t; 1834 1835 if (acpi_softc == NULL) 1836 return ENOSYS; 1837 1838 node = *rnode; 1839 t = sc->sc_sleepstate; 1840 node.sysctl_data = &t; 1841 1842 err = sysctl_lookup(SYSCTLFN_CALL(&node)); 1843 1844 if (err || newp == NULL) 1845 return err; 1846 1847 if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5) 1848 return EINVAL; 1849 1850 acpi_enter_sleep_state(t); 1851 1852 return 0; 1853 } 1854 1855 static int 1856 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS) 1857 { 1858 struct acpi_softc *sc = acpi_softc; 1859 struct sysctlnode node; 1860 char t[3 * 6 + 1]; 1861 int err; 1862 1863 if (acpi_softc == NULL) 1864 return ENOSYS; 1865 1866 (void)memset(t, '\0', sizeof(t)); 1867 1868 (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s", 1869 ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "", 1870 ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "", 1871 ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "", 1872 ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "", 1873 ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "", 1874 ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : ""); 1875 1876 node = *rnode; 1877 node.sysctl_data = &t; 1878 1879 err = sysctl_lookup(SYSCTLFN_CALL(&node)); 1880 1881 if (err || newp == NULL) 1882 return err; 1883 1884 return 0; 1885 } 1886 1887 /* 1888 * Tables. 1889 */ 1890 ACPI_PHYSICAL_ADDRESS 1891 acpi_OsGetRootPointer(void) 1892 { 1893 ACPI_PHYSICAL_ADDRESS PhysicalAddress; 1894 1895 /* 1896 * We let MD code handle this since there are multiple ways to do it: 1897 * 1898 * IA-32: Use AcpiFindRootPointer() to locate the RSDP. 1899 * 1900 * IA-64: Use the EFI. 1901 */ 1902 PhysicalAddress = acpi_md_OsGetRootPointer(); 1903 1904 if (acpi_root_pointer == 0) 1905 acpi_root_pointer = PhysicalAddress; 1906 1907 return PhysicalAddress; 1908 } 1909 1910 static ACPI_TABLE_HEADER * 1911 acpi_map_rsdt(void) 1912 { 1913 ACPI_PHYSICAL_ADDRESS paddr; 1914 ACPI_TABLE_RSDP *rsdp; 1915 1916 paddr = AcpiOsGetRootPointer(); 1917 1918 if (paddr == 0) 1919 return NULL; 1920 1921 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP)); 1922 1923 if (rsdp == NULL) 1924 return NULL; 1925 1926 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress) 1927 paddr = rsdp->XsdtPhysicalAddress; 1928 else 1929 paddr = rsdp->RsdtPhysicalAddress; 1930 1931 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 1932 1933 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER)); 1934 } 1935 1936 /* 1937 * XXX: Refactor to be a generic function that unmaps tables. 1938 */ 1939 static void 1940 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt) 1941 { 1942 1943 if (rsdt == NULL) 1944 return; 1945 1946 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 1947 } 1948 1949 /* 1950 * XXX: Refactor to be a generic function that maps tables. 1951 */ 1952 ACPI_STATUS 1953 acpi_madt_map(void) 1954 { 1955 ACPI_STATUS rv; 1956 1957 if (madt_header != NULL) 1958 return AE_ALREADY_EXISTS; 1959 1960 rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header); 1961 1962 if (ACPI_FAILURE(rv)) 1963 return rv; 1964 1965 return AE_OK; 1966 } 1967 1968 void 1969 acpi_madt_unmap(void) 1970 { 1971 madt_header = NULL; 1972 } 1973 1974 ACPI_STATUS 1975 acpi_gtdt_map(void) 1976 { 1977 ACPI_STATUS rv; 1978 1979 if (gtdt_header != NULL) 1980 return AE_ALREADY_EXISTS; 1981 1982 rv = AcpiGetTable(ACPI_SIG_GTDT, 1, >dt_header); 1983 1984 if (ACPI_FAILURE(rv)) 1985 return rv; 1986 1987 return AE_OK; 1988 } 1989 1990 void 1991 acpi_gtdt_unmap(void) 1992 { 1993 gtdt_header = NULL; 1994 } 1995 1996 /* 1997 * XXX: Refactor to be a generic function that walks tables. 1998 */ 1999 void 2000 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux) 2001 { 2002 ACPI_SUBTABLE_HEADER *hdrp; 2003 char *madtend, *where; 2004 2005 madtend = (char *)madt_header + madt_header->Length; 2006 where = (char *)madt_header + sizeof (ACPI_TABLE_MADT); 2007 2008 while (where < madtend) { 2009 2010 hdrp = (ACPI_SUBTABLE_HEADER *)where; 2011 2012 if (hdrp->Length == 0 || ACPI_FAILURE(func(hdrp, aux))) 2013 break; 2014 2015 where += hdrp->Length; 2016 } 2017 } 2018 2019 void 2020 acpi_gtdt_walk(ACPI_STATUS (*func)(ACPI_GTDT_HEADER *, void *), void *aux) 2021 { 2022 ACPI_GTDT_HEADER *hdrp; 2023 char *gtdtend, *where; 2024 2025 gtdtend = (char *)gtdt_header + gtdt_header->Length; 2026 where = (char *)gtdt_header + sizeof (ACPI_TABLE_GTDT); 2027 2028 while (where < gtdtend) { 2029 2030 hdrp = (ACPI_GTDT_HEADER *)where; 2031 2032 if (hdrp->Length == 0 || ACPI_FAILURE(func(hdrp, aux))) 2033 break; 2034 2035 where += hdrp->Length; 2036 } 2037 } 2038 2039 /* 2040 * Miscellaneous. 2041 */ 2042 static bool 2043 acpi_is_scope(struct acpi_devnode *ad) 2044 { 2045 int i; 2046 2047 /* 2048 * Return true if the node is a root scope. 2049 */ 2050 if (ad->ad_parent == NULL) 2051 return false; 2052 2053 if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT) 2054 return false; 2055 2056 for (i = 0; i < __arraycount(acpi_scopes); i++) { 2057 2058 if (acpi_scopes[i] == NULL) 2059 continue; 2060 2061 if (ad->ad_handle == acpi_scopes[i]) 2062 return true; 2063 } 2064 2065 return false; 2066 } 2067 2068 bool 2069 acpi_device_present(ACPI_HANDLE handle) 2070 { 2071 ACPI_STATUS rv; 2072 ACPI_INTEGER sta; 2073 2074 rv = acpi_eval_integer(handle, "_STA", &sta); 2075 2076 if (ACPI_FAILURE(rv)) { 2077 /* No _STA method -> must be there */ 2078 return rv == AE_NOT_FOUND; 2079 } 2080 2081 return (sta & ACPI_STA_OK) == ACPI_STA_OK; 2082 } 2083 2084 /* 2085 * ACPIVERBOSE. 2086 */ 2087 void 2088 acpi_load_verbose(void) 2089 { 2090 2091 if (acpi_verbose_loaded == 0) 2092 module_autoload("acpiverbose", MODULE_CLASS_MISC); 2093 } 2094 2095 void 2096 acpi_print_verbose_stub(struct acpi_softc *sc) 2097 { 2098 2099 acpi_load_verbose(); 2100 2101 if (acpi_verbose_loaded != 0) 2102 acpi_print_verbose(sc); 2103 } 2104 2105 void 2106 acpi_print_dev_stub(const char *pnpstr) 2107 { 2108 2109 acpi_load_verbose(); 2110 2111 if (acpi_verbose_loaded != 0) 2112 acpi_print_dev(pnpstr); 2113 } 2114 2115 MALLOC_DECLARE(M_ACPI); /* XXX: ACPI_ACTIVATE_DEV should use kmem(9). */ 2116 2117 /* 2118 * ACPI_ACTIVATE_DEV. 2119 */ 2120 static void 2121 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di) 2122 { 2123 2124 #ifndef ACPI_ACTIVATE_DEV 2125 return; 2126 } 2127 #else 2128 static const int valid = ACPI_VALID_HID; 2129 ACPI_DEVICE_INFO *newdi; 2130 ACPI_STATUS rv; 2131 2132 2133 /* 2134 * If the device is valid and present, 2135 * but not enabled, try to activate it. 2136 */ 2137 if (((*di)->Valid & valid) != valid) 2138 return; 2139 2140 if (!acpi_device_present(handle)) 2141 return; 2142 2143 rv = acpi_allocate_resources(handle); 2144 2145 if (ACPI_FAILURE(rv)) 2146 goto fail; 2147 2148 rv = AcpiGetObjectInfo(handle, &newdi); 2149 2150 if (ACPI_FAILURE(rv)) 2151 goto fail; 2152 2153 ACPI_FREE(*di); 2154 *di = newdi; 2155 2156 aprint_verbose_dev(acpi_softc->sc_dev, 2157 "%s activated\n", (*di)->HardwareId.String); 2158 2159 return; 2160 2161 fail: 2162 aprint_error_dev(acpi_softc->sc_dev, "failed to " 2163 "activate %s\n", (*di)->HardwareId.String); 2164 } 2165 2166 /* 2167 * XXX: This very incomplete. 2168 */ 2169 ACPI_STATUS 2170 acpi_allocate_resources(ACPI_HANDLE handle) 2171 { 2172 ACPI_BUFFER bufp, bufc, bufn; 2173 ACPI_RESOURCE *resp, *resc, *resn; 2174 ACPI_RESOURCE_IRQ *irq; 2175 #if 0 2176 ACPI_RESOURCE_EXTENDED_IRQ *xirq; 2177 #endif 2178 ACPI_STATUS rv; 2179 uint delta; 2180 2181 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources); 2182 if (ACPI_FAILURE(rv)) 2183 goto out; 2184 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources); 2185 if (ACPI_FAILURE(rv)) { 2186 goto out1; 2187 } 2188 2189 bufn.Length = 1000; 2190 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK); 2191 resp = bufp.Pointer; 2192 resc = bufc.Pointer; 2193 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG && 2194 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) { 2195 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG) 2196 resp = ACPI_NEXT_RESOURCE(resp); 2197 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG) 2198 break; 2199 /* Found identical Id */ 2200 resn->Type = resc->Type; 2201 switch (resc->Type) { 2202 case ACPI_RESOURCE_TYPE_IRQ: 2203 memcpy(&resn->Data, &resp->Data, 2204 sizeof(ACPI_RESOURCE_IRQ)); 2205 irq = (ACPI_RESOURCE_IRQ *)&resn->Data; 2206 irq->Interrupts[0] = 2207 ((ACPI_RESOURCE_IRQ *)&resp->Data)-> 2208 Interrupts[irq->InterruptCount-1]; 2209 irq->InterruptCount = 1; 2210 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ); 2211 break; 2212 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 2213 memcpy(&resn->Data, &resp->Data, 2214 sizeof(ACPI_RESOURCE_EXTENDED_IRQ)); 2215 #if 0 2216 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data; 2217 /* 2218 * XXX: Not duplicating the interrupt logic above 2219 * because its not clear what it accomplishes. 2220 */ 2221 xirq->Interrupts[0] = 2222 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)-> 2223 Interrupts[irq->NumberOfInterrupts-1]; 2224 xirq->NumberOfInterrupts = 1; 2225 #endif 2226 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ); 2227 break; 2228 case ACPI_RESOURCE_TYPE_IO: 2229 memcpy(&resn->Data, &resp->Data, 2230 sizeof(ACPI_RESOURCE_IO)); 2231 resn->Length = resp->Length; 2232 break; 2233 default: 2234 aprint_error_dev(acpi_softc->sc_dev, 2235 "%s: invalid type %u\n", __func__, resc->Type); 2236 rv = AE_BAD_DATA; 2237 goto out2; 2238 } 2239 resc = ACPI_NEXT_RESOURCE(resc); 2240 resn = ACPI_NEXT_RESOURCE(resn); 2241 resp = ACPI_NEXT_RESOURCE(resp); 2242 delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer; 2243 if (delta >= 2244 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) { 2245 bufn.Length *= 2; 2246 bufn.Pointer = realloc(bufn.Pointer, bufn.Length, 2247 M_ACPI, M_WAITOK); 2248 resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer + 2249 delta); 2250 } 2251 } 2252 2253 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) { 2254 aprint_error_dev(acpi_softc->sc_dev, 2255 "%s: resc not exhausted\n", __func__); 2256 rv = AE_BAD_DATA; 2257 goto out3; 2258 } 2259 2260 resn->Type = ACPI_RESOURCE_TYPE_END_TAG; 2261 rv = AcpiSetCurrentResources(handle, &bufn); 2262 2263 if (ACPI_FAILURE(rv)) 2264 aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set " 2265 "resources: %s\n", __func__, AcpiFormatException(rv)); 2266 2267 out3: 2268 free(bufn.Pointer, M_ACPI); 2269 out2: 2270 ACPI_FREE(bufc.Pointer); 2271 out1: 2272 ACPI_FREE(bufp.Pointer); 2273 out: 2274 return rv; 2275 } 2276 2277 #endif /* ACPI_ACTIVATE_DEV */ 2278