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