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