* Fix some cases where NULL was used but 0 was meant (and vice versa).
[dragonfly.git] / sys / dev / acpica5 / acpi.c
blob2120119e0aa1ff2fd5febb4b7e5998db4c017357
1 /*-
2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * Copyright (c) 2000, 2001 Michael Smith
5 * Copyright (c) 2000 BSDi
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
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.34 2008/06/05 18:06:31 swildner Exp $
33 #include "opt_acpi.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/proc.h>
37 #include <sys/fcntl.h>
38 #include <sys/malloc.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/ioccom.h>
43 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 #include <sys/ctype.h>
46 #include <sys/linker.h>
47 #include <sys/power.h>
48 #include <sys/sbuf.h>
49 #include <sys/rman.h>
51 #include <sys/thread2.h>
52 #include <sys/lock.h>
54 #include <machine/clock.h>
55 #include <machine/globaldata.h>
56 #include <bus/isa/isavar.h>
58 #include "acpi.h"
59 #include <dev/acpica5/acpivar.h>
60 #include <dev/acpica5/acpiio.h>
61 #include <acnamesp.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 },
76 .d_open = acpiopen,
77 .d_close = acpiclose,
78 .d_ioctl = acpiioctl
81 #if __FreeBSD_version >= 500000
82 struct mtx acpi_mutex;
83 #endif
85 /* Local pools for managing system resources for ACPI child devices. */
86 struct rman acpi_rman_io, acpi_rman_mem;
88 struct acpi_quirks {
89 char *OemId;
90 uint32_t OemRevision;
91 char *value;
94 #define ACPI_OEM_REV_ANY 0
96 static struct acpi_quirks acpi_quirks_table[] = {
97 #ifdef notyet
98 /* Bad PCI routing table. Used on some SuperMicro boards. */
99 { "PTLTD ", 0x06040000, "pci_link" },
100 #endif
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
105 * conditions.
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 */
114 { NULL, 0, NULL }
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,
127 uintptr_t *result);
128 static int acpi_write_ivar(device_t dev, device_t child, int index,
129 uintptr_t value);
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 int acpi_isa_pnp_probe(device_t bus, device_t child,
139 struct isa_pnp_id *ids);
140 static void acpi_probe_children(device_t bus);
141 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
142 void *context, void **status);
143 static void acpi_shutdown_pre_sync(void *arg, int howto);
144 static void acpi_shutdown_final(void *arg, int howto);
145 static void acpi_shutdown_poweroff(void *arg);
146 static void acpi_enable_fixed_events(struct acpi_softc *sc);
147 static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw);
148 static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context,
149 void **status);
150 static int acpi_wake_limit_walk(int sstate);
151 static int acpi_wake_sysctl_walk(device_t dev);
152 #ifdef dfly_notyet
153 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
154 #endif
155 static void acpi_system_eventhandler_sleep(void *arg, int state);
156 static void acpi_system_eventhandler_wakeup(void *arg, int state);
157 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
158 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
159 static int acpi_pm_func(u_long cmd, void *arg, ...);
160 static int acpi_child_location_str_method(device_t acdev, device_t child,
161 char *buf, size_t buflen);
162 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
163 char *buf, size_t buflen);
165 static device_method_t acpi_methods[] = {
166 /* Device interface */
167 DEVMETHOD(device_identify, acpi_identify),
168 DEVMETHOD(device_probe, acpi_probe),
169 DEVMETHOD(device_attach, acpi_attach),
170 DEVMETHOD(device_shutdown, acpi_shutdown),
171 DEVMETHOD(device_detach, bus_generic_detach),
172 DEVMETHOD(device_suspend, bus_generic_suspend),
173 DEVMETHOD(device_resume, bus_generic_resume),
175 /* Bus interface */
176 DEVMETHOD(bus_add_child, acpi_add_child),
177 DEVMETHOD(bus_print_child, acpi_print_child),
178 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
179 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
180 DEVMETHOD(bus_get_resource_list, acpi_get_rlist),
181 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
182 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
183 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
184 DEVMETHOD(bus_release_resource, acpi_release_resource),
185 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
186 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
187 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
188 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
189 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
190 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
191 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
193 /* ISA emulation */
194 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
196 {0, 0}
199 static driver_t acpi_driver = {
200 "acpi",
201 acpi_methods,
202 sizeof(struct acpi_softc),
205 static devclass_t acpi_devclass;
206 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
207 MODULE_VERSION(acpi, 1);
209 static const char* sleep_state_names[] = {
210 "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
212 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
213 static char acpi_ca_version[12];
214 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
215 acpi_ca_version, 0, "Version of Intel ACPI-CA");
218 * Allow override of whether methods execute in parallel or not.
219 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
220 * errors for AML that really can't handle parallel method execution.
221 * It is off by default since this breaks recursive methods and
222 * some IBMs use such code.
224 static int acpi_serialize_methods;
225 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
228 * ACPI can only be loaded as a module by the loader; activating it after
229 * system bootstrap time is not useful, and can be fatal to the system.
230 * It also cannot be unloaded, since the entire system bus heirarchy hangs
231 * off it.
233 static int
234 acpi_modevent(struct module *mod, int event, void *junk)
236 switch(event) {
237 case MOD_LOAD:
238 if (!cold) {
239 kprintf("The ACPI driver cannot be loaded after boot.\n");
240 return (EPERM);
242 break;
243 case MOD_UNLOAD:
244 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
245 return (EBUSY);
246 break;
247 default:
248 break;
250 return (0);
254 * Perform early initialization.
256 ACPI_STATUS
257 acpi_Startup(void)
259 #ifdef ACPI_DEBUGGER
260 char *debugpoint;
261 #endif
262 static int error, started = 0;
264 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
266 if (started)
267 return_VALUE (error);
268 started = 1;
270 /* Start up the ACPI CA subsystem. */
271 #ifdef ACPI_DEBUGGER
272 debugpoint = kgetenv("debug.acpi.debugger");
273 if (debugpoint) {
274 if (!strcmp(debugpoint, "init"))
275 acpi_EnterDebugger();
276 kfreeenv(debugpoint);
278 #endif
279 error = AcpiInitializeTables(NULL, 16, TRUE);
280 if (ACPI_FAILURE(error)) {
281 kprintf("ACPI: table initialization failed:\n");
282 return_VALUE (error);
285 /* Set up any quirks we have for this XSDT. */
286 acpi_quirks_set();
287 if (acpi_disabled("acpi"))
288 return_VALUE (AE_ERROR);
290 return_VALUE (AE_OK);
294 * Detect ACPI, perform early initialisation
296 static int
297 acpi_identify(driver_t *driver, device_t parent)
299 device_t child;
302 * No sense rescanning an ACPI 'bus'.
304 if (device_get_state(parent) == DS_ATTACHED)
305 return(0);
307 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
309 if (!cold)
310 return (ENXIO);
312 /* Check that we haven't been disabled with a hint. */
313 if (resource_disabled("acpi", 0))
314 return (ENXIO);
316 /* Make sure we're not being doubly invoked. */
317 if (device_find_child(parent, "acpi", 0) != NULL)
318 return (ENXIO);
320 /* Initialize ACPI-CA. */
321 if (ACPI_FAILURE(acpi_Startup()))
322 return (ENXIO);
324 ksnprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
326 /* Attach the actual ACPI device. */
327 if ((child = BUS_ADD_CHILD(parent, parent, 0, "acpi", 0)) == NULL) {
328 device_printf(parent, "ACPI: could not attach\n");
329 return (ENXIO);
331 return (0);
335 * Get a mapping of the root table header, as ACPICA code no longer
336 * keeps local copy of RSDT/XSDT
338 * return value: if non-NULL, mapped physical address of root table header.
339 * caller is supposed to unmap the region by AcpiOsUnmapMemory()
341 static ACPI_TABLE_HEADER *
342 acpi_map_rsdt_header(void)
344 ACPI_PHYSICAL_ADDRESS rsdp_addr, addr;
345 ACPI_TABLE_RSDP *rsdp;
347 if ((rsdp_addr = AcpiOsGetRootPointer()) == 0)
348 return(NULL);
349 if ((rsdp = AcpiOsMapMemory(rsdp_addr, sizeof(*rsdp))) == NULL)
350 return(NULL);
351 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
352 addr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
353 else
354 addr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
355 AcpiOsUnmapMemory(rsdp, sizeof(*rsdp));
357 return AcpiOsMapMemory(addr, sizeof(ACPI_TABLE_HEADER));
361 * Fetch some descriptive data from ACPI to put in our attach message
363 static int
364 acpi_probe(device_t dev)
366 ACPI_TABLE_HEADER *th;
367 char buf[20];
368 int error;
369 struct sbuf sb;
370 ACPI_LOCK_DECL;
372 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
374 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
375 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
377 device_printf(dev, "Other PM system enabled.\n");
378 return_VALUE(ENXIO);
381 ACPI_LOCK;
383 th = acpi_map_rsdt_header();
384 if (th == NULL) {
385 device_printf(dev, "couldn't get RSDT header\n");
386 error = ENXIO;
387 goto unlock;
390 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
391 sbuf_bcat(&sb, th->OemId, 6);
392 sbuf_trim(&sb);
393 sbuf_putc(&sb, ' ');
394 sbuf_bcat(&sb, th->OemTableId, 8);
395 sbuf_trim(&sb);
396 sbuf_finish(&sb);
397 device_set_desc_copy(dev, sbuf_data(&sb));
398 sbuf_delete(&sb);
399 AcpiOsUnmapMemory(th, sizeof(*th));
400 error = 0;
401 unlock:
402 ACPI_UNLOCK;
403 return_VALUE(error);
406 static int
407 acpi_attach(device_t dev)
409 struct acpi_softc *sc;
410 ACPI_STATUS status;
411 int error, state;
412 UINT32 flags;
413 UINT8 TypeA, TypeB;
414 char *env;
415 ACPI_TABLE_FACS *facsp;
416 #ifdef ACPI_DEBUGGER
417 char *debugpoint;
418 #endif
419 ACPI_LOCK_DECL;
421 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
422 #if __FreeBSD_version >= 500000
423 /* Initialise the ACPI mutex */
424 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
425 #endif
427 ACPI_LOCK;
428 sc = device_get_softc(dev);
429 bzero(sc, sizeof(*sc));
430 sc->acpi_dev = dev;
431 callout_init(&sc->acpi_sleep_timer);
433 if ((error = acpi_task_thread_init())) {
434 device_printf(dev, "Could not start task thread.\n");
435 goto out;
439 * Set the globals from our tunables. This is needed because ACPI-CA
440 * uses UINT8 for some values and we have no tunable_byte.
442 AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods;
443 AcpiGbl_EnableInterpreterSlack = TRUE;
445 error = ENXIO;
446 #ifdef ACPI_DEBUGGER
447 debugpoint = kgetenv("debug.acpi.debugger");
448 if (debugpoint) {
449 if (!strcmp(debugpoint, "tables"))
450 acpi_EnterDebugger();
451 kfreeenv(debugpoint);
453 #endif
455 if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
456 kprintf("ACPI: initialisation failed: %s\n",
457 AcpiFormatException(status));
458 goto out;
460 if (ACPI_FAILURE(status = AcpiLoadTables())) {
461 kprintf("ACPI: table load failed: %s\n", AcpiFormatException(status));
462 goto out;
465 /* Initialize resource manager. */
466 acpi_rman_io.rm_type = RMAN_ARRAY;
467 acpi_rman_io.rm_start = 0;
468 acpi_rman_io.rm_end = 0xffff;
469 acpi_rman_io.rm_descr = "I/O ports";
470 if (rman_init(&acpi_rman_io) != 0)
471 panic("acpi rman_init IO ports failed");
472 acpi_rman_mem.rm_type = RMAN_ARRAY;
473 acpi_rman_mem.rm_start = 0;
474 acpi_rman_mem.rm_end = ~0ul;
475 acpi_rman_mem.rm_descr = "I/O memory addresses";
476 if (rman_init(&acpi_rman_mem) != 0)
477 panic("acpi rman_init memory failed");
479 #ifdef ACPI_DEBUGGER
480 debugpoint = kgetenv("debug.acpi.debugger");
481 if (debugpoint) {
482 if (!strcmp(debugpoint, "spaces"))
483 acpi_EnterDebugger();
484 kfreeenv(debugpoint);
486 #endif
487 /* Install the default address space handlers. */
488 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
489 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
490 if (ACPI_FAILURE(status)) {
491 device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
492 AcpiFormatException(status));
493 goto out;
495 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
496 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
497 if (ACPI_FAILURE(status)) {
498 device_printf(dev, "Could not initialise SystemIO handler: %s\n",
499 AcpiFormatException(status));
500 goto out;
502 status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
503 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
504 if (ACPI_FAILURE(status)) {
505 device_printf(dev, "could not initialise PciConfig handler: %s\n",
506 AcpiFormatException(status));
507 goto out;
511 * Bring ACPI fully online.
513 * Note that some systems (specifically, those with namespace evaluation
514 * issues that require the avoidance of parts of the namespace) must
515 * avoid running _INI and _STA on everything, as well as dodging the final
516 * object init pass.
518 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
519 * For avoiding portions of the namespace without totally disabling _INI
520 * and _STA, use "debug.acpi.avoid.paths".
522 * XXX We should arrange for the object init pass after we have attached
523 * all our child devices, but on many systems it works here.
525 #ifdef ACPI_DEBUGGER
526 debugpoint = kgetenv("debug.acpi.debugger");
527 if (debugpoint) {
528 if (!strcmp(debugpoint, "enable"))
529 acpi_EnterDebugger();
530 kfreeenv(debugpoint);
532 #endif
533 flags = 0;
534 if (ktestenv("debug.acpi.avoid"))
535 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
536 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
537 device_printf(dev, "Could not enable ACPI: %s\n",
538 AcpiFormatException(status));
539 goto out;
543 * Call the ECDT probe function to provide EC functionality before
544 * the namespace has been evaluated.
546 acpi_ec_ecdt_probe(dev);
548 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
549 device_printf(dev, "Could not initialize ACPI objects: %s\n",
550 AcpiFormatException(status));
551 goto out;
555 * Setup our sysctl tree.
557 * XXX: This doesn't check to make sure that none of these fail.
559 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
560 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
561 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
562 device_get_name(dev), CTLFLAG_RD, 0, "");
563 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
564 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
565 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
566 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
567 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
568 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
569 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
570 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
571 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
572 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
573 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
574 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
575 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
576 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
577 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
578 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
579 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
580 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
581 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
582 OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
583 &sc->acpi_sleep_delay, 0, "sleep delay");
584 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
585 OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
586 &sc->acpi_s4bios, 0, "S4BIOS mode");
587 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
588 OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
589 &sc->acpi_verbose, 0, "verbose mode");
590 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
591 OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
592 &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
595 * Default to 1 second before sleeping to give some machines time to
596 * stabilize.
598 sc->acpi_sleep_delay = 1;
599 sc->acpi_disable_on_poweroff = 0;
600 if (bootverbose)
601 sc->acpi_verbose = 1;
602 if ((env = kgetenv("hw.acpi.verbose")) && strcmp(env, "0")) {
603 sc->acpi_verbose = 1;
604 kfreeenv(env);
607 /* Only enable S4BIOS by default if the FACS says it is available. */
608 status = AcpiGetTableByIndex(ACPI_TABLE_INDEX_FACS,
609 (ACPI_TABLE_HEADER **)&facsp);
610 if (ACPI_SUCCESS(status)) {
611 if ((facsp->Flags & ACPI_FACS_S4_BIOS_PRESENT) != 0)
612 sc->acpi_s4bios = 1;
616 * Dispatch the default sleep state to devices. The lid switch is set
617 * to NONE by default to avoid surprising users.
619 sc->acpi_power_button_sx = ACPI_STATE_S5;
620 sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
621 sc->acpi_standby_sx = ACPI_STATE_S1;
622 sc->acpi_suspend_sx = ACPI_STATE_S3;
624 /* Pick the first valid sleep state for the sleep button default. */
625 sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
626 for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
627 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
628 sc->acpi_sleep_button_sx = state;
629 break;
632 acpi_enable_fixed_events(sc);
635 * Scan the namespace and attach/initialise children.
637 #ifdef ACPI_DEBUGGER
638 debugpoint = kgetenv("debug.acpi.debugger");
639 if (debugpoint) {
640 if (!strcmp(debugpoint, "probe"))
641 acpi_EnterDebugger();
642 kfreeenv(debugpoint);
644 #endif
646 /* Register our shutdown handlers */
647 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
648 SHUTDOWN_PRI_LAST);
649 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
650 SHUTDOWN_PRI_LAST);
653 * Register our acpi event handlers.
654 * XXX should be configurable eg. via userland policy manager.
656 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
657 sc, ACPI_EVENT_PRI_LAST);
658 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
659 sc, ACPI_EVENT_PRI_LAST);
661 /* Flag our initial states. */
662 sc->acpi_enabled = 1;
663 sc->acpi_sstate = ACPI_STATE_S0;
664 sc->acpi_sleep_disabled = 0;
666 /* Create the control device */
667 dev_ops_add(&acpi_ops, 0, 0);
668 sc->acpi_dev_t = make_dev(&acpi_ops, 0, UID_ROOT, GID_WHEEL, 0644,
669 "acpi");
670 sc->acpi_dev_t->si_drv1 = sc;
672 #ifdef ACPI_DEBUGGER
673 debugpoint = kgetenv("debug.acpi.debugger");
674 if (debugpoint) {
675 if (strcmp(debugpoint, "running") == 0)
676 acpi_EnterDebugger();
677 kfreeenv(debugpoint);
679 #endif
681 if ((error = acpi_machdep_init(dev)))
682 goto out;
684 /* Register ACPI again to pass the correct argument of pm_func. */
685 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
687 if (!acpi_disabled("bus"))
688 acpi_probe_children(dev);
690 error = 0;
692 out:
693 ACPI_UNLOCK;
694 return_VALUE (error);
697 static int
698 acpi_shutdown(device_t dev)
700 /* Allow children to shutdown first. */
701 bus_generic_shutdown(dev);
703 /* Disable all wake GPEs not appropriate for reboot/poweroff. */
704 acpi_wake_limit_walk(ACPI_STATE_S5);
705 return (0);
708 static void
709 acpi_quirks_set(void)
711 ACPI_TABLE_HEADER *rsdt;
712 struct acpi_quirks *quirk;
713 char *env, *tmp;
714 int len;
717 * If the user loaded a custom table or disabled "quirks", leave
718 * the settings alone.
720 len = 0;
721 if ((env = kgetenv("acpi_dsdt_load")) != NULL) {
722 /* XXX No strcasecmp but this is good enough. */
723 if (*env == 'Y' || *env == 'y')
724 goto out;
725 kfreeenv(env);
727 if ((env = kgetenv("debug.acpi.disabled")) != NULL) {
728 if (strstr("quirks", env) != NULL)
729 goto out;
730 len = strlen(env);
734 * Search through our quirk table and concatenate the disabled
735 * values with whatever we find.
737 if ((rsdt = acpi_map_rsdt_header()) == NULL)
738 goto out;
739 for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
740 if (!strncmp(rsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
741 (rsdt->OemRevision == quirk->OemRevision ||
742 quirk->OemRevision == ACPI_OEM_REV_ANY)) {
743 len += strlen(quirk->value) + 2;
744 if ((tmp = kmalloc(len, M_TEMP, M_NOWAIT)) == NULL)
745 goto out;
746 ksprintf(tmp, "%s %s", env ? env : "", quirk->value);
747 ksetenv("debug.acpi.disabled", tmp);
748 kfree(tmp, M_TEMP);
749 break;
752 AcpiOsUnmapMemory(rsdt, sizeof(*rsdt));
754 out:
755 if (env)
756 kfreeenv(env);
760 * Handle a new device being added
762 static device_t
763 acpi_add_child(device_t bus, device_t parent, int order,
764 const char *name, int unit)
766 struct acpi_device *ad;
767 device_t child;
769 ad = kmalloc(sizeof(*ad), M_ACPIDEV, M_INTWAIT | M_ZERO);
771 resource_list_init(&ad->ad_rl);
773 child = device_add_child_ordered(parent, order, name, unit);
774 if (child != NULL)
775 device_set_ivars(child, ad);
776 return (child);
779 static int
780 acpi_print_child(device_t bus, device_t child)
782 struct acpi_device *adev = device_get_ivars(child);
783 struct resource_list *rl = &adev->ad_rl;
784 int retval = 0;
786 retval += bus_print_child_header(bus, child);
787 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
788 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
789 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
790 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
791 retval += bus_print_child_footer(bus, child);
793 return (retval);
796 /* Location hint for devctl(8) */
797 static int
798 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
799 size_t buflen)
801 struct acpi_device *dinfo = device_get_ivars(child);
803 if (dinfo->ad_handle)
804 ksnprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
805 else
806 ksnprintf(buf, buflen, "magic=unknown");
807 return (0);
810 /* PnP information for devctl(8) */
811 static int
812 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
813 size_t buflen)
815 ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
816 ACPI_DEVICE_INFO *adinfo;
817 struct acpi_device *dinfo = device_get_ivars(child);
818 char *end;
819 int error;
821 error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
822 adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
824 if (error)
825 ksnprintf(buf, buflen, "Unknown");
826 else
827 ksnprintf(buf, buflen, "_HID=%s _UID=%lu",
828 (adinfo->Valid & ACPI_VALID_HID) ?
829 adinfo->HardwareId.Value : "UNKNOWN",
830 (adinfo->Valid & ACPI_VALID_UID) ?
831 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
833 if (adinfo)
834 AcpiOsFree(adinfo);
836 return (0);
840 * Handle per-device ivars
842 static int
843 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
845 struct acpi_device *ad;
847 if ((ad = device_get_ivars(child)) == NULL) {
848 kprintf("device has no ivars\n");
849 return (ENOENT);
852 /* ACPI and ISA compatibility ivars */
853 switch(index) {
854 case ACPI_IVAR_HANDLE:
855 *(ACPI_HANDLE *)result = ad->ad_handle;
856 break;
857 case ACPI_IVAR_MAGIC:
858 *(int *)result = ad->ad_magic;
859 break;
860 case ACPI_IVAR_PRIVATE:
861 *(void **)result = ad->ad_private;
862 break;
863 case ISA_IVAR_VENDORID:
864 case ISA_IVAR_SERIAL:
865 case ISA_IVAR_COMPATID:
866 *(int *)result = -1;
867 break;
868 case ISA_IVAR_LOGICALID:
869 *(int *)result = acpi_isa_get_logicalid(child);
870 break;
871 default:
872 return (ENOENT);
875 return (0);
878 static int
879 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
881 struct acpi_device *ad;
883 if ((ad = device_get_ivars(child)) == NULL) {
884 kprintf("device has no ivars\n");
885 return (ENOENT);
888 switch(index) {
889 case ACPI_IVAR_HANDLE:
890 ad->ad_handle = (ACPI_HANDLE)value;
891 break;
892 case ACPI_IVAR_MAGIC:
893 ad->ad_magic = (int)value;
894 break;
895 case ACPI_IVAR_PRIVATE:
896 ad->ad_private = (void *)value;
897 break;
898 default:
899 panic("bad ivar write request (%d)", index);
900 return (ENOENT);
903 return (0);
907 * Handle child resource allocation/removal
909 static struct resource_list *
910 acpi_get_rlist(device_t dev, device_t child)
912 struct acpi_device *ad;
914 ad = device_get_ivars(child);
915 return (&ad->ad_rl);
918 static struct resource *
919 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
920 u_long start, u_long end, u_long count, u_int flags)
922 struct acpi_device *ad = device_get_ivars(child);
923 struct resource_list *rl = &ad->ad_rl;
924 struct resource_list_entry *rle;
925 struct resource *res;
926 struct rman *rm;
927 int needactivate;
930 * If this is an allocation of the "default" range for a given RID, and
931 * we know what the resources for this device are (i.e., they're on the
932 * child's resource list), use those start/end values.
934 if (start == 0UL && end == ~0UL) {
935 rle = resource_list_find(rl, type, *rid);
936 if (rle == NULL)
937 return (NULL);
938 start = rle->start;
939 end = rle->end;
940 count = rle->count;
943 /* If we don't manage this address, pass the request up to the parent. */
944 rle = acpi_sysres_find(type, start);
945 if (rle == NULL) {
946 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
947 start, end, count, flags));
950 /* We only handle memory and IO resources through rman. */
951 switch (type) {
952 case SYS_RES_IOPORT:
953 rm = &acpi_rman_io;
954 break;
955 case SYS_RES_MEMORY:
956 rm = &acpi_rman_mem;
957 break;
958 default:
959 panic("acpi_alloc_resource: invalid res type %d", type);
962 /* If we do know it, allocate it from the local pool. */
963 needactivate = flags & RF_ACTIVE;
964 flags &= ~RF_ACTIVE;
965 res = rman_reserve_resource(rm, start, end, count, flags, child);
966 if (res == NULL)
967 return (NULL);
969 /* Copy the bus tag from the pre-allocated resource. */
970 rman_set_bustag(res, rman_get_bustag(rle->res));
971 if (type == SYS_RES_IOPORT)
972 rman_set_bushandle(res, res->r_start);
974 /* If requested, activate the resource using the parent's method. */
975 if (needactivate)
976 if (bus_activate_resource(child, type, *rid, res) != 0) {
977 rman_release_resource(res);
978 return (NULL);
981 return (res);
984 static int
985 acpi_release_resource(device_t bus, device_t child, int type, int rid,
986 struct resource *r)
988 int ret;
991 * If we know about this address, deactivate it and release it to the
992 * local pool. If we don't, pass this request up to the parent.
994 if (acpi_sysres_find(type, rman_get_start(r)) == NULL) {
995 if (rman_get_flags(r) & RF_ACTIVE) {
996 ret = bus_deactivate_resource(child, type, rid, r);
997 if (ret != 0)
998 return (ret);
1000 ret = rman_release_resource(r);
1001 } else
1002 ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
1004 return (ret);
1007 /* Allocate an IO port or memory resource, given its GAS. */
1008 struct resource *
1009 acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
1011 int type;
1013 if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || gas->BitWidth < 8)
1014 return (NULL);
1016 switch (gas->SpaceId) {
1017 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1018 type = SYS_RES_MEMORY;
1019 break;
1020 case ACPI_ADR_SPACE_SYSTEM_IO:
1021 type = SYS_RES_IOPORT;
1022 break;
1023 default:
1024 return (NULL);
1027 bus_set_resource(dev, type, *rid, gas->Address, gas->BitWidth / 8);
1028 return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
1032 * Handle ISA-like devices probing for a PnP ID to match.
1034 #define PNP_EISAID(s) \
1035 ((((s[0] - '@') & 0x1f) << 2) \
1036 | (((s[1] - '@') & 0x18) >> 3) \
1037 | (((s[1] - '@') & 0x07) << 13) \
1038 | (((s[2] - '@') & 0x1f) << 8) \
1039 | (PNP_HEXTONUM(s[4]) << 16) \
1040 | (PNP_HEXTONUM(s[3]) << 20) \
1041 | (PNP_HEXTONUM(s[6]) << 24) \
1042 | (PNP_HEXTONUM(s[5]) << 28))
1044 static uint32_t
1045 acpi_isa_get_logicalid(device_t dev)
1047 ACPI_DEVICE_INFO *devinfo;
1048 ACPI_BUFFER buf;
1049 ACPI_HANDLE h;
1050 ACPI_STATUS error;
1051 u_int32_t pnpid;
1052 ACPI_LOCK_DECL;
1054 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1056 pnpid = 0;
1057 buf.Pointer = NULL;
1058 buf.Length = ACPI_ALLOCATE_BUFFER;
1060 ACPI_LOCK;
1062 /* Fetch and validate the HID. */
1063 if ((h = acpi_get_handle(dev)) == NULL)
1064 goto out;
1065 error = AcpiGetObjectInfo(h, &buf);
1066 if (ACPI_FAILURE(error))
1067 goto out;
1068 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1070 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
1071 pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1073 out:
1074 if (buf.Pointer != NULL)
1075 AcpiOsFree(buf.Pointer);
1076 ACPI_UNLOCK;
1077 return_VALUE (pnpid);
1080 static int
1081 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1083 ACPI_DEVICE_INFO *devinfo;
1084 ACPI_BUFFER buf;
1085 ACPI_HANDLE h;
1086 ACPI_STATUS error;
1087 uint32_t *pnpid;
1088 int valid, i;
1089 ACPI_LOCK_DECL;
1091 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1093 pnpid = cids;
1094 valid = 0;
1095 buf.Pointer = NULL;
1096 buf.Length = ACPI_ALLOCATE_BUFFER;
1098 ACPI_LOCK;
1100 /* Fetch and validate the CID */
1101 if ((h = acpi_get_handle(dev)) == NULL)
1102 goto out;
1103 error = AcpiGetObjectInfo(h, &buf);
1104 if (ACPI_FAILURE(error))
1105 goto out;
1106 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1107 if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1108 goto out;
1110 if (devinfo->CompatibilityId.Count < count)
1111 count = devinfo->CompatibilityId.Count;
1112 for (i = 0; i < count; i++) {
1113 if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
1114 continue;
1115 *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
1116 valid++;
1119 out:
1120 if (buf.Pointer != NULL)
1121 AcpiOsFree(buf.Pointer);
1122 ACPI_UNLOCK;
1123 return_VALUE (valid);
1126 static int
1127 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1129 int result, cid_count, i;
1130 uint32_t lid, cids[8];
1132 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1135 * ISA-style drivers attached to ACPI may persist and
1136 * probe manually if we return ENOENT. We never want
1137 * that to happen, so don't ever return it.
1139 result = ENXIO;
1141 /* Scan the supplied IDs for a match */
1142 lid = acpi_isa_get_logicalid(child);
1143 cid_count = acpi_isa_get_compatid(child, cids, 8);
1144 while (ids && ids->ip_id) {
1145 if (lid == ids->ip_id) {
1146 result = 0;
1147 goto out;
1149 for (i = 0; i < cid_count; i++) {
1150 if (cids[i] == ids->ip_id) {
1151 result = 0;
1152 goto out;
1155 ids++;
1158 out:
1159 return_VALUE (result);
1163 * Scan relevant portions of the ACPI namespace and attach child devices.
1165 * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1166 * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
1168 static void
1169 acpi_probe_children(device_t bus)
1171 ACPI_HANDLE parent;
1172 ACPI_STATUS status;
1173 int i;
1174 static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
1176 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1177 ACPI_ASSERTLOCK;
1180 * Scan the namespace and insert placeholders for all the devices that
1181 * we find. We also probe/attach any early devices.
1183 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1184 * we want to create nodes for all devices, not just those that are
1185 * currently present. (This assumes that we don't want to create/remove
1186 * devices as they appear, which might be smarter.)
1188 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1189 for (i = 0; scopes[i] != NULL; i++) {
1190 status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1191 if (ACPI_SUCCESS(status)) {
1192 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1193 bus, NULL);
1197 /* Create any static children by calling device identify methods. */
1198 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1199 bus_generic_probe(bus);
1202 * Scan all of the child devices we have created and let them probe/attach.
1204 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
1205 bus_generic_attach(bus);
1208 * Some of these children may have attached others as part of their attach
1209 * process (eg. the root PCI bus driver), so rescan.
1211 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
1212 bus_generic_attach(bus);
1214 /* Attach wake sysctls. */
1215 acpi_wake_sysctl_walk(bus);
1217 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1218 return_VOID;
1221 static int
1222 acpi_probe_order(ACPI_HANDLE handle, int level, int *order)
1224 int ret;
1226 ret = 0;
1227 /* IO port and memory system resource holders are first. */
1228 if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) {
1229 *order = 1;
1230 ret = 1;
1233 /* The embedded controller is needed to handle accesses early. */
1234 if (acpi_MatchHid(handle, "PNP0C09")) {
1235 *order = 2;
1236 ret = 1;
1239 *order = (level + 1) * 10;
1240 return (ret);
1244 * Evaluate a child device and determine whether we might attach a device to
1245 * it.
1247 static ACPI_STATUS
1248 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1250 ACPI_OBJECT_TYPE type;
1251 device_t child, bus;
1252 int order, probe_now;
1254 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1256 /* Skip this device if we think we'll have trouble with it. */
1257 if (acpi_avoid(handle)) {
1258 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "not scanning '%s'\n",
1259 acpi_name(handle)));
1260 return_ACPI_STATUS (AE_OK);
1263 bus = (device_t)context;
1264 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1265 switch(type) {
1266 case ACPI_TYPE_DEVICE:
1267 case ACPI_TYPE_PROCESSOR:
1268 case ACPI_TYPE_THERMAL:
1269 case ACPI_TYPE_POWER:
1270 if (acpi_disabled("children"))
1271 break;
1274 * Create a placeholder device for this node. Sort the placeholder
1275 * so that the probe/attach passes will run breadth-first.
1277 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1278 acpi_name(handle)));
1279 probe_now = acpi_probe_order(handle, level, &order);
1280 child = BUS_ADD_CHILD(bus, bus, order, NULL, -1);
1281 if (child == NULL)
1282 break;
1284 /* Associate the handle with the device_t and vice versa. */
1285 acpi_set_handle(child, handle);
1286 AcpiAttachData(handle, acpi_fake_objhandler, child);
1288 /* Check if the device can generate wake events. */
1289 if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL)))
1290 device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE);
1293 * Check that the device is present. If it's not present,
1294 * leave it disabled (so that we have a device_t attached to
1295 * the handle, but we don't probe it).
1297 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1298 device_disable(child);
1299 break;
1303 * Get the device's resource settings and attach them.
1304 * Note that if the device has _PRS but no _CRS, we need
1305 * to decide when it's appropriate to try to configure the
1306 * device. Ignore the return value here; it's OK for the
1307 * device not to have any resources.
1309 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1311 /* If order was overridden, probe/attach now rather than later. */
1312 if (probe_now)
1313 device_probe_and_attach(child);
1314 break;
1318 return_ACPI_STATUS (AE_OK);
1321 static void
1322 acpi_shutdown_pre_sync(void *arg, int howto)
1324 struct acpi_softc *sc = arg;
1326 ACPI_ASSERTLOCK;
1329 * Disable all ACPI events before soft off, otherwise the system
1330 * will be turned on again on some laptops.
1332 * XXX this should probably be restricted to masking some events just
1333 * before powering down, since we may still need ACPI during the
1334 * shutdown process.
1336 if (sc->acpi_disable_on_poweroff)
1337 acpi_Disable(sc);
1341 * AcpiAttachData() requires an object handler but never uses it. This is a
1342 * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
1344 void
1345 acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
1349 static void
1350 acpi_shutdown_final(void *arg, int howto)
1352 ACPI_STATUS status;
1353 ACPI_ASSERTLOCK;
1356 * If powering off, run the actual shutdown code on each processor.
1357 * It will only perform the shutdown on the BSP. Some chipsets do
1358 * not power off the system correctly if called from an AP.
1360 if ((howto & RB_POWEROFF) != 0) {
1361 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1362 if (ACPI_FAILURE(status)) {
1363 kprintf("AcpiEnterSleepStatePrep failed - %s\n",
1364 AcpiFormatException(status));
1365 return;
1367 kprintf("Powering system off using ACPI\n");
1368 acpi_shutdown_poweroff(NULL);
1369 } else {
1370 kprintf("Shutting down ACPI\n");
1371 AcpiTerminate();
1376 * Since this function may be called with locks held or in an unknown
1377 * context, it cannot allocate memory, acquire locks, sleep, etc.
1379 static void
1380 acpi_shutdown_poweroff(void *arg)
1382 ACPI_STATUS status;
1384 ACPI_ASSERTLOCK;
1386 /* Only attempt to power off if this is the BSP (cpuid 0). */
1387 if (mdcpu->mi.gd_cpuid != 0)
1388 return;
1390 ACPI_DISABLE_IRQS();
1391 status = AcpiEnterSleepState(ACPI_STATE_S5);
1392 if (ACPI_FAILURE(status)) {
1393 kprintf("ACPI power-off failed - %s\n", AcpiFormatException(status));
1394 } else {
1395 DELAY(1000000);
1396 kprintf("ACPI power-off failed - timeout\n");
1400 static void
1401 acpi_enable_fixed_events(struct acpi_softc *sc)
1403 static int first_time = 1;
1405 ACPI_ASSERTLOCK;
1407 /* Enable and clear fixed events and install handlers. */
1408 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
1409 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1410 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1411 acpi_event_power_button_sleep, sc);
1412 if (first_time)
1413 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1415 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1416 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1417 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1418 acpi_event_sleep_button_sleep, sc);
1419 if (first_time)
1420 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
1423 first_time = 0;
1427 * Returns true if the device is actually present and should
1428 * be attached to. This requires the present, enabled, UI-visible
1429 * and diagnostics-passed bits to be set.
1431 BOOLEAN
1432 acpi_DeviceIsPresent(device_t dev)
1434 ACPI_DEVICE_INFO *devinfo;
1435 ACPI_HANDLE h;
1436 ACPI_BUFFER buf;
1437 ACPI_STATUS error;
1438 int ret;
1440 ACPI_ASSERTLOCK;
1442 ret = FALSE;
1443 if ((h = acpi_get_handle(dev)) == NULL)
1444 return (FALSE);
1445 buf.Pointer = NULL;
1446 buf.Length = ACPI_ALLOCATE_BUFFER;
1447 error = AcpiGetObjectInfo(h, &buf);
1448 if (ACPI_FAILURE(error))
1449 return (FALSE);
1450 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1452 /* If no _STA method, must be present */
1453 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1454 ret = TRUE;
1456 /* Return true for 'present' and 'functioning' */
1457 if ((devinfo->CurrentStatus & 0x9) == 0x9)
1458 ret = TRUE;
1460 AcpiOsFree(buf.Pointer);
1461 return (ret);
1465 * Returns true if the battery is actually present and inserted.
1467 BOOLEAN
1468 acpi_BatteryIsPresent(device_t dev)
1470 ACPI_DEVICE_INFO *devinfo;
1471 ACPI_HANDLE h;
1472 ACPI_BUFFER buf;
1473 ACPI_STATUS error;
1474 int ret;
1476 ACPI_ASSERTLOCK;
1478 ret = FALSE;
1479 if ((h = acpi_get_handle(dev)) == NULL)
1480 return (FALSE);
1481 buf.Pointer = NULL;
1482 buf.Length = ACPI_ALLOCATE_BUFFER;
1483 error = AcpiGetObjectInfo(h, &buf);
1484 if (ACPI_FAILURE(error))
1485 return (FALSE);
1486 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1488 /* If no _STA method, must be present */
1489 if ((devinfo->Valid & ACPI_VALID_STA) == 0)
1490 ret = TRUE;
1492 /* Return true for 'present' and 'functioning' */
1493 if ((devinfo->CurrentStatus & 0x19) == 0x19)
1494 ret = TRUE;
1496 AcpiOsFree(buf.Pointer);
1497 return (ret);
1501 * Match a HID string against a handle
1503 BOOLEAN
1504 acpi_MatchHid(ACPI_HANDLE h, char *hid)
1506 ACPI_DEVICE_INFO *devinfo;
1507 ACPI_BUFFER buf;
1508 ACPI_STATUS error;
1509 int ret, i;
1511 ACPI_ASSERTLOCK;
1513 ret = FALSE;
1514 if (hid == NULL || h == NULL)
1515 return (ret);
1516 buf.Pointer = NULL;
1517 buf.Length = ACPI_ALLOCATE_BUFFER;
1518 error = AcpiGetObjectInfo(h, &buf);
1519 if (ACPI_FAILURE(error))
1520 return (ret);
1521 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1523 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1524 strcmp(hid, devinfo->HardwareId.Value) == 0)
1525 ret = TRUE;
1526 else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
1527 for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
1528 if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
1529 ret = TRUE;
1530 break;
1535 AcpiOsFree(buf.Pointer);
1536 return (ret);
1540 * Return the handle of a named object within our scope, ie. that of (parent)
1541 * or one if its parents.
1543 ACPI_STATUS
1544 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1546 ACPI_HANDLE r;
1547 ACPI_STATUS status;
1549 ACPI_ASSERTLOCK;
1551 /* Walk back up the tree to the root */
1552 for (;;) {
1553 status = AcpiGetHandle(parent, path, &r);
1554 if (ACPI_SUCCESS(status)) {
1555 *result = r;
1556 return (AE_OK);
1558 if (status != AE_NOT_FOUND)
1559 return (AE_OK);
1560 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1561 return (AE_NOT_FOUND);
1562 parent = r;
1566 /* Find the difference between two PM tick counts. */
1567 uint32_t
1568 acpi_TimerDelta(uint32_t end, uint32_t start)
1570 uint32_t delta;
1572 if (end >= start)
1573 delta = end - start;
1574 else if ((AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) == 0)
1575 delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1576 else
1577 delta = ((0xFFFFFFFF - start) + end + 1);
1578 return (delta);
1582 * Allocate a buffer with a preset data size.
1584 ACPI_BUFFER *
1585 acpi_AllocBuffer(int size)
1587 ACPI_BUFFER *buf;
1589 buf = kmalloc(size + sizeof(*buf), M_ACPIDEV, M_INTWAIT);
1590 buf->Length = size;
1591 buf->Pointer = (void *)(buf + 1);
1592 return (buf);
1595 ACPI_STATUS
1596 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1598 ACPI_OBJECT arg1;
1599 ACPI_OBJECT_LIST args;
1601 ACPI_ASSERTLOCK;
1603 arg1.Type = ACPI_TYPE_INTEGER;
1604 arg1.Integer.Value = number;
1605 args.Count = 1;
1606 args.Pointer = &arg1;
1608 return (AcpiEvaluateObject(handle, path, &args, NULL));
1612 * Evaluate a path that should return an integer.
1614 ACPI_STATUS
1615 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1617 ACPI_STATUS status;
1618 ACPI_BUFFER buf;
1619 ACPI_OBJECT param;
1621 ACPI_ASSERTLOCK;
1623 if (handle == NULL)
1624 handle = ACPI_ROOT_OBJECT;
1627 * Assume that what we've been pointed at is an Integer object, or
1628 * a method that will return an Integer.
1630 buf.Pointer = &param;
1631 buf.Length = sizeof(param);
1632 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1633 if (ACPI_SUCCESS(status)) {
1634 if (param.Type == ACPI_TYPE_INTEGER)
1635 *number = param.Integer.Value;
1636 else
1637 status = AE_TYPE;
1641 * In some applications, a method that's expected to return an Integer
1642 * may instead return a Buffer (probably to simplify some internal
1643 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
1644 * convert it into an Integer as best we can.
1646 * This is a hack.
1648 if (status == AE_BUFFER_OVERFLOW) {
1649 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1650 status = AE_NO_MEMORY;
1651 } else {
1652 status = AcpiEvaluateObject(handle, path, NULL, &buf);
1653 if (ACPI_SUCCESS(status))
1654 status = acpi_ConvertBufferToInteger(&buf, number);
1655 AcpiOsFree(buf.Pointer);
1658 return (status);
1661 ACPI_STATUS
1662 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1664 ACPI_OBJECT *p;
1665 UINT8 *val;
1666 int i;
1668 p = (ACPI_OBJECT *)bufp->Pointer;
1669 if (p->Type == ACPI_TYPE_INTEGER) {
1670 *number = p->Integer.Value;
1671 return (AE_OK);
1673 if (p->Type != ACPI_TYPE_BUFFER)
1674 return (AE_TYPE);
1675 if (p->Buffer.Length > sizeof(int))
1676 return (AE_BAD_DATA);
1678 *number = 0;
1679 val = p->Buffer.Pointer;
1680 for (i = 0; i < p->Buffer.Length; i++)
1681 *number += val[i] << (i * 8);
1682 return (AE_OK);
1686 * Iterate over the elements of an a package object, calling the supplied
1687 * function for each element.
1689 * XXX possible enhancement might be to abort traversal on error.
1691 ACPI_STATUS
1692 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1693 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1695 ACPI_OBJECT *comp;
1696 int i;
1698 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1699 return (AE_BAD_PARAMETER);
1701 /* Iterate over components */
1702 i = 0;
1703 comp = pkg->Package.Elements;
1704 for (; i < pkg->Package.Count; i++, comp++)
1705 func(comp, arg);
1707 return (AE_OK);
1711 * Find the (index)th resource object in a set.
1713 ACPI_STATUS
1714 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
1716 ACPI_RESOURCE *rp;
1717 int i;
1719 rp = (ACPI_RESOURCE *)buf->Pointer;
1720 i = index;
1721 while (i-- > 0) {
1722 /* Range check */
1723 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1724 return (AE_BAD_PARAMETER);
1726 /* Check for terminator */
1727 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1728 return (AE_NOT_FOUND);
1729 rp = ACPI_NEXT_RESOURCE(rp);
1731 if (resp != NULL)
1732 *resp = rp;
1734 return (AE_OK);
1738 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1740 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1741 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1742 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1743 * resources.
1745 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1747 ACPI_STATUS
1748 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1750 ACPI_RESOURCE *rp;
1751 void *newp;
1753 /* Initialise the buffer if necessary. */
1754 if (buf->Pointer == NULL) {
1755 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1756 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
1757 return (AE_NO_MEMORY);
1758 rp = (ACPI_RESOURCE *)buf->Pointer;
1759 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1760 rp->Length = 0;
1762 if (res == NULL)
1763 return (AE_OK);
1766 * Scan the current buffer looking for the terminator.
1767 * This will either find the terminator or hit the end
1768 * of the buffer and return an error.
1770 rp = (ACPI_RESOURCE *)buf->Pointer;
1771 for (;;) {
1772 /* Range check, don't go outside the buffer */
1773 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
1774 return (AE_BAD_PARAMETER);
1775 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1776 break;
1777 rp = ACPI_NEXT_RESOURCE(rp);
1781 * Check the size of the buffer and expand if required.
1783 * Required size is:
1784 * size of existing resources before terminator +
1785 * size of new resource and header +
1786 * size of terminator.
1788 * Note that this loop should really only run once, unless
1789 * for some reason we are stuffing a *really* huge resource.
1791 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1792 res->Length + ACPI_RS_SIZE_NO_DATA +
1793 ACPI_RS_SIZE_MIN) >= buf->Length) {
1794 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
1795 return (AE_NO_MEMORY);
1796 bcopy(buf->Pointer, newp, buf->Length);
1797 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1798 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1799 AcpiOsFree(buf->Pointer);
1800 buf->Pointer = newp;
1801 buf->Length += buf->Length;
1804 /* Insert the new resource. */
1805 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
1807 /* And add the terminator. */
1808 rp = ACPI_NEXT_RESOURCE(rp);
1809 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1810 rp->Length = 0;
1812 return (AE_OK);
1816 * Set interrupt model.
1818 ACPI_STATUS
1819 acpi_SetIntrModel(int model)
1821 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1824 #define ACPI_MINIMUM_AWAKETIME 5
1826 static void
1827 acpi_sleep_enable(void *arg)
1829 ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1833 * Set the system sleep state
1835 * Currently we support S1-S5 but S4 is only S4BIOS
1837 ACPI_STATUS
1838 acpi_SetSleepState(struct acpi_softc *sc, int state)
1840 ACPI_STATUS status = AE_OK;
1841 UINT8 TypeA;
1842 UINT8 TypeB;
1844 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1845 ACPI_ASSERTLOCK;
1847 /* Avoid reentry if already attempting to suspend. */
1848 if (sc->acpi_sstate != ACPI_STATE_S0)
1849 return_ACPI_STATUS (AE_BAD_PARAMETER);
1851 /* We recently woke up so don't suspend again for a while. */
1852 if (sc->acpi_sleep_disabled)
1853 return_ACPI_STATUS (AE_OK);
1855 switch (state) {
1856 case ACPI_STATE_S1:
1857 case ACPI_STATE_S2:
1858 case ACPI_STATE_S3:
1859 case ACPI_STATE_S4:
1860 status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
1861 if (status == AE_NOT_FOUND) {
1862 device_printf(sc->acpi_dev,
1863 "Sleep state S%d not supported by BIOS\n", state);
1864 break;
1865 } else if (ACPI_FAILURE(status)) {
1866 device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1867 AcpiFormatException(status));
1868 break;
1871 sc->acpi_sstate = state;
1872 sc->acpi_sleep_disabled = 1;
1874 /* Disable all wake GPEs not appropriate for this state. */
1875 acpi_wake_limit_walk(state);
1877 /* Inform all devices that we are going to sleep. */
1878 if (DEVICE_SUSPEND(root_bus) != 0) {
1880 * Re-wake the system.
1882 * XXX note that a better two-pass approach with a 'veto' pass
1883 * followed by a "real thing" pass would be better, but the
1884 * current bus interface does not provide for this.
1886 DEVICE_RESUME(root_bus);
1887 return_ACPI_STATUS (AE_ERROR);
1890 status = AcpiEnterSleepStatePrep(state);
1891 if (ACPI_FAILURE(status)) {
1892 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1893 AcpiFormatException(status));
1894 break;
1897 if (sc->acpi_sleep_delay > 0)
1898 DELAY(sc->acpi_sleep_delay * 1000000);
1900 if (state != ACPI_STATE_S1) {
1901 acpi_sleep_machdep(sc, state);
1902 #if 0
1903 /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
1904 AcpiOsReleaseLock(AcpiGbl_HardwareLock, 1);
1905 #endif
1906 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1907 if (state == ACPI_STATE_S4)
1908 AcpiEnable();
1909 } else {
1910 status = AcpiEnterSleepState((UINT8)state);
1911 if (ACPI_FAILURE(status)) {
1912 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1913 AcpiFormatException(status));
1914 break;
1917 AcpiLeaveSleepState((UINT8)state);
1918 DEVICE_RESUME(root_bus);
1919 sc->acpi_sstate = ACPI_STATE_S0;
1920 acpi_enable_fixed_events(sc);
1921 break;
1922 case ACPI_STATE_S5:
1924 * Shut down cleanly and power off. This will call us back through the
1925 * shutdown handlers.
1927 shutdown_nice(RB_POWEROFF);
1928 break;
1929 case ACPI_STATE_S0:
1930 default:
1931 status = AE_BAD_PARAMETER;
1932 break;
1935 /* Disable a second sleep request for a short period */
1936 if (sc->acpi_sleep_disabled)
1937 callout_reset(&sc->acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
1938 acpi_sleep_enable, sc);
1940 return_ACPI_STATUS (status);
1943 /* Initialize a device's wake GPE. */
1945 acpi_wake_init(device_t dev, int type)
1947 struct acpi_prw_data prw;
1949 /* Check that the device can wake the system. */
1950 if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0)
1951 return (ENXIO);
1953 /* Evaluate _PRW to find the GPE. */
1954 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
1955 return (ENXIO);
1957 /* Set the requested type for the GPE (runtime, wake, or both). */
1958 if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
1959 device_printf(dev, "set GPE type failed\n");
1960 return (ENXIO);
1963 return (0);
1966 /* Enable or disable the device's wake GPE. */
1968 acpi_wake_set_enable(device_t dev, int enable)
1970 struct acpi_prw_data prw;
1971 ACPI_HANDLE handle;
1972 ACPI_STATUS status;
1973 int flags;
1975 /* Make sure the device supports waking the system. */
1976 flags = device_get_flags(dev);
1977 handle = acpi_get_handle(dev);
1978 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1979 return (ENXIO);
1981 /* Evaluate _PRW to find the GPE. */
1982 if (acpi_parse_prw(handle, &prw) != 0)
1983 return (ENXIO);
1985 if (enable) {
1986 status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1987 if (ACPI_FAILURE(status)) {
1988 device_printf(dev, "enable wake failed\n");
1989 return (ENXIO);
1991 device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
1992 } else {
1993 status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1994 if (ACPI_FAILURE(status)) {
1995 device_printf(dev, "disable wake failed\n");
1996 return (ENXIO);
1998 device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2001 return (0);
2004 /* Configure a device's GPE appropriately for the new sleep state. */
2006 acpi_wake_sleep_prep(device_t dev, int sstate)
2008 struct acpi_prw_data prw;
2009 ACPI_HANDLE handle;
2010 int flags;
2012 /* Check that this is an ACPI device and get its GPE. */
2013 flags = device_get_flags(dev);
2014 handle = acpi_get_handle(dev);
2015 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
2016 return (ENXIO);
2018 /* Evaluate _PRW to find the GPE. */
2019 if (acpi_parse_prw(handle, &prw) != 0)
2020 return (ENXIO);
2023 * TBD: All Power Resources referenced by elements 2 through N
2024 * of the _PRW object are put into the ON state.
2028 * If the user requested that this device wake the system and the next
2029 * sleep state is valid for this GPE, enable it and the device's wake
2030 * capability. The sleep state must be less than (i.e., higher power)
2031 * or equal to the value specified by _PRW. Return early, leaving
2032 * the appropriate power resources enabled.
2034 if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 &&
2035 sstate <= prw.lowest_wake) {
2036 if (bootverbose)
2037 device_printf(dev, "wake_prep enabled gpe %#x for state %d\n",
2038 prw.gpe_bit, sstate);
2039 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2040 acpi_SetInteger(handle, "_PSW", 1);
2041 return (0);
2045 * If the device wake was disabled or this sleep state is too low for
2046 * this device, disable its wake capability and GPE.
2048 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2049 acpi_SetInteger(handle, "_PSW", 0);
2050 if (bootverbose)
2051 device_printf(dev, "wake_prep disabled gpe %#x for state %d\n",
2052 prw.gpe_bit, sstate);
2055 * TBD: All Power Resources referenced by elements 2 through N
2056 * of the _PRW object are put into the OFF state.
2059 return (0);
2062 /* Re-enable GPEs after wake. */
2064 acpi_wake_run_prep(device_t dev)
2066 struct acpi_prw_data prw;
2067 ACPI_HANDLE handle;
2068 int flags;
2070 /* Check that this is an ACPI device and get its GPE. */
2071 flags = device_get_flags(dev);
2072 handle = acpi_get_handle(dev);
2073 if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
2074 return (ENXIO);
2076 /* Evaluate _PRW to find the GPE. */
2077 if (acpi_parse_prw(handle, &prw) != 0)
2078 return (ENXIO);
2081 * TBD: Be sure all Power Resources referenced by elements 2 through N
2082 * of the _PRW object are in the ON state.
2085 /* Disable wake capability and if the user requested, enable the GPE. */
2086 acpi_SetInteger(handle, "_PSW", 0);
2087 if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0)
2088 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2089 return (0);
2092 static ACPI_STATUS
2093 acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status)
2095 struct acpi_prw_data prw;
2096 int *sstate;
2098 /* It's ok not to have _PRW if the device can't wake the system. */
2099 if (acpi_parse_prw(h, &prw) != 0)
2100 return (AE_OK);
2102 sstate = (int *)context;
2103 if (*sstate > prw.lowest_wake)
2104 AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2106 return (AE_OK);
2109 /* Walk all system devices, disabling them if necessary for sstate. */
2110 static int
2111 acpi_wake_limit_walk(int sstate)
2113 ACPI_HANDLE sb_handle;
2115 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2116 AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100,
2117 acpi_wake_limit, &sstate, NULL);
2118 return (0);
2121 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
2122 static int
2123 acpi_wake_sysctl_walk(device_t dev)
2125 int error, i, numdevs;
2126 device_t *devlist;
2127 device_t child;
2129 error = device_get_children(dev, &devlist, &numdevs);
2130 if (error != 0 || numdevs == 0)
2131 return (error);
2132 for (i = 0; i < numdevs; i++) {
2133 child = devlist[i];
2134 if (!device_is_attached(child))
2135 continue;
2136 if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) {
2137 #ifdef dfly_notyet
2138 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
2139 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
2140 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
2141 acpi_wake_set_sysctl, "I", "Device set to wake the system");
2142 #endif /* dfly_notyet */
2144 acpi_wake_sysctl_walk(child);
2146 kfree(devlist, M_TEMP);
2148 return (0);
2151 #ifdef dfly_notyet
2152 /* Enable or disable wake from userland. */
2153 static int
2154 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
2156 int enable, error;
2157 device_t dev;
2159 dev = (device_t)arg1;
2160 enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
2162 error = sysctl_handle_int(oidp, &enable, 0, req);
2163 if (error != 0 || req->newptr == NULL)
2164 return (error);
2165 if (enable != 0 && enable != 1)
2166 return (EINVAL);
2168 return (acpi_wake_set_enable(dev, enable));
2170 #endif /* dfly_notyet */
2172 /* Parse a device's _PRW into a structure. */
2173 static int
2174 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2176 ACPI_STATUS status;
2177 ACPI_BUFFER prw_buffer;
2178 ACPI_OBJECT *res, *res2;
2179 int error;
2181 if (h == NULL || prw == NULL)
2182 return (EINVAL);
2185 * The _PRW object (7.2.9) is only required for devices that have the
2186 * ability to wake the system from a sleeping state.
2188 error = EINVAL;
2189 prw_buffer.Pointer = NULL;
2190 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2191 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2192 if (ACPI_FAILURE(status))
2193 return (ENOENT);
2194 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2195 if (res == NULL)
2196 return (ENOENT);
2197 if (!ACPI_PKG_VALID(res, 2))
2198 goto out;
2201 * Element 1 of the _PRW object:
2202 * The lowest power system sleeping state that can be entered while still
2203 * providing wake functionality. The sleeping state being entered must
2204 * be less than (i.e., higher power) or equal to this value.
2206 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2207 goto out;
2210 * Element 0 of the _PRW object:
2212 switch (res->Package.Elements[0].Type) {
2213 case ACPI_TYPE_INTEGER:
2215 * If the data type of this package element is numeric, then this
2216 * _PRW package element is the bit index in the GPEx_EN, in the
2217 * GPE blocks described in the FADT, of the enable bit that is
2218 * enabled for the wake event.
2220 prw->gpe_handle = NULL;
2221 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2222 error = 0;
2223 break;
2224 case ACPI_TYPE_PACKAGE:
2226 * If the data type of this package element is a package, then this
2227 * _PRW package element is itself a package containing two
2228 * elements. The first is an object reference to the GPE Block
2229 * device that contains the GPE that will be triggered by the wake
2230 * event. The second element is numeric and it contains the bit
2231 * index in the GPEx_EN, in the GPE Block referenced by the
2232 * first element in the package, of the enable bit that is enabled for
2233 * the wake event.
2235 * For example, if this field is a package then it is of the form:
2236 * Package() {\_SB.PCI0.ISA.GPE, 2}
2238 res2 = &res->Package.Elements[0];
2239 if (!ACPI_PKG_VALID(res2, 2))
2240 goto out;
2241 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2242 if (prw->gpe_handle == NULL)
2243 goto out;
2244 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2245 goto out;
2246 error = 0;
2247 break;
2248 default:
2249 goto out;
2252 /* XXX No power resource handling yet. */
2253 prw->power_res = NULL;
2255 out:
2256 if (prw_buffer.Pointer != NULL)
2257 AcpiOsFree(prw_buffer.Pointer);
2258 return (error);
2262 * Enable/Disable ACPI
2264 ACPI_STATUS
2265 acpi_Enable(struct acpi_softc *sc)
2267 ACPI_STATUS status;
2268 u_int32_t flags;
2270 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2271 ACPI_ASSERTLOCK;
2273 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
2274 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
2275 if (!sc->acpi_enabled)
2276 status = AcpiEnableSubsystem(flags);
2277 else
2278 status = AE_OK;
2280 if (status == AE_OK)
2281 sc->acpi_enabled = 1;
2283 return_ACPI_STATUS (status);
2286 ACPI_STATUS
2287 acpi_Disable(struct acpi_softc *sc)
2289 ACPI_STATUS status;
2291 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2292 ACPI_ASSERTLOCK;
2294 if (sc->acpi_enabled)
2295 status = AcpiDisable();
2296 else
2297 status = AE_OK;
2299 if (status == AE_OK)
2300 sc->acpi_enabled = 0;
2302 return_ACPI_STATUS (status);
2306 * ACPI Event Handlers
2309 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
2311 static void
2312 acpi_system_eventhandler_sleep(void *arg, int state)
2314 ACPI_LOCK_DECL;
2315 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2317 ACPI_LOCK;
2318 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2319 acpi_SetSleepState((struct acpi_softc *)arg, state);
2320 ACPI_UNLOCK;
2321 return_VOID;
2324 static void
2325 acpi_system_eventhandler_wakeup(void *arg, int state)
2327 ACPI_LOCK_DECL;
2328 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2330 /* Well, what to do? :-) */
2332 ACPI_LOCK;
2333 ACPI_UNLOCK;
2335 return_VOID;
2339 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
2341 UINT32
2342 acpi_event_power_button_sleep(void *context)
2344 struct acpi_softc *sc = (struct acpi_softc *)context;
2346 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2348 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
2350 return_VALUE (ACPI_INTERRUPT_HANDLED);
2353 UINT32
2354 acpi_event_power_button_wake(void *context)
2356 struct acpi_softc *sc = (struct acpi_softc *)context;
2358 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2360 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
2362 return_VALUE (ACPI_INTERRUPT_HANDLED);
2365 UINT32
2366 acpi_event_sleep_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_sleep_button_sx);
2374 return_VALUE (ACPI_INTERRUPT_HANDLED);
2377 UINT32
2378 acpi_event_sleep_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_sleep_button_sx);
2386 return_VALUE (ACPI_INTERRUPT_HANDLED);
2390 * XXX This is kinda ugly, and should not be here.
2392 struct acpi_staticbuf {
2393 ACPI_BUFFER buffer;
2394 char data[512];
2397 char *
2398 acpi_name(ACPI_HANDLE handle)
2400 static struct acpi_staticbuf buf;
2402 ACPI_ASSERTLOCK;
2404 buf.buffer.Length = 512;
2405 buf.buffer.Pointer = &buf.data[0];
2407 if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
2408 return (buf.buffer.Pointer);
2410 return ("(unknown path)");
2414 * Debugging/bug-avoidance. Avoid trying to fetch info on various
2415 * parts of the namespace.
2418 acpi_avoid(ACPI_HANDLE handle)
2420 char *cp, *env, *np;
2421 int len;
2423 np = acpi_name(handle);
2424 if (*np == '\\')
2425 np++;
2426 if ((env = kgetenv("debug.acpi.avoid.paths")) == NULL &&
2427 (env = kgetenv("debug.acpi.avoid")) == NULL)
2428 return (0);
2430 /* Scan the avoid list checking for a match */
2431 cp = env;
2432 for (;;) {
2433 while ((*cp != 0) && isspace(*cp))
2434 cp++;
2435 if (*cp == 0)
2436 break;
2437 len = 0;
2438 while ((cp[len] != 0) && !isspace(cp[len]))
2439 len++;
2440 if (!strncmp(cp, np, len)) {
2441 kfreeenv(env);
2442 return(1);
2444 cp += len;
2446 kfreeenv(env);
2448 return (0);
2452 * Debugging/bug-avoidance. Disable ACPI subsystem components. Note that
2453 * some components may be disabled by default and can only be enabled
2454 * via acpi_enabled() (debug.acpi.enabled).
2457 acpi_disabled(char *subsys)
2459 char *cp, *env;
2460 int len;
2462 if ((env = kgetenv("debug.acpi.disabled")) == NULL)
2463 return (0);
2464 if (strcmp(env, "all") == 0) {
2465 kfreeenv(env);
2466 return (1);
2469 /* Scan the disable list, checking for a match. */
2470 cp = env;
2471 for (;;) {
2472 while (*cp != '\0' && isspace(*cp))
2473 cp++;
2474 if (*cp == '\0')
2475 break;
2476 len = 0;
2477 while (cp[len] != '\0' && !isspace(cp[len]))
2478 len++;
2479 if (strncmp(cp, subsys, len) == 0) {
2480 kfreeenv(env);
2481 return (1);
2483 cp += len;
2485 kfreeenv(env);
2487 return (0);
2491 * Debugging/bug-avoidance. Enable ACPI subsystem components. Most
2492 * components are enabled by default. The ones that are not have to be
2493 * enabled via debug.acpi.enabled.
2496 acpi_enabled(char *subsys)
2498 char *cp, *env;
2499 int len;
2501 if ((env = kgetenv("debug.acpi.enabled")) == NULL)
2502 return (0);
2503 if (strcmp(env, "all") == 0) {
2504 kfreeenv(env);
2505 return (1);
2508 /* Scan the enable list, checking for a match. */
2509 cp = env;
2510 for (;;) {
2511 while (*cp != '\0' && isspace(*cp))
2512 cp++;
2513 if (*cp == '\0')
2514 break;
2515 len = 0;
2516 while (cp[len] != '\0' && !isspace(cp[len]))
2517 len++;
2518 if (strncmp(cp, subsys, len) == 0) {
2519 kfreeenv(env);
2520 return (1);
2522 cp += len;
2524 kfreeenv(env);
2526 return (0);
2530 * Control interface.
2532 * We multiplex ioctls for all participating ACPI devices here. Individual
2533 * drivers wanting to be accessible via /dev/acpi should use the
2534 * register/deregister interface to make their handlers visible.
2536 struct acpi_ioctl_hook
2538 TAILQ_ENTRY(acpi_ioctl_hook) link;
2539 u_long cmd;
2540 acpi_ioctl_fn fn;
2541 void *arg;
2544 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
2545 static int acpi_ioctl_hooks_initted;
2548 * Register an ioctl handler.
2551 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
2553 struct acpi_ioctl_hook *hp;
2555 hp = kmalloc(sizeof(*hp), M_ACPIDEV, M_INTWAIT);
2556 hp->cmd = cmd;
2557 hp->fn = fn;
2558 hp->arg = arg;
2559 if (acpi_ioctl_hooks_initted == 0) {
2560 TAILQ_INIT(&acpi_ioctl_hooks);
2561 acpi_ioctl_hooks_initted = 1;
2563 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
2564 return (0);
2568 * Deregister an ioctl handler.
2570 void
2571 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
2573 struct acpi_ioctl_hook *hp;
2575 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
2576 if ((hp->cmd == cmd) && (hp->fn == fn))
2577 break;
2579 if (hp != NULL) {
2580 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
2581 kfree(hp, M_ACPIDEV);
2585 static int
2586 acpiopen(struct dev_open_args *ap)
2588 return (0);
2591 static int
2592 acpiclose(struct dev_close_args *ap)
2594 return (0);
2597 static int
2598 acpiioctl(struct dev_ioctl_args *ap)
2600 struct acpi_softc *sc;
2601 struct acpi_ioctl_hook *hp;
2602 int error, xerror, state;
2603 ACPI_LOCK_DECL;
2605 ACPI_LOCK;
2607 error = state = 0;
2608 sc = ap->a_head.a_dev->si_drv1;
2611 * Scan the list of registered ioctls, looking for handlers.
2613 if (acpi_ioctl_hooks_initted) {
2614 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2615 if (hp->cmd == ap->a_cmd) {
2616 xerror = hp->fn(ap->a_cmd, ap->a_data, hp->arg);
2617 if (xerror != 0)
2618 error = xerror;
2619 goto out;
2625 * Core ioctls are not permitted for non-writable user.
2626 * Currently, other ioctls just fetch information.
2627 * Not changing system behavior.
2629 if((ap->a_fflag & FWRITE) == 0) {
2630 error = EPERM;
2631 goto out;
2634 /* Core system ioctls. */
2635 switch (ap->a_cmd) {
2636 case ACPIIO_ENABLE:
2637 if (ACPI_FAILURE(acpi_Enable(sc)))
2638 error = ENXIO;
2639 break;
2640 case ACPIIO_DISABLE:
2641 if (ACPI_FAILURE(acpi_Disable(sc)))
2642 error = ENXIO;
2643 break;
2644 case ACPIIO_SETSLPSTATE:
2645 if (!sc->acpi_enabled) {
2646 error = ENXIO;
2647 break;
2649 state = *(int *)ap->a_data;
2650 if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) {
2651 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
2652 error = EINVAL;
2653 } else {
2654 error = EINVAL;
2656 break;
2657 default:
2658 if (error == 0)
2659 error = EINVAL;
2660 break;
2663 out:
2664 ACPI_UNLOCK;
2665 return (error);
2668 static int
2669 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2671 char sleep_state[4];
2672 char buf[16];
2673 int error;
2674 UINT8 state, TypeA, TypeB;
2676 buf[0] = '\0';
2677 for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) {
2678 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2679 ksprintf(sleep_state, "S%d ", state);
2680 strcat(buf, sleep_state);
2683 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2684 return (error);
2687 static int
2688 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2690 char sleep_state[10];
2691 int error;
2692 u_int new_state, old_state;
2694 old_state = *(u_int *)oidp->oid_arg1;
2695 if (old_state > ACPI_S_STATES_MAX + 1) {
2696 strcpy(sleep_state, "unknown");
2697 } else {
2698 bzero(sleep_state, sizeof(sleep_state));
2699 strncpy(sleep_state, sleep_state_names[old_state],
2700 sizeof(sleep_state_names[old_state]));
2702 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
2703 if (error == 0 && req->newptr != NULL) {
2704 new_state = ACPI_STATE_S0;
2705 for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
2706 if (strncmp(sleep_state, sleep_state_names[new_state],
2707 sizeof(sleep_state)) == 0)
2708 break;
2710 if (new_state <= ACPI_S_STATES_MAX + 1) {
2711 if (new_state != old_state)
2712 *(u_int *)oidp->oid_arg1 = new_state;
2713 } else {
2714 error = EINVAL;
2718 return (error);
2721 /* Inform devctl(4) when we receive a Notify. */
2722 void
2723 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
2725 char notify_buf[16];
2726 ACPI_BUFFER handle_buf;
2727 ACPI_STATUS status;
2729 if (subsystem == NULL)
2730 return;
2732 handle_buf.Pointer = NULL;
2733 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
2734 status = AcpiNsHandleToPathname(h, &handle_buf);
2735 if (ACPI_FAILURE(status))
2736 return;
2737 ksnprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
2738 #if 0
2739 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
2740 #endif
2741 AcpiOsFree(handle_buf.Pointer);
2744 #ifdef ACPI_DEBUG
2746 * Support for parsing debug options from the kernel environment.
2748 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
2749 * by specifying the names of the bits in the debug.acpi.layer and
2750 * debug.acpi.level environment variables. Bits may be unset by
2751 * prefixing the bit name with !.
2753 struct debugtag
2755 char *name;
2756 UINT32 value;
2759 static struct debugtag dbg_layer[] = {
2760 {"ACPI_UTILITIES", ACPI_UTILITIES},
2761 {"ACPI_HARDWARE", ACPI_HARDWARE},
2762 {"ACPI_EVENTS", ACPI_EVENTS},
2763 {"ACPI_TABLES", ACPI_TABLES},
2764 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
2765 {"ACPI_PARSER", ACPI_PARSER},
2766 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
2767 {"ACPI_EXECUTER", ACPI_EXECUTER},
2768 {"ACPI_RESOURCES", ACPI_RESOURCES},
2769 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
2770 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
2771 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
2772 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
2774 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
2775 {"ACPI_BATTERY", ACPI_BATTERY},
2776 {"ACPI_BUS", ACPI_BUS},
2777 {"ACPI_BUTTON", ACPI_BUTTON},
2778 {"ACPI_EC", ACPI_EC},
2779 {"ACPI_FAN", ACPI_FAN},
2780 {"ACPI_POWERRES", ACPI_POWERRES},
2781 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
2782 {"ACPI_THERMAL", ACPI_THERMAL},
2783 {"ACPI_TIMER", ACPI_TIMER},
2784 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
2785 {NULL, 0}
2788 static struct debugtag dbg_level[] = {
2789 {"ACPI_LV_ERROR", ACPI_LV_ERROR},
2790 {"ACPI_LV_WARN", ACPI_LV_WARN},
2791 {"ACPI_LV_INIT", ACPI_LV_INIT},
2792 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
2793 {"ACPI_LV_INFO", ACPI_LV_INFO},
2794 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
2796 /* Trace verbosity level 1 [Standard Trace Level] */
2797 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
2798 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
2799 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
2800 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
2801 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
2802 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
2803 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
2804 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
2805 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
2806 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
2807 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
2808 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
2809 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
2810 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
2811 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
2813 /* Trace verbosity level 2 [Function tracing and memory allocation] */
2814 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
2815 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
2816 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
2817 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
2818 {"ACPI_LV_ALL", ACPI_LV_ALL},
2820 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2821 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
2822 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
2823 {"ACPI_LV_IO", ACPI_LV_IO},
2824 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
2825 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
2827 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
2828 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
2829 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
2830 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
2831 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
2832 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
2833 {NULL, 0}
2836 static void
2837 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
2839 char *ep;
2840 int i, l;
2841 int set;
2843 while (*cp) {
2844 if (isspace(*cp)) {
2845 cp++;
2846 continue;
2848 ep = cp;
2849 while (*ep && !isspace(*ep))
2850 ep++;
2851 if (*cp == '!') {
2852 set = 0;
2853 cp++;
2854 if (cp == ep)
2855 continue;
2856 } else {
2857 set = 1;
2859 l = ep - cp;
2860 for (i = 0; tag[i].name != NULL; i++) {
2861 if (!strncmp(cp, tag[i].name, l)) {
2862 if (set)
2863 *flag |= tag[i].value;
2864 else
2865 *flag &= ~tag[i].value;
2868 cp = ep;
2873 * Warning: also called in early boot, before any allocators
2874 * are working.
2876 static void
2877 acpi_set_debugging(void *junk)
2879 char *layer, *level;
2881 if (cold) {
2882 AcpiDbgLayer = 0;
2883 AcpiDbgLevel = 0;
2886 layer = kgetenv("debug.acpi.layer");
2887 level = kgetenv("debug.acpi.level");
2888 if (layer == NULL && level == NULL)
2889 return;
2891 kprintf("ACPI set debug");
2892 if (layer != NULL) {
2893 if (strcmp("NONE", layer) != 0)
2894 kprintf(" layer '%s'", layer);
2895 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
2896 kfreeenv(layer);
2898 if (level != NULL) {
2899 if (strcmp("NONE", level) != 0)
2900 kprintf(" level '%s'", level);
2901 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
2902 kfreeenv(level);
2904 kprintf("\n");
2906 SYSINIT(acpi_debugging, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
2907 acpi_set_debugging, NULL);
2909 static int
2910 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
2912 int error, *dbg;
2913 struct debugtag *tag;
2914 struct sbuf sb;
2916 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
2917 return (ENOMEM);
2918 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
2919 tag = &dbg_layer[0];
2920 dbg = &AcpiDbgLayer;
2921 } else {
2922 tag = &dbg_level[0];
2923 dbg = &AcpiDbgLevel;
2926 /* Get old values if this is a get request. */
2927 if (*dbg == 0) {
2928 sbuf_cpy(&sb, "NONE");
2929 } else if (req->newptr == NULL) {
2930 for (; tag->name != NULL; tag++) {
2931 if ((*dbg & tag->value) == tag->value)
2932 sbuf_printf(&sb, "%s ", tag->name);
2935 sbuf_trim(&sb);
2936 sbuf_finish(&sb);
2938 /* Copy out the old values to the user. */
2939 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
2940 sbuf_delete(&sb);
2942 /* If the user is setting a string, parse it. */
2943 if (error == 0 && req->newptr != NULL) {
2944 *dbg = 0;
2945 ksetenv(oidp->oid_arg1, req->newptr);
2946 acpi_set_debugging(NULL);
2949 return (error);
2951 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
2952 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
2953 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
2954 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
2955 #endif
2957 static int
2958 acpi_pm_func(u_long cmd, void *arg, ...)
2960 int state, acpi_state;
2961 int error;
2962 struct acpi_softc *sc;
2963 va_list ap;
2965 error = 0;
2966 switch (cmd) {
2967 case POWER_CMD_SUSPEND:
2968 sc = (struct acpi_softc *)arg;
2969 if (sc == NULL) {
2970 error = EINVAL;
2971 goto out;
2974 va_start(ap, arg);
2975 state = va_arg(ap, int);
2976 va_end(ap);
2978 switch (state) {
2979 case POWER_SLEEP_STATE_STANDBY:
2980 acpi_state = sc->acpi_standby_sx;
2981 break;
2982 case POWER_SLEEP_STATE_SUSPEND:
2983 acpi_state = sc->acpi_suspend_sx;
2984 break;
2985 case POWER_SLEEP_STATE_HIBERNATE:
2986 acpi_state = ACPI_STATE_S4;
2987 break;
2988 default:
2989 error = EINVAL;
2990 goto out;
2993 acpi_SetSleepState(sc, acpi_state);
2994 break;
2995 default:
2996 error = EINVAL;
2997 goto out;
3000 out:
3001 return (error);
3004 static void
3005 acpi_pm_register(void *arg)
3007 if (!cold || resource_disabled("acpi", 0))
3008 return;
3010 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3013 SYSINIT(power, SI_BOOT2_KLD, SI_ORDER_ANY, acpi_pm_register, 0);