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/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/bus.h> 40 #include <sys/conf.h> 41 #include <sys/ioccom.h> 42 #include <sys/reboot.h> 43 #include <sys/sysctl.h> 44 #include <sys/ctype.h> 45 46 #include <machine/clock.h> 47 48 #include <machine/resource.h> 49 50 #include "acpi.h" 51 52 #include <dev/acpica/acpivar.h> 53 #include <dev/acpica/acpiio.h> 54 55 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 56 57 /* 58 * Hooks for the ACPI CA debugging infrastructure 59 */ 60 #define _COMPONENT ACPI_BUS 61 MODULE_NAME("ACPI") 62 63 /* 64 * Character device 65 */ 66 67 static d_open_t acpiopen; 68 static d_close_t acpiclose; 69 static d_ioctl_t acpiioctl; 70 71 #define CDEV_MAJOR 152 72 static struct cdevsw acpi_cdevsw = { 73 acpiopen, 74 acpiclose, 75 noread, 76 nowrite, 77 acpiioctl, 78 nopoll, 79 nommap, 80 nostrategy, 81 "acpi", 82 CDEV_MAJOR, 83 nodump, 84 nopsize, 85 0 86 }; 87 88 static const char* sleep_state_names[] = { 89 "S0", "S1", "S2", "S3", "S4", "S5", "S4B" }; 90 91 /* this has to be static, as the softc is gone when we need it */ 92 static int acpi_off_state = ACPI_STATE_S5; 93 94 struct mtx acpi_mutex; 95 96 static int acpi_modevent(struct module *mod, int event, void *junk); 97 static void acpi_identify(driver_t *driver, device_t parent); 98 static int acpi_probe(device_t dev); 99 static int acpi_attach(device_t dev); 100 static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 101 static int acpi_print_resources(struct resource_list *rl, const char *name, int type, 102 const char *format); 103 static int acpi_print_child(device_t bus, device_t child); 104 static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 105 static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 106 static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 107 u_long count); 108 static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 109 u_long *countp); 110 static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 111 u_long start, u_long end, u_long count, u_int flags); 112 static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 113 114 static void acpi_probe_children(device_t bus); 115 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 116 117 static void acpi_shutdown_pre_sync(void *arg, int howto); 118 static void acpi_shutdown_final(void *arg, int howto); 119 120 static void acpi_enable_fixed_events(struct acpi_softc *sc); 121 122 static void acpi_system_eventhandler_sleep(void *arg, int state); 123 static void acpi_system_eventhandler_wakeup(void *arg, int state); 124 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 125 126 static device_method_t acpi_methods[] = { 127 /* Device interface */ 128 DEVMETHOD(device_identify, acpi_identify), 129 DEVMETHOD(device_probe, acpi_probe), 130 DEVMETHOD(device_attach, acpi_attach), 131 DEVMETHOD(device_shutdown, bus_generic_shutdown), 132 DEVMETHOD(device_suspend, bus_generic_suspend), 133 DEVMETHOD(device_resume, bus_generic_resume), 134 135 /* Bus interface */ 136 DEVMETHOD(bus_add_child, acpi_add_child), 137 DEVMETHOD(bus_print_child, acpi_print_child), 138 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 139 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 140 DEVMETHOD(bus_set_resource, acpi_set_resource), 141 DEVMETHOD(bus_get_resource, acpi_get_resource), 142 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 143 DEVMETHOD(bus_release_resource, acpi_release_resource), 144 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 145 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 146 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 147 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 148 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 149 150 {0, 0} 151 }; 152 153 static driver_t acpi_driver = { 154 "acpi", 155 acpi_methods, 156 sizeof(struct acpi_softc), 157 }; 158 159 devclass_t acpi_devclass; 160 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 161 162 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 163 SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 164 165 /* 166 * ACPI can only be loaded as a module by the loader; activating it after 167 * system bootstrap time is not useful, and can be fatal to the system. 168 * It also cannot be unloaded, since the entire system bus heirarchy hangs off it. 169 */ 170 static int 171 acpi_modevent(struct module *mod, int event, void *junk) 172 { 173 switch(event) { 174 case MOD_LOAD: 175 if (!cold) 176 return(EPERM); 177 break; 178 case MOD_UNLOAD: 179 return(EBUSY); 180 default: 181 break; 182 } 183 return(0); 184 } 185 186 /* 187 * Detect ACPI, perform early initialisation 188 */ 189 static void 190 acpi_identify(driver_t *driver, device_t parent) 191 { 192 device_t child; 193 int error; 194 #ifdef ENABLE_DEBUGGER 195 char *debugpoint = getenv("debug.acpi.debugger"); 196 #endif 197 198 FUNCTION_TRACE(__func__); 199 200 if(!cold){ 201 printf("Don't load this driver from userland!!\n"); 202 return ; 203 } 204 205 /* 206 * Make sure we're not being doubly invoked. 207 */ 208 if (device_find_child(parent, "acpi", 0) != NULL) 209 return_VOID; 210 211 /* initialise the ACPI mutex */ 212 mtx_init(&acpi_mutex, "ACPI global lock", MTX_DEF); 213 214 /* 215 * Start up the ACPI CA subsystem. 216 */ 217 #ifdef ENABLE_DEBUGGER 218 if (debugpoint && !strcmp(debugpoint, "init")) 219 acpi_EnterDebugger(); 220 #endif 221 if ((error = AcpiInitializeSubsystem()) != AE_OK) { 222 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 223 return_VOID; 224 } 225 #ifdef ENABLE_DEBUGGER 226 if (debugpoint && !strcmp(debugpoint, "tables")) 227 acpi_EnterDebugger(); 228 #endif 229 if ((error = AcpiLoadTables()) != AE_OK) { 230 printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 231 return_VOID; 232 } 233 234 /* 235 * Attach the actual ACPI device. 236 */ 237 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 238 device_printf(parent, "ACPI: could not attach\n"); 239 return_VOID; 240 } 241 } 242 243 /* 244 * Fetch some descriptive data from ACPI to put in our attach message 245 */ 246 static int 247 acpi_probe(device_t dev) 248 { 249 ACPI_TABLE_HEADER th; 250 char buf[20]; 251 ACPI_STATUS status; 252 int error; 253 254 FUNCTION_TRACE(__func__); 255 ACPI_LOCK; 256 257 if ((status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) { 258 device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status)); 259 error = ENXIO; 260 } else { 261 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 262 device_set_desc_copy(dev, buf); 263 error = 0; 264 } 265 ACPI_UNLOCK; 266 return_VALUE(error); 267 } 268 269 static int 270 acpi_attach(device_t dev) 271 { 272 struct acpi_softc *sc; 273 ACPI_STATUS status; 274 int error; 275 #ifdef ENABLE_DEBUGGER 276 char *debugpoint = getenv("debug.acpi.debugger"); 277 #endif 278 279 FUNCTION_TRACE(__func__); 280 ACPI_LOCK; 281 sc = device_get_softc(dev); 282 bzero(sc, sizeof(*sc)); 283 sc->acpi_dev = dev; 284 285 acpi_install_wakeup_handler(sc); 286 287 #ifdef ENABLE_DEBUGGER 288 if (debugpoint && !strcmp(debugpoint, "spaces")) 289 acpi_EnterDebugger(); 290 #endif 291 292 /* 293 * Install the default address space handlers. 294 */ 295 error = ENXIO; 296 if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 297 ACPI_ADR_SPACE_SYSTEM_MEMORY, 298 ACPI_DEFAULT_HANDLER, 299 NULL, NULL)) != AE_OK) { 300 device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status)); 301 goto out; 302 } 303 if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 304 ACPI_ADR_SPACE_SYSTEM_IO, 305 ACPI_DEFAULT_HANDLER, 306 NULL, NULL)) != AE_OK) { 307 device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status)); 308 goto out; 309 } 310 if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 311 ACPI_ADR_SPACE_PCI_CONFIG, 312 ACPI_DEFAULT_HANDLER, 313 NULL, NULL)) != AE_OK) { 314 device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status)); 315 goto out; 316 } 317 318 /* 319 * Bring ACPI fully online. 320 * 321 * Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT) 322 * and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT). 323 * 324 * XXX We need to arrange for the object init pass after we have attached all our 325 * child devices. 326 */ 327 #ifdef ENABLE_DEBUGGER 328 if (debugpoint && !strcmp(debugpoint, "enable")) 329 acpi_EnterDebugger(); 330 #endif 331 if ((status = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) { 332 device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status)); 333 goto out; 334 } 335 336 /* 337 * Setup our sysctl tree. 338 * 339 * XXX: This doesn't check to make sure that none of these fail. 340 */ 341 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 342 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 343 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 344 device_get_name(dev), CTLFLAG_RD, 0, ""); 345 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 346 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 347 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 348 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 349 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 350 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 351 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 352 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 353 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 354 355 /* 356 * Dispatch the default sleep state to devices. 357 * TBD: should be configured from userland policy manager. 358 */ 359 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 360 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 361 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 362 363 acpi_enable_fixed_events(sc); 364 365 /* 366 * Scan the namespace and attach/initialise children. 367 */ 368 #ifdef ENABLE_DEBUGGER 369 if (debugpoint && !strcmp(debugpoint, "probe")) 370 acpi_EnterDebugger(); 371 #endif 372 if (!acpi_disabled("bus")) 373 acpi_probe_children(dev); 374 375 /* 376 * Register our shutdown handlers 377 */ 378 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 379 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 380 381 /* 382 * Register our acpi event handlers. 383 * XXX should be configurable eg. via userland policy manager. 384 */ 385 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 386 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 387 388 /* 389 * Flag our initial states. 390 */ 391 sc->acpi_enabled = 1; 392 sc->acpi_sstate = ACPI_STATE_S0; 393 394 /* 395 * Create the control device 396 */ 397 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi"); 398 sc->acpi_dev_t->si_drv1 = sc; 399 400 #ifdef ENABLE_DEBUGGER 401 if (debugpoint && !strcmp(debugpoint, "running")) 402 acpi_EnterDebugger(); 403 #endif 404 error = 0; 405 406 out: 407 ACPI_UNLOCK; 408 return_VALUE(error); 409 } 410 411 /* 412 * Handle a new device being added 413 */ 414 static device_t 415 acpi_add_child(device_t bus, int order, const char *name, int unit) 416 { 417 struct acpi_device *ad; 418 device_t child; 419 420 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 421 return(NULL); 422 bzero(ad, sizeof(*ad)); 423 424 resource_list_init(&ad->ad_rl); 425 426 child = device_add_child_ordered(bus, order, name, unit); 427 if (child != NULL) 428 device_set_ivars(child, ad); 429 return(child); 430 } 431 432 /* 433 * Print child device resource usage 434 */ 435 static int 436 acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format) 437 { 438 struct resource_list_entry *rle; 439 int printed, retval; 440 441 printed = 0; 442 retval = 0; 443 444 if (!SLIST_FIRST(rl)) 445 return(0); 446 447 /* Yes, this is kinda cheating */ 448 SLIST_FOREACH(rle, rl, link) { 449 if (rle->type == type) { 450 if (printed == 0) 451 retval += printf(" %s ", name); 452 else if (printed > 0) 453 retval += printf(","); 454 printed++; 455 retval += printf(format, rle->start); 456 if (rle->count > 1) { 457 retval += printf("-"); 458 retval += printf(format, rle->start + 459 rle->count - 1); 460 } 461 } 462 } 463 return(retval); 464 } 465 466 static int 467 acpi_print_child(device_t bus, device_t child) 468 { 469 struct acpi_device *adev = device_get_ivars(child); 470 struct resource_list *rl = &adev->ad_rl; 471 int retval = 0; 472 473 retval += bus_print_child_header(bus, child); 474 retval += acpi_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); 475 retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 476 retval += acpi_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); 477 retval += bus_print_child_footer(bus, child); 478 479 return(retval); 480 } 481 482 483 /* 484 * Handle per-device ivars 485 */ 486 static int 487 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 488 { 489 struct acpi_device *ad; 490 491 if ((ad = device_get_ivars(child)) == NULL) { 492 printf("device has no ivars\n"); 493 return(ENOENT); 494 } 495 496 switch(index) { 497 /* ACPI ivars */ 498 case ACPI_IVAR_HANDLE: 499 *(ACPI_HANDLE *)result = ad->ad_handle; 500 break; 501 case ACPI_IVAR_MAGIC: 502 *(int *)result = ad->ad_magic; 503 break; 504 case ACPI_IVAR_PRIVATE: 505 *(void **)result = ad->ad_private; 506 break; 507 508 default: 509 panic("bad ivar read request (%d)\n", index); 510 return(ENOENT); 511 } 512 return(0); 513 } 514 515 static int 516 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 517 { 518 struct acpi_device *ad; 519 520 if ((ad = device_get_ivars(child)) == NULL) { 521 printf("device has no ivars\n"); 522 return(ENOENT); 523 } 524 525 switch(index) { 526 /* ACPI ivars */ 527 case ACPI_IVAR_HANDLE: 528 ad->ad_handle = (ACPI_HANDLE)value; 529 break; 530 case ACPI_IVAR_MAGIC: 531 ad->ad_magic = (int )value; 532 break; 533 case ACPI_IVAR_PRIVATE: 534 ad->ad_private = (void *)value; 535 break; 536 537 default: 538 panic("bad ivar write request (%d)\n", index); 539 return(ENOENT); 540 } 541 return(0); 542 } 543 544 /* 545 * Handle child resource allocation/removal 546 */ 547 static int 548 acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 549 { 550 struct acpi_device *ad = device_get_ivars(child); 551 struct resource_list *rl = &ad->ad_rl; 552 553 resource_list_add(rl, type, rid, start, start + count -1, count); 554 555 return(0); 556 } 557 558 static int 559 acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 560 { 561 struct acpi_device *ad = device_get_ivars(child); 562 struct resource_list *rl = &ad->ad_rl; 563 struct resource_list_entry *rle; 564 565 rle = resource_list_find(rl, type, rid); 566 if (!rle) 567 return(ENOENT); 568 569 if (startp) 570 *startp = rle->start; 571 if (countp) 572 *countp = rle->count; 573 574 return(0); 575 } 576 577 static struct resource * 578 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 579 u_long start, u_long end, u_long count, u_int flags) 580 { 581 struct acpi_device *ad = device_get_ivars(child); 582 struct resource_list *rl = &ad->ad_rl; 583 584 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 585 } 586 587 static int 588 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 589 { 590 struct acpi_device *ad = device_get_ivars(child); 591 struct resource_list *rl = &ad->ad_rl; 592 593 return(resource_list_release(rl, bus, child, type, rid, r)); 594 } 595 596 /* 597 * Scan relevant portions of the ACPI namespace and attach child devices. 598 * 599 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 600 * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 601 */ 602 static void 603 acpi_probe_children(device_t bus) 604 { 605 ACPI_HANDLE parent; 606 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 607 int i; 608 609 FUNCTION_TRACE(__func__); 610 ACPI_ASSERTLOCK; 611 612 /* 613 * Create any static children by calling device identify methods. 614 */ 615 DEBUG_PRINT(TRACE_OBJECTS, ("device identify routines\n")); 616 bus_generic_probe(bus); 617 618 /* 619 * Scan the namespace and insert placeholders for all the devices that 620 * we find. 621 * 622 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 623 * we want to create nodes for all devices, not just those that are currently 624 * present. (This assumes that we don't want to create/remove devices as they 625 * appear, which might be smarter.) 626 */ 627 DEBUG_PRINT(TRACE_OBJECTS, ("namespace scan\n")); 628 for (i = 0; scopes[i] != NULL; i++) 629 if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK) 630 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 631 632 /* 633 * Scan all of the child devices we have created and let them probe/attach. 634 */ 635 DEBUG_PRINT(TRACE_OBJECTS, ("first bus_generic_attach\n")); 636 bus_generic_attach(bus); 637 638 /* 639 * Some of these children may have attached others as part of their attach 640 * process (eg. the root PCI bus driver), so rescan. 641 */ 642 DEBUG_PRINT(TRACE_OBJECTS, ("second bus_generic_attach\n")); 643 bus_generic_attach(bus); 644 645 return_VOID; 646 } 647 648 /* 649 * Evaluate a child device and determine whether we might attach a device to 650 * it. 651 */ 652 static ACPI_STATUS 653 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 654 { 655 ACPI_OBJECT_TYPE type; 656 device_t child, bus = (device_t)context; 657 658 FUNCTION_TRACE(__func__); 659 660 /* 661 * Skip this device if we think we'll have trouble with it. 662 */ 663 if (acpi_avoid(handle)) 664 return_ACPI_STATUS(AE_OK); 665 666 if (AcpiGetType(handle, &type) == AE_OK) { 667 switch(type) { 668 case ACPI_TYPE_DEVICE: 669 case ACPI_TYPE_PROCESSOR: 670 case ACPI_TYPE_THERMAL: 671 case ACPI_TYPE_POWER: 672 if (acpi_disabled("children")) 673 break; 674 /* 675 * Create a placeholder device for this node. Sort the placeholder 676 * so that the probe/attach passes will run breadth-first. 677 */ 678 DEBUG_PRINT(TRACE_OBJECTS, ("scanning '%s'\n", acpi_name(handle))) 679 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 680 acpi_set_handle(child, handle); 681 DEBUG_EXEC(device_probe_and_attach(child)); 682 break; 683 } 684 } 685 return_ACPI_STATUS(AE_OK); 686 } 687 688 static void 689 acpi_shutdown_pre_sync(void *arg, int howto) 690 { 691 692 ACPI_ASSERTLOCK; 693 694 /* 695 * Disable all ACPI events before soft off, otherwise the system 696 * will be turned on again on some laptops. 697 * 698 * XXX this should probably be restricted to masking some events just 699 * before powering down, since we may still need ACPI during the 700 * shutdown process. 701 */ 702 acpi_Disable((struct acpi_softc *)arg); 703 } 704 705 static void 706 acpi_shutdown_final(void *arg, int howto) 707 { 708 ACPI_STATUS status; 709 710 ACPI_ASSERTLOCK; 711 712 if (howto & RB_POWEROFF) { 713 printf("Power system off using ACPI...\n"); 714 if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) { 715 printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 716 } else { 717 DELAY(1000000); 718 printf("ACPI power-off failed - timeout\n"); 719 } 720 } 721 } 722 723 static void 724 acpi_enable_fixed_events(struct acpi_softc *sc) 725 { 726 static int first_time = 1; 727 #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 728 729 ACPI_ASSERTLOCK; 730 731 /* Enable and clear fixed events and install handlers. */ 732 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 733 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 734 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 735 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 736 acpi_eventhandler_power_button_for_sleep, sc); 737 if (first_time) { 738 device_printf(sc->acpi_dev, MSGFORMAT, "power"); 739 } 740 } 741 if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 742 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 743 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 744 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 745 acpi_eventhandler_sleep_button_for_sleep, sc); 746 if (first_time) { 747 device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 748 } 749 } 750 751 first_time = 0; 752 } 753 754 /* 755 * Returns true if the device is actually present and should 756 * be attached to. This requires the present, enabled, UI-visible 757 * and diagnostics-passed bits to be set. 758 */ 759 BOOLEAN 760 acpi_DeviceIsPresent(device_t dev) 761 { 762 ACPI_HANDLE h; 763 ACPI_DEVICE_INFO devinfo; 764 ACPI_STATUS error; 765 766 ACPI_ASSERTLOCK; 767 768 if ((h = acpi_get_handle(dev)) == NULL) 769 return(FALSE); 770 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 771 return(FALSE); 772 /* XXX 0xf is probably not appropriate */ 773 if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf)) 774 return(TRUE); 775 return(FALSE); 776 } 777 778 /* 779 * Match a HID string against a device 780 */ 781 BOOLEAN 782 acpi_MatchHid(device_t dev, char *hid) 783 { 784 ACPI_HANDLE h; 785 ACPI_DEVICE_INFO devinfo; 786 ACPI_STATUS error; 787 788 ACPI_ASSERTLOCK; 789 790 if (hid == NULL) 791 return(FALSE); 792 if ((h = acpi_get_handle(dev)) == NULL) 793 return(FALSE); 794 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 795 return(FALSE); 796 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 797 return(TRUE); 798 return(FALSE); 799 } 800 801 /* 802 * Return the handle of a named object within our scope, ie. that of (parent) 803 * or one if its parents. 804 */ 805 ACPI_STATUS 806 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 807 { 808 ACPI_HANDLE r; 809 ACPI_STATUS status; 810 811 ACPI_ASSERTLOCK; 812 813 /* walk back up the tree to the root */ 814 for (;;) { 815 status = AcpiGetHandle(parent, path, &r); 816 if (status == AE_OK) { 817 *result = r; 818 return(AE_OK); 819 } 820 if (status != AE_NOT_FOUND) 821 return(AE_OK); 822 if (AcpiGetParent(parent, &r) != AE_OK) 823 return(AE_NOT_FOUND); 824 parent = r; 825 } 826 } 827 828 /* 829 * Allocate a buffer with a preset data size. 830 */ 831 ACPI_BUFFER * 832 acpi_AllocBuffer(int size) 833 { 834 ACPI_BUFFER *buf; 835 836 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 837 return(NULL); 838 buf->Length = size; 839 buf->Pointer = (void *)(buf + 1); 840 return(buf); 841 } 842 843 /* 844 * Perform the tedious double-get procedure required for fetching something into 845 * an ACPI_BUFFER that has not been initialised. 846 */ 847 ACPI_STATUS 848 acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf) 849 { 850 ACPI_STATUS status; 851 852 ACPI_ASSERTLOCK; 853 854 buf->Length = 0; 855 buf->Pointer = NULL; 856 857 if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW) 858 return(status); 859 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 860 return(AE_NO_MEMORY); 861 return(func(handle, buf)); 862 } 863 864 /* 865 * Perform the tedious double-get procedure required for fetching a table into 866 * an ACPI_BUFFER that has not been initialised. 867 */ 868 ACPI_STATUS 869 acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf) 870 { 871 ACPI_STATUS status; 872 873 ACPI_ASSERTLOCK; 874 875 buf->Length = 0; 876 buf->Pointer = NULL; 877 878 if ((status = AcpiGetTable(table, instance, buf)) != AE_BUFFER_OVERFLOW) 879 return(status); 880 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 881 return(AE_NO_MEMORY); 882 return(AcpiGetTable(table, instance, buf)); 883 } 884 885 /* 886 * Perform the tedious double-evaluate procedure for evaluating something into 887 * an ACPI_BUFFER that has not been initialised. Note that this evaluates 888 * twice, so avoid applying this to things that may have side-effects. 889 * 890 * This is like AcpiEvaluateObject with automatic buffer allocation. 891 */ 892 ACPI_STATUS 893 acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params, 894 ACPI_BUFFER *buf) 895 { 896 ACPI_STATUS status; 897 898 ACPI_ASSERTLOCK; 899 900 buf->Length = 0; 901 buf->Pointer = NULL; 902 903 if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW) 904 return(status); 905 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 906 return(AE_NO_MEMORY); 907 return(AcpiEvaluateObject(object, pathname, params, buf)); 908 } 909 910 /* 911 * Evaluate a path that should return an integer. 912 */ 913 ACPI_STATUS 914 acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 915 { 916 ACPI_STATUS error; 917 ACPI_BUFFER buf; 918 ACPI_OBJECT param; 919 920 ACPI_ASSERTLOCK; 921 922 if (handle == NULL) 923 handle = ACPI_ROOT_OBJECT; 924 buf.Pointer = ¶m; 925 buf.Length = sizeof(param); 926 if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) { 927 if (param.Type == ACPI_TYPE_INTEGER) { 928 *number = param.Integer.Value; 929 } else { 930 error = AE_TYPE; 931 } 932 } 933 return(error); 934 } 935 936 /* 937 * Iterate over the elements of an a package object, calling the supplied 938 * function for each element. 939 * 940 * XXX possible enhancement might be to abort traversal on error. 941 */ 942 ACPI_STATUS 943 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 944 { 945 ACPI_OBJECT *comp; 946 int i; 947 948 if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 949 return(AE_BAD_PARAMETER); 950 951 /* iterate over components */ 952 for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 953 func(comp, arg); 954 955 return(AE_OK); 956 } 957 958 /* 959 * Find the (index)th resource object in a set. 960 */ 961 ACPI_STATUS 962 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 963 { 964 ACPI_RESOURCE *rp; 965 int i; 966 967 rp = (ACPI_RESOURCE *)buf->Pointer; 968 i = index; 969 while (i-- > 0) { 970 /* range check */ 971 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 972 return(AE_BAD_PARAMETER); 973 /* check for terminator */ 974 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 975 (rp->Length == 0)) 976 return(AE_NOT_FOUND); 977 rp = ACPI_RESOURCE_NEXT(rp); 978 } 979 if (resp != NULL) 980 *resp = rp; 981 return(AE_OK); 982 } 983 984 /* 985 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 986 * 987 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 988 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 989 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 990 * resources. 991 */ 992 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 993 994 ACPI_STATUS 995 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 996 { 997 ACPI_RESOURCE *rp; 998 void *newp; 999 1000 /* 1001 * Initialise the buffer if necessary. 1002 */ 1003 if (buf->Pointer == NULL) { 1004 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 1005 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 1006 return(AE_NO_MEMORY); 1007 rp = (ACPI_RESOURCE *)buf->Pointer; 1008 rp->Id = ACPI_RSTYPE_END_TAG; 1009 rp->Length = 0; 1010 } 1011 if (res == NULL) 1012 return(AE_OK); 1013 1014 /* 1015 * Scan the current buffer looking for the terminator. 1016 * This will either find the terminator or hit the end 1017 * of the buffer and return an error. 1018 */ 1019 rp = (ACPI_RESOURCE *)buf->Pointer; 1020 for (;;) { 1021 /* range check, don't go outside the buffer */ 1022 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1023 return(AE_BAD_PARAMETER); 1024 if ((rp->Id == ACPI_RSTYPE_END_TAG) || 1025 (rp->Length == 0)) { 1026 break; 1027 } 1028 rp = ACPI_RESOURCE_NEXT(rp); 1029 } 1030 1031 /* 1032 * Check the size of the buffer and expand if required. 1033 * 1034 * Required size is: 1035 * size of existing resources before terminator + 1036 * size of new resource and header + 1037 * size of terminator. 1038 * 1039 * Note that this loop should really only run once, unless 1040 * for some reason we are stuffing a *really* huge resource. 1041 */ 1042 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 1043 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 1044 ACPI_RESOURCE_LENGTH) >= buf->Length) { 1045 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 1046 return(AE_NO_MEMORY); 1047 bcopy(buf->Pointer, newp, buf->Length); 1048 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1049 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 1050 AcpiOsFree(buf->Pointer); 1051 buf->Pointer = newp; 1052 buf->Length += buf->Length; 1053 } 1054 1055 /* 1056 * Insert the new resource. 1057 */ 1058 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 1059 1060 /* 1061 * And add the terminator. 1062 */ 1063 rp = ACPI_RESOURCE_NEXT(rp); 1064 rp->Id = ACPI_RSTYPE_END_TAG; 1065 rp->Length = 0; 1066 1067 return(AE_OK); 1068 } 1069 1070 static ACPI_STATUS __inline 1071 acpi_wakeup(UINT8 state) 1072 { 1073 ACPI_STATUS Status; 1074 ACPI_OBJECT_LIST Arg_list; 1075 ACPI_OBJECT Arg; 1076 ACPI_OBJECT Objects[3]; /* package plus 2 number objects */ 1077 ACPI_BUFFER ReturnBuffer; 1078 1079 FUNCTION_TRACE_U32(__func__, state); 1080 ACPI_ASSERTLOCK; 1081 1082 /* 1083 * Evaluate the _WAK method 1084 */ 1085 bzero(&Arg_list, sizeof(Arg_list)); 1086 Arg_list.Count = 1; 1087 Arg_list.Pointer = &Arg; 1088 1089 bzero(&Arg, sizeof(Arg)); 1090 Arg.Type = ACPI_TYPE_INTEGER; 1091 Arg.Integer.Value = state; 1092 1093 /* 1094 * Set up _WAK result code buffer. 1095 * 1096 * XXX should use acpi_EvaluateIntoBuffer 1097 */ 1098 bzero(Objects, sizeof(Objects)); 1099 ReturnBuffer.Length = sizeof(Objects); 1100 ReturnBuffer.Pointer = Objects; 1101 AcpiEvaluateObject (NULL, "\\_WAK", &Arg_list, &ReturnBuffer); 1102 1103 Status = AE_OK; 1104 1105 /* Check result code for _WAK */ 1106 if (Objects[0].Type != ACPI_TYPE_PACKAGE || 1107 Objects[1].Type != ACPI_TYPE_INTEGER || 1108 Objects[2].Type != ACPI_TYPE_INTEGER) { 1109 /* 1110 * In many BIOSes, _WAK doesn't return a result code. 1111 * We don't need to worry about it too much :-). 1112 */ 1113 DEBUG_PRINT(ACPI_INFO, 1114 ("acpi_wakeup: _WAK result code is corrupted, " 1115 "but should be OK.\n")); 1116 } else { 1117 /* evaluate status code */ 1118 switch (Objects[1].Integer.Value) { 1119 case 0x00000001: 1120 DEBUG_PRINT(ACPI_ERROR, 1121 ("acpi_wakeup: Wake was signaled " 1122 "but failed due to lack of power.\n")); 1123 Status = AE_ERROR; 1124 break; 1125 1126 case 0x00000002: 1127 DEBUG_PRINT(ACPI_ERROR, 1128 ("acpi_wakeup: Wake was signaled " 1129 "but failed due to thermal condition.\n")); 1130 Status = AE_ERROR; 1131 break; 1132 } 1133 /* evaluate PSS code */ 1134 if (Objects[2].Integer.Value == 0) { 1135 DEBUG_PRINT(ACPI_ERROR, 1136 ("acpi_wakeup: The targeted S-state " 1137 "was not entered because of too much current " 1138 "being drawn from the power supply.\n")); 1139 Status = AE_ERROR; 1140 } 1141 } 1142 return_ACPI_STATUS(Status); 1143 } 1144 1145 /* 1146 * Set the system sleep state 1147 * 1148 * Currently we only support S1 and S5 1149 */ 1150 ACPI_STATUS 1151 acpi_SetSleepState(struct acpi_softc *sc, int state) 1152 { 1153 ACPI_STATUS status = AE_OK; 1154 UINT16 Count; 1155 UINT8 TypeA; 1156 UINT8 TypeB; 1157 1158 FUNCTION_TRACE_U32(__func__, state); 1159 ACPI_ASSERTLOCK; 1160 1161 switch (state) { 1162 case ACPI_STATE_S0: /* XXX only for testing */ 1163 status = AcpiEnterSleepState((UINT8)state); 1164 if (status != AE_OK) { 1165 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1166 } 1167 break; 1168 1169 case ACPI_STATE_S1: 1170 case ACPI_STATE_S2: 1171 case ACPI_STATE_S3: 1172 case ACPI_STATE_S4: 1173 status = AcpiHwObtainSleepTypeRegisterData((UINT8)state, &TypeA, &TypeB); 1174 if (status != AE_OK) { 1175 device_printf(sc->acpi_dev, "AcpiHwObtainSleepTypeRegisterData failed - %s\n", AcpiFormatException(status)); 1176 break; 1177 } 1178 1179 /* 1180 * Inform all devices that we are going to sleep. 1181 */ 1182 if (DEVICE_SUSPEND(root_bus) != 0) { 1183 /* 1184 * Re-wake the system. 1185 * 1186 * XXX note that a better two-pass approach with a 'veto' pass 1187 * followed by a "real thing" pass would be better, but the 1188 * current bus interface does not provide for this. 1189 */ 1190 DEVICE_RESUME(root_bus); 1191 return_ACPI_STATUS(AE_ERROR); 1192 } 1193 sc->acpi_sstate = state; 1194 1195 if (state != ACPI_STATE_S1) { 1196 acpi_sleep_machdep(sc, state); 1197 1198 /* AcpiEnterSleepState() maybe incompleted, unlock here. */ 1199 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1200 1201 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1202 if (state >= ACPI_STATE_S4) { 1203 acpi_Disable(sc); 1204 acpi_Enable(sc); 1205 } 1206 } else { 1207 status = AcpiEnterSleepState((UINT8)state); 1208 if (status != AE_OK) { 1209 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1210 break; 1211 } 1212 /* wait for the WAK_STS bit */ 1213 Count = 0; 1214 while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) { 1215 AcpiOsSleep(0, 1); 1216 /* 1217 * Some BIOSes don't set WAK_STS at all, 1218 * give up waiting for wakeup if we time out. 1219 */ 1220 if (Count > 1000) { 1221 break; /* giving up */ 1222 } 1223 Count++; 1224 } 1225 } 1226 acpi_wakeup((UINT8)state); 1227 DEVICE_RESUME(root_bus); 1228 sc->acpi_sstate = ACPI_STATE_S0; 1229 acpi_enable_fixed_events(sc); 1230 break; 1231 1232 case ACPI_STATE_S5: 1233 /* 1234 * Shut down cleanly and power off. This will call us back through the 1235 * shutdown handlers. 1236 */ 1237 shutdown_nice(RB_POWEROFF); 1238 break; 1239 1240 default: 1241 status = AE_BAD_PARAMETER; 1242 break; 1243 } 1244 return_ACPI_STATUS(status); 1245 } 1246 1247 /* 1248 * Enable/Disable ACPI 1249 */ 1250 ACPI_STATUS 1251 acpi_Enable(struct acpi_softc *sc) 1252 { 1253 ACPI_STATUS status; 1254 u_int32_t flags; 1255 1256 FUNCTION_TRACE(__func__); 1257 ACPI_ASSERTLOCK; 1258 1259 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 1260 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1261 if (!sc->acpi_enabled) { 1262 status = AcpiEnableSubsystem(flags); 1263 } else { 1264 status = AE_OK; 1265 } 1266 if (status == AE_OK) 1267 sc->acpi_enabled = 1; 1268 return_ACPI_STATUS(status); 1269 } 1270 1271 ACPI_STATUS 1272 acpi_Disable(struct acpi_softc *sc) 1273 { 1274 ACPI_STATUS status; 1275 1276 FUNCTION_TRACE(__func__); 1277 ACPI_ASSERTLOCK; 1278 1279 if (sc->acpi_enabled) { 1280 status = AcpiDisable(); 1281 } else { 1282 status = AE_OK; 1283 } 1284 if (status == AE_OK) 1285 sc->acpi_enabled = 0; 1286 return_ACPI_STATUS(status); 1287 } 1288 1289 /* 1290 * ACPI Event Handlers 1291 */ 1292 1293 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 1294 1295 static void 1296 acpi_system_eventhandler_sleep(void *arg, int state) 1297 { 1298 FUNCTION_TRACE_U32(__func__, state); 1299 1300 ACPI_LOCK; 1301 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 1302 acpi_SetSleepState((struct acpi_softc *)arg, state); 1303 ACPI_UNLOCK; 1304 return_VOID; 1305 } 1306 1307 static void 1308 acpi_system_eventhandler_wakeup(void *arg, int state) 1309 { 1310 FUNCTION_TRACE_U32(__func__, state); 1311 1312 /* Well, what to do? :-) */ 1313 1314 ACPI_LOCK; 1315 ACPI_UNLOCK; 1316 1317 return_VOID; 1318 } 1319 1320 /* 1321 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 1322 */ 1323 UINT32 1324 acpi_eventhandler_power_button_for_sleep(void *context) 1325 { 1326 struct acpi_softc *sc = (struct acpi_softc *)context; 1327 1328 FUNCTION_TRACE(__func__); 1329 1330 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 1331 1332 return_VALUE(INTERRUPT_HANDLED); 1333 } 1334 1335 UINT32 1336 acpi_eventhandler_power_button_for_wakeup(void *context) 1337 { 1338 struct acpi_softc *sc = (struct acpi_softc *)context; 1339 1340 FUNCTION_TRACE(__func__); 1341 1342 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 1343 1344 return_VALUE(INTERRUPT_HANDLED); 1345 } 1346 1347 UINT32 1348 acpi_eventhandler_sleep_button_for_sleep(void *context) 1349 { 1350 struct acpi_softc *sc = (struct acpi_softc *)context; 1351 1352 FUNCTION_TRACE(__func__); 1353 1354 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 1355 1356 return_VALUE(INTERRUPT_HANDLED); 1357 } 1358 1359 UINT32 1360 acpi_eventhandler_sleep_button_for_wakeup(void *context) 1361 { 1362 struct acpi_softc *sc = (struct acpi_softc *)context; 1363 1364 FUNCTION_TRACE(__func__); 1365 1366 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 1367 1368 return_VALUE(INTERRUPT_HANDLED); 1369 } 1370 1371 /* 1372 * XXX This is kinda ugly, and should not be here. 1373 */ 1374 struct acpi_staticbuf { 1375 ACPI_BUFFER buffer; 1376 char data[512]; 1377 }; 1378 1379 char * 1380 acpi_name(ACPI_HANDLE handle) 1381 { 1382 static struct acpi_staticbuf buf; 1383 1384 ACPI_ASSERTLOCK; 1385 1386 buf.buffer.Length = 512; 1387 buf.buffer.Pointer = &buf.data[0]; 1388 1389 if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK) 1390 return(buf.buffer.Pointer); 1391 return("(unknown path)"); 1392 } 1393 1394 /* 1395 * Debugging/bug-avoidance. Avoid trying to fetch info on various 1396 * parts of the namespace. 1397 */ 1398 int 1399 acpi_avoid(ACPI_HANDLE handle) 1400 { 1401 char *cp, *np; 1402 int len; 1403 1404 np = acpi_name(handle); 1405 if (*np == '\\') 1406 np++; 1407 if ((cp = getenv("debug.acpi.avoid")) == NULL) 1408 return(0); 1409 1410 /* scan the avoid list checking for a match */ 1411 for (;;) { 1412 while ((*cp != 0) && isspace(*cp)) 1413 cp++; 1414 if (*cp == 0) 1415 break; 1416 len = 0; 1417 while ((cp[len] != 0) && !isspace(cp[len])) 1418 len++; 1419 if (!strncmp(cp, np, len)) { 1420 DEBUG_PRINT(TRACE_OBJECTS, ("avoiding '%s'\n", np)); 1421 return(1); 1422 } 1423 cp += len; 1424 } 1425 return(0); 1426 } 1427 1428 /* 1429 * Debugging/bug-avoidance. Disable ACPI subsystem components. 1430 */ 1431 int 1432 acpi_disabled(char *subsys) 1433 { 1434 char *cp; 1435 int len; 1436 1437 if ((cp = getenv("debug.acpi.disable")) == NULL) 1438 return(0); 1439 if (!strcmp(cp, "all")) 1440 return(1); 1441 1442 /* scan the disable list checking for a match */ 1443 for (;;) { 1444 while ((*cp != 0) && isspace(*cp)) 1445 cp++; 1446 if (*cp == 0) 1447 break; 1448 len = 0; 1449 while ((cp[len] != 0) && !isspace(cp[len])) 1450 len++; 1451 if (!strncmp(cp, subsys, len)) { 1452 DEBUG_PRINT(TRACE_OBJECTS, ("disabled '%s'\n", subsys)); 1453 return(1); 1454 } 1455 cp += len; 1456 } 1457 return(0); 1458 } 1459 1460 /* 1461 * Control interface. 1462 * 1463 * We multiplex ioctls for all participating ACPI devices here. Individual 1464 * drivers wanting to be accessible via /dev/acpi should use the register/deregister 1465 * interface to make their handlers visible. 1466 */ 1467 struct acpi_ioctl_hook 1468 { 1469 TAILQ_ENTRY(acpi_ioctl_hook) link; 1470 u_long cmd; 1471 int (* fn)(u_long cmd, caddr_t addr, void *arg); 1472 void *arg; 1473 }; 1474 1475 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 1476 static int acpi_ioctl_hooks_initted; 1477 1478 /* 1479 * Register an ioctl handler. 1480 */ 1481 int 1482 acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 1483 { 1484 struct acpi_ioctl_hook *hp; 1485 1486 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 1487 return(ENOMEM); 1488 hp->cmd = cmd; 1489 hp->fn = fn; 1490 hp->arg = arg; 1491 if (acpi_ioctl_hooks_initted == 0) { 1492 TAILQ_INIT(&acpi_ioctl_hooks); 1493 acpi_ioctl_hooks_initted = 1; 1494 } 1495 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 1496 return(0); 1497 } 1498 1499 /* 1500 * Deregister an ioctl handler. 1501 */ 1502 void 1503 acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 1504 { 1505 struct acpi_ioctl_hook *hp; 1506 1507 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 1508 if ((hp->cmd == cmd) && (hp->fn == fn)) 1509 break; 1510 1511 if (hp != NULL) { 1512 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 1513 free(hp, M_ACPIDEV); 1514 } 1515 } 1516 1517 static int 1518 acpiopen(dev_t dev, int flag, int fmt, struct proc *p) 1519 { 1520 return(0); 1521 } 1522 1523 static int 1524 acpiclose(dev_t dev, int flag, int fmt, struct proc *p) 1525 { 1526 return(0); 1527 } 1528 1529 static int 1530 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1531 { 1532 struct acpi_softc *sc; 1533 struct acpi_ioctl_hook *hp; 1534 int error, xerror, state; 1535 1536 ACPI_LOCK; 1537 1538 error = state = 0; 1539 sc = dev->si_drv1; 1540 1541 /* 1542 * Scan the list of registered ioctls, looking for handlers. 1543 */ 1544 if (acpi_ioctl_hooks_initted) { 1545 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 1546 if (hp->cmd == cmd) { 1547 xerror = hp->fn(cmd, addr, hp->arg); 1548 if (xerror != 0) 1549 error = xerror; 1550 goto out; 1551 } 1552 } 1553 } 1554 1555 /* 1556 * Core system ioctls. 1557 */ 1558 switch (cmd) { 1559 case ACPIIO_ENABLE: 1560 if (ACPI_FAILURE(acpi_Enable(sc))) 1561 error = ENXIO; 1562 break; 1563 1564 case ACPIIO_DISABLE: 1565 if (ACPI_FAILURE(acpi_Disable(sc))) 1566 error = ENXIO; 1567 break; 1568 1569 case ACPIIO_SETSLPSTATE: 1570 if (!sc->acpi_enabled) { 1571 error = ENXIO; 1572 break; 1573 } 1574 state = *(int *)addr; 1575 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 1576 acpi_SetSleepState(sc, state); 1577 } else { 1578 error = EINVAL; 1579 } 1580 break; 1581 1582 default: 1583 if (error == 0) 1584 error = EINVAL; 1585 break; 1586 } 1587 1588 out: 1589 ACPI_UNLOCK; 1590 return(error); 1591 } 1592 1593 static int 1594 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 1595 { 1596 char sleep_state[10]; 1597 int error; 1598 u_int new_state, old_state; 1599 1600 old_state = *(u_int *)oidp->oid_arg1; 1601 if (old_state > ACPI_S_STATES_MAX) { 1602 strcpy(sleep_state, "unknown"); 1603 } else { 1604 strncpy(sleep_state, sleep_state_names[old_state], 1605 sizeof(sleep_state_names[old_state])); 1606 } 1607 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 1608 if (error == 0 && req->newptr != NULL) { 1609 for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX; new_state++) { 1610 if (strncmp(sleep_state, sleep_state_names[new_state], 1611 sizeof(sleep_state)) == 0) 1612 break; 1613 } 1614 if ((new_state != old_state) && (new_state <= ACPI_S_STATES_MAX)) { 1615 *(u_int *)oidp->oid_arg1 = new_state; 1616 } else { 1617 error = EINVAL; 1618 } 1619 } 1620 return(error); 1621 } 1622 1623 #ifdef ACPI_DEBUG 1624 /* 1625 * Support for parsing debug options from the kernel environment. 1626 * 1627 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 1628 * by specifying the names of the bits in the debug.acpi.layer and 1629 * debug.acpi.level environment variables. Bits may be unset by 1630 * prefixing the bit name with !. 1631 */ 1632 struct debugtag 1633 { 1634 char *name; 1635 UINT32 value; 1636 }; 1637 1638 static struct debugtag dbg_layer[] = { 1639 {"ACPI_UTILITIES", ACPI_UTILITIES}, 1640 {"ACPI_HARDWARE", ACPI_HARDWARE}, 1641 {"ACPI_EVENTS", ACPI_EVENTS}, 1642 {"ACPI_TABLES", ACPI_TABLES}, 1643 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1644 {"ACPI_PARSER", ACPI_PARSER}, 1645 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1646 {"ACPI_EXECUTER", ACPI_EXECUTER}, 1647 {"ACPI_RESOURCES", ACPI_RESOURCES}, 1648 {"ACPI_POWER", ACPI_POWER}, 1649 {"ACPI_BUS", ACPI_BUS}, 1650 {"ACPI_POWER", ACPI_POWER}, 1651 {"ACPI_EC", ACPI_EC}, 1652 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1653 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1654 {"ACPI_BATTERY", ACPI_BATTERY}, 1655 {"ACPI_BUTTON", ACPI_BUTTON}, 1656 {"ACPI_SYSTEM", ACPI_SYSTEM}, 1657 {"ACPI_THERMAL", ACPI_THERMAL}, 1658 {"ACPI_DEBUGGER", ACPI_DEBUGGER}, 1659 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1660 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 1661 {NULL, 0} 1662 }; 1663 1664 static struct debugtag dbg_level[] = { 1665 {"ACPI_OK", ACPI_OK}, 1666 {"ACPI_INFO", ACPI_INFO}, 1667 {"ACPI_WARN", ACPI_WARN}, 1668 {"ACPI_ERROR", ACPI_ERROR}, 1669 {"ACPI_FATAL", ACPI_FATAL}, 1670 {"ACPI_DEBUG_OBJECT", ACPI_DEBUG_OBJECT}, 1671 {"ACPI_ALL", ACPI_ALL}, 1672 {"TRACE_THREADS", TRACE_THREADS}, 1673 {"TRACE_PARSE", TRACE_PARSE}, 1674 {"TRACE_DISPATCH", TRACE_DISPATCH}, 1675 {"TRACE_LOAD", TRACE_LOAD}, 1676 {"TRACE_EXEC", TRACE_EXEC}, 1677 {"TRACE_NAMES", TRACE_NAMES}, 1678 {"TRACE_OPREGION", TRACE_OPREGION}, 1679 {"TRACE_BFIELD", TRACE_BFIELD}, 1680 {"TRACE_TRASH", TRACE_TRASH}, 1681 {"TRACE_TABLES", TRACE_TABLES}, 1682 {"TRACE_FUNCTIONS", TRACE_FUNCTIONS}, 1683 {"TRACE_VALUES", TRACE_VALUES}, 1684 {"TRACE_OBJECTS", TRACE_OBJECTS}, 1685 {"TRACE_ALLOCATIONS", TRACE_ALLOCATIONS}, 1686 {"TRACE_RESOURCES", TRACE_RESOURCES}, 1687 {"TRACE_IO", TRACE_IO}, 1688 {"TRACE_INTERRUPTS", TRACE_INTERRUPTS}, 1689 {"TRACE_USER_REQUESTS", TRACE_USER_REQUESTS}, 1690 {"TRACE_PACKAGE", TRACE_PACKAGE}, 1691 {"TRACE_MUTEX", TRACE_MUTEX}, 1692 {"TRACE_INIT", TRACE_INIT}, 1693 {"TRACE_ALL", TRACE_ALL}, 1694 {"VERBOSE_AML_DISASSEMBLE", VERBOSE_AML_DISASSEMBLE}, 1695 {"VERBOSE_INFO", VERBOSE_INFO}, 1696 {"VERBOSE_TABLES", VERBOSE_TABLES}, 1697 {"VERBOSE_EVENTS", VERBOSE_EVENTS}, 1698 {"VERBOSE_ALL", VERBOSE_ALL}, 1699 {NULL, 0} 1700 }; 1701 1702 static void 1703 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 1704 { 1705 char *ep; 1706 int i, l; 1707 int set; 1708 1709 while (*cp) { 1710 if (isspace(*cp)) { 1711 cp++; 1712 continue; 1713 } 1714 ep = cp; 1715 while (*ep && !isspace(*ep)) 1716 ep++; 1717 if (*cp == '!') { 1718 set = 0; 1719 cp++; 1720 if (cp == ep) 1721 continue; 1722 } else { 1723 set = 1; 1724 } 1725 l = ep - cp; 1726 for (i = 0; tag[i].name != NULL; i++) { 1727 if (!strncmp(cp, tag[i].name, l)) { 1728 if (set) { 1729 *flag |= tag[i].value; 1730 } else { 1731 *flag &= ~tag[i].value; 1732 } 1733 printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 1734 } 1735 } 1736 cp = ep; 1737 } 1738 } 1739 1740 static void 1741 acpi_set_debugging(void *junk) 1742 { 1743 char *cp; 1744 1745 AcpiDbgLayer = 0; 1746 AcpiDbgLevel = 0; 1747 if ((cp = getenv("debug.acpi.layer")) != NULL) 1748 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 1749 if ((cp = getenv("debug.acpi.level")) != NULL) 1750 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 1751 1752 printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 1753 } 1754 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 1755 #endif 1756