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