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