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