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