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