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