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