1 /*- 2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2000, 2001 Michael Smith 5 * Copyright (c) 2000 BSDi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include "opt_acpi.h" 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/proc.h> 36 #include <sys/malloc.h> 37 #include <sys/bus.h> 38 #include <sys/conf.h> 39 #include <sys/ioccom.h> 40 #include <sys/reboot.h> 41 #include <sys/sysctl.h> 42 #include <sys/ctype.h> 43 #include <sys/linker.h> 44 #include <sys/power.h> 45 46 #include <machine/clock.h> 47 #include <machine/resource.h> 48 49 #include <isa/isavar.h> 50 51 #include "acpi.h" 52 53 #include <dev/acpica/acpica_support.h> 54 55 #include <dev/acpica/acpivar.h> 56 #include <dev/acpica/acpiio.h> 57 58 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 59 60 /* 61 * Hooks for the ACPI CA debugging infrastructure 62 */ 63 #define _COMPONENT ACPI_BUS 64 ACPI_MODULE_NAME("ACPI") 65 66 /* 67 * Character device 68 */ 69 70 static d_open_t acpiopen; 71 static d_close_t acpiclose; 72 static d_ioctl_t acpiioctl; 73 74 #define CDEV_MAJOR 152 75 static struct cdevsw acpi_cdevsw = { 76 acpiopen, 77 acpiclose, 78 noread, 79 nowrite, 80 acpiioctl, 81 nopoll, 82 nommap, 83 nostrategy, 84 "acpi", 85 CDEV_MAJOR, 86 nodump, 87 nopsize, 88 0 89 }; 90 91 static const char* sleep_state_names[] = { 92 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 93 94 /* this has to be static, as the softc is gone when we need it */ 95 static int acpi_off_state = ACPI_STATE_S5; 96 97 #if __FreeBSD_version >= 500000 98 struct mtx acpi_mutex; 99 #endif 100 101 static int acpi_modevent(struct module *mod, int event, void *junk); 102 static void acpi_identify(driver_t *driver, device_t parent); 103 static int acpi_probe(device_t dev); 104 static int acpi_attach(device_t dev); 105 static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 106 static int acpi_print_child(device_t bus, device_t child); 107 static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 108 static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 109 static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 110 u_long count); 111 static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 112 u_long *countp); 113 static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 114 u_long start, u_long end, u_long count, u_int flags); 115 static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 116 static u_int32_t acpi_isa_get_logicalid(device_t dev); 117 static int acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids); 118 119 static void acpi_probe_children(device_t bus); 120 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 121 122 static void acpi_shutdown_pre_sync(void *arg, int howto); 123 static void acpi_shutdown_final(void *arg, int howto); 124 125 static void acpi_enable_fixed_events(struct acpi_softc *sc); 126 127 static void acpi_system_eventhandler_sleep(void *arg, int state); 128 static void acpi_system_eventhandler_wakeup(void *arg, int state); 129 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 130 131 static int acpi_pm_func(u_long cmd, void *arg, ...); 132 133 static device_method_t acpi_methods[] = { 134 /* Device interface */ 135 DEVMETHOD(device_identify, acpi_identify), 136 DEVMETHOD(device_probe, acpi_probe), 137 DEVMETHOD(device_attach, acpi_attach), 138 DEVMETHOD(device_shutdown, bus_generic_shutdown), 139 DEVMETHOD(device_suspend, bus_generic_suspend), 140 DEVMETHOD(device_resume, bus_generic_resume), 141 142 /* Bus interface */ 143 DEVMETHOD(bus_add_child, acpi_add_child), 144 DEVMETHOD(bus_print_child, acpi_print_child), 145 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 146 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 147 DEVMETHOD(bus_set_resource, acpi_set_resource), 148 DEVMETHOD(bus_get_resource, acpi_get_resource), 149 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 150 DEVMETHOD(bus_release_resource, acpi_release_resource), 151 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 152 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 153 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 154 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 155 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 156 157 /* ISA emulation */ 158 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 159 160 {0, 0} 161 }; 162 163 static driver_t acpi_driver = { 164 "acpi", 165 acpi_methods, 166 sizeof(struct acpi_softc), 167 }; 168 169 static devclass_t acpi_devclass; 170 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 171 MODULE_VERSION(acpi, 100); 172 173 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 174 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 175 static int acpi_ca_version = ACPI_CA_VERSION; 176 SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, ""); 177 178 /* 179 * ACPI can only be loaded as a module by the loader; activating it after 180 * system bootstrap time is not useful, and can be fatal to the system. 181 * It also cannot be unloaded, since the entire system bus heirarchy hangs off it. 182 */ 183 static int 184 acpi_modevent(struct module *mod, int event, void *junk) 185 { 186 switch(event) { 187 case MOD_LOAD: 188 if (!cold) { 189 printf("The ACPI driver cannot be loaded after boot.\n"); 190 return(EPERM); 191 } 192 break; 193 case MOD_UNLOAD: 194 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 195 return(EBUSY); 196 break; 197 default: 198 break; 199 } 200 return(0); 201 } 202 203 /* 204 * Detect ACPI, perform early initialisation 205 */ 206 static void 207 acpi_identify(driver_t *driver, device_t parent) 208 { 209 device_t child; 210 int error; 211 #ifdef ACPI_DEBUGGER 212 char *debugpoint; 213 #endif 214 215 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 216 217 if (!cold) 218 return_VOID; 219 220 /* 221 * Check that we haven't been disabled with a hint. 222 */ 223 if (!resource_int_value("acpi", 0, "disabled", &error) && 224 (error != 0)) 225 return_VOID; 226 227 /* 228 * Make sure we're not being doubly invoked. 229 */ 230 if (device_find_child(parent, "acpi", 0) != NULL) 231 return_VOID; 232 233 #if __FreeBSD_version >= 500000 234 /* initialise the ACPI mutex */ 235 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 236 #endif 237 238 /* 239 * Start up the ACPI CA subsystem. 240 */ 241 #ifdef ACPI_DEBUGGER 242 debugpoint = getenv("debug.acpi.debugger"); 243 if (debugpoint) { 244 if (!strcmp(debugpoint, "init")) 245 acpi_EnterDebugger(); 246 freeenv(debugpoint); 247 } 248 #endif 249 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 250 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 251 return_VOID; 252 } 253 #ifdef ACPI_DEBUGGER 254 debugpoint = getenv("debug.acpi.debugger"); 255 if (debugpoint) { 256 if (!strcmp(debugpoint, "tables")) 257 acpi_EnterDebugger(); 258 freeenv(debugpoint); 259 } 260 #endif 261 262 if (ACPI_FAILURE(error = AcpiLoadTables())) { 263 printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 264 return_VOID; 265 } 266 267 /* 268 * Attach the actual ACPI device. 269 */ 270 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 271 device_printf(parent, "ACPI: could not attach\n"); 272 return_VOID; 273 } 274 } 275 276 /* 277 * Fetch some descriptive data from ACPI to put in our attach message 278 */ 279 static int 280 acpi_probe(device_t dev) 281 { 282 ACPI_TABLE_HEADER th; 283 char buf[20]; 284 ACPI_STATUS status; 285 int error; 286 ACPI_LOCK_DECL; 287 288 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 289 290 if (power_pm_get_type() != POWER_PM_TYPE_NONE && 291 power_pm_get_type() != POWER_PM_TYPE_ACPI) { 292 device_printf(dev, "Other PM system enabled.\n"); 293 return_VALUE(ENXIO); 294 } 295 296 ACPI_LOCK; 297 298 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 299 device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status)); 300 error = ENXIO; 301 } else { 302 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 303 device_set_desc_copy(dev, buf); 304 error = 0; 305 } 306 ACPI_UNLOCK; 307 return_VALUE(error); 308 } 309 310 static int 311 acpi_attach(device_t dev) 312 { 313 struct acpi_softc *sc; 314 ACPI_STATUS status; 315 int error; 316 UINT32 flags; 317 char *env; 318 #ifdef ACPI_DEBUGGER 319 char *debugpoint; 320 #endif 321 ACPI_LOCK_DECL; 322 323 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 324 ACPI_LOCK; 325 sc = device_get_softc(dev); 326 bzero(sc, sizeof(*sc)); 327 sc->acpi_dev = dev; 328 329 #ifdef ACPI_DEBUGGER 330 debugpoint = getenv("debug.acpi.debugger"); 331 if (debugpoint) { 332 if (!strcmp(debugpoint, "spaces")) 333 acpi_EnterDebugger(); 334 freeenv(debugpoint); 335 } 336 #endif 337 338 /* 339 * Install the default address space handlers. 340 */ 341 error = ENXIO; 342 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 343 ACPI_ADR_SPACE_SYSTEM_MEMORY, 344 ACPI_DEFAULT_HANDLER, 345 NULL, NULL))) { 346 device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status)); 347 goto out; 348 } 349 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 350 ACPI_ADR_SPACE_SYSTEM_IO, 351 ACPI_DEFAULT_HANDLER, 352 NULL, NULL))) { 353 device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status)); 354 goto out; 355 } 356 if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 357 ACPI_ADR_SPACE_PCI_CONFIG, 358 ACPI_DEFAULT_HANDLER, 359 NULL, NULL))) { 360 device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status)); 361 goto out; 362 } 363 364 /* 365 * Bring ACPI fully online. 366 * 367 * Note that some systems (specifically, those with namespace evaluation issues 368 * that require the avoidance of parts of the namespace) must avoid running _INI 369 * and _STA on everything, as well as dodging the final object init pass. 370 * 371 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 372 * 373 * XXX We should arrange for the object init pass after we have attached all our 374 * child devices, but on many systems it works here. 375 */ 376 #ifdef ACPI_DEBUGGER 377 debugpoint = getenv("debug.acpi.debugger"); 378 if (debugpoint) { 379 if (!strcmp(debugpoint, "enable")) 380 acpi_EnterDebugger(); 381 freeenv(debugpoint); 382 } 383 #endif 384 flags = 0; 385 if (testenv("debug.acpi.avoid")) 386 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 387 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 388 device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status)); 389 goto out; 390 } 391 392 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 393 device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status)); 394 goto out; 395 } 396 397 /* 398 * Setup our sysctl tree. 399 * 400 * XXX: This doesn't check to make sure that none of these fail. 401 */ 402 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 403 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 404 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 405 device_get_name(dev), CTLFLAG_RD, 0, ""); 406 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 407 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 408 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 409 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 410 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 411 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 412 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 413 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 414 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 415 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 416 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 417 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 418 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 419 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 420 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 421 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 422 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 423 &sc->acpi_sleep_delay, 0, "sleep delay"); 424 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 425 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 426 &sc->acpi_s4bios, 0, "S4BIOS mode"); 427 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 428 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 429 &sc->acpi_verbose, 0, "verbose mode"); 430 sc->acpi_sleep_delay = 0; 431 sc->acpi_s4bios = 1; 432 if (bootverbose) 433 sc->acpi_verbose = 1; 434 if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 435 sc->acpi_verbose = 1; 436 freeenv(env); 437 } 438 439 /* 440 * Dispatch the default sleep state to devices. 441 * TBD: should be configured from userland policy manager. 442 */ 443 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 444 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 445 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 446 sc->acpi_standby_sx = ACPI_STATE_S1; 447 sc->acpi_suspend_sx = ACPI_STATE_S3; 448 449 acpi_enable_fixed_events(sc); 450 451 /* 452 * Scan the namespace and attach/initialise children. 453 */ 454 #ifdef ACPI_DEBUGGER 455 debugpoint = getenv("debug.acpi.debugger"); 456 if (debugpoint) { 457 if (!strcmp(debugpoint, "probe")) 458 acpi_EnterDebugger(); 459 freeenv(debugpoint); 460 } 461 #endif 462 463 /* 464 * Register our shutdown handlers 465 */ 466 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 467 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 468 469 /* 470 * Register our acpi event handlers. 471 * XXX should be configurable eg. via userland policy manager. 472 */ 473 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 474 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 475 476 /* 477 * Flag our initial states. 478 */ 479 sc->acpi_enabled = 1; 480 sc->acpi_sstate = ACPI_STATE_S0; 481 sc->acpi_sleep_disabled = 0; 482 483 /* 484 * Create the control device 485 */ 486 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi"); 487 sc->acpi_dev_t->si_drv1 = sc; 488 489 #ifdef ACPI_DEBUGGER 490 debugpoint = getenv("debug.acpi.debugger"); 491 if (debugpoint) { 492 if (!strcmp(debugpoint, "running")) 493 acpi_EnterDebugger(); 494 freeenv(debugpoint); 495 } 496 #endif 497 498 #ifdef ACPI_USE_THREADS 499 if ((error = acpi_task_thread_init())) { 500 goto out; 501 } 502 #endif 503 504 if ((error = acpi_machdep_init(dev))) { 505 goto out; 506 } 507 508 /* Register ACPI again to pass the correct argument of pm_func. */ 509 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 510 511 if (!acpi_disabled("bus")) 512 acpi_probe_children(dev); 513 514 error = 0; 515 516 out: 517 ACPI_UNLOCK; 518 return_VALUE(error); 519 } 520 521 /* 522 * Handle a new device being added 523 */ 524 static device_t 525 acpi_add_child(device_t bus, int order, const char *name, int unit) 526 { 527 struct acpi_device *ad; 528 device_t child; 529 530 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 531 return(NULL); 532 bzero(ad, sizeof(*ad)); 533 534 resource_list_init(&ad->ad_rl); 535 536 child = device_add_child_ordered(bus, order, name, unit); 537 if (child != NULL) 538 device_set_ivars(child, ad); 539 return(child); 540 } 541 542 static int 543 acpi_print_child(device_t bus, device_t child) 544 { 545 struct acpi_device *adev = device_get_ivars(child); 546 struct resource_list *rl = &adev->ad_rl; 547 int retval = 0; 548 549 retval += bus_print_child_header(bus, child); 550 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 551 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 552 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 553 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 554 retval += bus_print_child_footer(bus, child); 555 556 return(retval); 557 } 558 559 560 /* 561 * Handle per-device ivars 562 */ 563 static int 564 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 565 { 566 struct acpi_device *ad; 567 568 if ((ad = device_get_ivars(child)) == NULL) { 569 printf("device has no ivars\n"); 570 return(ENOENT); 571 } 572 573 switch(index) { 574 /* ACPI ivars */ 575 case ACPI_IVAR_HANDLE: 576 *(ACPI_HANDLE *)result = ad->ad_handle; 577 break; 578 case ACPI_IVAR_MAGIC: 579 *(int *)result = ad->ad_magic; 580 break; 581 case ACPI_IVAR_PRIVATE: 582 *(void **)result = ad->ad_private; 583 break; 584 585 /* ISA compatibility */ 586 case ISA_IVAR_VENDORID: 587 case ISA_IVAR_SERIAL: 588 case ISA_IVAR_COMPATID: 589 *(int *)result = -1; 590 break; 591 592 case ISA_IVAR_LOGICALID: 593 *(int *)result = acpi_isa_get_logicalid(child); 594 break; 595 596 default: 597 return(ENOENT); 598 } 599 return(0); 600 } 601 602 static int 603 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 604 { 605 struct acpi_device *ad; 606 607 if ((ad = device_get_ivars(child)) == NULL) { 608 printf("device has no ivars\n"); 609 return(ENOENT); 610 } 611 612 switch(index) { 613 /* ACPI ivars */ 614 case ACPI_IVAR_HANDLE: 615 ad->ad_handle = (ACPI_HANDLE)value; 616 break; 617 case ACPI_IVAR_MAGIC: 618 ad->ad_magic = (int )value; 619 break; 620 case ACPI_IVAR_PRIVATE: 621 ad->ad_private = (void *)value; 622 break; 623 624 default: 625 panic("bad ivar write request (%d)", index); 626 return(ENOENT); 627 } 628 return(0); 629 } 630 631 /* 632 * Handle child resource allocation/removal 633 */ 634 static int 635 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 636 { 637 struct acpi_device *ad = device_get_ivars(child); 638 struct resource_list *rl = &ad->ad_rl; 639 640 resource_list_add(rl, type, rid, start, start + count -1, count); 641 642 return(0); 643 } 644 645 static int 646 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 647 { 648 struct acpi_device *ad = device_get_ivars(child); 649 struct resource_list *rl = &ad->ad_rl; 650 struct resource_list_entry *rle; 651 652 rle = resource_list_find(rl, type, rid); 653 if (!rle) 654 return(ENOENT); 655 656 if (startp) 657 *startp = rle->start; 658 if (countp) 659 *countp = rle->count; 660 661 return(0); 662 } 663 664 static struct resource * 665 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 666 u_long start, u_long end, u_long count, u_int flags) 667 { 668 struct acpi_device *ad = device_get_ivars(child); 669 struct resource_list *rl = &ad->ad_rl; 670 671 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 672 } 673 674 static int 675 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 676 { 677 struct acpi_device *ad = device_get_ivars(child); 678 struct resource_list *rl = &ad->ad_rl; 679 680 return(resource_list_release(rl, bus, child, type, rid, r)); 681 } 682 683 /* 684 * Handle ISA-like devices probing for a PnP ID to match. 685 */ 686 #define PNP_EISAID(s) \ 687 ((((s[0] - '@') & 0x1f) << 2) \ 688 | (((s[1] - '@') & 0x18) >> 3) \ 689 | (((s[1] - '@') & 0x07) << 13) \ 690 | (((s[2] - '@') & 0x1f) << 8) \ 691 | (PNP_HEXTONUM(s[4]) << 16) \ 692 | (PNP_HEXTONUM(s[3]) << 20) \ 693 | (PNP_HEXTONUM(s[6]) << 24) \ 694 | (PNP_HEXTONUM(s[5]) << 28)) 695 696 static u_int32_t 697 acpi_isa_get_logicalid(device_t dev) 698 { 699 ACPI_HANDLE h; 700 ACPI_DEVICE_INFO devinfo; 701 ACPI_STATUS error; 702 u_int32_t pnpid; 703 ACPI_LOCK_DECL; 704 705 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 706 707 pnpid = 0; 708 ACPI_LOCK; 709 710 /* fetch and validate the HID */ 711 if ((h = acpi_get_handle(dev)) == NULL) 712 goto out; 713 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 714 goto out; 715 if (!(devinfo.Valid & ACPI_VALID_HID)) 716 goto out; 717 718 pnpid = PNP_EISAID(devinfo.HardwareId); 719 out: 720 ACPI_UNLOCK; 721 return_VALUE(pnpid); 722 } 723 724 static int 725 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 726 { 727 int result; 728 u_int32_t pnpid; 729 730 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 731 732 /* 733 * ISA-style drivers attached to ACPI may persist and 734 * probe manually if we return ENOENT. We never want 735 * that to happen, so don't ever return it. 736 */ 737 result = ENXIO; 738 739 /* scan the supplied IDs for a match */ 740 pnpid = acpi_isa_get_logicalid(child); 741 while (ids && ids->ip_id) { 742 if (pnpid == ids->ip_id) { 743 result = 0; 744 goto out; 745 } 746 ids++; 747 } 748 out: 749 return_VALUE(result); 750 } 751 752 /* 753 * Scan relevant portions of the ACPI namespace and attach child devices. 754 * 755 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 756 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 757 */ 758 static void 759 acpi_probe_children(device_t bus) 760 { 761 ACPI_HANDLE parent; 762 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 763 int i; 764 765 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 766 ACPI_ASSERTLOCK; 767 768 /* 769 * Create any static children by calling device identify methods. 770 */ 771 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 772 bus_generic_probe(bus); 773 774 /* 775 * Scan the namespace and insert placeholders for all the devices that 776 * we find. 777 * 778 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 779 * we want to create nodes for all devices, not just those that are currently 780 * present. (This assumes that we don't want to create/remove devices as they 781 * appear, which might be smarter.) 782 */ 783 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 784 for (i = 0; scopes[i] != NULL; i++) 785 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent))) 786 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 787 788 /* 789 * Scan all of the child devices we have created and let them probe/attach. 790 */ 791 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 792 bus_generic_attach(bus); 793 794 /* 795 * Some of these children may have attached others as part of their attach 796 * process (eg. the root PCI bus driver), so rescan. 797 */ 798 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 799 bus_generic_attach(bus); 800 801 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 802 return_VOID; 803 } 804 805 /* 806 * Evaluate a child device and determine whether we might attach a device to 807 * it. 808 */ 809 static ACPI_STATUS 810 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 811 { 812 ACPI_OBJECT_TYPE type; 813 device_t child, bus = (device_t)context; 814 815 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 816 817 /* 818 * Skip this device if we think we'll have trouble with it. 819 */ 820 if (acpi_avoid(handle)) 821 return_ACPI_STATUS(AE_OK); 822 823 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 824 switch(type) { 825 case ACPI_TYPE_DEVICE: 826 case ACPI_TYPE_PROCESSOR: 827 case ACPI_TYPE_THERMAL: 828 case ACPI_TYPE_POWER: 829 if (acpi_disabled("children")) 830 break; 831 /* 832 * Create a placeholder device for this node. Sort the placeholder 833 * so that the probe/attach passes will run breadth-first. 834 */ 835 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle))); 836 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 837 if (child == NULL) 838 break; 839 acpi_set_handle(child, handle); 840 841 /* 842 * Check that the device is present. If it's not present, 843 * leave it disabled (so that we have a device_t attached to 844 * the handle, but we don't probe it). 845 */ 846 if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) { 847 device_disable(child); 848 break; 849 } 850 851 /* 852 * Get the device's resource settings and attach them. 853 * Note that if the device has _PRS but no _CRS, we need 854 * to decide when it's appropriate to try to configure the 855 * device. Ignore the return value here; it's OK for the 856 * device not to have any resources. 857 */ 858 acpi_parse_resources(child, handle, &acpi_res_parse_set); 859 860 /* if we're debugging, probe/attach now rather than later */ 861 ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 862 break; 863 } 864 } 865 return_ACPI_STATUS(AE_OK); 866 } 867 868 static void 869 acpi_shutdown_pre_sync(void *arg, int howto) 870 { 871 872 ACPI_ASSERTLOCK; 873 874 /* 875 * Disable all ACPI events before soft off, otherwise the system 876 * will be turned on again on some laptops. 877 * 878 * XXX this should probably be restricted to masking some events just 879 * before powering down, since we may still need ACPI during the 880 * shutdown process. 881 */ 882 acpi_Disable((struct acpi_softc *)arg); 883 } 884 885 static void 886 acpi_shutdown_final(void *arg, int howto) 887 { 888 ACPI_STATUS status; 889 890 ACPI_ASSERTLOCK; 891 892 if (howto & RB_POWEROFF) { 893 printf("Power system off using ACPI...\n"); 894 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) { 895 printf("AcpiEnterSleepStatePrep failed - %s\n", 896 AcpiFormatException(status)); 897 return; 898 } 899 if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) { 900 printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 901 } else { 902 DELAY(1000000); 903 printf("ACPI power-off failed - timeout\n"); 904 } 905 } else { 906 printf("Terminate ACPI\n"); 907 AcpiTerminate(); 908 } 909 } 910 911 static void 912 acpi_enable_fixed_events(struct acpi_softc *sc) 913 { 914 static int first_time = 1; 915 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 916 917 ACPI_ASSERTLOCK; 918 919 /* Enable and clear fixed events and install handlers. */ 920 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 921 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0); 922 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 923 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 924 acpi_eventhandler_power_button_for_sleep, sc); 925 if (first_time) { 926 device_printf(sc->acpi_dev, MSGFORMAT, "power"); 927 } 928 } 929 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 930 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0); 931 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 932 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 933 acpi_eventhandler_sleep_button_for_sleep, sc); 934 if (first_time) { 935 device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 936 } 937 } 938 939 first_time = 0; 940 } 941 942 /* 943 * Returns true if the device is actually present and should 944 * be attached to. This requires the present, enabled, UI-visible 945 * and diagnostics-passed bits to be set. 946 */ 947 BOOLEAN 948 acpi_DeviceIsPresent(device_t dev) 949 { 950 ACPI_HANDLE h; 951 ACPI_DEVICE_INFO devinfo; 952 ACPI_STATUS error; 953 954 ACPI_ASSERTLOCK; 955 956 if ((h = acpi_get_handle(dev)) == NULL) 957 return(FALSE); 958 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 959 return(FALSE); 960 /* if no _STA method, must be present */ 961 if (!(devinfo.Valid & ACPI_VALID_STA)) 962 return(TRUE); 963 /* return true for 'present' and 'functioning' */ 964 if ((devinfo.CurrentStatus & 0x9) == 0x9) 965 return(TRUE); 966 return(FALSE); 967 } 968 969 /* 970 * Returns true if the battery is actually present and inserted. 971 */ 972 BOOLEAN 973 acpi_BatteryIsPresent(device_t dev) 974 { 975 ACPI_HANDLE h; 976 ACPI_DEVICE_INFO devinfo; 977 ACPI_STATUS error; 978 979 ACPI_ASSERTLOCK; 980 981 if ((h = acpi_get_handle(dev)) == NULL) 982 return(FALSE); 983 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 984 return(FALSE); 985 /* if no _STA method, must be present */ 986 if (!(devinfo.Valid & ACPI_VALID_STA)) 987 return(TRUE); 988 /* return true for 'present' and 'functioning' */ 989 if ((devinfo.CurrentStatus & 0x19) == 0x19) 990 return(TRUE); 991 return(FALSE); 992 } 993 994 /* 995 * Match a HID string against a device 996 */ 997 BOOLEAN 998 acpi_MatchHid(device_t dev, char *hid) 999 { 1000 ACPI_HANDLE h; 1001 ACPI_DEVICE_INFO devinfo; 1002 ACPI_STATUS error; 1003 int cid; 1004 1005 ACPI_ASSERTLOCK; 1006 1007 if (hid == NULL) 1008 return(FALSE); 1009 if ((h = acpi_get_handle(dev)) == NULL) 1010 return(FALSE); 1011 if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 1012 return(FALSE); 1013 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 1014 return(TRUE); 1015 if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1016 return(FALSE); 1017 if (cid == PNP_EISAID(hid)) 1018 return(TRUE); 1019 return(FALSE); 1020 } 1021 1022 /* 1023 * Return the handle of a named object within our scope, ie. that of (parent) 1024 * or one if its parents. 1025 */ 1026 ACPI_STATUS 1027 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1028 { 1029 ACPI_HANDLE r; 1030 ACPI_STATUS status; 1031 1032 ACPI_ASSERTLOCK; 1033 1034 /* walk back up the tree to the root */ 1035 for (;;) { 1036 if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) { 1037 *result = r; 1038 return(AE_OK); 1039 } 1040 if (status != AE_NOT_FOUND) 1041 return(AE_OK); 1042 if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1043 return(AE_NOT_FOUND); 1044 parent = r; 1045 } 1046 } 1047 1048 /* 1049 * Allocate a buffer with a preset data size. 1050 */ 1051 ACPI_BUFFER * 1052 acpi_AllocBuffer(int size) 1053 { 1054 ACPI_BUFFER *buf; 1055 1056 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1057 return(NULL); 1058 buf->Length = size; 1059 buf->Pointer = (void *)(buf + 1); 1060 return(buf); 1061 } 1062 1063 /* 1064 * Evaluate a path that should return an integer. 1065 */ 1066 ACPI_STATUS 1067 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1068 { 1069 ACPI_STATUS error; 1070 ACPI_BUFFER buf; 1071 ACPI_OBJECT param; 1072 1073 ACPI_ASSERTLOCK; 1074 1075 if (handle == NULL) 1076 handle = ACPI_ROOT_OBJECT; 1077 1078 /* 1079 * Assume that what we've been pointed at is an Integer object, or 1080 * a method that will return an Integer. 1081 */ 1082 buf.Pointer = ¶m; 1083 buf.Length = sizeof(param); 1084 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1085 if (param.Type == ACPI_TYPE_INTEGER) { 1086 *number = param.Integer.Value; 1087 } else { 1088 error = AE_TYPE; 1089 } 1090 } 1091 1092 /* 1093 * In some applications, a method that's expected to return an Integer 1094 * may instead return a Buffer (probably to simplify some internal 1095 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 1096 * convert it into an Integer as best we can. 1097 * 1098 * This is a hack. 1099 */ 1100 if (error == AE_BUFFER_OVERFLOW) { 1101 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1102 error = AE_NO_MEMORY; 1103 } else { 1104 if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1105 error = acpi_ConvertBufferToInteger(&buf, number); 1106 } 1107 } 1108 AcpiOsFree(buf.Pointer); 1109 } 1110 return(error); 1111 } 1112 1113 ACPI_STATUS 1114 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1115 { 1116 ACPI_OBJECT *p; 1117 int i; 1118 1119 p = (ACPI_OBJECT *)bufp->Pointer; 1120 if (p->Type == ACPI_TYPE_INTEGER) { 1121 *number = p->Integer.Value; 1122 return(AE_OK); 1123 } 1124 if (p->Type != ACPI_TYPE_BUFFER) 1125 return(AE_TYPE); 1126 if (p->Buffer.Length > sizeof(int)) 1127 return(AE_BAD_DATA); 1128 *number = 0; 1129 for (i = 0; i < p->Buffer.Length; i++) 1130 *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1131 return(AE_OK); 1132 } 1133 1134 /* 1135 * Iterate over the elements of an a package object, calling the supplied 1136 * function for each element. 1137 * 1138 * XXX possible enhancement might be to abort traversal on error. 1139 */ 1140 ACPI_STATUS 1141 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 1142 { 1143 ACPI_OBJECT *comp; 1144 int i; 1145 1146 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 1147 return(AE_BAD_PARAMETER); 1148 1149 /* iterate over components */ 1150 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 1151 func(comp, arg); 1152 1153 return(AE_OK); 1154 } 1155 1156 /* 1157 * Find the (index)th resource object in a set. 1158 */ 1159 ACPI_STATUS 1160 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 1161 { 1162 ACPI_RESOURCE *rp; 1163 int i; 1164 1165 rp = (ACPI_RESOURCE *)buf->Pointer; 1166 i = index; 1167 while (i-- > 0) { 1168 /* range check */ 1169 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1170 return(AE_BAD_PARAMETER); 1171 /* check for terminator */ 1172 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1173 (rp->Length == 0)) 1174 return(AE_NOT_FOUND); 1175 rp = ACPI_RESOURCE_NEXT(rp); 1176 } 1177 if (resp != NULL) 1178 *resp = rp; 1179 return(AE_OK); 1180 } 1181 1182 /* 1183 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 1184 * 1185 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 1186 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 1187 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 1188 * resources. 1189 */ 1190 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 1191 1192 ACPI_STATUS 1193 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 1194 { 1195 ACPI_RESOURCE *rp; 1196 void *newp; 1197 1198 /* 1199 * Initialise the buffer if necessary. 1200 */ 1201 if (buf->Pointer == NULL) { 1202 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 1203 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 1204 return(AE_NO_MEMORY); 1205 rp = (ACPI_RESOURCE *)buf->Pointer; 1206 rp->Id = ACPI_RSTYPE_END_TAG; 1207 rp->Length = 0; 1208 } 1209 if (res == NULL) 1210 return(AE_OK); 1211 1212 /* 1213 * Scan the current buffer looking for the terminator. 1214 * This will either find the terminator or hit the end 1215 * of the buffer and return an error. 1216 */ 1217 rp = (ACPI_RESOURCE *)buf->Pointer; 1218 for (;;) { 1219 /* range check, don't go outside the buffer */ 1220 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1221 return(AE_BAD_PARAMETER); 1222 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1223 (rp->Length == 0)) { 1224 break; 1225 } 1226 rp = ACPI_RESOURCE_NEXT(rp); 1227 } 1228 1229 /* 1230 * Check the size of the buffer and expand if required. 1231 * 1232 * Required size is: 1233 * size of existing resources before terminator + 1234 * size of new resource and header + 1235 * size of terminator. 1236 * 1237 * Note that this loop should really only run once, unless 1238 * for some reason we are stuffing a *really* huge resource. 1239 */ 1240 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 1241 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 1242 ACPI_RESOURCE_LENGTH) >= buf->Length) { 1243 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 1244 return(AE_NO_MEMORY); 1245 bcopy(buf->Pointer, newp, buf->Length); 1246 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1247 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 1248 AcpiOsFree(buf->Pointer); 1249 buf->Pointer = newp; 1250 buf->Length += buf->Length; 1251 } 1252 1253 /* 1254 * Insert the new resource. 1255 */ 1256 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 1257 1258 /* 1259 * And add the terminator. 1260 */ 1261 rp = ACPI_RESOURCE_NEXT(rp); 1262 rp->Id = ACPI_RSTYPE_END_TAG; 1263 rp->Length = 0; 1264 1265 return(AE_OK); 1266 } 1267 1268 /* 1269 * Set interrupt model. 1270 */ 1271 ACPI_STATUS 1272 acpi_SetIntrModel(int model) 1273 { 1274 ACPI_OBJECT_LIST ArgList; 1275 ACPI_OBJECT Arg; 1276 1277 Arg.Type = ACPI_TYPE_INTEGER; 1278 Arg.Integer.Value = model; 1279 ArgList.Count = 1; 1280 ArgList.Pointer = &Arg; 1281 return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1282 } 1283 1284 #define ACPI_MINIMUM_AWAKETIME 5 1285 1286 static void 1287 acpi_sleep_enable(void *arg) 1288 { 1289 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1290 } 1291 1292 /* 1293 * Set the system sleep state 1294 * 1295 * Currently we only support S1 and S5 1296 */ 1297 ACPI_STATUS 1298 acpi_SetSleepState(struct acpi_softc *sc, int state) 1299 { 1300 ACPI_STATUS status = AE_OK; 1301 UINT8 TypeA; 1302 UINT8 TypeB; 1303 1304 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1305 ACPI_ASSERTLOCK; 1306 1307 if (sc->acpi_sstate != ACPI_STATE_S0) 1308 return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */ 1309 1310 if (sc->acpi_sleep_disabled) 1311 return_ACPI_STATUS(AE_OK); 1312 1313 switch (state) { 1314 case ACPI_STATE_S0: /* XXX only for testing */ 1315 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1316 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1317 } 1318 break; 1319 1320 case ACPI_STATE_S1: 1321 case ACPI_STATE_S2: 1322 case ACPI_STATE_S3: 1323 case ACPI_STATE_S4: 1324 if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) { 1325 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status)); 1326 break; 1327 } 1328 1329 sc->acpi_sstate = state; 1330 sc->acpi_sleep_disabled = 1; 1331 1332 /* 1333 * Inform all devices that we are going to sleep. 1334 */ 1335 if (DEVICE_SUSPEND(root_bus) != 0) { 1336 /* 1337 * Re-wake the system. 1338 * 1339 * XXX note that a better two-pass approach with a 'veto' pass 1340 * followed by a "real thing" pass would be better, but the 1341 * current bus interface does not provide for this. 1342 */ 1343 DEVICE_RESUME(root_bus); 1344 return_ACPI_STATUS(AE_ERROR); 1345 } 1346 1347 if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) { 1348 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1349 AcpiFormatException(status)); 1350 break; 1351 } 1352 1353 if (sc->acpi_sleep_delay > 0) { 1354 DELAY(sc->acpi_sleep_delay * 1000000); 1355 } 1356 1357 if (state != ACPI_STATE_S1) { 1358 acpi_sleep_machdep(sc, state); 1359 1360 /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */ 1361 if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) { 1362 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1363 } 1364 1365 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1366 if (state == ACPI_STATE_S4) { 1367 AcpiEnable(); 1368 } 1369 } else { 1370 if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1371 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1372 break; 1373 } 1374 } 1375 AcpiLeaveSleepState((UINT8)state); 1376 DEVICE_RESUME(root_bus); 1377 sc->acpi_sstate = ACPI_STATE_S0; 1378 acpi_enable_fixed_events(sc); 1379 break; 1380 1381 case ACPI_STATE_S5: 1382 /* 1383 * Shut down cleanly and power off. This will call us back through the 1384 * shutdown handlers. 1385 */ 1386 shutdown_nice(RB_POWEROFF); 1387 break; 1388 1389 default: 1390 status = AE_BAD_PARAMETER; 1391 break; 1392 } 1393 1394 if (sc->acpi_sleep_disabled) 1395 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1396 1397 return_ACPI_STATUS(status); 1398 } 1399 1400 /* 1401 * Enable/Disable ACPI 1402 */ 1403 ACPI_STATUS 1404 acpi_Enable(struct acpi_softc *sc) 1405 { 1406 ACPI_STATUS status; 1407 u_int32_t flags; 1408 1409 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1410 ACPI_ASSERTLOCK; 1411 1412 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 1413 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1414 if (!sc->acpi_enabled) { 1415 status = AcpiEnableSubsystem(flags); 1416 } else { 1417 status = AE_OK; 1418 } 1419 if (status == AE_OK) 1420 sc->acpi_enabled = 1; 1421 return_ACPI_STATUS(status); 1422 } 1423 1424 ACPI_STATUS 1425 acpi_Disable(struct acpi_softc *sc) 1426 { 1427 ACPI_STATUS status; 1428 1429 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1430 ACPI_ASSERTLOCK; 1431 1432 if (sc->acpi_enabled) { 1433 status = AcpiDisable(); 1434 } else { 1435 status = AE_OK; 1436 } 1437 if (status == AE_OK) 1438 sc->acpi_enabled = 0; 1439 return_ACPI_STATUS(status); 1440 } 1441 1442 /* 1443 * ACPI Event Handlers 1444 */ 1445 1446 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 1447 1448 static void 1449 acpi_system_eventhandler_sleep(void *arg, int state) 1450 { 1451 ACPI_LOCK_DECL; 1452 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1453 1454 ACPI_LOCK; 1455 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 1456 acpi_SetSleepState((struct acpi_softc *)arg, state); 1457 ACPI_UNLOCK; 1458 return_VOID; 1459 } 1460 1461 static void 1462 acpi_system_eventhandler_wakeup(void *arg, int state) 1463 { 1464 ACPI_LOCK_DECL; 1465 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1466 1467 /* Well, what to do? :-) */ 1468 1469 ACPI_LOCK; 1470 ACPI_UNLOCK; 1471 1472 return_VOID; 1473 } 1474 1475 /* 1476 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 1477 */ 1478 UINT32 1479 acpi_eventhandler_power_button_for_sleep(void *context) 1480 { 1481 struct acpi_softc *sc = (struct acpi_softc *)context; 1482 1483 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1484 1485 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 1486 1487 return_VALUE(ACPI_INTERRUPT_HANDLED); 1488 } 1489 1490 UINT32 1491 acpi_eventhandler_power_button_for_wakeup(void *context) 1492 { 1493 struct acpi_softc *sc = (struct acpi_softc *)context; 1494 1495 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1496 1497 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 1498 1499 return_VALUE(ACPI_INTERRUPT_HANDLED); 1500 } 1501 1502 UINT32 1503 acpi_eventhandler_sleep_button_for_sleep(void *context) 1504 { 1505 struct acpi_softc *sc = (struct acpi_softc *)context; 1506 1507 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1508 1509 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 1510 1511 return_VALUE(ACPI_INTERRUPT_HANDLED); 1512 } 1513 1514 UINT32 1515 acpi_eventhandler_sleep_button_for_wakeup(void *context) 1516 { 1517 struct acpi_softc *sc = (struct acpi_softc *)context; 1518 1519 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1520 1521 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 1522 1523 return_VALUE(ACPI_INTERRUPT_HANDLED); 1524 } 1525 1526 /* 1527 * XXX This is kinda ugly, and should not be here. 1528 */ 1529 struct acpi_staticbuf { 1530 ACPI_BUFFER buffer; 1531 char data[512]; 1532 }; 1533 1534 char * 1535 acpi_name(ACPI_HANDLE handle) 1536 { 1537 static struct acpi_staticbuf buf; 1538 1539 ACPI_ASSERTLOCK; 1540 1541 buf.buffer.Length = 512; 1542 buf.buffer.Pointer = &buf.data[0]; 1543 1544 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 1545 return(buf.buffer.Pointer); 1546 return("(unknown path)"); 1547 } 1548 1549 /* 1550 * Debugging/bug-avoidance. Avoid trying to fetch info on various 1551 * parts of the namespace. 1552 */ 1553 int 1554 acpi_avoid(ACPI_HANDLE handle) 1555 { 1556 char *cp, *env, *np; 1557 int len; 1558 1559 np = acpi_name(handle); 1560 if (*np == '\\') 1561 np++; 1562 if ((env = getenv("debug.acpi.avoid")) == NULL) 1563 return(0); 1564 1565 /* scan the avoid list checking for a match */ 1566 cp = env; 1567 for (;;) { 1568 while ((*cp != 0) && isspace(*cp)) 1569 cp++; 1570 if (*cp == 0) 1571 break; 1572 len = 0; 1573 while ((cp[len] != 0) && !isspace(cp[len])) 1574 len++; 1575 if (!strncmp(cp, np, len)) { 1576 freeenv(env); 1577 return(1); 1578 } 1579 cp += len; 1580 } 1581 freeenv(env); 1582 return(0); 1583 } 1584 1585 /* 1586 * Debugging/bug-avoidance. Disable ACPI subsystem components. 1587 */ 1588 int 1589 acpi_disabled(char *subsys) 1590 { 1591 char *cp, *env; 1592 int len; 1593 1594 if ((env = getenv("debug.acpi.disable")) == NULL) 1595 return(0); 1596 if (!strcmp(env, "all")) { 1597 freeenv(env); 1598 return(1); 1599 } 1600 1601 /* scan the disable list checking for a match */ 1602 cp = env; 1603 for (;;) { 1604 while ((*cp != 0) && isspace(*cp)) 1605 cp++; 1606 if (*cp == 0) 1607 break; 1608 len = 0; 1609 while ((cp[len] != 0) && !isspace(cp[len])) 1610 len++; 1611 if (!strncmp(cp, subsys, len)) { 1612 freeenv(env); 1613 return(1); 1614 } 1615 cp += len; 1616 } 1617 freeenv(env); 1618 return(0); 1619 } 1620 1621 /* 1622 * Device wake capability enable/disable. 1623 */ 1624 void 1625 acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1626 { 1627 ACPI_OBJECT_LIST ArgList; 1628 ACPI_OBJECT Arg; 1629 1630 /* 1631 * TBD: All Power Resources referenced by elements 2 through N 1632 * of the _PRW object are put into the ON state. 1633 */ 1634 1635 /* 1636 * enable/disable device wake function. 1637 */ 1638 1639 ArgList.Count = 1; 1640 ArgList.Pointer = &Arg; 1641 1642 Arg.Type = ACPI_TYPE_INTEGER; 1643 Arg.Integer.Value = enable; 1644 1645 (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1646 } 1647 1648 void 1649 acpi_device_enable_wake_event(ACPI_HANDLE h) 1650 { 1651 struct acpi_softc *sc; 1652 ACPI_STATUS status; 1653 ACPI_BUFFER prw_buffer; 1654 ACPI_OBJECT *res; 1655 1656 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1657 1658 if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) { 1659 return; 1660 } 1661 1662 /* 1663 * _PRW object is only required for devices that have the ability 1664 * to wake the system from a system sleeping state. 1665 */ 1666 prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1667 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1668 if (ACPI_FAILURE(status)) { 1669 return; 1670 } 1671 1672 res = (ACPI_OBJECT *)prw_buffer.Pointer; 1673 if (res == NULL) { 1674 return; 1675 } 1676 1677 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1678 goto out; 1679 } 1680 1681 /* 1682 * The element 1 of the _PRW object: 1683 * The lowest power system sleeping state that can be entered 1684 * while still providing wake functionality. 1685 * The sleeping state being entered must be greater or equal to 1686 * the power state declared in element 1 of the _PRW object. 1687 */ 1688 if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) { 1689 goto out; 1690 } 1691 1692 if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) { 1693 goto out; 1694 } 1695 1696 /* 1697 * The element 0 of the _PRW object: 1698 */ 1699 switch(res->Package.Elements[0].Type) { 1700 case ACPI_TYPE_INTEGER: 1701 /* 1702 * If the data type of this package element is numeric, then this 1703 * _PRW package element is the bit index in the GPEx_EN, in the 1704 * GPE blocks described in the FADT, of the enable bit that is 1705 * enabled for the wake event. 1706 */ 1707 1708 status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value, 1709 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE); 1710 if (ACPI_FAILURE(status)) 1711 printf("%s: EnableEvent Failed\n", __func__); 1712 break; 1713 1714 case ACPI_TYPE_PACKAGE: 1715 /* XXX TBD */ 1716 1717 /* 1718 * If the data type of this package element is a package, then this 1719 * _PRW package element is itself a package containing two 1720 * elements. The first is an object reference to the GPE Block 1721 * device that contains the GPE that will be triggered by the wake 1722 * event. The second element is numeric and it contains the bit 1723 * index in the GPEx_EN, in the GPE Block referenced by the 1724 * first element in the package, of the enable bit that is enabled for 1725 * the wake event. 1726 * For example, if this field is a package then it is of the form: 1727 * Package() {\_SB.PCI0.ISA.GPE, 2} 1728 */ 1729 1730 break; 1731 1732 default: 1733 break; 1734 } 1735 1736 out: 1737 if (prw_buffer.Pointer != NULL) 1738 AcpiOsFree(prw_buffer.Pointer); 1739 return; 1740 } 1741 1742 /* 1743 * Control interface. 1744 * 1745 * We multiplex ioctls for all participating ACPI devices here. Individual 1746 * drivers wanting to be accessible via /dev/acpi should use the register/deregister 1747 * interface to make their handlers visible. 1748 */ 1749 struct acpi_ioctl_hook 1750 { 1751 TAILQ_ENTRY(acpi_ioctl_hook) link; 1752 u_long cmd; 1753 int (* fn)(u_long cmd, caddr_t addr, void *arg); 1754 void *arg; 1755 }; 1756 1757 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 1758 static int acpi_ioctl_hooks_initted; 1759 1760 /* 1761 * Register an ioctl handler. 1762 */ 1763 int 1764 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 1765 { 1766 struct acpi_ioctl_hook *hp; 1767 1768 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 1769 return(ENOMEM); 1770 hp->cmd = cmd; 1771 hp->fn = fn; 1772 hp->arg = arg; 1773 if (acpi_ioctl_hooks_initted == 0) { 1774 TAILQ_INIT(&acpi_ioctl_hooks); 1775 acpi_ioctl_hooks_initted = 1; 1776 } 1777 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 1778 return(0); 1779 } 1780 1781 /* 1782 * Deregister an ioctl handler. 1783 */ 1784 void 1785 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 1786 { 1787 struct acpi_ioctl_hook *hp; 1788 1789 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 1790 if ((hp->cmd == cmd) && (hp->fn == fn)) 1791 break; 1792 1793 if (hp != NULL) { 1794 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 1795 free(hp, M_ACPIDEV); 1796 } 1797 } 1798 1799 static int 1800 acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 1801 { 1802 return(0); 1803 } 1804 1805 static int 1806 acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 1807 { 1808 return(0); 1809 } 1810 1811 static int 1812 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 1813 { 1814 struct acpi_softc *sc; 1815 struct acpi_ioctl_hook *hp; 1816 int error, xerror, state; 1817 ACPI_LOCK_DECL; 1818 1819 ACPI_LOCK; 1820 1821 error = state = 0; 1822 sc = dev->si_drv1; 1823 1824 /* 1825 * Scan the list of registered ioctls, looking for handlers. 1826 */ 1827 if (acpi_ioctl_hooks_initted) { 1828 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 1829 if (hp->cmd == cmd) { 1830 xerror = hp->fn(cmd, addr, hp->arg); 1831 if (xerror != 0) 1832 error = xerror; 1833 goto out; 1834 } 1835 } 1836 } 1837 1838 /* 1839 * Core system ioctls. 1840 */ 1841 switch (cmd) { 1842 case ACPIIO_ENABLE: 1843 if (ACPI_FAILURE(acpi_Enable(sc))) 1844 error = ENXIO; 1845 break; 1846 1847 case ACPIIO_DISABLE: 1848 if (ACPI_FAILURE(acpi_Disable(sc))) 1849 error = ENXIO; 1850 break; 1851 1852 case ACPIIO_SETSLPSTATE: 1853 if (!sc->acpi_enabled) { 1854 error = ENXIO; 1855 break; 1856 } 1857 state = *(int *)addr; 1858 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 1859 acpi_SetSleepState(sc, state); 1860 } else { 1861 error = EINVAL; 1862 } 1863 break; 1864 1865 default: 1866 if (error == 0) 1867 error = EINVAL; 1868 break; 1869 } 1870 1871 out: 1872 ACPI_UNLOCK; 1873 return(error); 1874 } 1875 1876 static int 1877 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 1878 { 1879 char sleep_state[10]; 1880 int error; 1881 u_int new_state, old_state; 1882 1883 old_state = *(u_int *)oidp->oid_arg1; 1884 if (old_state > ACPI_S_STATES_MAX+1) { 1885 strcpy(sleep_state, "unknown"); 1886 } else { 1887 bzero(sleep_state, sizeof(sleep_state)); 1888 strncpy(sleep_state, sleep_state_names[old_state], 1889 sizeof(sleep_state_names[old_state])); 1890 } 1891 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 1892 if (error == 0 && req->newptr != NULL) { 1893 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) { 1894 if (strncmp(sleep_state, sleep_state_names[new_state], 1895 sizeof(sleep_state)) == 0) 1896 break; 1897 } 1898 if (new_state <= ACPI_S_STATES_MAX+1) { 1899 if (new_state != old_state) { 1900 *(u_int *)oidp->oid_arg1 = new_state; 1901 } 1902 } else { 1903 error = EINVAL; 1904 } 1905 } 1906 return(error); 1907 } 1908 1909 #ifdef ACPI_DEBUG 1910 /* 1911 * Support for parsing debug options from the kernel environment. 1912 * 1913 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 1914 * by specifying the names of the bits in the debug.acpi.layer and 1915 * debug.acpi.level environment variables. Bits may be unset by 1916 * prefixing the bit name with !. 1917 */ 1918 struct debugtag 1919 { 1920 char *name; 1921 UINT32 value; 1922 }; 1923 1924 static struct debugtag dbg_layer[] = { 1925 {"ACPI_UTILITIES", ACPI_UTILITIES}, 1926 {"ACPI_HARDWARE", ACPI_HARDWARE}, 1927 {"ACPI_EVENTS", ACPI_EVENTS}, 1928 {"ACPI_TABLES", ACPI_TABLES}, 1929 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1930 {"ACPI_PARSER", ACPI_PARSER}, 1931 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1932 {"ACPI_EXECUTER", ACPI_EXECUTER}, 1933 {"ACPI_RESOURCES", ACPI_RESOURCES}, 1934 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 1935 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1936 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 1937 1938 {"ACPI_BUS", ACPI_BUS}, 1939 {"ACPI_SYSTEM", ACPI_SYSTEM}, 1940 {"ACPI_POWER", ACPI_POWER}, 1941 {"ACPI_EC", ACPI_EC}, 1942 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1943 {"ACPI_BATTERY", ACPI_BATTERY}, 1944 {"ACPI_BUTTON", ACPI_BUTTON}, 1945 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1946 {"ACPI_THERMAL", ACPI_THERMAL}, 1947 {"ACPI_FAN", ACPI_FAN}, 1948 1949 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 1950 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 1951 {NULL, 0} 1952 }; 1953 1954 static struct debugtag dbg_level[] = { 1955 {"ACPI_LV_OK", ACPI_LV_OK}, 1956 {"ACPI_LV_INFO", ACPI_LV_INFO}, 1957 {"ACPI_LV_WARN", ACPI_LV_WARN}, 1958 {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 1959 {"ACPI_LV_FATAL", ACPI_LV_FATAL}, 1960 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 1961 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 1962 1963 /* Trace verbosity level 1 [Standard Trace Level] */ 1964 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 1965 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 1966 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 1967 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 1968 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 1969 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 1970 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 1971 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 1972 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 1973 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 1974 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 1975 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 1976 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 1977 {"ACPI_LV_INIT", ACPI_LV_INIT}, 1978 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 1979 1980 /* Trace verbosity level 2 [Function tracing and memory allocation] */ 1981 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 1982 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 1983 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 1984 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 1985 {"ACPI_LV_ALL", ACPI_LV_ALL}, 1986 1987 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 1988 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 1989 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 1990 {"ACPI_LV_IO", ACPI_LV_IO}, 1991 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 1992 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 1993 1994 /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 1995 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 1996 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 1997 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 1998 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 1999 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 2000 {NULL, 0} 2001 }; 2002 2003 static void 2004 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 2005 { 2006 char *ep; 2007 int i, l; 2008 int set; 2009 2010 while (*cp) { 2011 if (isspace(*cp)) { 2012 cp++; 2013 continue; 2014 } 2015 ep = cp; 2016 while (*ep && !isspace(*ep)) 2017 ep++; 2018 if (*cp == '!') { 2019 set = 0; 2020 cp++; 2021 if (cp == ep) 2022 continue; 2023 } else { 2024 set = 1; 2025 } 2026 l = ep - cp; 2027 for (i = 0; tag[i].name != NULL; i++) { 2028 if (!strncmp(cp, tag[i].name, l)) { 2029 if (set) { 2030 *flag |= tag[i].value; 2031 } else { 2032 *flag &= ~tag[i].value; 2033 } 2034 printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 2035 } 2036 } 2037 cp = ep; 2038 } 2039 } 2040 2041 static void 2042 acpi_set_debugging(void *junk) 2043 { 2044 char *cp; 2045 2046 if (!cold) 2047 return; 2048 2049 AcpiDbgLayer = 0; 2050 AcpiDbgLevel = 0; 2051 if ((cp = getenv("debug.acpi.layer")) != NULL) { 2052 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 2053 freeenv(cp); 2054 } 2055 if ((cp = getenv("debug.acpi.level")) != NULL) { 2056 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 2057 freeenv(cp); 2058 } 2059 2060 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 2061 } 2062 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 2063 #endif 2064 2065 static int 2066 acpi_pm_func(u_long cmd, void *arg, ...) 2067 { 2068 int state, acpi_state; 2069 int error; 2070 struct acpi_softc *sc; 2071 va_list ap; 2072 2073 error = 0; 2074 switch (cmd) { 2075 case POWER_CMD_SUSPEND: 2076 sc = (struct acpi_softc *)arg; 2077 if (sc == NULL) { 2078 error = EINVAL; 2079 goto out; 2080 } 2081 2082 va_start(ap, arg); 2083 state = va_arg(ap, int); 2084 va_end(ap); 2085 2086 switch (state) { 2087 case POWER_SLEEP_STATE_STANDBY: 2088 acpi_state = sc->acpi_standby_sx; 2089 break; 2090 case POWER_SLEEP_STATE_SUSPEND: 2091 acpi_state = sc->acpi_suspend_sx; 2092 break; 2093 case POWER_SLEEP_STATE_HIBERNATE: 2094 acpi_state = ACPI_STATE_S4; 2095 break; 2096 default: 2097 error = EINVAL; 2098 goto out; 2099 } 2100 2101 acpi_SetSleepState(sc, acpi_state); 2102 break; 2103 2104 default: 2105 error = EINVAL; 2106 goto out; 2107 } 2108 2109 out: 2110 return (error); 2111 } 2112 2113 static void 2114 acpi_pm_register(void *arg) 2115 { 2116 int error; 2117 2118 if (!cold) 2119 return; 2120 2121 if (!resource_int_value("acpi", 0, "disabled", &error) && 2122 (error != 0)) 2123 return; 2124 2125 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2126 } 2127 2128 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2129 2130