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