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