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