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