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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
29 * $FreeBSD: src/sys/dev/acpica/acpi.c,v 1.160 2004/06/14 03:52:19 njl Exp $
30 * $DragonFly: src/sys/dev/acpica5/acpi.c,v 1.37 2008/10/03 00:26:21 hasso Exp $
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/fcntl.h>
38 #include <sys/malloc.h>
41 #include <sys/device.h>
42 #include <sys/reboot.h>
43 #include <sys/sysctl.h>
44 #include <sys/ctype.h>
45 #include <sys/linker.h>
46 #include <sys/power.h>
50 #include <sys/thread2.h>
53 #include <machine/clock.h>
54 #include <machine/globaldata.h>
55 #include <bus/isa/isavar.h>
59 #include <dev/acpica5/acpivar.h>
60 #include <dev/acpica5/acpiio.h>
63 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
65 /* Hooks for the ACPI CA debugging infrastructure */
66 #define _COMPONENT ACPI_BUS
67 ACPI_MODULE_NAME("ACPI")
69 static d_open_t acpiopen;
70 static d_close_t acpiclose;
71 static d_ioctl_t acpiioctl;
73 #define CDEV_MAJOR 152
74 static struct dev_ops acpi_ops = {
75 { "acpi", CDEV_MAJOR, 0 },
81 #if __FreeBSD_version >= 500000
82 struct mtx acpi_mutex;
85 /* Local pools for managing system resources for ACPI child devices. */
86 struct rman acpi_rman_io, acpi_rman_mem;
94 #define ACPI_OEM_REV_ANY 0
96 static struct acpi_quirks acpi_quirks_table[] = {
98 /* Bad PCI routing table. Used on some SuperMicro boards. */
99 { "PTLTD ", 0x06040000, "pci_link" },
101 #ifdef ACPI_QUIRK_VMWARE
103 * VMware's ACPI-fast24 timer runs roughly 65 times too fast, and not
104 * predictably/monotonic either. This is observed at least under SMP
107 * NOTE: this combination of OemId and OemRevision is NOT unique; it is
108 * known or suspected that at least some SuperMicro boards (see above) and
109 * the Compaq Presario 1952 use this combination. That's why this quirks
110 * entry is guarded by an #ifdef, and associated config option.
112 { "PTLTD ", 0x06040000, "timer" },
113 #endif /* ACPI_QUIRK_VMWARE */
117 static int acpi_modevent(struct module *mod, int event, void *junk);
118 static int acpi_identify(driver_t *driver, device_t parent);
119 static int acpi_probe(device_t dev);
120 static int acpi_attach(device_t dev);
121 static int acpi_shutdown(device_t dev);
122 static void acpi_quirks_set(void);
123 static device_t acpi_add_child(device_t bus, device_t parent, int order,
124 const char *name, int unit);
125 static int acpi_print_child(device_t bus, device_t child);
126 static int acpi_read_ivar(device_t dev, device_t child, int index,
128 static int acpi_write_ivar(device_t dev, device_t child, int index,
130 static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
131 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
132 int type, int *rid, u_long start, u_long end,
133 u_long count, u_int flags);
134 static int acpi_release_resource(device_t bus, device_t child, int type,
135 int rid, struct resource *r);
136 static uint32_t acpi_isa_get_logicalid(device_t dev);
137 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
138 static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids);
139 static int acpi_isa_pnp_probe(device_t bus, device_t child,
140 struct isa_pnp_id *ids);
141 static void acpi_probe_children(device_t bus);
142 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
143 void *context, void **status);
144 static void acpi_shutdown_pre_sync(void *arg, int howto);
145 static void acpi_shutdown_final(void *arg, int howto);
146 static void acpi_shutdown_poweroff(void *arg);
147 static void acpi_enable_fixed_events(struct acpi_softc *sc);
148 static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw);
149 static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context,
151 static int acpi_wake_limit_walk(int sstate);
152 static int acpi_wake_sysctl_walk(device_t dev);
154 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
156 static void acpi_system_eventhandler_sleep(void *arg, int state);
157 static void acpi_system_eventhandler_wakeup(void *arg, int state);
158 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
159 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
160 static int acpi_pm_func(u_long cmd, void *arg, ...);
161 static int acpi_child_location_str_method(device_t acdev, device_t child,
162 char *buf, size_t buflen);
163 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
164 char *buf, size_t buflen);
166 static device_method_t acpi_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_identify, acpi_identify),
169 DEVMETHOD(device_probe, acpi_probe),
170 DEVMETHOD(device_attach, acpi_attach),
171 DEVMETHOD(device_shutdown, acpi_shutdown),
172 DEVMETHOD(device_detach, bus_generic_detach),
173 DEVMETHOD(device_suspend, bus_generic_suspend),
174 DEVMETHOD(device_resume, bus_generic_resume),
177 DEVMETHOD(bus_add_child, acpi_add_child),
178 DEVMETHOD(bus_print_child, acpi_print_child),
179 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
180 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
181 DEVMETHOD(bus_get_resource_list, acpi_get_rlist),
182 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
183 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
184 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
185 DEVMETHOD(bus_release_resource, acpi_release_resource),
186 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
187 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
188 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
189 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
190 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
191 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
192 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
195 DEVMETHOD(acpi_id_probe, acpi_device_id_probe),
198 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
203 static driver_t acpi_driver = {
206 sizeof(struct acpi_softc),
209 static devclass_t acpi_devclass;
210 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
211 MODULE_VERSION(acpi, 1);
213 static const char* sleep_state_names[] = {
214 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
216 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
217 static char acpi_ca_version[12];
218 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
219 acpi_ca_version, 0, "Version of Intel ACPI-CA");
222 * Allow override of whether methods execute in parallel or not.
223 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
224 * errors for AML that really can't handle parallel method execution.
225 * It is off by default since this breaks recursive methods and
226 * some IBMs use such code.
228 static int acpi_serialize_methods;
229 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
232 * ACPI can only be loaded as a module by the loader; activating it after
233 * system bootstrap time is not useful, and can be fatal to the system.
234 * It also cannot be unloaded, since the entire system bus heirarchy hangs
238 acpi_modevent(struct module *mod, int event, void *junk)
243 kprintf("The ACPI driver cannot be loaded after boot.\n");
248 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
258 * Perform early initialization.
266 static int error, started = 0;
268 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
271 return_VALUE (error);
274 /* Start up the ACPI CA subsystem. */
276 debugpoint = kgetenv("debug.acpi.debugger");
278 if (!strcmp(debugpoint, "init"))
279 acpi_EnterDebugger();
280 kfreeenv(debugpoint);
283 error = AcpiInitializeTables(NULL, 16, TRUE);
284 if (ACPI_FAILURE(error)) {
285 kprintf("ACPI: table initialization failed:\n");
286 return_VALUE (error);
289 /* Set up any quirks we have for this XSDT. */
291 if (acpi_disabled("acpi"))
292 return_VALUE (AE_ERROR);
294 return_VALUE (AE_OK);
298 * Detect ACPI, perform early initialisation
301 acpi_identify(driver_t *driver, device_t parent)
306 * No sense rescanning an ACPI 'bus'.
308 if (device_get_state(parent) == DS_ATTACHED)
311 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
316 /* Check that we haven't been disabled with a hint. */
317 if (resource_disabled("acpi", 0))
320 /* Make sure we're not being doubly invoked. */
321 if (device_find_child(parent, "acpi", 0) != NULL)
324 /* Initialize ACPI-CA. */
325 if (ACPI_FAILURE(acpi_Startup()))
328 ksnprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
330 /* Attach the actual ACPI device. */
331 if ((child = BUS_ADD_CHILD(parent, parent, 0, "acpi", 0)) == NULL) {
332 device_printf(parent, "ACPI: could not attach\n");
339 * Get a mapping of the root table header, as ACPICA code no longer
340 * keeps local copy of RSDT/XSDT
342 * return value: if non-NULL, mapped physical address of root table header.
343 * caller is supposed to unmap the region by AcpiOsUnmapMemory()
345 static ACPI_TABLE_HEADER *
346 acpi_map_rsdt_header(void)
348 ACPI_PHYSICAL_ADDRESS rsdp_addr, addr;
349 ACPI_TABLE_RSDP *rsdp;
351 if ((rsdp_addr = AcpiOsGetRootPointer()) == 0)
353 if ((rsdp = AcpiOsMapMemory(rsdp_addr, sizeof(*rsdp))) == NULL)
355 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
356 addr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
358 addr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
359 AcpiOsUnmapMemory(rsdp, sizeof(*rsdp));
361 return AcpiOsMapMemory(addr, sizeof(ACPI_TABLE_HEADER));
365 * Fetch some descriptive data from ACPI to put in our attach message
368 acpi_probe(device_t dev)
370 ACPI_TABLE_HEADER *th;
376 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
378 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
379 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
381 device_printf(dev, "Other PM system enabled.\n");
387 th = acpi_map_rsdt_header();
389 device_printf(dev, "couldn't get RSDT header\n");
394 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
395 sbuf_bcat(&sb, th->OemId, 6);
398 sbuf_bcat(&sb, th->OemTableId, 8);
401 device_set_desc_copy(dev, sbuf_data(&sb));
403 AcpiOsUnmapMemory(th, sizeof(*th));
411 acpi_attach(device_t dev)
413 struct acpi_softc *sc;
419 ACPI_TABLE_FACS *facsp;
425 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
426 #if __FreeBSD_version >= 500000
427 /* Initialise the ACPI mutex */
428 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
432 sc = device_get_softc(dev);
433 bzero(sc, sizeof(*sc));
435 callout_init(&sc->acpi_sleep_timer);
437 if ((error = acpi_task_thread_init())) {
438 device_printf(dev, "Could not start task thread.\n");
443 * Set the globals from our tunables. This is needed because ACPI-CA
444 * uses UINT8 for some values and we have no tunable_byte.
446 AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods;
447 AcpiGbl_EnableInterpreterSlack = TRUE;
451 debugpoint = kgetenv("debug.acpi.debugger");
453 if (!strcmp(debugpoint, "tables"))
454 acpi_EnterDebugger();
455 kfreeenv(debugpoint);
459 if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
460 kprintf("ACPI: initialisation failed: %s\n",
461 AcpiFormatException(status));
464 if (ACPI_FAILURE(status = AcpiLoadTables())) {
465 kprintf("ACPI: table load failed: %s\n", AcpiFormatException(status));
469 /* Initialize resource manager. */
470 acpi_rman_io.rm_type = RMAN_ARRAY;
471 acpi_rman_io.rm_start = 0;
472 acpi_rman_io.rm_end = 0xffff;
473 acpi_rman_io.rm_descr = "I/O ports";
474 if (rman_init(&acpi_rman_io) != 0)
475 panic("acpi rman_init IO ports failed");
476 acpi_rman_mem.rm_type = RMAN_ARRAY;
477 acpi_rman_mem.rm_start = 0;
478 acpi_rman_mem.rm_end = ~0ul;
479 acpi_rman_mem.rm_descr = "I/O memory addresses";
480 if (rman_init(&acpi_rman_mem) != 0)
481 panic("acpi rman_init memory failed");
484 debugpoint = kgetenv("debug.acpi.debugger");
486 if (!strcmp(debugpoint, "spaces"))
487 acpi_EnterDebugger();
488 kfreeenv(debugpoint);
491 /* Install the default address space handlers. */
492 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
493 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
494 if (ACPI_FAILURE(status)) {
495 device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
496 AcpiFormatException(status));
499 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
500 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
501 if (ACPI_FAILURE(status)) {
502 device_printf(dev, "Could not initialise SystemIO handler: %s\n",
503 AcpiFormatException(status));
506 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
507 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
508 if (ACPI_FAILURE(status)) {
509 device_printf(dev, "could not initialise PciConfig handler: %s\n",
510 AcpiFormatException(status));
515 * Bring ACPI fully online.
517 * Note that some systems (specifically, those with namespace evaluation
518 * issues that require the avoidance of parts of the namespace) must
519 * avoid running _INI and _STA on everything, as well as dodging the final
522 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
523 * For avoiding portions of the namespace without totally disabling _INI
524 * and _STA, use "debug.acpi.avoid.paths".
526 * XXX We should arrange for the object init pass after we have attached
527 * all our child devices, but on many systems it works here.
530 debugpoint = kgetenv("debug.acpi.debugger");
532 if (!strcmp(debugpoint, "enable"))
533 acpi_EnterDebugger();
534 kfreeenv(debugpoint);
538 if (ktestenv("debug.acpi.avoid"))
539 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
540 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
541 device_printf(dev, "Could not enable ACPI: %s\n",
542 AcpiFormatException(status));
547 * Call the ECDT probe function to provide EC functionality before
548 * the namespace has been evaluated.
550 acpi_ec_ecdt_probe(dev);
552 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
553 device_printf(dev, "Could not initialize ACPI objects: %s\n",
554 AcpiFormatException(status));
559 * Setup our sysctl tree.
561 * XXX: This doesn't check to make sure that none of these fail.
563 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
564 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
565 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
566 device_get_name(dev), CTLFLAG_RD, 0, "");
567 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
568 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
569 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
570 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
571 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
572 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
573 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
574 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
575 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
576 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
577 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
578 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
579 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
580 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
581 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
582 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
583 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
584 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
585 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
586 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
587 &sc->acpi_sleep_delay, 0, "sleep delay");
588 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
589 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
590 &sc->acpi_s4bios, 0, "S4BIOS mode");
591 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
592 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
593 &sc->acpi_verbose, 0, "verbose mode");
594 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
595 OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
596 &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
599 * Default to 1 second before sleeping to give some machines time to
602 sc->acpi_sleep_delay = 1;
603 sc->acpi_disable_on_poweroff = 0;
605 sc->acpi_verbose = 1;
606 if ((env = kgetenv("hw.acpi.verbose")) && strcmp(env, "0")) {
607 sc->acpi_verbose = 1;
611 /* Only enable S4BIOS by default if the FACS says it is available. */
612 status = AcpiGetTableByIndex(ACPI_TABLE_INDEX_FACS,
613 (ACPI_TABLE_HEADER **)&facsp);
614 if (ACPI_SUCCESS(status)) {
615 if ((facsp->Flags & ACPI_FACS_S4_BIOS_PRESENT) != 0)
620 * Dispatch the default sleep state to devices. The lid switch is set
621 * to NONE by default to avoid surprising users.
623 sc->acpi_power_button_sx = ACPI_STATE_S5;
624 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
625 sc->acpi_standby_sx = ACPI_STATE_S1;
626 sc->acpi_suspend_sx = ACPI_STATE_S3;
628 /* Pick the first valid sleep state for the sleep button default. */
629 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
630 for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
631 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
632 sc->acpi_sleep_button_sx = state;
636 acpi_enable_fixed_events(sc);
639 * Scan the namespace and attach/initialise children.
642 debugpoint = kgetenv("debug.acpi.debugger");
644 if (!strcmp(debugpoint, "probe"))
645 acpi_EnterDebugger();
646 kfreeenv(debugpoint);
650 /* Register our shutdown handlers */
651 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
653 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
657 * Register our acpi event handlers.
658 * XXX should be configurable eg. via userland policy manager.
660 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
661 sc, ACPI_EVENT_PRI_LAST);
662 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
663 sc, ACPI_EVENT_PRI_LAST);
665 /* Flag our initial states. */
666 sc->acpi_enabled = 1;
667 sc->acpi_sstate = ACPI_STATE_S0;
668 sc->acpi_sleep_disabled = 0;
670 /* Create the control device */
671 sc->acpi_dev_t = make_dev(&acpi_ops, 0, UID_ROOT, GID_WHEEL, 0644,
673 sc->acpi_dev_t->si_drv1 = sc;
676 debugpoint = kgetenv("debug.acpi.debugger");
678 if (strcmp(debugpoint, "running") == 0)
679 acpi_EnterDebugger();
680 kfreeenv(debugpoint);
684 if ((error = acpi_machdep_init(dev)))
687 /* Register ACPI again to pass the correct argument of pm_func. */
688 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
690 if (!acpi_disabled("bus"))
691 acpi_probe_children(dev);
698 cputimer_intr_pmfixup();
699 return_VALUE (error);
703 acpi_shutdown(device_t dev)
705 /* Allow children to shutdown first. */
706 bus_generic_shutdown(dev);
708 /* Disable all wake GPEs not appropriate for reboot/poweroff. */
709 acpi_wake_limit_walk(ACPI_STATE_S5);
714 acpi_quirks_set(void)
716 ACPI_TABLE_HEADER *rsdt;
717 struct acpi_quirks *quirk;
722 * If the user loaded a custom table or disabled "quirks", leave
723 * the settings alone.
726 if ((env = kgetenv("acpi_dsdt_load")) != NULL) {
727 /* XXX No strcasecmp but this is good enough. */
728 if (*env == 'Y' || *env == 'y')
732 if ((env = kgetenv("debug.acpi.disabled")) != NULL) {
733 if (strstr("quirks", env) != NULL)
739 * Search through our quirk table and concatenate the disabled
740 * values with whatever we find.
742 if ((rsdt = acpi_map_rsdt_header()) == NULL)
744 for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
745 if (!strncmp(rsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
746 (rsdt->OemRevision == quirk->OemRevision ||
747 quirk->OemRevision == ACPI_OEM_REV_ANY)) {
748 len += strlen(quirk->value) + 2;
749 if ((tmp = kmalloc(len, M_TEMP, M_NOWAIT)) == NULL)
751 ksprintf(tmp, "%s %s", env ? env : "", quirk->value);
752 ksetenv("debug.acpi.disabled", tmp);
757 AcpiOsUnmapMemory(rsdt, sizeof(*rsdt));
765 * Handle a new device being added
768 acpi_add_child(device_t bus, device_t parent, int order,
769 const char *name, int unit)
771 struct acpi_device *ad;
774 ad = kmalloc(sizeof(*ad), M_ACPIDEV, M_INTWAIT | M_ZERO);
776 resource_list_init(&ad->ad_rl);
778 child = device_add_child_ordered(parent, order, name, unit);
780 device_set_ivars(child, ad);
785 acpi_print_child(device_t bus, device_t child)
787 struct acpi_device *adev = device_get_ivars(child);
788 struct resource_list *rl = &adev->ad_rl;
791 retval += bus_print_child_header(bus, child);
792 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
793 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
794 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
795 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
796 retval += bus_print_child_footer(bus, child);
801 /* Location hint for devctl(8) */
803 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
806 struct acpi_device *dinfo = device_get_ivars(child);
808 if (dinfo->ad_handle)
809 ksnprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
811 ksnprintf(buf, buflen, "magic=unknown");
815 /* PnP information for devctl(8) */
817 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
820 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
821 ACPI_DEVICE_INFO *adinfo;
822 struct acpi_device *dinfo = device_get_ivars(child);
826 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
827 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
830 ksnprintf(buf, buflen, "Unknown");
832 ksnprintf(buf, buflen, "_HID=%s _UID=%lu",
833 (adinfo->Valid & ACPI_VALID_HID) ?
834 adinfo->HardwareId.Value : "UNKNOWN",
835 (adinfo->Valid & ACPI_VALID_UID) ?
836 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
845 * Handle per-device ivars
848 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
850 struct acpi_device *ad;
852 if ((ad = device_get_ivars(child)) == NULL) {
853 kprintf("device has no ivars\n");
857 /* ACPI and ISA compatibility ivars */
859 case ACPI_IVAR_HANDLE:
860 *(ACPI_HANDLE *)result = ad->ad_handle;
862 case ACPI_IVAR_MAGIC:
863 *(int *)result = ad->ad_magic;
865 case ACPI_IVAR_PRIVATE:
866 *(void **)result = ad->ad_private;
868 case ISA_IVAR_VENDORID:
869 case ISA_IVAR_SERIAL:
870 case ISA_IVAR_COMPATID:
873 case ISA_IVAR_LOGICALID:
874 *(int *)result = acpi_isa_get_logicalid(child);
884 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
886 struct acpi_device *ad;
888 if ((ad = device_get_ivars(child)) == NULL) {
889 kprintf("device has no ivars\n");
894 case ACPI_IVAR_HANDLE:
895 ad->ad_handle = (ACPI_HANDLE)value;
897 case ACPI_IVAR_MAGIC:
898 ad->ad_magic = (int)value;
900 case ACPI_IVAR_PRIVATE:
901 ad->ad_private = (void *)value;
904 panic("bad ivar write request (%d)", index);
912 * Handle child resource allocation/removal
914 static struct resource_list *
915 acpi_get_rlist(device_t dev, device_t child)
917 struct acpi_device *ad;
919 ad = device_get_ivars(child);
923 static struct resource *
924 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
925 u_long start, u_long end, u_long count, u_int flags)
927 struct acpi_device *ad = device_get_ivars(child);
928 struct resource_list *rl = &ad->ad_rl;
929 struct resource_list_entry *rle;
930 struct resource *res;
935 * If this is an allocation of the "default" range for a given RID, and
936 * we know what the resources for this device are (i.e., they're on the
937 * child's resource list), use those start/end values.
939 if (start == 0UL && end == ~0UL) {
940 rle = resource_list_find(rl, type, *rid);
948 /* If we don't manage this address, pass the request up to the parent. */
949 rle = acpi_sysres_find(type, start);
951 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
952 start, end, count, flags));
955 /* We only handle memory and IO resources through rman. */
964 panic("acpi_alloc_resource: invalid res type %d", type);
967 /* If we do know it, allocate it from the local pool. */
968 needactivate = flags & RF_ACTIVE;
970 res = rman_reserve_resource(rm, start, end, count, flags, child);
974 /* Copy the bus tag from the pre-allocated resource. */
975 rman_set_bustag(res, rman_get_bustag(rle->res));
976 if (type == SYS_RES_IOPORT)
977 rman_set_bushandle(res, res->r_start);
979 /* If requested, activate the resource using the parent's method. */
981 if (bus_activate_resource(child, type, *rid, res) != 0) {
982 rman_release_resource(res);
990 acpi_release_resource(device_t bus, device_t child, int type, int rid,
996 * If we know about this address, deactivate it and release it to the
997 * local pool. If we don't, pass this request up to the parent.
999 if (acpi_sysres_find(type, rman_get_start(r)) == NULL) {
1000 if (rman_get_flags(r) & RF_ACTIVE) {
1001 ret = bus_deactivate_resource(child, type, rid, r);
1005 ret = rman_release_resource(r);
1007 ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
1012 /* Allocate an IO port or memory resource, given its GAS. */
1014 acpi_bus_alloc_gas(device_t dev, int *rid, const ACPI_GENERIC_ADDRESS *gas,
1019 if (gas == NULL || gas->BitWidth < 8)
1022 switch (gas->SpaceId) {
1023 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1024 type = SYS_RES_MEMORY;
1026 case ACPI_ADR_SPACE_SYSTEM_IO:
1027 type = SYS_RES_IOPORT;
1033 bus_set_resource(dev, type, *rid, gas->Address, gas->BitWidth / 8);
1034 return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE | flags));
1038 * Handle ISA-like devices probing for a PnP ID to match.
1040 #define PNP_EISAID(s) \
1041 ((((s[0] - '@') & 0x1f) << 2) \
1042 | (((s[1] - '@') & 0x18) >> 3) \
1043 | (((s[1] - '@') & 0x07) << 13) \
1044 | (((s[2] - '@') & 0x1f) << 8) \
1045 | (PNP_HEXTONUM(s[4]) << 16) \
1046 | (PNP_HEXTONUM(s[3]) << 20) \
1047 | (PNP_HEXTONUM(s[6]) << 24) \
1048 | (PNP_HEXTONUM(s[5]) << 28))
1051 acpi_isa_get_logicalid(device_t dev)
1053 ACPI_DEVICE_INFO *devinfo;
1060 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1064 buf.Length = ACPI_ALLOCATE_BUFFER;
1068 /* Fetch and validate the HID. */
1069 if ((h = acpi_get_handle(dev)) == NULL)
1071 error = AcpiGetObjectInfo(h, &buf);
1072 if (ACPI_FAILURE(error))
1074 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1076 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
1077 pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1080 if (buf.Pointer != NULL)
1081 AcpiOsFree(buf.Pointer);
1083 return_VALUE (pnpid);
1087 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1089 ACPI_DEVICE_INFO *devinfo;
1097 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1102 buf.Length = ACPI_ALLOCATE_BUFFER;
1106 /* Fetch and validate the CID */
1107 if ((h = acpi_get_handle(dev)) == NULL)
1109 error = AcpiGetObjectInfo(h, &buf);
1110 if (ACPI_FAILURE(error))
1112 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1113 if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1116 if (devinfo->CompatibilityId.Count < count)
1117 count = devinfo->CompatibilityId.Count;
1118 for (i = 0; i < count; i++) {
1119 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
1121 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
1126 if (buf.Pointer != NULL)
1127 AcpiOsFree(buf.Pointer);
1129 return_VALUE (valid);
1133 acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1138 h = acpi_get_handle(dev);
1139 if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
1142 /* Try to match one of the array of IDs with a HID or CID. */
1143 for (i = 0; ids[i] != NULL; i++) {
1144 if (acpi_MatchHid(h, ids[i]))
1151 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1153 int result, cid_count, i;
1154 uint32_t lid, cids[8];
1156 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1159 * ISA-style drivers attached to ACPI may persist and
1160 * probe manually if we return ENOENT. We never want
1161 * that to happen, so don't ever return it.
1165 /* Scan the supplied IDs for a match */
1166 lid = acpi_isa_get_logicalid(child);
1167 cid_count = acpi_isa_get_compatid(child, cids, 8);
1168 while (ids && ids->ip_id) {
1169 if (lid == ids->ip_id) {
1173 for (i = 0; i < cid_count; i++) {
1174 if (cids[i] == ids->ip_id) {
1183 return_VALUE (result);
1187 * Scan relevant portions of the ACPI namespace and attach child devices.
1189 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1190 * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
1193 acpi_probe_children(device_t bus)
1198 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
1200 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1204 * Scan the namespace and insert placeholders for all the devices that
1205 * we find. We also probe/attach any early devices.
1207 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1208 * we want to create nodes for all devices, not just those that are
1209 * currently present. (This assumes that we don't want to create/remove
1210 * devices as they appear, which might be smarter.)
1212 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1213 for (i = 0; scopes[i] != NULL; i++) {
1214 status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1215 if (ACPI_SUCCESS(status)) {
1216 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1221 /* Create any static children by calling device identify methods. */
1222 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1223 bus_generic_probe(bus);
1226 * Scan all of the child devices we have created and let them probe/attach.
1228 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
1229 bus_generic_attach(bus);
1232 * Some of these children may have attached others as part of their attach
1233 * process (eg. the root PCI bus driver), so rescan.
1235 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
1236 bus_generic_attach(bus);
1238 /* Attach wake sysctls. */
1239 acpi_wake_sysctl_walk(bus);
1241 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1246 acpi_probe_order(ACPI_HANDLE handle, int level, int *order)
1251 /* IO port and memory system resource holders are first. */
1252 if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) {
1257 /* The embedded controller is needed to handle accesses early. */
1258 if (acpi_MatchHid(handle, "PNP0C09")) {
1263 *order = (level + 1) * 10;
1268 * Evaluate a child device and determine whether we might attach a device to
1272 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1274 ACPI_OBJECT_TYPE type;
1275 device_t child, bus;
1276 int order, probe_now;
1278 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1280 /* Skip this device if we think we'll have trouble with it. */
1281 if (acpi_avoid(handle)) {
1282 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "not scanning '%s'\n",
1283 acpi_name(handle)));
1284 return_ACPI_STATUS (AE_OK);
1287 bus = (device_t)context;
1288 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1290 case ACPI_TYPE_DEVICE:
1291 case ACPI_TYPE_PROCESSOR:
1292 case ACPI_TYPE_THERMAL:
1293 case ACPI_TYPE_POWER:
1294 if (acpi_disabled("children"))
1298 * Create a placeholder device for this node. Sort the placeholder
1299 * so that the probe/attach passes will run breadth-first.
1301 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1302 acpi_name(handle)));
1303 probe_now = acpi_probe_order(handle, level, &order);
1304 child = BUS_ADD_CHILD(bus, bus, order, NULL, -1);
1308 /* Associate the handle with the device_t and vice versa. */
1309 acpi_set_handle(child, handle);
1310 AcpiAttachData(handle, acpi_fake_objhandler, child);
1312 /* Check if the device can generate wake events. */
1313 if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL)))
1314 device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE);
1317 * Check that the device is present. If it's not present,
1318 * leave it disabled (so that we have a device_t attached to
1319 * the handle, but we don't probe it).
1321 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1322 device_disable(child);
1327 * Get the device's resource settings and attach them.
1328 * Note that if the device has _PRS but no _CRS, we need
1329 * to decide when it's appropriate to try to configure the
1330 * device. Ignore the return value here; it's OK for the
1331 * device not to have any resources.
1333 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1335 /* If order was overridden, probe/attach now rather than later. */
1337 device_probe_and_attach(child);
1342 return_ACPI_STATUS (AE_OK);
1346 acpi_shutdown_pre_sync(void *arg, int howto)
1348 struct acpi_softc *sc = arg;
1353 * Disable all ACPI events before soft off, otherwise the system
1354 * will be turned on again on some laptops.
1356 * XXX this should probably be restricted to masking some events just
1357 * before powering down, since we may still need ACPI during the
1360 if (sc->acpi_disable_on_poweroff)
1365 * AcpiAttachData() requires an object handler but never uses it. This is a
1366 * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
1369 acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
1374 acpi_shutdown_final(void *arg, int howto)
1380 * If powering off, run the actual shutdown code on each processor.
1381 * It will only perform the shutdown on the BSP. Some chipsets do
1382 * not power off the system correctly if called from an AP.
1384 if ((howto & RB_POWEROFF) != 0) {
1385 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1386 if (ACPI_FAILURE(status)) {
1387 kprintf("AcpiEnterSleepStatePrep failed - %s\n",
1388 AcpiFormatException(status));
1391 kprintf("Powering system off using ACPI\n");
1392 acpi_shutdown_poweroff(NULL);
1394 kprintf("Shutting down ACPI\n");
1400 * Since this function may be called with locks held or in an unknown
1401 * context, it cannot allocate memory, acquire locks, sleep, etc.
1404 acpi_shutdown_poweroff(void *arg)
1410 /* Only attempt to power off if this is the BSP (cpuid 0). */
1411 if (mdcpu->mi.gd_cpuid != 0)
1414 ACPI_DISABLE_IRQS();
1415 status = AcpiEnterSleepState(ACPI_STATE_S5);
1416 if (ACPI_FAILURE(status)) {
1417 kprintf("ACPI power-off failed - %s\n", AcpiFormatException(status));
1420 kprintf("ACPI power-off failed - timeout\n");
1425 acpi_enable_fixed_events(struct acpi_softc *sc)
1427 static int first_time = 1;
1431 /* Enable and clear fixed events and install handlers. */
1432 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
1433 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1434 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1435 acpi_event_power_button_sleep, sc);
1437 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1439 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1440 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1441 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1442 acpi_event_sleep_button_sleep, sc);
1444 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
1451 * Returns true if the device is actually present and should
1452 * be attached to. This requires the present, enabled, UI-visible
1453 * and diagnostics-passed bits to be set.
1456 acpi_DeviceIsPresent(device_t dev)
1458 ACPI_DEVICE_INFO *devinfo;
1467 if ((h = acpi_get_handle(dev)) == NULL)
1470 buf.Length = ACPI_ALLOCATE_BUFFER;
1471 error = AcpiGetObjectInfo(h, &buf);
1472 if (ACPI_FAILURE(error))
1474 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1476 /* If no _STA method, must be present */
1477 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1480 /* Return true for 'present' and 'functioning' */
1481 if ((devinfo->CurrentStatus & 0x9) == 0x9)
1484 AcpiOsFree(buf.Pointer);
1489 * Returns true if the battery is actually present and inserted.
1492 acpi_BatteryIsPresent(device_t dev)
1494 ACPI_DEVICE_INFO *devinfo;
1503 if ((h = acpi_get_handle(dev)) == NULL)
1506 buf.Length = ACPI_ALLOCATE_BUFFER;
1507 error = AcpiGetObjectInfo(h, &buf);
1508 if (ACPI_FAILURE(error))
1510 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1512 /* If no _STA method, must be present */
1513 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1516 /* Return true for 'present' and 'functioning' */
1517 if ((devinfo->CurrentStatus & 0x19) == 0x19)
1520 AcpiOsFree(buf.Pointer);
1525 * Match a HID string against a handle
1528 acpi_MatchHid(ACPI_HANDLE h, char *hid)
1530 ACPI_DEVICE_INFO *devinfo;
1538 if (hid == NULL || h == NULL)
1541 buf.Length = ACPI_ALLOCATE_BUFFER;
1542 error = AcpiGetObjectInfo(h, &buf);
1543 if (ACPI_FAILURE(error))
1545 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1547 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1548 strcmp(hid, devinfo->HardwareId.Value) == 0)
1550 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
1551 for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
1552 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
1559 AcpiOsFree(buf.Pointer);
1564 * Return the handle of a named object within our scope, ie. that of (parent)
1565 * or one if its parents.
1568 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1575 /* Walk back up the tree to the root */
1577 status = AcpiGetHandle(parent, path, &r);
1578 if (ACPI_SUCCESS(status)) {
1582 if (status != AE_NOT_FOUND)
1584 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1585 return (AE_NOT_FOUND);
1590 /* Find the difference between two PM tick counts. */
1592 acpi_TimerDelta(uint32_t end, uint32_t start)
1597 delta = end - start;
1598 else if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
1599 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1601 delta = ((0xFFFFFFFF - start) + end + 1);
1606 * Allocate a buffer with a preset data size.
1609 acpi_AllocBuffer(int size)
1613 buf = kmalloc(size + sizeof(*buf), M_ACPIDEV, M_INTWAIT);
1615 buf->Pointer = (void *)(buf + 1);
1620 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1623 ACPI_OBJECT_LIST args;
1627 arg1.Type = ACPI_TYPE_INTEGER;
1628 arg1.Integer.Value = number;
1630 args.Pointer = &arg1;
1632 return (AcpiEvaluateObject(handle, path, &args, NULL));
1636 * Evaluate a path that should return an integer.
1639 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1648 handle = ACPI_ROOT_OBJECT;
1651 * Assume that what we've been pointed at is an Integer object, or
1652 * a method that will return an Integer.
1654 buf.Pointer = ¶m;
1655 buf.Length = sizeof(param);
1656 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1657 if (ACPI_SUCCESS(status)) {
1658 if (param.Type == ACPI_TYPE_INTEGER)
1659 *number = param.Integer.Value;
1665 * In some applications, a method that's expected to return an Integer
1666 * may instead return a Buffer (probably to simplify some internal
1667 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1668 * convert it into an Integer as best we can.
1672 if (status == AE_BUFFER_OVERFLOW) {
1673 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1674 status = AE_NO_MEMORY;
1676 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1677 if (ACPI_SUCCESS(status))
1678 status = acpi_ConvertBufferToInteger(&buf, number);
1679 AcpiOsFree(buf.Pointer);
1686 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1692 p = (ACPI_OBJECT *)bufp->Pointer;
1693 if (p->Type == ACPI_TYPE_INTEGER) {
1694 *number = p->Integer.Value;
1697 if (p->Type != ACPI_TYPE_BUFFER)
1699 if (p->Buffer.Length > sizeof(int))
1700 return (AE_BAD_DATA);
1703 val = p->Buffer.Pointer;
1704 for (i = 0; i < p->Buffer.Length; i++)
1705 *number += val[i] << (i * 8);
1710 * Iterate over the elements of an a package object, calling the supplied
1711 * function for each element.
1713 * XXX possible enhancement might be to abort traversal on error.
1716 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1717 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1722 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1723 return (AE_BAD_PARAMETER);
1725 /* Iterate over components */
1727 comp = pkg->Package.Elements;
1728 for (; i < pkg->Package.Count; i++, comp++)
1735 * Find the (index)th resource object in a set.
1738 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1743 rp = (ACPI_RESOURCE *)buf->Pointer;
1747 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1748 return (AE_BAD_PARAMETER);
1750 /* Check for terminator */
1751 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1752 return (AE_NOT_FOUND);
1753 rp = ACPI_NEXT_RESOURCE(rp);
1762 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1764 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1765 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1766 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1769 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1772 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1777 /* Initialise the buffer if necessary. */
1778 if (buf->Pointer == NULL) {
1779 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1780 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1781 return (AE_NO_MEMORY);
1782 rp = (ACPI_RESOURCE *)buf->Pointer;
1783 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1790 * Scan the current buffer looking for the terminator.
1791 * This will either find the terminator or hit the end
1792 * of the buffer and return an error.
1794 rp = (ACPI_RESOURCE *)buf->Pointer;
1796 /* Range check, don't go outside the buffer */
1797 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1798 return (AE_BAD_PARAMETER);
1799 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1801 rp = ACPI_NEXT_RESOURCE(rp);
1805 * Check the size of the buffer and expand if required.
1808 * size of existing resources before terminator +
1809 * size of new resource and header +
1810 * size of terminator.
1812 * Note that this loop should really only run once, unless
1813 * for some reason we are stuffing a *really* huge resource.
1815 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1816 res->Length + ACPI_RS_SIZE_NO_DATA +
1817 ACPI_RS_SIZE_MIN) >= buf->Length) {
1818 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1819 return (AE_NO_MEMORY);
1820 bcopy(buf->Pointer, newp, buf->Length);
1821 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1822 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1823 AcpiOsFree(buf->Pointer);
1824 buf->Pointer = newp;
1825 buf->Length += buf->Length;
1828 /* Insert the new resource. */
1829 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
1831 /* And add the terminator. */
1832 rp = ACPI_NEXT_RESOURCE(rp);
1833 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1840 * Set interrupt model.
1843 acpi_SetIntrModel(int model)
1845 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1848 #define ACPI_MINIMUM_AWAKETIME 5
1851 acpi_sleep_enable(void *arg)
1853 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1857 * Set the system sleep state
1859 * Currently we support S1-S5 but S4 is only S4BIOS
1862 acpi_SetSleepState(struct acpi_softc *sc, int state)
1864 ACPI_STATUS status = AE_OK;
1868 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1871 /* Avoid reentry if already attempting to suspend. */
1872 if (sc->acpi_sstate != ACPI_STATE_S0)
1873 return_ACPI_STATUS (AE_BAD_PARAMETER);
1875 /* We recently woke up so don't suspend again for a while. */
1876 if (sc->acpi_sleep_disabled)
1877 return_ACPI_STATUS (AE_OK);
1884 status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
1885 if (status == AE_NOT_FOUND) {
1886 device_printf(sc->acpi_dev,
1887 "Sleep state S%d not supported by BIOS\n", state);
1889 } else if (ACPI_FAILURE(status)) {
1890 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1891 AcpiFormatException(status));
1895 sc->acpi_sstate = state;
1896 sc->acpi_sleep_disabled = 1;
1898 /* Disable all wake GPEs not appropriate for this state. */
1899 acpi_wake_limit_walk(state);
1901 /* Inform all devices that we are going to sleep. */
1902 if (DEVICE_SUSPEND(root_bus) != 0) {
1904 * Re-wake the system.
1906 * XXX note that a better two-pass approach with a 'veto' pass
1907 * followed by a "real thing" pass would be better, but the
1908 * current bus interface does not provide for this.
1910 DEVICE_RESUME(root_bus);
1911 return_ACPI_STATUS (AE_ERROR);
1914 status = AcpiEnterSleepStatePrep(state);
1915 if (ACPI_FAILURE(status)) {
1916 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1917 AcpiFormatException(status));
1921 if (sc->acpi_sleep_delay > 0)
1922 DELAY(sc->acpi_sleep_delay * 1000000);
1924 if (state != ACPI_STATE_S1) {
1925 acpi_sleep_machdep(sc, state);
1927 /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
1928 AcpiOsReleaseLock(AcpiGbl_HardwareLock, 1);
1930 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1931 if (state == ACPI_STATE_S4)
1934 status = AcpiEnterSleepState((UINT8)state);
1935 if (ACPI_FAILURE(status)) {
1936 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1937 AcpiFormatException(status));
1941 AcpiLeaveSleepState((UINT8)state);
1942 DEVICE_RESUME(root_bus);
1943 sc->acpi_sstate = ACPI_STATE_S0;
1944 acpi_enable_fixed_events(sc);
1948 * Shut down cleanly and power off. This will call us back through the
1949 * shutdown handlers.
1951 shutdown_nice(RB_POWEROFF);
1955 status = AE_BAD_PARAMETER;
1959 /* Disable a second sleep request for a short period */
1960 if (sc->acpi_sleep_disabled)
1961 callout_reset(&sc->acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
1962 acpi_sleep_enable, sc);
1964 return_ACPI_STATUS (status);
1967 /* Initialize a device's wake GPE. */
1969 acpi_wake_init(device_t dev, int type)
1971 struct acpi_prw_data prw;
1973 /* Check that the device can wake the system. */
1974 if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0)
1977 /* Evaluate _PRW to find the GPE. */
1978 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
1981 /* Set the requested type for the GPE (runtime, wake, or both). */
1982 if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
1983 device_printf(dev, "set GPE type failed\n");
1990 /* Enable or disable the device's wake GPE. */
1992 acpi_wake_set_enable(device_t dev, int enable)
1994 struct acpi_prw_data prw;
1999 /* Make sure the device supports waking the system. */
2000 flags = device_get_flags(dev);
2001 handle = acpi_get_handle(dev);
2002 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
2005 /* Evaluate _PRW to find the GPE. */
2006 if (acpi_parse_prw(handle, &prw) != 0)
2010 status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2011 if (ACPI_FAILURE(status)) {
2012 device_printf(dev, "enable wake failed\n");
2015 device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2017 status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2018 if (ACPI_FAILURE(status)) {
2019 device_printf(dev, "disable wake failed\n");
2022 device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2028 /* Configure a device's GPE appropriately for the new sleep state. */
2030 acpi_wake_sleep_prep(device_t dev, int sstate)
2032 struct acpi_prw_data prw;
2036 /* Check that this is an ACPI device and get its GPE. */
2037 flags = device_get_flags(dev);
2038 handle = acpi_get_handle(dev);
2039 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
2042 /* Evaluate _PRW to find the GPE. */
2043 if (acpi_parse_prw(handle, &prw) != 0)
2047 * TBD: All Power Resources referenced by elements 2 through N
2048 * of the _PRW object are put into the ON state.
2052 * If the user requested that this device wake the system and the next
2053 * sleep state is valid for this GPE, enable it and the device's wake
2054 * capability. The sleep state must be less than (i.e., higher power)
2055 * or equal to the value specified by _PRW. Return early, leaving
2056 * the appropriate power resources enabled.
2058 if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 &&
2059 sstate <= prw.lowest_wake) {
2061 device_printf(dev, "wake_prep enabled gpe %#x for state %d\n",
2062 prw.gpe_bit, sstate);
2063 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2064 acpi_SetInteger(handle, "_PSW", 1);
2069 * If the device wake was disabled or this sleep state is too low for
2070 * this device, disable its wake capability and GPE.
2072 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2073 acpi_SetInteger(handle, "_PSW", 0);
2075 device_printf(dev, "wake_prep disabled gpe %#x for state %d\n",
2076 prw.gpe_bit, sstate);
2079 * TBD: All Power Resources referenced by elements 2 through N
2080 * of the _PRW object are put into the OFF state.
2086 /* Re-enable GPEs after wake. */
2088 acpi_wake_run_prep(device_t dev)
2090 struct acpi_prw_data prw;
2094 /* Check that this is an ACPI device and get its GPE. */
2095 flags = device_get_flags(dev);
2096 handle = acpi_get_handle(dev);
2097 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
2100 /* Evaluate _PRW to find the GPE. */
2101 if (acpi_parse_prw(handle, &prw) != 0)
2105 * TBD: Be sure all Power Resources referenced by elements 2 through N
2106 * of the _PRW object are in the ON state.
2109 /* Disable wake capability and if the user requested, enable the GPE. */
2110 acpi_SetInteger(handle, "_PSW", 0);
2111 if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0)
2112 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2117 acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status)
2119 struct acpi_prw_data prw;
2122 /* It's ok not to have _PRW if the device can't wake the system. */
2123 if (acpi_parse_prw(h, &prw) != 0)
2126 sstate = (int *)context;
2127 if (*sstate > prw.lowest_wake)
2128 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2133 /* Walk all system devices, disabling them if necessary for sstate. */
2135 acpi_wake_limit_walk(int sstate)
2137 ACPI_HANDLE sb_handle;
2139 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2140 AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100,
2141 acpi_wake_limit, &sstate, NULL);
2145 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
2147 acpi_wake_sysctl_walk(device_t dev)
2149 int error, i, numdevs;
2153 error = device_get_children(dev, &devlist, &numdevs);
2154 if (error != 0 || numdevs == 0)
2156 for (i = 0; i < numdevs; i++) {
2158 if (!device_is_attached(child))
2160 if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) {
2162 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
2163 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
2164 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
2165 acpi_wake_set_sysctl, "I", "Device set to wake the system");
2166 #endif /* dfly_notyet */
2168 acpi_wake_sysctl_walk(child);
2170 kfree(devlist, M_TEMP);
2176 /* Enable or disable wake from userland. */
2178 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
2183 dev = (device_t)arg1;
2184 enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
2186 error = sysctl_handle_int(oidp, &enable, 0, req);
2187 if (error != 0 || req->newptr == NULL)
2189 if (enable != 0 && enable != 1)
2192 return (acpi_wake_set_enable(dev, enable));
2194 #endif /* dfly_notyet */
2196 /* Parse a device's _PRW into a structure. */
2198 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2201 ACPI_BUFFER prw_buffer;
2202 ACPI_OBJECT *res, *res2;
2205 if (h == NULL || prw == NULL)
2209 * The _PRW object (7.2.9) is only required for devices that have the
2210 * ability to wake the system from a sleeping state.
2213 prw_buffer.Pointer = NULL;
2214 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2215 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2216 if (ACPI_FAILURE(status))
2218 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2221 if (!ACPI_PKG_VALID(res, 2))
2225 * Element 1 of the _PRW object:
2226 * The lowest power system sleeping state that can be entered while still
2227 * providing wake functionality. The sleeping state being entered must
2228 * be less than (i.e., higher power) or equal to this value.
2230 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2234 * Element 0 of the _PRW object:
2236 switch (res->Package.Elements[0].Type) {
2237 case ACPI_TYPE_INTEGER:
2239 * If the data type of this package element is numeric, then this
2240 * _PRW package element is the bit index in the GPEx_EN, in the
2241 * GPE blocks described in the FADT, of the enable bit that is
2242 * enabled for the wake event.
2244 prw->gpe_handle = NULL;
2245 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2248 case ACPI_TYPE_PACKAGE:
2250 * If the data type of this package element is a package, then this
2251 * _PRW package element is itself a package containing two
2252 * elements. The first is an object reference to the GPE Block
2253 * device that contains the GPE that will be triggered by the wake
2254 * event. The second element is numeric and it contains the bit
2255 * index in the GPEx_EN, in the GPE Block referenced by the
2256 * first element in the package, of the enable bit that is enabled for
2259 * For example, if this field is a package then it is of the form:
2260 * Package() {\_SB.PCI0.ISA.GPE, 2}
2262 res2 = &res->Package.Elements[0];
2263 if (!ACPI_PKG_VALID(res2, 2))
2265 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2266 if (prw->gpe_handle == NULL)
2268 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2276 /* XXX No power resource handling yet. */
2277 prw->power_res = NULL;
2280 if (prw_buffer.Pointer != NULL)
2281 AcpiOsFree(prw_buffer.Pointer);
2286 * Enable/Disable ACPI
2289 acpi_Enable(struct acpi_softc *sc)
2294 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2297 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
2298 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
2299 if (!sc->acpi_enabled)
2300 status = AcpiEnableSubsystem(flags);
2304 if (status == AE_OK)
2305 sc->acpi_enabled = 1;
2307 return_ACPI_STATUS (status);
2311 acpi_Disable(struct acpi_softc *sc)
2315 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2318 if (sc->acpi_enabled)
2319 status = AcpiDisable();
2323 if (status == AE_OK)
2324 sc->acpi_enabled = 0;
2326 return_ACPI_STATUS (status);
2330 * ACPI Event Handlers
2333 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
2336 acpi_system_eventhandler_sleep(void *arg, int state)
2339 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2342 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2343 acpi_SetSleepState((struct acpi_softc *)arg, state);
2349 acpi_system_eventhandler_wakeup(void *arg, int state)
2352 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2354 /* Well, what to do? :-) */
2363 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
2366 acpi_event_power_button_sleep(void *context)
2368 struct acpi_softc *sc = (struct acpi_softc *)context;
2370 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2372 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
2374 return_VALUE (ACPI_INTERRUPT_HANDLED);
2378 acpi_event_power_button_wake(void *context)
2380 struct acpi_softc *sc = (struct acpi_softc *)context;
2382 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2384 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
2386 return_VALUE (ACPI_INTERRUPT_HANDLED);
2390 acpi_event_sleep_button_sleep(void *context)
2392 struct acpi_softc *sc = (struct acpi_softc *)context;
2394 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2396 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
2398 return_VALUE (ACPI_INTERRUPT_HANDLED);
2402 acpi_event_sleep_button_wake(void *context)
2404 struct acpi_softc *sc = (struct acpi_softc *)context;
2406 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2408 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
2410 return_VALUE (ACPI_INTERRUPT_HANDLED);
2414 * XXX This is kinda ugly, and should not be here.
2416 struct acpi_staticbuf {
2422 acpi_name(ACPI_HANDLE handle)
2424 static struct acpi_staticbuf buf;
2428 buf.buffer.Length = 512;
2429 buf.buffer.Pointer = &buf.data[0];
2431 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
2432 return (buf.buffer.Pointer);
2434 return ("(unknown path)");
2438 * Debugging/bug-avoidance. Avoid trying to fetch info on various
2439 * parts of the namespace.
2442 acpi_avoid(ACPI_HANDLE handle)
2444 char *cp, *env, *np;
2447 np = acpi_name(handle);
2450 if ((env = kgetenv("debug.acpi.avoid.paths")) == NULL &&
2451 (env = kgetenv("debug.acpi.avoid")) == NULL)
2454 /* Scan the avoid list checking for a match */
2457 while ((*cp != 0) && isspace(*cp))
2462 while ((cp[len] != 0) && !isspace(cp[len]))
2464 if (!strncmp(cp, np, len)) {
2476 * Debugging/bug-avoidance. Disable ACPI subsystem components. Note that
2477 * some components may be disabled by default and can only be enabled
2478 * via acpi_enabled() (debug.acpi.enabled).
2481 acpi_disabled(char *subsys)
2486 if ((env = kgetenv("debug.acpi.disabled")) == NULL)
2488 if (strcmp(env, "all") == 0) {
2493 /* Scan the disable list, checking for a match. */
2496 while (*cp != '\0' && isspace(*cp))
2501 while (cp[len] != '\0' && !isspace(cp[len]))
2503 if (strncmp(cp, subsys, len) == 0) {
2515 * Debugging/bug-avoidance. Enable ACPI subsystem components. Most
2516 * components are enabled by default. The ones that are not have to be
2517 * enabled via debug.acpi.enabled.
2520 acpi_enabled(char *subsys)
2525 if ((env = kgetenv("debug.acpi.enabled")) == NULL)
2527 if (strcmp(env, "all") == 0) {
2532 /* Scan the enable list, checking for a match. */
2535 while (*cp != '\0' && isspace(*cp))
2540 while (cp[len] != '\0' && !isspace(cp[len]))
2542 if (strncmp(cp, subsys, len) == 0) {
2554 * Control interface.
2556 * We multiplex ioctls for all participating ACPI devices here. Individual
2557 * drivers wanting to be accessible via /dev/acpi should use the
2558 * register/deregister interface to make their handlers visible.
2560 struct acpi_ioctl_hook
2562 TAILQ_ENTRY(acpi_ioctl_hook) link;
2568 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
2569 static int acpi_ioctl_hooks_initted;
2572 * Register an ioctl handler.
2575 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
2577 struct acpi_ioctl_hook *hp;
2579 hp = kmalloc(sizeof(*hp), M_ACPIDEV, M_INTWAIT);
2583 if (acpi_ioctl_hooks_initted == 0) {
2584 TAILQ_INIT(&acpi_ioctl_hooks);
2585 acpi_ioctl_hooks_initted = 1;
2587 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
2592 * Deregister an ioctl handler.
2595 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
2597 struct acpi_ioctl_hook *hp;
2599 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
2600 if ((hp->cmd == cmd) && (hp->fn == fn))
2604 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
2605 kfree(hp, M_ACPIDEV);
2610 acpiopen(struct dev_open_args *ap)
2616 acpiclose(struct dev_close_args *ap)
2622 acpiioctl(struct dev_ioctl_args *ap)
2624 struct acpi_softc *sc;
2625 struct acpi_ioctl_hook *hp;
2626 int error, xerror, state;
2632 sc = ap->a_head.a_dev->si_drv1;
2635 * Scan the list of registered ioctls, looking for handlers.
2637 if (acpi_ioctl_hooks_initted) {
2638 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2639 if (hp->cmd == ap->a_cmd) {
2640 xerror = hp->fn(ap->a_cmd, ap->a_data, hp->arg);
2649 * Core ioctls are not permitted for non-writable user.
2650 * Currently, other ioctls just fetch information.
2651 * Not changing system behavior.
2653 if((ap->a_fflag & FWRITE) == 0) {
2658 /* Core system ioctls. */
2659 switch (ap->a_cmd) {
2661 if (ACPI_FAILURE(acpi_Enable(sc)))
2664 case ACPIIO_DISABLE:
2665 if (ACPI_FAILURE(acpi_Disable(sc)))
2668 case ACPIIO_SETSLPSTATE:
2669 if (!sc->acpi_enabled) {
2673 state = *(int *)ap->a_data;
2674 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
2675 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
2693 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2695 char sleep_state[4];
2698 UINT8 state, TypeA, TypeB;
2701 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) {
2702 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2703 ksprintf(sleep_state, "S%d ", state);
2704 strcat(buf, sleep_state);
2707 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2712 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2714 char sleep_state[10];
2716 u_int new_state, old_state;
2718 old_state = *(u_int *)oidp->oid_arg1;
2719 if (old_state > ACPI_S_STATES_MAX + 1) {
2720 strcpy(sleep_state, "unknown");
2722 bzero(sleep_state, sizeof(sleep_state));
2723 strncpy(sleep_state, sleep_state_names[old_state],
2724 sizeof(sleep_state_names[old_state]));
2726 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
2727 if (error == 0 && req->newptr != NULL) {
2728 new_state = ACPI_STATE_S0;
2729 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
2730 if (strncmp(sleep_state, sleep_state_names[new_state],
2731 sizeof(sleep_state)) == 0)
2734 if (new_state <= ACPI_S_STATES_MAX + 1) {
2735 if (new_state != old_state)
2736 *(u_int *)oidp->oid_arg1 = new_state;
2745 /* Inform devctl(4) when we receive a Notify. */
2747 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
2749 char notify_buf[16];
2750 ACPI_BUFFER handle_buf;
2753 if (subsystem == NULL)
2756 handle_buf.Pointer = NULL;
2757 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
2758 status = AcpiNsHandleToPathname(h, &handle_buf);
2759 if (ACPI_FAILURE(status))
2761 ksnprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
2762 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
2763 AcpiOsFree(handle_buf.Pointer);
2768 * Support for parsing debug options from the kernel environment.
2770 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
2771 * by specifying the names of the bits in the debug.acpi.layer and
2772 * debug.acpi.level environment variables. Bits may be unset by
2773 * prefixing the bit name with !.
2781 static struct debugtag dbg_layer[] = {
2782 {"ACPI_UTILITIES", ACPI_UTILITIES},
2783 {"ACPI_HARDWARE", ACPI_HARDWARE},
2784 {"ACPI_EVENTS", ACPI_EVENTS},
2785 {"ACPI_TABLES", ACPI_TABLES},
2786 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
2787 {"ACPI_PARSER", ACPI_PARSER},
2788 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
2789 {"ACPI_EXECUTER", ACPI_EXECUTER},
2790 {"ACPI_RESOURCES", ACPI_RESOURCES},
2791 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
2792 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
2793 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
2794 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
2796 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
2797 {"ACPI_BATTERY", ACPI_BATTERY},
2798 {"ACPI_BUS", ACPI_BUS},
2799 {"ACPI_BUTTON", ACPI_BUTTON},
2800 {"ACPI_EC", ACPI_EC},
2801 {"ACPI_FAN", ACPI_FAN},
2802 {"ACPI_POWERRES", ACPI_POWERRES},
2803 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
2804 {"ACPI_THERMAL", ACPI_THERMAL},
2805 {"ACPI_TIMER", ACPI_TIMER},
2806 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
2810 static struct debugtag dbg_level[] = {
2811 {"ACPI_LV_INIT", ACPI_LV_INIT},
2812 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
2813 {"ACPI_LV_INFO", ACPI_LV_INFO},
2814 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
2816 /* Trace verbosity level 1 [Standard Trace Level] */
2817 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
2818 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
2819 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
2820 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
2821 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
2822 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
2823 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
2824 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
2825 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
2826 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
2827 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
2828 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
2829 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
2830 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
2831 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
2833 /* Trace verbosity level 2 [Function tracing and memory allocation] */
2834 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
2835 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
2836 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
2837 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
2838 {"ACPI_LV_ALL", ACPI_LV_ALL},
2840 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2841 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
2842 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
2843 {"ACPI_LV_IO", ACPI_LV_IO},
2844 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
2845 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
2847 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
2848 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
2849 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
2850 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
2851 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
2852 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
2857 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2869 while (*ep && !isspace(*ep))
2880 for (i = 0; tag[i].name != NULL; i++) {
2881 if (!strncmp(cp, tag[i].name, l)) {
2883 *flag |= tag[i].value;
2885 *flag &= ~tag[i].value;
2893 * Warning: also called in early boot, before any allocators
2897 acpi_set_debugging(void *junk)
2899 char *layer, *level;
2906 layer = kgetenv("debug.acpi.layer");
2907 level = kgetenv("debug.acpi.level");
2908 if (layer == NULL && level == NULL)
2911 kprintf("ACPI set debug");
2912 if (layer != NULL) {
2913 if (strcmp("NONE", layer) != 0)
2914 kprintf(" layer '%s'", layer);
2915 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
2918 if (level != NULL) {
2919 if (strcmp("NONE", level) != 0)
2920 kprintf(" level '%s'", level);
2921 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
2926 SYSINIT(acpi_debugging, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
2927 acpi_set_debugging, NULL);
2930 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
2933 struct debugtag *tag;
2936 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
2938 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
2939 tag = &dbg_layer[0];
2940 dbg = &AcpiDbgLayer;
2942 tag = &dbg_level[0];
2943 dbg = &AcpiDbgLevel;
2946 /* Get old values if this is a get request. */
2948 sbuf_cpy(&sb, "NONE");
2949 } else if (req->newptr == NULL) {
2950 for (; tag->name != NULL; tag++) {
2951 if ((*dbg & tag->value) == tag->value)
2952 sbuf_printf(&sb, "%s ", tag->name);
2958 /* Copy out the old values to the user. */
2959 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
2962 /* If the user is setting a string, parse it. */
2963 if (error == 0 && req->newptr != NULL) {
2965 ksetenv(oidp->oid_arg1, req->newptr);
2966 acpi_set_debugging(NULL);
2971 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
2972 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
2973 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
2974 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
2978 acpi_pm_func(u_long cmd, void *arg, ...)
2980 int state, acpi_state;
2982 struct acpi_softc *sc;
2987 case POWER_CMD_SUSPEND:
2988 sc = (struct acpi_softc *)arg;
2995 state = va_arg(ap, int);
2999 case POWER_SLEEP_STATE_STANDBY:
3000 acpi_state = sc->acpi_standby_sx;
3002 case POWER_SLEEP_STATE_SUSPEND:
3003 acpi_state = sc->acpi_suspend_sx;
3005 case POWER_SLEEP_STATE_HIBERNATE:
3006 acpi_state = ACPI_STATE_S4;
3013 acpi_SetSleepState(sc, acpi_state);
3025 acpi_pm_register(void *arg)
3027 if (!cold || resource_disabled("acpi", 0))
3030 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3033 SYSINIT(power, SI_BOOT2_KLD, SI_ORDER_ANY, acpi_pm_register, 0);