acpi.c revision 279904
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 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: stable/10/sys/dev/acpica/acpi.c 279904 2015-03-12 07:07:41Z scottl $"); 32 33#include "opt_acpi.h" 34#include <sys/param.h> 35#include <sys/kernel.h> 36#include <sys/proc.h> 37#include <sys/fcntl.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/bus.h> 41#include <sys/conf.h> 42#include <sys/ioccom.h> 43#include <sys/reboot.h> 44#include <sys/sysctl.h> 45#include <sys/ctype.h> 46#include <sys/linker.h> 47#include <sys/power.h> 48#include <sys/sbuf.h> 49#include <sys/sched.h> 50#include <sys/smp.h> 51#include <sys/timetc.h> 52 53#if defined(__i386__) || defined(__amd64__) 54#include <machine/pci_cfgreg.h> 55#endif 56#include <machine/resource.h> 57#include <machine/bus.h> 58#include <sys/rman.h> 59#include <isa/isavar.h> 60#include <isa/pnpvar.h> 61 62#include <contrib/dev/acpica/include/acpi.h> 63#include <contrib/dev/acpica/include/accommon.h> 64#include <contrib/dev/acpica/include/acnamesp.h> 65 66#include <dev/acpica/acpivar.h> 67#include <dev/acpica/acpiio.h> 68 69#include <vm/vm_param.h> 70 71static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 72 73/* Hooks for the ACPI CA debugging infrastructure */ 74#define _COMPONENT ACPI_BUS 75ACPI_MODULE_NAME("ACPI") 76 77static d_open_t acpiopen; 78static d_close_t acpiclose; 79static d_ioctl_t acpiioctl; 80 81static struct cdevsw acpi_cdevsw = { 82 .d_version = D_VERSION, 83 .d_open = acpiopen, 84 .d_close = acpiclose, 85 .d_ioctl = acpiioctl, 86 .d_name = "acpi", 87}; 88 89struct acpi_interface { 90 ACPI_STRING *data; 91 int num; 92}; 93 94/* Global mutex for locking access to the ACPI subsystem. */ 95struct mtx acpi_mutex; 96 97/* Bitmap of device quirks. */ 98int acpi_quirks; 99 100/* Supported sleep states. */ 101static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 102 103static int acpi_modevent(struct module *mod, int event, void *junk); 104static int acpi_probe(device_t dev); 105static int acpi_attach(device_t dev); 106static int acpi_suspend(device_t dev); 107static int acpi_resume(device_t dev); 108static int acpi_shutdown(device_t dev); 109static device_t acpi_add_child(device_t bus, u_int order, const char *name, 110 int unit); 111static int acpi_print_child(device_t bus, device_t child); 112static void acpi_probe_nomatch(device_t bus, device_t child); 113static void acpi_driver_added(device_t dev, driver_t *driver); 114static int acpi_read_ivar(device_t dev, device_t child, int index, 115 uintptr_t *result); 116static int acpi_write_ivar(device_t dev, device_t child, int index, 117 uintptr_t value); 118static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 119static void acpi_reserve_resources(device_t dev); 120static int acpi_sysres_alloc(device_t dev); 121static int acpi_set_resource(device_t dev, device_t child, int type, 122 int rid, u_long start, u_long count); 123static struct resource *acpi_alloc_resource(device_t bus, device_t child, 124 int type, int *rid, u_long start, u_long end, 125 u_long count, u_int flags); 126static int acpi_adjust_resource(device_t bus, device_t child, int type, 127 struct resource *r, u_long start, u_long end); 128static int acpi_release_resource(device_t bus, device_t child, int type, 129 int rid, struct resource *r); 130static void acpi_delete_resource(device_t bus, device_t child, int type, 131 int rid); 132static uint32_t acpi_isa_get_logicalid(device_t dev); 133static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 134static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 135static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 136 ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 137 ACPI_BUFFER *ret); 138static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 139 void *context, void **retval); 140static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 141 int max_depth, acpi_scan_cb_t user_fn, void *arg); 142static int acpi_set_powerstate(device_t child, int state); 143static int acpi_isa_pnp_probe(device_t bus, device_t child, 144 struct isa_pnp_id *ids); 145static void acpi_probe_children(device_t bus); 146static void acpi_probe_order(ACPI_HANDLE handle, int *order); 147static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 148 void *context, void **status); 149static void acpi_sleep_enable(void *arg); 150static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 151static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 152static void acpi_shutdown_final(void *arg, int howto); 153static void acpi_enable_fixed_events(struct acpi_softc *sc); 154static BOOLEAN acpi_has_hid(ACPI_HANDLE handle); 155static void acpi_resync_clock(struct acpi_softc *sc); 156static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 157static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 158static int acpi_wake_prep_walk(int sstate); 159static int acpi_wake_sysctl_walk(device_t dev); 160static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 161static void acpi_system_eventhandler_sleep(void *arg, int state); 162static void acpi_system_eventhandler_wakeup(void *arg, int state); 163static int acpi_sname2sstate(const char *sname); 164static const char *acpi_sstate2sname(int sstate); 165static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 166static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 167static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 168static int acpi_pm_func(u_long cmd, void *arg, ...); 169static int acpi_child_location_str_method(device_t acdev, device_t child, 170 char *buf, size_t buflen); 171static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 172 char *buf, size_t buflen); 173#if defined(__i386__) || defined(__amd64__) 174static void acpi_enable_pcie(void); 175#endif 176static void acpi_hint_device_unit(device_t acdev, device_t child, 177 const char *name, int *unitp); 178static void acpi_reset_interfaces(device_t dev); 179 180static device_method_t acpi_methods[] = { 181 /* Device interface */ 182 DEVMETHOD(device_probe, acpi_probe), 183 DEVMETHOD(device_attach, acpi_attach), 184 DEVMETHOD(device_shutdown, acpi_shutdown), 185 DEVMETHOD(device_detach, bus_generic_detach), 186 DEVMETHOD(device_suspend, acpi_suspend), 187 DEVMETHOD(device_resume, acpi_resume), 188 189 /* Bus interface */ 190 DEVMETHOD(bus_add_child, acpi_add_child), 191 DEVMETHOD(bus_print_child, acpi_print_child), 192 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 193 DEVMETHOD(bus_driver_added, acpi_driver_added), 194 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 195 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 196 DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 197 DEVMETHOD(bus_set_resource, acpi_set_resource), 198 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 199 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 200 DEVMETHOD(bus_adjust_resource, acpi_adjust_resource), 201 DEVMETHOD(bus_release_resource, acpi_release_resource), 202 DEVMETHOD(bus_delete_resource, acpi_delete_resource), 203 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 204 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 205 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 206 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 207 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 208 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 209 DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 210 DEVMETHOD(bus_get_domain, acpi_get_domain), 211 212 /* ACPI bus */ 213 DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 214 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 215 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 216 DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 217 218 /* ISA emulation */ 219 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 220 221 DEVMETHOD_END 222}; 223 224static driver_t acpi_driver = { 225 "acpi", 226 acpi_methods, 227 sizeof(struct acpi_softc), 228}; 229 230static devclass_t acpi_devclass; 231DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 232MODULE_VERSION(acpi, 1); 233 234ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 235 236/* Local pools for managing system resources for ACPI child devices. */ 237static struct rman acpi_rman_io, acpi_rman_mem; 238 239#define ACPI_MINIMUM_AWAKETIME 5 240 241/* Holds the description of the acpi0 device. */ 242static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 243 244SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 245static char acpi_ca_version[12]; 246SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 247 acpi_ca_version, 0, "Version of Intel ACPI-CA"); 248 249/* 250 * Allow overriding _OSI methods. 251 */ 252static char acpi_install_interface[256]; 253TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface, 254 sizeof(acpi_install_interface)); 255static char acpi_remove_interface[256]; 256TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface, 257 sizeof(acpi_remove_interface)); 258 259/* 260 * Allow override of whether methods execute in parallel or not. 261 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 262 * errors for AML that really can't handle parallel method execution. 263 * It is off by default since this breaks recursive methods and 264 * some IBMs use such code. 265 */ 266static int acpi_serialize_methods; 267TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 268 269/* Allow users to dump Debug objects without ACPI debugger. */ 270static int acpi_debug_objects; 271TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 272SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 273 CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 274 "Enable Debug objects"); 275 276/* Allow the interpreter to ignore common mistakes in BIOS. */ 277static int acpi_interpreter_slack = 1; 278TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 279SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 280 &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 281 282#ifdef __amd64__ 283/* Reset system clock while resuming. XXX Remove once tested. */ 284static int acpi_reset_clock = 1; 285TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock); 286SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW, 287 &acpi_reset_clock, 1, "Reset system clock while resuming."); 288#endif 289 290/* Allow users to override quirks. */ 291TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 292 293static int acpi_susp_bounce; 294SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 295 &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 296 297/* 298 * ACPI can only be loaded as a module by the loader; activating it after 299 * system bootstrap time is not useful, and can be fatal to the system. 300 * It also cannot be unloaded, since the entire system bus hierarchy hangs 301 * off it. 302 */ 303static int 304acpi_modevent(struct module *mod, int event, void *junk) 305{ 306 switch (event) { 307 case MOD_LOAD: 308 if (!cold) { 309 printf("The ACPI driver cannot be loaded after boot.\n"); 310 return (EPERM); 311 } 312 break; 313 case MOD_UNLOAD: 314 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 315 return (EBUSY); 316 break; 317 default: 318 break; 319 } 320 return (0); 321} 322 323/* 324 * Perform early initialization. 325 */ 326ACPI_STATUS 327acpi_Startup(void) 328{ 329 static int started = 0; 330 ACPI_STATUS status; 331 int val; 332 333 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 334 335 /* Only run the startup code once. The MADT driver also calls this. */ 336 if (started) 337 return_VALUE (AE_OK); 338 started = 1; 339 340 /* 341 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 342 * if more tables exist. 343 */ 344 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 345 printf("ACPI: Table initialisation failed: %s\n", 346 AcpiFormatException(status)); 347 return_VALUE (status); 348 } 349 350 /* Set up any quirks we have for this system. */ 351 if (acpi_quirks == ACPI_Q_OK) 352 acpi_table_quirks(&acpi_quirks); 353 354 /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 355 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 356 acpi_quirks &= ~ACPI_Q_BROKEN; 357 if (acpi_quirks & ACPI_Q_BROKEN) { 358 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 359 status = AE_SUPPORT; 360 } 361 362 return_VALUE (status); 363} 364 365/* 366 * Detect ACPI and perform early initialisation. 367 */ 368int 369acpi_identify(void) 370{ 371 ACPI_TABLE_RSDP *rsdp; 372 ACPI_TABLE_HEADER *rsdt; 373 ACPI_PHYSICAL_ADDRESS paddr; 374 struct sbuf sb; 375 376 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 377 378 if (!cold) 379 return (ENXIO); 380 381 /* Check that we haven't been disabled with a hint. */ 382 if (resource_disabled("acpi", 0)) 383 return (ENXIO); 384 385 /* Check for other PM systems. */ 386 if (power_pm_get_type() != POWER_PM_TYPE_NONE && 387 power_pm_get_type() != POWER_PM_TYPE_ACPI) { 388 printf("ACPI identify failed, other PM system enabled.\n"); 389 return (ENXIO); 390 } 391 392 /* Initialize root tables. */ 393 if (ACPI_FAILURE(acpi_Startup())) { 394 printf("ACPI: Try disabling either ACPI or apic support.\n"); 395 return (ENXIO); 396 } 397 398 if ((paddr = AcpiOsGetRootPointer()) == 0 || 399 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 400 return (ENXIO); 401 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 402 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 403 else 404 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 405 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 406 407 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 408 return (ENXIO); 409 sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 410 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 411 sbuf_trim(&sb); 412 sbuf_putc(&sb, ' '); 413 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 414 sbuf_trim(&sb); 415 sbuf_finish(&sb); 416 sbuf_delete(&sb); 417 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 418 419 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 420 421 return (0); 422} 423 424/* 425 * Fetch some descriptive data from ACPI to put in our attach message. 426 */ 427static int 428acpi_probe(device_t dev) 429{ 430 431 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 432 433 device_set_desc(dev, acpi_desc); 434 435 return_VALUE (BUS_PROBE_NOWILDCARD); 436} 437 438static int 439acpi_attach(device_t dev) 440{ 441 struct acpi_softc *sc; 442 ACPI_STATUS status; 443 int error, state; 444 UINT32 flags; 445 UINT8 TypeA, TypeB; 446 char *env; 447 448 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 449 450 sc = device_get_softc(dev); 451 sc->acpi_dev = dev; 452 callout_init(&sc->susp_force_to, TRUE); 453 454 error = ENXIO; 455 456 /* Initialize resource manager. */ 457 acpi_rman_io.rm_type = RMAN_ARRAY; 458 acpi_rman_io.rm_start = 0; 459 acpi_rman_io.rm_end = 0xffff; 460 acpi_rman_io.rm_descr = "ACPI I/O ports"; 461 if (rman_init(&acpi_rman_io) != 0) 462 panic("acpi rman_init IO ports failed"); 463 acpi_rman_mem.rm_type = RMAN_ARRAY; 464 acpi_rman_mem.rm_start = 0; 465 acpi_rman_mem.rm_end = ~0ul; 466 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 467 if (rman_init(&acpi_rman_mem) != 0) 468 panic("acpi rman_init memory failed"); 469 470 /* Initialise the ACPI mutex */ 471 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 472 473 /* 474 * Set the globals from our tunables. This is needed because ACPI-CA 475 * uses UINT8 for some values and we have no tunable_byte. 476 */ 477 AcpiGbl_AllMethodsSerialized = acpi_serialize_methods ? TRUE : FALSE; 478 AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE; 479 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 480 481#ifndef ACPI_DEBUG 482 /* 483 * Disable all debugging layers and levels. 484 */ 485 AcpiDbgLayer = 0; 486 AcpiDbgLevel = 0; 487#endif 488 489 /* Start up the ACPI CA subsystem. */ 490 status = AcpiInitializeSubsystem(); 491 if (ACPI_FAILURE(status)) { 492 device_printf(dev, "Could not initialize Subsystem: %s\n", 493 AcpiFormatException(status)); 494 goto out; 495 } 496 497 /* Override OS interfaces if the user requested. */ 498 acpi_reset_interfaces(dev); 499 500 /* Load ACPI name space. */ 501 status = AcpiLoadTables(); 502 if (ACPI_FAILURE(status)) { 503 device_printf(dev, "Could not load Namespace: %s\n", 504 AcpiFormatException(status)); 505 goto out; 506 } 507 508#if defined(__i386__) || defined(__amd64__) 509 /* Handle MCFG table if present. */ 510 acpi_enable_pcie(); 511#endif 512 513 /* 514 * Note that some systems (specifically, those with namespace evaluation 515 * issues that require the avoidance of parts of the namespace) must 516 * avoid running _INI and _STA on everything, as well as dodging the final 517 * object init pass. 518 * 519 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 520 * 521 * XXX We should arrange for the object init pass after we have attached 522 * all our child devices, but on many systems it works here. 523 */ 524 flags = 0; 525 if (testenv("debug.acpi.avoid")) 526 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 527 528 /* Bring the hardware and basic handlers online. */ 529 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 530 device_printf(dev, "Could not enable ACPI: %s\n", 531 AcpiFormatException(status)); 532 goto out; 533 } 534 535 /* 536 * Call the ECDT probe function to provide EC functionality before 537 * the namespace has been evaluated. 538 * 539 * XXX This happens before the sysresource devices have been probed and 540 * attached so its resources come from nexus0. In practice, this isn't 541 * a problem but should be addressed eventually. 542 */ 543 acpi_ec_ecdt_probe(dev); 544 545 /* Bring device objects and regions online. */ 546 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 547 device_printf(dev, "Could not initialize ACPI objects: %s\n", 548 AcpiFormatException(status)); 549 goto out; 550 } 551 552 /* 553 * Setup our sysctl tree. 554 * 555 * XXX: This doesn't check to make sure that none of these fail. 556 */ 557 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 558 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 559 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 560 device_get_name(dev), CTLFLAG_RD, 0, ""); 561 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 562 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 563 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 564 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 565 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 566 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 567 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 568 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 569 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 570 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 571 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 572 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 573 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 574 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 575 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 576 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 577 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 578 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 579 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 580 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 581 "sleep delay in seconds"); 582 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 583 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 584 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 585 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 586 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 587 OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 588 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 589 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 590 OID_AUTO, "handle_reboot", CTLFLAG_RW, 591 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 592 593 /* 594 * Default to 1 second before sleeping to give some machines time to 595 * stabilize. 596 */ 597 sc->acpi_sleep_delay = 1; 598 if (bootverbose) 599 sc->acpi_verbose = 1; 600 if ((env = getenv("hw.acpi.verbose")) != NULL) { 601 if (strcmp(env, "0") != 0) 602 sc->acpi_verbose = 1; 603 freeenv(env); 604 } 605 606 /* Only enable reboot by default if the FADT says it is available. */ 607 if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) 608 sc->acpi_handle_reboot = 1; 609 610 /* Only enable S4BIOS by default if the FACS says it is available. */ 611 if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 612 sc->acpi_s4bios = 1; 613 614 /* Probe all supported sleep states. */ 615 acpi_sleep_states[ACPI_STATE_S0] = TRUE; 616 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 617 if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT, 618 __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) && 619 ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 620 acpi_sleep_states[state] = TRUE; 621 622 /* 623 * Dispatch the default sleep state to devices. The lid switch is set 624 * to UNKNOWN by default to avoid surprising users. 625 */ 626 sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 627 ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 628 sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 629 sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 630 ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 631 sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 632 ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 633 634 /* Pick the first valid sleep state for the sleep button default. */ 635 sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 636 for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 637 if (acpi_sleep_states[state]) { 638 sc->acpi_sleep_button_sx = state; 639 break; 640 } 641 642 acpi_enable_fixed_events(sc); 643 644 /* 645 * Scan the namespace and attach/initialise children. 646 */ 647 648 /* Register our shutdown handler. */ 649 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 650 SHUTDOWN_PRI_LAST); 651 652 /* 653 * Register our acpi event handlers. 654 * XXX should be configurable eg. via userland policy manager. 655 */ 656 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 657 sc, ACPI_EVENT_PRI_LAST); 658 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 659 sc, ACPI_EVENT_PRI_LAST); 660 661 /* Flag our initial states. */ 662 sc->acpi_enabled = TRUE; 663 sc->acpi_sstate = ACPI_STATE_S0; 664 sc->acpi_sleep_disabled = TRUE; 665 666 /* Create the control device */ 667 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 668 "acpi"); 669 sc->acpi_dev_t->si_drv1 = sc; 670 671 if ((error = acpi_machdep_init(dev))) 672 goto out; 673 674 /* Register ACPI again to pass the correct argument of pm_func. */ 675 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 676 677 if (!acpi_disabled("bus")) 678 acpi_probe_children(dev); 679 680 /* Update all GPEs and enable runtime GPEs. */ 681 status = AcpiUpdateAllGpes(); 682 if (ACPI_FAILURE(status)) 683 device_printf(dev, "Could not update all GPEs: %s\n", 684 AcpiFormatException(status)); 685 686 /* Allow sleep request after a while. */ 687 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 688 689 error = 0; 690 691 out: 692 return_VALUE (error); 693} 694 695static void 696acpi_set_power_children(device_t dev, int state) 697{ 698 device_t child, parent; 699 device_t *devlist; 700 struct pci_devinfo *dinfo; 701 int dstate, i, numdevs; 702 703 if (device_get_children(dev, &devlist, &numdevs) != 0) 704 return; 705 706 /* 707 * Retrieve and set D-state for the sleep state if _SxD is present. 708 * Skip children who aren't attached since they are handled separately. 709 */ 710 parent = device_get_parent(dev); 711 for (i = 0; i < numdevs; i++) { 712 child = devlist[i]; 713 dinfo = device_get_ivars(child); 714 dstate = state; 715 if (device_is_attached(child) && 716 acpi_device_pwr_for_sleep(parent, dev, &dstate) == 0) 717 acpi_set_powerstate(child, dstate); 718 } 719 free(devlist, M_TEMP); 720} 721 722static int 723acpi_suspend(device_t dev) 724{ 725 int error; 726 727 GIANT_REQUIRED; 728 729 error = bus_generic_suspend(dev); 730 if (error == 0) 731 acpi_set_power_children(dev, ACPI_STATE_D3); 732 733 return (error); 734} 735 736static int 737acpi_resume(device_t dev) 738{ 739 740 GIANT_REQUIRED; 741 742 acpi_set_power_children(dev, ACPI_STATE_D0); 743 744 return (bus_generic_resume(dev)); 745} 746 747static int 748acpi_shutdown(device_t dev) 749{ 750 751 GIANT_REQUIRED; 752 753 /* Allow children to shutdown first. */ 754 bus_generic_shutdown(dev); 755 756 /* 757 * Enable any GPEs that are able to power-on the system (i.e., RTC). 758 * Also, disable any that are not valid for this state (most). 759 */ 760 acpi_wake_prep_walk(ACPI_STATE_S5); 761 762 return (0); 763} 764 765/* 766 * Handle a new device being added 767 */ 768static device_t 769acpi_add_child(device_t bus, u_int order, const char *name, int unit) 770{ 771 struct acpi_device *ad; 772 device_t child; 773 774 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 775 return (NULL); 776 777 resource_list_init(&ad->ad_rl); 778 779 child = device_add_child_ordered(bus, order, name, unit); 780 if (child != NULL) 781 device_set_ivars(child, ad); 782 else 783 free(ad, M_ACPIDEV); 784 return (child); 785} 786 787static int 788acpi_print_child(device_t bus, device_t child) 789{ 790 struct acpi_device *adev = device_get_ivars(child); 791 struct resource_list *rl = &adev->ad_rl; 792 int retval = 0; 793 794 retval += bus_print_child_header(bus, child); 795 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 796 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 797 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 798 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 799 if (device_get_flags(child)) 800 retval += printf(" flags %#x", device_get_flags(child)); 801 retval += bus_print_child_domain(bus, child); 802 retval += bus_print_child_footer(bus, child); 803 804 return (retval); 805} 806 807/* 808 * If this device is an ACPI child but no one claimed it, attempt 809 * to power it off. We'll power it back up when a driver is added. 810 * 811 * XXX Disabled for now since many necessary devices (like fdc and 812 * ATA) don't claim the devices we created for them but still expect 813 * them to be powered up. 814 */ 815static void 816acpi_probe_nomatch(device_t bus, device_t child) 817{ 818#ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 819 acpi_set_powerstate(child, ACPI_STATE_D3); 820#endif 821} 822 823/* 824 * If a new driver has a chance to probe a child, first power it up. 825 * 826 * XXX Disabled for now (see acpi_probe_nomatch for details). 827 */ 828static void 829acpi_driver_added(device_t dev, driver_t *driver) 830{ 831 device_t child, *devlist; 832 int i, numdevs; 833 834 DEVICE_IDENTIFY(driver, dev); 835 if (device_get_children(dev, &devlist, &numdevs)) 836 return; 837 for (i = 0; i < numdevs; i++) { 838 child = devlist[i]; 839 if (device_get_state(child) == DS_NOTPRESENT) { 840#ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 841 acpi_set_powerstate(child, ACPI_STATE_D0); 842 if (device_probe_and_attach(child) != 0) 843 acpi_set_powerstate(child, ACPI_STATE_D3); 844#else 845 device_probe_and_attach(child); 846#endif 847 } 848 } 849 free(devlist, M_TEMP); 850} 851 852/* Location hint for devctl(8) */ 853static int 854acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 855 size_t buflen) 856{ 857 struct acpi_device *dinfo = device_get_ivars(child); 858 char buf2[32]; 859 int pxm; 860 861 if (dinfo->ad_handle) { 862 snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 863 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) { 864 snprintf(buf2, 32, " _PXM=%d", pxm); 865 strlcat(buf, buf2, buflen); 866 } 867 } else { 868 snprintf(buf, buflen, "unknown"); 869 } 870 return (0); 871} 872 873/* PnP information for devctl(8) */ 874static int 875acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 876 size_t buflen) 877{ 878 struct acpi_device *dinfo = device_get_ivars(child); 879 ACPI_DEVICE_INFO *adinfo; 880 881 if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 882 snprintf(buf, buflen, "unknown"); 883 return (0); 884 } 885 886 snprintf(buf, buflen, "_HID=%s _UID=%lu", 887 (adinfo->Valid & ACPI_VALID_HID) ? 888 adinfo->HardwareId.String : "none", 889 (adinfo->Valid & ACPI_VALID_UID) ? 890 strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 891 AcpiOsFree(adinfo); 892 893 return (0); 894} 895 896/* 897 * Handle per-device ivars 898 */ 899static int 900acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 901{ 902 struct acpi_device *ad; 903 904 if ((ad = device_get_ivars(child)) == NULL) { 905 device_printf(child, "device has no ivars\n"); 906 return (ENOENT); 907 } 908 909 /* ACPI and ISA compatibility ivars */ 910 switch(index) { 911 case ACPI_IVAR_HANDLE: 912 *(ACPI_HANDLE *)result = ad->ad_handle; 913 break; 914 case ACPI_IVAR_PRIVATE: 915 *(void **)result = ad->ad_private; 916 break; 917 case ACPI_IVAR_FLAGS: 918 *(int *)result = ad->ad_flags; 919 break; 920 case ISA_IVAR_VENDORID: 921 case ISA_IVAR_SERIAL: 922 case ISA_IVAR_COMPATID: 923 *(int *)result = -1; 924 break; 925 case ISA_IVAR_LOGICALID: 926 *(int *)result = acpi_isa_get_logicalid(child); 927 break; 928 default: 929 return (ENOENT); 930 } 931 932 return (0); 933} 934 935static int 936acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 937{ 938 struct acpi_device *ad; 939 940 if ((ad = device_get_ivars(child)) == NULL) { 941 device_printf(child, "device has no ivars\n"); 942 return (ENOENT); 943 } 944 945 switch(index) { 946 case ACPI_IVAR_HANDLE: 947 ad->ad_handle = (ACPI_HANDLE)value; 948 break; 949 case ACPI_IVAR_PRIVATE: 950 ad->ad_private = (void *)value; 951 break; 952 case ACPI_IVAR_FLAGS: 953 ad->ad_flags = (int)value; 954 break; 955 default: 956 panic("bad ivar write request (%d)", index); 957 return (ENOENT); 958 } 959 960 return (0); 961} 962 963/* 964 * Handle child resource allocation/removal 965 */ 966static struct resource_list * 967acpi_get_rlist(device_t dev, device_t child) 968{ 969 struct acpi_device *ad; 970 971 ad = device_get_ivars(child); 972 return (&ad->ad_rl); 973} 974 975static int 976acpi_match_resource_hint(device_t dev, int type, long value) 977{ 978 struct acpi_device *ad = device_get_ivars(dev); 979 struct resource_list *rl = &ad->ad_rl; 980 struct resource_list_entry *rle; 981 982 STAILQ_FOREACH(rle, rl, link) { 983 if (rle->type != type) 984 continue; 985 if (rle->start <= value && rle->end >= value) 986 return (1); 987 } 988 return (0); 989} 990 991/* 992 * Wire device unit numbers based on resource matches in hints. 993 */ 994static void 995acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 996 int *unitp) 997{ 998 const char *s; 999 long value; 1000 int line, matches, unit; 1001 1002 /* 1003 * Iterate over all the hints for the devices with the specified 1004 * name to see if one's resources are a subset of this device. 1005 */ 1006 line = 0; 1007 for (;;) { 1008 if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 1009 break; 1010 1011 /* Must have an "at" for acpi or isa. */ 1012 resource_string_value(name, unit, "at", &s); 1013 if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 1014 strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 1015 continue; 1016 1017 /* 1018 * Check for matching resources. We must have at least one match. 1019 * Since I/O and memory resources cannot be shared, if we get a 1020 * match on either of those, ignore any mismatches in IRQs or DRQs. 1021 * 1022 * XXX: We may want to revisit this to be more lenient and wire 1023 * as long as it gets one match. 1024 */ 1025 matches = 0; 1026 if (resource_long_value(name, unit, "port", &value) == 0) { 1027 /* 1028 * Floppy drive controllers are notorious for having a 1029 * wide variety of resources not all of which include the 1030 * first port that is specified by the hint (typically 1031 * 0x3f0) (see the comment above fdc_isa_alloc_resources() 1032 * in fdc_isa.c). However, they do all seem to include 1033 * port + 2 (e.g. 0x3f2) so for a floppy device, look for 1034 * 'value + 2' in the port resources instead of the hint 1035 * value. 1036 */ 1037 if (strcmp(name, "fdc") == 0) 1038 value += 2; 1039 if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 1040 matches++; 1041 else 1042 continue; 1043 } 1044 if (resource_long_value(name, unit, "maddr", &value) == 0) { 1045 if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 1046 matches++; 1047 else 1048 continue; 1049 } 1050 if (matches > 0) 1051 goto matched; 1052 if (resource_long_value(name, unit, "irq", &value) == 0) { 1053 if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 1054 matches++; 1055 else 1056 continue; 1057 } 1058 if (resource_long_value(name, unit, "drq", &value) == 0) { 1059 if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 1060 matches++; 1061 else 1062 continue; 1063 } 1064 1065 matched: 1066 if (matches > 0) { 1067 /* We have a winner! */ 1068 *unitp = unit; 1069 break; 1070 } 1071 } 1072} 1073 1074/* 1075 * Fech the NUMA domain for the given device. 1076 * 1077 * If a device has a _PXM method, map that to a NUMA domain. 1078 * 1079 * If none is found, then it'll call the parent method. 1080 * If there's no domain, return ENOENT. 1081 */ 1082int 1083acpi_get_domain(device_t dev, device_t child, int *domain) 1084{ 1085#if MAXMEMDOM > 1 1086 ACPI_HANDLE h; 1087 int d, pxm; 1088 1089 h = acpi_get_handle(child); 1090 if ((h != NULL) && 1091 ACPI_SUCCESS(acpi_GetInteger(h, "_PXM", &pxm))) { 1092 d = acpi_map_pxm_to_vm_domainid(pxm); 1093 if (d < 0) 1094 return (ENOENT); 1095 *domain = d; 1096 return (0); 1097 } 1098#endif 1099 /* No _PXM node; go up a level */ 1100 return (bus_generic_get_domain(dev, child, domain)); 1101} 1102 1103/* 1104 * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1105 * duplicates, we merge any in the sysresource attach routine. 1106 */ 1107static int 1108acpi_sysres_alloc(device_t dev) 1109{ 1110 struct resource *res; 1111 struct resource_list *rl; 1112 struct resource_list_entry *rle; 1113 struct rman *rm; 1114 char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1115 device_t *children; 1116 int child_count, i; 1117 1118 /* 1119 * Probe/attach any sysresource devices. This would be unnecessary if we 1120 * had multi-pass probe/attach. 1121 */ 1122 if (device_get_children(dev, &children, &child_count) != 0) 1123 return (ENXIO); 1124 for (i = 0; i < child_count; i++) { 1125 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1126 device_probe_and_attach(children[i]); 1127 } 1128 free(children, M_TEMP); 1129 1130 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1131 STAILQ_FOREACH(rle, rl, link) { 1132 if (rle->res != NULL) { 1133 device_printf(dev, "duplicate resource for %lx\n", rle->start); 1134 continue; 1135 } 1136 1137 /* Only memory and IO resources are valid here. */ 1138 switch (rle->type) { 1139 case SYS_RES_IOPORT: 1140 rm = &acpi_rman_io; 1141 break; 1142 case SYS_RES_MEMORY: 1143 rm = &acpi_rman_mem; 1144 break; 1145 default: 1146 continue; 1147 } 1148 1149 /* Pre-allocate resource and add to our rman pool. */ 1150 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1151 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1152 if (res != NULL) { 1153 rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1154 rle->res = res; 1155 } else 1156 device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 1157 rle->start, rle->count, rle->type); 1158 } 1159 return (0); 1160} 1161 1162static char *pcilink_ids[] = { "PNP0C0F", NULL }; 1163static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1164 1165/* 1166 * Reserve declared resources for devices found during attach once system 1167 * resources have been allocated. 1168 */ 1169static void 1170acpi_reserve_resources(device_t dev) 1171{ 1172 struct resource_list_entry *rle; 1173 struct resource_list *rl; 1174 struct acpi_device *ad; 1175 struct acpi_softc *sc; 1176 device_t *children; 1177 int child_count, i; 1178 1179 sc = device_get_softc(dev); 1180 if (device_get_children(dev, &children, &child_count) != 0) 1181 return; 1182 for (i = 0; i < child_count; i++) { 1183 ad = device_get_ivars(children[i]); 1184 rl = &ad->ad_rl; 1185 1186 /* Don't reserve system resources. */ 1187 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1188 continue; 1189 1190 STAILQ_FOREACH(rle, rl, link) { 1191 /* 1192 * Don't reserve IRQ resources. There are many sticky things 1193 * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET 1194 * when using legacy routing). 1195 */ 1196 if (rle->type == SYS_RES_IRQ) 1197 continue; 1198 1199 /* 1200 * Don't reserve the resource if it is already allocated. 1201 * The acpi_ec(4) driver can allocate its resources early 1202 * if ECDT is present. 1203 */ 1204 if (rle->res != NULL) 1205 continue; 1206 1207 /* 1208 * Try to reserve the resource from our parent. If this 1209 * fails because the resource is a system resource, just 1210 * let it be. The resource range is already reserved so 1211 * that other devices will not use it. If the driver 1212 * needs to allocate the resource, then 1213 * acpi_alloc_resource() will sub-alloc from the system 1214 * resource. 1215 */ 1216 resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid, 1217 rle->start, rle->end, rle->count, 0); 1218 } 1219 } 1220 free(children, M_TEMP); 1221 sc->acpi_resources_reserved = 1; 1222} 1223 1224static int 1225acpi_set_resource(device_t dev, device_t child, int type, int rid, 1226 u_long start, u_long count) 1227{ 1228 struct acpi_softc *sc = device_get_softc(dev); 1229 struct acpi_device *ad = device_get_ivars(child); 1230 struct resource_list *rl = &ad->ad_rl; 1231 ACPI_DEVICE_INFO *devinfo; 1232 u_long end; 1233 1234 /* Ignore IRQ resources for PCI link devices. */ 1235 if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL) 1236 return (0); 1237 1238 /* 1239 * Ignore most resources for PCI root bridges. Some BIOSes 1240 * incorrectly enumerate the memory ranges they decode as plain 1241 * memory resources instead of as ResourceProducer ranges. Other 1242 * BIOSes incorrectly list system resource entries for I/O ranges 1243 * under the PCI bridge. Do allow the one known-correct case on 1244 * x86 of a PCI bridge claiming the I/O ports used for PCI config 1245 * access. 1246 */ 1247 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 1248 if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) { 1249 if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) { 1250#if defined(__i386__) || defined(__amd64__) 1251 if (!(type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT)) 1252#endif 1253 { 1254 AcpiOsFree(devinfo); 1255 return (0); 1256 } 1257 } 1258 AcpiOsFree(devinfo); 1259 } 1260 } 1261 1262 /* If the resource is already allocated, fail. */ 1263 if (resource_list_busy(rl, type, rid)) 1264 return (EBUSY); 1265 1266 /* If the resource is already reserved, release it. */ 1267 if (resource_list_reserved(rl, type, rid)) 1268 resource_list_unreserve(rl, dev, child, type, rid); 1269 1270 /* Add the resource. */ 1271 end = (start + count - 1); 1272 resource_list_add(rl, type, rid, start, end, count); 1273 1274 /* Don't reserve resources until the system resources are allocated. */ 1275 if (!sc->acpi_resources_reserved) 1276 return (0); 1277 1278 /* Don't reserve system resources. */ 1279 if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL) 1280 return (0); 1281 1282 /* 1283 * Don't reserve IRQ resources. There are many sticky things to 1284 * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when 1285 * using legacy routing). 1286 */ 1287 if (type == SYS_RES_IRQ) 1288 return (0); 1289 1290 /* 1291 * Reserve the resource. 1292 * 1293 * XXX: Ignores failure for now. Failure here is probably a 1294 * BIOS/firmware bug? 1295 */ 1296 resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0); 1297 return (0); 1298} 1299 1300static struct resource * 1301acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 1302 u_long start, u_long end, u_long count, u_int flags) 1303{ 1304 ACPI_RESOURCE ares; 1305 struct acpi_device *ad; 1306 struct resource_list_entry *rle; 1307 struct resource_list *rl; 1308 struct resource *res; 1309 int isdefault = (start == 0UL && end == ~0UL); 1310 1311 /* 1312 * First attempt at allocating the resource. For direct children, 1313 * use resource_list_alloc() to handle reserved resources. For 1314 * other devices, pass the request up to our parent. 1315 */ 1316 if (bus == device_get_parent(child)) { 1317 ad = device_get_ivars(child); 1318 rl = &ad->ad_rl; 1319 1320 /* 1321 * Simulate the behavior of the ISA bus for direct children 1322 * devices. That is, if a non-default range is specified for 1323 * a resource that doesn't exist, use bus_set_resource() to 1324 * add the resource before allocating it. Note that these 1325 * resources will not be reserved. 1326 */ 1327 if (!isdefault && resource_list_find(rl, type, *rid) == NULL) 1328 resource_list_add(rl, type, *rid, start, end, count); 1329 res = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 1330 flags); 1331 if (res != NULL && type == SYS_RES_IRQ) { 1332 /* 1333 * Since bus_config_intr() takes immediate effect, we cannot 1334 * configure the interrupt associated with a device when we 1335 * parse the resources but have to defer it until a driver 1336 * actually allocates the interrupt via bus_alloc_resource(). 1337 * 1338 * XXX: Should we handle the lookup failing? 1339 */ 1340 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 1341 acpi_config_intr(child, &ares); 1342 } 1343 1344 /* 1345 * If this is an allocation of the "default" range for a given 1346 * RID, fetch the exact bounds for this resource from the 1347 * resource list entry to try to allocate the range from the 1348 * system resource regions. 1349 */ 1350 if (res == NULL && isdefault) { 1351 rle = resource_list_find(rl, type, *rid); 1352 if (rle != NULL) { 1353 start = rle->start; 1354 end = rle->end; 1355 count = rle->count; 1356 } 1357 } 1358 } else 1359 res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1360 start, end, count, flags); 1361 1362 /* 1363 * If the first attempt failed and this is an allocation of a 1364 * specific range, try to satisfy the request via a suballocation 1365 * from our system resource regions. 1366 */ 1367 if (res == NULL && start + count - 1 == end) 1368 res = acpi_alloc_sysres(child, type, rid, start, end, count, flags); 1369 return (res); 1370} 1371 1372/* 1373 * Attempt to allocate a specific resource range from the system 1374 * resource ranges. Note that we only handle memory and I/O port 1375 * system resources. 1376 */ 1377struct resource * 1378acpi_alloc_sysres(device_t child, int type, int *rid, u_long start, u_long end, 1379 u_long count, u_int flags) 1380{ 1381 struct rman *rm; 1382 struct resource *res; 1383 1384 switch (type) { 1385 case SYS_RES_IOPORT: 1386 rm = &acpi_rman_io; 1387 break; 1388 case SYS_RES_MEMORY: 1389 rm = &acpi_rman_mem; 1390 break; 1391 default: 1392 return (NULL); 1393 } 1394 1395 KASSERT(start + count - 1 == end, ("wildcard resource range")); 1396 res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1397 child); 1398 if (res == NULL) 1399 return (NULL); 1400 1401 rman_set_rid(res, *rid); 1402 1403 /* If requested, activate the resource using the parent's method. */ 1404 if (flags & RF_ACTIVE) 1405 if (bus_activate_resource(child, type, *rid, res) != 0) { 1406 rman_release_resource(res); 1407 return (NULL); 1408 } 1409 1410 return (res); 1411} 1412 1413static int 1414acpi_is_resource_managed(int type, struct resource *r) 1415{ 1416 1417 /* We only handle memory and IO resources through rman. */ 1418 switch (type) { 1419 case SYS_RES_IOPORT: 1420 return (rman_is_region_manager(r, &acpi_rman_io)); 1421 case SYS_RES_MEMORY: 1422 return (rman_is_region_manager(r, &acpi_rman_mem)); 1423 } 1424 return (0); 1425} 1426 1427static int 1428acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 1429 u_long start, u_long end) 1430{ 1431 1432 if (acpi_is_resource_managed(type, r)) 1433 return (rman_adjust_resource(r, start, end)); 1434 return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 1435} 1436 1437static int 1438acpi_release_resource(device_t bus, device_t child, int type, int rid, 1439 struct resource *r) 1440{ 1441 int ret; 1442 1443 /* 1444 * If this resource belongs to one of our internal managers, 1445 * deactivate it and release it to the local pool. 1446 */ 1447 if (acpi_is_resource_managed(type, r)) { 1448 if (rman_get_flags(r) & RF_ACTIVE) { 1449 ret = bus_deactivate_resource(child, type, rid, r); 1450 if (ret != 0) 1451 return (ret); 1452 } 1453 return (rman_release_resource(r)); 1454 } 1455 1456 return (bus_generic_rl_release_resource(bus, child, type, rid, r)); 1457} 1458 1459static void 1460acpi_delete_resource(device_t bus, device_t child, int type, int rid) 1461{ 1462 struct resource_list *rl; 1463 1464 rl = acpi_get_rlist(bus, child); 1465 if (resource_list_busy(rl, type, rid)) { 1466 device_printf(bus, "delete_resource: Resource still owned by child" 1467 " (type=%d, rid=%d)\n", type, rid); 1468 return; 1469 } 1470 resource_list_unreserve(rl, bus, child, type, rid); 1471 resource_list_delete(rl, type, rid); 1472} 1473 1474/* Allocate an IO port or memory resource, given its GAS. */ 1475int 1476acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1477 struct resource **res, u_int flags) 1478{ 1479 int error, res_type; 1480 1481 error = ENOMEM; 1482 if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1483 return (EINVAL); 1484 1485 /* We only support memory and IO spaces. */ 1486 switch (gas->SpaceId) { 1487 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1488 res_type = SYS_RES_MEMORY; 1489 break; 1490 case ACPI_ADR_SPACE_SYSTEM_IO: 1491 res_type = SYS_RES_IOPORT; 1492 break; 1493 default: 1494 return (EOPNOTSUPP); 1495 } 1496 1497 /* 1498 * If the register width is less than 8, assume the BIOS author means 1499 * it is a bit field and just allocate a byte. 1500 */ 1501 if (gas->BitWidth && gas->BitWidth < 8) 1502 gas->BitWidth = 8; 1503 1504 /* Validate the address after we're sure we support the space. */ 1505 if (gas->Address == 0 || gas->BitWidth == 0) 1506 return (EINVAL); 1507 1508 bus_set_resource(dev, res_type, *rid, gas->Address, 1509 gas->BitWidth / 8); 1510 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1511 if (*res != NULL) { 1512 *type = res_type; 1513 error = 0; 1514 } else 1515 bus_delete_resource(dev, res_type, *rid); 1516 1517 return (error); 1518} 1519 1520/* Probe _HID and _CID for compatible ISA PNP ids. */ 1521static uint32_t 1522acpi_isa_get_logicalid(device_t dev) 1523{ 1524 ACPI_DEVICE_INFO *devinfo; 1525 ACPI_HANDLE h; 1526 uint32_t pnpid; 1527 1528 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1529 1530 /* Fetch and validate the HID. */ 1531 if ((h = acpi_get_handle(dev)) == NULL || 1532 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1533 return_VALUE (0); 1534 1535 pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 1536 devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 1537 PNP_EISAID(devinfo->HardwareId.String) : 0; 1538 AcpiOsFree(devinfo); 1539 1540 return_VALUE (pnpid); 1541} 1542 1543static int 1544acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1545{ 1546 ACPI_DEVICE_INFO *devinfo; 1547 ACPI_PNP_DEVICE_ID *ids; 1548 ACPI_HANDLE h; 1549 uint32_t *pnpid; 1550 int i, valid; 1551 1552 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1553 1554 pnpid = cids; 1555 1556 /* Fetch and validate the CID */ 1557 if ((h = acpi_get_handle(dev)) == NULL || 1558 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1559 return_VALUE (0); 1560 1561 if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 1562 AcpiOsFree(devinfo); 1563 return_VALUE (0); 1564 } 1565 1566 if (devinfo->CompatibleIdList.Count < count) 1567 count = devinfo->CompatibleIdList.Count; 1568 ids = devinfo->CompatibleIdList.Ids; 1569 for (i = 0, valid = 0; i < count; i++) 1570 if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 1571 strncmp(ids[i].String, "PNP", 3) == 0) { 1572 *pnpid++ = PNP_EISAID(ids[i].String); 1573 valid++; 1574 } 1575 AcpiOsFree(devinfo); 1576 1577 return_VALUE (valid); 1578} 1579 1580static char * 1581acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1582{ 1583 ACPI_HANDLE h; 1584 ACPI_OBJECT_TYPE t; 1585 int i; 1586 1587 h = acpi_get_handle(dev); 1588 if (ids == NULL || h == NULL) 1589 return (NULL); 1590 t = acpi_get_type(dev); 1591 if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 1592 return (NULL); 1593 1594 /* Try to match one of the array of IDs with a HID or CID. */ 1595 for (i = 0; ids[i] != NULL; i++) { 1596 if (acpi_MatchHid(h, ids[i])) 1597 return (ids[i]); 1598 } 1599 return (NULL); 1600} 1601 1602static ACPI_STATUS 1603acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1604 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1605{ 1606 ACPI_HANDLE h; 1607 1608 if (dev == NULL) 1609 h = ACPI_ROOT_OBJECT; 1610 else if ((h = acpi_get_handle(dev)) == NULL) 1611 return (AE_BAD_PARAMETER); 1612 return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1613} 1614 1615int 1616acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 1617{ 1618 struct acpi_softc *sc; 1619 ACPI_HANDLE handle; 1620 ACPI_STATUS status; 1621 char sxd[8]; 1622 1623 handle = acpi_get_handle(dev); 1624 1625 /* 1626 * XXX If we find these devices, don't try to power them down. 1627 * The serial and IRDA ports on my T23 hang the system when 1628 * set to D3 and it appears that such legacy devices may 1629 * need special handling in their drivers. 1630 */ 1631 if (dstate == NULL || handle == NULL || 1632 acpi_MatchHid(handle, "PNP0500") || 1633 acpi_MatchHid(handle, "PNP0501") || 1634 acpi_MatchHid(handle, "PNP0502") || 1635 acpi_MatchHid(handle, "PNP0510") || 1636 acpi_MatchHid(handle, "PNP0511")) 1637 return (ENXIO); 1638 1639 /* 1640 * Override next state with the value from _SxD, if present. 1641 * Note illegal _S0D is evaluated because some systems expect this. 1642 */ 1643 sc = device_get_softc(bus); 1644 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 1645 status = acpi_GetInteger(handle, sxd, dstate); 1646 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1647 device_printf(dev, "failed to get %s on %s: %s\n", sxd, 1648 acpi_name(handle), AcpiFormatException(status)); 1649 return (ENXIO); 1650 } 1651 1652 return (0); 1653} 1654 1655/* Callback arg for our implementation of walking the namespace. */ 1656struct acpi_device_scan_ctx { 1657 acpi_scan_cb_t user_fn; 1658 void *arg; 1659 ACPI_HANDLE parent; 1660}; 1661 1662static ACPI_STATUS 1663acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1664{ 1665 struct acpi_device_scan_ctx *ctx; 1666 device_t dev, old_dev; 1667 ACPI_STATUS status; 1668 ACPI_OBJECT_TYPE type; 1669 1670 /* 1671 * Skip this device if we think we'll have trouble with it or it is 1672 * the parent where the scan began. 1673 */ 1674 ctx = (struct acpi_device_scan_ctx *)arg; 1675 if (acpi_avoid(h) || h == ctx->parent) 1676 return (AE_OK); 1677 1678 /* If this is not a valid device type (e.g., a method), skip it. */ 1679 if (ACPI_FAILURE(AcpiGetType(h, &type))) 1680 return (AE_OK); 1681 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1682 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1683 return (AE_OK); 1684 1685 /* 1686 * Call the user function with the current device. If it is unchanged 1687 * afterwards, return. Otherwise, we update the handle to the new dev. 1688 */ 1689 old_dev = acpi_get_device(h); 1690 dev = old_dev; 1691 status = ctx->user_fn(h, &dev, level, ctx->arg); 1692 if (ACPI_FAILURE(status) || old_dev == dev) 1693 return (status); 1694 1695 /* Remove the old child and its connection to the handle. */ 1696 if (old_dev != NULL) { 1697 device_delete_child(device_get_parent(old_dev), old_dev); 1698 AcpiDetachData(h, acpi_fake_objhandler); 1699 } 1700 1701 /* Recreate the handle association if the user created a device. */ 1702 if (dev != NULL) 1703 AcpiAttachData(h, acpi_fake_objhandler, dev); 1704 1705 return (AE_OK); 1706} 1707 1708static ACPI_STATUS 1709acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1710 acpi_scan_cb_t user_fn, void *arg) 1711{ 1712 ACPI_HANDLE h; 1713 struct acpi_device_scan_ctx ctx; 1714 1715 if (acpi_disabled("children")) 1716 return (AE_OK); 1717 1718 if (dev == NULL) 1719 h = ACPI_ROOT_OBJECT; 1720 else if ((h = acpi_get_handle(dev)) == NULL) 1721 return (AE_BAD_PARAMETER); 1722 ctx.user_fn = user_fn; 1723 ctx.arg = arg; 1724 ctx.parent = h; 1725 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1726 acpi_device_scan_cb, NULL, &ctx, NULL)); 1727} 1728 1729/* 1730 * Even though ACPI devices are not PCI, we use the PCI approach for setting 1731 * device power states since it's close enough to ACPI. 1732 */ 1733static int 1734acpi_set_powerstate(device_t child, int state) 1735{ 1736 ACPI_HANDLE h; 1737 ACPI_STATUS status; 1738 1739 h = acpi_get_handle(child); 1740 if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 1741 return (EINVAL); 1742 if (h == NULL) 1743 return (0); 1744 1745 /* Ignore errors if the power methods aren't present. */ 1746 status = acpi_pwr_switch_consumer(h, state); 1747 if (ACPI_SUCCESS(status)) { 1748 if (bootverbose) 1749 device_printf(child, "set ACPI power state D%d on %s\n", 1750 state, acpi_name(h)); 1751 } else if (status != AE_NOT_FOUND) 1752 device_printf(child, 1753 "failed to set ACPI power state D%d on %s: %s\n", state, 1754 acpi_name(h), AcpiFormatException(status)); 1755 1756 return (0); 1757} 1758 1759static int 1760acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 1761{ 1762 int result, cid_count, i; 1763 uint32_t lid, cids[8]; 1764 1765 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1766 1767 /* 1768 * ISA-style drivers attached to ACPI may persist and 1769 * probe manually if we return ENOENT. We never want 1770 * that to happen, so don't ever return it. 1771 */ 1772 result = ENXIO; 1773 1774 /* Scan the supplied IDs for a match */ 1775 lid = acpi_isa_get_logicalid(child); 1776 cid_count = acpi_isa_get_compatid(child, cids, 8); 1777 while (ids && ids->ip_id) { 1778 if (lid == ids->ip_id) { 1779 result = 0; 1780 goto out; 1781 } 1782 for (i = 0; i < cid_count; i++) { 1783 if (cids[i] == ids->ip_id) { 1784 result = 0; 1785 goto out; 1786 } 1787 } 1788 ids++; 1789 } 1790 1791 out: 1792 if (result == 0 && ids->ip_desc) 1793 device_set_desc(child, ids->ip_desc); 1794 1795 return_VALUE (result); 1796} 1797 1798#if defined(__i386__) || defined(__amd64__) 1799/* 1800 * Look for a MCFG table. If it is present, use the settings for 1801 * domain (segment) 0 to setup PCI config space access via the memory 1802 * map. 1803 */ 1804static void 1805acpi_enable_pcie(void) 1806{ 1807 ACPI_TABLE_HEADER *hdr; 1808 ACPI_MCFG_ALLOCATION *alloc, *end; 1809 ACPI_STATUS status; 1810 1811 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1812 if (ACPI_FAILURE(status)) 1813 return; 1814 1815 end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1816 alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1817 while (alloc < end) { 1818 if (alloc->PciSegment == 0) { 1819 pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1820 alloc->EndBusNumber); 1821 return; 1822 } 1823 alloc++; 1824 } 1825} 1826#endif 1827 1828/* 1829 * Scan all of the ACPI namespace and attach child devices. 1830 * 1831 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 1832 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 1833 * However, in violation of the spec, some systems place their PCI link 1834 * devices in \, so we have to walk the whole namespace. We check the 1835 * type of namespace nodes, so this should be ok. 1836 */ 1837static void 1838acpi_probe_children(device_t bus) 1839{ 1840 1841 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1842 1843 /* 1844 * Scan the namespace and insert placeholders for all the devices that 1845 * we find. We also probe/attach any early devices. 1846 * 1847 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1848 * we want to create nodes for all devices, not just those that are 1849 * currently present. (This assumes that we don't want to create/remove 1850 * devices as they appear, which might be smarter.) 1851 */ 1852 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1853 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 1854 NULL, bus, NULL); 1855 1856 /* Pre-allocate resources for our rman from any sysresource devices. */ 1857 acpi_sysres_alloc(bus); 1858 1859 /* Reserve resources already allocated to children. */ 1860 acpi_reserve_resources(bus); 1861 1862 /* Create any static children by calling device identify methods. */ 1863 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1864 bus_generic_probe(bus); 1865 1866 /* Probe/attach all children, created statically and from the namespace. */ 1867 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 1868 bus_generic_attach(bus); 1869 1870 /* Attach wake sysctls. */ 1871 acpi_wake_sysctl_walk(bus); 1872 1873 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 1874 return_VOID; 1875} 1876 1877/* 1878 * Determine the probe order for a given device. 1879 */ 1880static void 1881acpi_probe_order(ACPI_HANDLE handle, int *order) 1882{ 1883 ACPI_OBJECT_TYPE type; 1884 1885 /* 1886 * 0. CPUs 1887 * 1. I/O port and memory system resource holders 1888 * 2. Clocks and timers (to handle early accesses) 1889 * 3. Embedded controllers (to handle early accesses) 1890 * 4. PCI Link Devices 1891 */ 1892 AcpiGetType(handle, &type); 1893 if (type == ACPI_TYPE_PROCESSOR) 1894 *order = 0; 1895 else if (acpi_MatchHid(handle, "PNP0C01") || 1896 acpi_MatchHid(handle, "PNP0C02")) 1897 *order = 1; 1898 else if (acpi_MatchHid(handle, "PNP0100") || 1899 acpi_MatchHid(handle, "PNP0103") || 1900 acpi_MatchHid(handle, "PNP0B00")) 1901 *order = 2; 1902 else if (acpi_MatchHid(handle, "PNP0C09")) 1903 *order = 3; 1904 else if (acpi_MatchHid(handle, "PNP0C0F")) 1905 *order = 4; 1906} 1907 1908/* 1909 * Evaluate a child device and determine whether we might attach a device to 1910 * it. 1911 */ 1912static ACPI_STATUS 1913acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 1914{ 1915 struct acpi_prw_data prw; 1916 ACPI_OBJECT_TYPE type; 1917 ACPI_HANDLE h; 1918 device_t bus, child; 1919 char *handle_str; 1920 int order; 1921 1922 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1923 1924 if (acpi_disabled("children")) 1925 return_ACPI_STATUS (AE_OK); 1926 1927 /* Skip this device if we think we'll have trouble with it. */ 1928 if (acpi_avoid(handle)) 1929 return_ACPI_STATUS (AE_OK); 1930 1931 bus = (device_t)context; 1932 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1933 handle_str = acpi_name(handle); 1934 switch (type) { 1935 case ACPI_TYPE_DEVICE: 1936 /* 1937 * Since we scan from \, be sure to skip system scope objects. 1938 * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around 1939 * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run 1940 * during the intialization and \_TZ_ is to support Notify() on it. 1941 */ 1942 if (strcmp(handle_str, "\\_SB_") == 0 || 1943 strcmp(handle_str, "\\_TZ_") == 0) 1944 break; 1945 if (acpi_parse_prw(handle, &prw) == 0) 1946 AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit); 1947 1948 /* 1949 * Ignore devices that do not have a _HID or _CID. They should 1950 * be discovered by other buses (e.g. the PCI bus driver). 1951 */ 1952 if (!acpi_has_hid(handle)) 1953 break; 1954 /* FALLTHROUGH */ 1955 case ACPI_TYPE_PROCESSOR: 1956 case ACPI_TYPE_THERMAL: 1957 case ACPI_TYPE_POWER: 1958 /* 1959 * Create a placeholder device for this node. Sort the 1960 * placeholder so that the probe/attach passes will run 1961 * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1962 * are reserved for special objects (i.e., system 1963 * resources). 1964 */ 1965 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1966 order = level * 10 + ACPI_DEV_BASE_ORDER; 1967 acpi_probe_order(handle, &order); 1968 child = BUS_ADD_CHILD(bus, order, NULL, -1); 1969 if (child == NULL) 1970 break; 1971 1972 /* Associate the handle with the device_t and vice versa. */ 1973 acpi_set_handle(child, handle); 1974 AcpiAttachData(handle, acpi_fake_objhandler, child); 1975 1976 /* 1977 * Check that the device is present. If it's not present, 1978 * leave it disabled (so that we have a device_t attached to 1979 * the handle, but we don't probe it). 1980 * 1981 * XXX PCI link devices sometimes report "present" but not 1982 * "functional" (i.e. if disabled). Go ahead and probe them 1983 * anyway since we may enable them later. 1984 */ 1985 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1986 /* Never disable PCI link devices. */ 1987 if (acpi_MatchHid(handle, "PNP0C0F")) 1988 break; 1989 /* 1990 * Docking stations should remain enabled since the system 1991 * may be undocked at boot. 1992 */ 1993 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1994 break; 1995 1996 device_disable(child); 1997 break; 1998 } 1999 2000 /* 2001 * Get the device's resource settings and attach them. 2002 * Note that if the device has _PRS but no _CRS, we need 2003 * to decide when it's appropriate to try to configure the 2004 * device. Ignore the return value here; it's OK for the 2005 * device not to have any resources. 2006 */ 2007 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 2008 break; 2009 } 2010 } 2011 2012 return_ACPI_STATUS (AE_OK); 2013} 2014 2015/* 2016 * AcpiAttachData() requires an object handler but never uses it. This is a 2017 * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 2018 */ 2019void 2020acpi_fake_objhandler(ACPI_HANDLE h, void *data) 2021{ 2022} 2023 2024static void 2025acpi_shutdown_final(void *arg, int howto) 2026{ 2027 struct acpi_softc *sc = (struct acpi_softc *)arg; 2028 register_t intr; 2029 ACPI_STATUS status; 2030 2031 /* 2032 * XXX Shutdown code should only run on the BSP (cpuid 0). 2033 * Some chipsets do not power off the system correctly if called from 2034 * an AP. 2035 */ 2036 if ((howto & RB_POWEROFF) != 0) { 2037 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 2038 if (ACPI_FAILURE(status)) { 2039 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2040 AcpiFormatException(status)); 2041 return; 2042 } 2043 device_printf(sc->acpi_dev, "Powering system off\n"); 2044 intr = intr_disable(); 2045 status = AcpiEnterSleepState(ACPI_STATE_S5); 2046 if (ACPI_FAILURE(status)) { 2047 intr_restore(intr); 2048 device_printf(sc->acpi_dev, "power-off failed - %s\n", 2049 AcpiFormatException(status)); 2050 } else { 2051 DELAY(1000000); 2052 intr_restore(intr); 2053 device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 2054 } 2055 } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) { 2056 /* Reboot using the reset register. */ 2057 status = AcpiReset(); 2058 if (ACPI_SUCCESS(status)) { 2059 DELAY(1000000); 2060 device_printf(sc->acpi_dev, "reset failed - timeout\n"); 2061 } else if (status != AE_NOT_EXIST) 2062 device_printf(sc->acpi_dev, "reset failed - %s\n", 2063 AcpiFormatException(status)); 2064 } else if (sc->acpi_do_disable && panicstr == NULL) { 2065 /* 2066 * Only disable ACPI if the user requested. On some systems, writing 2067 * the disable value to SMI_CMD hangs the system. 2068 */ 2069 device_printf(sc->acpi_dev, "Shutting down\n"); 2070 AcpiTerminate(); 2071 } 2072} 2073 2074static void 2075acpi_enable_fixed_events(struct acpi_softc *sc) 2076{ 2077 static int first_time = 1; 2078 2079 /* Enable and clear fixed events and install handlers. */ 2080 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 2081 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 2082 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 2083 acpi_event_power_button_sleep, sc); 2084 if (first_time) 2085 device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 2086 } 2087 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 2088 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 2089 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 2090 acpi_event_sleep_button_sleep, sc); 2091 if (first_time) 2092 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 2093 } 2094 2095 first_time = 0; 2096} 2097 2098/* 2099 * Returns true if the device is actually present and should 2100 * be attached to. This requires the present, enabled, UI-visible 2101 * and diagnostics-passed bits to be set. 2102 */ 2103BOOLEAN 2104acpi_DeviceIsPresent(device_t dev) 2105{ 2106 ACPI_DEVICE_INFO *devinfo; 2107 ACPI_HANDLE h; 2108 BOOLEAN present; 2109 2110 if ((h = acpi_get_handle(dev)) == NULL || 2111 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2112 return (FALSE); 2113 2114 /* If no _STA method, must be present */ 2115 present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 2116 ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2117 2118 AcpiOsFree(devinfo); 2119 return (present); 2120} 2121 2122/* 2123 * Returns true if the battery is actually present and inserted. 2124 */ 2125BOOLEAN 2126acpi_BatteryIsPresent(device_t dev) 2127{ 2128 ACPI_DEVICE_INFO *devinfo; 2129 ACPI_HANDLE h; 2130 BOOLEAN present; 2131 2132 if ((h = acpi_get_handle(dev)) == NULL || 2133 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2134 return (FALSE); 2135 2136 /* If no _STA method, must be present */ 2137 present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 2138 ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2139 2140 AcpiOsFree(devinfo); 2141 return (present); 2142} 2143 2144/* 2145 * Returns true if a device has at least one valid device ID. 2146 */ 2147static BOOLEAN 2148acpi_has_hid(ACPI_HANDLE h) 2149{ 2150 ACPI_DEVICE_INFO *devinfo; 2151 BOOLEAN ret; 2152 2153 if (h == NULL || 2154 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2155 return (FALSE); 2156 2157 ret = FALSE; 2158 if ((devinfo->Valid & ACPI_VALID_HID) != 0) 2159 ret = TRUE; 2160 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2161 if (devinfo->CompatibleIdList.Count > 0) 2162 ret = TRUE; 2163 2164 AcpiOsFree(devinfo); 2165 return (ret); 2166} 2167 2168/* 2169 * Match a HID string against a handle 2170 */ 2171BOOLEAN 2172acpi_MatchHid(ACPI_HANDLE h, const char *hid) 2173{ 2174 ACPI_DEVICE_INFO *devinfo; 2175 BOOLEAN ret; 2176 int i; 2177 2178 if (hid == NULL || h == NULL || 2179 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2180 return (FALSE); 2181 2182 ret = FALSE; 2183 if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 2184 strcmp(hid, devinfo->HardwareId.String) == 0) 2185 ret = TRUE; 2186 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2187 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 2188 if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 2189 ret = TRUE; 2190 break; 2191 } 2192 } 2193 2194 AcpiOsFree(devinfo); 2195 return (ret); 2196} 2197 2198/* 2199 * Return the handle of a named object within our scope, ie. that of (parent) 2200 * or one if its parents. 2201 */ 2202ACPI_STATUS 2203acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 2204{ 2205 ACPI_HANDLE r; 2206 ACPI_STATUS status; 2207 2208 /* Walk back up the tree to the root */ 2209 for (;;) { 2210 status = AcpiGetHandle(parent, path, &r); 2211 if (ACPI_SUCCESS(status)) { 2212 *result = r; 2213 return (AE_OK); 2214 } 2215 /* XXX Return error here? */ 2216 if (status != AE_NOT_FOUND) 2217 return (AE_OK); 2218 if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 2219 return (AE_NOT_FOUND); 2220 parent = r; 2221 } 2222} 2223 2224/* 2225 * Allocate a buffer with a preset data size. 2226 */ 2227ACPI_BUFFER * 2228acpi_AllocBuffer(int size) 2229{ 2230 ACPI_BUFFER *buf; 2231 2232 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2233 return (NULL); 2234 buf->Length = size; 2235 buf->Pointer = (void *)(buf + 1); 2236 return (buf); 2237} 2238 2239ACPI_STATUS 2240acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2241{ 2242 ACPI_OBJECT arg1; 2243 ACPI_OBJECT_LIST args; 2244 2245 arg1.Type = ACPI_TYPE_INTEGER; 2246 arg1.Integer.Value = number; 2247 args.Count = 1; 2248 args.Pointer = &arg1; 2249 2250 return (AcpiEvaluateObject(handle, path, &args, NULL)); 2251} 2252 2253/* 2254 * Evaluate a path that should return an integer. 2255 */ 2256ACPI_STATUS 2257acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2258{ 2259 ACPI_STATUS status; 2260 ACPI_BUFFER buf; 2261 ACPI_OBJECT param; 2262 2263 if (handle == NULL) 2264 handle = ACPI_ROOT_OBJECT; 2265 2266 /* 2267 * Assume that what we've been pointed at is an Integer object, or 2268 * a method that will return an Integer. 2269 */ 2270 buf.Pointer = ¶m; 2271 buf.Length = sizeof(param); 2272 status = AcpiEvaluateObject(handle, path, NULL, &buf); 2273 if (ACPI_SUCCESS(status)) { 2274 if (param.Type == ACPI_TYPE_INTEGER) 2275 *number = param.Integer.Value; 2276 else 2277 status = AE_TYPE; 2278 } 2279 2280 /* 2281 * In some applications, a method that's expected to return an Integer 2282 * may instead return a Buffer (probably to simplify some internal 2283 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 2284 * convert it into an Integer as best we can. 2285 * 2286 * This is a hack. 2287 */ 2288 if (status == AE_BUFFER_OVERFLOW) { 2289 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2290 status = AE_NO_MEMORY; 2291 } else { 2292 status = AcpiEvaluateObject(handle, path, NULL, &buf); 2293 if (ACPI_SUCCESS(status)) 2294 status = acpi_ConvertBufferToInteger(&buf, number); 2295 AcpiOsFree(buf.Pointer); 2296 } 2297 } 2298 return (status); 2299} 2300 2301ACPI_STATUS 2302acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2303{ 2304 ACPI_OBJECT *p; 2305 UINT8 *val; 2306 int i; 2307 2308 p = (ACPI_OBJECT *)bufp->Pointer; 2309 if (p->Type == ACPI_TYPE_INTEGER) { 2310 *number = p->Integer.Value; 2311 return (AE_OK); 2312 } 2313 if (p->Type != ACPI_TYPE_BUFFER) 2314 return (AE_TYPE); 2315 if (p->Buffer.Length > sizeof(int)) 2316 return (AE_BAD_DATA); 2317 2318 *number = 0; 2319 val = p->Buffer.Pointer; 2320 for (i = 0; i < p->Buffer.Length; i++) 2321 *number += val[i] << (i * 8); 2322 return (AE_OK); 2323} 2324 2325/* 2326 * Iterate over the elements of an a package object, calling the supplied 2327 * function for each element. 2328 * 2329 * XXX possible enhancement might be to abort traversal on error. 2330 */ 2331ACPI_STATUS 2332acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2333 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2334{ 2335 ACPI_OBJECT *comp; 2336 int i; 2337 2338 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2339 return (AE_BAD_PARAMETER); 2340 2341 /* Iterate over components */ 2342 i = 0; 2343 comp = pkg->Package.Elements; 2344 for (; i < pkg->Package.Count; i++, comp++) 2345 func(comp, arg); 2346 2347 return (AE_OK); 2348} 2349 2350/* 2351 * Find the (index)th resource object in a set. 2352 */ 2353ACPI_STATUS 2354acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 2355{ 2356 ACPI_RESOURCE *rp; 2357 int i; 2358 2359 rp = (ACPI_RESOURCE *)buf->Pointer; 2360 i = index; 2361 while (i-- > 0) { 2362 /* Range check */ 2363 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2364 return (AE_BAD_PARAMETER); 2365 2366 /* Check for terminator */ 2367 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2368 return (AE_NOT_FOUND); 2369 rp = ACPI_NEXT_RESOURCE(rp); 2370 } 2371 if (resp != NULL) 2372 *resp = rp; 2373 2374 return (AE_OK); 2375} 2376 2377/* 2378 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 2379 * 2380 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 2381 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 2382 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 2383 * resources. 2384 */ 2385#define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 2386 2387ACPI_STATUS 2388acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 2389{ 2390 ACPI_RESOURCE *rp; 2391 void *newp; 2392 2393 /* Initialise the buffer if necessary. */ 2394 if (buf->Pointer == NULL) { 2395 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 2396 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 2397 return (AE_NO_MEMORY); 2398 rp = (ACPI_RESOURCE *)buf->Pointer; 2399 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2400 rp->Length = ACPI_RS_SIZE_MIN; 2401 } 2402 if (res == NULL) 2403 return (AE_OK); 2404 2405 /* 2406 * Scan the current buffer looking for the terminator. 2407 * This will either find the terminator or hit the end 2408 * of the buffer and return an error. 2409 */ 2410 rp = (ACPI_RESOURCE *)buf->Pointer; 2411 for (;;) { 2412 /* Range check, don't go outside the buffer */ 2413 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2414 return (AE_BAD_PARAMETER); 2415 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2416 break; 2417 rp = ACPI_NEXT_RESOURCE(rp); 2418 } 2419 2420 /* 2421 * Check the size of the buffer and expand if required. 2422 * 2423 * Required size is: 2424 * size of existing resources before terminator + 2425 * size of new resource and header + 2426 * size of terminator. 2427 * 2428 * Note that this loop should really only run once, unless 2429 * for some reason we are stuffing a *really* huge resource. 2430 */ 2431 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2432 res->Length + ACPI_RS_SIZE_NO_DATA + 2433 ACPI_RS_SIZE_MIN) >= buf->Length) { 2434 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 2435 return (AE_NO_MEMORY); 2436 bcopy(buf->Pointer, newp, buf->Length); 2437 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2438 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 2439 AcpiOsFree(buf->Pointer); 2440 buf->Pointer = newp; 2441 buf->Length += buf->Length; 2442 } 2443 2444 /* Insert the new resource. */ 2445 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 2446 2447 /* And add the terminator. */ 2448 rp = ACPI_NEXT_RESOURCE(rp); 2449 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2450 rp->Length = ACPI_RS_SIZE_MIN; 2451 2452 return (AE_OK); 2453} 2454 2455/* 2456 * Set interrupt model. 2457 */ 2458ACPI_STATUS 2459acpi_SetIntrModel(int model) 2460{ 2461 2462 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2463} 2464 2465/* 2466 * Walk subtables of a table and call a callback routine for each 2467 * subtable. The caller should provide the first subtable and a 2468 * pointer to the end of the table. This can be used to walk tables 2469 * such as MADT and SRAT that use subtable entries. 2470 */ 2471void 2472acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2473 void *arg) 2474{ 2475 ACPI_SUBTABLE_HEADER *entry; 2476 2477 for (entry = first; (void *)entry < end; ) { 2478 /* Avoid an infinite loop if we hit a bogus entry. */ 2479 if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2480 return; 2481 2482 handler(entry, arg); 2483 entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2484 } 2485} 2486 2487/* 2488 * DEPRECATED. This interface has serious deficiencies and will be 2489 * removed. 2490 * 2491 * Immediately enter the sleep state. In the old model, acpiconf(8) ran 2492 * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 2493 */ 2494ACPI_STATUS 2495acpi_SetSleepState(struct acpi_softc *sc, int state) 2496{ 2497 static int once; 2498 2499 if (!once) { 2500 device_printf(sc->acpi_dev, 2501"warning: acpi_SetSleepState() deprecated, need to update your software\n"); 2502 once = 1; 2503 } 2504 return (acpi_EnterSleepState(sc, state)); 2505} 2506 2507#if defined(__amd64__) || defined(__i386__) 2508static void 2509acpi_sleep_force_task(void *context) 2510{ 2511 struct acpi_softc *sc = (struct acpi_softc *)context; 2512 2513 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2514 device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 2515 sc->acpi_next_sstate); 2516} 2517 2518static void 2519acpi_sleep_force(void *arg) 2520{ 2521 struct acpi_softc *sc = (struct acpi_softc *)arg; 2522 2523 device_printf(sc->acpi_dev, 2524 "suspend request timed out, forcing sleep now\n"); 2525 /* 2526 * XXX Suspending from callout cause the freeze in DEVICE_SUSPEND(). 2527 * Suspend from acpi_task thread in stead. 2528 */ 2529 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 2530 acpi_sleep_force_task, sc))) 2531 device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n"); 2532} 2533#endif 2534 2535/* 2536 * Request that the system enter the given suspend state. All /dev/apm 2537 * devices and devd(8) will be notified. Userland then has a chance to 2538 * save state and acknowledge the request. The system sleeps once all 2539 * acks are in. 2540 */ 2541int 2542acpi_ReqSleepState(struct acpi_softc *sc, int state) 2543{ 2544#if defined(__amd64__) || defined(__i386__) 2545 struct apm_clone_data *clone; 2546 ACPI_STATUS status; 2547 2548 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 2549 return (EINVAL); 2550 if (!acpi_sleep_states[state]) 2551 return (EOPNOTSUPP); 2552 2553 /* If a suspend request is already in progress, just return. */ 2554 if (sc->acpi_next_sstate != 0) { 2555 return (0); 2556 } 2557 2558 /* Wait until sleep is enabled. */ 2559 while (sc->acpi_sleep_disabled) { 2560 AcpiOsSleep(1000); 2561 } 2562 2563 ACPI_LOCK(acpi); 2564 2565 sc->acpi_next_sstate = state; 2566 2567 /* S5 (soft-off) should be entered directly with no waiting. */ 2568 if (state == ACPI_STATE_S5) { 2569 ACPI_UNLOCK(acpi); 2570 status = acpi_EnterSleepState(sc, state); 2571 return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2572 } 2573 2574 /* Record the pending state and notify all apm devices. */ 2575 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 2576 clone->notify_status = APM_EV_NONE; 2577 if ((clone->flags & ACPI_EVF_DEVD) == 0) { 2578 selwakeuppri(&clone->sel_read, PZERO); 2579 KNOTE_LOCKED(&clone->sel_read.si_note, 0); 2580 } 2581 } 2582 2583 /* If devd(8) is not running, immediately enter the sleep state. */ 2584 if (!devctl_process_running()) { 2585 ACPI_UNLOCK(acpi); 2586 status = acpi_EnterSleepState(sc, state); 2587 return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2588 } 2589 2590 /* 2591 * Set a timeout to fire if userland doesn't ack the suspend request 2592 * in time. This way we still eventually go to sleep if we were 2593 * overheating or running low on battery, even if userland is hung. 2594 * We cancel this timeout once all userland acks are in or the 2595 * suspend request is aborted. 2596 */ 2597 callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 2598 ACPI_UNLOCK(acpi); 2599 2600 /* Now notify devd(8) also. */ 2601 acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2602 2603 return (0); 2604#else 2605 /* This platform does not support acpi suspend/resume. */ 2606 return (EOPNOTSUPP); 2607#endif 2608} 2609 2610/* 2611 * Acknowledge (or reject) a pending sleep state. The caller has 2612 * prepared for suspend and is now ready for it to proceed. If the 2613 * error argument is non-zero, it indicates suspend should be cancelled 2614 * and gives an errno value describing why. Once all votes are in, 2615 * we suspend the system. 2616 */ 2617int 2618acpi_AckSleepState(struct apm_clone_data *clone, int error) 2619{ 2620#if defined(__amd64__) || defined(__i386__) 2621 struct acpi_softc *sc; 2622 int ret, sleeping; 2623 2624 /* If no pending sleep state, return an error. */ 2625 ACPI_LOCK(acpi); 2626 sc = clone->acpi_sc; 2627 if (sc->acpi_next_sstate == 0) { 2628 ACPI_UNLOCK(acpi); 2629 return (ENXIO); 2630 } 2631 2632 /* Caller wants to abort suspend process. */ 2633 if (error) { 2634 sc->acpi_next_sstate = 0; 2635 callout_stop(&sc->susp_force_to); 2636 device_printf(sc->acpi_dev, 2637 "listener on %s cancelled the pending suspend\n", 2638 devtoname(clone->cdev)); 2639 ACPI_UNLOCK(acpi); 2640 return (0); 2641 } 2642 2643 /* 2644 * Mark this device as acking the suspend request. Then, walk through 2645 * all devices, seeing if they agree yet. We only count devices that 2646 * are writable since read-only devices couldn't ack the request. 2647 */ 2648 sleeping = TRUE; 2649 clone->notify_status = APM_EV_ACKED; 2650 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 2651 if ((clone->flags & ACPI_EVF_WRITE) != 0 && 2652 clone->notify_status != APM_EV_ACKED) { 2653 sleeping = FALSE; 2654 break; 2655 } 2656 } 2657 2658 /* If all devices have voted "yes", we will suspend now. */ 2659 if (sleeping) 2660 callout_stop(&sc->susp_force_to); 2661 ACPI_UNLOCK(acpi); 2662 ret = 0; 2663 if (sleeping) { 2664 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2665 ret = ENODEV; 2666 } 2667 return (ret); 2668#else 2669 /* This platform does not support acpi suspend/resume. */ 2670 return (EOPNOTSUPP); 2671#endif 2672} 2673 2674static void 2675acpi_sleep_enable(void *arg) 2676{ 2677 struct acpi_softc *sc = (struct acpi_softc *)arg; 2678 2679 /* Reschedule if the system is not fully up and running. */ 2680 if (!AcpiGbl_SystemAwakeAndRunning) { 2681 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2682 return; 2683 } 2684 2685 ACPI_LOCK(acpi); 2686 sc->acpi_sleep_disabled = FALSE; 2687 ACPI_UNLOCK(acpi); 2688} 2689 2690static ACPI_STATUS 2691acpi_sleep_disable(struct acpi_softc *sc) 2692{ 2693 ACPI_STATUS status; 2694 2695 /* Fail if the system is not fully up and running. */ 2696 if (!AcpiGbl_SystemAwakeAndRunning) 2697 return (AE_ERROR); 2698 2699 ACPI_LOCK(acpi); 2700 status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2701 sc->acpi_sleep_disabled = TRUE; 2702 ACPI_UNLOCK(acpi); 2703 2704 return (status); 2705} 2706 2707enum acpi_sleep_state { 2708 ACPI_SS_NONE, 2709 ACPI_SS_GPE_SET, 2710 ACPI_SS_DEV_SUSPEND, 2711 ACPI_SS_SLP_PREP, 2712 ACPI_SS_SLEPT, 2713}; 2714 2715/* 2716 * Enter the desired system sleep state. 2717 * 2718 * Currently we support S1-S5 but S4 is only S4BIOS 2719 */ 2720static ACPI_STATUS 2721acpi_EnterSleepState(struct acpi_softc *sc, int state) 2722{ 2723 register_t intr; 2724 ACPI_STATUS status; 2725 ACPI_EVENT_STATUS power_button_status; 2726 enum acpi_sleep_state slp_state; 2727 int sleep_result; 2728 2729 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2730 2731 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 2732 return_ACPI_STATUS (AE_BAD_PARAMETER); 2733 if (!acpi_sleep_states[state]) { 2734 device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2735 state); 2736 return (AE_SUPPORT); 2737 } 2738 2739 /* Re-entry once we're suspending is not allowed. */ 2740 status = acpi_sleep_disable(sc); 2741 if (ACPI_FAILURE(status)) { 2742 device_printf(sc->acpi_dev, 2743 "suspend request ignored (not ready yet)\n"); 2744 return (status); 2745 } 2746 2747 if (state == ACPI_STATE_S5) { 2748 /* 2749 * Shut down cleanly and power off. This will call us back through the 2750 * shutdown handlers. 2751 */ 2752 shutdown_nice(RB_POWEROFF); 2753 return_ACPI_STATUS (AE_OK); 2754 } 2755 2756 EVENTHANDLER_INVOKE(power_suspend); 2757 2758 if (smp_started) { 2759 thread_lock(curthread); 2760 sched_bind(curthread, 0); 2761 thread_unlock(curthread); 2762 } 2763 2764 /* 2765 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2766 * drivers need this. 2767 */ 2768 mtx_lock(&Giant); 2769 2770 slp_state = ACPI_SS_NONE; 2771 2772 sc->acpi_sstate = state; 2773 2774 /* Enable any GPEs as appropriate and requested by the user. */ 2775 acpi_wake_prep_walk(state); 2776 slp_state = ACPI_SS_GPE_SET; 2777 2778 /* 2779 * Inform all devices that we are going to sleep. If at least one 2780 * device fails, DEVICE_SUSPEND() automatically resumes the tree. 2781 * 2782 * XXX Note that a better two-pass approach with a 'veto' pass 2783 * followed by a "real thing" pass would be better, but the current 2784 * bus interface does not provide for this. 2785 */ 2786 if (DEVICE_SUSPEND(root_bus) != 0) { 2787 device_printf(sc->acpi_dev, "device_suspend failed\n"); 2788 goto backout; 2789 } 2790 slp_state = ACPI_SS_DEV_SUSPEND; 2791 2792 /* If testing device suspend only, back out of everything here. */ 2793 if (acpi_susp_bounce) 2794 goto backout; 2795 2796 status = AcpiEnterSleepStatePrep(state); 2797 if (ACPI_FAILURE(status)) { 2798 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2799 AcpiFormatException(status)); 2800 goto backout; 2801 } 2802 slp_state = ACPI_SS_SLP_PREP; 2803 2804 if (sc->acpi_sleep_delay > 0) 2805 DELAY(sc->acpi_sleep_delay * 1000000); 2806 2807 intr = intr_disable(); 2808 if (state != ACPI_STATE_S1) { 2809 sleep_result = acpi_sleep_machdep(sc, state); 2810 acpi_wakeup_machdep(sc, state, sleep_result, 0); 2811 2812 /* 2813 * XXX According to ACPI specification SCI_EN bit should be restored 2814 * by ACPI platform (BIOS, firmware) to its pre-sleep state. 2815 * Unfortunately some BIOSes fail to do that and that leads to 2816 * unexpected and serious consequences during wake up like a system 2817 * getting stuck in SMI handlers. 2818 * This hack is picked up from Linux, which claims that it follows 2819 * Windows behavior. 2820 */ 2821 if (sleep_result == 1 && state != ACPI_STATE_S4) 2822 AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT); 2823 2824 AcpiLeaveSleepStatePrep(state); 2825 2826 if (sleep_result == 1 && state == ACPI_STATE_S3) { 2827 /* 2828 * Prevent mis-interpretation of the wakeup by power button 2829 * as a request for power off. 2830 * Ideally we should post an appropriate wakeup event, 2831 * perhaps using acpi_event_power_button_wake or alike. 2832 * 2833 * Clearing of power button status after wakeup is mandated 2834 * by ACPI specification in section "Fixed Power Button". 2835 * 2836 * XXX As of ACPICA 20121114 AcpiGetEventStatus provides 2837 * status as 0/1 corressponding to inactive/active despite 2838 * its type being ACPI_EVENT_STATUS. In other words, 2839 * we should not test for ACPI_EVENT_FLAG_SET for time being. 2840 */ 2841 if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON, 2842 &power_button_status)) && power_button_status != 0) { 2843 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 2844 device_printf(sc->acpi_dev, 2845 "cleared fixed power button status\n"); 2846 } 2847 } 2848 2849 intr_restore(intr); 2850 2851 /* call acpi_wakeup_machdep() again with interrupt enabled */ 2852 acpi_wakeup_machdep(sc, state, sleep_result, 1); 2853 2854 if (sleep_result == -1) 2855 goto backout; 2856 2857 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2858 if (state == ACPI_STATE_S4) 2859 AcpiEnable(); 2860 } else { 2861 status = AcpiEnterSleepState(state); 2862 AcpiLeaveSleepStatePrep(state); 2863 intr_restore(intr); 2864 if (ACPI_FAILURE(status)) { 2865 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2866 AcpiFormatException(status)); 2867 goto backout; 2868 } 2869 } 2870 slp_state = ACPI_SS_SLEPT; 2871 2872 /* 2873 * Back out state according to how far along we got in the suspend 2874 * process. This handles both the error and success cases. 2875 */ 2876backout: 2877 if (slp_state >= ACPI_SS_GPE_SET) { 2878 acpi_wake_prep_walk(state); 2879 sc->acpi_sstate = ACPI_STATE_S0; 2880 } 2881 if (slp_state >= ACPI_SS_DEV_SUSPEND) 2882 DEVICE_RESUME(root_bus); 2883 if (slp_state >= ACPI_SS_SLP_PREP) 2884 AcpiLeaveSleepState(state); 2885 if (slp_state >= ACPI_SS_SLEPT) { 2886 acpi_resync_clock(sc); 2887 acpi_enable_fixed_events(sc); 2888 } 2889 sc->acpi_next_sstate = 0; 2890 2891 mtx_unlock(&Giant); 2892 2893 if (smp_started) { 2894 thread_lock(curthread); 2895 sched_unbind(curthread); 2896 thread_unlock(curthread); 2897 } 2898 2899 EVENTHANDLER_INVOKE(power_resume); 2900 2901 /* Allow another sleep request after a while. */ 2902 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2903 2904 /* Run /etc/rc.resume after we are back. */ 2905 if (devctl_process_running()) 2906 acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2907 2908 return_ACPI_STATUS (status); 2909} 2910 2911static void 2912acpi_resync_clock(struct acpi_softc *sc) 2913{ 2914#ifdef __amd64__ 2915 if (!acpi_reset_clock) 2916 return; 2917 2918 /* 2919 * Warm up timecounter again and reset system clock. 2920 */ 2921 (void)timecounter->tc_get_timecount(timecounter); 2922 (void)timecounter->tc_get_timecount(timecounter); 2923 inittodr(time_second + sc->acpi_sleep_delay); 2924#endif 2925} 2926 2927/* Enable or disable the device's wake GPE. */ 2928int 2929acpi_wake_set_enable(device_t dev, int enable) 2930{ 2931 struct acpi_prw_data prw; 2932 ACPI_STATUS status; 2933 int flags; 2934 2935 /* Make sure the device supports waking the system and get the GPE. */ 2936 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2937 return (ENXIO); 2938 2939 flags = acpi_get_flags(dev); 2940 if (enable) { 2941 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 2942 ACPI_GPE_ENABLE); 2943 if (ACPI_FAILURE(status)) { 2944 device_printf(dev, "enable wake failed\n"); 2945 return (ENXIO); 2946 } 2947 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2948 } else { 2949 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 2950 ACPI_GPE_DISABLE); 2951 if (ACPI_FAILURE(status)) { 2952 device_printf(dev, "disable wake failed\n"); 2953 return (ENXIO); 2954 } 2955 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2956 } 2957 2958 return (0); 2959} 2960 2961static int 2962acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2963{ 2964 struct acpi_prw_data prw; 2965 device_t dev; 2966 2967 /* Check that this is a wake-capable device and get its GPE. */ 2968 if (acpi_parse_prw(handle, &prw) != 0) 2969 return (ENXIO); 2970 dev = acpi_get_device(handle); 2971 2972 /* 2973 * The destination sleep state must be less than (i.e., higher power) 2974 * or equal to the value specified by _PRW. If this GPE cannot be 2975 * enabled for the next sleep state, then disable it. If it can and 2976 * the user requested it be enabled, turn on any required power resources 2977 * and set _PSW. 2978 */ 2979 if (sstate > prw.lowest_wake) { 2980 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 2981 if (bootverbose) 2982 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2983 acpi_name(handle), sstate); 2984 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2985 acpi_pwr_wake_enable(handle, 1); 2986 acpi_SetInteger(handle, "_PSW", 1); 2987 if (bootverbose) 2988 device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2989 acpi_name(handle), sstate); 2990 } 2991 2992 return (0); 2993} 2994 2995static int 2996acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2997{ 2998 struct acpi_prw_data prw; 2999 device_t dev; 3000 3001 /* 3002 * Check that this is a wake-capable device and get its GPE. Return 3003 * now if the user didn't enable this device for wake. 3004 */ 3005 if (acpi_parse_prw(handle, &prw) != 0) 3006 return (ENXIO); 3007 dev = acpi_get_device(handle); 3008 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 3009 return (0); 3010 3011 /* 3012 * If this GPE couldn't be enabled for the previous sleep state, it was 3013 * disabled before going to sleep so re-enable it. If it was enabled, 3014 * clear _PSW and turn off any power resources it used. 3015 */ 3016 if (sstate > prw.lowest_wake) { 3017 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 3018 if (bootverbose) 3019 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 3020 } else { 3021 acpi_SetInteger(handle, "_PSW", 0); 3022 acpi_pwr_wake_enable(handle, 0); 3023 if (bootverbose) 3024 device_printf(dev, "run_prep cleaned up for %s\n", 3025 acpi_name(handle)); 3026 } 3027 3028 return (0); 3029} 3030 3031static ACPI_STATUS 3032acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 3033{ 3034 int sstate; 3035 3036 /* If suspending, run the sleep prep function, otherwise wake. */ 3037 sstate = *(int *)context; 3038 if (AcpiGbl_SystemAwakeAndRunning) 3039 acpi_wake_sleep_prep(handle, sstate); 3040 else 3041 acpi_wake_run_prep(handle, sstate); 3042 return (AE_OK); 3043} 3044 3045/* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 3046static int 3047acpi_wake_prep_walk(int sstate) 3048{ 3049 ACPI_HANDLE sb_handle; 3050 3051 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 3052 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 3053 acpi_wake_prep, NULL, &sstate, NULL); 3054 return (0); 3055} 3056 3057/* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 3058static int 3059acpi_wake_sysctl_walk(device_t dev) 3060{ 3061 int error, i, numdevs; 3062 device_t *devlist; 3063 device_t child; 3064 ACPI_STATUS status; 3065 3066 error = device_get_children(dev, &devlist, &numdevs); 3067 if (error != 0 || numdevs == 0) { 3068 if (numdevs == 0) 3069 free(devlist, M_TEMP); 3070 return (error); 3071 } 3072 for (i = 0; i < numdevs; i++) { 3073 child = devlist[i]; 3074 acpi_wake_sysctl_walk(child); 3075 if (!device_is_attached(child)) 3076 continue; 3077 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 3078 if (ACPI_SUCCESS(status)) { 3079 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 3080 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 3081 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 3082 acpi_wake_set_sysctl, "I", "Device set to wake the system"); 3083 } 3084 } 3085 free(devlist, M_TEMP); 3086 3087 return (0); 3088} 3089 3090/* Enable or disable wake from userland. */ 3091static int 3092acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 3093{ 3094 int enable, error; 3095 device_t dev; 3096 3097 dev = (device_t)arg1; 3098 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 3099 3100 error = sysctl_handle_int(oidp, &enable, 0, req); 3101 if (error != 0 || req->newptr == NULL) 3102 return (error); 3103 if (enable != 0 && enable != 1) 3104 return (EINVAL); 3105 3106 return (acpi_wake_set_enable(dev, enable)); 3107} 3108 3109/* Parse a device's _PRW into a structure. */ 3110int 3111acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 3112{ 3113 ACPI_STATUS status; 3114 ACPI_BUFFER prw_buffer; 3115 ACPI_OBJECT *res, *res2; 3116 int error, i, power_count; 3117 3118 if (h == NULL || prw == NULL) 3119 return (EINVAL); 3120 3121 /* 3122 * The _PRW object (7.2.9) is only required for devices that have the 3123 * ability to wake the system from a sleeping state. 3124 */ 3125 error = EINVAL; 3126 prw_buffer.Pointer = NULL; 3127 prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 3128 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 3129 if (ACPI_FAILURE(status)) 3130 return (ENOENT); 3131 res = (ACPI_OBJECT *)prw_buffer.Pointer; 3132 if (res == NULL) 3133 return (ENOENT); 3134 if (!ACPI_PKG_VALID(res, 2)) 3135 goto out; 3136 3137 /* 3138 * Element 1 of the _PRW object: 3139 * The lowest power system sleeping state that can be entered while still 3140 * providing wake functionality. The sleeping state being entered must 3141 * be less than (i.e., higher power) or equal to this value. 3142 */ 3143 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 3144 goto out; 3145 3146 /* 3147 * Element 0 of the _PRW object: 3148 */ 3149 switch (res->Package.Elements[0].Type) { 3150 case ACPI_TYPE_INTEGER: 3151 /* 3152 * If the data type of this package element is numeric, then this 3153 * _PRW package element is the bit index in the GPEx_EN, in the 3154 * GPE blocks described in the FADT, of the enable bit that is 3155 * enabled for the wake event. 3156 */ 3157 prw->gpe_handle = NULL; 3158 prw->gpe_bit = res->Package.Elements[0].Integer.Value; 3159 error = 0; 3160 break; 3161 case ACPI_TYPE_PACKAGE: 3162 /* 3163 * If the data type of this package element is a package, then this 3164 * _PRW package element is itself a package containing two 3165 * elements. The first is an object reference to the GPE Block 3166 * device that contains the GPE that will be triggered by the wake 3167 * event. The second element is numeric and it contains the bit 3168 * index in the GPEx_EN, in the GPE Block referenced by the 3169 * first element in the package, of the enable bit that is enabled for 3170 * the wake event. 3171 * 3172 * For example, if this field is a package then it is of the form: 3173 * Package() {\_SB.PCI0.ISA.GPE, 2} 3174 */ 3175 res2 = &res->Package.Elements[0]; 3176 if (!ACPI_PKG_VALID(res2, 2)) 3177 goto out; 3178 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 3179 if (prw->gpe_handle == NULL) 3180 goto out; 3181 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 3182 goto out; 3183 error = 0; 3184 break; 3185 default: 3186 goto out; 3187 } 3188 3189 /* Elements 2 to N of the _PRW object are power resources. */ 3190 power_count = res->Package.Count - 2; 3191 if (power_count > ACPI_PRW_MAX_POWERRES) { 3192 printf("ACPI device %s has too many power resources\n", acpi_name(h)); 3193 power_count = 0; 3194 } 3195 prw->power_res_count = power_count; 3196 for (i = 0; i < power_count; i++) 3197 prw->power_res[i] = res->Package.Elements[i]; 3198 3199out: 3200 if (prw_buffer.Pointer != NULL) 3201 AcpiOsFree(prw_buffer.Pointer); 3202 return (error); 3203} 3204 3205/* 3206 * ACPI Event Handlers 3207 */ 3208 3209/* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 3210 3211static void 3212acpi_system_eventhandler_sleep(void *arg, int state) 3213{ 3214 struct acpi_softc *sc = (struct acpi_softc *)arg; 3215 int ret; 3216 3217 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 3218 3219 /* Check if button action is disabled or unknown. */ 3220 if (state == ACPI_STATE_UNKNOWN) 3221 return; 3222 3223 /* Request that the system prepare to enter the given suspend state. */ 3224 ret = acpi_ReqSleepState(sc, state); 3225 if (ret != 0) 3226 device_printf(sc->acpi_dev, 3227 "request to enter state S%d failed (err %d)\n", state, ret); 3228 3229 return_VOID; 3230} 3231 3232static void 3233acpi_system_eventhandler_wakeup(void *arg, int state) 3234{ 3235 3236 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 3237 3238 /* Currently, nothing to do for wakeup. */ 3239 3240 return_VOID; 3241} 3242 3243/* 3244 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 3245 */ 3246static void 3247acpi_invoke_sleep_eventhandler(void *context) 3248{ 3249 3250 EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context); 3251} 3252 3253static void 3254acpi_invoke_wake_eventhandler(void *context) 3255{ 3256 3257 EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context); 3258} 3259 3260UINT32 3261acpi_event_power_button_sleep(void *context) 3262{ 3263 struct acpi_softc *sc = (struct acpi_softc *)context; 3264 3265 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3266 3267 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3268 acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx))) 3269 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3270 return_VALUE (ACPI_INTERRUPT_HANDLED); 3271} 3272 3273UINT32 3274acpi_event_power_button_wake(void *context) 3275{ 3276 struct acpi_softc *sc = (struct acpi_softc *)context; 3277 3278 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3279 3280 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3281 acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx))) 3282 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3283 return_VALUE (ACPI_INTERRUPT_HANDLED); 3284} 3285 3286UINT32 3287acpi_event_sleep_button_sleep(void *context) 3288{ 3289 struct acpi_softc *sc = (struct acpi_softc *)context; 3290 3291 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3292 3293 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3294 acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx))) 3295 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3296 return_VALUE (ACPI_INTERRUPT_HANDLED); 3297} 3298 3299UINT32 3300acpi_event_sleep_button_wake(void *context) 3301{ 3302 struct acpi_softc *sc = (struct acpi_softc *)context; 3303 3304 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3305 3306 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3307 acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx))) 3308 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3309 return_VALUE (ACPI_INTERRUPT_HANDLED); 3310} 3311 3312/* 3313 * XXX This static buffer is suboptimal. There is no locking so only 3314 * use this for single-threaded callers. 3315 */ 3316char * 3317acpi_name(ACPI_HANDLE handle) 3318{ 3319 ACPI_BUFFER buf; 3320 static char data[256]; 3321 3322 buf.Length = sizeof(data); 3323 buf.Pointer = data; 3324 3325 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3326 return (data); 3327 return ("(unknown)"); 3328} 3329 3330/* 3331 * Debugging/bug-avoidance. Avoid trying to fetch info on various 3332 * parts of the namespace. 3333 */ 3334int 3335acpi_avoid(ACPI_HANDLE handle) 3336{ 3337 char *cp, *env, *np; 3338 int len; 3339 3340 np = acpi_name(handle); 3341 if (*np == '\\') 3342 np++; 3343 if ((env = getenv("debug.acpi.avoid")) == NULL) 3344 return (0); 3345 3346 /* Scan the avoid list checking for a match */ 3347 cp = env; 3348 for (;;) { 3349 while (*cp != 0 && isspace(*cp)) 3350 cp++; 3351 if (*cp == 0) 3352 break; 3353 len = 0; 3354 while (cp[len] != 0 && !isspace(cp[len])) 3355 len++; 3356 if (!strncmp(cp, np, len)) { 3357 freeenv(env); 3358 return(1); 3359 } 3360 cp += len; 3361 } 3362 freeenv(env); 3363 3364 return (0); 3365} 3366 3367/* 3368 * Debugging/bug-avoidance. Disable ACPI subsystem components. 3369 */ 3370int 3371acpi_disabled(char *subsys) 3372{ 3373 char *cp, *env; 3374 int len; 3375 3376 if ((env = getenv("debug.acpi.disabled")) == NULL) 3377 return (0); 3378 if (strcmp(env, "all") == 0) { 3379 freeenv(env); 3380 return (1); 3381 } 3382 3383 /* Scan the disable list, checking for a match. */ 3384 cp = env; 3385 for (;;) { 3386 while (*cp != '\0' && isspace(*cp)) 3387 cp++; 3388 if (*cp == '\0') 3389 break; 3390 len = 0; 3391 while (cp[len] != '\0' && !isspace(cp[len])) 3392 len++; 3393 if (strncmp(cp, subsys, len) == 0) { 3394 freeenv(env); 3395 return (1); 3396 } 3397 cp += len; 3398 } 3399 freeenv(env); 3400 3401 return (0); 3402} 3403 3404/* 3405 * Control interface. 3406 * 3407 * We multiplex ioctls for all participating ACPI devices here. Individual 3408 * drivers wanting to be accessible via /dev/acpi should use the 3409 * register/deregister interface to make their handlers visible. 3410 */ 3411struct acpi_ioctl_hook 3412{ 3413 TAILQ_ENTRY(acpi_ioctl_hook) link; 3414 u_long cmd; 3415 acpi_ioctl_fn fn; 3416 void *arg; 3417}; 3418 3419static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 3420static int acpi_ioctl_hooks_initted; 3421 3422int 3423acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 3424{ 3425 struct acpi_ioctl_hook *hp; 3426 3427 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 3428 return (ENOMEM); 3429 hp->cmd = cmd; 3430 hp->fn = fn; 3431 hp->arg = arg; 3432 3433 ACPI_LOCK(acpi); 3434 if (acpi_ioctl_hooks_initted == 0) { 3435 TAILQ_INIT(&acpi_ioctl_hooks); 3436 acpi_ioctl_hooks_initted = 1; 3437 } 3438 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 3439 ACPI_UNLOCK(acpi); 3440 3441 return (0); 3442} 3443 3444void 3445acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 3446{ 3447 struct acpi_ioctl_hook *hp; 3448 3449 ACPI_LOCK(acpi); 3450 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 3451 if (hp->cmd == cmd && hp->fn == fn) 3452 break; 3453 3454 if (hp != NULL) { 3455 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 3456 free(hp, M_ACPIDEV); 3457 } 3458 ACPI_UNLOCK(acpi); 3459} 3460 3461static int 3462acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 3463{ 3464 return (0); 3465} 3466 3467static int 3468acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 3469{ 3470 return (0); 3471} 3472 3473static int 3474acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 3475{ 3476 struct acpi_softc *sc; 3477 struct acpi_ioctl_hook *hp; 3478 int error, state; 3479 3480 error = 0; 3481 hp = NULL; 3482 sc = dev->si_drv1; 3483 3484 /* 3485 * Scan the list of registered ioctls, looking for handlers. 3486 */ 3487 ACPI_LOCK(acpi); 3488 if (acpi_ioctl_hooks_initted) 3489 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3490 if (hp->cmd == cmd) 3491 break; 3492 } 3493 ACPI_UNLOCK(acpi); 3494 if (hp) 3495 return (hp->fn(cmd, addr, hp->arg)); 3496 3497 /* 3498 * Core ioctls are not permitted for non-writable user. 3499 * Currently, other ioctls just fetch information. 3500 * Not changing system behavior. 3501 */ 3502 if ((flag & FWRITE) == 0) 3503 return (EPERM); 3504 3505 /* Core system ioctls. */ 3506 switch (cmd) { 3507 case ACPIIO_REQSLPSTATE: 3508 state = *(int *)addr; 3509 if (state != ACPI_STATE_S5) 3510 return (acpi_ReqSleepState(sc, state)); 3511 device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3512 error = EOPNOTSUPP; 3513 break; 3514 case ACPIIO_ACKSLPSTATE: 3515 error = *(int *)addr; 3516 error = acpi_AckSleepState(sc->acpi_clone, error); 3517 break; 3518 case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 3519 state = *(int *)addr; 3520 if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3521 return (EINVAL); 3522 if (!acpi_sleep_states[state]) 3523 return (EOPNOTSUPP); 3524 if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3525 error = ENXIO; 3526 break; 3527 default: 3528 error = ENXIO; 3529 break; 3530 } 3531 3532 return (error); 3533} 3534 3535static int 3536acpi_sname2sstate(const char *sname) 3537{ 3538 int sstate; 3539 3540 if (toupper(sname[0]) == 'S') { 3541 sstate = sname[1] - '0'; 3542 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3543 sname[2] == '\0') 3544 return (sstate); 3545 } else if (strcasecmp(sname, "NONE") == 0) 3546 return (ACPI_STATE_UNKNOWN); 3547 return (-1); 3548} 3549 3550static const char * 3551acpi_sstate2sname(int sstate) 3552{ 3553 static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3554 3555 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3556 return (snames[sstate]); 3557 else if (sstate == ACPI_STATE_UNKNOWN) 3558 return ("NONE"); 3559 return (NULL); 3560} 3561 3562static int 3563acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3564{ 3565 int error; 3566 struct sbuf sb; 3567 UINT8 state; 3568 3569 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3570 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3571 if (acpi_sleep_states[state]) 3572 sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3573 sbuf_trim(&sb); 3574 sbuf_finish(&sb); 3575 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3576 sbuf_delete(&sb); 3577 return (error); 3578} 3579 3580static int 3581acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3582{ 3583 char sleep_state[10]; 3584 int error, new_state, old_state; 3585 3586 old_state = *(int *)oidp->oid_arg1; 3587 strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 3588 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 3589 if (error == 0 && req->newptr != NULL) { 3590 new_state = acpi_sname2sstate(sleep_state); 3591 if (new_state < ACPI_STATE_S1) 3592 return (EINVAL); 3593 if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3594 return (EOPNOTSUPP); 3595 if (new_state != old_state) 3596 *(int *)oidp->oid_arg1 = new_state; 3597 } 3598 return (error); 3599} 3600 3601/* Inform devctl(4) when we receive a Notify. */ 3602void 3603acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 3604{ 3605 char notify_buf[16]; 3606 ACPI_BUFFER handle_buf; 3607 ACPI_STATUS status; 3608 3609 if (subsystem == NULL) 3610 return; 3611 3612 handle_buf.Pointer = NULL; 3613 handle_buf.Length = ACPI_ALLOCATE_BUFFER; 3614 status = AcpiNsHandleToPathname(h, &handle_buf); 3615 if (ACPI_FAILURE(status)) 3616 return; 3617 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 3618 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 3619 AcpiOsFree(handle_buf.Pointer); 3620} 3621 3622#ifdef ACPI_DEBUG 3623/* 3624 * Support for parsing debug options from the kernel environment. 3625 * 3626 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 3627 * by specifying the names of the bits in the debug.acpi.layer and 3628 * debug.acpi.level environment variables. Bits may be unset by 3629 * prefixing the bit name with !. 3630 */ 3631struct debugtag 3632{ 3633 char *name; 3634 UINT32 value; 3635}; 3636 3637static struct debugtag dbg_layer[] = { 3638 {"ACPI_UTILITIES", ACPI_UTILITIES}, 3639 {"ACPI_HARDWARE", ACPI_HARDWARE}, 3640 {"ACPI_EVENTS", ACPI_EVENTS}, 3641 {"ACPI_TABLES", ACPI_TABLES}, 3642 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3643 {"ACPI_PARSER", ACPI_PARSER}, 3644 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3645 {"ACPI_EXECUTER", ACPI_EXECUTER}, 3646 {"ACPI_RESOURCES", ACPI_RESOURCES}, 3647 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 3648 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3649 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3650 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 3651 3652 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3653 {"ACPI_BATTERY", ACPI_BATTERY}, 3654 {"ACPI_BUS", ACPI_BUS}, 3655 {"ACPI_BUTTON", ACPI_BUTTON}, 3656 {"ACPI_EC", ACPI_EC}, 3657 {"ACPI_FAN", ACPI_FAN}, 3658 {"ACPI_POWERRES", ACPI_POWERRES}, 3659 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3660 {"ACPI_THERMAL", ACPI_THERMAL}, 3661 {"ACPI_TIMER", ACPI_TIMER}, 3662 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 3663 {NULL, 0} 3664}; 3665 3666static struct debugtag dbg_level[] = { 3667 {"ACPI_LV_INIT", ACPI_LV_INIT}, 3668 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 3669 {"ACPI_LV_INFO", ACPI_LV_INFO}, 3670 {"ACPI_LV_REPAIR", ACPI_LV_REPAIR}, 3671 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3672 3673 /* Trace verbosity level 1 [Standard Trace Level] */ 3674 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 3675 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 3676 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3677 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 3678 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 3679 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 3680 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 3681 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 3682 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 3683 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 3684 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 3685 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 3686 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 3687 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3688 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3689 3690 /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3691 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3692 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3693 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3694 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 3695 {"ACPI_LV_ALL", ACPI_LV_ALL}, 3696 3697 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3698 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3699 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3700 {"ACPI_LV_IO", ACPI_LV_IO}, 3701 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3702 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3703 3704 /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 3705 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 3706 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 3707 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 3708 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 3709 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 3710 {NULL, 0} 3711}; 3712 3713static void 3714acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 3715{ 3716 char *ep; 3717 int i, l; 3718 int set; 3719 3720 while (*cp) { 3721 if (isspace(*cp)) { 3722 cp++; 3723 continue; 3724 } 3725 ep = cp; 3726 while (*ep && !isspace(*ep)) 3727 ep++; 3728 if (*cp == '!') { 3729 set = 0; 3730 cp++; 3731 if (cp == ep) 3732 continue; 3733 } else { 3734 set = 1; 3735 } 3736 l = ep - cp; 3737 for (i = 0; tag[i].name != NULL; i++) { 3738 if (!strncmp(cp, tag[i].name, l)) { 3739 if (set) 3740 *flag |= tag[i].value; 3741 else 3742 *flag &= ~tag[i].value; 3743 } 3744 } 3745 cp = ep; 3746 } 3747} 3748 3749static void 3750acpi_set_debugging(void *junk) 3751{ 3752 char *layer, *level; 3753 3754 if (cold) { 3755 AcpiDbgLayer = 0; 3756 AcpiDbgLevel = 0; 3757 } 3758 3759 layer = getenv("debug.acpi.layer"); 3760 level = getenv("debug.acpi.level"); 3761 if (layer == NULL && level == NULL) 3762 return; 3763 3764 printf("ACPI set debug"); 3765 if (layer != NULL) { 3766 if (strcmp("NONE", layer) != 0) 3767 printf(" layer '%s'", layer); 3768 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 3769 freeenv(layer); 3770 } 3771 if (level != NULL) { 3772 if (strcmp("NONE", level) != 0) 3773 printf(" level '%s'", level); 3774 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 3775 freeenv(level); 3776 } 3777 printf("\n"); 3778} 3779 3780SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3781 NULL); 3782 3783static int 3784acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 3785{ 3786 int error, *dbg; 3787 struct debugtag *tag; 3788 struct sbuf sb; 3789 char temp[128]; 3790 3791 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 3792 return (ENOMEM); 3793 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 3794 tag = &dbg_layer[0]; 3795 dbg = &AcpiDbgLayer; 3796 } else { 3797 tag = &dbg_level[0]; 3798 dbg = &AcpiDbgLevel; 3799 } 3800 3801 /* Get old values if this is a get request. */ 3802 ACPI_SERIAL_BEGIN(acpi); 3803 if (*dbg == 0) { 3804 sbuf_cpy(&sb, "NONE"); 3805 } else if (req->newptr == NULL) { 3806 for (; tag->name != NULL; tag++) { 3807 if ((*dbg & tag->value) == tag->value) 3808 sbuf_printf(&sb, "%s ", tag->name); 3809 } 3810 } 3811 sbuf_trim(&sb); 3812 sbuf_finish(&sb); 3813 strlcpy(temp, sbuf_data(&sb), sizeof(temp)); 3814 sbuf_delete(&sb); 3815 3816 error = sysctl_handle_string(oidp, temp, sizeof(temp), req); 3817 3818 /* Check for error or no change */ 3819 if (error == 0 && req->newptr != NULL) { 3820 *dbg = 0; 3821 setenv((char *)oidp->oid_arg1, temp); 3822 acpi_set_debugging(NULL); 3823 } 3824 ACPI_SERIAL_END(acpi); 3825 3826 return (error); 3827} 3828 3829SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 3830 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 3831SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 3832 "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3833#endif /* ACPI_DEBUG */ 3834 3835static int 3836acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 3837{ 3838 int error; 3839 int old; 3840 3841 old = acpi_debug_objects; 3842 error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 3843 if (error != 0 || req->newptr == NULL) 3844 return (error); 3845 if (old == acpi_debug_objects || (old && acpi_debug_objects)) 3846 return (0); 3847 3848 ACPI_SERIAL_BEGIN(acpi); 3849 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 3850 ACPI_SERIAL_END(acpi); 3851 3852 return (0); 3853} 3854 3855static int 3856acpi_parse_interfaces(char *str, struct acpi_interface *iface) 3857{ 3858 char *p; 3859 size_t len; 3860 int i, j; 3861 3862 p = str; 3863 while (isspace(*p) || *p == ',') 3864 p++; 3865 len = strlen(p); 3866 if (len == 0) 3867 return (0); 3868 p = strdup(p, M_TEMP); 3869 for (i = 0; i < len; i++) 3870 if (p[i] == ',') 3871 p[i] = '\0'; 3872 i = j = 0; 3873 while (i < len) 3874 if (isspace(p[i]) || p[i] == '\0') 3875 i++; 3876 else { 3877 i += strlen(p + i) + 1; 3878 j++; 3879 } 3880 if (j == 0) { 3881 free(p, M_TEMP); 3882 return (0); 3883 } 3884 iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK); 3885 iface->num = j; 3886 i = j = 0; 3887 while (i < len) 3888 if (isspace(p[i]) || p[i] == '\0') 3889 i++; 3890 else { 3891 iface->data[j] = p + i; 3892 i += strlen(p + i) + 1; 3893 j++; 3894 } 3895 3896 return (j); 3897} 3898 3899static void 3900acpi_free_interfaces(struct acpi_interface *iface) 3901{ 3902 3903 free(iface->data[0], M_TEMP); 3904 free(iface->data, M_TEMP); 3905} 3906 3907static void 3908acpi_reset_interfaces(device_t dev) 3909{ 3910 struct acpi_interface list; 3911 ACPI_STATUS status; 3912 int i; 3913 3914 if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) { 3915 for (i = 0; i < list.num; i++) { 3916 status = AcpiInstallInterface(list.data[i]); 3917 if (ACPI_FAILURE(status)) 3918 device_printf(dev, 3919 "failed to install _OSI(\"%s\"): %s\n", 3920 list.data[i], AcpiFormatException(status)); 3921 else if (bootverbose) 3922 device_printf(dev, "installed _OSI(\"%s\")\n", 3923 list.data[i]); 3924 } 3925 acpi_free_interfaces(&list); 3926 } 3927 if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) { 3928 for (i = 0; i < list.num; i++) { 3929 status = AcpiRemoveInterface(list.data[i]); 3930 if (ACPI_FAILURE(status)) 3931 device_printf(dev, 3932 "failed to remove _OSI(\"%s\"): %s\n", 3933 list.data[i], AcpiFormatException(status)); 3934 else if (bootverbose) 3935 device_printf(dev, "removed _OSI(\"%s\")\n", 3936 list.data[i]); 3937 } 3938 acpi_free_interfaces(&list); 3939 } 3940} 3941 3942static int 3943acpi_pm_func(u_long cmd, void *arg, ...) 3944{ 3945 int state, acpi_state; 3946 int error; 3947 struct acpi_softc *sc; 3948 va_list ap; 3949 3950 error = 0; 3951 switch (cmd) { 3952 case POWER_CMD_SUSPEND: 3953 sc = (struct acpi_softc *)arg; 3954 if (sc == NULL) { 3955 error = EINVAL; 3956 goto out; 3957 } 3958 3959 va_start(ap, arg); 3960 state = va_arg(ap, int); 3961 va_end(ap); 3962 3963 switch (state) { 3964 case POWER_SLEEP_STATE_STANDBY: 3965 acpi_state = sc->acpi_standby_sx; 3966 break; 3967 case POWER_SLEEP_STATE_SUSPEND: 3968 acpi_state = sc->acpi_suspend_sx; 3969 break; 3970 case POWER_SLEEP_STATE_HIBERNATE: 3971 acpi_state = ACPI_STATE_S4; 3972 break; 3973 default: 3974 error = EINVAL; 3975 goto out; 3976 } 3977 3978 if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 3979 error = ENXIO; 3980 break; 3981 default: 3982 error = EINVAL; 3983 goto out; 3984 } 3985 3986out: 3987 return (error); 3988} 3989 3990static void 3991acpi_pm_register(void *arg) 3992{ 3993 if (!cold || resource_disabled("acpi", 0)) 3994 return; 3995 3996 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3997} 3998 3999SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 4000