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