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