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