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