acpi.c revision 223502
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 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi.c 223502 2011-06-24 13:58:56Z jhb $"); 32 33#include "opt_acpi.h" 34#include <sys/param.h> 35#include <sys/kernel.h> 36#include <sys/proc.h> 37#include <sys/fcntl.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/bus.h> 41#include <sys/conf.h> 42#include <sys/ioccom.h> 43#include <sys/reboot.h> 44#include <sys/sysctl.h> 45#include <sys/ctype.h> 46#include <sys/linker.h> 47#include <sys/power.h> 48#include <sys/sbuf.h> 49#include <sys/sched.h> 50#include <sys/smp.h> 51#include <sys/timetc.h> 52 53#if defined(__i386__) || defined(__amd64__) 54#include <machine/pci_cfgreg.h> 55#endif 56#include <machine/resource.h> 57#include <machine/bus.h> 58#include <sys/rman.h> 59#include <isa/isavar.h> 60#include <isa/pnpvar.h> 61 62#include <contrib/dev/acpica/include/acpi.h> 63#include <contrib/dev/acpica/include/accommon.h> 64#include <contrib/dev/acpica/include/acnamesp.h> 65 66#include <dev/acpica/acpivar.h> 67#include <dev/acpica/acpiio.h> 68 69#include <vm/vm_param.h> 70 71MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 72 73/* Hooks for the ACPI CA debugging infrastructure */ 74#define _COMPONENT ACPI_BUS 75ACPI_MODULE_NAME("ACPI") 76 77static d_open_t acpiopen; 78static d_close_t acpiclose; 79static d_ioctl_t acpiioctl; 80 81static struct cdevsw acpi_cdevsw = { 82 .d_version = D_VERSION, 83 .d_open = acpiopen, 84 .d_close = acpiclose, 85 .d_ioctl = acpiioctl, 86 .d_name = "acpi", 87}; 88 89struct acpi_interface { 90 ACPI_STRING *data; 91 int num; 92}; 93 94/* Global mutex for locking access to the ACPI subsystem. */ 95struct mtx acpi_mutex; 96 97/* Bitmap of device quirks. */ 98int acpi_quirks; 99 100/* Supported sleep states. */ 101static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 102 103static int acpi_modevent(struct module *mod, int event, void *junk); 104static int acpi_probe(device_t dev); 105static int acpi_attach(device_t dev); 106static int acpi_suspend(device_t dev); 107static int acpi_resume(device_t dev); 108static int acpi_shutdown(device_t dev); 109static device_t acpi_add_child(device_t bus, u_int order, const char *name, 110 int unit); 111static int acpi_print_child(device_t bus, device_t child); 112static void acpi_probe_nomatch(device_t bus, device_t child); 113static void acpi_driver_added(device_t dev, driver_t *driver); 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 void acpi_reserve_resources(device_t dev); 120static int acpi_sysres_alloc(device_t dev); 121static int acpi_set_resource(device_t dev, device_t child, int type, 122 int rid, u_long start, u_long count); 123static struct resource *acpi_alloc_resource(device_t bus, device_t child, 124 int type, int *rid, u_long start, u_long end, 125 u_long count, u_int flags); 126static int acpi_adjust_resource(device_t bus, device_t child, int type, 127 struct resource *r, u_long start, u_long end); 128static int acpi_release_resource(device_t bus, device_t child, int type, 129 int rid, struct resource *r); 130static void acpi_delete_resource(device_t bus, device_t child, int type, 131 int rid); 132static uint32_t acpi_isa_get_logicalid(device_t dev); 133static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 134static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 135static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 136 ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 137 ACPI_BUFFER *ret); 138static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 139 void *context, void **retval); 140static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 141 int max_depth, acpi_scan_cb_t user_fn, void *arg); 142static int acpi_set_powerstate(device_t child, int state); 143static int acpi_isa_pnp_probe(device_t bus, device_t child, 144 struct isa_pnp_id *ids); 145static void acpi_probe_children(device_t bus); 146static void acpi_probe_order(ACPI_HANDLE handle, int *order); 147static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 148 void *context, void **status); 149static void acpi_sleep_enable(void *arg); 150static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 151static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 152static void acpi_shutdown_final(void *arg, int howto); 153static void acpi_enable_fixed_events(struct acpi_softc *sc); 154static BOOLEAN acpi_has_hid(ACPI_HANDLE handle); 155static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 156static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 157static int acpi_wake_prep_walk(int sstate); 158static int acpi_wake_sysctl_walk(device_t dev); 159static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 160static void acpi_system_eventhandler_sleep(void *arg, int state); 161static void acpi_system_eventhandler_wakeup(void *arg, int state); 162static int acpi_sname2sstate(const char *sname); 163static const char *acpi_sstate2sname(int sstate); 164static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 165static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 166static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 167static int acpi_pm_func(u_long cmd, void *arg, ...); 168static int acpi_child_location_str_method(device_t acdev, device_t child, 169 char *buf, size_t buflen); 170static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 171 char *buf, size_t buflen); 172#if defined(__i386__) || defined(__amd64__) 173static void acpi_enable_pcie(void); 174#endif 175static void acpi_hint_device_unit(device_t acdev, device_t child, 176 const char *name, int *unitp); 177static void acpi_reset_interfaces(device_t dev); 178 179static device_method_t acpi_methods[] = { 180 /* Device interface */ 181 DEVMETHOD(device_probe, acpi_probe), 182 DEVMETHOD(device_attach, acpi_attach), 183 DEVMETHOD(device_shutdown, acpi_shutdown), 184 DEVMETHOD(device_detach, bus_generic_detach), 185 DEVMETHOD(device_suspend, acpi_suspend), 186 DEVMETHOD(device_resume, acpi_resume), 187 188 /* Bus interface */ 189 DEVMETHOD(bus_add_child, acpi_add_child), 190 DEVMETHOD(bus_print_child, acpi_print_child), 191 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 192 DEVMETHOD(bus_driver_added, acpi_driver_added), 193 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 194 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 195 DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 196 DEVMETHOD(bus_set_resource, acpi_set_resource), 197 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 198 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 199 DEVMETHOD(bus_adjust_resource, acpi_adjust_resource), 200 DEVMETHOD(bus_release_resource, acpi_release_resource), 201 DEVMETHOD(bus_delete_resource, acpi_delete_resource), 202 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 203 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 204 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 205 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 206 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 207 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 208 DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 209 210 /* ACPI bus */ 211 DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 212 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 213 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 214 DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 215 216 /* ISA emulation */ 217 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 218 219 {0, 0} 220}; 221 222static driver_t acpi_driver = { 223 "acpi", 224 acpi_methods, 225 sizeof(struct acpi_softc), 226}; 227 228static devclass_t acpi_devclass; 229DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 230MODULE_VERSION(acpi, 1); 231 232ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 233 234/* Local pools for managing system resources for ACPI child devices. */ 235static struct rman acpi_rman_io, acpi_rman_mem; 236 237#define ACPI_MINIMUM_AWAKETIME 5 238 239/* Holds the description of the acpi0 device. */ 240static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 241 242SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 243static char acpi_ca_version[12]; 244SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 245 acpi_ca_version, 0, "Version of Intel ACPI-CA"); 246 247/* 248 * Allow overriding _OSI methods. 249 */ 250static char acpi_install_interface[256]; 251TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface, 252 sizeof(acpi_install_interface)); 253static char acpi_remove_interface[256]; 254TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface, 255 sizeof(acpi_remove_interface)); 256 257/* 258 * Allow override of whether methods execute in parallel or not. 259 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 260 * errors for AML that really can't handle parallel method execution. 261 * It is off by default since this breaks recursive methods and 262 * some IBMs use such code. 263 */ 264static int acpi_serialize_methods; 265TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 266 267/* Allow users to dump Debug objects without ACPI debugger. */ 268static int acpi_debug_objects; 269TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 270SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 271 CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 272 "Enable Debug objects"); 273 274/* Allow the interpreter to ignore common mistakes in BIOS. */ 275static int acpi_interpreter_slack = 1; 276TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 277SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 278 &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 279 280/* Reset system clock while resuming. XXX Remove once tested. */ 281static int acpi_reset_clock = 1; 282TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock); 283SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW, 284 &acpi_reset_clock, 1, "Reset system clock while resuming."); 285 286/* Allow users to override quirks. */ 287TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 288 289static int acpi_susp_bounce; 290SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 291 &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 292 293/* 294 * ACPI can only be loaded as a module by the loader; activating it after 295 * system bootstrap time is not useful, and can be fatal to the system. 296 * It also cannot be unloaded, since the entire system bus hierarchy hangs 297 * off it. 298 */ 299static int 300acpi_modevent(struct module *mod, int event, void *junk) 301{ 302 switch (event) { 303 case MOD_LOAD: 304 if (!cold) { 305 printf("The ACPI driver cannot be loaded after boot.\n"); 306 return (EPERM); 307 } 308 break; 309 case MOD_UNLOAD: 310 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 311 return (EBUSY); 312 break; 313 default: 314 break; 315 } 316 return (0); 317} 318 319/* 320 * Perform early initialization. 321 */ 322ACPI_STATUS 323acpi_Startup(void) 324{ 325 static int started = 0; 326 ACPI_STATUS status; 327 int val; 328 329 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 330 331 /* Only run the startup code once. The MADT driver also calls this. */ 332 if (started) 333 return_VALUE (AE_OK); 334 started = 1; 335 336 /* 337 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 338 * if more tables exist. 339 */ 340 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 341 printf("ACPI: Table initialisation failed: %s\n", 342 AcpiFormatException(status)); 343 return_VALUE (status); 344 } 345 346 /* Set up any quirks we have for this system. */ 347 if (acpi_quirks == ACPI_Q_OK) 348 acpi_table_quirks(&acpi_quirks); 349 350 /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 351 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 352 acpi_quirks &= ~ACPI_Q_BROKEN; 353 if (acpi_quirks & ACPI_Q_BROKEN) { 354 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 355 status = AE_SUPPORT; 356 } 357 358 return_VALUE (status); 359} 360 361/* 362 * Detect ACPI and perform early initialisation. 363 */ 364int 365acpi_identify(void) 366{ 367 ACPI_TABLE_RSDP *rsdp; 368 ACPI_TABLE_HEADER *rsdt; 369 ACPI_PHYSICAL_ADDRESS paddr; 370 struct sbuf sb; 371 372 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 373 374 if (!cold) 375 return (ENXIO); 376 377 /* Check that we haven't been disabled with a hint. */ 378 if (resource_disabled("acpi", 0)) 379 return (ENXIO); 380 381 /* Check for other PM systems. */ 382 if (power_pm_get_type() != POWER_PM_TYPE_NONE && 383 power_pm_get_type() != POWER_PM_TYPE_ACPI) { 384 printf("ACPI identify failed, other PM system enabled.\n"); 385 return (ENXIO); 386 } 387 388 /* Initialize root tables. */ 389 if (ACPI_FAILURE(acpi_Startup())) { 390 printf("ACPI: Try disabling either ACPI or apic support.\n"); 391 return (ENXIO); 392 } 393 394 if ((paddr = AcpiOsGetRootPointer()) == 0 || 395 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 396 return (ENXIO); 397 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 398 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 399 else 400 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 401 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 402 403 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 404 return (ENXIO); 405 sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 406 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 407 sbuf_trim(&sb); 408 sbuf_putc(&sb, ' '); 409 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 410 sbuf_trim(&sb); 411 sbuf_finish(&sb); 412 sbuf_delete(&sb); 413 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 414 415 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 416 417 return (0); 418} 419 420/* 421 * Fetch some descriptive data from ACPI to put in our attach message. 422 */ 423static int 424acpi_probe(device_t dev) 425{ 426 427 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 428 429 device_set_desc(dev, acpi_desc); 430 431 return_VALUE (0); 432} 433 434static int 435acpi_attach(device_t dev) 436{ 437 struct acpi_softc *sc; 438 ACPI_STATUS status; 439 int error, state; 440 UINT32 flags; 441 UINT8 TypeA, TypeB; 442 char *env; 443 444 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 445 446 sc = device_get_softc(dev); 447 sc->acpi_dev = dev; 448 callout_init(&sc->susp_force_to, TRUE); 449 450 error = ENXIO; 451 452 /* Initialize resource manager. */ 453 acpi_rman_io.rm_type = RMAN_ARRAY; 454 acpi_rman_io.rm_start = 0; 455 acpi_rman_io.rm_end = 0xffff; 456 acpi_rman_io.rm_descr = "ACPI I/O ports"; 457 if (rman_init(&acpi_rman_io) != 0) 458 panic("acpi rman_init IO ports failed"); 459 acpi_rman_mem.rm_type = RMAN_ARRAY; 460 acpi_rman_mem.rm_start = 0; 461 acpi_rman_mem.rm_end = ~0ul; 462 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 463 if (rman_init(&acpi_rman_mem) != 0) 464 panic("acpi rman_init memory failed"); 465 466 /* Initialise the ACPI mutex */ 467 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 468 469 /* 470 * Set the globals from our tunables. This is needed because ACPI-CA 471 * uses UINT8 for some values and we have no tunable_byte. 472 */ 473 AcpiGbl_AllMethodsSerialized = acpi_serialize_methods ? TRUE : FALSE; 474 AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE; 475 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 476 477#ifndef ACPI_DEBUG 478 /* 479 * Disable all debugging layers and levels. 480 */ 481 AcpiDbgLayer = 0; 482 AcpiDbgLevel = 0; 483#endif 484 485 /* Start up the ACPI CA subsystem. */ 486 status = AcpiInitializeSubsystem(); 487 if (ACPI_FAILURE(status)) { 488 device_printf(dev, "Could not initialize Subsystem: %s\n", 489 AcpiFormatException(status)); 490 goto out; 491 } 492 493 /* Override OS interfaces if the user requested. */ 494 acpi_reset_interfaces(dev); 495 496 /* Load ACPI name space. */ 497 status = AcpiLoadTables(); 498 if (ACPI_FAILURE(status)) { 499 device_printf(dev, "Could not load Namespace: %s\n", 500 AcpiFormatException(status)); 501 goto out; 502 } 503 504#if defined(__i386__) || defined(__amd64__) 505 /* Handle MCFG table if present. */ 506 acpi_enable_pcie(); 507#endif 508 509 /* 510 * Note that some systems (specifically, those with namespace evaluation 511 * issues that require the avoidance of parts of the namespace) must 512 * avoid running _INI and _STA on everything, as well as dodging the final 513 * object init pass. 514 * 515 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 516 * 517 * XXX We should arrange for the object init pass after we have attached 518 * all our child devices, but on many systems it works here. 519 */ 520 flags = 0; 521 if (testenv("debug.acpi.avoid")) 522 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 523 524 /* Bring the hardware and basic handlers online. */ 525 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 526 device_printf(dev, "Could not enable ACPI: %s\n", 527 AcpiFormatException(status)); 528 goto out; 529 } 530 531 /* 532 * Call the ECDT probe function to provide EC functionality before 533 * the namespace has been evaluated. 534 * 535 * XXX This happens before the sysresource devices have been probed and 536 * attached so its resources come from nexus0. In practice, this isn't 537 * a problem but should be addressed eventually. 538 */ 539 acpi_ec_ecdt_probe(dev); 540 541 /* Bring device objects and regions online. */ 542 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 543 device_printf(dev, "Could not initialize ACPI objects: %s\n", 544 AcpiFormatException(status)); 545 goto out; 546 } 547 548 /* 549 * Setup our sysctl tree. 550 * 551 * XXX: This doesn't check to make sure that none of these fail. 552 */ 553 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 554 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 555 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 556 device_get_name(dev), CTLFLAG_RD, 0, ""); 557 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 558 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 559 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 560 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 561 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 562 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 563 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 564 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 565 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 566 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 567 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 568 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 569 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 570 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 571 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 572 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 573 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 574 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 575 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 576 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 577 "sleep delay"); 578 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 579 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 580 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 581 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 582 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 583 OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 584 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 585 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 586 OID_AUTO, "handle_reboot", CTLFLAG_RW, 587 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 588 589 /* 590 * Default to 1 second before sleeping to give some machines time to 591 * stabilize. 592 */ 593 sc->acpi_sleep_delay = 1; 594 if (bootverbose) 595 sc->acpi_verbose = 1; 596 if ((env = getenv("hw.acpi.verbose")) != NULL) { 597 if (strcmp(env, "0") != 0) 598 sc->acpi_verbose = 1; 599 freeenv(env); 600 } 601 602 /* Only enable reboot by default if the FADT says it is available. */ 603 if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) 604 sc->acpi_handle_reboot = 1; 605 606 /* Only enable S4BIOS by default if the FACS says it is available. */ 607 if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 608 sc->acpi_s4bios = 1; 609 610 /* Probe all supported sleep states. */ 611 acpi_sleep_states[ACPI_STATE_S0] = TRUE; 612 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 613 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 614 acpi_sleep_states[state] = TRUE; 615 616 /* 617 * Dispatch the default sleep state to devices. The lid switch is set 618 * to UNKNOWN by default to avoid surprising users. 619 */ 620 sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 621 ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 622 sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 623 sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 624 ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 625 sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 626 ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 627 628 /* Pick the first valid sleep state for the sleep button default. */ 629 sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 630 for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 631 if (acpi_sleep_states[state]) { 632 sc->acpi_sleep_button_sx = state; 633 break; 634 } 635 636 acpi_enable_fixed_events(sc); 637 638 /* 639 * Scan the namespace and attach/initialise children. 640 */ 641 642 /* Register our shutdown handler. */ 643 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 644 SHUTDOWN_PRI_LAST); 645 646 /* 647 * Register our acpi event handlers. 648 * XXX should be configurable eg. via userland policy manager. 649 */ 650 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 651 sc, ACPI_EVENT_PRI_LAST); 652 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 653 sc, ACPI_EVENT_PRI_LAST); 654 655 /* Flag our initial states. */ 656 sc->acpi_enabled = TRUE; 657 sc->acpi_sstate = ACPI_STATE_S0; 658 sc->acpi_sleep_disabled = TRUE; 659 660 /* Create the control device */ 661 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 662 "acpi"); 663 sc->acpi_dev_t->si_drv1 = sc; 664 665 if ((error = acpi_machdep_init(dev))) 666 goto out; 667 668 /* Register ACPI again to pass the correct argument of pm_func. */ 669 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 670 671 if (!acpi_disabled("bus")) 672 acpi_probe_children(dev); 673 674 /* Update all GPEs and enable runtime GPEs. */ 675 status = AcpiUpdateAllGpes(); 676 if (ACPI_FAILURE(status)) 677 device_printf(dev, "Could not update all GPEs: %s\n", 678 AcpiFormatException(status)); 679 680 /* Allow sleep request after a while. */ 681 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 682 683 error = 0; 684 685 out: 686 return_VALUE (error); 687} 688 689static void 690acpi_set_power_children(device_t dev, int state) 691{ 692 device_t child, parent; 693 device_t *devlist; 694 struct pci_devinfo *dinfo; 695 int dstate, i, numdevs; 696 697 if (device_get_children(dev, &devlist, &numdevs) != 0) 698 return; 699 700 /* 701 * Retrieve and set D-state for the sleep state if _SxD is present. 702 * Skip children who aren't attached since they are handled separately. 703 */ 704 parent = device_get_parent(dev); 705 for (i = 0; i < numdevs; i++) { 706 child = devlist[i]; 707 dinfo = device_get_ivars(child); 708 dstate = state; 709 if (device_is_attached(child) && 710 acpi_device_pwr_for_sleep(parent, dev, &dstate) == 0) 711 acpi_set_powerstate(child, dstate); 712 } 713 free(devlist, M_TEMP); 714} 715 716static int 717acpi_suspend(device_t dev) 718{ 719 int error; 720 721 GIANT_REQUIRED; 722 723 error = bus_generic_suspend(dev); 724 if (error == 0) 725 acpi_set_power_children(dev, ACPI_STATE_D3); 726 727 return (error); 728} 729 730static int 731acpi_resume(device_t dev) 732{ 733 734 GIANT_REQUIRED; 735 736 acpi_set_power_children(dev, ACPI_STATE_D0); 737 738 return (bus_generic_resume(dev)); 739} 740 741static int 742acpi_shutdown(device_t dev) 743{ 744 745 GIANT_REQUIRED; 746 747 /* Allow children to shutdown first. */ 748 bus_generic_shutdown(dev); 749 750 /* 751 * Enable any GPEs that are able to power-on the system (i.e., RTC). 752 * Also, disable any that are not valid for this state (most). 753 */ 754 acpi_wake_prep_walk(ACPI_STATE_S5); 755 756 return (0); 757} 758 759/* 760 * Handle a new device being added 761 */ 762static device_t 763acpi_add_child(device_t bus, u_int order, const char *name, int unit) 764{ 765 struct acpi_device *ad; 766 device_t child; 767 768 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 769 return (NULL); 770 771 resource_list_init(&ad->ad_rl); 772 773 child = device_add_child_ordered(bus, order, name, unit); 774 if (child != NULL) 775 device_set_ivars(child, ad); 776 else 777 free(ad, M_ACPIDEV); 778 return (child); 779} 780 781static int 782acpi_print_child(device_t bus, device_t child) 783{ 784 struct acpi_device *adev = device_get_ivars(child); 785 struct resource_list *rl = &adev->ad_rl; 786 int retval = 0; 787 788 retval += bus_print_child_header(bus, child); 789 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 790 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 791 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 792 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 793 if (device_get_flags(child)) 794 retval += printf(" flags %#x", device_get_flags(child)); 795 retval += bus_print_child_footer(bus, child); 796 797 return (retval); 798} 799 800/* 801 * If this device is an ACPI child but no one claimed it, attempt 802 * to power it off. We'll power it back up when a driver is added. 803 * 804 * XXX Disabled for now since many necessary devices (like fdc and 805 * ATA) don't claim the devices we created for them but still expect 806 * them to be powered up. 807 */ 808static void 809acpi_probe_nomatch(device_t bus, device_t child) 810{ 811#ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 812 acpi_set_powerstate(child, ACPI_STATE_D3); 813#endif 814} 815 816/* 817 * If a new driver has a chance to probe a child, first power it up. 818 * 819 * XXX Disabled for now (see acpi_probe_nomatch for details). 820 */ 821static void 822acpi_driver_added(device_t dev, driver_t *driver) 823{ 824 device_t child, *devlist; 825 int i, numdevs; 826 827 DEVICE_IDENTIFY(driver, dev); 828 if (device_get_children(dev, &devlist, &numdevs)) 829 return; 830 for (i = 0; i < numdevs; i++) { 831 child = devlist[i]; 832 if (device_get_state(child) == DS_NOTPRESENT) { 833#ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 834 acpi_set_powerstate(child, ACPI_STATE_D0); 835 if (device_probe_and_attach(child) != 0) 836 acpi_set_powerstate(child, ACPI_STATE_D3); 837#else 838 device_probe_and_attach(child); 839#endif 840 } 841 } 842 free(devlist, M_TEMP); 843} 844 845/* Location hint for devctl(8) */ 846static int 847acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 848 size_t buflen) 849{ 850 struct acpi_device *dinfo = device_get_ivars(child); 851 852 if (dinfo->ad_handle) 853 snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 854 else 855 snprintf(buf, buflen, "unknown"); 856 return (0); 857} 858 859/* PnP information for devctl(8) */ 860static int 861acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 862 size_t buflen) 863{ 864 struct acpi_device *dinfo = device_get_ivars(child); 865 ACPI_DEVICE_INFO *adinfo; 866 867 if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 868 snprintf(buf, buflen, "unknown"); 869 return (0); 870 } 871 872 snprintf(buf, buflen, "_HID=%s _UID=%lu", 873 (adinfo->Valid & ACPI_VALID_HID) ? 874 adinfo->HardwareId.String : "none", 875 (adinfo->Valid & ACPI_VALID_UID) ? 876 strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 877 AcpiOsFree(adinfo); 878 879 return (0); 880} 881 882/* 883 * Handle per-device ivars 884 */ 885static int 886acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 887{ 888 struct acpi_device *ad; 889 890 if ((ad = device_get_ivars(child)) == NULL) { 891 device_printf(child, "device has no ivars\n"); 892 return (ENOENT); 893 } 894 895 /* ACPI and ISA compatibility ivars */ 896 switch(index) { 897 case ACPI_IVAR_HANDLE: 898 *(ACPI_HANDLE *)result = ad->ad_handle; 899 break; 900 case ACPI_IVAR_PRIVATE: 901 *(void **)result = ad->ad_private; 902 break; 903 case ACPI_IVAR_FLAGS: 904 *(int *)result = ad->ad_flags; 905 break; 906 case ISA_IVAR_VENDORID: 907 case ISA_IVAR_SERIAL: 908 case ISA_IVAR_COMPATID: 909 *(int *)result = -1; 910 break; 911 case ISA_IVAR_LOGICALID: 912 *(int *)result = acpi_isa_get_logicalid(child); 913 break; 914 default: 915 return (ENOENT); 916 } 917 918 return (0); 919} 920 921static int 922acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 923{ 924 struct acpi_device *ad; 925 926 if ((ad = device_get_ivars(child)) == NULL) { 927 device_printf(child, "device has no ivars\n"); 928 return (ENOENT); 929 } 930 931 switch(index) { 932 case ACPI_IVAR_HANDLE: 933 ad->ad_handle = (ACPI_HANDLE)value; 934 break; 935 case ACPI_IVAR_PRIVATE: 936 ad->ad_private = (void *)value; 937 break; 938 case ACPI_IVAR_FLAGS: 939 ad->ad_flags = (int)value; 940 break; 941 default: 942 panic("bad ivar write request (%d)", index); 943 return (ENOENT); 944 } 945 946 return (0); 947} 948 949/* 950 * Handle child resource allocation/removal 951 */ 952static struct resource_list * 953acpi_get_rlist(device_t dev, device_t child) 954{ 955 struct acpi_device *ad; 956 957 ad = device_get_ivars(child); 958 return (&ad->ad_rl); 959} 960 961static int 962acpi_match_resource_hint(device_t dev, int type, long value) 963{ 964 struct acpi_device *ad = device_get_ivars(dev); 965 struct resource_list *rl = &ad->ad_rl; 966 struct resource_list_entry *rle; 967 968 STAILQ_FOREACH(rle, rl, link) { 969 if (rle->type != type) 970 continue; 971 if (rle->start <= value && rle->end >= value) 972 return (1); 973 } 974 return (0); 975} 976 977/* 978 * Wire device unit numbers based on resource matches in hints. 979 */ 980static void 981acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 982 int *unitp) 983{ 984 const char *s; 985 long value; 986 int line, matches, unit; 987 988 /* 989 * Iterate over all the hints for the devices with the specified 990 * name to see if one's resources are a subset of this device. 991 */ 992 line = 0; 993 for (;;) { 994 if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 995 break; 996 997 /* Must have an "at" for acpi or isa. */ 998 resource_string_value(name, unit, "at", &s); 999 if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 1000 strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 1001 continue; 1002 1003 /* 1004 * Check for matching resources. We must have at least one match. 1005 * Since I/O and memory resources cannot be shared, if we get a 1006 * match on either of those, ignore any mismatches in IRQs or DRQs. 1007 * 1008 * XXX: We may want to revisit this to be more lenient and wire 1009 * as long as it gets one match. 1010 */ 1011 matches = 0; 1012 if (resource_long_value(name, unit, "port", &value) == 0) { 1013 /* 1014 * Floppy drive controllers are notorious for having a 1015 * wide variety of resources not all of which include the 1016 * first port that is specified by the hint (typically 1017 * 0x3f0) (see the comment above fdc_isa_alloc_resources() 1018 * in fdc_isa.c). However, they do all seem to include 1019 * port + 2 (e.g. 0x3f2) so for a floppy device, look for 1020 * 'value + 2' in the port resources instead of the hint 1021 * value. 1022 */ 1023 if (strcmp(name, "fdc") == 0) 1024 value += 2; 1025 if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 1026 matches++; 1027 else 1028 continue; 1029 } 1030 if (resource_long_value(name, unit, "maddr", &value) == 0) { 1031 if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 1032 matches++; 1033 else 1034 continue; 1035 } 1036 if (matches > 0) 1037 goto matched; 1038 if (resource_long_value(name, unit, "irq", &value) == 0) { 1039 if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 1040 matches++; 1041 else 1042 continue; 1043 } 1044 if (resource_long_value(name, unit, "drq", &value) == 0) { 1045 if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 1046 matches++; 1047 else 1048 continue; 1049 } 1050 1051 matched: 1052 if (matches > 0) { 1053 /* We have a winner! */ 1054 *unitp = unit; 1055 break; 1056 } 1057 } 1058} 1059 1060/* 1061 * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1062 * duplicates, we merge any in the sysresource attach routine. 1063 */ 1064static int 1065acpi_sysres_alloc(device_t dev) 1066{ 1067 struct resource *res; 1068 struct resource_list *rl; 1069 struct resource_list_entry *rle; 1070 struct rman *rm; 1071 char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1072 device_t *children; 1073 int child_count, i; 1074 1075 /* 1076 * Probe/attach any sysresource devices. This would be unnecessary if we 1077 * had multi-pass probe/attach. 1078 */ 1079 if (device_get_children(dev, &children, &child_count) != 0) 1080 return (ENXIO); 1081 for (i = 0; i < child_count; i++) { 1082 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1083 device_probe_and_attach(children[i]); 1084 } 1085 free(children, M_TEMP); 1086 1087 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1088 STAILQ_FOREACH(rle, rl, link) { 1089 if (rle->res != NULL) { 1090 device_printf(dev, "duplicate resource for %lx\n", rle->start); 1091 continue; 1092 } 1093 1094 /* Only memory and IO resources are valid here. */ 1095 switch (rle->type) { 1096 case SYS_RES_IOPORT: 1097 rm = &acpi_rman_io; 1098 break; 1099 case SYS_RES_MEMORY: 1100 rm = &acpi_rman_mem; 1101 break; 1102 default: 1103 continue; 1104 } 1105 1106 /* Pre-allocate resource and add to our rman pool. */ 1107 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1108 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1109 if (res != NULL) { 1110 rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1111 rle->res = res; 1112 } else 1113 device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 1114 rle->start, rle->count, rle->type); 1115 } 1116 return (0); 1117} 1118 1119static char *pcilink_ids[] = { "PNP0C0F", NULL }; 1120static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1121 1122/* 1123 * Reserve declared resources for devices found during attach once system 1124 * resources have been allocated. 1125 */ 1126static void 1127acpi_reserve_resources(device_t dev) 1128{ 1129 struct resource_list_entry *rle; 1130 struct resource_list *rl; 1131 struct acpi_device *ad; 1132 struct acpi_softc *sc; 1133 device_t *children; 1134 int child_count, i; 1135 1136 sc = device_get_softc(dev); 1137 if (device_get_children(dev, &children, &child_count) != 0) 1138 return; 1139 for (i = 0; i < child_count; i++) { 1140 ad = device_get_ivars(children[i]); 1141 rl = &ad->ad_rl; 1142 1143 /* Don't reserve system resources. */ 1144 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1145 continue; 1146 1147 STAILQ_FOREACH(rle, rl, link) { 1148 /* 1149 * Don't reserve IRQ resources. There are many sticky things 1150 * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET 1151 * when using legacy routing). 1152 */ 1153 if (rle->type == SYS_RES_IRQ) 1154 continue; 1155 1156 /* 1157 * Don't reserve the resource if it is already allocated. 1158 * The acpi_ec(4) driver can allocate its resources early 1159 * if ECDT is present. 1160 */ 1161 if (rle->res != NULL) 1162 continue; 1163 1164 /* 1165 * Try to reserve the resource from our parent. If this 1166 * fails because the resource is a system resource, just 1167 * let it be. The resource range is already reserved so 1168 * that other devices will not use it. If the driver 1169 * needs to allocate the resource, then 1170 * acpi_alloc_resource() will sub-alloc from the system 1171 * resource. 1172 */ 1173 resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid, 1174 rle->start, rle->end, rle->count, 0); 1175 } 1176 } 1177 free(children, M_TEMP); 1178 sc->acpi_resources_reserved = 1; 1179} 1180 1181static int 1182acpi_set_resource(device_t dev, device_t child, int type, int rid, 1183 u_long start, u_long count) 1184{ 1185 struct acpi_softc *sc = device_get_softc(dev); 1186 struct acpi_device *ad = device_get_ivars(child); 1187 struct resource_list *rl = &ad->ad_rl; 1188 u_long end; 1189 1190 /* Ignore IRQ resources for PCI link devices. */ 1191 if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL) 1192 return (0); 1193 1194 /* If the resource is already allocated, fail. */ 1195 if (resource_list_busy(rl, type, rid)) 1196 return (EBUSY); 1197 1198 /* If the resource is already reserved, release it. */ 1199 if (resource_list_reserved(rl, type, rid)) 1200 resource_list_unreserve(rl, dev, child, type, rid); 1201 1202 /* Add the resource. */ 1203 end = (start + count - 1); 1204 resource_list_add(rl, type, rid, start, end, count); 1205 1206 /* Don't reserve resources until the system resources are allocated. */ 1207 if (!sc->acpi_resources_reserved) 1208 return (0); 1209 1210 /* Don't reserve system resources. */ 1211 if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL) 1212 return (0); 1213 1214 /* 1215 * Don't reserve IRQ resources. There are many sticky things to 1216 * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when 1217 * using legacy routing). 1218 */ 1219 if (type == SYS_RES_IRQ) 1220 return (0); 1221 1222 /* 1223 * Reserve the resource. 1224 * 1225 * XXX: Ignores failure for now. Failure here is probably a 1226 * BIOS/firmware bug? 1227 */ 1228 resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0); 1229 return (0); 1230} 1231 1232static struct resource * 1233acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 1234 u_long start, u_long end, u_long count, u_int flags) 1235{ 1236 ACPI_RESOURCE ares; 1237 struct acpi_device *ad; 1238 struct resource_list_entry *rle; 1239 struct resource_list *rl; 1240 struct resource *res; 1241 struct rman *rm; 1242 int isdefault = (start == 0UL && end == ~0UL); 1243 1244 /* 1245 * First attempt at allocating the resource. For direct children, 1246 * use resource_list_alloc() to handle reserved resources. For 1247 * other devices, pass the request up to our parent. 1248 */ 1249 if (bus == device_get_parent(child)) { 1250 ad = device_get_ivars(child); 1251 rl = &ad->ad_rl; 1252 1253 /* 1254 * Simulate the behavior of the ISA bus for direct children 1255 * devices. That is, if a non-default range is specified for 1256 * a resource that doesn't exist, use bus_set_resource() to 1257 * add the resource before allocating it. Note that these 1258 * resources will not be reserved. 1259 */ 1260 if (!isdefault && resource_list_find(rl, type, *rid) == NULL) 1261 resource_list_add(rl, type, *rid, start, end, count); 1262 res = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 1263 flags); 1264 if (res != NULL && type == SYS_RES_IRQ) { 1265 /* 1266 * Since bus_config_intr() takes immediate effect, we cannot 1267 * configure the interrupt associated with a device when we 1268 * parse the resources but have to defer it until a driver 1269 * actually allocates the interrupt via bus_alloc_resource(). 1270 * 1271 * XXX: Should we handle the lookup failing? 1272 */ 1273 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 1274 acpi_config_intr(child, &ares); 1275 } 1276 1277 /* 1278 * If this is an allocation of the "default" range for a given 1279 * RID, fetch the exact bounds for this resource from the 1280 * resource list entry to try to allocate the range from the 1281 * system resource regions. 1282 */ 1283 if (res == NULL && isdefault) { 1284 rle = resource_list_find(rl, type, *rid); 1285 if (rle != NULL) { 1286 start = rle->start; 1287 end = rle->end; 1288 count = rle->count; 1289 } 1290 } 1291 } else 1292 res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1293 start, end, count, flags); 1294 if (res != NULL || start + count - 1 != end) 1295 return (res); 1296 1297 /* 1298 * If the first attempt failed and this is an allocation of a 1299 * specific range, try to satisfy the request via a suballocation 1300 * from our system resource regions. Note that we only handle 1301 * memory and I/O port system resources. 1302 */ 1303 switch (type) { 1304 case SYS_RES_IOPORT: 1305 rm = &acpi_rman_io; 1306 break; 1307 case SYS_RES_MEMORY: 1308 rm = &acpi_rman_mem; 1309 break; 1310 default: 1311 return (NULL); 1312 } 1313 1314 res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1315 child); 1316 if (res == NULL) 1317 return (NULL); 1318 1319 rman_set_rid(res, *rid); 1320 1321 /* If requested, activate the resource using the parent's method. */ 1322 if (flags & RF_ACTIVE) 1323 if (bus_activate_resource(child, type, *rid, res) != 0) { 1324 rman_release_resource(res); 1325 return (NULL); 1326 } 1327 1328 return (res); 1329} 1330 1331static int 1332acpi_is_resource_managed(int type, struct resource *r) 1333{ 1334 1335 /* We only handle memory and IO resources through rman. */ 1336 switch (type) { 1337 case SYS_RES_IOPORT: 1338 return (rman_is_region_manager(r, &acpi_rman_io)); 1339 case SYS_RES_MEMORY: 1340 return (rman_is_region_manager(r, &acpi_rman_mem)); 1341 } 1342 return (0); 1343} 1344 1345static int 1346acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 1347 u_long start, u_long end) 1348{ 1349 1350 if (acpi_is_resource_managed(type, r)) 1351 return (rman_adjust_resource(r, start, end)); 1352 return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 1353} 1354 1355static int 1356acpi_release_resource(device_t bus, device_t child, int type, int rid, 1357 struct resource *r) 1358{ 1359 int ret; 1360 1361 /* 1362 * If this resource belongs to one of our internal managers, 1363 * deactivate it and release it to the local pool. 1364 */ 1365 if (acpi_is_resource_managed(type, r)) { 1366 if (rman_get_flags(r) & RF_ACTIVE) { 1367 ret = bus_deactivate_resource(child, type, rid, r); 1368 if (ret != 0) 1369 return (ret); 1370 } 1371 return (rman_release_resource(r)); 1372 } 1373 1374 return (bus_generic_rl_release_resource(bus, child, type, rid, r)); 1375} 1376 1377static void 1378acpi_delete_resource(device_t bus, device_t child, int type, int rid) 1379{ 1380 struct resource_list *rl; 1381 1382 rl = acpi_get_rlist(bus, child); 1383 if (resource_list_busy(rl, type, rid)) { 1384 device_printf(bus, "delete_resource: Resource still owned by child" 1385 " (type=%d, rid=%d)\n", type, rid); 1386 return; 1387 } 1388 resource_list_unreserve(rl, bus, child, type, rid); 1389 resource_list_delete(rl, type, rid); 1390} 1391 1392/* Allocate an IO port or memory resource, given its GAS. */ 1393int 1394acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1395 struct resource **res, u_int flags) 1396{ 1397 int error, res_type; 1398 1399 error = ENOMEM; 1400 if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1401 return (EINVAL); 1402 1403 /* We only support memory and IO spaces. */ 1404 switch (gas->SpaceId) { 1405 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1406 res_type = SYS_RES_MEMORY; 1407 break; 1408 case ACPI_ADR_SPACE_SYSTEM_IO: 1409 res_type = SYS_RES_IOPORT; 1410 break; 1411 default: 1412 return (EOPNOTSUPP); 1413 } 1414 1415 /* 1416 * If the register width is less than 8, assume the BIOS author means 1417 * it is a bit field and just allocate a byte. 1418 */ 1419 if (gas->BitWidth && gas->BitWidth < 8) 1420 gas->BitWidth = 8; 1421 1422 /* Validate the address after we're sure we support the space. */ 1423 if (gas->Address == 0 || gas->BitWidth == 0) 1424 return (EINVAL); 1425 1426 bus_set_resource(dev, res_type, *rid, gas->Address, 1427 gas->BitWidth / 8); 1428 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1429 if (*res != NULL) { 1430 *type = res_type; 1431 error = 0; 1432 } else 1433 bus_delete_resource(dev, res_type, *rid); 1434 1435 return (error); 1436} 1437 1438/* Probe _HID and _CID for compatible ISA PNP ids. */ 1439static uint32_t 1440acpi_isa_get_logicalid(device_t dev) 1441{ 1442 ACPI_DEVICE_INFO *devinfo; 1443 ACPI_HANDLE h; 1444 uint32_t pnpid; 1445 1446 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1447 1448 /* Fetch and validate the HID. */ 1449 if ((h = acpi_get_handle(dev)) == NULL || 1450 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1451 return_VALUE (0); 1452 1453 pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 1454 devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 1455 PNP_EISAID(devinfo->HardwareId.String) : 0; 1456 AcpiOsFree(devinfo); 1457 1458 return_VALUE (pnpid); 1459} 1460 1461static int 1462acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1463{ 1464 ACPI_DEVICE_INFO *devinfo; 1465 ACPI_DEVICE_ID *ids; 1466 ACPI_HANDLE h; 1467 uint32_t *pnpid; 1468 int i, valid; 1469 1470 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1471 1472 pnpid = cids; 1473 1474 /* Fetch and validate the CID */ 1475 if ((h = acpi_get_handle(dev)) == NULL || 1476 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1477 return_VALUE (0); 1478 1479 if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 1480 AcpiOsFree(devinfo); 1481 return_VALUE (0); 1482 } 1483 1484 if (devinfo->CompatibleIdList.Count < count) 1485 count = devinfo->CompatibleIdList.Count; 1486 ids = devinfo->CompatibleIdList.Ids; 1487 for (i = 0, valid = 0; i < count; i++) 1488 if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 1489 strncmp(ids[i].String, "PNP", 3) == 0) { 1490 *pnpid++ = PNP_EISAID(ids[i].String); 1491 valid++; 1492 } 1493 AcpiOsFree(devinfo); 1494 1495 return_VALUE (valid); 1496} 1497 1498static char * 1499acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1500{ 1501 ACPI_HANDLE h; 1502 ACPI_OBJECT_TYPE t; 1503 int i; 1504 1505 h = acpi_get_handle(dev); 1506 if (ids == NULL || h == NULL) 1507 return (NULL); 1508 t = acpi_get_type(dev); 1509 if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 1510 return (NULL); 1511 1512 /* Try to match one of the array of IDs with a HID or CID. */ 1513 for (i = 0; ids[i] != NULL; i++) { 1514 if (acpi_MatchHid(h, ids[i])) 1515 return (ids[i]); 1516 } 1517 return (NULL); 1518} 1519 1520static ACPI_STATUS 1521acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1522 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1523{ 1524 ACPI_HANDLE h; 1525 1526 if (dev == NULL) 1527 h = ACPI_ROOT_OBJECT; 1528 else if ((h = acpi_get_handle(dev)) == NULL) 1529 return (AE_BAD_PARAMETER); 1530 return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1531} 1532 1533int 1534acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 1535{ 1536 struct acpi_softc *sc; 1537 ACPI_HANDLE handle; 1538 ACPI_STATUS status; 1539 char sxd[8]; 1540 1541 handle = acpi_get_handle(dev); 1542 1543 /* 1544 * XXX If we find these devices, don't try to power them down. 1545 * The serial and IRDA ports on my T23 hang the system when 1546 * set to D3 and it appears that such legacy devices may 1547 * need special handling in their drivers. 1548 */ 1549 if (dstate == NULL || handle == NULL || 1550 acpi_MatchHid(handle, "PNP0500") || 1551 acpi_MatchHid(handle, "PNP0501") || 1552 acpi_MatchHid(handle, "PNP0502") || 1553 acpi_MatchHid(handle, "PNP0510") || 1554 acpi_MatchHid(handle, "PNP0511")) 1555 return (ENXIO); 1556 1557 /* 1558 * Override next state with the value from _SxD, if present. 1559 * Note illegal _S0D is evaluated because some systems expect this. 1560 */ 1561 sc = device_get_softc(bus); 1562 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 1563 status = acpi_GetInteger(handle, sxd, dstate); 1564 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1565 device_printf(dev, "failed to get %s on %s: %s\n", sxd, 1566 acpi_name(handle), AcpiFormatException(status)); 1567 return (ENXIO); 1568 } 1569 1570 return (0); 1571} 1572 1573/* Callback arg for our implementation of walking the namespace. */ 1574struct acpi_device_scan_ctx { 1575 acpi_scan_cb_t user_fn; 1576 void *arg; 1577 ACPI_HANDLE parent; 1578}; 1579 1580static ACPI_STATUS 1581acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1582{ 1583 struct acpi_device_scan_ctx *ctx; 1584 device_t dev, old_dev; 1585 ACPI_STATUS status; 1586 ACPI_OBJECT_TYPE type; 1587 1588 /* 1589 * Skip this device if we think we'll have trouble with it or it is 1590 * the parent where the scan began. 1591 */ 1592 ctx = (struct acpi_device_scan_ctx *)arg; 1593 if (acpi_avoid(h) || h == ctx->parent) 1594 return (AE_OK); 1595 1596 /* If this is not a valid device type (e.g., a method), skip it. */ 1597 if (ACPI_FAILURE(AcpiGetType(h, &type))) 1598 return (AE_OK); 1599 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1600 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1601 return (AE_OK); 1602 1603 /* 1604 * Call the user function with the current device. If it is unchanged 1605 * afterwards, return. Otherwise, we update the handle to the new dev. 1606 */ 1607 old_dev = acpi_get_device(h); 1608 dev = old_dev; 1609 status = ctx->user_fn(h, &dev, level, ctx->arg); 1610 if (ACPI_FAILURE(status) || old_dev == dev) 1611 return (status); 1612 1613 /* Remove the old child and its connection to the handle. */ 1614 if (old_dev != NULL) { 1615 device_delete_child(device_get_parent(old_dev), old_dev); 1616 AcpiDetachData(h, acpi_fake_objhandler); 1617 } 1618 1619 /* Recreate the handle association if the user created a device. */ 1620 if (dev != NULL) 1621 AcpiAttachData(h, acpi_fake_objhandler, dev); 1622 1623 return (AE_OK); 1624} 1625 1626static ACPI_STATUS 1627acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1628 acpi_scan_cb_t user_fn, void *arg) 1629{ 1630 ACPI_HANDLE h; 1631 struct acpi_device_scan_ctx ctx; 1632 1633 if (acpi_disabled("children")) 1634 return (AE_OK); 1635 1636 if (dev == NULL) 1637 h = ACPI_ROOT_OBJECT; 1638 else if ((h = acpi_get_handle(dev)) == NULL) 1639 return (AE_BAD_PARAMETER); 1640 ctx.user_fn = user_fn; 1641 ctx.arg = arg; 1642 ctx.parent = h; 1643 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1644 acpi_device_scan_cb, NULL, &ctx, NULL)); 1645} 1646 1647/* 1648 * Even though ACPI devices are not PCI, we use the PCI approach for setting 1649 * device power states since it's close enough to ACPI. 1650 */ 1651static int 1652acpi_set_powerstate(device_t child, int state) 1653{ 1654 ACPI_HANDLE h; 1655 ACPI_STATUS status; 1656 1657 h = acpi_get_handle(child); 1658 if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 1659 return (EINVAL); 1660 if (h == NULL) 1661 return (0); 1662 1663 /* Ignore errors if the power methods aren't present. */ 1664 status = acpi_pwr_switch_consumer(h, state); 1665 if (ACPI_SUCCESS(status)) { 1666 if (bootverbose) 1667 device_printf(child, "set ACPI power state D%d on %s\n", 1668 state, acpi_name(h)); 1669 } else if (status != AE_NOT_FOUND) 1670 device_printf(child, 1671 "failed to set ACPI power state D%d on %s: %s\n", state, 1672 acpi_name(h), AcpiFormatException(status)); 1673 1674 return (0); 1675} 1676 1677static int 1678acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 1679{ 1680 int result, cid_count, i; 1681 uint32_t lid, cids[8]; 1682 1683 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1684 1685 /* 1686 * ISA-style drivers attached to ACPI may persist and 1687 * probe manually if we return ENOENT. We never want 1688 * that to happen, so don't ever return it. 1689 */ 1690 result = ENXIO; 1691 1692 /* Scan the supplied IDs for a match */ 1693 lid = acpi_isa_get_logicalid(child); 1694 cid_count = acpi_isa_get_compatid(child, cids, 8); 1695 while (ids && ids->ip_id) { 1696 if (lid == ids->ip_id) { 1697 result = 0; 1698 goto out; 1699 } 1700 for (i = 0; i < cid_count; i++) { 1701 if (cids[i] == ids->ip_id) { 1702 result = 0; 1703 goto out; 1704 } 1705 } 1706 ids++; 1707 } 1708 1709 out: 1710 if (result == 0 && ids->ip_desc) 1711 device_set_desc(child, ids->ip_desc); 1712 1713 return_VALUE (result); 1714} 1715 1716#if defined(__i386__) || defined(__amd64__) 1717/* 1718 * Look for a MCFG table. If it is present, use the settings for 1719 * domain (segment) 0 to setup PCI config space access via the memory 1720 * map. 1721 */ 1722static void 1723acpi_enable_pcie(void) 1724{ 1725 ACPI_TABLE_HEADER *hdr; 1726 ACPI_MCFG_ALLOCATION *alloc, *end; 1727 ACPI_STATUS status; 1728 1729 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1730 if (ACPI_FAILURE(status)) 1731 return; 1732 1733 end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1734 alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1735 while (alloc < end) { 1736 if (alloc->PciSegment == 0) { 1737 pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1738 alloc->EndBusNumber); 1739 return; 1740 } 1741 alloc++; 1742 } 1743} 1744#endif 1745 1746/* 1747 * Scan all of the ACPI namespace and attach child devices. 1748 * 1749 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 1750 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 1751 * However, in violation of the spec, some systems place their PCI link 1752 * devices in \, so we have to walk the whole namespace. We check the 1753 * type of namespace nodes, so this should be ok. 1754 */ 1755static void 1756acpi_probe_children(device_t bus) 1757{ 1758 1759 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1760 1761 /* 1762 * Scan the namespace and insert placeholders for all the devices that 1763 * we find. We also probe/attach any early devices. 1764 * 1765 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1766 * we want to create nodes for all devices, not just those that are 1767 * currently present. (This assumes that we don't want to create/remove 1768 * devices as they appear, which might be smarter.) 1769 */ 1770 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1771 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 1772 NULL, bus, NULL); 1773 1774 /* Pre-allocate resources for our rman from any sysresource devices. */ 1775 acpi_sysres_alloc(bus); 1776 1777 /* Reserve resources already allocated to children. */ 1778 acpi_reserve_resources(bus); 1779 1780 /* Create any static children by calling device identify methods. */ 1781 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1782 bus_generic_probe(bus); 1783 1784 /* Probe/attach all children, created statically and from the namespace. */ 1785 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 1786 bus_generic_attach(bus); 1787 1788 /* Attach wake sysctls. */ 1789 acpi_wake_sysctl_walk(bus); 1790 1791 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 1792 return_VOID; 1793} 1794 1795/* 1796 * Determine the probe order for a given device. 1797 */ 1798static void 1799acpi_probe_order(ACPI_HANDLE handle, int *order) 1800{ 1801 ACPI_OBJECT_TYPE type; 1802 1803 /* 1804 * 1. CPUs 1805 * 2. I/O port and memory system resource holders 1806 * 3. Embedded controllers (to handle early accesses) 1807 * 4. PCI Link Devices 1808 */ 1809 AcpiGetType(handle, &type); 1810 if (type == ACPI_TYPE_PROCESSOR) 1811 *order = 1; 1812 else if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) 1813 *order = 2; 1814 else if (acpi_MatchHid(handle, "PNP0C09")) 1815 *order = 3; 1816 else if (acpi_MatchHid(handle, "PNP0C0F")) 1817 *order = 4; 1818} 1819 1820/* 1821 * Evaluate a child device and determine whether we might attach a device to 1822 * it. 1823 */ 1824static ACPI_STATUS 1825acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 1826{ 1827 struct acpi_prw_data prw; 1828 ACPI_OBJECT_TYPE type; 1829 ACPI_HANDLE h; 1830 device_t bus, child; 1831 char *handle_str; 1832 int order; 1833 1834 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1835 1836 if (acpi_disabled("children")) 1837 return_ACPI_STATUS (AE_OK); 1838 1839 /* Skip this device if we think we'll have trouble with it. */ 1840 if (acpi_avoid(handle)) 1841 return_ACPI_STATUS (AE_OK); 1842 1843 bus = (device_t)context; 1844 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1845 handle_str = acpi_name(handle); 1846 switch (type) { 1847 case ACPI_TYPE_DEVICE: 1848 /* 1849 * Since we scan from \, be sure to skip system scope objects. 1850 * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around 1851 * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run 1852 * during the intialization and \_TZ_ is to support Notify() on it. 1853 */ 1854 if (strcmp(handle_str, "\\_SB_") == 0 || 1855 strcmp(handle_str, "\\_TZ_") == 0) 1856 break; 1857 if (acpi_parse_prw(handle, &prw) == 0) 1858 AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit); 1859 1860 /* 1861 * Ignore devices that do not have a _HID or _CID. They should 1862 * be discovered by other buses (e.g. the PCI bus driver). 1863 */ 1864 if (!acpi_has_hid(handle)) 1865 break; 1866 /* FALLTHROUGH */ 1867 case ACPI_TYPE_PROCESSOR: 1868 case ACPI_TYPE_THERMAL: 1869 case ACPI_TYPE_POWER: 1870 /* 1871 * Create a placeholder device for this node. Sort the 1872 * placeholder so that the probe/attach passes will run 1873 * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1874 * are reserved for special objects (i.e., system 1875 * resources). 1876 */ 1877 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1878 order = level * 10 + 100; 1879 acpi_probe_order(handle, &order); 1880 child = BUS_ADD_CHILD(bus, order, NULL, -1); 1881 if (child == NULL) 1882 break; 1883 1884 /* Associate the handle with the device_t and vice versa. */ 1885 acpi_set_handle(child, handle); 1886 AcpiAttachData(handle, acpi_fake_objhandler, child); 1887 1888 /* 1889 * Check that the device is present. If it's not present, 1890 * leave it disabled (so that we have a device_t attached to 1891 * the handle, but we don't probe it). 1892 * 1893 * XXX PCI link devices sometimes report "present" but not 1894 * "functional" (i.e. if disabled). Go ahead and probe them 1895 * anyway since we may enable them later. 1896 */ 1897 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1898 /* Never disable PCI link devices. */ 1899 if (acpi_MatchHid(handle, "PNP0C0F")) 1900 break; 1901 /* 1902 * Docking stations should remain enabled since the system 1903 * may be undocked at boot. 1904 */ 1905 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1906 break; 1907 1908 device_disable(child); 1909 break; 1910 } 1911 1912 /* 1913 * Get the device's resource settings and attach them. 1914 * Note that if the device has _PRS but no _CRS, we need 1915 * to decide when it's appropriate to try to configure the 1916 * device. Ignore the return value here; it's OK for the 1917 * device not to have any resources. 1918 */ 1919 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1920 break; 1921 } 1922 } 1923 1924 return_ACPI_STATUS (AE_OK); 1925} 1926 1927/* 1928 * AcpiAttachData() requires an object handler but never uses it. This is a 1929 * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 1930 */ 1931void 1932acpi_fake_objhandler(ACPI_HANDLE h, void *data) 1933{ 1934} 1935 1936static void 1937acpi_shutdown_final(void *arg, int howto) 1938{ 1939 struct acpi_softc *sc = (struct acpi_softc *)arg; 1940 ACPI_STATUS status; 1941 1942 /* 1943 * XXX Shutdown code should only run on the BSP (cpuid 0). 1944 * Some chipsets do not power off the system correctly if called from 1945 * an AP. 1946 */ 1947 if ((howto & RB_POWEROFF) != 0) { 1948 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1949 if (ACPI_FAILURE(status)) { 1950 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1951 AcpiFormatException(status)); 1952 return; 1953 } 1954 device_printf(sc->acpi_dev, "Powering system off\n"); 1955 ACPI_DISABLE_IRQS(); 1956 status = AcpiEnterSleepState(ACPI_STATE_S5); 1957 if (ACPI_FAILURE(status)) 1958 device_printf(sc->acpi_dev, "power-off failed - %s\n", 1959 AcpiFormatException(status)); 1960 else { 1961 DELAY(1000000); 1962 device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 1963 } 1964 } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) { 1965 /* Reboot using the reset register. */ 1966 status = AcpiReset(); 1967 if (ACPI_SUCCESS(status)) { 1968 DELAY(1000000); 1969 device_printf(sc->acpi_dev, "reset failed - timeout\n"); 1970 } else if (status != AE_NOT_EXIST) 1971 device_printf(sc->acpi_dev, "reset failed - %s\n", 1972 AcpiFormatException(status)); 1973 } else if (sc->acpi_do_disable && panicstr == NULL) { 1974 /* 1975 * Only disable ACPI if the user requested. On some systems, writing 1976 * the disable value to SMI_CMD hangs the system. 1977 */ 1978 device_printf(sc->acpi_dev, "Shutting down\n"); 1979 AcpiTerminate(); 1980 } 1981} 1982 1983static void 1984acpi_enable_fixed_events(struct acpi_softc *sc) 1985{ 1986 static int first_time = 1; 1987 1988 /* Enable and clear fixed events and install handlers. */ 1989 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 1990 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 1991 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1992 acpi_event_power_button_sleep, sc); 1993 if (first_time) 1994 device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 1995 } 1996 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 1997 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 1998 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 1999 acpi_event_sleep_button_sleep, sc); 2000 if (first_time) 2001 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 2002 } 2003 2004 first_time = 0; 2005} 2006 2007/* 2008 * Returns true if the device is actually present and should 2009 * be attached to. This requires the present, enabled, UI-visible 2010 * and diagnostics-passed bits to be set. 2011 */ 2012BOOLEAN 2013acpi_DeviceIsPresent(device_t dev) 2014{ 2015 ACPI_DEVICE_INFO *devinfo; 2016 ACPI_HANDLE h; 2017 BOOLEAN present; 2018 2019 if ((h = acpi_get_handle(dev)) == NULL || 2020 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2021 return (FALSE); 2022 2023 /* If no _STA method, must be present */ 2024 present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 2025 ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2026 2027 AcpiOsFree(devinfo); 2028 return (present); 2029} 2030 2031/* 2032 * Returns true if the battery is actually present and inserted. 2033 */ 2034BOOLEAN 2035acpi_BatteryIsPresent(device_t dev) 2036{ 2037 ACPI_DEVICE_INFO *devinfo; 2038 ACPI_HANDLE h; 2039 BOOLEAN present; 2040 2041 if ((h = acpi_get_handle(dev)) == NULL || 2042 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2043 return (FALSE); 2044 2045 /* If no _STA method, must be present */ 2046 present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 2047 ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2048 2049 AcpiOsFree(devinfo); 2050 return (present); 2051} 2052 2053/* 2054 * Returns true if a device has at least one valid device ID. 2055 */ 2056static BOOLEAN 2057acpi_has_hid(ACPI_HANDLE h) 2058{ 2059 ACPI_DEVICE_INFO *devinfo; 2060 BOOLEAN ret; 2061 2062 if (h == NULL || 2063 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2064 return (FALSE); 2065 2066 ret = FALSE; 2067 if ((devinfo->Valid & ACPI_VALID_HID) != 0) 2068 ret = TRUE; 2069 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2070 if (devinfo->CompatibleIdList.Count > 0) 2071 ret = TRUE; 2072 2073 AcpiOsFree(devinfo); 2074 return (ret); 2075} 2076 2077/* 2078 * Match a HID string against a handle 2079 */ 2080BOOLEAN 2081acpi_MatchHid(ACPI_HANDLE h, const char *hid) 2082{ 2083 ACPI_DEVICE_INFO *devinfo; 2084 BOOLEAN ret; 2085 int i; 2086 2087 if (hid == NULL || h == NULL || 2088 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2089 return (FALSE); 2090 2091 ret = FALSE; 2092 if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 2093 strcmp(hid, devinfo->HardwareId.String) == 0) 2094 ret = TRUE; 2095 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2096 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 2097 if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 2098 ret = TRUE; 2099 break; 2100 } 2101 } 2102 2103 AcpiOsFree(devinfo); 2104 return (ret); 2105} 2106 2107/* 2108 * Return the handle of a named object within our scope, ie. that of (parent) 2109 * or one if its parents. 2110 */ 2111ACPI_STATUS 2112acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 2113{ 2114 ACPI_HANDLE r; 2115 ACPI_STATUS status; 2116 2117 /* Walk back up the tree to the root */ 2118 for (;;) { 2119 status = AcpiGetHandle(parent, path, &r); 2120 if (ACPI_SUCCESS(status)) { 2121 *result = r; 2122 return (AE_OK); 2123 } 2124 /* XXX Return error here? */ 2125 if (status != AE_NOT_FOUND) 2126 return (AE_OK); 2127 if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 2128 return (AE_NOT_FOUND); 2129 parent = r; 2130 } 2131} 2132 2133/* 2134 * Allocate a buffer with a preset data size. 2135 */ 2136ACPI_BUFFER * 2137acpi_AllocBuffer(int size) 2138{ 2139 ACPI_BUFFER *buf; 2140 2141 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2142 return (NULL); 2143 buf->Length = size; 2144 buf->Pointer = (void *)(buf + 1); 2145 return (buf); 2146} 2147 2148ACPI_STATUS 2149acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2150{ 2151 ACPI_OBJECT arg1; 2152 ACPI_OBJECT_LIST args; 2153 2154 arg1.Type = ACPI_TYPE_INTEGER; 2155 arg1.Integer.Value = number; 2156 args.Count = 1; 2157 args.Pointer = &arg1; 2158 2159 return (AcpiEvaluateObject(handle, path, &args, NULL)); 2160} 2161 2162/* 2163 * Evaluate a path that should return an integer. 2164 */ 2165ACPI_STATUS 2166acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2167{ 2168 ACPI_STATUS status; 2169 ACPI_BUFFER buf; 2170 ACPI_OBJECT param; 2171 2172 if (handle == NULL) 2173 handle = ACPI_ROOT_OBJECT; 2174 2175 /* 2176 * Assume that what we've been pointed at is an Integer object, or 2177 * a method that will return an Integer. 2178 */ 2179 buf.Pointer = ¶m; 2180 buf.Length = sizeof(param); 2181 status = AcpiEvaluateObject(handle, path, NULL, &buf); 2182 if (ACPI_SUCCESS(status)) { 2183 if (param.Type == ACPI_TYPE_INTEGER) 2184 *number = param.Integer.Value; 2185 else 2186 status = AE_TYPE; 2187 } 2188 2189 /* 2190 * In some applications, a method that's expected to return an Integer 2191 * may instead return a Buffer (probably to simplify some internal 2192 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 2193 * convert it into an Integer as best we can. 2194 * 2195 * This is a hack. 2196 */ 2197 if (status == AE_BUFFER_OVERFLOW) { 2198 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2199 status = AE_NO_MEMORY; 2200 } else { 2201 status = AcpiEvaluateObject(handle, path, NULL, &buf); 2202 if (ACPI_SUCCESS(status)) 2203 status = acpi_ConvertBufferToInteger(&buf, number); 2204 AcpiOsFree(buf.Pointer); 2205 } 2206 } 2207 return (status); 2208} 2209 2210ACPI_STATUS 2211acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2212{ 2213 ACPI_OBJECT *p; 2214 UINT8 *val; 2215 int i; 2216 2217 p = (ACPI_OBJECT *)bufp->Pointer; 2218 if (p->Type == ACPI_TYPE_INTEGER) { 2219 *number = p->Integer.Value; 2220 return (AE_OK); 2221 } 2222 if (p->Type != ACPI_TYPE_BUFFER) 2223 return (AE_TYPE); 2224 if (p->Buffer.Length > sizeof(int)) 2225 return (AE_BAD_DATA); 2226 2227 *number = 0; 2228 val = p->Buffer.Pointer; 2229 for (i = 0; i < p->Buffer.Length; i++) 2230 *number += val[i] << (i * 8); 2231 return (AE_OK); 2232} 2233 2234/* 2235 * Iterate over the elements of an a package object, calling the supplied 2236 * function for each element. 2237 * 2238 * XXX possible enhancement might be to abort traversal on error. 2239 */ 2240ACPI_STATUS 2241acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2242 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2243{ 2244 ACPI_OBJECT *comp; 2245 int i; 2246 2247 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2248 return (AE_BAD_PARAMETER); 2249 2250 /* Iterate over components */ 2251 i = 0; 2252 comp = pkg->Package.Elements; 2253 for (; i < pkg->Package.Count; i++, comp++) 2254 func(comp, arg); 2255 2256 return (AE_OK); 2257} 2258 2259/* 2260 * Find the (index)th resource object in a set. 2261 */ 2262ACPI_STATUS 2263acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 2264{ 2265 ACPI_RESOURCE *rp; 2266 int i; 2267 2268 rp = (ACPI_RESOURCE *)buf->Pointer; 2269 i = index; 2270 while (i-- > 0) { 2271 /* Range check */ 2272 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2273 return (AE_BAD_PARAMETER); 2274 2275 /* Check for terminator */ 2276 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2277 return (AE_NOT_FOUND); 2278 rp = ACPI_NEXT_RESOURCE(rp); 2279 } 2280 if (resp != NULL) 2281 *resp = rp; 2282 2283 return (AE_OK); 2284} 2285 2286/* 2287 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 2288 * 2289 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 2290 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 2291 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 2292 * resources. 2293 */ 2294#define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 2295 2296ACPI_STATUS 2297acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 2298{ 2299 ACPI_RESOURCE *rp; 2300 void *newp; 2301 2302 /* Initialise the buffer if necessary. */ 2303 if (buf->Pointer == NULL) { 2304 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 2305 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 2306 return (AE_NO_MEMORY); 2307 rp = (ACPI_RESOURCE *)buf->Pointer; 2308 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2309 rp->Length = 0; 2310 } 2311 if (res == NULL) 2312 return (AE_OK); 2313 2314 /* 2315 * Scan the current buffer looking for the terminator. 2316 * This will either find the terminator or hit the end 2317 * of the buffer and return an error. 2318 */ 2319 rp = (ACPI_RESOURCE *)buf->Pointer; 2320 for (;;) { 2321 /* Range check, don't go outside the buffer */ 2322 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2323 return (AE_BAD_PARAMETER); 2324 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2325 break; 2326 rp = ACPI_NEXT_RESOURCE(rp); 2327 } 2328 2329 /* 2330 * Check the size of the buffer and expand if required. 2331 * 2332 * Required size is: 2333 * size of existing resources before terminator + 2334 * size of new resource and header + 2335 * size of terminator. 2336 * 2337 * Note that this loop should really only run once, unless 2338 * for some reason we are stuffing a *really* huge resource. 2339 */ 2340 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2341 res->Length + ACPI_RS_SIZE_NO_DATA + 2342 ACPI_RS_SIZE_MIN) >= buf->Length) { 2343 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 2344 return (AE_NO_MEMORY); 2345 bcopy(buf->Pointer, newp, buf->Length); 2346 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2347 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 2348 AcpiOsFree(buf->Pointer); 2349 buf->Pointer = newp; 2350 buf->Length += buf->Length; 2351 } 2352 2353 /* Insert the new resource. */ 2354 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 2355 2356 /* And add the terminator. */ 2357 rp = ACPI_NEXT_RESOURCE(rp); 2358 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2359 rp->Length = 0; 2360 2361 return (AE_OK); 2362} 2363 2364/* 2365 * Set interrupt model. 2366 */ 2367ACPI_STATUS 2368acpi_SetIntrModel(int model) 2369{ 2370 2371 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2372} 2373 2374/* 2375 * Walk subtables of a table and call a callback routine for each 2376 * subtable. The caller should provide the first subtable and a 2377 * pointer to the end of the table. This can be used to walk tables 2378 * such as MADT and SRAT that use subtable entries. 2379 */ 2380void 2381acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2382 void *arg) 2383{ 2384 ACPI_SUBTABLE_HEADER *entry; 2385 2386 for (entry = first; (void *)entry < end; ) { 2387 /* Avoid an infinite loop if we hit a bogus entry. */ 2388 if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2389 return; 2390 2391 handler(entry, arg); 2392 entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2393 } 2394} 2395 2396/* 2397 * DEPRECATED. This interface has serious deficiencies and will be 2398 * removed. 2399 * 2400 * Immediately enter the sleep state. In the old model, acpiconf(8) ran 2401 * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 2402 */ 2403ACPI_STATUS 2404acpi_SetSleepState(struct acpi_softc *sc, int state) 2405{ 2406 static int once; 2407 2408 if (!once) { 2409 device_printf(sc->acpi_dev, 2410"warning: acpi_SetSleepState() deprecated, need to update your software\n"); 2411 once = 1; 2412 } 2413 return (acpi_EnterSleepState(sc, state)); 2414} 2415 2416#if defined(__amd64__) || defined(__i386__) 2417static void 2418acpi_sleep_force(void *arg) 2419{ 2420 struct acpi_softc *sc = (struct acpi_softc *)arg; 2421 2422 device_printf(sc->acpi_dev, 2423 "suspend request timed out, forcing sleep now\n"); 2424 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2425 device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 2426 sc->acpi_next_sstate); 2427} 2428#endif 2429 2430/* 2431 * Request that the system enter the given suspend state. All /dev/apm 2432 * devices and devd(8) will be notified. Userland then has a chance to 2433 * save state and acknowledge the request. The system sleeps once all 2434 * acks are in. 2435 */ 2436int 2437acpi_ReqSleepState(struct acpi_softc *sc, int state) 2438{ 2439#if defined(__amd64__) || defined(__i386__) 2440 struct apm_clone_data *clone; 2441 ACPI_STATUS status; 2442 2443 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 2444 return (EINVAL); 2445 if (!acpi_sleep_states[state]) 2446 return (EOPNOTSUPP); 2447 2448 ACPI_LOCK(acpi); 2449 2450 /* If a suspend request is already in progress, just return. */ 2451 if (sc->acpi_next_sstate != 0) { 2452 ACPI_UNLOCK(acpi); 2453 return (0); 2454 } 2455 2456 /* S5 (soft-off) should be entered directly with no waiting. */ 2457 if (state == ACPI_STATE_S5) { 2458 ACPI_UNLOCK(acpi); 2459 status = acpi_EnterSleepState(sc, state); 2460 return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2461 } 2462 2463 /* Record the pending state and notify all apm devices. */ 2464 sc->acpi_next_sstate = state; 2465 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 2466 clone->notify_status = APM_EV_NONE; 2467 if ((clone->flags & ACPI_EVF_DEVD) == 0) { 2468 selwakeuppri(&clone->sel_read, PZERO); 2469 KNOTE_LOCKED(&clone->sel_read.si_note, 0); 2470 } 2471 } 2472 2473 /* If devd(8) is not running, immediately enter the sleep state. */ 2474 if (!devctl_process_running()) { 2475 ACPI_UNLOCK(acpi); 2476 status = acpi_EnterSleepState(sc, state); 2477 return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2478 } 2479 2480 /* 2481 * Set a timeout to fire if userland doesn't ack the suspend request 2482 * in time. This way we still eventually go to sleep if we were 2483 * overheating or running low on battery, even if userland is hung. 2484 * We cancel this timeout once all userland acks are in or the 2485 * suspend request is aborted. 2486 */ 2487 callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 2488 ACPI_UNLOCK(acpi); 2489 2490 /* Now notify devd(8) also. */ 2491 acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2492 2493 return (0); 2494#else 2495 /* This platform does not support acpi suspend/resume. */ 2496 return (EOPNOTSUPP); 2497#endif 2498} 2499 2500/* 2501 * Acknowledge (or reject) a pending sleep state. The caller has 2502 * prepared for suspend and is now ready for it to proceed. If the 2503 * error argument is non-zero, it indicates suspend should be cancelled 2504 * and gives an errno value describing why. Once all votes are in, 2505 * we suspend the system. 2506 */ 2507int 2508acpi_AckSleepState(struct apm_clone_data *clone, int error) 2509{ 2510#if defined(__amd64__) || defined(__i386__) 2511 struct acpi_softc *sc; 2512 int ret, sleeping; 2513 2514 /* If no pending sleep state, return an error. */ 2515 ACPI_LOCK(acpi); 2516 sc = clone->acpi_sc; 2517 if (sc->acpi_next_sstate == 0) { 2518 ACPI_UNLOCK(acpi); 2519 return (ENXIO); 2520 } 2521 2522 /* Caller wants to abort suspend process. */ 2523 if (error) { 2524 sc->acpi_next_sstate = 0; 2525 callout_stop(&sc->susp_force_to); 2526 device_printf(sc->acpi_dev, 2527 "listener on %s cancelled the pending suspend\n", 2528 devtoname(clone->cdev)); 2529 ACPI_UNLOCK(acpi); 2530 return (0); 2531 } 2532 2533 /* 2534 * Mark this device as acking the suspend request. Then, walk through 2535 * all devices, seeing if they agree yet. We only count devices that 2536 * are writable since read-only devices couldn't ack the request. 2537 */ 2538 sleeping = TRUE; 2539 clone->notify_status = APM_EV_ACKED; 2540 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 2541 if ((clone->flags & ACPI_EVF_WRITE) != 0 && 2542 clone->notify_status != APM_EV_ACKED) { 2543 sleeping = FALSE; 2544 break; 2545 } 2546 } 2547 2548 /* If all devices have voted "yes", we will suspend now. */ 2549 if (sleeping) 2550 callout_stop(&sc->susp_force_to); 2551 ACPI_UNLOCK(acpi); 2552 ret = 0; 2553 if (sleeping) { 2554 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2555 ret = ENODEV; 2556 } 2557 return (ret); 2558#else 2559 /* This platform does not support acpi suspend/resume. */ 2560 return (EOPNOTSUPP); 2561#endif 2562} 2563 2564static void 2565acpi_sleep_enable(void *arg) 2566{ 2567 struct acpi_softc *sc = (struct acpi_softc *)arg; 2568 2569 /* Reschedule if the system is not fully up and running. */ 2570 if (!AcpiGbl_SystemAwakeAndRunning) { 2571 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2572 return; 2573 } 2574 2575 ACPI_LOCK(acpi); 2576 sc->acpi_sleep_disabled = FALSE; 2577 ACPI_UNLOCK(acpi); 2578} 2579 2580static ACPI_STATUS 2581acpi_sleep_disable(struct acpi_softc *sc) 2582{ 2583 ACPI_STATUS status; 2584 2585 /* Fail if the system is not fully up and running. */ 2586 if (!AcpiGbl_SystemAwakeAndRunning) 2587 return (AE_ERROR); 2588 2589 ACPI_LOCK(acpi); 2590 status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2591 sc->acpi_sleep_disabled = TRUE; 2592 ACPI_UNLOCK(acpi); 2593 2594 return (status); 2595} 2596 2597enum acpi_sleep_state { 2598 ACPI_SS_NONE, 2599 ACPI_SS_GPE_SET, 2600 ACPI_SS_DEV_SUSPEND, 2601 ACPI_SS_SLP_PREP, 2602 ACPI_SS_SLEPT, 2603}; 2604 2605/* 2606 * Enter the desired system sleep state. 2607 * 2608 * Currently we support S1-S5 but S4 is only S4BIOS 2609 */ 2610static ACPI_STATUS 2611acpi_EnterSleepState(struct acpi_softc *sc, int state) 2612{ 2613 ACPI_STATUS status; 2614 enum acpi_sleep_state slp_state; 2615 2616 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2617 2618 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 2619 return_ACPI_STATUS (AE_BAD_PARAMETER); 2620 if (!acpi_sleep_states[state]) { 2621 device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2622 state); 2623 return (AE_SUPPORT); 2624 } 2625 2626 /* Re-entry once we're suspending is not allowed. */ 2627 status = acpi_sleep_disable(sc); 2628 if (ACPI_FAILURE(status)) { 2629 device_printf(sc->acpi_dev, 2630 "suspend request ignored (not ready yet)\n"); 2631 return (status); 2632 } 2633 2634 if (state == ACPI_STATE_S5) { 2635 /* 2636 * Shut down cleanly and power off. This will call us back through the 2637 * shutdown handlers. 2638 */ 2639 shutdown_nice(RB_POWEROFF); 2640 return_ACPI_STATUS (AE_OK); 2641 } 2642 2643 EVENTHANDLER_INVOKE(power_suspend); 2644 2645 if (smp_started) { 2646 thread_lock(curthread); 2647 sched_bind(curthread, 0); 2648 thread_unlock(curthread); 2649 } 2650 2651 /* 2652 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2653 * drivers need this. 2654 */ 2655 mtx_lock(&Giant); 2656 2657 slp_state = ACPI_SS_NONE; 2658 2659 sc->acpi_sstate = state; 2660 2661 /* Enable any GPEs as appropriate and requested by the user. */ 2662 acpi_wake_prep_walk(state); 2663 slp_state = ACPI_SS_GPE_SET; 2664 2665 /* 2666 * Inform all devices that we are going to sleep. If at least one 2667 * device fails, DEVICE_SUSPEND() automatically resumes the tree. 2668 * 2669 * XXX Note that a better two-pass approach with a 'veto' pass 2670 * followed by a "real thing" pass would be better, but the current 2671 * bus interface does not provide for this. 2672 */ 2673 if (DEVICE_SUSPEND(root_bus) != 0) { 2674 device_printf(sc->acpi_dev, "device_suspend failed\n"); 2675 goto backout; 2676 } 2677 slp_state = ACPI_SS_DEV_SUSPEND; 2678 2679 /* If testing device suspend only, back out of everything here. */ 2680 if (acpi_susp_bounce) 2681 goto backout; 2682 2683 status = AcpiEnterSleepStatePrep(state); 2684 if (ACPI_FAILURE(status)) { 2685 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2686 AcpiFormatException(status)); 2687 goto backout; 2688 } 2689 slp_state = ACPI_SS_SLP_PREP; 2690 2691 if (sc->acpi_sleep_delay > 0) 2692 DELAY(sc->acpi_sleep_delay * 1000000); 2693 2694 if (state != ACPI_STATE_S1) { 2695 acpi_sleep_machdep(sc, state); 2696 2697 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2698 if (state == ACPI_STATE_S4) 2699 AcpiEnable(); 2700 } else { 2701 ACPI_DISABLE_IRQS(); 2702 status = AcpiEnterSleepState(state); 2703 if (ACPI_FAILURE(status)) { 2704 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2705 AcpiFormatException(status)); 2706 goto backout; 2707 } 2708 } 2709 slp_state = ACPI_SS_SLEPT; 2710 2711 /* 2712 * Back out state according to how far along we got in the suspend 2713 * process. This handles both the error and success cases. 2714 */ 2715backout: 2716 if (slp_state >= ACPI_SS_GPE_SET) { 2717 acpi_wake_prep_walk(state); 2718 sc->acpi_sstate = ACPI_STATE_S0; 2719 } 2720 if (slp_state >= ACPI_SS_SLP_PREP) 2721 AcpiLeaveSleepState(state); 2722 if (slp_state >= ACPI_SS_DEV_SUSPEND) 2723 DEVICE_RESUME(root_bus); 2724 if (slp_state >= ACPI_SS_SLEPT) 2725 acpi_enable_fixed_events(sc); 2726 sc->acpi_next_sstate = 0; 2727 2728 mtx_unlock(&Giant); 2729 2730 if (smp_started) { 2731 thread_lock(curthread); 2732 sched_unbind(curthread); 2733 thread_unlock(curthread); 2734 } 2735 2736 EVENTHANDLER_INVOKE(power_resume); 2737 2738 /* Allow another sleep request after a while. */ 2739 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2740 2741 /* Run /etc/rc.resume after we are back. */ 2742 if (devctl_process_running()) 2743 acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2744 2745 return_ACPI_STATUS (status); 2746} 2747 2748void 2749acpi_resync_clock(struct acpi_softc *sc) 2750{ 2751 2752 if (!acpi_reset_clock) 2753 return; 2754 2755 /* 2756 * Warm up timecounter again and reset system clock. 2757 */ 2758 (void)timecounter->tc_get_timecount(timecounter); 2759 (void)timecounter->tc_get_timecount(timecounter); 2760 inittodr(time_second + sc->acpi_sleep_delay); 2761} 2762 2763/* Enable or disable the device's wake GPE. */ 2764int 2765acpi_wake_set_enable(device_t dev, int enable) 2766{ 2767 struct acpi_prw_data prw; 2768 ACPI_STATUS status; 2769 int flags; 2770 2771 /* Make sure the device supports waking the system and get the GPE. */ 2772 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2773 return (ENXIO); 2774 2775 flags = acpi_get_flags(dev); 2776 if (enable) { 2777 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 2778 ACPI_GPE_ENABLE); 2779 if (ACPI_FAILURE(status)) { 2780 device_printf(dev, "enable wake failed\n"); 2781 return (ENXIO); 2782 } 2783 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2784 } else { 2785 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 2786 ACPI_GPE_DISABLE); 2787 if (ACPI_FAILURE(status)) { 2788 device_printf(dev, "disable wake failed\n"); 2789 return (ENXIO); 2790 } 2791 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2792 } 2793 2794 return (0); 2795} 2796 2797static int 2798acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2799{ 2800 struct acpi_prw_data prw; 2801 device_t dev; 2802 2803 /* Check that this is a wake-capable device and get its GPE. */ 2804 if (acpi_parse_prw(handle, &prw) != 0) 2805 return (ENXIO); 2806 dev = acpi_get_device(handle); 2807 2808 /* 2809 * The destination sleep state must be less than (i.e., higher power) 2810 * or equal to the value specified by _PRW. If this GPE cannot be 2811 * enabled for the next sleep state, then disable it. If it can and 2812 * the user requested it be enabled, turn on any required power resources 2813 * and set _PSW. 2814 */ 2815 if (sstate > prw.lowest_wake) { 2816 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 2817 if (bootverbose) 2818 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2819 acpi_name(handle), sstate); 2820 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2821 acpi_pwr_wake_enable(handle, 1); 2822 acpi_SetInteger(handle, "_PSW", 1); 2823 if (bootverbose) 2824 device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2825 acpi_name(handle), sstate); 2826 } 2827 2828 return (0); 2829} 2830 2831static int 2832acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2833{ 2834 struct acpi_prw_data prw; 2835 device_t dev; 2836 2837 /* 2838 * Check that this is a wake-capable device and get its GPE. Return 2839 * now if the user didn't enable this device for wake. 2840 */ 2841 if (acpi_parse_prw(handle, &prw) != 0) 2842 return (ENXIO); 2843 dev = acpi_get_device(handle); 2844 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2845 return (0); 2846 2847 /* 2848 * If this GPE couldn't be enabled for the previous sleep state, it was 2849 * disabled before going to sleep so re-enable it. If it was enabled, 2850 * clear _PSW and turn off any power resources it used. 2851 */ 2852 if (sstate > prw.lowest_wake) { 2853 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 2854 if (bootverbose) 2855 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2856 } else { 2857 acpi_SetInteger(handle, "_PSW", 0); 2858 acpi_pwr_wake_enable(handle, 0); 2859 if (bootverbose) 2860 device_printf(dev, "run_prep cleaned up for %s\n", 2861 acpi_name(handle)); 2862 } 2863 2864 return (0); 2865} 2866 2867static ACPI_STATUS 2868acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2869{ 2870 int sstate; 2871 2872 /* If suspending, run the sleep prep function, otherwise wake. */ 2873 sstate = *(int *)context; 2874 if (AcpiGbl_SystemAwakeAndRunning) 2875 acpi_wake_sleep_prep(handle, sstate); 2876 else 2877 acpi_wake_run_prep(handle, sstate); 2878 return (AE_OK); 2879} 2880 2881/* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2882static int 2883acpi_wake_prep_walk(int sstate) 2884{ 2885 ACPI_HANDLE sb_handle; 2886 2887 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2888 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 2889 acpi_wake_prep, NULL, &sstate, NULL); 2890 return (0); 2891} 2892 2893/* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 2894static int 2895acpi_wake_sysctl_walk(device_t dev) 2896{ 2897 int error, i, numdevs; 2898 device_t *devlist; 2899 device_t child; 2900 ACPI_STATUS status; 2901 2902 error = device_get_children(dev, &devlist, &numdevs); 2903 if (error != 0 || numdevs == 0) { 2904 if (numdevs == 0) 2905 free(devlist, M_TEMP); 2906 return (error); 2907 } 2908 for (i = 0; i < numdevs; i++) { 2909 child = devlist[i]; 2910 acpi_wake_sysctl_walk(child); 2911 if (!device_is_attached(child)) 2912 continue; 2913 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2914 if (ACPI_SUCCESS(status)) { 2915 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 2916 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 2917 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 2918 acpi_wake_set_sysctl, "I", "Device set to wake the system"); 2919 } 2920 } 2921 free(devlist, M_TEMP); 2922 2923 return (0); 2924} 2925 2926/* Enable or disable wake from userland. */ 2927static int 2928acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 2929{ 2930 int enable, error; 2931 device_t dev; 2932 2933 dev = (device_t)arg1; 2934 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 2935 2936 error = sysctl_handle_int(oidp, &enable, 0, req); 2937 if (error != 0 || req->newptr == NULL) 2938 return (error); 2939 if (enable != 0 && enable != 1) 2940 return (EINVAL); 2941 2942 return (acpi_wake_set_enable(dev, enable)); 2943} 2944 2945/* Parse a device's _PRW into a structure. */ 2946int 2947acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2948{ 2949 ACPI_STATUS status; 2950 ACPI_BUFFER prw_buffer; 2951 ACPI_OBJECT *res, *res2; 2952 int error, i, power_count; 2953 2954 if (h == NULL || prw == NULL) 2955 return (EINVAL); 2956 2957 /* 2958 * The _PRW object (7.2.9) is only required for devices that have the 2959 * ability to wake the system from a sleeping state. 2960 */ 2961 error = EINVAL; 2962 prw_buffer.Pointer = NULL; 2963 prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2964 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2965 if (ACPI_FAILURE(status)) 2966 return (ENOENT); 2967 res = (ACPI_OBJECT *)prw_buffer.Pointer; 2968 if (res == NULL) 2969 return (ENOENT); 2970 if (!ACPI_PKG_VALID(res, 2)) 2971 goto out; 2972 2973 /* 2974 * Element 1 of the _PRW object: 2975 * The lowest power system sleeping state that can be entered while still 2976 * providing wake functionality. The sleeping state being entered must 2977 * be less than (i.e., higher power) or equal to this value. 2978 */ 2979 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2980 goto out; 2981 2982 /* 2983 * Element 0 of the _PRW object: 2984 */ 2985 switch (res->Package.Elements[0].Type) { 2986 case ACPI_TYPE_INTEGER: 2987 /* 2988 * If the data type of this package element is numeric, then this 2989 * _PRW package element is the bit index in the GPEx_EN, in the 2990 * GPE blocks described in the FADT, of the enable bit that is 2991 * enabled for the wake event. 2992 */ 2993 prw->gpe_handle = NULL; 2994 prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2995 error = 0; 2996 break; 2997 case ACPI_TYPE_PACKAGE: 2998 /* 2999 * If the data type of this package element is a package, then this 3000 * _PRW package element is itself a package containing two 3001 * elements. The first is an object reference to the GPE Block 3002 * device that contains the GPE that will be triggered by the wake 3003 * event. The second element is numeric and it contains the bit 3004 * index in the GPEx_EN, in the GPE Block referenced by the 3005 * first element in the package, of the enable bit that is enabled for 3006 * the wake event. 3007 * 3008 * For example, if this field is a package then it is of the form: 3009 * Package() {\_SB.PCI0.ISA.GPE, 2} 3010 */ 3011 res2 = &res->Package.Elements[0]; 3012 if (!ACPI_PKG_VALID(res2, 2)) 3013 goto out; 3014 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 3015 if (prw->gpe_handle == NULL) 3016 goto out; 3017 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 3018 goto out; 3019 error = 0; 3020 break; 3021 default: 3022 goto out; 3023 } 3024 3025 /* Elements 2 to N of the _PRW object are power resources. */ 3026 power_count = res->Package.Count - 2; 3027 if (power_count > ACPI_PRW_MAX_POWERRES) { 3028 printf("ACPI device %s has too many power resources\n", acpi_name(h)); 3029 power_count = 0; 3030 } 3031 prw->power_res_count = power_count; 3032 for (i = 0; i < power_count; i++) 3033 prw->power_res[i] = res->Package.Elements[i]; 3034 3035out: 3036 if (prw_buffer.Pointer != NULL) 3037 AcpiOsFree(prw_buffer.Pointer); 3038 return (error); 3039} 3040 3041/* 3042 * ACPI Event Handlers 3043 */ 3044 3045/* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 3046 3047static void 3048acpi_system_eventhandler_sleep(void *arg, int state) 3049{ 3050 struct acpi_softc *sc = (struct acpi_softc *)arg; 3051 int ret; 3052 3053 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 3054 3055 /* Check if button action is disabled or unknown. */ 3056 if (state == ACPI_STATE_UNKNOWN) 3057 return; 3058 3059 /* Request that the system prepare to enter the given suspend state. */ 3060 ret = acpi_ReqSleepState(sc, state); 3061 if (ret != 0) 3062 device_printf(sc->acpi_dev, 3063 "request to enter state S%d failed (err %d)\n", state, ret); 3064 3065 return_VOID; 3066} 3067 3068static void 3069acpi_system_eventhandler_wakeup(void *arg, int state) 3070{ 3071 3072 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 3073 3074 /* Currently, nothing to do for wakeup. */ 3075 3076 return_VOID; 3077} 3078 3079/* 3080 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 3081 */ 3082static void 3083acpi_invoke_sleep_eventhandler(void *context) 3084{ 3085 3086 EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context); 3087} 3088 3089static void 3090acpi_invoke_wake_eventhandler(void *context) 3091{ 3092 3093 EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context); 3094} 3095 3096UINT32 3097acpi_event_power_button_sleep(void *context) 3098{ 3099 struct acpi_softc *sc = (struct acpi_softc *)context; 3100 3101 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3102 3103 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3104 acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx))) 3105 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3106 return_VALUE (ACPI_INTERRUPT_HANDLED); 3107} 3108 3109UINT32 3110acpi_event_power_button_wake(void *context) 3111{ 3112 struct acpi_softc *sc = (struct acpi_softc *)context; 3113 3114 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3115 3116 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3117 acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx))) 3118 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3119 return_VALUE (ACPI_INTERRUPT_HANDLED); 3120} 3121 3122UINT32 3123acpi_event_sleep_button_sleep(void *context) 3124{ 3125 struct acpi_softc *sc = (struct acpi_softc *)context; 3126 3127 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3128 3129 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3130 acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx))) 3131 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3132 return_VALUE (ACPI_INTERRUPT_HANDLED); 3133} 3134 3135UINT32 3136acpi_event_sleep_button_wake(void *context) 3137{ 3138 struct acpi_softc *sc = (struct acpi_softc *)context; 3139 3140 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3141 3142 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3143 acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx))) 3144 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3145 return_VALUE (ACPI_INTERRUPT_HANDLED); 3146} 3147 3148/* 3149 * XXX This static buffer is suboptimal. There is no locking so only 3150 * use this for single-threaded callers. 3151 */ 3152char * 3153acpi_name(ACPI_HANDLE handle) 3154{ 3155 ACPI_BUFFER buf; 3156 static char data[256]; 3157 3158 buf.Length = sizeof(data); 3159 buf.Pointer = data; 3160 3161 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3162 return (data); 3163 return ("(unknown)"); 3164} 3165 3166/* 3167 * Debugging/bug-avoidance. Avoid trying to fetch info on various 3168 * parts of the namespace. 3169 */ 3170int 3171acpi_avoid(ACPI_HANDLE handle) 3172{ 3173 char *cp, *env, *np; 3174 int len; 3175 3176 np = acpi_name(handle); 3177 if (*np == '\\') 3178 np++; 3179 if ((env = getenv("debug.acpi.avoid")) == NULL) 3180 return (0); 3181 3182 /* Scan the avoid list checking for a match */ 3183 cp = env; 3184 for (;;) { 3185 while (*cp != 0 && isspace(*cp)) 3186 cp++; 3187 if (*cp == 0) 3188 break; 3189 len = 0; 3190 while (cp[len] != 0 && !isspace(cp[len])) 3191 len++; 3192 if (!strncmp(cp, np, len)) { 3193 freeenv(env); 3194 return(1); 3195 } 3196 cp += len; 3197 } 3198 freeenv(env); 3199 3200 return (0); 3201} 3202 3203/* 3204 * Debugging/bug-avoidance. Disable ACPI subsystem components. 3205 */ 3206int 3207acpi_disabled(char *subsys) 3208{ 3209 char *cp, *env; 3210 int len; 3211 3212 if ((env = getenv("debug.acpi.disabled")) == NULL) 3213 return (0); 3214 if (strcmp(env, "all") == 0) { 3215 freeenv(env); 3216 return (1); 3217 } 3218 3219 /* Scan the disable list, checking for a match. */ 3220 cp = env; 3221 for (;;) { 3222 while (*cp != '\0' && isspace(*cp)) 3223 cp++; 3224 if (*cp == '\0') 3225 break; 3226 len = 0; 3227 while (cp[len] != '\0' && !isspace(cp[len])) 3228 len++; 3229 if (strncmp(cp, subsys, len) == 0) { 3230 freeenv(env); 3231 return (1); 3232 } 3233 cp += len; 3234 } 3235 freeenv(env); 3236 3237 return (0); 3238} 3239 3240/* 3241 * Control interface. 3242 * 3243 * We multiplex ioctls for all participating ACPI devices here. Individual 3244 * drivers wanting to be accessible via /dev/acpi should use the 3245 * register/deregister interface to make their handlers visible. 3246 */ 3247struct acpi_ioctl_hook 3248{ 3249 TAILQ_ENTRY(acpi_ioctl_hook) link; 3250 u_long cmd; 3251 acpi_ioctl_fn fn; 3252 void *arg; 3253}; 3254 3255static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 3256static int acpi_ioctl_hooks_initted; 3257 3258int 3259acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 3260{ 3261 struct acpi_ioctl_hook *hp; 3262 3263 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 3264 return (ENOMEM); 3265 hp->cmd = cmd; 3266 hp->fn = fn; 3267 hp->arg = arg; 3268 3269 ACPI_LOCK(acpi); 3270 if (acpi_ioctl_hooks_initted == 0) { 3271 TAILQ_INIT(&acpi_ioctl_hooks); 3272 acpi_ioctl_hooks_initted = 1; 3273 } 3274 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 3275 ACPI_UNLOCK(acpi); 3276 3277 return (0); 3278} 3279 3280void 3281acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 3282{ 3283 struct acpi_ioctl_hook *hp; 3284 3285 ACPI_LOCK(acpi); 3286 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 3287 if (hp->cmd == cmd && hp->fn == fn) 3288 break; 3289 3290 if (hp != NULL) { 3291 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 3292 free(hp, M_ACPIDEV); 3293 } 3294 ACPI_UNLOCK(acpi); 3295} 3296 3297static int 3298acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 3299{ 3300 return (0); 3301} 3302 3303static int 3304acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 3305{ 3306 return (0); 3307} 3308 3309static int 3310acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 3311{ 3312 struct acpi_softc *sc; 3313 struct acpi_ioctl_hook *hp; 3314 int error, state; 3315 3316 error = 0; 3317 hp = NULL; 3318 sc = dev->si_drv1; 3319 3320 /* 3321 * Scan the list of registered ioctls, looking for handlers. 3322 */ 3323 ACPI_LOCK(acpi); 3324 if (acpi_ioctl_hooks_initted) 3325 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3326 if (hp->cmd == cmd) 3327 break; 3328 } 3329 ACPI_UNLOCK(acpi); 3330 if (hp) 3331 return (hp->fn(cmd, addr, hp->arg)); 3332 3333 /* 3334 * Core ioctls are not permitted for non-writable user. 3335 * Currently, other ioctls just fetch information. 3336 * Not changing system behavior. 3337 */ 3338 if ((flag & FWRITE) == 0) 3339 return (EPERM); 3340 3341 /* Core system ioctls. */ 3342 switch (cmd) { 3343 case ACPIIO_REQSLPSTATE: 3344 state = *(int *)addr; 3345 if (state != ACPI_STATE_S5) 3346 return (acpi_ReqSleepState(sc, state)); 3347 device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3348 error = EOPNOTSUPP; 3349 break; 3350 case ACPIIO_ACKSLPSTATE: 3351 error = *(int *)addr; 3352 error = acpi_AckSleepState(sc->acpi_clone, error); 3353 break; 3354 case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 3355 state = *(int *)addr; 3356 if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3357 return (EINVAL); 3358 if (!acpi_sleep_states[state]) 3359 return (EOPNOTSUPP); 3360 if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3361 error = ENXIO; 3362 break; 3363 default: 3364 error = ENXIO; 3365 break; 3366 } 3367 3368 return (error); 3369} 3370 3371static int 3372acpi_sname2sstate(const char *sname) 3373{ 3374 int sstate; 3375 3376 if (toupper(sname[0]) == 'S') { 3377 sstate = sname[1] - '0'; 3378 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3379 sname[2] == '\0') 3380 return (sstate); 3381 } else if (strcasecmp(sname, "NONE") == 0) 3382 return (ACPI_STATE_UNKNOWN); 3383 return (-1); 3384} 3385 3386static const char * 3387acpi_sstate2sname(int sstate) 3388{ 3389 static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3390 3391 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3392 return (snames[sstate]); 3393 else if (sstate == ACPI_STATE_UNKNOWN) 3394 return ("NONE"); 3395 return (NULL); 3396} 3397 3398static int 3399acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3400{ 3401 int error; 3402 struct sbuf sb; 3403 UINT8 state; 3404 3405 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3406 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3407 if (acpi_sleep_states[state]) 3408 sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3409 sbuf_trim(&sb); 3410 sbuf_finish(&sb); 3411 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3412 sbuf_delete(&sb); 3413 return (error); 3414} 3415 3416static int 3417acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3418{ 3419 char sleep_state[10]; 3420 int error, new_state, old_state; 3421 3422 old_state = *(int *)oidp->oid_arg1; 3423 strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 3424 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 3425 if (error == 0 && req->newptr != NULL) { 3426 new_state = acpi_sname2sstate(sleep_state); 3427 if (new_state < ACPI_STATE_S1) 3428 return (EINVAL); 3429 if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3430 return (EOPNOTSUPP); 3431 if (new_state != old_state) 3432 *(int *)oidp->oid_arg1 = new_state; 3433 } 3434 return (error); 3435} 3436 3437/* Inform devctl(4) when we receive a Notify. */ 3438void 3439acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 3440{ 3441 char notify_buf[16]; 3442 ACPI_BUFFER handle_buf; 3443 ACPI_STATUS status; 3444 3445 if (subsystem == NULL) 3446 return; 3447 3448 handle_buf.Pointer = NULL; 3449 handle_buf.Length = ACPI_ALLOCATE_BUFFER; 3450 status = AcpiNsHandleToPathname(h, &handle_buf); 3451 if (ACPI_FAILURE(status)) 3452 return; 3453 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 3454 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 3455 AcpiOsFree(handle_buf.Pointer); 3456} 3457 3458#ifdef ACPI_DEBUG 3459/* 3460 * Support for parsing debug options from the kernel environment. 3461 * 3462 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 3463 * by specifying the names of the bits in the debug.acpi.layer and 3464 * debug.acpi.level environment variables. Bits may be unset by 3465 * prefixing the bit name with !. 3466 */ 3467struct debugtag 3468{ 3469 char *name; 3470 UINT32 value; 3471}; 3472 3473static struct debugtag dbg_layer[] = { 3474 {"ACPI_UTILITIES", ACPI_UTILITIES}, 3475 {"ACPI_HARDWARE", ACPI_HARDWARE}, 3476 {"ACPI_EVENTS", ACPI_EVENTS}, 3477 {"ACPI_TABLES", ACPI_TABLES}, 3478 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3479 {"ACPI_PARSER", ACPI_PARSER}, 3480 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3481 {"ACPI_EXECUTER", ACPI_EXECUTER}, 3482 {"ACPI_RESOURCES", ACPI_RESOURCES}, 3483 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 3484 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3485 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3486 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 3487 3488 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3489 {"ACPI_BATTERY", ACPI_BATTERY}, 3490 {"ACPI_BUS", ACPI_BUS}, 3491 {"ACPI_BUTTON", ACPI_BUTTON}, 3492 {"ACPI_EC", ACPI_EC}, 3493 {"ACPI_FAN", ACPI_FAN}, 3494 {"ACPI_POWERRES", ACPI_POWERRES}, 3495 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3496 {"ACPI_THERMAL", ACPI_THERMAL}, 3497 {"ACPI_TIMER", ACPI_TIMER}, 3498 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 3499 {NULL, 0} 3500}; 3501 3502static struct debugtag dbg_level[] = { 3503 {"ACPI_LV_INIT", ACPI_LV_INIT}, 3504 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 3505 {"ACPI_LV_INFO", ACPI_LV_INFO}, 3506 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3507 3508 /* Trace verbosity level 1 [Standard Trace Level] */ 3509 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 3510 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 3511 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3512 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 3513 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 3514 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 3515 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 3516 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 3517 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 3518 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 3519 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 3520 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 3521 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 3522 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3523 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3524 3525 /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3526 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3527 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3528 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3529 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 3530 {"ACPI_LV_ALL", ACPI_LV_ALL}, 3531 3532 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3533 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3534 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3535 {"ACPI_LV_IO", ACPI_LV_IO}, 3536 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3537 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3538 3539 /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 3540 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 3541 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 3542 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 3543 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 3544 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 3545 {NULL, 0} 3546}; 3547 3548static void 3549acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 3550{ 3551 char *ep; 3552 int i, l; 3553 int set; 3554 3555 while (*cp) { 3556 if (isspace(*cp)) { 3557 cp++; 3558 continue; 3559 } 3560 ep = cp; 3561 while (*ep && !isspace(*ep)) 3562 ep++; 3563 if (*cp == '!') { 3564 set = 0; 3565 cp++; 3566 if (cp == ep) 3567 continue; 3568 } else { 3569 set = 1; 3570 } 3571 l = ep - cp; 3572 for (i = 0; tag[i].name != NULL; i++) { 3573 if (!strncmp(cp, tag[i].name, l)) { 3574 if (set) 3575 *flag |= tag[i].value; 3576 else 3577 *flag &= ~tag[i].value; 3578 } 3579 } 3580 cp = ep; 3581 } 3582} 3583 3584static void 3585acpi_set_debugging(void *junk) 3586{ 3587 char *layer, *level; 3588 3589 if (cold) { 3590 AcpiDbgLayer = 0; 3591 AcpiDbgLevel = 0; 3592 } 3593 3594 layer = getenv("debug.acpi.layer"); 3595 level = getenv("debug.acpi.level"); 3596 if (layer == NULL && level == NULL) 3597 return; 3598 3599 printf("ACPI set debug"); 3600 if (layer != NULL) { 3601 if (strcmp("NONE", layer) != 0) 3602 printf(" layer '%s'", layer); 3603 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 3604 freeenv(layer); 3605 } 3606 if (level != NULL) { 3607 if (strcmp("NONE", level) != 0) 3608 printf(" level '%s'", level); 3609 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 3610 freeenv(level); 3611 } 3612 printf("\n"); 3613} 3614 3615SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3616 NULL); 3617 3618static int 3619acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 3620{ 3621 int error, *dbg; 3622 struct debugtag *tag; 3623 struct sbuf sb; 3624 3625 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 3626 return (ENOMEM); 3627 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 3628 tag = &dbg_layer[0]; 3629 dbg = &AcpiDbgLayer; 3630 } else { 3631 tag = &dbg_level[0]; 3632 dbg = &AcpiDbgLevel; 3633 } 3634 3635 /* Get old values if this is a get request. */ 3636 ACPI_SERIAL_BEGIN(acpi); 3637 if (*dbg == 0) { 3638 sbuf_cpy(&sb, "NONE"); 3639 } else if (req->newptr == NULL) { 3640 for (; tag->name != NULL; tag++) { 3641 if ((*dbg & tag->value) == tag->value) 3642 sbuf_printf(&sb, "%s ", tag->name); 3643 } 3644 } 3645 sbuf_trim(&sb); 3646 sbuf_finish(&sb); 3647 3648 /* Copy out the old values to the user. */ 3649 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 3650 sbuf_delete(&sb); 3651 3652 /* If the user is setting a string, parse it. */ 3653 if (error == 0 && req->newptr != NULL) { 3654 *dbg = 0; 3655 setenv((char *)oidp->oid_arg1, (char *)req->newptr); 3656 acpi_set_debugging(NULL); 3657 } 3658 ACPI_SERIAL_END(acpi); 3659 3660 return (error); 3661} 3662 3663SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 3664 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 3665SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 3666 "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3667#endif /* ACPI_DEBUG */ 3668 3669static int 3670acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 3671{ 3672 int error; 3673 int old; 3674 3675 old = acpi_debug_objects; 3676 error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 3677 if (error != 0 || req->newptr == NULL) 3678 return (error); 3679 if (old == acpi_debug_objects || (old && acpi_debug_objects)) 3680 return (0); 3681 3682 ACPI_SERIAL_BEGIN(acpi); 3683 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 3684 ACPI_SERIAL_END(acpi); 3685 3686 return (0); 3687} 3688 3689static int 3690acpi_parse_interfaces(char *str, struct acpi_interface *iface) 3691{ 3692 char *p; 3693 size_t len; 3694 int i, j; 3695 3696 p = str; 3697 while (isspace(*p) || *p == ',') 3698 p++; 3699 len = strlen(p); 3700 if (len == 0) 3701 return (0); 3702 p = strdup(p, M_TEMP); 3703 for (i = 0; i < len; i++) 3704 if (p[i] == ',') 3705 p[i] = '\0'; 3706 i = j = 0; 3707 while (i < len) 3708 if (isspace(p[i]) || p[i] == '\0') 3709 i++; 3710 else { 3711 i += strlen(p + i) + 1; 3712 j++; 3713 } 3714 if (j == 0) { 3715 free(p, M_TEMP); 3716 return (0); 3717 } 3718 iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK); 3719 iface->num = j; 3720 i = j = 0; 3721 while (i < len) 3722 if (isspace(p[i]) || p[i] == '\0') 3723 i++; 3724 else { 3725 iface->data[j] = p + i; 3726 i += strlen(p + i) + 1; 3727 j++; 3728 } 3729 3730 return (j); 3731} 3732 3733static void 3734acpi_free_interfaces(struct acpi_interface *iface) 3735{ 3736 3737 free(iface->data[0], M_TEMP); 3738 free(iface->data, M_TEMP); 3739} 3740 3741static void 3742acpi_reset_interfaces(device_t dev) 3743{ 3744 struct acpi_interface list; 3745 ACPI_STATUS status; 3746 int i; 3747 3748 if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) { 3749 for (i = 0; i < list.num; i++) { 3750 status = AcpiInstallInterface(list.data[i]); 3751 if (ACPI_FAILURE(status)) 3752 device_printf(dev, 3753 "failed to install _OSI(\"%s\"): %s\n", 3754 list.data[i], AcpiFormatException(status)); 3755 else if (bootverbose) 3756 device_printf(dev, "installed _OSI(\"%s\")\n", 3757 list.data[i]); 3758 } 3759 acpi_free_interfaces(&list); 3760 } 3761 if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) { 3762 for (i = 0; i < list.num; i++) { 3763 status = AcpiRemoveInterface(list.data[i]); 3764 if (ACPI_FAILURE(status)) 3765 device_printf(dev, 3766 "failed to remove _OSI(\"%s\"): %s\n", 3767 list.data[i], AcpiFormatException(status)); 3768 else if (bootverbose) 3769 device_printf(dev, "removed _OSI(\"%s\")\n", 3770 list.data[i]); 3771 } 3772 acpi_free_interfaces(&list); 3773 } 3774} 3775 3776static int 3777acpi_pm_func(u_long cmd, void *arg, ...) 3778{ 3779 int state, acpi_state; 3780 int error; 3781 struct acpi_softc *sc; 3782 va_list ap; 3783 3784 error = 0; 3785 switch (cmd) { 3786 case POWER_CMD_SUSPEND: 3787 sc = (struct acpi_softc *)arg; 3788 if (sc == NULL) { 3789 error = EINVAL; 3790 goto out; 3791 } 3792 3793 va_start(ap, arg); 3794 state = va_arg(ap, int); 3795 va_end(ap); 3796 3797 switch (state) { 3798 case POWER_SLEEP_STATE_STANDBY: 3799 acpi_state = sc->acpi_standby_sx; 3800 break; 3801 case POWER_SLEEP_STATE_SUSPEND: 3802 acpi_state = sc->acpi_suspend_sx; 3803 break; 3804 case POWER_SLEEP_STATE_HIBERNATE: 3805 acpi_state = ACPI_STATE_S4; 3806 break; 3807 default: 3808 error = EINVAL; 3809 goto out; 3810 } 3811 3812 if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 3813 error = ENXIO; 3814 break; 3815 default: 3816 error = EINVAL; 3817 goto out; 3818 } 3819 3820out: 3821 return (error); 3822} 3823 3824static void 3825acpi_pm_register(void *arg) 3826{ 3827 if (!cold || resource_disabled("acpi", 0)) 3828 return; 3829 3830 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3831} 3832 3833SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3834