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