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