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