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