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