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