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_rid(res, *rid); 1019 rman_set_bustag(res, rman_get_bustag(rle->res)); 1020 rman_set_bushandle(res, rman_get_start(res)); 1021 1022 /* If requested, activate the resource using the parent's method. */ 1023 if (flags & RF_ACTIVE) 1024 if (bus_activate_resource(child, type, *rid, res) != 0) { 1025 rman_release_resource(res); 1026 res = NULL; 1027 goto out; 1028 } 1029 } 1030 1031 if (res != NULL && device_get_parent(child) == bus) 1032 switch (type) { 1033 case SYS_RES_IRQ: 1034 /* 1035 * Since bus_config_intr() takes immediate effect, we cannot 1036 * configure the interrupt associated with a device when we 1037 * parse the resources but have to defer it until a driver 1038 * actually allocates the interrupt via bus_alloc_resource(). 1039 * 1040 * XXX: Should we handle the lookup failing? 1041 */ 1042 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 1043 acpi_config_intr(child, &ares); 1044 break; 1045 } 1046 1047 out: 1048 ACPI_SERIAL_END(acpi); 1049 return (res); 1050 } 1051 1052 static int 1053 acpi_release_resource(device_t bus, device_t child, int type, int rid, 1054 struct resource *r) 1055 { 1056 int ret; 1057 1058 ACPI_SERIAL_BEGIN(acpi); 1059 1060 /* 1061 * If we know about this address, deactivate it and release it to the 1062 * local pool. If we don't, pass this request up to the parent. 1063 */ 1064 if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) { 1065 if (rman_get_flags(r) & RF_ACTIVE) { 1066 ret = bus_deactivate_resource(child, type, rid, r); 1067 if (ret != 0) 1068 goto out; 1069 } 1070 ret = rman_release_resource(r); 1071 } else 1072 ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 1073 1074 out: 1075 ACPI_SERIAL_END(acpi); 1076 return (ret); 1077 } 1078 1079 static void 1080 acpi_delete_resource(device_t bus, device_t child, int type, int rid) 1081 { 1082 struct resource_list *rl; 1083 1084 rl = acpi_get_rlist(bus, child); 1085 resource_list_delete(rl, type, rid); 1086 } 1087 1088 /* Allocate an IO port or memory resource, given its GAS. */ 1089 int 1090 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1091 struct resource **res) 1092 { 1093 int error, res_type; 1094 1095 error = ENOMEM; 1096 if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1097 return (EINVAL); 1098 1099 /* We only support memory and IO spaces. */ 1100 switch (gas->AddressSpaceId) { 1101 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1102 res_type = SYS_RES_MEMORY; 1103 break; 1104 case ACPI_ADR_SPACE_SYSTEM_IO: 1105 res_type = SYS_RES_IOPORT; 1106 break; 1107 default: 1108 return (EOPNOTSUPP); 1109 } 1110 1111 /* 1112 * If the register width is less than 8, assume the BIOS author means 1113 * it is a bit field and just allocate a byte. 1114 */ 1115 if (gas->RegisterBitWidth && gas->RegisterBitWidth < 8) 1116 gas->RegisterBitWidth = 8; 1117 1118 /* Validate the address after we're sure we support the space. */ 1119 if (!ACPI_VALID_ADDRESS(gas->Address) || gas->RegisterBitWidth == 0) 1120 return (EINVAL); 1121 1122 bus_set_resource(dev, res_type, *rid, gas->Address, 1123 gas->RegisterBitWidth / 8); 1124 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE); 1125 if (*res != NULL) { 1126 *type = res_type; 1127 error = 0; 1128 } else 1129 bus_delete_resource(dev, res_type, *rid); 1130 1131 return (error); 1132 } 1133 1134 /* Probe _HID and _CID for compatible ISA PNP ids. */ 1135 static uint32_t 1136 acpi_isa_get_logicalid(device_t dev) 1137 { 1138 ACPI_DEVICE_INFO *devinfo; 1139 ACPI_BUFFER buf; 1140 ACPI_HANDLE h; 1141 ACPI_STATUS error; 1142 u_int32_t pnpid; 1143 1144 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1145 1146 pnpid = 0; 1147 buf.Pointer = NULL; 1148 buf.Length = ACPI_ALLOCATE_BUFFER; 1149 1150 /* Fetch and validate the HID. */ 1151 if ((h = acpi_get_handle(dev)) == NULL) 1152 goto out; 1153 error = AcpiGetObjectInfo(h, &buf); 1154 if (ACPI_FAILURE(error)) 1155 goto out; 1156 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1157 1158 if ((devinfo->Valid & ACPI_VALID_HID) != 0) 1159 pnpid = PNP_EISAID(devinfo->HardwareId.Value); 1160 1161 out: 1162 if (buf.Pointer != NULL) 1163 AcpiOsFree(buf.Pointer); 1164 return_VALUE (pnpid); 1165 } 1166 1167 static int 1168 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1169 { 1170 ACPI_DEVICE_INFO *devinfo; 1171 ACPI_BUFFER buf; 1172 ACPI_HANDLE h; 1173 ACPI_STATUS error; 1174 uint32_t *pnpid; 1175 int valid, i; 1176 1177 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1178 1179 pnpid = cids; 1180 valid = 0; 1181 buf.Pointer = NULL; 1182 buf.Length = ACPI_ALLOCATE_BUFFER; 1183 1184 /* Fetch and validate the CID */ 1185 if ((h = acpi_get_handle(dev)) == NULL) 1186 goto out; 1187 error = AcpiGetObjectInfo(h, &buf); 1188 if (ACPI_FAILURE(error)) 1189 goto out; 1190 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1191 if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1192 goto out; 1193 1194 if (devinfo->CompatibilityId.Count < count) 1195 count = devinfo->CompatibilityId.Count; 1196 for (i = 0; i < count; i++) { 1197 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 1198 continue; 1199 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 1200 valid++; 1201 } 1202 1203 out: 1204 if (buf.Pointer != NULL) 1205 AcpiOsFree(buf.Pointer); 1206 return_VALUE (valid); 1207 } 1208 1209 static char * 1210 acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1211 { 1212 ACPI_HANDLE h; 1213 int i; 1214 1215 h = acpi_get_handle(dev); 1216 if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1217 return (NULL); 1218 1219 /* Try to match one of the array of IDs with a HID or CID. */ 1220 for (i = 0; ids[i] != NULL; i++) { 1221 if (acpi_MatchHid(h, ids[i])) 1222 return (ids[i]); 1223 } 1224 return (NULL); 1225 } 1226 1227 static ACPI_STATUS 1228 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1229 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1230 { 1231 ACPI_HANDLE h; 1232 1233 if (dev == NULL) 1234 h = ACPI_ROOT_OBJECT; 1235 else if ((h = acpi_get_handle(dev)) == NULL) 1236 return (AE_BAD_PARAMETER); 1237 return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1238 } 1239 1240 static int 1241 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 1242 { 1243 struct acpi_softc *sc; 1244 ACPI_HANDLE handle; 1245 ACPI_STATUS status; 1246 char sxd[8]; 1247 int error; 1248 1249 sc = device_get_softc(bus); 1250 handle = acpi_get_handle(dev); 1251 1252 /* 1253 * XXX If we find these devices, don't try to power them down. 1254 * The serial and IRDA ports on my T23 hang the system when 1255 * set to D3 and it appears that such legacy devices may 1256 * need special handling in their drivers. 1257 */ 1258 if (handle == NULL || 1259 acpi_MatchHid(handle, "PNP0500") || 1260 acpi_MatchHid(handle, "PNP0501") || 1261 acpi_MatchHid(handle, "PNP0502") || 1262 acpi_MatchHid(handle, "PNP0510") || 1263 acpi_MatchHid(handle, "PNP0511")) 1264 return (ENXIO); 1265 1266 /* 1267 * Override next state with the value from _SxD, if present. If no 1268 * dstate argument was provided, don't fetch the return value. 1269 */ 1270 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 1271 if (dstate) 1272 status = acpi_GetInteger(handle, sxd, dstate); 1273 else 1274 status = AcpiEvaluateObject(handle, sxd, NULL, NULL); 1275 1276 switch (status) { 1277 case AE_OK: 1278 error = 0; 1279 break; 1280 case AE_NOT_FOUND: 1281 error = ESRCH; 1282 break; 1283 default: 1284 error = ENXIO; 1285 break; 1286 } 1287 1288 return (error); 1289 } 1290 1291 /* Callback arg for our implementation of walking the namespace. */ 1292 struct acpi_device_scan_ctx { 1293 acpi_scan_cb_t user_fn; 1294 void *arg; 1295 ACPI_HANDLE parent; 1296 }; 1297 1298 static ACPI_STATUS 1299 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1300 { 1301 struct acpi_device_scan_ctx *ctx; 1302 device_t dev, old_dev; 1303 ACPI_STATUS status; 1304 ACPI_OBJECT_TYPE type; 1305 1306 /* 1307 * Skip this device if we think we'll have trouble with it or it is 1308 * the parent where the scan began. 1309 */ 1310 ctx = (struct acpi_device_scan_ctx *)arg; 1311 if (acpi_avoid(h) || h == ctx->parent) 1312 return (AE_OK); 1313 1314 /* If this is not a valid device type (e.g., a method), skip it. */ 1315 if (ACPI_FAILURE(AcpiGetType(h, &type))) 1316 return (AE_OK); 1317 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1318 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1319 return (AE_OK); 1320 1321 /* 1322 * Call the user function with the current device. If it is unchanged 1323 * afterwards, return. Otherwise, we update the handle to the new dev. 1324 */ 1325 old_dev = acpi_get_device(h); 1326 dev = old_dev; 1327 status = ctx->user_fn(h, &dev, level, ctx->arg); 1328 if (ACPI_FAILURE(status) || old_dev == dev) 1329 return (status); 1330 1331 /* Remove the old child and its connection to the handle. */ 1332 if (old_dev != NULL) { 1333 device_delete_child(device_get_parent(old_dev), old_dev); 1334 AcpiDetachData(h, acpi_fake_objhandler); 1335 } 1336 1337 /* Recreate the handle association if the user created a device. */ 1338 if (dev != NULL) 1339 AcpiAttachData(h, acpi_fake_objhandler, dev); 1340 1341 return (AE_OK); 1342 } 1343 1344 static ACPI_STATUS 1345 acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1346 acpi_scan_cb_t user_fn, void *arg) 1347 { 1348 ACPI_HANDLE h; 1349 struct acpi_device_scan_ctx ctx; 1350 1351 if (acpi_disabled("children")) 1352 return (AE_OK); 1353 1354 if (dev == NULL) 1355 h = ACPI_ROOT_OBJECT; 1356 else if ((h = acpi_get_handle(dev)) == NULL) 1357 return (AE_BAD_PARAMETER); 1358 ctx.user_fn = user_fn; 1359 ctx.arg = arg; 1360 ctx.parent = h; 1361 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1362 acpi_device_scan_cb, &ctx, NULL)); 1363 } 1364 1365 /* 1366 * Even though ACPI devices are not PCI, we use the PCI approach for setting 1367 * device power states since it's close enough to ACPI. 1368 */ 1369 static int 1370 acpi_set_powerstate_method(device_t bus, device_t child, int state) 1371 { 1372 ACPI_HANDLE h; 1373 ACPI_STATUS status; 1374 int error; 1375 1376 error = 0; 1377 h = acpi_get_handle(child); 1378 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 1379 return (EINVAL); 1380 if (h == NULL) 1381 return (0); 1382 1383 /* Ignore errors if the power methods aren't present. */ 1384 status = acpi_pwr_switch_consumer(h, state); 1385 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND 1386 && status != AE_BAD_PARAMETER) 1387 device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n", 1388 state, acpi_name(h), AcpiFormatException(status)); 1389 1390 return (error); 1391 } 1392 1393 static int 1394 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 1395 { 1396 int result, cid_count, i; 1397 uint32_t lid, cids[8]; 1398 1399 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1400 1401 /* 1402 * ISA-style drivers attached to ACPI may persist and 1403 * probe manually if we return ENOENT. We never want 1404 * that to happen, so don't ever return it. 1405 */ 1406 result = ENXIO; 1407 1408 /* Scan the supplied IDs for a match */ 1409 lid = acpi_isa_get_logicalid(child); 1410 cid_count = acpi_isa_get_compatid(child, cids, 8); 1411 while (ids && ids->ip_id) { 1412 if (lid == ids->ip_id) { 1413 result = 0; 1414 goto out; 1415 } 1416 for (i = 0; i < cid_count; i++) { 1417 if (cids[i] == ids->ip_id) { 1418 result = 0; 1419 goto out; 1420 } 1421 } 1422 ids++; 1423 } 1424 1425 out: 1426 if (result == 0 && ids->ip_desc) 1427 device_set_desc(child, ids->ip_desc); 1428 1429 return_VALUE (result); 1430 } 1431 1432 /* 1433 * Scan all of the ACPI namespace and attach child devices. 1434 * 1435 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 1436 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 1437 * However, in violation of the spec, some systems place their PCI link 1438 * devices in \, so we have to walk the whole namespace. We check the 1439 * type of namespace nodes, so this should be ok. 1440 */ 1441 static void 1442 acpi_probe_children(device_t bus) 1443 { 1444 1445 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1446 1447 /* 1448 * Scan the namespace and insert placeholders for all the devices that 1449 * we find. We also probe/attach any early devices. 1450 * 1451 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1452 * we want to create nodes for all devices, not just those that are 1453 * currently present. (This assumes that we don't want to create/remove 1454 * devices as they appear, which might be smarter.) 1455 */ 1456 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1457 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 1458 bus, NULL); 1459 1460 /* Pre-allocate resources for our rman from any sysresource devices. */ 1461 acpi_sysres_alloc(bus); 1462 1463 /* Create any static children by calling device identify methods. */ 1464 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1465 bus_generic_probe(bus); 1466 1467 /* Probe/attach all children, created staticly and from the namespace. */ 1468 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 1469 bus_generic_attach(bus); 1470 1471 /* 1472 * Some of these children may have attached others as part of their attach 1473 * process (eg. the root PCI bus driver), so rescan. 1474 */ 1475 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 1476 bus_generic_attach(bus); 1477 1478 /* Attach wake sysctls. */ 1479 acpi_wake_sysctl_walk(bus); 1480 1481 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 1482 return_VOID; 1483 } 1484 1485 /* 1486 * Determine the probe order for a given device and return non-zero if it 1487 * should be attached immediately. 1488 */ 1489 static int 1490 acpi_probe_order(ACPI_HANDLE handle, int *order) 1491 { 1492 int ret; 1493 1494 /* 1495 * 1. I/O port and memory system resource holders 1496 * 2. Embedded controllers (to handle early accesses) 1497 * 3. PCI Link Devices 1498 */ 1499 ret = 0; 1500 if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) { 1501 *order = 1; 1502 ret = 1; 1503 } else if (acpi_MatchHid(handle, "PNP0C09")) { 1504 *order = 2; 1505 ret = 1; 1506 } else if (acpi_MatchHid(handle, "PNP0C0F")) { 1507 *order = 3; 1508 ret = 1; 1509 } 1510 1511 return (ret); 1512 } 1513 1514 /* 1515 * Evaluate a child device and determine whether we might attach a device to 1516 * it. 1517 */ 1518 static ACPI_STATUS 1519 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 1520 { 1521 ACPI_OBJECT_TYPE type; 1522 ACPI_HANDLE h; 1523 device_t bus, child; 1524 int order, probe_now; 1525 char *handle_str, **search; 1526 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL}; 1527 1528 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1529 1530 /* Skip this device if we think we'll have trouble with it. */ 1531 if (acpi_avoid(handle)) 1532 return_ACPI_STATUS (AE_OK); 1533 1534 bus = (device_t)context; 1535 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1536 switch (type) { 1537 case ACPI_TYPE_DEVICE: 1538 case ACPI_TYPE_PROCESSOR: 1539 case ACPI_TYPE_THERMAL: 1540 case ACPI_TYPE_POWER: 1541 if (acpi_disabled("children")) 1542 break; 1543 1544 /* 1545 * Since we scan from \, be sure to skip system scope objects. 1546 * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?) 1547 */ 1548 handle_str = acpi_name(handle); 1549 for (search = scopes; *search != NULL; search++) { 1550 if (strcmp(handle_str, *search) == 0) 1551 break; 1552 } 1553 if (*search != NULL) 1554 break; 1555 1556 /* 1557 * Create a placeholder device for this node. Sort the placeholder 1558 * so that the probe/attach passes will run breadth-first. Orders 1559 * less than 10 are reserved for special objects (i.e., system 1560 * resources). Larger values are used for all other devices. 1561 */ 1562 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1563 order = (level + 1) * 10; 1564 probe_now = acpi_probe_order(handle, &order); 1565 child = BUS_ADD_CHILD(bus, order, NULL, -1); 1566 if (child == NULL) 1567 break; 1568 1569 /* Associate the handle with the device_t and vice versa. */ 1570 acpi_set_handle(child, handle); 1571 AcpiAttachData(handle, acpi_fake_objhandler, child); 1572 1573 /* 1574 * Check that the device is present. If it's not present, 1575 * leave it disabled (so that we have a device_t attached to 1576 * the handle, but we don't probe it). 1577 * 1578 * XXX PCI link devices sometimes report "present" but not 1579 * "functional" (i.e. if disabled). Go ahead and probe them 1580 * anyway since we may enable them later. 1581 */ 1582 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1583 /* Never disable PCI link devices. */ 1584 if (acpi_MatchHid(handle, "PNP0C0F")) 1585 break; 1586 /* 1587 * Docking stations should remain enabled since the system 1588 * may be undocked at boot. 1589 */ 1590 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1591 break; 1592 1593 device_disable(child); 1594 break; 1595 } 1596 1597 /* 1598 * Get the device's resource settings and attach them. 1599 * Note that if the device has _PRS but no _CRS, we need 1600 * to decide when it's appropriate to try to configure the 1601 * device. Ignore the return value here; it's OK for the 1602 * device not to have any resources. 1603 */ 1604 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1605 1606 /* If order was overridden, probe/attach now rather than later. */ 1607 if (probe_now) 1608 device_probe_and_attach(child); 1609 break; 1610 } 1611 } 1612 1613 return_ACPI_STATUS (AE_OK); 1614 } 1615 1616 /* 1617 * AcpiAttachData() requires an object handler but never uses it. This is a 1618 * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 1619 */ 1620 void 1621 acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 1622 { 1623 } 1624 1625 static void 1626 acpi_shutdown_final(void *arg, int howto) 1627 { 1628 ACPI_STATUS status; 1629 1630 /* 1631 * XXX Shutdown code should only run on the BSP (cpuid 0). 1632 * Some chipsets do not power off the system correctly if called from 1633 * an AP. 1634 */ 1635 if ((howto & RB_POWEROFF) != 0) { 1636 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1637 if (ACPI_FAILURE(status)) { 1638 printf("AcpiEnterSleepStatePrep failed - %s\n", 1639 AcpiFormatException(status)); 1640 return; 1641 } 1642 printf("Powering system off using ACPI\n"); 1643 ACPI_DISABLE_IRQS(); 1644 status = AcpiEnterSleepState(ACPI_STATE_S5); 1645 if (ACPI_FAILURE(status)) { 1646 printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 1647 } else { 1648 DELAY(1000000); 1649 printf("ACPI power-off failed - timeout\n"); 1650 } 1651 } else if ((howto & RB_AUTOBOOT) != 0 && AcpiGbl_FADT->ResetRegSup) { 1652 status = AcpiHwLowLevelWrite( 1653 AcpiGbl_FADT->ResetRegister.RegisterBitWidth, 1654 AcpiGbl_FADT->ResetValue, &AcpiGbl_FADT->ResetRegister); 1655 if (ACPI_FAILURE(status)) { 1656 printf("ACPI reset failed - %s\n", AcpiFormatException(status)); 1657 } else { 1658 DELAY(1000000); 1659 printf("ACPI reset failed - timeout\n"); 1660 } 1661 } else if (panicstr == NULL) { 1662 printf("Shutting down ACPI\n"); 1663 AcpiTerminate(); 1664 } 1665 } 1666 1667 static void 1668 acpi_enable_fixed_events(struct acpi_softc *sc) 1669 { 1670 static int first_time = 1; 1671 1672 /* Enable and clear fixed events and install handlers. */ 1673 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 1674 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 1675 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1676 acpi_event_power_button_sleep, sc); 1677 if (first_time) 1678 device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 1679 } 1680 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 1681 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 1682 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 1683 acpi_event_sleep_button_sleep, sc); 1684 if (first_time) 1685 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 1686 } 1687 1688 first_time = 0; 1689 } 1690 1691 /* 1692 * Returns true if the device is actually present and should 1693 * be attached to. This requires the present, enabled, UI-visible 1694 * and diagnostics-passed bits to be set. 1695 */ 1696 BOOLEAN 1697 acpi_DeviceIsPresent(device_t dev) 1698 { 1699 ACPI_DEVICE_INFO *devinfo; 1700 ACPI_HANDLE h; 1701 ACPI_BUFFER buf; 1702 ACPI_STATUS error; 1703 int ret; 1704 1705 ret = FALSE; 1706 if ((h = acpi_get_handle(dev)) == NULL) 1707 return (FALSE); 1708 buf.Pointer = NULL; 1709 buf.Length = ACPI_ALLOCATE_BUFFER; 1710 error = AcpiGetObjectInfo(h, &buf); 1711 if (ACPI_FAILURE(error)) 1712 return (FALSE); 1713 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1714 1715 /* If no _STA method, must be present */ 1716 if ((devinfo->Valid & ACPI_VALID_STA) == 0) 1717 ret = TRUE; 1718 1719 /* Return true for 'present' and 'functioning' */ 1720 if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus)) 1721 ret = TRUE; 1722 1723 AcpiOsFree(buf.Pointer); 1724 return (ret); 1725 } 1726 1727 /* 1728 * Returns true if the battery is actually present and inserted. 1729 */ 1730 BOOLEAN 1731 acpi_BatteryIsPresent(device_t dev) 1732 { 1733 ACPI_DEVICE_INFO *devinfo; 1734 ACPI_HANDLE h; 1735 ACPI_BUFFER buf; 1736 ACPI_STATUS error; 1737 int ret; 1738 1739 ret = FALSE; 1740 if ((h = acpi_get_handle(dev)) == NULL) 1741 return (FALSE); 1742 buf.Pointer = NULL; 1743 buf.Length = ACPI_ALLOCATE_BUFFER; 1744 error = AcpiGetObjectInfo(h, &buf); 1745 if (ACPI_FAILURE(error)) 1746 return (FALSE); 1747 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1748 1749 /* If no _STA method, must be present */ 1750 if ((devinfo->Valid & ACPI_VALID_STA) == 0) 1751 ret = TRUE; 1752 1753 /* Return true for 'present', 'battery present', and 'functioning' */ 1754 if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus)) 1755 ret = TRUE; 1756 1757 AcpiOsFree(buf.Pointer); 1758 return (ret); 1759 } 1760 1761 /* 1762 * Match a HID string against a handle 1763 */ 1764 static BOOLEAN 1765 acpi_MatchHid(ACPI_HANDLE h, const char *hid) 1766 { 1767 ACPI_DEVICE_INFO *devinfo; 1768 ACPI_BUFFER buf; 1769 ACPI_STATUS error; 1770 int ret, i; 1771 1772 ret = FALSE; 1773 if (hid == NULL || h == NULL) 1774 return (ret); 1775 buf.Pointer = NULL; 1776 buf.Length = ACPI_ALLOCATE_BUFFER; 1777 error = AcpiGetObjectInfo(h, &buf); 1778 if (ACPI_FAILURE(error)) 1779 return (ret); 1780 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1781 1782 if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1783 strcmp(hid, devinfo->HardwareId.Value) == 0) 1784 ret = TRUE; 1785 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 1786 for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 1787 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 1788 ret = TRUE; 1789 break; 1790 } 1791 } 1792 } 1793 1794 AcpiOsFree(buf.Pointer); 1795 return (ret); 1796 } 1797 1798 /* 1799 * Return the handle of a named object within our scope, ie. that of (parent) 1800 * or one if its parents. 1801 */ 1802 ACPI_STATUS 1803 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1804 { 1805 ACPI_HANDLE r; 1806 ACPI_STATUS status; 1807 1808 /* Walk back up the tree to the root */ 1809 for (;;) { 1810 status = AcpiGetHandle(parent, path, &r); 1811 if (ACPI_SUCCESS(status)) { 1812 *result = r; 1813 return (AE_OK); 1814 } 1815 /* XXX Return error here? */ 1816 if (status != AE_NOT_FOUND) 1817 return (AE_OK); 1818 if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1819 return (AE_NOT_FOUND); 1820 parent = r; 1821 } 1822 } 1823 1824 /* Find the difference between two PM tick counts. */ 1825 uint32_t 1826 acpi_TimerDelta(uint32_t end, uint32_t start) 1827 { 1828 uint32_t delta; 1829 1830 if (end >= start) 1831 delta = end - start; 1832 else if (AcpiGbl_FADT->TmrValExt == 0) 1833 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1834 else 1835 delta = ((0xFFFFFFFF - start) + end + 1); 1836 return (delta); 1837 } 1838 1839 /* 1840 * Allocate a buffer with a preset data size. 1841 */ 1842 ACPI_BUFFER * 1843 acpi_AllocBuffer(int size) 1844 { 1845 ACPI_BUFFER *buf; 1846 1847 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1848 return (NULL); 1849 buf->Length = size; 1850 buf->Pointer = (void *)(buf + 1); 1851 return (buf); 1852 } 1853 1854 ACPI_STATUS 1855 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1856 { 1857 ACPI_OBJECT arg1; 1858 ACPI_OBJECT_LIST args; 1859 1860 arg1.Type = ACPI_TYPE_INTEGER; 1861 arg1.Integer.Value = number; 1862 args.Count = 1; 1863 args.Pointer = &arg1; 1864 1865 return (AcpiEvaluateObject(handle, path, &args, NULL)); 1866 } 1867 1868 /* 1869 * Evaluate a path that should return an integer. 1870 */ 1871 ACPI_STATUS 1872 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1873 { 1874 ACPI_STATUS status; 1875 ACPI_BUFFER buf; 1876 ACPI_OBJECT param; 1877 1878 if (handle == NULL) 1879 handle = ACPI_ROOT_OBJECT; 1880 1881 /* 1882 * Assume that what we've been pointed at is an Integer object, or 1883 * a method that will return an Integer. 1884 */ 1885 buf.Pointer = ¶m; 1886 buf.Length = sizeof(param); 1887 status = AcpiEvaluateObject(handle, path, NULL, &buf); 1888 if (ACPI_SUCCESS(status)) { 1889 if (param.Type == ACPI_TYPE_INTEGER) 1890 *number = param.Integer.Value; 1891 else 1892 status = AE_TYPE; 1893 } 1894 1895 /* 1896 * In some applications, a method that's expected to return an Integer 1897 * may instead return a Buffer (probably to simplify some internal 1898 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 1899 * convert it into an Integer as best we can. 1900 * 1901 * This is a hack. 1902 */ 1903 if (status == AE_BUFFER_OVERFLOW) { 1904 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1905 status = AE_NO_MEMORY; 1906 } else { 1907 status = AcpiEvaluateObject(handle, path, NULL, &buf); 1908 if (ACPI_SUCCESS(status)) 1909 status = acpi_ConvertBufferToInteger(&buf, number); 1910 AcpiOsFree(buf.Pointer); 1911 } 1912 } 1913 return (status); 1914 } 1915 1916 ACPI_STATUS 1917 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1918 { 1919 ACPI_OBJECT *p; 1920 UINT8 *val; 1921 int i; 1922 1923 p = (ACPI_OBJECT *)bufp->Pointer; 1924 if (p->Type == ACPI_TYPE_INTEGER) { 1925 *number = p->Integer.Value; 1926 return (AE_OK); 1927 } 1928 if (p->Type != ACPI_TYPE_BUFFER) 1929 return (AE_TYPE); 1930 if (p->Buffer.Length > sizeof(int)) 1931 return (AE_BAD_DATA); 1932 1933 *number = 0; 1934 val = p->Buffer.Pointer; 1935 for (i = 0; i < p->Buffer.Length; i++) 1936 *number += val[i] << (i * 8); 1937 return (AE_OK); 1938 } 1939 1940 /* 1941 * Iterate over the elements of an a package object, calling the supplied 1942 * function for each element. 1943 * 1944 * XXX possible enhancement might be to abort traversal on error. 1945 */ 1946 ACPI_STATUS 1947 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1948 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1949 { 1950 ACPI_OBJECT *comp; 1951 int i; 1952 1953 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1954 return (AE_BAD_PARAMETER); 1955 1956 /* Iterate over components */ 1957 i = 0; 1958 comp = pkg->Package.Elements; 1959 for (; i < pkg->Package.Count; i++, comp++) 1960 func(comp, arg); 1961 1962 return (AE_OK); 1963 } 1964 1965 /* 1966 * Find the (index)th resource object in a set. 1967 */ 1968 ACPI_STATUS 1969 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 1970 { 1971 ACPI_RESOURCE *rp; 1972 int i; 1973 1974 rp = (ACPI_RESOURCE *)buf->Pointer; 1975 i = index; 1976 while (i-- > 0) { 1977 /* Range check */ 1978 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 1979 return (AE_BAD_PARAMETER); 1980 1981 /* Check for terminator */ 1982 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 1983 return (AE_NOT_FOUND); 1984 rp = ACPI_NEXT_RESOURCE(rp); 1985 } 1986 if (resp != NULL) 1987 *resp = rp; 1988 1989 return (AE_OK); 1990 } 1991 1992 /* 1993 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 1994 * 1995 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 1996 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 1997 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 1998 * resources. 1999 */ 2000 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 2001 2002 ACPI_STATUS 2003 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 2004 { 2005 ACPI_RESOURCE *rp; 2006 void *newp; 2007 2008 /* Initialise the buffer if necessary. */ 2009 if (buf->Pointer == NULL) { 2010 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 2011 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 2012 return (AE_NO_MEMORY); 2013 rp = (ACPI_RESOURCE *)buf->Pointer; 2014 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2015 rp->Length = 0; 2016 } 2017 if (res == NULL) 2018 return (AE_OK); 2019 2020 /* 2021 * Scan the current buffer looking for the terminator. 2022 * This will either find the terminator or hit the end 2023 * of the buffer and return an error. 2024 */ 2025 rp = (ACPI_RESOURCE *)buf->Pointer; 2026 for (;;) { 2027 /* Range check, don't go outside the buffer */ 2028 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2029 return (AE_BAD_PARAMETER); 2030 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2031 break; 2032 rp = ACPI_NEXT_RESOURCE(rp); 2033 } 2034 2035 /* 2036 * Check the size of the buffer and expand if required. 2037 * 2038 * Required size is: 2039 * size of existing resources before terminator + 2040 * size of new resource and header + 2041 * size of terminator. 2042 * 2043 * Note that this loop should really only run once, unless 2044 * for some reason we are stuffing a *really* huge resource. 2045 */ 2046 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2047 res->Length + ACPI_RS_SIZE_NO_DATA + 2048 ACPI_RS_SIZE_MIN) >= buf->Length) { 2049 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 2050 return (AE_NO_MEMORY); 2051 bcopy(buf->Pointer, newp, buf->Length); 2052 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2053 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 2054 AcpiOsFree(buf->Pointer); 2055 buf->Pointer = newp; 2056 buf->Length += buf->Length; 2057 } 2058 2059 /* Insert the new resource. */ 2060 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 2061 2062 /* And add the terminator. */ 2063 rp = ACPI_NEXT_RESOURCE(rp); 2064 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2065 rp->Length = 0; 2066 2067 return (AE_OK); 2068 } 2069 2070 /* 2071 * Set interrupt model. 2072 */ 2073 ACPI_STATUS 2074 acpi_SetIntrModel(int model) 2075 { 2076 2077 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2078 } 2079 2080 static void 2081 acpi_sleep_enable(void *arg) 2082 { 2083 2084 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 2085 } 2086 2087 enum acpi_sleep_state { 2088 ACPI_SS_NONE, 2089 ACPI_SS_GPE_SET, 2090 ACPI_SS_DEV_SUSPEND, 2091 ACPI_SS_SLP_PREP, 2092 ACPI_SS_SLEPT, 2093 }; 2094 2095 /* 2096 * Set the system sleep state 2097 * 2098 * Currently we support S1-S5 but S4 is only S4BIOS 2099 */ 2100 ACPI_STATUS 2101 acpi_SetSleepState(struct acpi_softc *sc, int state) 2102 { 2103 ACPI_STATUS status; 2104 UINT8 TypeA; 2105 UINT8 TypeB; 2106 enum acpi_sleep_state slp_state; 2107 2108 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2109 2110 status = AE_OK; 2111 ACPI_LOCK(acpi); 2112 if (sc->acpi_sleep_disabled) { 2113 if (sc->acpi_sstate != ACPI_STATE_S0) 2114 status = AE_ERROR; 2115 ACPI_UNLOCK(acpi); 2116 printf("acpi: suspend request ignored (not ready yet)\n"); 2117 return (status); 2118 } 2119 sc->acpi_sleep_disabled = 1; 2120 ACPI_UNLOCK(acpi); 2121 2122 /* 2123 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2124 * drivers need this. 2125 */ 2126 mtx_lock(&Giant); 2127 slp_state = ACPI_SS_NONE; 2128 switch (state) { 2129 case ACPI_STATE_S1: 2130 case ACPI_STATE_S2: 2131 case ACPI_STATE_S3: 2132 case ACPI_STATE_S4: 2133 status = AcpiGetSleepTypeData(state, &TypeA, &TypeB); 2134 if (status == AE_NOT_FOUND) { 2135 device_printf(sc->acpi_dev, 2136 "Sleep state S%d not supported by BIOS\n", state); 2137 break; 2138 } else if (ACPI_FAILURE(status)) { 2139 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 2140 AcpiFormatException(status)); 2141 break; 2142 } 2143 2144 sc->acpi_sstate = state; 2145 2146 /* Enable any GPEs as appropriate and requested by the user. */ 2147 acpi_wake_prep_walk(state); 2148 slp_state = ACPI_SS_GPE_SET; 2149 2150 /* 2151 * Inform all devices that we are going to sleep. If at least one 2152 * device fails, DEVICE_SUSPEND() automatically resumes the tree. 2153 * 2154 * XXX Note that a better two-pass approach with a 'veto' pass 2155 * followed by a "real thing" pass would be better, but the current 2156 * bus interface does not provide for this. 2157 */ 2158 if (DEVICE_SUSPEND(root_bus) != 0) { 2159 device_printf(sc->acpi_dev, "device_suspend failed\n"); 2160 break; 2161 } 2162 slp_state = ACPI_SS_DEV_SUSPEND; 2163 2164 status = AcpiEnterSleepStatePrep(state); 2165 if (ACPI_FAILURE(status)) { 2166 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2167 AcpiFormatException(status)); 2168 break; 2169 } 2170 slp_state = ACPI_SS_SLP_PREP; 2171 2172 if (sc->acpi_sleep_delay > 0) 2173 DELAY(sc->acpi_sleep_delay * 1000000); 2174 2175 if (state != ACPI_STATE_S1) { 2176 acpi_sleep_machdep(sc, state); 2177 2178 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2179 if (state == ACPI_STATE_S4) 2180 AcpiEnable(); 2181 } else { 2182 ACPI_DISABLE_IRQS(); 2183 status = AcpiEnterSleepState(state); 2184 if (ACPI_FAILURE(status)) { 2185 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2186 AcpiFormatException(status)); 2187 break; 2188 } 2189 } 2190 slp_state = ACPI_SS_SLEPT; 2191 break; 2192 case ACPI_STATE_S5: 2193 /* 2194 * Shut down cleanly and power off. This will call us back through the 2195 * shutdown handlers. 2196 */ 2197 shutdown_nice(RB_POWEROFF); 2198 break; 2199 case ACPI_STATE_S0: 2200 default: 2201 status = AE_BAD_PARAMETER; 2202 break; 2203 } 2204 2205 /* 2206 * Back out state according to how far along we got in the suspend 2207 * process. This handles both the error and success cases. 2208 */ 2209 if (slp_state >= ACPI_SS_GPE_SET) { 2210 acpi_wake_prep_walk(state); 2211 sc->acpi_sstate = ACPI_STATE_S0; 2212 } 2213 if (slp_state >= ACPI_SS_SLP_PREP) 2214 AcpiLeaveSleepState(state); 2215 if (slp_state >= ACPI_SS_DEV_SUSPEND) 2216 DEVICE_RESUME(root_bus); 2217 if (slp_state >= ACPI_SS_SLEPT) 2218 acpi_enable_fixed_events(sc); 2219 2220 /* Allow another sleep request after a while. */ 2221 if (state != ACPI_STATE_S5) 2222 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 2223 2224 mtx_unlock(&Giant); 2225 return_ACPI_STATUS (status); 2226 } 2227 2228 /* Initialize a device's wake GPE. */ 2229 int 2230 acpi_wake_init(device_t dev, int type) 2231 { 2232 struct acpi_prw_data prw; 2233 2234 /* Evaluate _PRW to find the GPE. */ 2235 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2236 return (ENXIO); 2237 2238 /* Set the requested type for the GPE (runtime, wake, or both). */ 2239 if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 2240 device_printf(dev, "set GPE type failed\n"); 2241 return (ENXIO); 2242 } 2243 2244 return (0); 2245 } 2246 2247 /* Enable or disable the device's wake GPE. */ 2248 int 2249 acpi_wake_set_enable(device_t dev, int enable) 2250 { 2251 struct acpi_prw_data prw; 2252 ACPI_HANDLE handle; 2253 ACPI_STATUS status; 2254 int flags; 2255 2256 /* Make sure the device supports waking the system and get the GPE. */ 2257 handle = acpi_get_handle(dev); 2258 if (acpi_parse_prw(handle, &prw) != 0) 2259 return (ENXIO); 2260 2261 flags = acpi_get_flags(dev); 2262 if (enable) { 2263 status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2264 if (ACPI_FAILURE(status)) { 2265 device_printf(dev, "enable wake failed\n"); 2266 return (ENXIO); 2267 } 2268 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2269 } else { 2270 status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2271 if (ACPI_FAILURE(status)) { 2272 device_printf(dev, "disable wake failed\n"); 2273 return (ENXIO); 2274 } 2275 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2276 } 2277 2278 return (0); 2279 } 2280 2281 static int 2282 acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2283 { 2284 struct acpi_prw_data prw; 2285 device_t dev; 2286 2287 /* Check that this is a wake-capable device and get its GPE. */ 2288 if (acpi_parse_prw(handle, &prw) != 0) 2289 return (ENXIO); 2290 dev = acpi_get_device(handle); 2291 2292 /* 2293 * The destination sleep state must be less than (i.e., higher power) 2294 * or equal to the value specified by _PRW. If this GPE cannot be 2295 * enabled for the next sleep state, then disable it. If it can and 2296 * the user requested it be enabled, turn on any required power resources 2297 * and set _PSW. 2298 */ 2299 if (sstate > prw.lowest_wake) { 2300 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2301 if (bootverbose) 2302 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2303 acpi_name(handle), sstate); 2304 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2305 acpi_pwr_wake_enable(handle, 1); 2306 acpi_SetInteger(handle, "_PSW", 1); 2307 if (bootverbose) 2308 device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2309 acpi_name(handle), sstate); 2310 } 2311 2312 return (0); 2313 } 2314 2315 static int 2316 acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2317 { 2318 struct acpi_prw_data prw; 2319 device_t dev; 2320 2321 /* 2322 * Check that this is a wake-capable device and get its GPE. Return 2323 * now if the user didn't enable this device for wake. 2324 */ 2325 if (acpi_parse_prw(handle, &prw) != 0) 2326 return (ENXIO); 2327 dev = acpi_get_device(handle); 2328 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2329 return (0); 2330 2331 /* 2332 * If this GPE couldn't be enabled for the previous sleep state, it was 2333 * disabled before going to sleep so re-enable it. If it was enabled, 2334 * clear _PSW and turn off any power resources it used. 2335 */ 2336 if (sstate > prw.lowest_wake) { 2337 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2338 if (bootverbose) 2339 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2340 } else { 2341 acpi_SetInteger(handle, "_PSW", 0); 2342 acpi_pwr_wake_enable(handle, 0); 2343 if (bootverbose) 2344 device_printf(dev, "run_prep cleaned up for %s\n", 2345 acpi_name(handle)); 2346 } 2347 2348 return (0); 2349 } 2350 2351 static ACPI_STATUS 2352 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2353 { 2354 int sstate; 2355 2356 /* If suspending, run the sleep prep function, otherwise wake. */ 2357 sstate = *(int *)context; 2358 if (AcpiGbl_SystemAwakeAndRunning) 2359 acpi_wake_sleep_prep(handle, sstate); 2360 else 2361 acpi_wake_run_prep(handle, sstate); 2362 return (AE_OK); 2363 } 2364 2365 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2366 static int 2367 acpi_wake_prep_walk(int sstate) 2368 { 2369 ACPI_HANDLE sb_handle; 2370 2371 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2372 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 2373 acpi_wake_prep, &sstate, NULL); 2374 return (0); 2375 } 2376 2377 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 2378 static int 2379 acpi_wake_sysctl_walk(device_t dev) 2380 { 2381 int error, i, numdevs; 2382 device_t *devlist; 2383 device_t child; 2384 ACPI_STATUS status; 2385 2386 error = device_get_children(dev, &devlist, &numdevs); 2387 if (error != 0 || numdevs == 0) { 2388 if (numdevs == 0) 2389 free(devlist, M_TEMP); 2390 return (error); 2391 } 2392 for (i = 0; i < numdevs; i++) { 2393 child = devlist[i]; 2394 acpi_wake_sysctl_walk(child); 2395 if (!device_is_attached(child)) 2396 continue; 2397 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2398 if (ACPI_SUCCESS(status)) { 2399 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 2400 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 2401 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 2402 acpi_wake_set_sysctl, "I", "Device set to wake the system"); 2403 } 2404 } 2405 free(devlist, M_TEMP); 2406 2407 return (0); 2408 } 2409 2410 /* Enable or disable wake from userland. */ 2411 static int 2412 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 2413 { 2414 int enable, error; 2415 device_t dev; 2416 2417 dev = (device_t)arg1; 2418 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 2419 2420 error = sysctl_handle_int(oidp, &enable, 0, req); 2421 if (error != 0 || req->newptr == NULL) 2422 return (error); 2423 if (enable != 0 && enable != 1) 2424 return (EINVAL); 2425 2426 return (acpi_wake_set_enable(dev, enable)); 2427 } 2428 2429 /* Parse a device's _PRW into a structure. */ 2430 int 2431 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2432 { 2433 ACPI_STATUS status; 2434 ACPI_BUFFER prw_buffer; 2435 ACPI_OBJECT *res, *res2; 2436 int error, i, power_count; 2437 2438 if (h == NULL || prw == NULL) 2439 return (EINVAL); 2440 2441 /* 2442 * The _PRW object (7.2.9) is only required for devices that have the 2443 * ability to wake the system from a sleeping state. 2444 */ 2445 error = EINVAL; 2446 prw_buffer.Pointer = NULL; 2447 prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2448 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2449 if (ACPI_FAILURE(status)) 2450 return (ENOENT); 2451 res = (ACPI_OBJECT *)prw_buffer.Pointer; 2452 if (res == NULL) 2453 return (ENOENT); 2454 if (!ACPI_PKG_VALID(res, 2)) 2455 goto out; 2456 2457 /* 2458 * Element 1 of the _PRW object: 2459 * The lowest power system sleeping state that can be entered while still 2460 * providing wake functionality. The sleeping state being entered must 2461 * be less than (i.e., higher power) or equal to this value. 2462 */ 2463 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2464 goto out; 2465 2466 /* 2467 * Element 0 of the _PRW object: 2468 */ 2469 switch (res->Package.Elements[0].Type) { 2470 case ACPI_TYPE_INTEGER: 2471 /* 2472 * If the data type of this package element is numeric, then this 2473 * _PRW package element is the bit index in the GPEx_EN, in the 2474 * GPE blocks described in the FADT, of the enable bit that is 2475 * enabled for the wake event. 2476 */ 2477 prw->gpe_handle = NULL; 2478 prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2479 error = 0; 2480 break; 2481 case ACPI_TYPE_PACKAGE: 2482 /* 2483 * If the data type of this package element is a package, then this 2484 * _PRW package element is itself a package containing two 2485 * elements. The first is an object reference to the GPE Block 2486 * device that contains the GPE that will be triggered by the wake 2487 * event. The second element is numeric and it contains the bit 2488 * index in the GPEx_EN, in the GPE Block referenced by the 2489 * first element in the package, of the enable bit that is enabled for 2490 * the wake event. 2491 * 2492 * For example, if this field is a package then it is of the form: 2493 * Package() {\_SB.PCI0.ISA.GPE, 2} 2494 */ 2495 res2 = &res->Package.Elements[0]; 2496 if (!ACPI_PKG_VALID(res2, 2)) 2497 goto out; 2498 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2499 if (prw->gpe_handle == NULL) 2500 goto out; 2501 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2502 goto out; 2503 error = 0; 2504 break; 2505 default: 2506 goto out; 2507 } 2508 2509 /* Elements 2 to N of the _PRW object are power resources. */ 2510 power_count = res->Package.Count - 2; 2511 if (power_count > ACPI_PRW_MAX_POWERRES) { 2512 printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2513 power_count = 0; 2514 } 2515 prw->power_res_count = power_count; 2516 for (i = 0; i < power_count; i++) 2517 prw->power_res[i] = res->Package.Elements[i]; 2518 2519 out: 2520 if (prw_buffer.Pointer != NULL) 2521 AcpiOsFree(prw_buffer.Pointer); 2522 return (error); 2523 } 2524 2525 /* 2526 * ACPI Event Handlers 2527 */ 2528 2529 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 2530 2531 static void 2532 acpi_system_eventhandler_sleep(void *arg, int state) 2533 { 2534 2535 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2536 2537 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 2538 acpi_SetSleepState((struct acpi_softc *)arg, state); 2539 2540 return_VOID; 2541 } 2542 2543 static void 2544 acpi_system_eventhandler_wakeup(void *arg, int state) 2545 { 2546 2547 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2548 2549 /* Currently, nothing to do for wakeup. */ 2550 2551 return_VOID; 2552 } 2553 2554 /* 2555 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 2556 */ 2557 UINT32 2558 acpi_event_power_button_sleep(void *context) 2559 { 2560 struct acpi_softc *sc = (struct acpi_softc *)context; 2561 2562 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2563 2564 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 2565 2566 return_VALUE (ACPI_INTERRUPT_HANDLED); 2567 } 2568 2569 UINT32 2570 acpi_event_power_button_wake(void *context) 2571 { 2572 struct acpi_softc *sc = (struct acpi_softc *)context; 2573 2574 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2575 2576 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 2577 2578 return_VALUE (ACPI_INTERRUPT_HANDLED); 2579 } 2580 2581 UINT32 2582 acpi_event_sleep_button_sleep(void *context) 2583 { 2584 struct acpi_softc *sc = (struct acpi_softc *)context; 2585 2586 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2587 2588 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 2589 2590 return_VALUE (ACPI_INTERRUPT_HANDLED); 2591 } 2592 2593 UINT32 2594 acpi_event_sleep_button_wake(void *context) 2595 { 2596 struct acpi_softc *sc = (struct acpi_softc *)context; 2597 2598 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2599 2600 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 2601 2602 return_VALUE (ACPI_INTERRUPT_HANDLED); 2603 } 2604 2605 /* 2606 * XXX This static buffer is suboptimal. There is no locking so only 2607 * use this for single-threaded callers. 2608 */ 2609 char * 2610 acpi_name(ACPI_HANDLE handle) 2611 { 2612 ACPI_BUFFER buf; 2613 static char data[256]; 2614 2615 buf.Length = sizeof(data); 2616 buf.Pointer = data; 2617 2618 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 2619 return (data); 2620 return ("(unknown)"); 2621 } 2622 2623 /* 2624 * Debugging/bug-avoidance. Avoid trying to fetch info on various 2625 * parts of the namespace. 2626 */ 2627 int 2628 acpi_avoid(ACPI_HANDLE handle) 2629 { 2630 char *cp, *env, *np; 2631 int len; 2632 2633 np = acpi_name(handle); 2634 if (*np == '\\') 2635 np++; 2636 if ((env = getenv("debug.acpi.avoid")) == NULL) 2637 return (0); 2638 2639 /* Scan the avoid list checking for a match */ 2640 cp = env; 2641 for (;;) { 2642 while (*cp != 0 && isspace(*cp)) 2643 cp++; 2644 if (*cp == 0) 2645 break; 2646 len = 0; 2647 while (cp[len] != 0 && !isspace(cp[len])) 2648 len++; 2649 if (!strncmp(cp, np, len)) { 2650 freeenv(env); 2651 return(1); 2652 } 2653 cp += len; 2654 } 2655 freeenv(env); 2656 2657 return (0); 2658 } 2659 2660 /* 2661 * Debugging/bug-avoidance. Disable ACPI subsystem components. 2662 */ 2663 int 2664 acpi_disabled(char *subsys) 2665 { 2666 char *cp, *env; 2667 int len; 2668 2669 if ((env = getenv("debug.acpi.disabled")) == NULL) 2670 return (0); 2671 if (strcmp(env, "all") == 0) { 2672 freeenv(env); 2673 return (1); 2674 } 2675 2676 /* Scan the disable list, checking for a match. */ 2677 cp = env; 2678 for (;;) { 2679 while (*cp != '\0' && isspace(*cp)) 2680 cp++; 2681 if (*cp == '\0') 2682 break; 2683 len = 0; 2684 while (cp[len] != '\0' && !isspace(cp[len])) 2685 len++; 2686 if (strncmp(cp, subsys, len) == 0) { 2687 freeenv(env); 2688 return (1); 2689 } 2690 cp += len; 2691 } 2692 freeenv(env); 2693 2694 return (0); 2695 } 2696 2697 /* 2698 * Control interface. 2699 * 2700 * We multiplex ioctls for all participating ACPI devices here. Individual 2701 * drivers wanting to be accessible via /dev/acpi should use the 2702 * register/deregister interface to make their handlers visible. 2703 */ 2704 struct acpi_ioctl_hook 2705 { 2706 TAILQ_ENTRY(acpi_ioctl_hook) link; 2707 u_long cmd; 2708 acpi_ioctl_fn fn; 2709 void *arg; 2710 }; 2711 2712 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 2713 static int acpi_ioctl_hooks_initted; 2714 2715 int 2716 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 2717 { 2718 struct acpi_ioctl_hook *hp; 2719 2720 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 2721 return (ENOMEM); 2722 hp->cmd = cmd; 2723 hp->fn = fn; 2724 hp->arg = arg; 2725 2726 ACPI_LOCK(acpi); 2727 if (acpi_ioctl_hooks_initted == 0) { 2728 TAILQ_INIT(&acpi_ioctl_hooks); 2729 acpi_ioctl_hooks_initted = 1; 2730 } 2731 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 2732 ACPI_UNLOCK(acpi); 2733 2734 return (0); 2735 } 2736 2737 void 2738 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 2739 { 2740 struct acpi_ioctl_hook *hp; 2741 2742 ACPI_LOCK(acpi); 2743 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 2744 if (hp->cmd == cmd && hp->fn == fn) 2745 break; 2746 2747 if (hp != NULL) { 2748 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 2749 free(hp, M_ACPIDEV); 2750 } 2751 ACPI_UNLOCK(acpi); 2752 } 2753 2754 static int 2755 acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 2756 { 2757 return (0); 2758 } 2759 2760 static int 2761 acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 2762 { 2763 return (0); 2764 } 2765 2766 static int 2767 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 2768 { 2769 struct acpi_softc *sc; 2770 struct acpi_ioctl_hook *hp; 2771 int error, state; 2772 2773 error = 0; 2774 hp = NULL; 2775 sc = dev->si_drv1; 2776 2777 /* 2778 * Scan the list of registered ioctls, looking for handlers. 2779 */ 2780 ACPI_LOCK(acpi); 2781 if (acpi_ioctl_hooks_initted) 2782 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 2783 if (hp->cmd == cmd) 2784 break; 2785 } 2786 ACPI_UNLOCK(acpi); 2787 if (hp) 2788 return (hp->fn(cmd, addr, hp->arg)); 2789 2790 /* 2791 * Core ioctls are not permitted for non-writable user. 2792 * Currently, other ioctls just fetch information. 2793 * Not changing system behavior. 2794 */ 2795 if ((flag & FWRITE) == 0) 2796 return (EPERM); 2797 2798 /* Core system ioctls. */ 2799 switch (cmd) { 2800 case ACPIIO_SETSLPSTATE: 2801 error = EINVAL; 2802 state = *(int *)addr; 2803 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 2804 if (ACPI_SUCCESS(acpi_SetSleepState(sc, state))) 2805 error = 0; 2806 break; 2807 default: 2808 error = ENXIO; 2809 break; 2810 } 2811 2812 return (error); 2813 } 2814 2815 static int 2816 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2817 { 2818 int error; 2819 struct sbuf sb; 2820 UINT8 state, TypeA, TypeB; 2821 2822 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 2823 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) 2824 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 2825 sbuf_printf(&sb, "S%d ", state); 2826 sbuf_trim(&sb); 2827 sbuf_finish(&sb); 2828 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 2829 sbuf_delete(&sb); 2830 return (error); 2831 } 2832 2833 static int 2834 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2835 { 2836 char sleep_state[10]; 2837 int error; 2838 u_int new_state, old_state; 2839 2840 old_state = *(u_int *)oidp->oid_arg1; 2841 if (old_state > ACPI_S_STATES_MAX + 1) 2842 strlcpy(sleep_state, "unknown", sizeof(sleep_state)); 2843 else 2844 strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state)); 2845 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 2846 if (error == 0 && req->newptr != NULL) { 2847 new_state = ACPI_STATE_S0; 2848 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) 2849 if (strcmp(sleep_state, sleep_state_names[new_state]) == 0) 2850 break; 2851 if (new_state <= ACPI_S_STATES_MAX + 1) { 2852 if (new_state != old_state) 2853 *(u_int *)oidp->oid_arg1 = new_state; 2854 } else 2855 error = EINVAL; 2856 } 2857 2858 return (error); 2859 } 2860 2861 /* Inform devctl(4) when we receive a Notify. */ 2862 void 2863 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 2864 { 2865 char notify_buf[16]; 2866 ACPI_BUFFER handle_buf; 2867 ACPI_STATUS status; 2868 2869 if (subsystem == NULL) 2870 return; 2871 2872 handle_buf.Pointer = NULL; 2873 handle_buf.Length = ACPI_ALLOCATE_BUFFER; 2874 status = AcpiNsHandleToPathname(h, &handle_buf); 2875 if (ACPI_FAILURE(status)) 2876 return; 2877 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 2878 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 2879 AcpiOsFree(handle_buf.Pointer); 2880 } 2881 2882 #ifdef ACPI_DEBUG 2883 /* 2884 * Support for parsing debug options from the kernel environment. 2885 * 2886 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 2887 * by specifying the names of the bits in the debug.acpi.layer and 2888 * debug.acpi.level environment variables. Bits may be unset by 2889 * prefixing the bit name with !. 2890 */ 2891 struct debugtag 2892 { 2893 char *name; 2894 UINT32 value; 2895 }; 2896 2897 static struct debugtag dbg_layer[] = { 2898 {"ACPI_UTILITIES", ACPI_UTILITIES}, 2899 {"ACPI_HARDWARE", ACPI_HARDWARE}, 2900 {"ACPI_EVENTS", ACPI_EVENTS}, 2901 {"ACPI_TABLES", ACPI_TABLES}, 2902 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2903 {"ACPI_PARSER", ACPI_PARSER}, 2904 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2905 {"ACPI_EXECUTER", ACPI_EXECUTER}, 2906 {"ACPI_RESOURCES", ACPI_RESOURCES}, 2907 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 2908 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2909 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2910 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 2911 2912 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2913 {"ACPI_BATTERY", ACPI_BATTERY}, 2914 {"ACPI_BUS", ACPI_BUS}, 2915 {"ACPI_BUTTON", ACPI_BUTTON}, 2916 {"ACPI_EC", ACPI_EC}, 2917 {"ACPI_FAN", ACPI_FAN}, 2918 {"ACPI_POWERRES", ACPI_POWERRES}, 2919 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2920 {"ACPI_THERMAL", ACPI_THERMAL}, 2921 {"ACPI_TIMER", ACPI_TIMER}, 2922 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 2923 {NULL, 0} 2924 }; 2925 2926 static struct debugtag dbg_level[] = { 2927 {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 2928 {"ACPI_LV_WARN", ACPI_LV_WARN}, 2929 {"ACPI_LV_INIT", ACPI_LV_INIT}, 2930 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 2931 {"ACPI_LV_INFO", ACPI_LV_INFO}, 2932 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2933 2934 /* Trace verbosity level 1 [Standard Trace Level] */ 2935 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 2936 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 2937 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2938 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 2939 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 2940 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 2941 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 2942 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 2943 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 2944 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 2945 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 2946 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 2947 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 2948 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2949 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2950 2951 /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2952 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2953 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2954 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2955 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 2956 {"ACPI_LV_ALL", ACPI_LV_ALL}, 2957 2958 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2959 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2960 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2961 {"ACPI_LV_IO", ACPI_LV_IO}, 2962 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2963 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2964 2965 /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 2966 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 2967 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 2968 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 2969 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 2970 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 2971 {NULL, 0} 2972 }; 2973 2974 static void 2975 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 2976 { 2977 char *ep; 2978 int i, l; 2979 int set; 2980 2981 while (*cp) { 2982 if (isspace(*cp)) { 2983 cp++; 2984 continue; 2985 } 2986 ep = cp; 2987 while (*ep && !isspace(*ep)) 2988 ep++; 2989 if (*cp == '!') { 2990 set = 0; 2991 cp++; 2992 if (cp == ep) 2993 continue; 2994 } else { 2995 set = 1; 2996 } 2997 l = ep - cp; 2998 for (i = 0; tag[i].name != NULL; i++) { 2999 if (!strncmp(cp, tag[i].name, l)) { 3000 if (set) 3001 *flag |= tag[i].value; 3002 else 3003 *flag &= ~tag[i].value; 3004 } 3005 } 3006 cp = ep; 3007 } 3008 } 3009 3010 static void 3011 acpi_set_debugging(void *junk) 3012 { 3013 char *layer, *level; 3014 3015 if (cold) { 3016 AcpiDbgLayer = 0; 3017 AcpiDbgLevel = 0; 3018 } 3019 3020 layer = getenv("debug.acpi.layer"); 3021 level = getenv("debug.acpi.level"); 3022 if (layer == NULL && level == NULL) 3023 return; 3024 3025 printf("ACPI set debug"); 3026 if (layer != NULL) { 3027 if (strcmp("NONE", layer) != 0) 3028 printf(" layer '%s'", layer); 3029 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 3030 freeenv(layer); 3031 } 3032 if (level != NULL) { 3033 if (strcmp("NONE", level) != 0) 3034 printf(" level '%s'", level); 3035 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 3036 freeenv(level); 3037 } 3038 printf("\n"); 3039 } 3040 3041 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3042 NULL); 3043 3044 static int 3045 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 3046 { 3047 int error, *dbg; 3048 struct debugtag *tag; 3049 struct sbuf sb; 3050 3051 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 3052 return (ENOMEM); 3053 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 3054 tag = &dbg_layer[0]; 3055 dbg = &AcpiDbgLayer; 3056 } else { 3057 tag = &dbg_level[0]; 3058 dbg = &AcpiDbgLevel; 3059 } 3060 3061 /* Get old values if this is a get request. */ 3062 ACPI_SERIAL_BEGIN(acpi); 3063 if (*dbg == 0) { 3064 sbuf_cpy(&sb, "NONE"); 3065 } else if (req->newptr == NULL) { 3066 for (; tag->name != NULL; tag++) { 3067 if ((*dbg & tag->value) == tag->value) 3068 sbuf_printf(&sb, "%s ", tag->name); 3069 } 3070 } 3071 sbuf_trim(&sb); 3072 sbuf_finish(&sb); 3073 3074 /* Copy out the old values to the user. */ 3075 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 3076 sbuf_delete(&sb); 3077 3078 /* If the user is setting a string, parse it. */ 3079 if (error == 0 && req->newptr != NULL) { 3080 *dbg = 0; 3081 setenv((char *)oidp->oid_arg1, (char *)req->newptr); 3082 acpi_set_debugging(NULL); 3083 } 3084 ACPI_SERIAL_END(acpi); 3085 3086 return (error); 3087 } 3088 3089 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 3090 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 3091 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 3092 "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3093 #endif /* ACPI_DEBUG */ 3094 3095 static int 3096 acpi_pm_func(u_long cmd, void *arg, ...) 3097 { 3098 int state, acpi_state; 3099 int error; 3100 struct acpi_softc *sc; 3101 va_list ap; 3102 3103 error = 0; 3104 switch (cmd) { 3105 case POWER_CMD_SUSPEND: 3106 sc = (struct acpi_softc *)arg; 3107 if (sc == NULL) { 3108 error = EINVAL; 3109 goto out; 3110 } 3111 3112 va_start(ap, arg); 3113 state = va_arg(ap, int); 3114 va_end(ap); 3115 3116 switch (state) { 3117 case POWER_SLEEP_STATE_STANDBY: 3118 acpi_state = sc->acpi_standby_sx; 3119 break; 3120 case POWER_SLEEP_STATE_SUSPEND: 3121 acpi_state = sc->acpi_suspend_sx; 3122 break; 3123 case POWER_SLEEP_STATE_HIBERNATE: 3124 acpi_state = ACPI_STATE_S4; 3125 break; 3126 default: 3127 error = EINVAL; 3128 goto out; 3129 } 3130 3131 acpi_SetSleepState(sc, acpi_state); 3132 break; 3133 default: 3134 error = EINVAL; 3135 goto out; 3136 } 3137 3138 out: 3139 return (error); 3140 } 3141 3142 static void 3143 acpi_pm_register(void *arg) 3144 { 3145 if (!cold || resource_disabled("acpi", 0)) 3146 return; 3147 3148 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3149 } 3150 3151 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3152