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 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/proc.h> 37 #include <sys/fcntl.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/conf.h> 42 #include <sys/ioccom.h> 43 #include <sys/reboot.h> 44 #include <sys/sysctl.h> 45 #include <sys/ctype.h> 46 #include <sys/linker.h> 47 #include <sys/power.h> 48 #include <sys/sbuf.h> 49 #include <sys/smp.h> 50 51 #include <machine/resource.h> 52 #include <machine/bus.h> 53 #include <sys/rman.h> 54 #include <isa/isavar.h> 55 #include <isa/pnpvar.h> 56 57 #include <contrib/dev/acpica/acpi.h> 58 #include <dev/acpica/acpivar.h> 59 #include <dev/acpica/acpiio.h> 60 #include <contrib/dev/acpica/achware.h> 61 #include <contrib/dev/acpica/acnamesp.h> 62 63 #include "pci_if.h" 64 #include <dev/pci/pcivar.h> 65 #include <dev/pci/pci_private.h> 66 67 #include <vm/vm_param.h> 68 69 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 70 71 /* Hooks for the ACPI CA debugging infrastructure */ 72 #define _COMPONENT ACPI_BUS 73 ACPI_MODULE_NAME("ACPI") 74 75 static d_open_t acpiopen; 76 static d_close_t acpiclose; 77 static d_ioctl_t acpiioctl; 78 79 static struct cdevsw acpi_cdevsw = { 80 .d_version = D_VERSION, 81 .d_open = acpiopen, 82 .d_close = acpiclose, 83 .d_ioctl = acpiioctl, 84 .d_name = "acpi", 85 }; 86 87 /* Global mutex for locking access to the ACPI subsystem. */ 88 struct mtx acpi_mutex; 89 90 /* Bitmap of device quirks. */ 91 int acpi_quirks; 92 93 static int acpi_modevent(struct module *mod, int event, void *junk); 94 static void acpi_identify(driver_t *driver, device_t parent); 95 static int acpi_probe(device_t dev); 96 static int acpi_attach(device_t dev); 97 static int acpi_suspend(device_t dev); 98 static int acpi_resume(device_t dev); 99 static int acpi_shutdown(device_t dev); 100 static device_t acpi_add_child(device_t bus, int order, const char *name, 101 int unit); 102 static int acpi_print_child(device_t bus, device_t child); 103 static void acpi_probe_nomatch(device_t bus, device_t child); 104 static void acpi_driver_added(device_t dev, driver_t *driver); 105 static int acpi_read_ivar(device_t dev, device_t child, int index, 106 uintptr_t *result); 107 static int acpi_write_ivar(device_t dev, device_t child, int index, 108 uintptr_t value); 109 static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 110 static int acpi_sysres_alloc(device_t dev); 111 static struct resource *acpi_alloc_resource(device_t bus, device_t child, 112 int type, int *rid, u_long start, u_long end, 113 u_long count, u_int flags); 114 static int acpi_release_resource(device_t bus, device_t child, int type, 115 int rid, struct resource *r); 116 static void acpi_delete_resource(device_t bus, device_t child, int type, 117 int rid); 118 static uint32_t acpi_isa_get_logicalid(device_t dev); 119 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 120 static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 121 static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 122 ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 123 ACPI_BUFFER *ret); 124 static int acpi_device_pwr_for_sleep(device_t bus, device_t dev, 125 int *dstate); 126 static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 127 void *context, void **retval); 128 static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 129 int max_depth, acpi_scan_cb_t user_fn, void *arg); 130 static int acpi_set_powerstate_method(device_t bus, device_t child, 131 int state); 132 static int acpi_isa_pnp_probe(device_t bus, device_t child, 133 struct isa_pnp_id *ids); 134 static void acpi_probe_children(device_t bus); 135 static int acpi_probe_order(ACPI_HANDLE handle, int *order); 136 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 137 void *context, void **status); 138 static BOOLEAN acpi_MatchHid(ACPI_HANDLE h, const char *hid); 139 static void acpi_shutdown_final(void *arg, int howto); 140 static void acpi_enable_fixed_events(struct acpi_softc *sc); 141 static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 142 static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 143 static int acpi_wake_prep_walk(int sstate); 144 static int acpi_wake_sysctl_walk(device_t dev); 145 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 146 static void acpi_system_eventhandler_sleep(void *arg, int state); 147 static void acpi_system_eventhandler_wakeup(void *arg, int state); 148 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 149 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 150 static int acpi_pm_func(u_long cmd, void *arg, ...); 151 static int acpi_child_location_str_method(device_t acdev, device_t child, 152 char *buf, size_t buflen); 153 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 154 char *buf, size_t buflen); 155 156 static device_method_t acpi_methods[] = { 157 /* Device interface */ 158 DEVMETHOD(device_identify, acpi_identify), 159 DEVMETHOD(device_probe, acpi_probe), 160 DEVMETHOD(device_attach, acpi_attach), 161 DEVMETHOD(device_shutdown, acpi_shutdown), 162 DEVMETHOD(device_detach, bus_generic_detach), 163 DEVMETHOD(device_suspend, acpi_suspend), 164 DEVMETHOD(device_resume, acpi_resume), 165 166 /* Bus interface */ 167 DEVMETHOD(bus_add_child, acpi_add_child), 168 DEVMETHOD(bus_print_child, acpi_print_child), 169 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 170 DEVMETHOD(bus_driver_added, acpi_driver_added), 171 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 172 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 173 DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 174 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 175 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 176 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 177 DEVMETHOD(bus_release_resource, acpi_release_resource), 178 DEVMETHOD(bus_delete_resource, acpi_delete_resource), 179 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 180 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 181 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 182 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 183 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 184 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 185 186 /* ACPI bus */ 187 DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 188 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 189 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 190 DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 191 192 /* PCI emulation */ 193 DEVMETHOD(pci_set_powerstate, acpi_set_powerstate_method), 194 195 /* ISA emulation */ 196 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 197 198 {0, 0} 199 }; 200 201 static driver_t acpi_driver = { 202 "acpi", 203 acpi_methods, 204 sizeof(struct acpi_softc), 205 }; 206 207 static devclass_t acpi_devclass; 208 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 209 MODULE_VERSION(acpi, 1); 210 211 ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 212 213 /* Local pools for managing system resources for ACPI child devices. */ 214 static struct rman acpi_rman_io, acpi_rman_mem; 215 216 #define ACPI_MINIMUM_AWAKETIME 5 217 218 static const char* sleep_state_names[] = { 219 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 220 221 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 222 static char acpi_ca_version[12]; 223 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 224 acpi_ca_version, 0, "Version of Intel ACPI-CA"); 225 226 /* 227 * Allow override of whether methods execute in parallel or not. 228 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 229 * errors for AML that really can't handle parallel method execution. 230 * It is off by default since this breaks recursive methods and 231 * some IBMs use such code. 232 */ 233 static int acpi_serialize_methods; 234 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 235 236 /* Power devices off and on in suspend and resume. XXX Remove once tested. */ 237 static int acpi_do_powerstate = 1; 238 TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate); 239 SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW, 240 &acpi_do_powerstate, 1, "Turn off devices when suspending."); 241 242 /* Allow users to override quirks. */ 243 TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 244 245 /* 246 * ACPI can only be loaded as a module by the loader; activating it after 247 * system bootstrap time is not useful, and can be fatal to the system. 248 * It also cannot be unloaded, since the entire system bus heirarchy hangs 249 * off it. 250 */ 251 static int 252 acpi_modevent(struct module *mod, int event, void *junk) 253 { 254 switch (event) { 255 case MOD_LOAD: 256 if (!cold) { 257 printf("The ACPI driver cannot be loaded after boot.\n"); 258 return (EPERM); 259 } 260 break; 261 case MOD_UNLOAD: 262 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 263 return (EBUSY); 264 break; 265 default: 266 break; 267 } 268 return (0); 269 } 270 271 /* 272 * Perform early initialization. 273 */ 274 ACPI_STATUS 275 acpi_Startup(void) 276 { 277 static int started = 0; 278 ACPI_STATUS status; 279 int val; 280 281 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 282 283 /* Only run the startup code once. The MADT driver also calls this. */ 284 if (started) 285 return_VALUE (AE_OK); 286 started = 1; 287 288 /* 289 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 290 * if more tables exist. 291 */ 292 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 293 printf("ACPI: Table initialisation failed: %s\n", 294 AcpiFormatException(status)); 295 return_VALUE (status); 296 } 297 298 /* Set up any quirks we have for this system. */ 299 if (acpi_quirks == ACPI_Q_OK) 300 acpi_table_quirks(&acpi_quirks); 301 302 /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 303 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 304 acpi_quirks &= ~ACPI_Q_BROKEN; 305 if (acpi_quirks & ACPI_Q_BROKEN) { 306 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 307 status = AE_SUPPORT; 308 } 309 310 return_VALUE (status); 311 } 312 313 /* 314 * Detect ACPI, perform early initialisation 315 */ 316 static void 317 acpi_identify(driver_t *driver, device_t parent) 318 { 319 device_t child; 320 321 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 322 323 if (!cold) 324 return_VOID; 325 326 /* Check that we haven't been disabled with a hint. */ 327 if (resource_disabled("acpi", 0)) 328 return_VOID; 329 330 /* Make sure we're not being doubly invoked. */ 331 if (device_find_child(parent, "acpi", 0) != NULL) 332 return_VOID; 333 334 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 335 336 /* Initialize root tables. */ 337 if (ACPI_FAILURE(acpi_Startup())) { 338 printf("ACPI: Try disabling either ACPI or apic support.\n"); 339 return_VOID; 340 } 341 342 /* Attach the actual ACPI device. */ 343 if ((child = BUS_ADD_CHILD(parent, 10, "acpi", 0)) == NULL) { 344 device_printf(parent, "device_identify failed\n"); 345 return_VOID; 346 } 347 } 348 349 /* 350 * Fetch some descriptive data from ACPI to put in our attach message. 351 */ 352 static int 353 acpi_probe(device_t dev) 354 { 355 ACPI_TABLE_RSDP *rsdp; 356 ACPI_TABLE_HEADER *rsdt; 357 ACPI_PHYSICAL_ADDRESS paddr; 358 char buf[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 359 struct sbuf sb; 360 361 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 362 363 if (power_pm_get_type() != POWER_PM_TYPE_NONE && 364 power_pm_get_type() != POWER_PM_TYPE_ACPI) { 365 device_printf(dev, "probe failed, other PM system enabled.\n"); 366 return_VALUE (ENXIO); 367 } 368 369 if ((paddr = AcpiOsGetRootPointer()) == 0 || 370 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 371 return_VALUE (ENXIO); 372 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 373 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 374 else 375 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 376 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 377 378 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 379 return_VALUE (ENXIO); 380 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 381 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 382 sbuf_trim(&sb); 383 sbuf_putc(&sb, ' '); 384 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 385 sbuf_trim(&sb); 386 sbuf_finish(&sb); 387 device_set_desc_copy(dev, sbuf_data(&sb)); 388 sbuf_delete(&sb); 389 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 390 391 return_VALUE (0); 392 } 393 394 static int 395 acpi_attach(device_t dev) 396 { 397 struct acpi_softc *sc; 398 ACPI_TABLE_FACS *facs; 399 ACPI_STATUS status; 400 int error, state; 401 UINT32 flags; 402 UINT8 TypeA, TypeB; 403 char *env; 404 405 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 406 407 sc = device_get_softc(dev); 408 sc->acpi_dev = dev; 409 410 error = ENXIO; 411 412 /* Initialize resource manager. */ 413 acpi_rman_io.rm_type = RMAN_ARRAY; 414 acpi_rman_io.rm_start = 0; 415 acpi_rman_io.rm_end = 0xffff; 416 acpi_rman_io.rm_descr = "ACPI I/O ports"; 417 if (rman_init(&acpi_rman_io) != 0) 418 panic("acpi rman_init IO ports failed"); 419 acpi_rman_mem.rm_type = RMAN_ARRAY; 420 acpi_rman_mem.rm_start = 0; 421 acpi_rman_mem.rm_end = ~0ul; 422 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 423 if (rman_init(&acpi_rman_mem) != 0) 424 panic("acpi rman_init memory failed"); 425 426 /* Initialise the ACPI mutex */ 427 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 428 429 /* 430 * Set the globals from our tunables. This is needed because ACPI-CA 431 * uses UINT8 for some values and we have no tunable_byte. 432 */ 433 AcpiGbl_AllMethodsSerialized = acpi_serialize_methods; 434 AcpiGbl_EnableInterpreterSlack = TRUE; 435 436 /* Start up the ACPI CA subsystem. */ 437 status = AcpiInitializeSubsystem(); 438 if (ACPI_FAILURE(status)) { 439 device_printf(dev, "Could not initialize Subsystem: %s\n", 440 AcpiFormatException(status)); 441 goto out; 442 } 443 444 /* Load ACPI name space. */ 445 status = AcpiLoadTables(); 446 if (ACPI_FAILURE(status)) { 447 device_printf(dev, "Could not load Namespace: %s\n", 448 AcpiFormatException(status)); 449 goto out; 450 } 451 452 /* Install the default address space handlers. */ 453 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 454 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 455 if (ACPI_FAILURE(status)) { 456 device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 457 AcpiFormatException(status)); 458 goto out; 459 } 460 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 461 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 462 if (ACPI_FAILURE(status)) { 463 device_printf(dev, "Could not initialise SystemIO handler: %s\n", 464 AcpiFormatException(status)); 465 goto out; 466 } 467 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 468 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 469 if (ACPI_FAILURE(status)) { 470 device_printf(dev, "could not initialise PciConfig handler: %s\n", 471 AcpiFormatException(status)); 472 goto out; 473 } 474 475 /* 476 * Note that some systems (specifically, those with namespace evaluation 477 * issues that require the avoidance of parts of the namespace) must 478 * avoid running _INI and _STA on everything, as well as dodging the final 479 * object init pass. 480 * 481 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 482 * 483 * XXX We should arrange for the object init pass after we have attached 484 * all our child devices, but on many systems it works here. 485 */ 486 flags = 0; 487 if (testenv("debug.acpi.avoid")) 488 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 489 490 /* Bring the hardware and basic handlers online. */ 491 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 492 device_printf(dev, "Could not enable ACPI: %s\n", 493 AcpiFormatException(status)); 494 goto out; 495 } 496 497 /* 498 * Call the ECDT probe function to provide EC functionality before 499 * the namespace has been evaluated. 500 * 501 * XXX This happens before the sysresource devices have been probed and 502 * attached so its resources come from nexus0. In practice, this isn't 503 * a problem but should be addressed eventually. 504 */ 505 acpi_ec_ecdt_probe(dev); 506 acpi_hpet_table_probe(dev); 507 508 /* Bring device objects and regions online. */ 509 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 510 device_printf(dev, "Could not initialize ACPI objects: %s\n", 511 AcpiFormatException(status)); 512 goto out; 513 } 514 515 /* 516 * Setup our sysctl tree. 517 * 518 * XXX: This doesn't check to make sure that none of these fail. 519 */ 520 sysctl_ctx_init(&sc->acpi_sysctl_ctx); 521 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 522 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 523 device_get_name(dev), CTLFLAG_RD, 0, ""); 524 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 525 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 526 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 527 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 528 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 529 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 530 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 531 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 532 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 533 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 534 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 535 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 536 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 537 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 538 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 539 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 540 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 541 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 542 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 543 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 544 "sleep delay"); 545 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 546 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 547 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 548 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 549 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 550 OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 551 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 552 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 553 OID_AUTO, "handle_reboot", CTLFLAG_RW, 554 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 555 556 /* 557 * Default to 1 second before sleeping to give some machines time to 558 * stabilize. 559 */ 560 sc->acpi_sleep_delay = 1; 561 if (bootverbose) 562 sc->acpi_verbose = 1; 563 if ((env = getenv("hw.acpi.verbose")) != NULL) { 564 if (strcmp(env, "0") != 0) 565 sc->acpi_verbose = 1; 566 freeenv(env); 567 } 568 569 /* Only enable S4BIOS by default if the FACS says it is available. */ 570 status = AcpiGetTable(ACPI_SIG_FACS, 0, (ACPI_TABLE_HEADER **)&facs); 571 if (ACPI_FAILURE(status)) { 572 device_printf(dev, "couldn't get FACS: %s\n", 573 AcpiFormatException(status)); 574 error = ENXIO; 575 goto out; 576 } 577 if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT) 578 sc->acpi_s4bios = 1; 579 580 /* 581 * Dispatch the default sleep state to devices. The lid switch is set 582 * to NONE by default to avoid surprising users. 583 */ 584 sc->acpi_power_button_sx = ACPI_STATE_S5; 585 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 586 sc->acpi_standby_sx = ACPI_STATE_S1; 587 sc->acpi_suspend_sx = ACPI_STATE_S3; 588 589 /* Pick the first valid sleep state for the sleep button default. */ 590 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 591 for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 592 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 593 sc->acpi_sleep_button_sx = state; 594 break; 595 } 596 597 acpi_enable_fixed_events(sc); 598 599 /* 600 * Scan the namespace and attach/initialise children. 601 */ 602 603 /* Register our shutdown handler. */ 604 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 605 SHUTDOWN_PRI_LAST); 606 607 /* 608 * Register our acpi event handlers. 609 * XXX should be configurable eg. via userland policy manager. 610 */ 611 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 612 sc, ACPI_EVENT_PRI_LAST); 613 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 614 sc, ACPI_EVENT_PRI_LAST); 615 616 /* Flag our initial states. */ 617 sc->acpi_enabled = 1; 618 sc->acpi_sstate = ACPI_STATE_S0; 619 sc->acpi_sleep_disabled = 0; 620 621 /* Create the control device */ 622 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 623 "acpi"); 624 sc->acpi_dev_t->si_drv1 = sc; 625 626 if ((error = acpi_machdep_init(dev))) 627 goto out; 628 629 /* Register ACPI again to pass the correct argument of pm_func. */ 630 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 631 632 if (!acpi_disabled("bus")) 633 acpi_probe_children(dev); 634 635 error = 0; 636 637 out: 638 return_VALUE (error); 639 } 640 641 static int 642 acpi_suspend(device_t dev) 643 { 644 device_t child, *devlist; 645 int error, i, numdevs, pstate; 646 647 GIANT_REQUIRED; 648 649 /* First give child devices a chance to suspend. */ 650 error = bus_generic_suspend(dev); 651 if (error) 652 return (error); 653 654 /* 655 * Now, set them into the appropriate power state, usually D3. If the 656 * device has an _SxD method for the next sleep state, use that power 657 * state instead. 658 */ 659 device_get_children(dev, &devlist, &numdevs); 660 for (i = 0; i < numdevs; i++) { 661 /* If the device is not attached, we've powered it down elsewhere. */ 662 child = devlist[i]; 663 if (!device_is_attached(child)) 664 continue; 665 666 /* 667 * Default to D3 for all sleep states. The _SxD method is optional 668 * so set the powerstate even if it's absent. 669 */ 670 pstate = PCI_POWERSTATE_D3; 671 error = acpi_device_pwr_for_sleep(device_get_parent(child), 672 child, &pstate); 673 if ((error == 0 || error == ESRCH) && acpi_do_powerstate) 674 pci_set_powerstate(child, pstate); 675 } 676 free(devlist, M_TEMP); 677 error = 0; 678 679 return (error); 680 } 681 682 static int 683 acpi_resume(device_t dev) 684 { 685 ACPI_HANDLE handle; 686 int i, numdevs; 687 device_t child, *devlist; 688 689 GIANT_REQUIRED; 690 691 /* 692 * Put all devices in D0 before resuming them. Call _S0D on each one 693 * since some systems expect this. 694 */ 695 device_get_children(dev, &devlist, &numdevs); 696 for (i = 0; i < numdevs; i++) { 697 child = devlist[i]; 698 handle = acpi_get_handle(child); 699 if (handle) 700 AcpiEvaluateObject(handle, "_S0D", NULL, NULL); 701 if (device_is_attached(child) && acpi_do_powerstate) 702 pci_set_powerstate(child, PCI_POWERSTATE_D0); 703 } 704 free(devlist, M_TEMP); 705 706 return (bus_generic_resume(dev)); 707 } 708 709 static int 710 acpi_shutdown(device_t dev) 711 { 712 713 GIANT_REQUIRED; 714 715 /* Allow children to shutdown first. */ 716 bus_generic_shutdown(dev); 717 718 /* 719 * Enable any GPEs that are able to power-on the system (i.e., RTC). 720 * Also, disable any that are not valid for this state (most). 721 */ 722 acpi_wake_prep_walk(ACPI_STATE_S5); 723 724 return (0); 725 } 726 727 /* 728 * Handle a new device being added 729 */ 730 static device_t 731 acpi_add_child(device_t bus, int order, const char *name, int unit) 732 { 733 struct acpi_device *ad; 734 device_t child; 735 736 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 737 return (NULL); 738 739 resource_list_init(&ad->ad_rl); 740 741 child = device_add_child_ordered(bus, order, name, unit); 742 if (child != NULL) 743 device_set_ivars(child, ad); 744 else 745 free(ad, M_ACPIDEV); 746 return (child); 747 } 748 749 static int 750 acpi_print_child(device_t bus, device_t child) 751 { 752 struct acpi_device *adev = device_get_ivars(child); 753 struct resource_list *rl = &adev->ad_rl; 754 int retval = 0; 755 756 retval += bus_print_child_header(bus, child); 757 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 758 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 759 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 760 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 761 if (device_get_flags(child)) 762 retval += printf(" flags %#x", device_get_flags(child)); 763 retval += bus_print_child_footer(bus, child); 764 765 return (retval); 766 } 767 768 /* 769 * If this device is an ACPI child but no one claimed it, attempt 770 * to power it off. We'll power it back up when a driver is added. 771 * 772 * XXX Disabled for now since many necessary devices (like fdc and 773 * ATA) don't claim the devices we created for them but still expect 774 * them to be powered up. 775 */ 776 static void 777 acpi_probe_nomatch(device_t bus, device_t child) 778 { 779 780 /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */ 781 } 782 783 /* 784 * If a new driver has a chance to probe a child, first power it up. 785 * 786 * XXX Disabled for now (see acpi_probe_nomatch for details). 787 */ 788 static void 789 acpi_driver_added(device_t dev, driver_t *driver) 790 { 791 device_t child, *devlist; 792 int i, numdevs; 793 794 DEVICE_IDENTIFY(driver, dev); 795 device_get_children(dev, &devlist, &numdevs); 796 for (i = 0; i < numdevs; i++) { 797 child = devlist[i]; 798 if (device_get_state(child) == DS_NOTPRESENT) { 799 /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */ 800 if (device_probe_and_attach(child) != 0) 801 ; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */ 802 } 803 } 804 free(devlist, M_TEMP); 805 } 806 807 /* Location hint for devctl(8) */ 808 static int 809 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 810 size_t buflen) 811 { 812 struct acpi_device *dinfo = device_get_ivars(child); 813 814 if (dinfo->ad_handle) 815 snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 816 else 817 snprintf(buf, buflen, "unknown"); 818 return (0); 819 } 820 821 /* PnP information for devctl(8) */ 822 static int 823 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 824 size_t buflen) 825 { 826 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 827 ACPI_DEVICE_INFO *adinfo; 828 struct acpi_device *dinfo = device_get_ivars(child); 829 char *end; 830 int error; 831 832 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 833 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 834 if (error) 835 snprintf(buf, buflen, "unknown"); 836 else 837 snprintf(buf, buflen, "_HID=%s _UID=%lu", 838 (adinfo->Valid & ACPI_VALID_HID) ? 839 adinfo->HardwareId.Value : "none", 840 (adinfo->Valid & ACPI_VALID_UID) ? 841 strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 842 if (adinfo) 843 AcpiOsFree(adinfo); 844 845 return (0); 846 } 847 848 /* 849 * Handle per-device ivars 850 */ 851 static int 852 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 853 { 854 struct acpi_device *ad; 855 856 if ((ad = device_get_ivars(child)) == NULL) { 857 printf("device has no ivars\n"); 858 return (ENOENT); 859 } 860 861 /* ACPI and ISA compatibility ivars */ 862 switch(index) { 863 case ACPI_IVAR_HANDLE: 864 *(ACPI_HANDLE *)result = ad->ad_handle; 865 break; 866 case ACPI_IVAR_MAGIC: 867 *(int *)result = ad->ad_magic; 868 break; 869 case ACPI_IVAR_PRIVATE: 870 *(void **)result = ad->ad_private; 871 break; 872 case ACPI_IVAR_FLAGS: 873 *(int *)result = ad->ad_flags; 874 break; 875 case ISA_IVAR_VENDORID: 876 case ISA_IVAR_SERIAL: 877 case ISA_IVAR_COMPATID: 878 *(int *)result = -1; 879 break; 880 case ISA_IVAR_LOGICALID: 881 *(int *)result = acpi_isa_get_logicalid(child); 882 break; 883 default: 884 return (ENOENT); 885 } 886 887 return (0); 888 } 889 890 static int 891 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 892 { 893 struct acpi_device *ad; 894 895 if ((ad = device_get_ivars(child)) == NULL) { 896 printf("device has no ivars\n"); 897 return (ENOENT); 898 } 899 900 switch(index) { 901 case ACPI_IVAR_HANDLE: 902 ad->ad_handle = (ACPI_HANDLE)value; 903 break; 904 case ACPI_IVAR_MAGIC: 905 ad->ad_magic = (int)value; 906 break; 907 case ACPI_IVAR_PRIVATE: 908 ad->ad_private = (void *)value; 909 break; 910 case ACPI_IVAR_FLAGS: 911 ad->ad_flags = (int)value; 912 break; 913 default: 914 panic("bad ivar write request (%d)", index); 915 return (ENOENT); 916 } 917 918 return (0); 919 } 920 921 /* 922 * Handle child resource allocation/removal 923 */ 924 static struct resource_list * 925 acpi_get_rlist(device_t dev, device_t child) 926 { 927 struct acpi_device *ad; 928 929 ad = device_get_ivars(child); 930 return (&ad->ad_rl); 931 } 932 933 /* 934 * Pre-allocate/manage all memory and IO resources. Since rman can't handle 935 * duplicates, we merge any in the sysresource attach routine. 936 */ 937 static int 938 acpi_sysres_alloc(device_t dev) 939 { 940 struct resource *res; 941 struct resource_list *rl; 942 struct resource_list_entry *rle; 943 struct rman *rm; 944 char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 945 device_t *children; 946 int child_count, i; 947 948 /* 949 * Probe/attach any sysresource devices. This would be unnecessary if we 950 * had multi-pass probe/attach. 951 */ 952 if (device_get_children(dev, &children, &child_count) != 0) 953 return (ENXIO); 954 for (i = 0; i < child_count; i++) { 955 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 956 device_probe_and_attach(children[i]); 957 } 958 free(children, M_TEMP); 959 960 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 961 STAILQ_FOREACH(rle, rl, link) { 962 if (rle->res != NULL) { 963 device_printf(dev, "duplicate resource for %lx\n", rle->start); 964 continue; 965 } 966 967 /* Only memory and IO resources are valid here. */ 968 switch (rle->type) { 969 case SYS_RES_IOPORT: 970 rm = &acpi_rman_io; 971 break; 972 case SYS_RES_MEMORY: 973 rm = &acpi_rman_mem; 974 break; 975 default: 976 continue; 977 } 978 979 /* Pre-allocate resource and add to our rman pool. */ 980 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 981 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 982 if (res != NULL) { 983 rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 984 rle->res = res; 985 } else 986 device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 987 rle->start, rle->count, rle->type); 988 } 989 return (0); 990 } 991 992 static struct resource * 993 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 994 u_long start, u_long end, u_long count, u_int flags) 995 { 996 ACPI_RESOURCE ares; 997 struct acpi_device *ad = device_get_ivars(child); 998 struct resource_list *rl = &ad->ad_rl; 999 struct resource_list_entry *rle; 1000 struct resource *res; 1001 struct rman *rm; 1002 1003 res = NULL; 1004 1005 /* We only handle memory and IO resources through rman. */ 1006 switch (type) { 1007 case SYS_RES_IOPORT: 1008 rm = &acpi_rman_io; 1009 break; 1010 case SYS_RES_MEMORY: 1011 rm = &acpi_rman_mem; 1012 break; 1013 default: 1014 rm = NULL; 1015 } 1016 1017 ACPI_SERIAL_BEGIN(acpi); 1018 1019 /* 1020 * If this is an allocation of the "default" range for a given RID, and 1021 * we know what the resources for this device are (i.e., they're on the 1022 * child's resource list), use those start/end values. 1023 */ 1024 if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) { 1025 rle = resource_list_find(rl, type, *rid); 1026 if (rle == NULL) 1027 goto out; 1028 start = rle->start; 1029 end = rle->end; 1030 count = rle->count; 1031 } 1032 1033 /* 1034 * If this is an allocation of a specific range, see if we can satisfy 1035 * the request from our system resource regions. If we can't, pass the 1036 * request up to the parent. 1037 */ 1038 if (start + count - 1 == end && rm != NULL) 1039 res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1040 child); 1041 if (res == NULL) { 1042 res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1043 start, end, count, flags); 1044 } else { 1045 rman_set_rid(res, *rid); 1046 1047 /* If requested, activate the resource using the parent's method. */ 1048 if (flags & RF_ACTIVE) 1049 if (bus_activate_resource(child, type, *rid, res) != 0) { 1050 rman_release_resource(res); 1051 res = NULL; 1052 goto out; 1053 } 1054 } 1055 1056 if (res != NULL && device_get_parent(child) == bus) 1057 switch (type) { 1058 case SYS_RES_IRQ: 1059 /* 1060 * Since bus_config_intr() takes immediate effect, we cannot 1061 * configure the interrupt associated with a device when we 1062 * parse the resources but have to defer it until a driver 1063 * actually allocates the interrupt via bus_alloc_resource(). 1064 * 1065 * XXX: Should we handle the lookup failing? 1066 */ 1067 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 1068 acpi_config_intr(child, &ares); 1069 break; 1070 } 1071 1072 out: 1073 ACPI_SERIAL_END(acpi); 1074 return (res); 1075 } 1076 1077 static int 1078 acpi_release_resource(device_t bus, device_t child, int type, int rid, 1079 struct resource *r) 1080 { 1081 struct rman *rm; 1082 int ret; 1083 1084 /* We only handle memory and IO resources through rman. */ 1085 switch (type) { 1086 case SYS_RES_IOPORT: 1087 rm = &acpi_rman_io; 1088 break; 1089 case SYS_RES_MEMORY: 1090 rm = &acpi_rman_mem; 1091 break; 1092 default: 1093 rm = NULL; 1094 } 1095 1096 ACPI_SERIAL_BEGIN(acpi); 1097 1098 /* 1099 * If this resource belongs to one of our internal managers, 1100 * deactivate it and release it to the local pool. If it doesn't, 1101 * pass this request up to the parent. 1102 */ 1103 if (rm != NULL && rman_is_region_manager(r, rm)) { 1104 if (rman_get_flags(r) & RF_ACTIVE) { 1105 ret = bus_deactivate_resource(child, type, rid, r); 1106 if (ret != 0) 1107 goto out; 1108 } 1109 ret = rman_release_resource(r); 1110 } else 1111 ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 1112 1113 out: 1114 ACPI_SERIAL_END(acpi); 1115 return (ret); 1116 } 1117 1118 static void 1119 acpi_delete_resource(device_t bus, device_t child, int type, int rid) 1120 { 1121 struct resource_list *rl; 1122 1123 rl = acpi_get_rlist(bus, child); 1124 resource_list_delete(rl, type, rid); 1125 } 1126 1127 /* Allocate an IO port or memory resource, given its GAS. */ 1128 int 1129 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1130 struct resource **res, u_int flags) 1131 { 1132 int error, res_type; 1133 1134 error = ENOMEM; 1135 if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1136 return (EINVAL); 1137 1138 /* We only support memory and IO spaces. */ 1139 switch (gas->SpaceId) { 1140 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1141 res_type = SYS_RES_MEMORY; 1142 break; 1143 case ACPI_ADR_SPACE_SYSTEM_IO: 1144 res_type = SYS_RES_IOPORT; 1145 break; 1146 default: 1147 return (EOPNOTSUPP); 1148 } 1149 1150 /* 1151 * If the register width is less than 8, assume the BIOS author means 1152 * it is a bit field and just allocate a byte. 1153 */ 1154 if (gas->BitWidth && gas->BitWidth < 8) 1155 gas->BitWidth = 8; 1156 1157 /* Validate the address after we're sure we support the space. */ 1158 if (gas->Address == 0 || gas->BitWidth == 0) 1159 return (EINVAL); 1160 1161 bus_set_resource(dev, res_type, *rid, gas->Address, 1162 gas->BitWidth / 8); 1163 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1164 if (*res != NULL) { 1165 *type = res_type; 1166 error = 0; 1167 } else 1168 bus_delete_resource(dev, res_type, *rid); 1169 1170 return (error); 1171 } 1172 1173 /* Probe _HID and _CID for compatible ISA PNP ids. */ 1174 static uint32_t 1175 acpi_isa_get_logicalid(device_t dev) 1176 { 1177 ACPI_DEVICE_INFO *devinfo; 1178 ACPI_BUFFER buf; 1179 ACPI_HANDLE h; 1180 ACPI_STATUS error; 1181 u_int32_t pnpid; 1182 1183 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1184 1185 pnpid = 0; 1186 buf.Pointer = NULL; 1187 buf.Length = ACPI_ALLOCATE_BUFFER; 1188 1189 /* Fetch and validate the HID. */ 1190 if ((h = acpi_get_handle(dev)) == NULL) 1191 goto out; 1192 error = AcpiGetObjectInfo(h, &buf); 1193 if (ACPI_FAILURE(error)) 1194 goto out; 1195 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1196 1197 if ((devinfo->Valid & ACPI_VALID_HID) != 0) 1198 pnpid = PNP_EISAID(devinfo->HardwareId.Value); 1199 1200 out: 1201 if (buf.Pointer != NULL) 1202 AcpiOsFree(buf.Pointer); 1203 return_VALUE (pnpid); 1204 } 1205 1206 static int 1207 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1208 { 1209 ACPI_DEVICE_INFO *devinfo; 1210 ACPI_BUFFER buf; 1211 ACPI_HANDLE h; 1212 ACPI_STATUS error; 1213 uint32_t *pnpid; 1214 int valid, i; 1215 1216 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1217 1218 pnpid = cids; 1219 valid = 0; 1220 buf.Pointer = NULL; 1221 buf.Length = ACPI_ALLOCATE_BUFFER; 1222 1223 /* Fetch and validate the CID */ 1224 if ((h = acpi_get_handle(dev)) == NULL) 1225 goto out; 1226 error = AcpiGetObjectInfo(h, &buf); 1227 if (ACPI_FAILURE(error)) 1228 goto out; 1229 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1230 if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1231 goto out; 1232 1233 if (devinfo->CompatibilityId.Count < count) 1234 count = devinfo->CompatibilityId.Count; 1235 for (i = 0; i < count; i++) { 1236 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 1237 continue; 1238 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 1239 valid++; 1240 } 1241 1242 out: 1243 if (buf.Pointer != NULL) 1244 AcpiOsFree(buf.Pointer); 1245 return_VALUE (valid); 1246 } 1247 1248 static char * 1249 acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1250 { 1251 ACPI_HANDLE h; 1252 int i; 1253 1254 h = acpi_get_handle(dev); 1255 if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1256 return (NULL); 1257 1258 /* Try to match one of the array of IDs with a HID or CID. */ 1259 for (i = 0; ids[i] != NULL; i++) { 1260 if (acpi_MatchHid(h, ids[i])) 1261 return (ids[i]); 1262 } 1263 return (NULL); 1264 } 1265 1266 static ACPI_STATUS 1267 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1268 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1269 { 1270 ACPI_HANDLE h; 1271 1272 if (dev == NULL) 1273 h = ACPI_ROOT_OBJECT; 1274 else if ((h = acpi_get_handle(dev)) == NULL) 1275 return (AE_BAD_PARAMETER); 1276 return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1277 } 1278 1279 static int 1280 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 1281 { 1282 struct acpi_softc *sc; 1283 ACPI_HANDLE handle; 1284 ACPI_STATUS status; 1285 char sxd[8]; 1286 int error; 1287 1288 sc = device_get_softc(bus); 1289 handle = acpi_get_handle(dev); 1290 1291 /* 1292 * XXX If we find these devices, don't try to power them down. 1293 * The serial and IRDA ports on my T23 hang the system when 1294 * set to D3 and it appears that such legacy devices may 1295 * need special handling in their drivers. 1296 */ 1297 if (handle == NULL || 1298 acpi_MatchHid(handle, "PNP0500") || 1299 acpi_MatchHid(handle, "PNP0501") || 1300 acpi_MatchHid(handle, "PNP0502") || 1301 acpi_MatchHid(handle, "PNP0510") || 1302 acpi_MatchHid(handle, "PNP0511")) 1303 return (ENXIO); 1304 1305 /* 1306 * Override next state with the value from _SxD, if present. If no 1307 * dstate argument was provided, don't fetch the return value. 1308 */ 1309 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 1310 if (dstate) 1311 status = acpi_GetInteger(handle, sxd, dstate); 1312 else 1313 status = AcpiEvaluateObject(handle, sxd, NULL, NULL); 1314 1315 switch (status) { 1316 case AE_OK: 1317 error = 0; 1318 break; 1319 case AE_NOT_FOUND: 1320 error = ESRCH; 1321 break; 1322 default: 1323 error = ENXIO; 1324 break; 1325 } 1326 1327 return (error); 1328 } 1329 1330 /* Callback arg for our implementation of walking the namespace. */ 1331 struct acpi_device_scan_ctx { 1332 acpi_scan_cb_t user_fn; 1333 void *arg; 1334 ACPI_HANDLE parent; 1335 }; 1336 1337 static ACPI_STATUS 1338 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1339 { 1340 struct acpi_device_scan_ctx *ctx; 1341 device_t dev, old_dev; 1342 ACPI_STATUS status; 1343 ACPI_OBJECT_TYPE type; 1344 1345 /* 1346 * Skip this device if we think we'll have trouble with it or it is 1347 * the parent where the scan began. 1348 */ 1349 ctx = (struct acpi_device_scan_ctx *)arg; 1350 if (acpi_avoid(h) || h == ctx->parent) 1351 return (AE_OK); 1352 1353 /* If this is not a valid device type (e.g., a method), skip it. */ 1354 if (ACPI_FAILURE(AcpiGetType(h, &type))) 1355 return (AE_OK); 1356 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1357 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1358 return (AE_OK); 1359 1360 /* 1361 * Call the user function with the current device. If it is unchanged 1362 * afterwards, return. Otherwise, we update the handle to the new dev. 1363 */ 1364 old_dev = acpi_get_device(h); 1365 dev = old_dev; 1366 status = ctx->user_fn(h, &dev, level, ctx->arg); 1367 if (ACPI_FAILURE(status) || old_dev == dev) 1368 return (status); 1369 1370 /* Remove the old child and its connection to the handle. */ 1371 if (old_dev != NULL) { 1372 device_delete_child(device_get_parent(old_dev), old_dev); 1373 AcpiDetachData(h, acpi_fake_objhandler); 1374 } 1375 1376 /* Recreate the handle association if the user created a device. */ 1377 if (dev != NULL) 1378 AcpiAttachData(h, acpi_fake_objhandler, dev); 1379 1380 return (AE_OK); 1381 } 1382 1383 static ACPI_STATUS 1384 acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1385 acpi_scan_cb_t user_fn, void *arg) 1386 { 1387 ACPI_HANDLE h; 1388 struct acpi_device_scan_ctx ctx; 1389 1390 if (acpi_disabled("children")) 1391 return (AE_OK); 1392 1393 if (dev == NULL) 1394 h = ACPI_ROOT_OBJECT; 1395 else if ((h = acpi_get_handle(dev)) == NULL) 1396 return (AE_BAD_PARAMETER); 1397 ctx.user_fn = user_fn; 1398 ctx.arg = arg; 1399 ctx.parent = h; 1400 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1401 acpi_device_scan_cb, &ctx, NULL)); 1402 } 1403 1404 /* 1405 * Even though ACPI devices are not PCI, we use the PCI approach for setting 1406 * device power states since it's close enough to ACPI. 1407 */ 1408 static int 1409 acpi_set_powerstate_method(device_t bus, device_t child, int state) 1410 { 1411 ACPI_HANDLE h; 1412 ACPI_STATUS status; 1413 int error; 1414 1415 error = 0; 1416 h = acpi_get_handle(child); 1417 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 1418 return (EINVAL); 1419 if (h == NULL) 1420 return (0); 1421 1422 /* Ignore errors if the power methods aren't present. */ 1423 status = acpi_pwr_switch_consumer(h, state); 1424 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND 1425 && status != AE_BAD_PARAMETER) 1426 device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n", 1427 state, acpi_name(h), AcpiFormatException(status)); 1428 1429 return (error); 1430 } 1431 1432 static int 1433 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 1434 { 1435 int result, cid_count, i; 1436 uint32_t lid, cids[8]; 1437 1438 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1439 1440 /* 1441 * ISA-style drivers attached to ACPI may persist and 1442 * probe manually if we return ENOENT. We never want 1443 * that to happen, so don't ever return it. 1444 */ 1445 result = ENXIO; 1446 1447 /* Scan the supplied IDs for a match */ 1448 lid = acpi_isa_get_logicalid(child); 1449 cid_count = acpi_isa_get_compatid(child, cids, 8); 1450 while (ids && ids->ip_id) { 1451 if (lid == ids->ip_id) { 1452 result = 0; 1453 goto out; 1454 } 1455 for (i = 0; i < cid_count; i++) { 1456 if (cids[i] == ids->ip_id) { 1457 result = 0; 1458 goto out; 1459 } 1460 } 1461 ids++; 1462 } 1463 1464 out: 1465 if (result == 0 && ids->ip_desc) 1466 device_set_desc(child, ids->ip_desc); 1467 1468 return_VALUE (result); 1469 } 1470 1471 /* 1472 * Scan all of the ACPI namespace and attach child devices. 1473 * 1474 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 1475 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 1476 * However, in violation of the spec, some systems place their PCI link 1477 * devices in \, so we have to walk the whole namespace. We check the 1478 * type of namespace nodes, so this should be ok. 1479 */ 1480 static void 1481 acpi_probe_children(device_t bus) 1482 { 1483 1484 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1485 1486 /* 1487 * Scan the namespace and insert placeholders for all the devices that 1488 * we find. We also probe/attach any early devices. 1489 * 1490 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1491 * we want to create nodes for all devices, not just those that are 1492 * currently present. (This assumes that we don't want to create/remove 1493 * devices as they appear, which might be smarter.) 1494 */ 1495 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1496 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 1497 bus, NULL); 1498 1499 /* Pre-allocate resources for our rman from any sysresource devices. */ 1500 acpi_sysres_alloc(bus); 1501 1502 /* Create any static children by calling device identify methods. */ 1503 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1504 bus_generic_probe(bus); 1505 1506 /* Probe/attach all children, created staticly and from the namespace. */ 1507 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 1508 bus_generic_attach(bus); 1509 1510 /* 1511 * Some of these children may have attached others as part of their attach 1512 * process (eg. the root PCI bus driver), so rescan. 1513 */ 1514 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 1515 bus_generic_attach(bus); 1516 1517 /* Attach wake sysctls. */ 1518 acpi_wake_sysctl_walk(bus); 1519 1520 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 1521 return_VOID; 1522 } 1523 1524 /* 1525 * Determine the probe order for a given device and return non-zero if it 1526 * should be attached immediately. 1527 */ 1528 static int 1529 acpi_probe_order(ACPI_HANDLE handle, int *order) 1530 { 1531 1532 /* 1533 * 1. I/O port and memory system resource holders 1534 * 2. Embedded controllers (to handle early accesses) 1535 * 3. PCI Link Devices 1536 */ 1537 if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) 1538 *order = 1; 1539 else if (acpi_MatchHid(handle, "PNP0C09")) 1540 *order = 2; 1541 else if (acpi_MatchHid(handle, "PNP0C0F")) 1542 *order = 3; 1543 return (0); 1544 } 1545 1546 /* 1547 * Evaluate a child device and determine whether we might attach a device to 1548 * it. 1549 */ 1550 static ACPI_STATUS 1551 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 1552 { 1553 ACPI_OBJECT_TYPE type; 1554 ACPI_HANDLE h; 1555 device_t bus, child; 1556 int order; 1557 char *handle_str, **search; 1558 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL}; 1559 1560 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1561 1562 /* Skip this device if we think we'll have trouble with it. */ 1563 if (acpi_avoid(handle)) 1564 return_ACPI_STATUS (AE_OK); 1565 1566 bus = (device_t)context; 1567 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1568 switch (type) { 1569 case ACPI_TYPE_DEVICE: 1570 case ACPI_TYPE_PROCESSOR: 1571 case ACPI_TYPE_THERMAL: 1572 case ACPI_TYPE_POWER: 1573 if (acpi_disabled("children")) 1574 break; 1575 1576 /* 1577 * Since we scan from \, be sure to skip system scope objects. 1578 * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?) 1579 */ 1580 handle_str = acpi_name(handle); 1581 for (search = scopes; *search != NULL; search++) { 1582 if (strcmp(handle_str, *search) == 0) 1583 break; 1584 } 1585 if (*search != NULL) 1586 break; 1587 1588 /* 1589 * Create a placeholder device for this node. Sort the placeholder 1590 * so that the probe/attach passes will run breadth-first. Orders 1591 * less than 10 are reserved for special objects (i.e., system 1592 * resources). Larger values are used for all other devices. 1593 */ 1594 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1595 order = (level + 1) * 10; 1596 acpi_probe_order(handle, &order); 1597 child = BUS_ADD_CHILD(bus, order, NULL, -1); 1598 if (child == NULL) 1599 break; 1600 1601 /* Associate the handle with the device_t and vice versa. */ 1602 acpi_set_handle(child, handle); 1603 AcpiAttachData(handle, acpi_fake_objhandler, child); 1604 1605 /* 1606 * Check that the device is present. If it's not present, 1607 * leave it disabled (so that we have a device_t attached to 1608 * the handle, but we don't probe it). 1609 * 1610 * XXX PCI link devices sometimes report "present" but not 1611 * "functional" (i.e. if disabled). Go ahead and probe them 1612 * anyway since we may enable them later. 1613 */ 1614 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1615 /* Never disable PCI link devices. */ 1616 if (acpi_MatchHid(handle, "PNP0C0F")) 1617 break; 1618 /* 1619 * Docking stations should remain enabled since the system 1620 * may be undocked at boot. 1621 */ 1622 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1623 break; 1624 1625 device_disable(child); 1626 break; 1627 } 1628 1629 /* 1630 * Get the device's resource settings and attach them. 1631 * Note that if the device has _PRS but no _CRS, we need 1632 * to decide when it's appropriate to try to configure the 1633 * device. Ignore the return value here; it's OK for the 1634 * device not to have any resources. 1635 */ 1636 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1637 break; 1638 } 1639 } 1640 1641 return_ACPI_STATUS (AE_OK); 1642 } 1643 1644 /* 1645 * AcpiAttachData() requires an object handler but never uses it. This is a 1646 * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 1647 */ 1648 void 1649 acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 1650 { 1651 } 1652 1653 static void 1654 acpi_shutdown_final(void *arg, int howto) 1655 { 1656 struct acpi_softc *sc; 1657 ACPI_STATUS status; 1658 1659 /* 1660 * XXX Shutdown code should only run on the BSP (cpuid 0). 1661 * Some chipsets do not power off the system correctly if called from 1662 * an AP. 1663 */ 1664 sc = arg; 1665 if ((howto & RB_POWEROFF) != 0) { 1666 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1667 if (ACPI_FAILURE(status)) { 1668 printf("AcpiEnterSleepStatePrep failed - %s\n", 1669 AcpiFormatException(status)); 1670 return; 1671 } 1672 printf("Powering system off using ACPI\n"); 1673 ACPI_DISABLE_IRQS(); 1674 status = AcpiEnterSleepState(ACPI_STATE_S5); 1675 if (ACPI_FAILURE(status)) { 1676 printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 1677 } else { 1678 DELAY(1000000); 1679 printf("ACPI power-off failed - timeout\n"); 1680 } 1681 } else if ((howto & RB_HALT) == 0 && 1682 (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) && 1683 sc->acpi_handle_reboot) { 1684 /* Reboot using the reset register. */ 1685 status = AcpiHwLowLevelWrite( 1686 AcpiGbl_FADT.ResetRegister.BitWidth, 1687 AcpiGbl_FADT.ResetValue, &AcpiGbl_FADT.ResetRegister); 1688 if (ACPI_FAILURE(status)) { 1689 printf("ACPI reset failed - %s\n", AcpiFormatException(status)); 1690 } else { 1691 DELAY(1000000); 1692 printf("ACPI reset failed - timeout\n"); 1693 } 1694 } else if (sc->acpi_do_disable && panicstr == NULL) { 1695 /* 1696 * Only disable ACPI if the user requested. On some systems, writing 1697 * the disable value to SMI_CMD hangs the system. 1698 */ 1699 printf("Shutting down ACPI\n"); 1700 AcpiTerminate(); 1701 } 1702 } 1703 1704 static void 1705 acpi_enable_fixed_events(struct acpi_softc *sc) 1706 { 1707 static int first_time = 1; 1708 1709 /* Enable and clear fixed events and install handlers. */ 1710 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 1711 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 1712 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1713 acpi_event_power_button_sleep, sc); 1714 if (first_time) 1715 device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 1716 } 1717 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 1718 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 1719 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 1720 acpi_event_sleep_button_sleep, sc); 1721 if (first_time) 1722 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 1723 } 1724 1725 first_time = 0; 1726 } 1727 1728 /* 1729 * Returns true if the device is actually present and should 1730 * be attached to. This requires the present, enabled, UI-visible 1731 * and diagnostics-passed bits to be set. 1732 */ 1733 BOOLEAN 1734 acpi_DeviceIsPresent(device_t dev) 1735 { 1736 ACPI_DEVICE_INFO *devinfo; 1737 ACPI_HANDLE h; 1738 ACPI_BUFFER buf; 1739 ACPI_STATUS error; 1740 int ret; 1741 1742 ret = FALSE; 1743 if ((h = acpi_get_handle(dev)) == NULL) 1744 return (FALSE); 1745 buf.Pointer = NULL; 1746 buf.Length = ACPI_ALLOCATE_BUFFER; 1747 error = AcpiGetObjectInfo(h, &buf); 1748 if (ACPI_FAILURE(error)) 1749 return (FALSE); 1750 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1751 1752 /* If no _STA method, must be present */ 1753 if ((devinfo->Valid & ACPI_VALID_STA) == 0) 1754 ret = TRUE; 1755 1756 /* Return true for 'present' and 'functioning' */ 1757 if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus)) 1758 ret = TRUE; 1759 1760 AcpiOsFree(buf.Pointer); 1761 return (ret); 1762 } 1763 1764 /* 1765 * Returns true if the battery is actually present and inserted. 1766 */ 1767 BOOLEAN 1768 acpi_BatteryIsPresent(device_t dev) 1769 { 1770 ACPI_DEVICE_INFO *devinfo; 1771 ACPI_HANDLE h; 1772 ACPI_BUFFER buf; 1773 ACPI_STATUS error; 1774 int ret; 1775 1776 ret = FALSE; 1777 if ((h = acpi_get_handle(dev)) == NULL) 1778 return (FALSE); 1779 buf.Pointer = NULL; 1780 buf.Length = ACPI_ALLOCATE_BUFFER; 1781 error = AcpiGetObjectInfo(h, &buf); 1782 if (ACPI_FAILURE(error)) 1783 return (FALSE); 1784 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1785 1786 /* If no _STA method, must be present */ 1787 if ((devinfo->Valid & ACPI_VALID_STA) == 0) 1788 ret = TRUE; 1789 1790 /* Return true for 'present', 'battery present', and 'functioning' */ 1791 if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus)) 1792 ret = TRUE; 1793 1794 AcpiOsFree(buf.Pointer); 1795 return (ret); 1796 } 1797 1798 /* 1799 * Match a HID string against a handle 1800 */ 1801 static BOOLEAN 1802 acpi_MatchHid(ACPI_HANDLE h, const char *hid) 1803 { 1804 ACPI_DEVICE_INFO *devinfo; 1805 ACPI_BUFFER buf; 1806 ACPI_STATUS error; 1807 int ret, i; 1808 1809 ret = FALSE; 1810 if (hid == NULL || h == NULL) 1811 return (ret); 1812 buf.Pointer = NULL; 1813 buf.Length = ACPI_ALLOCATE_BUFFER; 1814 error = AcpiGetObjectInfo(h, &buf); 1815 if (ACPI_FAILURE(error)) 1816 return (ret); 1817 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1818 1819 if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1820 strcmp(hid, devinfo->HardwareId.Value) == 0) 1821 ret = TRUE; 1822 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 1823 for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 1824 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 1825 ret = TRUE; 1826 break; 1827 } 1828 } 1829 } 1830 1831 AcpiOsFree(buf.Pointer); 1832 return (ret); 1833 } 1834 1835 /* 1836 * Return the handle of a named object within our scope, ie. that of (parent) 1837 * or one if its parents. 1838 */ 1839 ACPI_STATUS 1840 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1841 { 1842 ACPI_HANDLE r; 1843 ACPI_STATUS status; 1844 1845 /* Walk back up the tree to the root */ 1846 for (;;) { 1847 status = AcpiGetHandle(parent, path, &r); 1848 if (ACPI_SUCCESS(status)) { 1849 *result = r; 1850 return (AE_OK); 1851 } 1852 /* XXX Return error here? */ 1853 if (status != AE_NOT_FOUND) 1854 return (AE_OK); 1855 if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1856 return (AE_NOT_FOUND); 1857 parent = r; 1858 } 1859 } 1860 1861 /* Find the difference between two PM tick counts. */ 1862 uint32_t 1863 acpi_TimerDelta(uint32_t end, uint32_t start) 1864 { 1865 uint32_t delta; 1866 1867 if (end >= start) 1868 delta = end - start; 1869 else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 1870 delta = ((0xFFFFFFFF - start) + end + 1); 1871 else 1872 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1873 return (delta); 1874 } 1875 1876 /* 1877 * Allocate a buffer with a preset data size. 1878 */ 1879 ACPI_BUFFER * 1880 acpi_AllocBuffer(int size) 1881 { 1882 ACPI_BUFFER *buf; 1883 1884 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1885 return (NULL); 1886 buf->Length = size; 1887 buf->Pointer = (void *)(buf + 1); 1888 return (buf); 1889 } 1890 1891 ACPI_STATUS 1892 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1893 { 1894 ACPI_OBJECT arg1; 1895 ACPI_OBJECT_LIST args; 1896 1897 arg1.Type = ACPI_TYPE_INTEGER; 1898 arg1.Integer.Value = number; 1899 args.Count = 1; 1900 args.Pointer = &arg1; 1901 1902 return (AcpiEvaluateObject(handle, path, &args, NULL)); 1903 } 1904 1905 /* 1906 * Evaluate a path that should return an integer. 1907 */ 1908 ACPI_STATUS 1909 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1910 { 1911 ACPI_STATUS status; 1912 ACPI_BUFFER buf; 1913 ACPI_OBJECT param; 1914 1915 if (handle == NULL) 1916 handle = ACPI_ROOT_OBJECT; 1917 1918 /* 1919 * Assume that what we've been pointed at is an Integer object, or 1920 * a method that will return an Integer. 1921 */ 1922 buf.Pointer = ¶m; 1923 buf.Length = sizeof(param); 1924 status = AcpiEvaluateObject(handle, path, NULL, &buf); 1925 if (ACPI_SUCCESS(status)) { 1926 if (param.Type == ACPI_TYPE_INTEGER) 1927 *number = param.Integer.Value; 1928 else 1929 status = AE_TYPE; 1930 } 1931 1932 /* 1933 * In some applications, a method that's expected to return an Integer 1934 * may instead return a Buffer (probably to simplify some internal 1935 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 1936 * convert it into an Integer as best we can. 1937 * 1938 * This is a hack. 1939 */ 1940 if (status == AE_BUFFER_OVERFLOW) { 1941 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1942 status = AE_NO_MEMORY; 1943 } else { 1944 status = AcpiEvaluateObject(handle, path, NULL, &buf); 1945 if (ACPI_SUCCESS(status)) 1946 status = acpi_ConvertBufferToInteger(&buf, number); 1947 AcpiOsFree(buf.Pointer); 1948 } 1949 } 1950 return (status); 1951 } 1952 1953 ACPI_STATUS 1954 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1955 { 1956 ACPI_OBJECT *p; 1957 UINT8 *val; 1958 int i; 1959 1960 p = (ACPI_OBJECT *)bufp->Pointer; 1961 if (p->Type == ACPI_TYPE_INTEGER) { 1962 *number = p->Integer.Value; 1963 return (AE_OK); 1964 } 1965 if (p->Type != ACPI_TYPE_BUFFER) 1966 return (AE_TYPE); 1967 if (p->Buffer.Length > sizeof(int)) 1968 return (AE_BAD_DATA); 1969 1970 *number = 0; 1971 val = p->Buffer.Pointer; 1972 for (i = 0; i < p->Buffer.Length; i++) 1973 *number += val[i] << (i * 8); 1974 return (AE_OK); 1975 } 1976 1977 /* 1978 * Iterate over the elements of an a package object, calling the supplied 1979 * function for each element. 1980 * 1981 * XXX possible enhancement might be to abort traversal on error. 1982 */ 1983 ACPI_STATUS 1984 acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1985 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1986 { 1987 ACPI_OBJECT *comp; 1988 int i; 1989 1990 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1991 return (AE_BAD_PARAMETER); 1992 1993 /* Iterate over components */ 1994 i = 0; 1995 comp = pkg->Package.Elements; 1996 for (; i < pkg->Package.Count; i++, comp++) 1997 func(comp, arg); 1998 1999 return (AE_OK); 2000 } 2001 2002 /* 2003 * Find the (index)th resource object in a set. 2004 */ 2005 ACPI_STATUS 2006 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 2007 { 2008 ACPI_RESOURCE *rp; 2009 int i; 2010 2011 rp = (ACPI_RESOURCE *)buf->Pointer; 2012 i = index; 2013 while (i-- > 0) { 2014 /* Range check */ 2015 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2016 return (AE_BAD_PARAMETER); 2017 2018 /* Check for terminator */ 2019 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2020 return (AE_NOT_FOUND); 2021 rp = ACPI_NEXT_RESOURCE(rp); 2022 } 2023 if (resp != NULL) 2024 *resp = rp; 2025 2026 return (AE_OK); 2027 } 2028 2029 /* 2030 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 2031 * 2032 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 2033 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 2034 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 2035 * resources. 2036 */ 2037 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 2038 2039 ACPI_STATUS 2040 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 2041 { 2042 ACPI_RESOURCE *rp; 2043 void *newp; 2044 2045 /* Initialise the buffer if necessary. */ 2046 if (buf->Pointer == NULL) { 2047 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 2048 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 2049 return (AE_NO_MEMORY); 2050 rp = (ACPI_RESOURCE *)buf->Pointer; 2051 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2052 rp->Length = 0; 2053 } 2054 if (res == NULL) 2055 return (AE_OK); 2056 2057 /* 2058 * Scan the current buffer looking for the terminator. 2059 * This will either find the terminator or hit the end 2060 * of the buffer and return an error. 2061 */ 2062 rp = (ACPI_RESOURCE *)buf->Pointer; 2063 for (;;) { 2064 /* Range check, don't go outside the buffer */ 2065 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 2066 return (AE_BAD_PARAMETER); 2067 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 2068 break; 2069 rp = ACPI_NEXT_RESOURCE(rp); 2070 } 2071 2072 /* 2073 * Check the size of the buffer and expand if required. 2074 * 2075 * Required size is: 2076 * size of existing resources before terminator + 2077 * size of new resource and header + 2078 * size of terminator. 2079 * 2080 * Note that this loop should really only run once, unless 2081 * for some reason we are stuffing a *really* huge resource. 2082 */ 2083 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2084 res->Length + ACPI_RS_SIZE_NO_DATA + 2085 ACPI_RS_SIZE_MIN) >= buf->Length) { 2086 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 2087 return (AE_NO_MEMORY); 2088 bcopy(buf->Pointer, newp, buf->Length); 2089 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2090 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 2091 AcpiOsFree(buf->Pointer); 2092 buf->Pointer = newp; 2093 buf->Length += buf->Length; 2094 } 2095 2096 /* Insert the new resource. */ 2097 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 2098 2099 /* And add the terminator. */ 2100 rp = ACPI_NEXT_RESOURCE(rp); 2101 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 2102 rp->Length = 0; 2103 2104 return (AE_OK); 2105 } 2106 2107 /* 2108 * Set interrupt model. 2109 */ 2110 ACPI_STATUS 2111 acpi_SetIntrModel(int model) 2112 { 2113 2114 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2115 } 2116 2117 static void 2118 acpi_sleep_enable(void *arg) 2119 { 2120 2121 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 2122 } 2123 2124 enum acpi_sleep_state { 2125 ACPI_SS_NONE, 2126 ACPI_SS_GPE_SET, 2127 ACPI_SS_DEV_SUSPEND, 2128 ACPI_SS_SLP_PREP, 2129 ACPI_SS_SLEPT, 2130 }; 2131 2132 /* 2133 * Set the system sleep state 2134 * 2135 * Currently we support S1-S5 but S4 is only S4BIOS 2136 */ 2137 ACPI_STATUS 2138 acpi_SetSleepState(struct acpi_softc *sc, int state) 2139 { 2140 ACPI_STATUS status; 2141 UINT8 TypeA; 2142 UINT8 TypeB; 2143 enum acpi_sleep_state slp_state; 2144 2145 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2146 2147 status = AE_OK; 2148 ACPI_LOCK(acpi); 2149 if (sc->acpi_sleep_disabled) { 2150 if (sc->acpi_sstate != ACPI_STATE_S0) 2151 status = AE_ERROR; 2152 ACPI_UNLOCK(acpi); 2153 printf("acpi: suspend request ignored (not ready yet)\n"); 2154 return (status); 2155 } 2156 sc->acpi_sleep_disabled = 1; 2157 ACPI_UNLOCK(acpi); 2158 2159 /* 2160 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2161 * drivers need this. 2162 */ 2163 mtx_lock(&Giant); 2164 slp_state = ACPI_SS_NONE; 2165 switch (state) { 2166 case ACPI_STATE_S1: 2167 case ACPI_STATE_S2: 2168 case ACPI_STATE_S3: 2169 case ACPI_STATE_S4: 2170 status = AcpiGetSleepTypeData(state, &TypeA, &TypeB); 2171 if (status == AE_NOT_FOUND) { 2172 device_printf(sc->acpi_dev, 2173 "Sleep state S%d not supported by BIOS\n", state); 2174 break; 2175 } else if (ACPI_FAILURE(status)) { 2176 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 2177 AcpiFormatException(status)); 2178 break; 2179 } 2180 2181 sc->acpi_sstate = state; 2182 2183 /* Enable any GPEs as appropriate and requested by the user. */ 2184 acpi_wake_prep_walk(state); 2185 slp_state = ACPI_SS_GPE_SET; 2186 2187 /* 2188 * Inform all devices that we are going to sleep. If at least one 2189 * device fails, DEVICE_SUSPEND() automatically resumes the tree. 2190 * 2191 * XXX Note that a better two-pass approach with a 'veto' pass 2192 * followed by a "real thing" pass would be better, but the current 2193 * bus interface does not provide for this. 2194 */ 2195 if (DEVICE_SUSPEND(root_bus) != 0) { 2196 device_printf(sc->acpi_dev, "device_suspend failed\n"); 2197 break; 2198 } 2199 slp_state = ACPI_SS_DEV_SUSPEND; 2200 2201 status = AcpiEnterSleepStatePrep(state); 2202 if (ACPI_FAILURE(status)) { 2203 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2204 AcpiFormatException(status)); 2205 break; 2206 } 2207 slp_state = ACPI_SS_SLP_PREP; 2208 2209 if (sc->acpi_sleep_delay > 0) 2210 DELAY(sc->acpi_sleep_delay * 1000000); 2211 2212 if (state != ACPI_STATE_S1) { 2213 acpi_sleep_machdep(sc, state); 2214 2215 /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2216 if (state == ACPI_STATE_S4) 2217 AcpiEnable(); 2218 } else { 2219 ACPI_DISABLE_IRQS(); 2220 status = AcpiEnterSleepState(state); 2221 if (ACPI_FAILURE(status)) { 2222 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2223 AcpiFormatException(status)); 2224 break; 2225 } 2226 } 2227 slp_state = ACPI_SS_SLEPT; 2228 break; 2229 case ACPI_STATE_S5: 2230 /* 2231 * Shut down cleanly and power off. This will call us back through the 2232 * shutdown handlers. 2233 */ 2234 shutdown_nice(RB_POWEROFF); 2235 break; 2236 case ACPI_STATE_S0: 2237 default: 2238 status = AE_BAD_PARAMETER; 2239 break; 2240 } 2241 2242 /* 2243 * Back out state according to how far along we got in the suspend 2244 * process. This handles both the error and success cases. 2245 */ 2246 if (slp_state >= ACPI_SS_GPE_SET) { 2247 acpi_wake_prep_walk(state); 2248 sc->acpi_sstate = ACPI_STATE_S0; 2249 } 2250 if (slp_state >= ACPI_SS_SLP_PREP) 2251 AcpiLeaveSleepState(state); 2252 if (slp_state >= ACPI_SS_DEV_SUSPEND) 2253 DEVICE_RESUME(root_bus); 2254 if (slp_state >= ACPI_SS_SLEPT) 2255 acpi_enable_fixed_events(sc); 2256 2257 /* Allow another sleep request after a while. */ 2258 if (state != ACPI_STATE_S5) 2259 timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 2260 2261 mtx_unlock(&Giant); 2262 return_ACPI_STATUS (status); 2263 } 2264 2265 /* Initialize a device's wake GPE. */ 2266 int 2267 acpi_wake_init(device_t dev, int type) 2268 { 2269 struct acpi_prw_data prw; 2270 2271 /* Evaluate _PRW to find the GPE. */ 2272 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2273 return (ENXIO); 2274 2275 /* Set the requested type for the GPE (runtime, wake, or both). */ 2276 if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 2277 device_printf(dev, "set GPE type failed\n"); 2278 return (ENXIO); 2279 } 2280 2281 return (0); 2282 } 2283 2284 /* Enable or disable the device's wake GPE. */ 2285 int 2286 acpi_wake_set_enable(device_t dev, int enable) 2287 { 2288 struct acpi_prw_data prw; 2289 ACPI_STATUS status; 2290 int flags; 2291 2292 /* Make sure the device supports waking the system and get the GPE. */ 2293 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2294 return (ENXIO); 2295 2296 flags = acpi_get_flags(dev); 2297 if (enable) { 2298 status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2299 if (ACPI_FAILURE(status)) { 2300 device_printf(dev, "enable wake failed\n"); 2301 return (ENXIO); 2302 } 2303 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2304 } else { 2305 status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2306 if (ACPI_FAILURE(status)) { 2307 device_printf(dev, "disable wake failed\n"); 2308 return (ENXIO); 2309 } 2310 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2311 } 2312 2313 return (0); 2314 } 2315 2316 static int 2317 acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2318 { 2319 struct acpi_prw_data prw; 2320 device_t dev; 2321 2322 /* Check that this is a wake-capable device and get its GPE. */ 2323 if (acpi_parse_prw(handle, &prw) != 0) 2324 return (ENXIO); 2325 dev = acpi_get_device(handle); 2326 2327 /* 2328 * The destination sleep state must be less than (i.e., higher power) 2329 * or equal to the value specified by _PRW. If this GPE cannot be 2330 * enabled for the next sleep state, then disable it. If it can and 2331 * the user requested it be enabled, turn on any required power resources 2332 * and set _PSW. 2333 */ 2334 if (sstate > prw.lowest_wake) { 2335 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2336 if (bootverbose) 2337 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2338 acpi_name(handle), sstate); 2339 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2340 acpi_pwr_wake_enable(handle, 1); 2341 acpi_SetInteger(handle, "_PSW", 1); 2342 if (bootverbose) 2343 device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2344 acpi_name(handle), sstate); 2345 } 2346 2347 return (0); 2348 } 2349 2350 static int 2351 acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2352 { 2353 struct acpi_prw_data prw; 2354 device_t dev; 2355 2356 /* 2357 * Check that this is a wake-capable device and get its GPE. Return 2358 * now if the user didn't enable this device for wake. 2359 */ 2360 if (acpi_parse_prw(handle, &prw) != 0) 2361 return (ENXIO); 2362 dev = acpi_get_device(handle); 2363 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2364 return (0); 2365 2366 /* 2367 * If this GPE couldn't be enabled for the previous sleep state, it was 2368 * disabled before going to sleep so re-enable it. If it was enabled, 2369 * clear _PSW and turn off any power resources it used. 2370 */ 2371 if (sstate > prw.lowest_wake) { 2372 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2373 if (bootverbose) 2374 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2375 } else { 2376 acpi_SetInteger(handle, "_PSW", 0); 2377 acpi_pwr_wake_enable(handle, 0); 2378 if (bootverbose) 2379 device_printf(dev, "run_prep cleaned up for %s\n", 2380 acpi_name(handle)); 2381 } 2382 2383 return (0); 2384 } 2385 2386 static ACPI_STATUS 2387 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2388 { 2389 int sstate; 2390 2391 /* If suspending, run the sleep prep function, otherwise wake. */ 2392 sstate = *(int *)context; 2393 if (AcpiGbl_SystemAwakeAndRunning) 2394 acpi_wake_sleep_prep(handle, sstate); 2395 else 2396 acpi_wake_run_prep(handle, sstate); 2397 return (AE_OK); 2398 } 2399 2400 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2401 static int 2402 acpi_wake_prep_walk(int sstate) 2403 { 2404 ACPI_HANDLE sb_handle; 2405 2406 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2407 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 2408 acpi_wake_prep, &sstate, NULL); 2409 return (0); 2410 } 2411 2412 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 2413 static int 2414 acpi_wake_sysctl_walk(device_t dev) 2415 { 2416 int error, i, numdevs; 2417 device_t *devlist; 2418 device_t child; 2419 ACPI_STATUS status; 2420 2421 error = device_get_children(dev, &devlist, &numdevs); 2422 if (error != 0 || numdevs == 0) { 2423 if (numdevs == 0) 2424 free(devlist, M_TEMP); 2425 return (error); 2426 } 2427 for (i = 0; i < numdevs; i++) { 2428 child = devlist[i]; 2429 acpi_wake_sysctl_walk(child); 2430 if (!device_is_attached(child)) 2431 continue; 2432 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2433 if (ACPI_SUCCESS(status)) { 2434 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 2435 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 2436 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 2437 acpi_wake_set_sysctl, "I", "Device set to wake the system"); 2438 } 2439 } 2440 free(devlist, M_TEMP); 2441 2442 return (0); 2443 } 2444 2445 /* Enable or disable wake from userland. */ 2446 static int 2447 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 2448 { 2449 int enable, error; 2450 device_t dev; 2451 2452 dev = (device_t)arg1; 2453 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 2454 2455 error = sysctl_handle_int(oidp, &enable, 0, req); 2456 if (error != 0 || req->newptr == NULL) 2457 return (error); 2458 if (enable != 0 && enable != 1) 2459 return (EINVAL); 2460 2461 return (acpi_wake_set_enable(dev, enable)); 2462 } 2463 2464 /* Parse a device's _PRW into a structure. */ 2465 int 2466 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2467 { 2468 ACPI_STATUS status; 2469 ACPI_BUFFER prw_buffer; 2470 ACPI_OBJECT *res, *res2; 2471 int error, i, power_count; 2472 2473 if (h == NULL || prw == NULL) 2474 return (EINVAL); 2475 2476 /* 2477 * The _PRW object (7.2.9) is only required for devices that have the 2478 * ability to wake the system from a sleeping state. 2479 */ 2480 error = EINVAL; 2481 prw_buffer.Pointer = NULL; 2482 prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2483 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2484 if (ACPI_FAILURE(status)) 2485 return (ENOENT); 2486 res = (ACPI_OBJECT *)prw_buffer.Pointer; 2487 if (res == NULL) 2488 return (ENOENT); 2489 if (!ACPI_PKG_VALID(res, 2)) 2490 goto out; 2491 2492 /* 2493 * Element 1 of the _PRW object: 2494 * The lowest power system sleeping state that can be entered while still 2495 * providing wake functionality. The sleeping state being entered must 2496 * be less than (i.e., higher power) or equal to this value. 2497 */ 2498 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2499 goto out; 2500 2501 /* 2502 * Element 0 of the _PRW object: 2503 */ 2504 switch (res->Package.Elements[0].Type) { 2505 case ACPI_TYPE_INTEGER: 2506 /* 2507 * If the data type of this package element is numeric, then this 2508 * _PRW package element is the bit index in the GPEx_EN, in the 2509 * GPE blocks described in the FADT, of the enable bit that is 2510 * enabled for the wake event. 2511 */ 2512 prw->gpe_handle = NULL; 2513 prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2514 error = 0; 2515 break; 2516 case ACPI_TYPE_PACKAGE: 2517 /* 2518 * If the data type of this package element is a package, then this 2519 * _PRW package element is itself a package containing two 2520 * elements. The first is an object reference to the GPE Block 2521 * device that contains the GPE that will be triggered by the wake 2522 * event. The second element is numeric and it contains the bit 2523 * index in the GPEx_EN, in the GPE Block referenced by the 2524 * first element in the package, of the enable bit that is enabled for 2525 * the wake event. 2526 * 2527 * For example, if this field is a package then it is of the form: 2528 * Package() {\_SB.PCI0.ISA.GPE, 2} 2529 */ 2530 res2 = &res->Package.Elements[0]; 2531 if (!ACPI_PKG_VALID(res2, 2)) 2532 goto out; 2533 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2534 if (prw->gpe_handle == NULL) 2535 goto out; 2536 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2537 goto out; 2538 error = 0; 2539 break; 2540 default: 2541 goto out; 2542 } 2543 2544 /* Elements 2 to N of the _PRW object are power resources. */ 2545 power_count = res->Package.Count - 2; 2546 if (power_count > ACPI_PRW_MAX_POWERRES) { 2547 printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2548 power_count = 0; 2549 } 2550 prw->power_res_count = power_count; 2551 for (i = 0; i < power_count; i++) 2552 prw->power_res[i] = res->Package.Elements[i]; 2553 2554 out: 2555 if (prw_buffer.Pointer != NULL) 2556 AcpiOsFree(prw_buffer.Pointer); 2557 return (error); 2558 } 2559 2560 /* 2561 * ACPI Event Handlers 2562 */ 2563 2564 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 2565 2566 static void 2567 acpi_system_eventhandler_sleep(void *arg, int state) 2568 { 2569 2570 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2571 2572 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 2573 acpi_SetSleepState((struct acpi_softc *)arg, state); 2574 2575 return_VOID; 2576 } 2577 2578 static void 2579 acpi_system_eventhandler_wakeup(void *arg, int state) 2580 { 2581 2582 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 2583 2584 /* Currently, nothing to do for wakeup. */ 2585 2586 return_VOID; 2587 } 2588 2589 /* 2590 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 2591 */ 2592 UINT32 2593 acpi_event_power_button_sleep(void *context) 2594 { 2595 struct acpi_softc *sc = (struct acpi_softc *)context; 2596 2597 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2598 2599 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 2600 2601 return_VALUE (ACPI_INTERRUPT_HANDLED); 2602 } 2603 2604 UINT32 2605 acpi_event_power_button_wake(void *context) 2606 { 2607 struct acpi_softc *sc = (struct acpi_softc *)context; 2608 2609 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2610 2611 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 2612 2613 return_VALUE (ACPI_INTERRUPT_HANDLED); 2614 } 2615 2616 UINT32 2617 acpi_event_sleep_button_sleep(void *context) 2618 { 2619 struct acpi_softc *sc = (struct acpi_softc *)context; 2620 2621 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2622 2623 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 2624 2625 return_VALUE (ACPI_INTERRUPT_HANDLED); 2626 } 2627 2628 UINT32 2629 acpi_event_sleep_button_wake(void *context) 2630 { 2631 struct acpi_softc *sc = (struct acpi_softc *)context; 2632 2633 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2634 2635 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 2636 2637 return_VALUE (ACPI_INTERRUPT_HANDLED); 2638 } 2639 2640 /* 2641 * XXX This static buffer is suboptimal. There is no locking so only 2642 * use this for single-threaded callers. 2643 */ 2644 char * 2645 acpi_name(ACPI_HANDLE handle) 2646 { 2647 ACPI_BUFFER buf; 2648 static char data[256]; 2649 2650 buf.Length = sizeof(data); 2651 buf.Pointer = data; 2652 2653 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 2654 return (data); 2655 return ("(unknown)"); 2656 } 2657 2658 /* 2659 * Debugging/bug-avoidance. Avoid trying to fetch info on various 2660 * parts of the namespace. 2661 */ 2662 int 2663 acpi_avoid(ACPI_HANDLE handle) 2664 { 2665 char *cp, *env, *np; 2666 int len; 2667 2668 np = acpi_name(handle); 2669 if (*np == '\\') 2670 np++; 2671 if ((env = getenv("debug.acpi.avoid")) == NULL) 2672 return (0); 2673 2674 /* Scan the avoid list checking for a match */ 2675 cp = env; 2676 for (;;) { 2677 while (*cp != 0 && isspace(*cp)) 2678 cp++; 2679 if (*cp == 0) 2680 break; 2681 len = 0; 2682 while (cp[len] != 0 && !isspace(cp[len])) 2683 len++; 2684 if (!strncmp(cp, np, len)) { 2685 freeenv(env); 2686 return(1); 2687 } 2688 cp += len; 2689 } 2690 freeenv(env); 2691 2692 return (0); 2693 } 2694 2695 /* 2696 * Debugging/bug-avoidance. Disable ACPI subsystem components. 2697 */ 2698 int 2699 acpi_disabled(char *subsys) 2700 { 2701 char *cp, *env; 2702 int len; 2703 2704 if ((env = getenv("debug.acpi.disabled")) == NULL) 2705 return (0); 2706 if (strcmp(env, "all") == 0) { 2707 freeenv(env); 2708 return (1); 2709 } 2710 2711 /* Scan the disable list, checking for a match. */ 2712 cp = env; 2713 for (;;) { 2714 while (*cp != '\0' && isspace(*cp)) 2715 cp++; 2716 if (*cp == '\0') 2717 break; 2718 len = 0; 2719 while (cp[len] != '\0' && !isspace(cp[len])) 2720 len++; 2721 if (strncmp(cp, subsys, len) == 0) { 2722 freeenv(env); 2723 return (1); 2724 } 2725 cp += len; 2726 } 2727 freeenv(env); 2728 2729 return (0); 2730 } 2731 2732 /* 2733 * Control interface. 2734 * 2735 * We multiplex ioctls for all participating ACPI devices here. Individual 2736 * drivers wanting to be accessible via /dev/acpi should use the 2737 * register/deregister interface to make their handlers visible. 2738 */ 2739 struct acpi_ioctl_hook 2740 { 2741 TAILQ_ENTRY(acpi_ioctl_hook) link; 2742 u_long cmd; 2743 acpi_ioctl_fn fn; 2744 void *arg; 2745 }; 2746 2747 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 2748 static int acpi_ioctl_hooks_initted; 2749 2750 int 2751 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 2752 { 2753 struct acpi_ioctl_hook *hp; 2754 2755 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 2756 return (ENOMEM); 2757 hp->cmd = cmd; 2758 hp->fn = fn; 2759 hp->arg = arg; 2760 2761 ACPI_LOCK(acpi); 2762 if (acpi_ioctl_hooks_initted == 0) { 2763 TAILQ_INIT(&acpi_ioctl_hooks); 2764 acpi_ioctl_hooks_initted = 1; 2765 } 2766 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 2767 ACPI_UNLOCK(acpi); 2768 2769 return (0); 2770 } 2771 2772 void 2773 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 2774 { 2775 struct acpi_ioctl_hook *hp; 2776 2777 ACPI_LOCK(acpi); 2778 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 2779 if (hp->cmd == cmd && hp->fn == fn) 2780 break; 2781 2782 if (hp != NULL) { 2783 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 2784 free(hp, M_ACPIDEV); 2785 } 2786 ACPI_UNLOCK(acpi); 2787 } 2788 2789 static int 2790 acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 2791 { 2792 return (0); 2793 } 2794 2795 static int 2796 acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 2797 { 2798 return (0); 2799 } 2800 2801 static int 2802 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 2803 { 2804 struct acpi_softc *sc; 2805 struct acpi_ioctl_hook *hp; 2806 int error, state; 2807 2808 error = 0; 2809 hp = NULL; 2810 sc = dev->si_drv1; 2811 2812 /* 2813 * Scan the list of registered ioctls, looking for handlers. 2814 */ 2815 ACPI_LOCK(acpi); 2816 if (acpi_ioctl_hooks_initted) 2817 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 2818 if (hp->cmd == cmd) 2819 break; 2820 } 2821 ACPI_UNLOCK(acpi); 2822 if (hp) 2823 return (hp->fn(cmd, addr, hp->arg)); 2824 2825 /* 2826 * Core ioctls are not permitted for non-writable user. 2827 * Currently, other ioctls just fetch information. 2828 * Not changing system behavior. 2829 */ 2830 if ((flag & FWRITE) == 0) 2831 return (EPERM); 2832 2833 /* Core system ioctls. */ 2834 switch (cmd) { 2835 case ACPIIO_SETSLPSTATE: 2836 error = EINVAL; 2837 state = *(int *)addr; 2838 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 2839 if (ACPI_SUCCESS(acpi_SetSleepState(sc, state))) 2840 error = 0; 2841 break; 2842 default: 2843 error = ENXIO; 2844 break; 2845 } 2846 2847 return (error); 2848 } 2849 2850 static int 2851 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2852 { 2853 int error; 2854 struct sbuf sb; 2855 UINT8 state, TypeA, TypeB; 2856 2857 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 2858 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) 2859 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 2860 sbuf_printf(&sb, "S%d ", state); 2861 sbuf_trim(&sb); 2862 sbuf_finish(&sb); 2863 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 2864 sbuf_delete(&sb); 2865 return (error); 2866 } 2867 2868 static int 2869 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2870 { 2871 char sleep_state[10]; 2872 int error; 2873 u_int new_state, old_state; 2874 2875 old_state = *(u_int *)oidp->oid_arg1; 2876 if (old_state > ACPI_S_STATES_MAX + 1) 2877 strlcpy(sleep_state, "unknown", sizeof(sleep_state)); 2878 else 2879 strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state)); 2880 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 2881 if (error == 0 && req->newptr != NULL) { 2882 new_state = ACPI_STATE_S0; 2883 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) 2884 if (strcmp(sleep_state, sleep_state_names[new_state]) == 0) 2885 break; 2886 if (new_state <= ACPI_S_STATES_MAX + 1) { 2887 if (new_state != old_state) 2888 *(u_int *)oidp->oid_arg1 = new_state; 2889 } else 2890 error = EINVAL; 2891 } 2892 2893 return (error); 2894 } 2895 2896 /* Inform devctl(4) when we receive a Notify. */ 2897 void 2898 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 2899 { 2900 char notify_buf[16]; 2901 ACPI_BUFFER handle_buf; 2902 ACPI_STATUS status; 2903 2904 if (subsystem == NULL) 2905 return; 2906 2907 handle_buf.Pointer = NULL; 2908 handle_buf.Length = ACPI_ALLOCATE_BUFFER; 2909 status = AcpiNsHandleToPathname(h, &handle_buf); 2910 if (ACPI_FAILURE(status)) 2911 return; 2912 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 2913 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 2914 AcpiOsFree(handle_buf.Pointer); 2915 } 2916 2917 #ifdef ACPI_DEBUG 2918 /* 2919 * Support for parsing debug options from the kernel environment. 2920 * 2921 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 2922 * by specifying the names of the bits in the debug.acpi.layer and 2923 * debug.acpi.level environment variables. Bits may be unset by 2924 * prefixing the bit name with !. 2925 */ 2926 struct debugtag 2927 { 2928 char *name; 2929 UINT32 value; 2930 }; 2931 2932 static struct debugtag dbg_layer[] = { 2933 {"ACPI_UTILITIES", ACPI_UTILITIES}, 2934 {"ACPI_HARDWARE", ACPI_HARDWARE}, 2935 {"ACPI_EVENTS", ACPI_EVENTS}, 2936 {"ACPI_TABLES", ACPI_TABLES}, 2937 {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2938 {"ACPI_PARSER", ACPI_PARSER}, 2939 {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2940 {"ACPI_EXECUTER", ACPI_EXECUTER}, 2941 {"ACPI_RESOURCES", ACPI_RESOURCES}, 2942 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 2943 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2944 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2945 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 2946 2947 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2948 {"ACPI_BATTERY", ACPI_BATTERY}, 2949 {"ACPI_BUS", ACPI_BUS}, 2950 {"ACPI_BUTTON", ACPI_BUTTON}, 2951 {"ACPI_EC", ACPI_EC}, 2952 {"ACPI_FAN", ACPI_FAN}, 2953 {"ACPI_POWERRES", ACPI_POWERRES}, 2954 {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2955 {"ACPI_THERMAL", ACPI_THERMAL}, 2956 {"ACPI_TIMER", ACPI_TIMER}, 2957 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 2958 {NULL, 0} 2959 }; 2960 2961 static struct debugtag dbg_level[] = { 2962 {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 2963 {"ACPI_LV_WARN", ACPI_LV_WARN}, 2964 {"ACPI_LV_INIT", ACPI_LV_INIT}, 2965 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 2966 {"ACPI_LV_INFO", ACPI_LV_INFO}, 2967 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2968 2969 /* Trace verbosity level 1 [Standard Trace Level] */ 2970 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 2971 {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 2972 {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2973 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 2974 {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 2975 {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 2976 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 2977 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 2978 {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 2979 {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 2980 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 2981 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 2982 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 2983 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2984 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2985 2986 /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2987 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2988 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2989 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2990 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 2991 {"ACPI_LV_ALL", ACPI_LV_ALL}, 2992 2993 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2994 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2995 {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2996 {"ACPI_LV_IO", ACPI_LV_IO}, 2997 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2998 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2999 3000 /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 3001 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 3002 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 3003 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 3004 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 3005 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 3006 {NULL, 0} 3007 }; 3008 3009 static void 3010 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 3011 { 3012 char *ep; 3013 int i, l; 3014 int set; 3015 3016 while (*cp) { 3017 if (isspace(*cp)) { 3018 cp++; 3019 continue; 3020 } 3021 ep = cp; 3022 while (*ep && !isspace(*ep)) 3023 ep++; 3024 if (*cp == '!') { 3025 set = 0; 3026 cp++; 3027 if (cp == ep) 3028 continue; 3029 } else { 3030 set = 1; 3031 } 3032 l = ep - cp; 3033 for (i = 0; tag[i].name != NULL; i++) { 3034 if (!strncmp(cp, tag[i].name, l)) { 3035 if (set) 3036 *flag |= tag[i].value; 3037 else 3038 *flag &= ~tag[i].value; 3039 } 3040 } 3041 cp = ep; 3042 } 3043 } 3044 3045 static void 3046 acpi_set_debugging(void *junk) 3047 { 3048 char *layer, *level; 3049 3050 if (cold) { 3051 AcpiDbgLayer = 0; 3052 AcpiDbgLevel = 0; 3053 } 3054 3055 layer = getenv("debug.acpi.layer"); 3056 level = getenv("debug.acpi.level"); 3057 if (layer == NULL && level == NULL) 3058 return; 3059 3060 printf("ACPI set debug"); 3061 if (layer != NULL) { 3062 if (strcmp("NONE", layer) != 0) 3063 printf(" layer '%s'", layer); 3064 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 3065 freeenv(layer); 3066 } 3067 if (level != NULL) { 3068 if (strcmp("NONE", level) != 0) 3069 printf(" level '%s'", level); 3070 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 3071 freeenv(level); 3072 } 3073 printf("\n"); 3074 } 3075 3076 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3077 NULL); 3078 3079 static int 3080 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 3081 { 3082 int error, *dbg; 3083 struct debugtag *tag; 3084 struct sbuf sb; 3085 3086 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 3087 return (ENOMEM); 3088 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 3089 tag = &dbg_layer[0]; 3090 dbg = &AcpiDbgLayer; 3091 } else { 3092 tag = &dbg_level[0]; 3093 dbg = &AcpiDbgLevel; 3094 } 3095 3096 /* Get old values if this is a get request. */ 3097 ACPI_SERIAL_BEGIN(acpi); 3098 if (*dbg == 0) { 3099 sbuf_cpy(&sb, "NONE"); 3100 } else if (req->newptr == NULL) { 3101 for (; tag->name != NULL; tag++) { 3102 if ((*dbg & tag->value) == tag->value) 3103 sbuf_printf(&sb, "%s ", tag->name); 3104 } 3105 } 3106 sbuf_trim(&sb); 3107 sbuf_finish(&sb); 3108 3109 /* Copy out the old values to the user. */ 3110 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 3111 sbuf_delete(&sb); 3112 3113 /* If the user is setting a string, parse it. */ 3114 if (error == 0 && req->newptr != NULL) { 3115 *dbg = 0; 3116 setenv((char *)oidp->oid_arg1, (char *)req->newptr); 3117 acpi_set_debugging(NULL); 3118 } 3119 ACPI_SERIAL_END(acpi); 3120 3121 return (error); 3122 } 3123 3124 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 3125 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 3126 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 3127 "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3128 #endif /* ACPI_DEBUG */ 3129 3130 static int 3131 acpi_pm_func(u_long cmd, void *arg, ...) 3132 { 3133 int state, acpi_state; 3134 int error; 3135 struct acpi_softc *sc; 3136 va_list ap; 3137 3138 error = 0; 3139 switch (cmd) { 3140 case POWER_CMD_SUSPEND: 3141 sc = (struct acpi_softc *)arg; 3142 if (sc == NULL) { 3143 error = EINVAL; 3144 goto out; 3145 } 3146 3147 va_start(ap, arg); 3148 state = va_arg(ap, int); 3149 va_end(ap); 3150 3151 switch (state) { 3152 case POWER_SLEEP_STATE_STANDBY: 3153 acpi_state = sc->acpi_standby_sx; 3154 break; 3155 case POWER_SLEEP_STATE_SUSPEND: 3156 acpi_state = sc->acpi_suspend_sx; 3157 break; 3158 case POWER_SLEEP_STATE_HIBERNATE: 3159 acpi_state = ACPI_STATE_S4; 3160 break; 3161 default: 3162 error = EINVAL; 3163 goto out; 3164 } 3165 3166 acpi_SetSleepState(sc, acpi_state); 3167 break; 3168 default: 3169 error = EINVAL; 3170 goto out; 3171 } 3172 3173 out: 3174 return (error); 3175 } 3176 3177 static void 3178 acpi_pm_register(void *arg) 3179 { 3180 if (!cold || resource_disabled("acpi", 0)) 3181 return; 3182 3183 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3184 } 3185 3186 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3187