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