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