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