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.157 2004/06/05 09:56:04 njl Exp $
30 * $DragonFly: src/sys/dev/acpica5/acpi.c,v 1.19 2005/12/11 01:54:07 swildner Exp $
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/fcntl.h>
38 #include <sys/malloc.h>
41 #include <sys/ioccom.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>
48 #include <sys/thread2.h>
50 #include <machine/clock.h>
51 #include <machine/globaldata.h>
52 #include <machine/resource.h>
53 #include <machine/bus.h>
55 #include <bus/isa/isavar.h>
58 #include <dev/acpica5/acpivar.h>
59 #include <dev/acpica5/acpiio.h>
62 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
64 /* Hooks for the ACPI CA debugging infrastructure */
65 #define _COMPONENT ACPI_BUS
66 ACPI_MODULE_NAME("ACPI")
68 static d_open_t acpiopen;
69 static d_close_t acpiclose;
70 static d_ioctl_t acpiioctl;
72 #define CDEV_MAJOR 152
73 static struct cdevsw acpi_cdevsw = {
80 .old_close = acpiclose,
81 .old_ioctl = acpiioctl
84 #if __FreeBSD_version >= 500000
85 struct mtx acpi_mutex;
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" },
105 static int acpi_modevent(struct module *mod, int event, void *junk);
106 static int acpi_identify(driver_t *driver, device_t parent);
107 static int acpi_probe(device_t dev);
108 static int acpi_attach(device_t dev);
109 static int acpi_shutdown(device_t dev);
110 static void acpi_quirks_set(void);
111 static device_t acpi_add_child(device_t bus, device_t parent, int order,
112 const char *name, int unit);
113 static int acpi_print_child(device_t bus, device_t child);
114 static int acpi_read_ivar(device_t dev, device_t child, int index,
116 static int acpi_write_ivar(device_t dev, device_t child, int index,
118 static int acpi_set_resource(device_t dev, device_t child, int type,
119 int rid, u_long start, u_long count);
120 static int acpi_get_resource(device_t dev, device_t child, int type,
121 int rid, u_long *startp, u_long *countp);
122 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
123 int type, int *rid, u_long start, u_long end,
124 u_long count, u_int flags);
125 static int acpi_release_resource(device_t bus, device_t child, int type,
126 int rid, struct resource *r);
127 static uint32_t acpi_isa_get_logicalid(device_t dev);
128 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
129 static int acpi_isa_pnp_probe(device_t bus, device_t child,
130 struct isa_pnp_id *ids);
131 static void acpi_probe_children(device_t bus);
132 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
133 void *context, void **status);
134 static void acpi_shutdown_pre_sync(void *arg, int howto);
135 static void acpi_shutdown_final(void *arg, int howto);
136 static void acpi_shutdown_poweroff(void *arg);
137 static void acpi_enable_fixed_events(struct acpi_softc *sc);
138 static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw);
139 static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context,
141 static int acpi_wake_limit_walk(int sstate);
142 static int acpi_wake_sysctl_walk(device_t dev);
144 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);
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, bus_generic_suspend),
164 DEVMETHOD(device_resume, bus_generic_resume),
167 DEVMETHOD(bus_add_child, acpi_add_child),
168 DEVMETHOD(bus_print_child, acpi_print_child),
169 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
170 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
171 DEVMETHOD(bus_set_resource, acpi_set_resource),
172 DEVMETHOD(bus_get_resource, acpi_get_resource),
173 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
174 DEVMETHOD(bus_release_resource, acpi_release_resource),
175 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
176 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
177 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
178 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
179 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
180 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
181 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
184 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
189 static driver_t acpi_driver = {
192 sizeof(struct acpi_softc),
195 static devclass_t acpi_devclass;
196 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
197 MODULE_VERSION(acpi, 1);
199 static const char* sleep_state_names[] = {
200 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
202 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
203 static char acpi_ca_version[12];
204 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
205 acpi_ca_version, 0, "Version of Intel ACPI-CA");
208 * Allow override of whether methods execute in parallel or not.
209 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
210 * errors for AML that really can't handle parallel method execution.
211 * It is off by default since this breaks recursive methods and
212 * some IBMs use such code.
214 static int acpi_serialize_methods;
215 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
218 * ACPI can only be loaded as a module by the loader; activating it after
219 * system bootstrap time is not useful, and can be fatal to the system.
220 * It also cannot be unloaded, since the entire system bus heirarchy hangs
224 acpi_modevent(struct module *mod, int event, void *junk)
229 printf("The ACPI driver cannot be loaded after boot.\n");
234 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
244 * Perform early initialization.
252 static int error, started = 0;
254 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
257 return_VALUE (error);
260 #if __FreeBSD_version >= 500000
261 /* Initialise the ACPI mutex */
262 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
266 * Set the globals from our tunables. This is needed because ACPI-CA
267 * uses UINT8 for some values and we have no tunable_byte.
269 AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods;
271 /* Start up the ACPI CA subsystem. */
273 debugpoint = getenv("debug.acpi.debugger");
275 if (!strcmp(debugpoint, "init"))
276 acpi_EnterDebugger();
280 if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
281 printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
282 return_VALUE (error);
285 debugpoint = getenv("debug.acpi.debugger");
287 if (!strcmp(debugpoint, "tables"))
288 acpi_EnterDebugger();
293 if (ACPI_FAILURE(error = AcpiLoadTables())) {
294 printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
298 /* Set up any quirks we have for this XSDT. */
300 if (acpi_disabled("acpi"))
301 return_VALUE (AE_ERROR);
303 return_VALUE (AE_OK);
307 * Detect ACPI, perform early initialisation
310 acpi_identify(driver_t *driver, device_t parent)
315 * No sense rescanning an ACPI 'bus'.
317 if (device_get_state(parent) == DS_ATTACHED)
320 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
325 /* Check that we haven't been disabled with a hint. */
326 if (resource_disabled("acpi", 0))
329 /* Make sure we're not being doubly invoked. */
330 if (device_find_child(parent, "acpi", 0) != NULL)
333 /* Initialize ACPI-CA. */
334 if (ACPI_FAILURE(acpi_Startup()))
337 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
339 /* Attach the actual ACPI device. */
340 if ((child = BUS_ADD_CHILD(parent, parent, 0, "acpi", 0)) == NULL) {
341 device_printf(parent, "ACPI: could not attach\n");
348 * Fetch some descriptive data from ACPI to put in our attach message
351 acpi_probe(device_t dev)
353 ACPI_TABLE_HEADER th;
360 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
362 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
363 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
365 device_printf(dev, "Other PM system enabled.\n");
371 if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
372 device_printf(dev, "couldn't get XSDT header: %s\n",
373 AcpiFormatException(status));
376 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
377 sbuf_bcat(&sb, th.OemId, 6);
380 sbuf_bcat(&sb, th.OemTableId, 8);
383 device_set_desc_copy(dev, sbuf_data(&sb));
392 acpi_attach(device_t dev)
394 struct acpi_softc *sc;
405 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
407 sc = device_get_softc(dev);
408 bzero(sc, sizeof(*sc));
410 callout_init(&sc->acpi_sleep_timer);
413 debugpoint = getenv("debug.acpi.debugger");
415 if (!strcmp(debugpoint, "spaces"))
416 acpi_EnterDebugger();
421 /* Install the default address space handlers. */
423 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
424 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
425 if (ACPI_FAILURE(status)) {
426 device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
427 AcpiFormatException(status));
430 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
431 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
432 if (ACPI_FAILURE(status)) {
433 device_printf(dev, "Could not initialise SystemIO handler: %s\n",
434 AcpiFormatException(status));
437 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
438 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
439 if (ACPI_FAILURE(status)) {
440 device_printf(dev, "could not initialise PciConfig handler: %s\n",
441 AcpiFormatException(status));
446 * Bring ACPI fully online.
448 * Note that some systems (specifically, those with namespace evaluation
449 * issues that require the avoidance of parts of the namespace) must
450 * avoid running _INI and _STA on everything, as well as dodging the final
453 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
454 * For avoiding portions of the namespace without totally disabling _INI
455 * and _STA, use "debug.acpi.avoid.paths".
457 * XXX We should arrange for the object init pass after we have attached
458 * all our child devices, but on many systems it works here.
461 debugpoint = getenv("debug.acpi.debugger");
463 if (!strcmp(debugpoint, "enable"))
464 acpi_EnterDebugger();
469 if (testenv("debug.acpi.avoid"))
470 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
471 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
472 device_printf(dev, "Could not enable ACPI: %s\n",
473 AcpiFormatException(status));
478 * Call the ECDT probe function to provide EC functionality before
479 * the namespace has been evaluated.
481 acpi_ec_ecdt_probe(dev);
483 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
484 device_printf(dev, "Could not initialize ACPI objects: %s\n",
485 AcpiFormatException(status));
490 * Setup our sysctl tree.
492 * XXX: This doesn't check to make sure that none of these fail.
494 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
495 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
496 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
497 device_get_name(dev), CTLFLAG_RD, 0, "");
498 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
499 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
500 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
501 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
502 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
503 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
504 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
505 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
506 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
507 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
508 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
509 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
510 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
511 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
512 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
513 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
514 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
515 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
516 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
517 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
518 &sc->acpi_sleep_delay, 0, "sleep delay");
519 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
520 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
521 &sc->acpi_s4bios, 0, "S4BIOS mode");
522 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
523 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
524 &sc->acpi_verbose, 0, "verbose mode");
525 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
526 OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
527 &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
530 * Default to 1 second before sleeping to give some machines time to
533 sc->acpi_sleep_delay = 1;
534 sc->acpi_disable_on_poweroff = 0;
536 sc->acpi_verbose = 1;
537 if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
538 sc->acpi_verbose = 1;
542 /* Only enable S4BIOS by default if the FACS says it is available. */
543 if (AcpiGbl_FACS->S4Bios_f != 0)
547 * Dispatch the default sleep state to devices. The lid switch is set
548 * to NONE by default to avoid surprising users.
550 sc->acpi_power_button_sx = ACPI_STATE_S5;
551 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
552 sc->acpi_standby_sx = ACPI_STATE_S1;
553 sc->acpi_suspend_sx = ACPI_STATE_S3;
555 /* Pick the first valid sleep state for the sleep button default. */
556 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
557 for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
558 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
559 sc->acpi_sleep_button_sx = state;
563 acpi_enable_fixed_events(sc);
566 * Scan the namespace and attach/initialise children.
569 debugpoint = getenv("debug.acpi.debugger");
571 if (!strcmp(debugpoint, "probe"))
572 acpi_EnterDebugger();
577 /* Register our shutdown handlers */
578 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
580 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
584 * Register our acpi event handlers.
585 * XXX should be configurable eg. via userland policy manager.
587 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
588 sc, ACPI_EVENT_PRI_LAST);
589 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
590 sc, ACPI_EVENT_PRI_LAST);
592 /* Flag our initial states. */
593 sc->acpi_enabled = 1;
594 sc->acpi_sstate = ACPI_STATE_S0;
595 sc->acpi_sleep_disabled = 0;
597 /* Create the control device */
598 cdevsw_add(&acpi_cdevsw, 0, 0);
599 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
601 sc->acpi_dev_t->si_drv1 = sc;
604 debugpoint = getenv("debug.acpi.debugger");
606 if (strcmp(debugpoint, "running") == 0)
607 acpi_EnterDebugger();
612 if ((error = acpi_task_thread_init()))
615 if ((error = acpi_machdep_init(dev)))
618 /* Register ACPI again to pass the correct argument of pm_func. */
619 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
621 if (!acpi_disabled("bus"))
622 acpi_probe_children(dev);
628 return_VALUE (error);
632 acpi_shutdown(device_t dev)
634 /* Allow children to shutdown first. */
635 bus_generic_shutdown(dev);
637 /* Disable all wake GPEs not appropriate for reboot/poweroff. */
638 acpi_wake_limit_walk(ACPI_STATE_S5);
643 acpi_quirks_set(void)
645 XSDT_DESCRIPTOR *xsdt;
646 struct acpi_quirks *quirk;
651 * If the user loaded a custom table or disabled "quirks", leave
652 * the settings alone.
655 if ((env = getenv("acpi_dsdt_load")) != NULL) {
656 /* XXX No strcasecmp but this is good enough. */
657 if (*env == 'Y' || *env == 'y')
661 if ((env = getenv("debug.acpi.disabled")) != NULL) {
662 if (strstr("quirks", env) != NULL)
668 * Search through our quirk table and concatenate the disabled
669 * values with whatever we find.
672 for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
673 if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
674 (xsdt->OemRevision == quirk->OemRevision ||
675 quirk->OemRevision == ACPI_OEM_REV_ANY)) {
676 len += strlen(quirk->value) + 2;
677 if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
679 sprintf(tmp, "%s %s", env ? env : "", quirk->value);
681 setenv("debug.acpi.disabled", tmp);
694 * Handle a new device being added
697 acpi_add_child(device_t bus, device_t parent, int order,
698 const char *name, int unit)
700 struct acpi_device *ad;
703 ad = malloc(sizeof(*ad), M_ACPIDEV, M_INTWAIT | M_ZERO);
705 resource_list_init(&ad->ad_rl);
707 child = device_add_child_ordered(parent, order, name, unit);
709 device_set_ivars(child, ad);
714 acpi_print_child(device_t bus, device_t child)
716 struct acpi_device *adev = device_get_ivars(child);
717 struct resource_list *rl = &adev->ad_rl;
720 retval += bus_print_child_header(bus, child);
721 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
722 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
723 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
724 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
725 retval += bus_print_child_footer(bus, child);
730 /* Location hint for devctl(8) */
732 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
735 struct acpi_device *dinfo = device_get_ivars(child);
737 if (dinfo->ad_handle)
738 snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
740 snprintf(buf, buflen, "magic=unknown");
744 /* PnP information for devctl(8) */
746 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
749 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
750 ACPI_DEVICE_INFO *adinfo;
751 struct acpi_device *dinfo = device_get_ivars(child);
755 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
756 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
759 snprintf(buf, buflen, "Unknown");
761 snprintf(buf, buflen, "_HID=%s _UID=%lu",
762 (adinfo->Valid & ACPI_VALID_HID) ?
763 adinfo->HardwareId.Value : "UNKNOWN",
764 (adinfo->Valid & ACPI_VALID_UID) ?
765 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
774 * Handle per-device ivars
777 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
779 struct acpi_device *ad;
781 if ((ad = device_get_ivars(child)) == NULL) {
782 printf("device has no ivars\n");
786 /* ACPI and ISA compatibility ivars */
788 case ACPI_IVAR_HANDLE:
789 *(ACPI_HANDLE *)result = ad->ad_handle;
791 case ACPI_IVAR_MAGIC:
792 *(int *)result = ad->ad_magic;
794 case ACPI_IVAR_PRIVATE:
795 *(void **)result = ad->ad_private;
797 case ISA_IVAR_VENDORID:
798 case ISA_IVAR_SERIAL:
799 case ISA_IVAR_COMPATID:
802 case ISA_IVAR_LOGICALID:
803 *(int *)result = acpi_isa_get_logicalid(child);
813 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
815 struct acpi_device *ad;
817 if ((ad = device_get_ivars(child)) == NULL) {
818 printf("device has no ivars\n");
823 case ACPI_IVAR_HANDLE:
824 ad->ad_handle = (ACPI_HANDLE)value;
826 case ACPI_IVAR_MAGIC:
827 ad->ad_magic = (int)value;
829 case ACPI_IVAR_PRIVATE:
830 ad->ad_private = (void *)value;
833 panic("bad ivar write request (%d)", index);
841 * Handle child resource allocation/removal
844 acpi_set_resource(device_t dev, device_t child, int type, int rid,
845 u_long start, u_long count)
847 struct acpi_device *ad = device_get_ivars(child);
848 struct resource_list *rl = &ad->ad_rl;
850 resource_list_add(rl, type, rid, start, start + count -1, count);
856 acpi_get_resource(device_t dev, device_t child, int type, int rid,
857 u_long *startp, u_long *countp)
859 struct acpi_device *ad = device_get_ivars(child);
860 struct resource_list *rl = &ad->ad_rl;
861 struct resource_list_entry *rle;
863 rle = resource_list_find(rl, type, rid);
868 *startp = rle->start;
870 *countp = rle->count;
875 static struct resource *
876 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
877 u_long start, u_long end, u_long count, u_int flags)
879 struct acpi_device *ad = device_get_ivars(child);
880 struct resource_list *rl = &ad->ad_rl;
882 return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
887 acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
889 struct acpi_device *ad = device_get_ivars(child);
890 struct resource_list *rl = &ad->ad_rl;
892 return (resource_list_release(rl, bus, child, type, rid, r));
895 /* Allocate an IO port or memory resource, given its GAS. */
897 acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
901 if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
902 gas->RegisterBitWidth < 8)
905 switch (gas->AddressSpaceId) {
906 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
907 type = SYS_RES_MEMORY;
909 case ACPI_ADR_SPACE_SYSTEM_IO:
910 type = SYS_RES_IOPORT;
916 bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
917 return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
921 * Handle ISA-like devices probing for a PnP ID to match.
923 #define PNP_EISAID(s) \
924 ((((s[0] - '@') & 0x1f) << 2) \
925 | (((s[1] - '@') & 0x18) >> 3) \
926 | (((s[1] - '@') & 0x07) << 13) \
927 | (((s[2] - '@') & 0x1f) << 8) \
928 | (PNP_HEXTONUM(s[4]) << 16) \
929 | (PNP_HEXTONUM(s[3]) << 20) \
930 | (PNP_HEXTONUM(s[6]) << 24) \
931 | (PNP_HEXTONUM(s[5]) << 28))
934 acpi_isa_get_logicalid(device_t dev)
936 ACPI_DEVICE_INFO *devinfo;
943 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
947 buf.Length = ACPI_ALLOCATE_BUFFER;
951 /* Fetch and validate the HID. */
952 if ((h = acpi_get_handle(dev)) == NULL)
954 error = AcpiGetObjectInfo(h, &buf);
955 if (ACPI_FAILURE(error))
957 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
959 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
960 pnpid = PNP_EISAID(devinfo->HardwareId.Value);
963 if (buf.Pointer != NULL)
964 AcpiOsFree(buf.Pointer);
966 return_VALUE (pnpid);
970 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
972 ACPI_DEVICE_INFO *devinfo;
980 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
985 buf.Length = ACPI_ALLOCATE_BUFFER;
989 /* Fetch and validate the CID */
990 if ((h = acpi_get_handle(dev)) == NULL)
992 error = AcpiGetObjectInfo(h, &buf);
993 if (ACPI_FAILURE(error))
995 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
996 if ((devinfo->Valid & ACPI_VALID_CID) == 0)
999 if (devinfo->CompatibilityId.Count < count)
1000 count = devinfo->CompatibilityId.Count;
1001 for (i = 0; i < count; i++) {
1002 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
1004 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
1009 if (buf.Pointer != NULL)
1010 AcpiOsFree(buf.Pointer);
1012 return_VALUE (valid);
1016 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1018 int result, cid_count, i;
1019 uint32_t lid, cids[8];
1021 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1024 * ISA-style drivers attached to ACPI may persist and
1025 * probe manually if we return ENOENT. We never want
1026 * that to happen, so don't ever return it.
1030 /* Scan the supplied IDs for a match */
1031 lid = acpi_isa_get_logicalid(child);
1032 cid_count = acpi_isa_get_compatid(child, cids, 8);
1033 while (ids && ids->ip_id) {
1034 if (lid == ids->ip_id) {
1038 for (i = 0; i < cid_count; i++) {
1039 if (cids[i] == ids->ip_id) {
1048 return_VALUE (result);
1052 * Scan relevant portions of the ACPI namespace and attach child devices.
1054 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1055 * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
1058 acpi_probe_children(device_t bus)
1062 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
1065 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1068 /* Create any static children by calling device identify methods. */
1069 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1070 bus_generic_probe(bus);
1073 * Scan the namespace and insert placeholders for all the devices that
1076 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1077 * we want to create nodes for all devices, not just those that are
1078 * currently present. (This assumes that we don't want to create/remove
1079 * devices as they appear, which might be smarter.)
1081 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1082 for (i = 0; scopes[i] != NULL; i++) {
1083 status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1084 if (ACPI_SUCCESS(status)) {
1085 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1091 * Scan all of the child devices we have created and let them probe/attach.
1093 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
1094 bus_generic_attach(bus);
1097 * Some of these children may have attached others as part of their attach
1098 * process (eg. the root PCI bus driver), so rescan.
1100 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
1101 bus_generic_attach(bus);
1103 /* Attach wake sysctls. */
1104 acpi_wake_sysctl_walk(bus);
1106 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1111 * Evaluate a child device and determine whether we might attach a device to
1115 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1117 ACPI_OBJECT_TYPE type;
1118 device_t child, bus = (device_t)context;
1120 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1122 /* Skip this device if we think we'll have trouble with it. */
1123 if (acpi_avoid(handle)) {
1124 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "not scanning '%s'\n",
1125 acpi_name(handle)));
1126 return_ACPI_STATUS (AE_OK);
1129 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1131 case ACPI_TYPE_DEVICE:
1132 case ACPI_TYPE_PROCESSOR:
1133 case ACPI_TYPE_THERMAL:
1134 case ACPI_TYPE_POWER:
1135 if (acpi_disabled("children"))
1139 * Create a placeholder device for this node. Sort the placeholder
1140 * so that the probe/attach passes will run breadth-first.
1142 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1143 acpi_name(handle)));
1144 child = BUS_ADD_CHILD(bus, bus, level * 10, NULL, -1);
1147 acpi_set_handle(child, handle);
1149 /* Check if the device can generate wake events. */
1150 if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL)))
1151 device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE);
1154 * Check that the device is present. If it's not present,
1155 * leave it disabled (so that we have a device_t attached to
1156 * the handle, but we don't probe it).
1158 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1159 device_disable(child);
1164 * Get the device's resource settings and attach them.
1165 * Note that if the device has _PRS but no _CRS, we need
1166 * to decide when it's appropriate to try to configure the
1167 * device. Ignore the return value here; it's OK for the
1168 * device not to have any resources.
1170 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1172 /* If we're debugging, probe/attach now rather than later */
1173 ACPI_DEBUG_EXEC(device_probe_and_attach(child));
1178 return_ACPI_STATUS (AE_OK);
1182 acpi_shutdown_pre_sync(void *arg, int howto)
1184 struct acpi_softc *sc = arg;
1189 * Disable all ACPI events before soft off, otherwise the system
1190 * will be turned on again on some laptops.
1192 * XXX this should probably be restricted to masking some events just
1193 * before powering down, since we may still need ACPI during the
1196 if (sc->acpi_disable_on_poweroff)
1201 acpi_shutdown_final(void *arg, int howto)
1207 * If powering off, run the actual shutdown code on each processor.
1208 * It will only perform the shutdown on the BSP. Some chipsets do
1209 * not power off the system correctly if called from an AP.
1211 if ((howto & RB_POWEROFF) != 0) {
1212 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1213 if (ACPI_FAILURE(status)) {
1214 printf("AcpiEnterSleepStatePrep failed - %s\n",
1215 AcpiFormatException(status));
1218 printf("Powering system off using ACPI\n");
1219 acpi_shutdown_poweroff(NULL);
1221 printf("Shutting down ACPI\n");
1227 * Since this function may be called with locks held or in an unknown
1228 * context, it cannot allocate memory, acquire locks, sleep, etc.
1231 acpi_shutdown_poweroff(void *arg)
1237 /* Only attempt to power off if this is the BSP (cpuid 0). */
1238 if (mdcpu->mi.gd_cpuid != 0)
1241 ACPI_DISABLE_IRQS();
1242 status = AcpiEnterSleepState(ACPI_STATE_S5);
1243 if (ACPI_FAILURE(status)) {
1244 printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
1247 printf("ACPI power-off failed - timeout\n");
1252 acpi_enable_fixed_events(struct acpi_softc *sc)
1254 static int first_time = 1;
1258 /* Enable and clear fixed events and install handlers. */
1259 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
1260 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1261 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1262 acpi_event_power_button_sleep, sc);
1264 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1266 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
1267 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1268 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1269 acpi_event_sleep_button_sleep, sc);
1271 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
1278 * Returns true if the device is actually present and should
1279 * be attached to. This requires the present, enabled, UI-visible
1280 * and diagnostics-passed bits to be set.
1283 acpi_DeviceIsPresent(device_t dev)
1285 ACPI_DEVICE_INFO *devinfo;
1294 if ((h = acpi_get_handle(dev)) == NULL)
1297 buf.Length = ACPI_ALLOCATE_BUFFER;
1298 error = AcpiGetObjectInfo(h, &buf);
1299 if (ACPI_FAILURE(error))
1301 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1303 /* If no _STA method, must be present */
1304 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1307 /* Return true for 'present' and 'functioning' */
1308 if ((devinfo->CurrentStatus & 0x9) == 0x9)
1311 AcpiOsFree(buf.Pointer);
1316 * Returns true if the battery is actually present and inserted.
1319 acpi_BatteryIsPresent(device_t dev)
1321 ACPI_DEVICE_INFO *devinfo;
1330 if ((h = acpi_get_handle(dev)) == NULL)
1333 buf.Length = ACPI_ALLOCATE_BUFFER;
1334 error = AcpiGetObjectInfo(h, &buf);
1335 if (ACPI_FAILURE(error))
1337 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1339 /* If no _STA method, must be present */
1340 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1343 /* Return true for 'present' and 'functioning' */
1344 if ((devinfo->CurrentStatus & 0x19) == 0x19)
1347 AcpiOsFree(buf.Pointer);
1352 * Match a HID string against a device
1355 acpi_MatchHid(device_t dev, char *hid)
1357 ACPI_DEVICE_INFO *devinfo;
1368 if ((h = acpi_get_handle(dev)) == NULL)
1371 buf.Length = ACPI_ALLOCATE_BUFFER;
1372 error = AcpiGetObjectInfo(h, &buf);
1373 if (ACPI_FAILURE(error))
1375 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1377 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1378 strcmp(hid, devinfo->HardwareId.Value) == 0)
1380 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
1381 for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
1382 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
1389 AcpiOsFree(buf.Pointer);
1394 * Return the handle of a named object within our scope, ie. that of (parent)
1395 * or one if its parents.
1398 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1405 /* Walk back up the tree to the root */
1407 status = AcpiGetHandle(parent, path, &r);
1408 if (ACPI_SUCCESS(status)) {
1412 if (status != AE_NOT_FOUND)
1414 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1415 return (AE_NOT_FOUND);
1420 /* Find the difference between two PM tick counts. */
1422 acpi_TimerDelta(uint32_t end, uint32_t start)
1427 delta = end - start;
1428 else if (AcpiGbl_FADT->TmrValExt == 0)
1429 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1431 delta = ((0xFFFFFFFF - start) + end + 1);
1436 * Allocate a buffer with a preset data size.
1439 acpi_AllocBuffer(int size)
1443 buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_INTWAIT);
1445 buf->Pointer = (void *)(buf + 1);
1450 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1453 ACPI_OBJECT_LIST args;
1457 arg1.Type = ACPI_TYPE_INTEGER;
1458 arg1.Integer.Value = number;
1460 args.Pointer = &arg1;
1462 return (AcpiEvaluateObject(handle, path, &args, NULL));
1466 * Evaluate a path that should return an integer.
1469 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1478 handle = ACPI_ROOT_OBJECT;
1481 * Assume that what we've been pointed at is an Integer object, or
1482 * a method that will return an Integer.
1484 buf.Pointer = ¶m;
1485 buf.Length = sizeof(param);
1486 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1487 if (ACPI_SUCCESS(status)) {
1488 if (param.Type == ACPI_TYPE_INTEGER)
1489 *number = param.Integer.Value;
1495 * In some applications, a method that's expected to return an Integer
1496 * may instead return a Buffer (probably to simplify some internal
1497 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1498 * convert it into an Integer as best we can.
1502 if (status == AE_BUFFER_OVERFLOW) {
1503 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1504 status = AE_NO_MEMORY;
1506 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1507 if (ACPI_SUCCESS(status))
1508 status = acpi_ConvertBufferToInteger(&buf, number);
1509 AcpiOsFree(buf.Pointer);
1516 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1522 p = (ACPI_OBJECT *)bufp->Pointer;
1523 if (p->Type == ACPI_TYPE_INTEGER) {
1524 *number = p->Integer.Value;
1527 if (p->Type != ACPI_TYPE_BUFFER)
1529 if (p->Buffer.Length > sizeof(int))
1530 return (AE_BAD_DATA);
1533 val = p->Buffer.Pointer;
1534 for (i = 0; i < p->Buffer.Length; i++)
1535 *number += val[i] << (i * 8);
1540 * Iterate over the elements of an a package object, calling the supplied
1541 * function for each element.
1543 * XXX possible enhancement might be to abort traversal on error.
1546 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1547 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1552 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1553 return (AE_BAD_PARAMETER);
1555 /* Iterate over components */
1557 comp = pkg->Package.Elements;
1558 for (; i < pkg->Package.Count; i++, comp++)
1565 * Find the (index)th resource object in a set.
1568 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1573 rp = (ACPI_RESOURCE *)buf->Pointer;
1577 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1578 return (AE_BAD_PARAMETER);
1580 /* Check for terminator */
1581 if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
1582 return (AE_NOT_FOUND);
1583 rp = ACPI_NEXT_RESOURCE(rp);
1592 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1594 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1595 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1596 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1599 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1602 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1607 /* Initialise the buffer if necessary. */
1608 if (buf->Pointer == NULL) {
1609 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1610 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1611 return (AE_NO_MEMORY);
1612 rp = (ACPI_RESOURCE *)buf->Pointer;
1613 rp->Id = ACPI_RSTYPE_END_TAG;
1620 * Scan the current buffer looking for the terminator.
1621 * This will either find the terminator or hit the end
1622 * of the buffer and return an error.
1624 rp = (ACPI_RESOURCE *)buf->Pointer;
1626 /* Range check, don't go outside the buffer */
1627 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1628 return (AE_BAD_PARAMETER);
1629 if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
1631 rp = ACPI_NEXT_RESOURCE(rp);
1635 * Check the size of the buffer and expand if required.
1638 * size of existing resources before terminator +
1639 * size of new resource and header +
1640 * size of terminator.
1642 * Note that this loop should really only run once, unless
1643 * for some reason we are stuffing a *really* huge resource.
1645 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1646 res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
1647 ACPI_RESOURCE_LENGTH) >= buf->Length) {
1648 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1649 return (AE_NO_MEMORY);
1650 bcopy(buf->Pointer, newp, buf->Length);
1651 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1652 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1653 AcpiOsFree(buf->Pointer);
1654 buf->Pointer = newp;
1655 buf->Length += buf->Length;
1658 /* Insert the new resource. */
1659 bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
1661 /* And add the terminator. */
1662 rp = ACPI_NEXT_RESOURCE(rp);
1663 rp->Id = ACPI_RSTYPE_END_TAG;
1670 * Set interrupt model.
1673 acpi_SetIntrModel(int model)
1675 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1678 #define ACPI_MINIMUM_AWAKETIME 5
1681 acpi_sleep_enable(void *arg)
1683 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1687 * Set the system sleep state
1689 * Currently we support S1-S5 but S4 is only S4BIOS
1692 acpi_SetSleepState(struct acpi_softc *sc, int state)
1694 ACPI_STATUS status = AE_OK;
1698 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1701 /* Avoid reentry if already attempting to suspend. */
1702 if (sc->acpi_sstate != ACPI_STATE_S0)
1703 return_ACPI_STATUS (AE_BAD_PARAMETER);
1705 /* We recently woke up so don't suspend again for a while. */
1706 if (sc->acpi_sleep_disabled)
1707 return_ACPI_STATUS (AE_OK);
1714 status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
1715 if (status == AE_NOT_FOUND) {
1716 device_printf(sc->acpi_dev,
1717 "Sleep state S%d not supported by BIOS\n", state);
1719 } else if (ACPI_FAILURE(status)) {
1720 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1721 AcpiFormatException(status));
1725 sc->acpi_sstate = state;
1726 sc->acpi_sleep_disabled = 1;
1728 /* Disable all wake GPEs not appropriate for this state. */
1729 acpi_wake_limit_walk(state);
1731 /* Inform all devices that we are going to sleep. */
1732 if (DEVICE_SUSPEND(root_bus) != 0) {
1734 * Re-wake the system.
1736 * XXX note that a better two-pass approach with a 'veto' pass
1737 * followed by a "real thing" pass would be better, but the
1738 * current bus interface does not provide for this.
1740 DEVICE_RESUME(root_bus);
1741 return_ACPI_STATUS (AE_ERROR);
1744 status = AcpiEnterSleepStatePrep(state);
1745 if (ACPI_FAILURE(status)) {
1746 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1747 AcpiFormatException(status));
1751 if (sc->acpi_sleep_delay > 0)
1752 DELAY(sc->acpi_sleep_delay * 1000000);
1754 if (state != ACPI_STATE_S1) {
1755 acpi_sleep_machdep(sc, state);
1757 /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
1758 if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
1759 ACPI_MUTEX_NOT_ACQUIRED) {
1761 AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1764 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1765 if (state == ACPI_STATE_S4)
1768 status = AcpiEnterSleepState((UINT8)state);
1769 if (ACPI_FAILURE(status)) {
1770 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1771 AcpiFormatException(status));
1775 AcpiLeaveSleepState((UINT8)state);
1776 DEVICE_RESUME(root_bus);
1777 sc->acpi_sstate = ACPI_STATE_S0;
1778 acpi_enable_fixed_events(sc);
1782 * Shut down cleanly and power off. This will call us back through the
1783 * shutdown handlers.
1785 shutdown_nice(RB_POWEROFF);
1789 status = AE_BAD_PARAMETER;
1793 /* Disable a second sleep request for a short period */
1794 if (sc->acpi_sleep_disabled)
1795 callout_reset(&sc->acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
1796 acpi_sleep_enable, sc);
1798 return_ACPI_STATUS (status);
1801 /* Initialize a device's wake GPE. */
1803 acpi_wake_init(device_t dev, int type)
1805 struct acpi_prw_data prw;
1807 /* Check that the device can wake the system. */
1808 if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0)
1811 /* Evaluate _PRW to find the GPE. */
1812 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
1815 /* Set the requested type for the GPE (runtime, wake, or both). */
1816 if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
1817 device_printf(dev, "set GPE type failed\n");
1824 /* Enable or disable the device's wake GPE. */
1826 acpi_wake_set_enable(device_t dev, int enable)
1828 struct acpi_prw_data prw;
1833 /* Make sure the device supports waking the system. */
1834 flags = device_get_flags(dev);
1835 handle = acpi_get_handle(dev);
1836 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1839 /* Evaluate _PRW to find the GPE. */
1840 if (acpi_parse_prw(handle, &prw) != 0)
1844 status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1845 if (ACPI_FAILURE(status)) {
1846 device_printf(dev, "enable wake failed\n");
1849 device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
1851 status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1852 if (ACPI_FAILURE(status)) {
1853 device_printf(dev, "disable wake failed\n");
1856 device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
1862 /* Configure a device's GPE appropriately for the new sleep state. */
1864 acpi_wake_sleep_prep(device_t dev, int sstate)
1866 struct acpi_prw_data prw;
1870 /* Check that this is an ACPI device and get its GPE. */
1871 flags = device_get_flags(dev);
1872 handle = acpi_get_handle(dev);
1873 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1876 /* Evaluate _PRW to find the GPE. */
1877 if (acpi_parse_prw(handle, &prw) != 0)
1881 * TBD: All Power Resources referenced by elements 2 through N
1882 * of the _PRW object are put into the ON state.
1886 * If the user requested that this device wake the system and the next
1887 * sleep state is valid for this GPE, enable it and the device's wake
1888 * capability. The sleep state must be less than (i.e., higher power)
1889 * or equal to the value specified by _PRW. Return early, leaving
1890 * the appropriate power resources enabled.
1892 if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 &&
1893 sstate <= prw.lowest_wake) {
1895 device_printf(dev, "wake_prep enabled gpe %#x for state %d\n",
1896 prw.gpe_bit, sstate);
1897 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1898 acpi_SetInteger(handle, "_PSW", 1);
1903 * If the device wake was disabled or this sleep state is too low for
1904 * this device, disable its wake capability and GPE.
1906 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1907 acpi_SetInteger(handle, "_PSW", 0);
1909 device_printf(dev, "wake_prep disabled gpe %#x for state %d\n",
1910 prw.gpe_bit, sstate);
1913 * TBD: All Power Resources referenced by elements 2 through N
1914 * of the _PRW object are put into the OFF state.
1920 /* Re-enable GPEs after wake. */
1922 acpi_wake_run_prep(device_t dev)
1924 struct acpi_prw_data prw;
1928 /* Check that this is an ACPI device and get its GPE. */
1929 flags = device_get_flags(dev);
1930 handle = acpi_get_handle(dev);
1931 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1934 /* Evaluate _PRW to find the GPE. */
1935 if (acpi_parse_prw(handle, &prw) != 0)
1939 * TBD: Be sure all Power Resources referenced by elements 2 through N
1940 * of the _PRW object are in the ON state.
1943 /* Disable wake capability and if the user requested, enable the GPE. */
1944 acpi_SetInteger(handle, "_PSW", 0);
1945 if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0)
1946 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1951 acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status)
1953 struct acpi_prw_data prw;
1956 /* It's ok not to have _PRW if the device can't wake the system. */
1957 if (acpi_parse_prw(h, &prw) != 0)
1960 sstate = (int *)context;
1961 if (*sstate > prw.lowest_wake)
1962 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1967 /* Walk all system devices, disabling them if necessary for sstate. */
1969 acpi_wake_limit_walk(int sstate)
1971 ACPI_HANDLE sb_handle;
1973 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
1974 AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100,
1975 acpi_wake_limit, &sstate, NULL);
1979 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
1981 acpi_wake_sysctl_walk(device_t dev)
1983 int error, i, numdevs;
1987 error = device_get_children(dev, &devlist, &numdevs);
1988 if (error != 0 || numdevs == 0)
1990 for (i = 0; i < numdevs; i++) {
1992 if (!device_is_attached(child))
1994 if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) {
1996 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
1997 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
1998 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
1999 acpi_wake_set_sysctl, "I", "Device set to wake the system");
2000 #endif /* dfly_notyet */
2002 acpi_wake_sysctl_walk(child);
2004 free(devlist, M_TEMP);
2010 /* Enable or disable wake from userland. */
2012 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
2017 dev = (device_t)arg1;
2018 enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
2020 error = sysctl_handle_int(oidp, &enable, 0, req);
2021 if (error != 0 || req->newptr == NULL)
2023 if (enable != 0 && enable != 1)
2026 return (acpi_wake_set_enable(dev, enable));
2028 #endif /* dfly_notyet */
2030 /* Parse a device's _PRW into a structure. */
2032 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2035 ACPI_BUFFER prw_buffer;
2036 ACPI_OBJECT *res, *res2;
2039 if (h == NULL || prw == NULL)
2043 * The _PRW object (7.2.9) is only required for devices that have the
2044 * ability to wake the system from a sleeping state.
2047 prw_buffer.Pointer = NULL;
2048 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2049 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2050 if (ACPI_FAILURE(status))
2052 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2055 if (!ACPI_PKG_VALID(res, 2))
2059 * Element 1 of the _PRW object:
2060 * The lowest power system sleeping state that can be entered while still
2061 * providing wake functionality. The sleeping state being entered must
2062 * be less than (i.e., higher power) or equal to this value.
2064 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2068 * Element 0 of the _PRW object:
2070 switch (res->Package.Elements[0].Type) {
2071 case ACPI_TYPE_INTEGER:
2073 * If the data type of this package element is numeric, then this
2074 * _PRW package element is the bit index in the GPEx_EN, in the
2075 * GPE blocks described in the FADT, of the enable bit that is
2076 * enabled for the wake event.
2078 prw->gpe_handle = NULL;
2079 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2082 case ACPI_TYPE_PACKAGE:
2084 * If the data type of this package element is a package, then this
2085 * _PRW package element is itself a package containing two
2086 * elements. The first is an object reference to the GPE Block
2087 * device that contains the GPE that will be triggered by the wake
2088 * event. The second element is numeric and it contains the bit
2089 * index in the GPEx_EN, in the GPE Block referenced by the
2090 * first element in the package, of the enable bit that is enabled for
2093 * For example, if this field is a package then it is of the form:
2094 * Package() {\_SB.PCI0.ISA.GPE, 2}
2096 res2 = &res->Package.Elements[0];
2097 if (!ACPI_PKG_VALID(res2, 2))
2099 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2100 if (prw->gpe_handle == NULL)
2102 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2110 /* XXX No power resource handling yet. */
2111 prw->power_res = NULL;
2114 if (prw_buffer.Pointer != NULL)
2115 AcpiOsFree(prw_buffer.Pointer);
2120 * Enable/Disable ACPI
2123 acpi_Enable(struct acpi_softc *sc)
2128 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2131 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
2132 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
2133 if (!sc->acpi_enabled)
2134 status = AcpiEnableSubsystem(flags);
2138 if (status == AE_OK)
2139 sc->acpi_enabled = 1;
2141 return_ACPI_STATUS (status);
2145 acpi_Disable(struct acpi_softc *sc)
2149 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2152 if (sc->acpi_enabled)
2153 status = AcpiDisable();
2157 if (status == AE_OK)
2158 sc->acpi_enabled = 0;
2160 return_ACPI_STATUS (status);
2164 * ACPI Event Handlers
2167 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
2170 acpi_system_eventhandler_sleep(void *arg, int state)
2173 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2176 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2177 acpi_SetSleepState((struct acpi_softc *)arg, state);
2183 acpi_system_eventhandler_wakeup(void *arg, int state)
2186 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2188 /* Well, what to do? :-) */
2197 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
2200 acpi_event_power_button_sleep(void *context)
2202 struct acpi_softc *sc = (struct acpi_softc *)context;
2204 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2206 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
2208 return_VALUE (ACPI_INTERRUPT_HANDLED);
2212 acpi_event_power_button_wake(void *context)
2214 struct acpi_softc *sc = (struct acpi_softc *)context;
2216 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2218 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
2220 return_VALUE (ACPI_INTERRUPT_HANDLED);
2224 acpi_event_sleep_button_sleep(void *context)
2226 struct acpi_softc *sc = (struct acpi_softc *)context;
2228 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2230 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
2232 return_VALUE (ACPI_INTERRUPT_HANDLED);
2236 acpi_event_sleep_button_wake(void *context)
2238 struct acpi_softc *sc = (struct acpi_softc *)context;
2240 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2242 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
2244 return_VALUE (ACPI_INTERRUPT_HANDLED);
2248 * XXX This is kinda ugly, and should not be here.
2250 struct acpi_staticbuf {
2256 acpi_name(ACPI_HANDLE handle)
2258 static struct acpi_staticbuf buf;
2262 buf.buffer.Length = 512;
2263 buf.buffer.Pointer = &buf.data[0];
2265 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
2266 return (buf.buffer.Pointer);
2268 return ("(unknown path)");
2272 * Debugging/bug-avoidance. Avoid trying to fetch info on various
2273 * parts of the namespace.
2276 acpi_avoid(ACPI_HANDLE handle)
2278 char *cp, *env, *np;
2281 np = acpi_name(handle);
2284 if ((env = getenv("debug.acpi.avoid.paths")) == NULL &&
2285 (env = getenv("debug.acpi.avoid")) == NULL)
2288 /* Scan the avoid list checking for a match */
2291 while ((*cp != 0) && isspace(*cp))
2296 while ((cp[len] != 0) && !isspace(cp[len]))
2298 if (!strncmp(cp, np, len)) {
2310 * Debugging/bug-avoidance. Disable ACPI subsystem components. Note that
2311 * some components may be disabled by default and can only be enabled
2312 * via acpi_enabled() (debug.acpi.enabled).
2315 acpi_disabled(char *subsys)
2320 if ((env = getenv("debug.acpi.disabled")) == NULL)
2322 if (strcmp(env, "all") == 0) {
2327 /* Scan the disable list, checking for a match. */
2330 while (*cp != '\0' && isspace(*cp))
2335 while (cp[len] != '\0' && !isspace(cp[len]))
2337 if (strncmp(cp, subsys, len) == 0) {
2349 * Debugging/bug-avoidance. Enable ACPI subsystem components. Most
2350 * components are enabled by default. The ones that are not have to be
2351 * enabled via debug.acpi.enabled.
2354 acpi_enabled(char *subsys)
2359 if ((env = getenv("debug.acpi.enabled")) == NULL)
2361 if (strcmp(env, "all") == 0) {
2366 /* Scan the enable list, checking for a match. */
2369 while (*cp != '\0' && isspace(*cp))
2374 while (cp[len] != '\0' && !isspace(cp[len]))
2376 if (strncmp(cp, subsys, len) == 0) {
2388 * Control interface.
2390 * We multiplex ioctls for all participating ACPI devices here. Individual
2391 * drivers wanting to be accessible via /dev/acpi should use the
2392 * register/deregister interface to make their handlers visible.
2394 struct acpi_ioctl_hook
2396 TAILQ_ENTRY(acpi_ioctl_hook) link;
2402 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
2403 static int acpi_ioctl_hooks_initted;
2406 * Register an ioctl handler.
2409 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
2411 struct acpi_ioctl_hook *hp;
2413 hp = malloc(sizeof(*hp), M_ACPIDEV, M_INTWAIT);
2417 if (acpi_ioctl_hooks_initted == 0) {
2418 TAILQ_INIT(&acpi_ioctl_hooks);
2419 acpi_ioctl_hooks_initted = 1;
2421 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
2426 * Deregister an ioctl handler.
2429 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
2431 struct acpi_ioctl_hook *hp;
2433 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
2434 if ((hp->cmd == cmd) && (hp->fn == fn))
2438 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
2439 free(hp, M_ACPIDEV);
2444 acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
2450 acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
2456 acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
2458 struct acpi_softc *sc;
2459 struct acpi_ioctl_hook *hp;
2460 int error, xerror, state;
2469 * Scan the list of registered ioctls, looking for handlers.
2471 if (acpi_ioctl_hooks_initted) {
2472 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2473 if (hp->cmd == cmd) {
2474 xerror = hp->fn(cmd, addr, hp->arg);
2483 * Core ioctls are not permitted for non-writable user.
2484 * Currently, other ioctls just fetch information.
2485 * Not changing system behavior.
2487 if((flag & FWRITE) == 0)
2490 /* Core system ioctls. */
2493 if (ACPI_FAILURE(acpi_Enable(sc)))
2496 case ACPIIO_DISABLE:
2497 if (ACPI_FAILURE(acpi_Disable(sc)))
2500 case ACPIIO_SETSLPSTATE:
2501 if (!sc->acpi_enabled) {
2505 state = *(int *)addr;
2506 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
2507 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
2525 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2527 char sleep_state[4];
2530 UINT8 state, TypeA, TypeB;
2533 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) {
2534 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2535 sprintf(sleep_state, "S%d ", state);
2536 strcat(buf, sleep_state);
2539 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2544 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2546 char sleep_state[10];
2548 u_int new_state, old_state;
2550 old_state = *(u_int *)oidp->oid_arg1;
2551 if (old_state > ACPI_S_STATES_MAX + 1) {
2552 strcpy(sleep_state, "unknown");
2554 bzero(sleep_state, sizeof(sleep_state));
2555 strncpy(sleep_state, sleep_state_names[old_state],
2556 sizeof(sleep_state_names[old_state]));
2558 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
2559 if (error == 0 && req->newptr != NULL) {
2560 new_state = ACPI_STATE_S0;
2561 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
2562 if (strncmp(sleep_state, sleep_state_names[new_state],
2563 sizeof(sleep_state)) == 0)
2566 if (new_state <= ACPI_S_STATES_MAX + 1) {
2567 if (new_state != old_state)
2568 *(u_int *)oidp->oid_arg1 = new_state;
2577 /* Inform devctl(4) when we receive a Notify. */
2579 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
2581 char notify_buf[16];
2582 ACPI_BUFFER handle_buf;
2585 if (subsystem == NULL)
2588 handle_buf.Pointer = NULL;
2589 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
2590 status = AcpiNsHandleToPathname(h, &handle_buf);
2591 if (ACPI_FAILURE(status))
2593 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
2595 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
2597 AcpiOsFree(handle_buf.Pointer);
2602 * Support for parsing debug options from the kernel environment.
2604 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
2605 * by specifying the names of the bits in the debug.acpi.layer and
2606 * debug.acpi.level environment variables. Bits may be unset by
2607 * prefixing the bit name with !.
2615 static struct debugtag dbg_layer[] = {
2616 {"ACPI_UTILITIES", ACPI_UTILITIES},
2617 {"ACPI_HARDWARE", ACPI_HARDWARE},
2618 {"ACPI_EVENTS", ACPI_EVENTS},
2619 {"ACPI_TABLES", ACPI_TABLES},
2620 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
2621 {"ACPI_PARSER", ACPI_PARSER},
2622 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
2623 {"ACPI_EXECUTER", ACPI_EXECUTER},
2624 {"ACPI_RESOURCES", ACPI_RESOURCES},
2625 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
2626 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
2627 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
2628 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
2630 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
2631 {"ACPI_BATTERY", ACPI_BATTERY},
2632 {"ACPI_BUS", ACPI_BUS},
2633 {"ACPI_BUTTON", ACPI_BUTTON},
2634 {"ACPI_EC", ACPI_EC},
2635 {"ACPI_FAN", ACPI_FAN},
2636 {"ACPI_POWERRES", ACPI_POWERRES},
2637 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
2638 {"ACPI_THERMAL", ACPI_THERMAL},
2639 {"ACPI_TIMER", ACPI_TIMER},
2640 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
2644 static struct debugtag dbg_level[] = {
2645 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
2646 {"ACPI_LV_WARN", ACPI_LV_WARN},
2647 {"ACPI_LV_INIT", ACPI_LV_INIT},
2648 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
2649 {"ACPI_LV_INFO", ACPI_LV_INFO},
2650 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
2652 /* Trace verbosity level 1 [Standard Trace Level] */
2653 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
2654 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
2655 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
2656 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
2657 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
2658 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
2659 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
2660 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
2661 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
2662 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
2663 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
2664 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
2665 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
2666 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
2667 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
2669 /* Trace verbosity level 2 [Function tracing and memory allocation] */
2670 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
2671 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
2672 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
2673 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
2674 {"ACPI_LV_ALL", ACPI_LV_ALL},
2676 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2677 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
2678 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
2679 {"ACPI_LV_IO", ACPI_LV_IO},
2680 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
2681 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
2683 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
2684 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
2685 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
2686 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
2687 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
2688 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
2693 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2705 while (*ep && !isspace(*ep))
2716 for (i = 0; tag[i].name != NULL; i++) {
2717 if (!strncmp(cp, tag[i].name, l)) {
2719 *flag |= tag[i].value;
2721 *flag &= ~tag[i].value;
2729 acpi_set_debugging(void *junk)
2731 char *layer, *level;
2738 layer = getenv("debug.acpi.layer");
2739 level = getenv("debug.acpi.level");
2740 if (layer == NULL && level == NULL)
2743 printf("ACPI set debug");
2744 if (layer != NULL) {
2745 if (strcmp("NONE", layer) != 0)
2746 printf(" layer '%s'", layer);
2747 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
2750 if (level != NULL) {
2751 if (strcmp("NONE", level) != 0)
2752 printf(" level '%s'", level);
2753 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
2758 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2762 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
2765 struct debugtag *tag;
2768 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
2770 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
2771 tag = &dbg_layer[0];
2772 dbg = &AcpiDbgLayer;
2774 tag = &dbg_level[0];
2775 dbg = &AcpiDbgLevel;
2778 /* Get old values if this is a get request. */
2780 sbuf_cpy(&sb, "NONE");
2781 } else if (req->newptr == NULL) {
2782 for (; tag->name != NULL; tag++) {
2783 if ((*dbg & tag->value) == tag->value)
2784 sbuf_printf(&sb, "%s ", tag->name);
2790 /* Copy out the old values to the user. */
2791 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
2794 /* If the user is setting a string, parse it. */
2795 if (error == 0 && req->newptr != NULL) {
2797 /* XXX setenv((char *)oidp->oid_arg1, (char *)req->newptr); */
2798 acpi_set_debugging(NULL);
2803 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
2804 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
2805 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
2806 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
2810 acpi_pm_func(u_long cmd, void *arg, ...)
2812 int state, acpi_state;
2814 struct acpi_softc *sc;
2819 case POWER_CMD_SUSPEND:
2820 sc = (struct acpi_softc *)arg;
2827 state = va_arg(ap, int);
2831 case POWER_SLEEP_STATE_STANDBY:
2832 acpi_state = sc->acpi_standby_sx;
2834 case POWER_SLEEP_STATE_SUSPEND:
2835 acpi_state = sc->acpi_suspend_sx;
2837 case POWER_SLEEP_STATE_HIBERNATE:
2838 acpi_state = ACPI_STATE_S4;
2845 acpi_SetSleepState(sc, acpi_state);
2857 acpi_pm_register(void *arg)
2859 if (!cold || resource_disabled("acpi", 0))
2862 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2865 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);