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