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