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