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