1 /* $NetBSD: acpi.c,v 1.35 2003/04/18 01:31:34 thorpej Exp $ */ 2 3 /* 4 * Copyright 2001, 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Autoconfiguration support for the Intel ACPI Component Architecture 40 * ACPI reference implementation. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.35 2003/04/18 01:31:34 thorpej Exp $"); 45 46 #include "opt_acpi.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/device.h> 51 #include <sys/malloc.h> 52 #include <sys/kernel.h> 53 #include <sys/proc.h> 54 55 #include <dev/acpi/acpica.h> 56 #include <dev/acpi/acpireg.h> 57 #include <dev/acpi/acpivar.h> 58 #include <dev/acpi/acpi_osd.h> 59 #ifdef ACPIVERBOSE 60 #include <dev/acpi/acpidevs_data.h> 61 #endif 62 63 #ifndef ACPI_ACTIVATE_DEV 64 #define ACPI_ACTIVATE_DEV 0 65 #endif 66 67 #ifdef ACPI_PCI_FIXUP 68 #include <dev/acpi/acpica/Subsystem/acnamesp.h> /* AcpiNsGetNodeByPath() */ 69 #include <dev/pci/pcidevs.h> 70 #endif 71 72 MALLOC_DECLARE(M_ACPI); 73 74 #include <machine/acpi_machdep.h> 75 76 #ifdef ENABLE_DEBUGGER 77 #define ACPI_DBGR_INIT 0x01 78 #define ACPI_DBGR_TABLES 0x02 79 #define ACPI_DBGR_ENABLE 0x04 80 #define ACPI_DBGR_PROBE 0x08 81 #define ACPI_DBGR_RUNNING 0x10 82 83 int acpi_dbgr = 0x00; 84 #endif 85 86 int acpi_match(struct device *, struct cfdata *, void *); 87 void acpi_attach(struct device *, struct device *, void *); 88 89 int acpi_print(void *aux, const char *); 90 91 extern struct cfdriver acpi_cd; 92 93 CFATTACH_DECL(acpi, sizeof(struct acpi_softc), 94 acpi_match, acpi_attach, NULL, NULL); 95 96 /* 97 * This is a flag we set when the ACPI subsystem is active. Machine 98 * dependent code may wish to skip other steps (such as attaching 99 * subsystems that ACPI supercedes) when ACPI is active. 100 */ 101 int acpi_active; 102 103 /* 104 * Pointer to the ACPI subsystem's state. There can be only 105 * one ACPI instance. 106 */ 107 struct acpi_softc *acpi_softc; 108 109 /* 110 * Locking stuff. 111 */ 112 static struct simplelock acpi_slock; 113 static int acpi_locked; 114 115 /* 116 * Prototypes. 117 */ 118 void acpi_shutdown(void *); 119 ACPI_STATUS acpi_disable(struct acpi_softc *sc); 120 void acpi_build_tree(struct acpi_softc *); 121 ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **); 122 123 void acpi_enable_fixed_events(struct acpi_softc *); 124 #ifdef ACPI_PCI_FIXUP 125 void acpi_pci_fixup(struct acpi_softc *); 126 #endif 127 #if defined(ACPI_PCI_FIXUP) || ACPI_ACTIVATE_DEV 128 ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE handle); 129 #endif 130 131 /* 132 * acpi_probe: 133 * 134 * Probe for ACPI support. This is called by the 135 * machine-dependent ACPI front-end. All of the 136 * actual work is done by ACPICA. 137 * 138 * NOTE: This is not an autoconfiguration interface function. 139 */ 140 int 141 acpi_probe(void) 142 { 143 static int beenhere; 144 ACPI_STATUS rv; 145 146 if (beenhere != 0) 147 panic("acpi_probe: ACPI has already been probed"); 148 beenhere = 1; 149 150 simple_lock_init(&acpi_slock); 151 acpi_locked = 0; 152 153 /* 154 * Start up ACPICA. 155 */ 156 #ifdef ENABLE_DEBUGGER 157 if (acpi_dbgr & ACPI_DBGR_INIT) 158 acpi_osd_debugger(); 159 #endif 160 161 rv = AcpiInitializeSubsystem(); 162 if (rv != AE_OK) { 163 printf("ACPI: unable to initialize ACPICA: %d\n", rv); 164 return (0); 165 } 166 167 #ifdef ENABLE_DEBUGGER 168 if (acpi_dbgr & ACPI_DBGR_TABLES) 169 acpi_osd_debugger(); 170 #endif 171 172 rv = AcpiLoadTables(); 173 if (rv != AE_OK) { 174 printf("ACPI: unable to load tables: %d\n", rv); 175 return (0); 176 } 177 178 /* 179 * Looks like we have ACPI! 180 */ 181 182 return (1); 183 } 184 185 /* 186 * acpi_match: 187 * 188 * Autoconfiguration `match' routine. 189 */ 190 int 191 acpi_match(struct device *parent, struct cfdata *match, void *aux) 192 { 193 struct acpibus_attach_args *aa = aux; 194 195 if (strcmp(aa->aa_busname, acpi_cd.cd_name) != 0) 196 return (0); 197 198 /* 199 * XXX Check other locators? Hard to know -- machine 200 * dependent code has already checked for the presence 201 * of ACPI by calling acpi_probe(), so I suppose we 202 * don't really have to do anything else. 203 */ 204 return (1); 205 } 206 207 /* 208 * acpi_attach: 209 * 210 * Autoconfiguration `attach' routine. Finish initializing 211 * ACPICA (some initialization was done in acpi_probe(), 212 * which was required to check for the presence of ACPI), 213 * and enable the ACPI subsystem. 214 */ 215 void 216 acpi_attach(struct device *parent, struct device *self, void *aux) 217 { 218 struct acpi_softc *sc = (void *) self; 219 struct acpibus_attach_args *aa = aux; 220 ACPI_TABLE_HEADER *ap = &AcpiGbl_XSDT->Header; 221 ACPI_STATUS rv; 222 223 printf("\n"); 224 225 if (acpi_softc != NULL) 226 panic("acpi_attach: ACPI has already been attached"); 227 228 sysmon_power_settype("acpi"); 229 230 printf("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n", 231 sc->sc_dev.dv_xname, 232 ap->OemId, ap->OemTableId, ap->OemRevision, 233 ap->AslCompilerId, ap->AslCompilerRevision); 234 235 sc->sc_iot = aa->aa_iot; 236 sc->sc_memt = aa->aa_memt; 237 sc->sc_pc = aa->aa_pc; 238 sc->sc_pciflags = aa->aa_pciflags; 239 sc->sc_ic = aa->aa_ic; 240 241 acpi_softc = sc; 242 243 /* 244 * Install the default address space handlers. 245 */ 246 247 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 248 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 249 if (rv != AE_OK) { 250 printf("%s: unable to install SYSTEM MEMORY handler: %d\n", 251 sc->sc_dev.dv_xname, rv); 252 return; 253 } 254 255 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 256 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 257 if (rv != AE_OK) { 258 printf("%s: unable to install SYSTEM IO handler: %d\n", 259 sc->sc_dev.dv_xname, rv); 260 return; 261 } 262 263 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 264 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 265 if (rv != AE_OK) { 266 printf("%s: unable to install PCI CONFIG handler: %d\n", 267 sc->sc_dev.dv_xname, rv); 268 return; 269 } 270 271 /* 272 * Bring ACPI on-line. 273 * 274 * Note that we request that _STA (device init) and _INI (object init) 275 * methods not be run. 276 * 277 * XXX We need to arrange for the object init pass after we have 278 * XXX attached all of our children. 279 */ 280 #ifdef ENABLE_DEBUGGER 281 if (acpi_dbgr & ACPI_DBGR_ENABLE) 282 acpi_osd_debugger(); 283 #endif 284 rv = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT); 285 if (rv != AE_OK) { 286 printf("%s: unable to enable ACPI: %d\n", 287 sc->sc_dev.dv_xname, rv); 288 return; 289 } 290 acpi_active = 1; 291 292 /* Our current state is "awake". */ 293 sc->sc_sleepstate = ACPI_STATE_S0; 294 295 /* Show SCI interrupt. */ 296 if (AcpiGbl_FADT != NULL) 297 printf("%s: SCI interrupting at int %d\n", 298 sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt); 299 /* 300 * Check for fixed-hardware features. 301 */ 302 acpi_enable_fixed_events(sc); 303 304 /* 305 * Fix up PCI devices. 306 */ 307 #ifdef ACPI_PCI_FIXUP 308 acpi_pci_fixup(sc); 309 #endif 310 311 /* 312 * Scan the namespace and build our device tree. 313 */ 314 #ifdef ENABLE_DEBUGGER 315 if (acpi_dbgr & ACPI_DBGR_PROBE) 316 acpi_osd_debugger(); 317 #endif 318 acpi_md_callback((struct device *)sc); 319 acpi_build_tree(sc); 320 321 /* 322 * Register a shutdown hook that disables certain ACPI 323 * events that might happen and confuse us while we're 324 * trying to shut down. 325 */ 326 sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc); 327 if (sc->sc_sdhook == NULL) 328 printf("%s: WARNING: unable to register shutdown hook\n", 329 sc->sc_dev.dv_xname); 330 331 #ifdef ENABLE_DEBUGGER 332 if (acpi_dbgr & ACPI_DBGR_RUNNING) 333 acpi_osd_debugger(); 334 #endif 335 } 336 337 /* 338 * acpi_shutdown: 339 * 340 * Shutdown hook for ACPI -- disable some events that 341 * might confuse us. 342 */ 343 void 344 acpi_shutdown(void *arg) 345 { 346 struct acpi_softc *sc = arg; 347 348 if (acpi_disable(sc) != AE_OK) 349 printf("%s: WARNING: unable to disable ACPI\n", 350 sc->sc_dev.dv_xname); 351 } 352 353 /* 354 * acpi_disable: 355 * 356 * Disable ACPI. 357 */ 358 ACPI_STATUS 359 acpi_disable(struct acpi_softc *sc) 360 { 361 ACPI_STATUS rv = AE_OK; 362 363 if (acpi_active) { 364 rv = AcpiDisable(); 365 if (rv == AE_OK) 366 acpi_active = 0; 367 } 368 return (rv); 369 } 370 371 struct acpi_make_devnode_state { 372 struct acpi_softc *softc; 373 struct acpi_scope *scope; 374 }; 375 376 /* 377 * acpi_build_tree: 378 * 379 * Scan relevant portions of the ACPI namespace and attach 380 * child devices. 381 */ 382 void 383 acpi_build_tree(struct acpi_softc *sc) 384 { 385 static const char *scopes[] = { 386 "\\_PR_", /* ACPI 1.0 processor namespace */ 387 "\\_SB_", /* system bus namespace */ 388 "\\_SI_", /* system idicator namespace */ 389 "\\_TZ_", /* ACPI 1.0 thermal zone namespace */ 390 NULL, 391 }; 392 struct acpi_attach_args aa; 393 struct acpi_make_devnode_state state; 394 struct acpi_scope *as; 395 struct acpi_devnode *ad; 396 ACPI_HANDLE parent; 397 int i; 398 399 TAILQ_INIT(&sc->sc_scopes); 400 401 state.softc = sc; 402 403 /* 404 * Scan the namespace and build our tree. 405 */ 406 for (i = 0; scopes[i] != NULL; i++) { 407 as = malloc(sizeof(*as), M_ACPI, M_WAITOK); 408 as->as_name = scopes[i]; 409 TAILQ_INIT(&as->as_devnodes); 410 411 TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list); 412 413 state.scope = as; 414 415 if (AcpiGetHandle(ACPI_ROOT_OBJECT, (char *) scopes[i], 416 &parent) == AE_OK) { 417 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, 418 acpi_make_devnode, &state, NULL); 419 } 420 421 /* Now, for this namespace, try and attach the devices. */ 422 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) { 423 aa.aa_node = ad; 424 aa.aa_iot = sc->sc_iot; 425 aa.aa_memt = sc->sc_memt; 426 aa.aa_pc = sc->sc_pc; 427 aa.aa_pciflags = sc->sc_pciflags; 428 aa.aa_ic = sc->sc_ic; 429 430 if (ad->ad_devinfo.Type == ACPI_TYPE_DEVICE) { 431 /* 432 * XXX We only attach devices which are: 433 * 434 * - present 435 * - enabled 436 * - functioning properly 437 * 438 * However, if enabled, it's decoding resources, 439 * so we should claim them, if possible. 440 * Requires changes to bus_space(9). 441 */ 442 if ((ad->ad_devinfo.CurrentStatus & 443 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED| 444 ACPI_STA_DEV_OK)) != 445 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED| 446 ACPI_STA_DEV_OK)) 447 continue; 448 449 /* 450 * XXX Same problem as above... 451 */ 452 if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) 453 == 0) 454 continue; 455 } 456 457 ad->ad_device = config_found(&sc->sc_dev, 458 &aa, acpi_print); 459 } 460 } 461 } 462 463 #if ACPI_ACTIVATE_DEV 464 static void 465 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO *di) 466 { 467 ACPI_STATUS rv; 468 469 #ifdef ACPI_DEBUG 470 printf("acpi_activate_device: %s, old status=%x\n", 471 di->HardwareId, di->CurrentStatus); 472 #endif 473 474 rv = acpi_allocate_resources(handle); 475 if (ACPI_FAILURE(rv)) { 476 printf("acpi: activate failed for %s\n", di->HardwareId); 477 } else { 478 printf("acpi: activated %s\n", di->HardwareId); 479 } 480 481 (void)AcpiGetObjectInfo(handle, di); 482 #ifdef ACPI_DEBUG 483 printf("acpi_activate_device: %s, new status=%x\n", 484 di->HardwareId, di->CurrentStatus); 485 #endif 486 } 487 #endif /* ACPI_ACTIVATE_DEV */ 488 489 /* 490 * acpi_make_devnode: 491 * 492 * Make an ACPI devnode. 493 */ 494 ACPI_STATUS 495 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context, 496 void **status) 497 { 498 struct acpi_make_devnode_state *state = context; 499 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG) 500 struct acpi_softc *sc = state->softc; 501 #endif 502 struct acpi_scope *as = state->scope; 503 struct acpi_devnode *ad; 504 ACPI_DEVICE_INFO devinfo; 505 ACPI_OBJECT_TYPE type; 506 ACPI_STATUS rv; 507 508 if (AcpiGetType(handle, &type) == AE_OK) { 509 rv = AcpiGetObjectInfo(handle, &devinfo); 510 if (rv != AE_OK) { 511 #ifdef ACPI_DEBUG 512 printf("%s: AcpiGetObjectInfo failed\n", 513 sc->sc_dev.dv_xname); 514 #endif 515 goto out; /* XXX why return OK */ 516 } 517 518 switch (type) { 519 case ACPI_TYPE_DEVICE: 520 #if ACPI_ACTIVATE_DEV 521 if ((devinfo.Valid & ACPI_VALID_STA) && 522 (devinfo.CurrentStatus & 523 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) == 524 ACPI_STA_DEV_PRESENT) 525 acpi_activate_device(handle, &devinfo); 526 /* FALLTHROUGH */ 527 #endif 528 529 case ACPI_TYPE_PROCESSOR: 530 case ACPI_TYPE_THERMAL: 531 case ACPI_TYPE_POWER: 532 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO); 533 if (ad == NULL) 534 return (AE_NO_MEMORY); 535 536 ad->ad_handle = handle; 537 ad->ad_level = level; 538 ad->ad_scope = as; 539 ad->ad_type = type; 540 541 TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list); 542 543 rv = AcpiGetObjectInfo(handle, &ad->ad_devinfo); 544 if (rv != AE_OK) 545 goto out; 546 547 if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0) 548 goto out; 549 550 #ifdef ACPI_EXTRA_DEBUG 551 printf("%s: HID %s found in scope %s level %d\n", 552 sc->sc_dev.dv_xname, ad->ad_devinfo.HardwareId, 553 as->as_name, ad->ad_level); 554 if (ad->ad_devinfo.Valid & ACPI_VALID_UID) 555 printf(" UID %s\n", 556 ad->ad_devinfo.UniqueId); 557 if (ad->ad_devinfo.Valid & ACPI_VALID_ADR) 558 printf(" ADR 0x%016qx\n", 559 ad->ad_devinfo.Address); 560 if (ad->ad_devinfo.Valid & ACPI_VALID_STA) 561 printf(" STA 0x%08x\n", 562 ad->ad_devinfo.CurrentStatus); 563 #endif 564 } 565 } 566 out: 567 return (AE_OK); 568 } 569 570 /* 571 * acpi_print: 572 * 573 * Autoconfiguration print routine. 574 */ 575 int 576 acpi_print(void *aux, const char *pnp) 577 { 578 struct acpi_attach_args *aa = aux; 579 580 if (pnp) { 581 if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_HID) { 582 char *pnpstr = aa->aa_node->ad_devinfo.HardwareId; 583 char *str; 584 585 aprint_normal("%s ", pnpstr); 586 if (acpi_eval_string(aa->aa_node->ad_handle, 587 "_STR", &str) == AE_OK) { 588 aprint_normal("[%s] ", str); 589 AcpiOsFree(str); 590 } 591 #ifdef ACPIVERBOSE 592 else { 593 int i; 594 595 for (i = 0; i < sizeof(acpi_knowndevs) / 596 sizeof(acpi_knowndevs[0]); i++) { 597 if (strcmp(acpi_knowndevs[i].pnp, 598 pnpstr) == 0) { 599 printf("[%s] ", 600 acpi_knowndevs[i].str); 601 } 602 } 603 } 604 605 #endif 606 } else { 607 aprint_normal("ACPI Object Type '%s' (0x%02x) ", 608 AcpiUtGetTypeName(aa->aa_node->ad_devinfo.Type), 609 aa->aa_node->ad_devinfo.Type); 610 } 611 aprint_normal("at %s", pnp); 612 } else { 613 aprint_normal(" (%s", aa->aa_node->ad_devinfo.HardwareId); 614 if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_UID) { 615 char *uid; 616 617 if (aa->aa_node->ad_devinfo.UniqueId[0] == '\0') 618 uid = "<null>"; 619 else 620 uid = aa->aa_node->ad_devinfo.UniqueId; 621 aprint_normal("-%s", uid); 622 } 623 aprint_normal(")"); 624 } 625 626 return (UNCONF); 627 } 628 629 /***************************************************************************** 630 * ACPI fixed-hardware feature handlers 631 *****************************************************************************/ 632 633 UINT32 acpi_fixed_button_handler(void *); 634 void acpi_fixed_button_pressed(void *); 635 636 /* 637 * acpi_enable_fixed_events: 638 * 639 * Enable any fixed-hardware feature handlers. 640 */ 641 void 642 acpi_enable_fixed_events(struct acpi_softc *sc) 643 { 644 static int beenhere; 645 ACPI_STATUS rv; 646 647 KASSERT(beenhere == 0); 648 beenhere = 1; 649 650 /* 651 * Check for fixed-hardware buttons. 652 */ 653 654 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 655 printf("%s: fixed-feature power button present\n", 656 sc->sc_dev.dv_xname); 657 sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname; 658 sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER; 659 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) { 660 printf("%s: unable to register fixed power button " 661 "with sysmon\n", sc->sc_dev.dv_xname); 662 } else { 663 rv = AcpiInstallFixedEventHandler( 664 ACPI_EVENT_POWER_BUTTON, 665 acpi_fixed_button_handler, &sc->sc_smpsw_power); 666 if (rv != AE_OK) { 667 printf("%s: unable to install handler for " 668 "fixed power button: %d\n", 669 sc->sc_dev.dv_xname, rv); 670 } 671 } 672 } 673 674 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 675 printf("%s: fixed-feature sleep button present\n", 676 sc->sc_dev.dv_xname); 677 sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname; 678 sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP; 679 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) { 680 printf("%s: unable to register fixed sleep button " 681 "with sysmon\n", sc->sc_dev.dv_xname); 682 } else { 683 rv = AcpiInstallFixedEventHandler( 684 ACPI_EVENT_SLEEP_BUTTON, 685 acpi_fixed_button_handler, &sc->sc_smpsw_sleep); 686 if (rv != AE_OK) { 687 printf("%s: unable to install handler for " 688 "fixed sleep button: %d\n", 689 sc->sc_dev.dv_xname, rv); 690 } 691 } 692 } 693 } 694 695 /* 696 * acpi_fixed_button_handler: 697 * 698 * Event handler for the fixed buttons. 699 */ 700 UINT32 701 acpi_fixed_button_handler(void *context) 702 { 703 struct sysmon_pswitch *smpsw = context; 704 int rv; 705 706 #ifdef ACPI_BUT_DEBUG 707 printf("%s: fixed button handler\n", smpsw->smpsw_name); 708 #endif 709 710 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO, 711 acpi_fixed_button_pressed, smpsw); 712 if (rv != AE_OK) 713 printf("%s: WARNING: unable to queue fixed button pressed " 714 "callback: %d\n", smpsw->smpsw_name, rv); 715 716 return (ACPI_INTERRUPT_HANDLED); 717 } 718 719 /* 720 * acpi_fixed_button_pressed: 721 * 722 * Deal with a fixed button being pressed. 723 */ 724 void 725 acpi_fixed_button_pressed(void *context) 726 { 727 struct sysmon_pswitch *smpsw = context; 728 729 #ifdef ACPI_BUT_DEBUG 730 printf("%s: fixed button pressed, calling sysmon\n", 731 smpsw->smpsw_name); 732 #endif 733 734 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED); 735 } 736 737 /***************************************************************************** 738 * ACPI utility routines. 739 *****************************************************************************/ 740 741 /* 742 * acpi_eval_integer: 743 * 744 * Evaluate an integer object. 745 */ 746 ACPI_STATUS 747 acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp) 748 { 749 ACPI_STATUS rv; 750 ACPI_BUFFER buf; 751 ACPI_OBJECT param; 752 753 if (handle == NULL) 754 handle = ACPI_ROOT_OBJECT; 755 756 buf.Pointer = ¶m; 757 buf.Length = sizeof(param); 758 759 rv = AcpiEvaluateObject(handle, path, NULL, &buf); 760 if (rv == AE_OK) { 761 if (param.Type == ACPI_TYPE_INTEGER) 762 *valp = param.Integer.Value; 763 else 764 rv = AE_TYPE; 765 } 766 767 return (rv); 768 } 769 770 /* 771 * acpi_eval_string: 772 * 773 * Evaluate a (Unicode) string object. 774 */ 775 ACPI_STATUS 776 acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp) 777 { 778 ACPI_STATUS rv; 779 ACPI_BUFFER buf; 780 ACPI_OBJECT *param; 781 782 if (handle == NULL) 783 handle = ACPI_ROOT_OBJECT; 784 785 buf.Pointer = NULL; 786 buf.Length = ACPI_ALLOCATE_BUFFER; 787 788 rv = AcpiEvaluateObject(handle, path, NULL, &buf); 789 param = (ACPI_OBJECT *)buf.Pointer; 790 if (rv == AE_OK && param) { 791 if (param->Type == ACPI_TYPE_STRING) { 792 char *ptr = param->String.Pointer; 793 size_t len; 794 while (*ptr++) 795 continue; 796 len = ptr - param->String.Pointer; 797 if ((*stringp = AcpiOsAllocate(len)) == NULL) { 798 rv = AE_NO_MEMORY; 799 goto done; 800 } 801 (void)memcpy(*stringp, param->String.Pointer, len); 802 goto done; 803 } 804 rv = AE_TYPE; 805 } 806 done: 807 if (buf.Pointer) 808 AcpiOsFree(buf.Pointer); 809 return (rv); 810 } 811 812 813 /* 814 * acpi_eval_struct: 815 * 816 * Evaluate a more complex structure. Caller must free buf.Pointer. 817 */ 818 ACPI_STATUS 819 acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp) 820 { 821 ACPI_STATUS rv; 822 823 if (handle == NULL) 824 handle = ACPI_ROOT_OBJECT; 825 826 bufp->Pointer = NULL; 827 bufp->Length = ACPI_ALLOCATE_BUFFER; 828 829 rv = AcpiEvaluateObject(handle, path, NULL, bufp); 830 831 return (rv); 832 } 833 834 /* 835 * acpi_get: 836 * 837 * Fetch data info the specified (empty) ACPI buffer. 838 */ 839 ACPI_STATUS 840 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf, 841 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *)) 842 { 843 ACPI_STATUS rv; 844 845 buf->Pointer = NULL; 846 buf->Length = 0; 847 848 rv = (*getit)(handle, buf); 849 if (rv != AE_BUFFER_OVERFLOW) 850 return (rv); 851 852 buf->Pointer = AcpiOsAllocate(buf->Length); 853 if (buf->Pointer == NULL) 854 return (AE_NO_MEMORY); 855 memset(buf->Pointer, 0, buf->Length); 856 857 return ((*getit)(handle, buf)); 858 } 859 860 861 /* 862 * acpi_acquire_global_lock: 863 * 864 * Acquire global lock, with sleeping appropriately. 865 */ 866 #define ACQUIRE_WAIT 1000 867 868 ACPI_STATUS 869 acpi_acquire_global_lock(UINT32 *handle) 870 { 871 ACPI_STATUS status; 872 UINT32 h; 873 int s; 874 875 s = splhigh(); 876 simple_lock(&acpi_slock); 877 while (acpi_locked) 878 ltsleep(&acpi_slock, PVM, "acpi_lock", 0, &acpi_slock); 879 acpi_locked = 1; 880 simple_unlock(&acpi_slock); 881 splx(s); 882 status = AcpiAcquireGlobalLock(ACQUIRE_WAIT, &h); 883 if (ACPI_SUCCESS(status)) 884 *handle = h; 885 else { 886 printf("acpi: cannot acquire lock. status=0x%X\n", status); 887 s = splhigh(); 888 simple_lock(&acpi_slock); 889 acpi_locked = 0; 890 simple_unlock(&acpi_slock); 891 splx(s); 892 } 893 894 return (status); 895 } 896 897 void 898 acpi_release_global_lock(UINT32 handle) 899 { 900 ACPI_STATUS status; 901 int s; 902 903 if (!acpi_locked) { 904 printf("acpi: not locked.\n"); 905 return; 906 } 907 908 status = AcpiReleaseGlobalLock(handle); 909 if (ACPI_FAILURE(status)) { 910 printf("acpi: AcpiReleaseGlobalLock failed. status=0x%X\n", 911 status); 912 return; 913 } 914 915 s = splhigh(); 916 simple_lock(&acpi_slock); 917 acpi_locked = 0; 918 simple_unlock(&acpi_slock); 919 splx(s); 920 wakeup(&acpi_slock); 921 } 922 923 int 924 acpi_is_global_locked(void) 925 { 926 return (acpi_locked); 927 } 928 929 930 931 /***************************************************************************** 932 * ACPI sleep support. 933 *****************************************************************************/ 934 935 static int 936 is_available_state(struct acpi_softc *sc, int state) 937 { 938 UINT8 type_a, type_b; 939 940 return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state, 941 &type_a, &type_b))); 942 } 943 944 /* 945 * acpi_enter_sleep_state: 946 * 947 * enter to the specified sleep state. 948 */ 949 950 ACPI_STATUS 951 acpi_enter_sleep_state(struct acpi_softc *sc, int state) 952 { 953 int s; 954 ACPI_STATUS ret = AE_OK; 955 956 switch (state) { 957 case ACPI_STATE_S0: 958 break; 959 case ACPI_STATE_S1: 960 case ACPI_STATE_S2: 961 case ACPI_STATE_S3: 962 case ACPI_STATE_S4: 963 if (!is_available_state(sc, state)) { 964 printf("acpi: cannot enter the sleep state (%d).\n", 965 state); 966 break; 967 } 968 ret = AcpiEnterSleepStatePrep(state); 969 if (ACPI_FAILURE(ret)) { 970 printf("acpi: failed preparing to sleep (%s)\n", 971 AcpiFormatException(ret)); 972 break; 973 } 974 if (state==ACPI_STATE_S1) { 975 /* just enter the state */ 976 acpi_md_OsDisableInterrupt(); 977 AcpiEnterSleepState((UINT8)state); 978 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 979 } else { 980 /* XXX: powerhooks(9) framework is too poor to 981 * support ACPI sleep state... 982 */ 983 dopowerhooks(PWR_SOFTSUSPEND); 984 s = splhigh(); 985 dopowerhooks(PWR_SUSPEND); 986 acpi_md_sleep(state); 987 dopowerhooks(PWR_RESUME); 988 splx(s); 989 dopowerhooks(PWR_SOFTRESUME); 990 if (state==ACPI_STATE_S4) 991 AcpiEnable(); 992 } 993 AcpiLeaveSleepState((UINT8)state); 994 break; 995 case ACPI_STATE_S5: 996 AcpiEnterSleepStatePrep(ACPI_STATE_S5); 997 acpi_md_OsDisableInterrupt(); 998 AcpiEnterSleepState(ACPI_STATE_S5); 999 printf("WARNING: powerdown failed!\n"); 1000 break; 1001 } 1002 1003 return (ret); 1004 } 1005 1006 #ifdef ACPI_PCI_FIXUP 1007 ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **); 1008 /* 1009 * acpi_pci_fixup: 1010 * 1011 * Set up PCI devices that BIOS didn't handle right. 1012 * Iterate through all devices and try to get the _PTR 1013 * (PCI Routing Table). If it exists then make sure all 1014 * interrupt links that it uses are working. 1015 */ 1016 void 1017 acpi_pci_fixup(struct acpi_softc *sc) 1018 { 1019 ACPI_HANDLE parent; 1020 1021 #ifdef ACPI_DEBUG 1022 printf("acpi_pci_fixup starts:\n"); 1023 #endif 1024 if (AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent) != AE_OK) 1025 return; 1026 sc->sc_pci_bus = 0; 1027 AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100, 1028 acpi_pci_fixup_bus, sc, NULL); 1029 } 1030 1031 static ACPI_HANDLE 1032 acpi_get_node(char *name) 1033 { 1034 ACPI_NAMESPACE_NODE *ObjDesc; 1035 ACPI_STATUS Status; 1036 1037 Status = AcpiNsGetNodeByPath(name, NULL, 0, &ObjDesc); 1038 if (ACPI_FAILURE (Status)) { 1039 printf("acpi_get_node: could not find: %s\n", 1040 AcpiFormatException (Status)); 1041 return NULL; 1042 } 1043 return ObjDesc; 1044 } 1045 1046 static uint 1047 acpi_get_intr(ACPI_HANDLE handle) 1048 { 1049 ACPI_BUFFER ret; 1050 ACPI_STATUS rv; 1051 ACPI_RESOURCE *res; 1052 ACPI_RESOURCE_IRQ *irq; 1053 uint intr; 1054 1055 intr = -1; 1056 rv = acpi_get(handle, &ret, AcpiGetCurrentResources); 1057 if (ACPI_FAILURE(rv)) 1058 return (intr); 1059 for (res = ret.Pointer; res->Id != ACPI_RSTYPE_END_TAG; 1060 res = ACPI_NEXT_RESOURCE(res)) { 1061 if (res->Id == ACPI_RSTYPE_IRQ) { 1062 irq = (ACPI_RESOURCE_IRQ *)&res->Data; 1063 if (irq->NumberOfInterrupts == 1) 1064 intr = irq->Interrupts[0]; 1065 break; 1066 } 1067 } 1068 free(ret.Pointer, M_ACPI); 1069 return (intr); 1070 } 1071 1072 static void 1073 acpi_pci_set_line(int bus, int dev, int pin, int line) 1074 { 1075 ACPI_STATUS err; 1076 ACPI_PCI_ID pid; 1077 UINT32 intr, id, bhlc; 1078 int func, nfunc; 1079 1080 pid.Bus = bus; 1081 pid.Device = dev; 1082 pid.Function = 0; 1083 1084 err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32); 1085 if (err) 1086 return; 1087 if (PCI_HDRTYPE_MULTIFN(bhlc)) 1088 nfunc = 8; 1089 else 1090 nfunc = 1; 1091 1092 for (func = 0; func < nfunc; func++) { 1093 pid.Function = func; 1094 1095 err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32); 1096 if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID || 1097 PCI_VENDOR(id) == 0) 1098 continue; 1099 1100 err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG, 1101 &intr, 32); 1102 if (err) { 1103 printf("AcpiOsReadPciConfiguration failed %d\n", err); 1104 return; 1105 } 1106 if (pin == PCI_INTERRUPT_PIN(intr) && 1107 line != PCI_INTERRUPT_LINE(intr)) { 1108 #ifdef ACPI_DEBUG 1109 printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n", 1110 bus, dev, func, 1111 pin + '@', PCI_INTERRUPT_LINE(intr), 1112 line); 1113 #endif 1114 intr &= ~(PCI_INTERRUPT_LINE_MASK << 1115 PCI_INTERRUPT_LINE_SHIFT); 1116 intr |= line << PCI_INTERRUPT_LINE_SHIFT; 1117 err = AcpiOsWritePciConfiguration(&pid, 1118 PCI_INTERRUPT_REG, intr, 32); 1119 if (err) { 1120 printf("AcpiOsWritePciConfiguration failed" 1121 " %d\n", err); 1122 return; 1123 } 1124 } 1125 } 1126 } 1127 1128 ACPI_STATUS 1129 acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context, 1130 void **status) 1131 { 1132 struct acpi_softc *sc = context; 1133 ACPI_STATUS rv; 1134 ACPI_BUFFER buf; 1135 UINT8 *Buffer; 1136 ACPI_PCI_ROUTING_TABLE *PrtElement; 1137 ACPI_HANDLE link; 1138 uint line; 1139 ACPI_NAMESPACE_NODE *node; 1140 ACPI_INTEGER val; 1141 1142 rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable); 1143 if (ACPI_FAILURE(rv)) 1144 return (AE_OK); 1145 1146 /* 1147 * If at level 1, this is a PCI root bus. Try the _BBN method 1148 * to get the right PCI bus numbering for the following 1149 * busses (this is a depth-first walk). It may fail, 1150 * for example if there's only one root bus, but that 1151 * case should be ok, so we'll ignore that. 1152 */ 1153 if (level == 1) { 1154 node = AcpiNsMapHandleToNode(handle); 1155 rv = AcpiUtEvaluateNumericObject(METHOD_NAME__BBN, node, &val); 1156 if (!ACPI_FAILURE(rv)) { 1157 #ifdef ACPI_DEBUG 1158 printf("%s: fixup: _BBN success, bus # was %d now %d\n", 1159 sc->sc_dev.dv_xname, sc->sc_pci_bus, 1160 ACPI_LOWORD(val)); 1161 #endif 1162 sc->sc_pci_bus = ACPI_LOWORD(val); 1163 } 1164 } 1165 1166 1167 #ifdef ACPI_DEBUG 1168 printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname, 1169 sc->sc_pci_bus, level); 1170 #endif 1171 1172 for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) { 1173 PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer; 1174 if (PrtElement->Length == 0) 1175 break; 1176 if (PrtElement->Source[0] == 0) 1177 continue; 1178 1179 link = acpi_get_node(PrtElement->Source); 1180 if (link == NULL) 1181 continue; 1182 line = acpi_get_intr(link); 1183 if (line == -1) { 1184 #ifdef ACPI_DEBUG 1185 printf("%s: fixing up intr link %s\n", 1186 sc->sc_dev.dv_xname, PrtElement->Source); 1187 #endif 1188 rv = acpi_allocate_resources(link); 1189 if (ACPI_FAILURE(rv)) { 1190 printf("%s: interrupt allocation failed %s\n", 1191 sc->sc_dev.dv_xname, PrtElement->Source); 1192 continue; 1193 } 1194 line = acpi_get_intr(link); 1195 if (line == -1) { 1196 printf("%s: get intr failed %s\n", 1197 sc->sc_dev.dv_xname, PrtElement->Source); 1198 continue; 1199 } 1200 } 1201 1202 acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16, 1203 PrtElement->Pin + 1, line); 1204 } 1205 1206 sc->sc_pci_bus++; 1207 1208 free(buf.Pointer, M_ACPI); 1209 return (AE_OK); 1210 } 1211 #endif /* ACPI_PCI_FIXUP */ 1212 1213 #if defined(ACPI_PCI_FIXUP) || ACPI_ACTIVATE_DEV 1214 /* XXX This very incomplete */ 1215 ACPI_STATUS 1216 acpi_allocate_resources(ACPI_HANDLE handle) 1217 { 1218 ACPI_BUFFER bufp, bufc, bufn; 1219 ACPI_RESOURCE *resp, *resc, *resn; 1220 ACPI_RESOURCE_IRQ *irq; 1221 ACPI_STATUS rv; 1222 uint delta; 1223 1224 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources); 1225 if (ACPI_FAILURE(rv)) 1226 goto out; 1227 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources); 1228 if (ACPI_FAILURE(rv)) { 1229 goto out1; 1230 } 1231 1232 bufn.Length = 1000; 1233 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK); 1234 resp = bufp.Pointer; 1235 resc = bufc.Pointer; 1236 while (resc->Id != ACPI_RSTYPE_END_TAG && 1237 resp->Id != ACPI_RSTYPE_END_TAG) { 1238 while (resc->Id != resp->Id && resp->Id != ACPI_RSTYPE_END_TAG) 1239 resp = ACPI_NEXT_RESOURCE(resp); 1240 if (resp->Id == ACPI_RSTYPE_END_TAG) 1241 break; 1242 /* Found identical Id */ 1243 resn->Id = resc->Id; 1244 switch (resc->Id) { 1245 case ACPI_RSTYPE_IRQ: 1246 memcpy(&resn->Data, &resp->Data, 1247 sizeof(ACPI_RESOURCE_IRQ)); 1248 irq = (ACPI_RESOURCE_IRQ *)&resn->Data; 1249 irq->Interrupts[0] = 1250 ((ACPI_RESOURCE_IRQ *)&resp->Data)-> 1251 Interrupts[irq->NumberOfInterrupts-1]; 1252 irq->NumberOfInterrupts = 1; 1253 resn->Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ); 1254 break; 1255 case ACPI_RSTYPE_IO: 1256 memcpy(&resn->Data, &resp->Data, 1257 sizeof(ACPI_RESOURCE_IO)); 1258 resn->Length = resp->Length; 1259 break; 1260 default: 1261 printf("acpi_allocate_resources: res=%d\n", resc->Id); 1262 rv = AE_BAD_DATA; 1263 goto out2; 1264 } 1265 resc = ACPI_NEXT_RESOURCE(resc); 1266 resn = ACPI_NEXT_RESOURCE(resn); 1267 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer; 1268 if (delta >= 1269 bufn.Length-ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_DATA)) { 1270 bufn.Length *= 2; 1271 bufn.Pointer = realloc(bufn.Pointer, bufn.Length, 1272 M_ACPI, M_WAITOK); 1273 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta); 1274 } 1275 } 1276 if (resc->Id != ACPI_RSTYPE_END_TAG) { 1277 printf("acpi_allocate_resources: resc not exhausted\n"); 1278 rv = AE_BAD_DATA; 1279 goto out3; 1280 } 1281 1282 resn->Id = ACPI_RSTYPE_END_TAG; 1283 rv = AcpiSetCurrentResources(handle, &bufn); 1284 if (ACPI_FAILURE(rv)) { 1285 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n", 1286 AcpiFormatException(rv)); 1287 } 1288 1289 out3: 1290 free(bufn.Pointer, M_ACPI); 1291 out2: 1292 free(bufc.Pointer, M_ACPI); 1293 out1: 1294 free(bufp.Pointer, M_ACPI); 1295 out: 1296 return rv; 1297 } 1298 #endif /* ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV */ 1299