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