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