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