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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/fcntl.h>
38 #include <sys/malloc.h>
39 #include <sys/module.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>
49 #include <sys/sched.h>
51 #include <sys/timetc.h>
53 #if defined(__i386__) || defined(__amd64__)
54 #include <machine/pci_cfgreg.h>
56 #include <machine/resource.h>
57 #include <machine/bus.h>
59 #include <isa/isavar.h>
60 #include <isa/pnpvar.h>
62 #include <contrib/dev/acpica/include/acpi.h>
63 #include <contrib/dev/acpica/include/accommon.h>
64 #include <contrib/dev/acpica/include/acnamesp.h>
66 #include <dev/acpica/acpivar.h>
67 #include <dev/acpica/acpiio.h>
69 #include <vm/vm_param.h>
71 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
73 /* Hooks for the ACPI CA debugging infrastructure */
74 #define _COMPONENT ACPI_BUS
75 ACPI_MODULE_NAME("ACPI")
77 static d_open_t acpiopen;
78 static d_close_t acpiclose;
79 static d_ioctl_t acpiioctl;
81 static struct cdevsw acpi_cdevsw = {
82 .d_version = D_VERSION,
89 struct acpi_interface {
94 /* Global mutex for locking access to the ACPI subsystem. */
95 struct mtx acpi_mutex;
97 /* Bitmap of device quirks. */
100 /* Supported sleep states. */
101 static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT];
103 static int acpi_modevent(struct module *mod, int event, void *junk);
104 static int acpi_probe(device_t dev);
105 static int acpi_attach(device_t dev);
106 static int acpi_suspend(device_t dev);
107 static int acpi_resume(device_t dev);
108 static int acpi_shutdown(device_t dev);
109 static device_t acpi_add_child(device_t bus, u_int order, const char *name,
111 static int acpi_print_child(device_t bus, device_t child);
112 static void acpi_probe_nomatch(device_t bus, device_t child);
113 static void acpi_driver_added(device_t dev, driver_t *driver);
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 struct resource_list *acpi_get_rlist(device_t dev, device_t child);
119 static void acpi_reserve_resources(device_t dev);
120 static int acpi_sysres_alloc(device_t dev);
121 static int acpi_set_resource(device_t dev, device_t child, int type,
122 int rid, u_long start, u_long count);
123 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
124 int type, int *rid, u_long start, u_long end,
125 u_long count, u_int flags);
126 static int acpi_adjust_resource(device_t bus, device_t child, int type,
127 struct resource *r, u_long start, u_long end);
128 static int acpi_release_resource(device_t bus, device_t child, int type,
129 int rid, struct resource *r);
130 static void acpi_delete_resource(device_t bus, device_t child, int type,
132 static uint32_t acpi_isa_get_logicalid(device_t dev);
133 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
134 static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids);
135 static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
136 ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
138 static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
139 void *context, void **retval);
140 static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
141 int max_depth, acpi_scan_cb_t user_fn, void *arg);
142 static int acpi_set_powerstate(device_t child, int state);
143 static int acpi_isa_pnp_probe(device_t bus, device_t child,
144 struct isa_pnp_id *ids);
145 static void acpi_probe_children(device_t bus);
146 static void acpi_probe_order(ACPI_HANDLE handle, int *order);
147 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
148 void *context, void **status);
149 static void acpi_sleep_enable(void *arg);
150 static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
151 static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
152 static void acpi_shutdown_final(void *arg, int howto);
153 static void acpi_enable_fixed_events(struct acpi_softc *sc);
154 static BOOLEAN acpi_has_hid(ACPI_HANDLE handle);
155 static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
156 static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
157 static int acpi_wake_prep_walk(int sstate);
158 static int acpi_wake_sysctl_walk(device_t dev);
159 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
160 static void acpi_system_eventhandler_sleep(void *arg, int state);
161 static void acpi_system_eventhandler_wakeup(void *arg, int state);
162 static int acpi_sname2sstate(const char *sname);
163 static const char *acpi_sstate2sname(int sstate);
164 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
165 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
166 static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
167 static int acpi_pm_func(u_long cmd, void *arg, ...);
168 static int acpi_child_location_str_method(device_t acdev, device_t child,
169 char *buf, size_t buflen);
170 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
171 char *buf, size_t buflen);
172 #if defined(__i386__) || defined(__amd64__)
173 static void acpi_enable_pcie(void);
175 static void acpi_hint_device_unit(device_t acdev, device_t child,
176 const char *name, int *unitp);
177 static void acpi_reset_interfaces(device_t dev);
179 static device_method_t acpi_methods[] = {
180 /* Device interface */
181 DEVMETHOD(device_probe, acpi_probe),
182 DEVMETHOD(device_attach, acpi_attach),
183 DEVMETHOD(device_shutdown, acpi_shutdown),
184 DEVMETHOD(device_detach, bus_generic_detach),
185 DEVMETHOD(device_suspend, acpi_suspend),
186 DEVMETHOD(device_resume, acpi_resume),
189 DEVMETHOD(bus_add_child, acpi_add_child),
190 DEVMETHOD(bus_print_child, acpi_print_child),
191 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch),
192 DEVMETHOD(bus_driver_added, acpi_driver_added),
193 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
194 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
195 DEVMETHOD(bus_get_resource_list, acpi_get_rlist),
196 DEVMETHOD(bus_set_resource, acpi_set_resource),
197 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
198 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
199 DEVMETHOD(bus_adjust_resource, acpi_adjust_resource),
200 DEVMETHOD(bus_release_resource, acpi_release_resource),
201 DEVMETHOD(bus_delete_resource, acpi_delete_resource),
202 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
203 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
204 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
205 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
206 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
207 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
208 DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit),
211 DEVMETHOD(acpi_id_probe, acpi_device_id_probe),
212 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj),
213 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep),
214 DEVMETHOD(acpi_scan_children, acpi_device_scan_children),
217 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
222 static driver_t acpi_driver = {
225 sizeof(struct acpi_softc),
228 static devclass_t acpi_devclass;
229 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
230 MODULE_VERSION(acpi, 1);
232 ACPI_SERIAL_DECL(acpi, "ACPI root bus");
234 /* Local pools for managing system resources for ACPI child devices. */
235 static struct rman acpi_rman_io, acpi_rman_mem;
237 #define ACPI_MINIMUM_AWAKETIME 5
239 /* Holds the description of the acpi0 device. */
240 static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
242 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
243 static char acpi_ca_version[12];
244 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
245 acpi_ca_version, 0, "Version of Intel ACPI-CA");
248 * Allow overriding _OSI methods.
250 static char acpi_install_interface[256];
251 TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
252 sizeof(acpi_install_interface));
253 static char acpi_remove_interface[256];
254 TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
255 sizeof(acpi_remove_interface));
258 * Allow override of whether methods execute in parallel or not.
259 * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
260 * errors for AML that really can't handle parallel method execution.
261 * It is off by default since this breaks recursive methods and
262 * some IBMs use such code.
264 static int acpi_serialize_methods;
265 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
267 /* Allow users to dump Debug objects without ACPI debugger. */
268 static int acpi_debug_objects;
269 TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
270 SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
271 CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I",
272 "Enable Debug objects");
274 /* Allow the interpreter to ignore common mistakes in BIOS. */
275 static int acpi_interpreter_slack = 1;
276 TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
277 SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
278 &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
280 /* Reset system clock while resuming. XXX Remove once tested. */
281 static int acpi_reset_clock = 1;
282 TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock);
283 SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW,
284 &acpi_reset_clock, 1, "Reset system clock while resuming.");
286 /* Allow users to override quirks. */
287 TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
289 static int acpi_susp_bounce;
290 SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
291 &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
294 * ACPI can only be loaded as a module by the loader; activating it after
295 * system bootstrap time is not useful, and can be fatal to the system.
296 * It also cannot be unloaded, since the entire system bus hierarchy hangs
300 acpi_modevent(struct module *mod, int event, void *junk)
305 printf("The ACPI driver cannot be loaded after boot.\n");
310 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
320 * Perform early initialization.
325 static int started = 0;
329 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
331 /* Only run the startup code once. The MADT driver also calls this. */
333 return_VALUE (AE_OK);
337 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
338 * if more tables exist.
340 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
341 printf("ACPI: Table initialisation failed: %s\n",
342 AcpiFormatException(status));
343 return_VALUE (status);
346 /* Set up any quirks we have for this system. */
347 if (acpi_quirks == ACPI_Q_OK)
348 acpi_table_quirks(&acpi_quirks);
350 /* If the user manually set the disabled hint to 0, force-enable ACPI. */
351 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
352 acpi_quirks &= ~ACPI_Q_BROKEN;
353 if (acpi_quirks & ACPI_Q_BROKEN) {
354 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n");
358 return_VALUE (status);
362 * Detect ACPI and perform early initialisation.
367 ACPI_TABLE_RSDP *rsdp;
368 ACPI_TABLE_HEADER *rsdt;
369 ACPI_PHYSICAL_ADDRESS paddr;
372 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
377 /* Check that we haven't been disabled with a hint. */
378 if (resource_disabled("acpi", 0))
381 /* Check for other PM systems. */
382 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
383 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
384 printf("ACPI identify failed, other PM system enabled.\n");
388 /* Initialize root tables. */
389 if (ACPI_FAILURE(acpi_Startup())) {
390 printf("ACPI: Try disabling either ACPI or apic support.\n");
394 if ((paddr = AcpiOsGetRootPointer()) == 0 ||
395 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
397 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
398 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
400 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
401 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
403 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
405 sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
406 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
409 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
413 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
415 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
421 * Fetch some descriptive data from ACPI to put in our attach message.
424 acpi_probe(device_t dev)
427 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
429 device_set_desc(dev, acpi_desc);
435 acpi_attach(device_t dev)
437 struct acpi_softc *sc;
444 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
446 sc = device_get_softc(dev);
448 callout_init(&sc->susp_force_to, TRUE);
452 /* Initialize resource manager. */
453 acpi_rman_io.rm_type = RMAN_ARRAY;
454 acpi_rman_io.rm_start = 0;
455 acpi_rman_io.rm_end = 0xffff;
456 acpi_rman_io.rm_descr = "ACPI I/O ports";
457 if (rman_init(&acpi_rman_io) != 0)
458 panic("acpi rman_init IO ports failed");
459 acpi_rman_mem.rm_type = RMAN_ARRAY;
460 acpi_rman_mem.rm_start = 0;
461 acpi_rman_mem.rm_end = ~0ul;
462 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
463 if (rman_init(&acpi_rman_mem) != 0)
464 panic("acpi rman_init memory failed");
466 /* Initialise the ACPI mutex */
467 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
470 * Set the globals from our tunables. This is needed because ACPI-CA
471 * uses UINT8 for some values and we have no tunable_byte.
473 AcpiGbl_AllMethodsSerialized = acpi_serialize_methods ? TRUE : FALSE;
474 AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
475 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
479 * Disable all debugging layers and levels.
485 /* Start up the ACPI CA subsystem. */
486 status = AcpiInitializeSubsystem();
487 if (ACPI_FAILURE(status)) {
488 device_printf(dev, "Could not initialize Subsystem: %s\n",
489 AcpiFormatException(status));
493 /* Override OS interfaces if the user requested. */
494 acpi_reset_interfaces(dev);
496 /* Load ACPI name space. */
497 status = AcpiLoadTables();
498 if (ACPI_FAILURE(status)) {
499 device_printf(dev, "Could not load Namespace: %s\n",
500 AcpiFormatException(status));
504 #if defined(__i386__) || defined(__amd64__)
505 /* Handle MCFG table if present. */
510 * Note that some systems (specifically, those with namespace evaluation
511 * issues that require the avoidance of parts of the namespace) must
512 * avoid running _INI and _STA on everything, as well as dodging the final
515 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
517 * XXX We should arrange for the object init pass after we have attached
518 * all our child devices, but on many systems it works here.
521 if (testenv("debug.acpi.avoid"))
522 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
524 /* Bring the hardware and basic handlers online. */
525 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
526 device_printf(dev, "Could not enable ACPI: %s\n",
527 AcpiFormatException(status));
532 * Call the ECDT probe function to provide EC functionality before
533 * the namespace has been evaluated.
535 * XXX This happens before the sysresource devices have been probed and
536 * attached so its resources come from nexus0. In practice, this isn't
537 * a problem but should be addressed eventually.
539 acpi_ec_ecdt_probe(dev);
541 /* Bring device objects and regions online. */
542 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
543 device_printf(dev, "Could not initialize ACPI objects: %s\n",
544 AcpiFormatException(status));
549 * Setup our sysctl tree.
551 * XXX: This doesn't check to make sure that none of these fail.
553 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
554 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
555 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
556 device_get_name(dev), CTLFLAG_RD, 0, "");
557 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
558 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
559 0, 0, acpi_supported_sleep_state_sysctl, "A", "");
560 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
561 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
562 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
563 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
564 OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
565 &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
566 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
567 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
568 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
569 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
570 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
571 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
572 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
573 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
574 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
575 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
576 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
578 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
579 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
580 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
581 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
582 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
583 OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
584 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
585 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
586 OID_AUTO, "handle_reboot", CTLFLAG_RW,
587 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
590 * Default to 1 second before sleeping to give some machines time to
593 sc->acpi_sleep_delay = 1;
595 sc->acpi_verbose = 1;
596 if ((env = getenv("hw.acpi.verbose")) != NULL) {
597 if (strcmp(env, "0") != 0)
598 sc->acpi_verbose = 1;
602 /* Only enable reboot by default if the FADT says it is available. */
603 if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
604 sc->acpi_handle_reboot = 1;
606 /* Only enable S4BIOS by default if the FACS says it is available. */
607 if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
610 /* Probe all supported sleep states. */
611 acpi_sleep_states[ACPI_STATE_S0] = TRUE;
612 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
613 if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
614 acpi_sleep_states[state] = TRUE;
617 * Dispatch the default sleep state to devices. The lid switch is set
618 * to UNKNOWN by default to avoid surprising users.
620 sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
621 ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
622 sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
623 sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
624 ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
625 sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
626 ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
628 /* Pick the first valid sleep state for the sleep button default. */
629 sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
630 for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
631 if (acpi_sleep_states[state]) {
632 sc->acpi_sleep_button_sx = state;
636 acpi_enable_fixed_events(sc);
639 * Scan the namespace and attach/initialise children.
642 /* Register our shutdown handler. */
643 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
647 * Register our acpi event handlers.
648 * XXX should be configurable eg. via userland policy manager.
650 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
651 sc, ACPI_EVENT_PRI_LAST);
652 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
653 sc, ACPI_EVENT_PRI_LAST);
655 /* Flag our initial states. */
656 sc->acpi_enabled = TRUE;
657 sc->acpi_sstate = ACPI_STATE_S0;
658 sc->acpi_sleep_disabled = TRUE;
660 /* Create the control device */
661 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
663 sc->acpi_dev_t->si_drv1 = sc;
665 if ((error = acpi_machdep_init(dev)))
668 /* Register ACPI again to pass the correct argument of pm_func. */
669 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
671 if (!acpi_disabled("bus"))
672 acpi_probe_children(dev);
674 /* Update all GPEs and enable runtime GPEs. */
675 status = AcpiUpdateAllGpes();
676 if (ACPI_FAILURE(status))
677 device_printf(dev, "Could not update all GPEs: %s\n",
678 AcpiFormatException(status));
680 /* Allow sleep request after a while. */
681 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
686 return_VALUE (error);
690 acpi_set_power_children(device_t dev, int state)
692 device_t child, parent;
694 struct pci_devinfo *dinfo;
695 int dstate, i, numdevs;
697 if (device_get_children(dev, &devlist, &numdevs) != 0)
701 * Retrieve and set D-state for the sleep state if _SxD is present.
702 * Skip children who aren't attached since they are handled separately.
704 parent = device_get_parent(dev);
705 for (i = 0; i < numdevs; i++) {
707 dinfo = device_get_ivars(child);
709 if (device_is_attached(child) &&
710 acpi_device_pwr_for_sleep(parent, dev, &dstate) == 0)
711 acpi_set_powerstate(child, dstate);
713 free(devlist, M_TEMP);
717 acpi_suspend(device_t dev)
723 error = bus_generic_suspend(dev);
725 acpi_set_power_children(dev, ACPI_STATE_D3);
731 acpi_resume(device_t dev)
736 acpi_set_power_children(dev, ACPI_STATE_D0);
738 return (bus_generic_resume(dev));
742 acpi_shutdown(device_t dev)
747 /* Allow children to shutdown first. */
748 bus_generic_shutdown(dev);
751 * Enable any GPEs that are able to power-on the system (i.e., RTC).
752 * Also, disable any that are not valid for this state (most).
754 acpi_wake_prep_walk(ACPI_STATE_S5);
760 * Handle a new device being added
763 acpi_add_child(device_t bus, u_int order, const char *name, int unit)
765 struct acpi_device *ad;
768 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
771 resource_list_init(&ad->ad_rl);
773 child = device_add_child_ordered(bus, order, name, unit);
775 device_set_ivars(child, ad);
782 acpi_print_child(device_t bus, device_t child)
784 struct acpi_device *adev = device_get_ivars(child);
785 struct resource_list *rl = &adev->ad_rl;
788 retval += bus_print_child_header(bus, child);
789 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
790 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
791 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
792 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld");
793 if (device_get_flags(child))
794 retval += printf(" flags %#x", device_get_flags(child));
795 retval += bus_print_child_footer(bus, child);
801 * If this device is an ACPI child but no one claimed it, attempt
802 * to power it off. We'll power it back up when a driver is added.
804 * XXX Disabled for now since many necessary devices (like fdc and
805 * ATA) don't claim the devices we created for them but still expect
806 * them to be powered up.
809 acpi_probe_nomatch(device_t bus, device_t child)
811 #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
812 acpi_set_powerstate(child, ACPI_STATE_D3);
817 * If a new driver has a chance to probe a child, first power it up.
819 * XXX Disabled for now (see acpi_probe_nomatch for details).
822 acpi_driver_added(device_t dev, driver_t *driver)
824 device_t child, *devlist;
827 DEVICE_IDENTIFY(driver, dev);
828 if (device_get_children(dev, &devlist, &numdevs))
830 for (i = 0; i < numdevs; i++) {
832 if (device_get_state(child) == DS_NOTPRESENT) {
833 #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
834 acpi_set_powerstate(child, ACPI_STATE_D0);
835 if (device_probe_and_attach(child) != 0)
836 acpi_set_powerstate(child, ACPI_STATE_D3);
838 device_probe_and_attach(child);
842 free(devlist, M_TEMP);
845 /* Location hint for devctl(8) */
847 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
850 struct acpi_device *dinfo = device_get_ivars(child);
852 if (dinfo->ad_handle)
853 snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
855 snprintf(buf, buflen, "unknown");
859 /* PnP information for devctl(8) */
861 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
864 struct acpi_device *dinfo = device_get_ivars(child);
865 ACPI_DEVICE_INFO *adinfo;
867 if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) {
868 snprintf(buf, buflen, "unknown");
872 snprintf(buf, buflen, "_HID=%s _UID=%lu",
873 (adinfo->Valid & ACPI_VALID_HID) ?
874 adinfo->HardwareId.String : "none",
875 (adinfo->Valid & ACPI_VALID_UID) ?
876 strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL);
883 * Handle per-device ivars
886 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
888 struct acpi_device *ad;
890 if ((ad = device_get_ivars(child)) == NULL) {
891 device_printf(child, "device has no ivars\n");
895 /* ACPI and ISA compatibility ivars */
897 case ACPI_IVAR_HANDLE:
898 *(ACPI_HANDLE *)result = ad->ad_handle;
900 case ACPI_IVAR_PRIVATE:
901 *(void **)result = ad->ad_private;
903 case ACPI_IVAR_FLAGS:
904 *(int *)result = ad->ad_flags;
906 case ISA_IVAR_VENDORID:
907 case ISA_IVAR_SERIAL:
908 case ISA_IVAR_COMPATID:
911 case ISA_IVAR_LOGICALID:
912 *(int *)result = acpi_isa_get_logicalid(child);
922 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
924 struct acpi_device *ad;
926 if ((ad = device_get_ivars(child)) == NULL) {
927 device_printf(child, "device has no ivars\n");
932 case ACPI_IVAR_HANDLE:
933 ad->ad_handle = (ACPI_HANDLE)value;
935 case ACPI_IVAR_PRIVATE:
936 ad->ad_private = (void *)value;
938 case ACPI_IVAR_FLAGS:
939 ad->ad_flags = (int)value;
942 panic("bad ivar write request (%d)", index);
950 * Handle child resource allocation/removal
952 static struct resource_list *
953 acpi_get_rlist(device_t dev, device_t child)
955 struct acpi_device *ad;
957 ad = device_get_ivars(child);
962 acpi_match_resource_hint(device_t dev, int type, long value)
964 struct acpi_device *ad = device_get_ivars(dev);
965 struct resource_list *rl = &ad->ad_rl;
966 struct resource_list_entry *rle;
968 STAILQ_FOREACH(rle, rl, link) {
969 if (rle->type != type)
971 if (rle->start <= value && rle->end >= value)
978 * Wire device unit numbers based on resource matches in hints.
981 acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
986 int line, matches, unit;
989 * Iterate over all the hints for the devices with the specified
990 * name to see if one's resources are a subset of this device.
994 if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
997 /* Must have an "at" for acpi or isa. */
998 resource_string_value(name, unit, "at", &s);
999 if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
1000 strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0))
1004 * Check for matching resources. We must have at least one match.
1005 * Since I/O and memory resources cannot be shared, if we get a
1006 * match on either of those, ignore any mismatches in IRQs or DRQs.
1008 * XXX: We may want to revisit this to be more lenient and wire
1009 * as long as it gets one match.
1012 if (resource_long_value(name, unit, "port", &value) == 0) {
1014 * Floppy drive controllers are notorious for having a
1015 * wide variety of resources not all of which include the
1016 * first port that is specified by the hint (typically
1017 * 0x3f0) (see the comment above fdc_isa_alloc_resources()
1018 * in fdc_isa.c). However, they do all seem to include
1019 * port + 2 (e.g. 0x3f2) so for a floppy device, look for
1020 * 'value + 2' in the port resources instead of the hint
1023 if (strcmp(name, "fdc") == 0)
1025 if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
1030 if (resource_long_value(name, unit, "maddr", &value) == 0) {
1031 if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
1038 if (resource_long_value(name, unit, "irq", &value) == 0) {
1039 if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
1044 if (resource_long_value(name, unit, "drq", &value) == 0) {
1045 if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
1053 /* We have a winner! */
1061 * Pre-allocate/manage all memory and IO resources. Since rman can't handle
1062 * duplicates, we merge any in the sysresource attach routine.
1065 acpi_sysres_alloc(device_t dev)
1067 struct resource *res;
1068 struct resource_list *rl;
1069 struct resource_list_entry *rle;
1071 char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1076 * Probe/attach any sysresource devices. This would be unnecessary if we
1077 * had multi-pass probe/attach.
1079 if (device_get_children(dev, &children, &child_count) != 0)
1081 for (i = 0; i < child_count; i++) {
1082 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1083 device_probe_and_attach(children[i]);
1085 free(children, M_TEMP);
1087 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
1088 STAILQ_FOREACH(rle, rl, link) {
1089 if (rle->res != NULL) {
1090 device_printf(dev, "duplicate resource for %lx\n", rle->start);
1094 /* Only memory and IO resources are valid here. */
1095 switch (rle->type) {
1096 case SYS_RES_IOPORT:
1099 case SYS_RES_MEMORY:
1100 rm = &acpi_rman_mem;
1106 /* Pre-allocate resource and add to our rman pool. */
1107 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
1108 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
1110 rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1113 device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
1114 rle->start, rle->count, rle->type);
1119 static char *pcilink_ids[] = { "PNP0C0F", NULL };
1120 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1123 * Reserve declared resources for devices found during attach once system
1124 * resources have been allocated.
1127 acpi_reserve_resources(device_t dev)
1129 struct resource_list_entry *rle;
1130 struct resource_list *rl;
1131 struct acpi_device *ad;
1132 struct acpi_softc *sc;
1136 sc = device_get_softc(dev);
1137 if (device_get_children(dev, &children, &child_count) != 0)
1139 for (i = 0; i < child_count; i++) {
1140 ad = device_get_ivars(children[i]);
1143 /* Don't reserve system resources. */
1144 if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1147 STAILQ_FOREACH(rle, rl, link) {
1149 * Don't reserve IRQ resources. There are many sticky things
1150 * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1151 * when using legacy routing).
1153 if (rle->type == SYS_RES_IRQ)
1157 * Don't reserve the resource if it is already allocated.
1158 * The acpi_ec(4) driver can allocate its resources early
1159 * if ECDT is present.
1161 if (rle->res != NULL)
1165 * Try to reserve the resource from our parent. If this
1166 * fails because the resource is a system resource, just
1167 * let it be. The resource range is already reserved so
1168 * that other devices will not use it. If the driver
1169 * needs to allocate the resource, then
1170 * acpi_alloc_resource() will sub-alloc from the system
1173 resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1174 rle->start, rle->end, rle->count, 0);
1177 free(children, M_TEMP);
1178 sc->acpi_resources_reserved = 1;
1182 acpi_set_resource(device_t dev, device_t child, int type, int rid,
1183 u_long start, u_long count)
1185 struct acpi_softc *sc = device_get_softc(dev);
1186 struct acpi_device *ad = device_get_ivars(child);
1187 struct resource_list *rl = &ad->ad_rl;
1190 /* Ignore IRQ resources for PCI link devices. */
1191 if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL)
1194 /* If the resource is already allocated, fail. */
1195 if (resource_list_busy(rl, type, rid))
1198 /* If the resource is already reserved, release it. */
1199 if (resource_list_reserved(rl, type, rid))
1200 resource_list_unreserve(rl, dev, child, type, rid);
1202 /* Add the resource. */
1203 end = (start + count - 1);
1204 resource_list_add(rl, type, rid, start, end, count);
1206 /* Don't reserve resources until the system resources are allocated. */
1207 if (!sc->acpi_resources_reserved)
1210 /* Don't reserve system resources. */
1211 if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL)
1215 * Don't reserve IRQ resources. There are many sticky things to
1216 * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when
1217 * using legacy routing).
1219 if (type == SYS_RES_IRQ)
1223 * Reserve the resource.
1225 * XXX: Ignores failure for now. Failure here is probably a
1226 * BIOS/firmware bug?
1228 resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0);
1232 static struct resource *
1233 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
1234 u_long start, u_long end, u_long count, u_int flags)
1237 struct acpi_device *ad;
1238 struct resource_list_entry *rle;
1239 struct resource_list *rl;
1240 struct resource *res;
1242 int isdefault = (start == 0UL && end == ~0UL);
1245 * First attempt at allocating the resource. For direct children,
1246 * use resource_list_alloc() to handle reserved resources. For
1247 * other devices, pass the request up to our parent.
1249 if (bus == device_get_parent(child)) {
1250 ad = device_get_ivars(child);
1254 * Simulate the behavior of the ISA bus for direct children
1255 * devices. That is, if a non-default range is specified for
1256 * a resource that doesn't exist, use bus_set_resource() to
1257 * add the resource before allocating it. Note that these
1258 * resources will not be reserved.
1260 if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1261 resource_list_add(rl, type, *rid, start, end, count);
1262 res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1264 if (res != NULL && type == SYS_RES_IRQ) {
1266 * Since bus_config_intr() takes immediate effect, we cannot
1267 * configure the interrupt associated with a device when we
1268 * parse the resources but have to defer it until a driver
1269 * actually allocates the interrupt via bus_alloc_resource().
1271 * XXX: Should we handle the lookup failing?
1273 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
1274 acpi_config_intr(child, &ares);
1278 * If this is an allocation of the "default" range for a given
1279 * RID, fetch the exact bounds for this resource from the
1280 * resource list entry to try to allocate the range from the
1281 * system resource regions.
1283 if (res == NULL && isdefault) {
1284 rle = resource_list_find(rl, type, *rid);
1292 res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
1293 start, end, count, flags);
1294 if (res != NULL || start + count - 1 != end)
1298 * If the first attempt failed and this is an allocation of a
1299 * specific range, try to satisfy the request via a suballocation
1300 * from our system resource regions. Note that we only handle
1301 * memory and I/O port system resources.
1304 case SYS_RES_IOPORT:
1307 case SYS_RES_MEMORY:
1308 rm = &acpi_rman_mem;
1314 res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1319 rman_set_rid(res, *rid);
1321 /* If requested, activate the resource using the parent's method. */
1322 if (flags & RF_ACTIVE)
1323 if (bus_activate_resource(child, type, *rid, res) != 0) {
1324 rman_release_resource(res);
1332 acpi_is_resource_managed(int type, struct resource *r)
1335 /* We only handle memory and IO resources through rman. */
1337 case SYS_RES_IOPORT:
1338 return (rman_is_region_manager(r, &acpi_rman_io));
1339 case SYS_RES_MEMORY:
1340 return (rman_is_region_manager(r, &acpi_rman_mem));
1346 acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
1347 u_long start, u_long end)
1350 if (acpi_is_resource_managed(type, r))
1351 return (rman_adjust_resource(r, start, end));
1352 return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1356 acpi_release_resource(device_t bus, device_t child, int type, int rid,
1362 * If this resource belongs to one of our internal managers,
1363 * deactivate it and release it to the local pool.
1365 if (acpi_is_resource_managed(type, r)) {
1366 if (rman_get_flags(r) & RF_ACTIVE) {
1367 ret = bus_deactivate_resource(child, type, rid, r);
1371 return (rman_release_resource(r));
1374 return (bus_generic_rl_release_resource(bus, child, type, rid, r));
1378 acpi_delete_resource(device_t bus, device_t child, int type, int rid)
1380 struct resource_list *rl;
1382 rl = acpi_get_rlist(bus, child);
1383 if (resource_list_busy(rl, type, rid)) {
1384 device_printf(bus, "delete_resource: Resource still owned by child"
1385 " (type=%d, rid=%d)\n", type, rid);
1388 resource_list_unreserve(rl, bus, child, type, rid);
1389 resource_list_delete(rl, type, rid);
1392 /* Allocate an IO port or memory resource, given its GAS. */
1394 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1395 struct resource **res, u_int flags)
1397 int error, res_type;
1400 if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1403 /* We only support memory and IO spaces. */
1404 switch (gas->SpaceId) {
1405 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1406 res_type = SYS_RES_MEMORY;
1408 case ACPI_ADR_SPACE_SYSTEM_IO:
1409 res_type = SYS_RES_IOPORT;
1412 return (EOPNOTSUPP);
1416 * If the register width is less than 8, assume the BIOS author means
1417 * it is a bit field and just allocate a byte.
1419 if (gas->BitWidth && gas->BitWidth < 8)
1422 /* Validate the address after we're sure we support the space. */
1423 if (gas->Address == 0 || gas->BitWidth == 0)
1426 bus_set_resource(dev, res_type, *rid, gas->Address,
1428 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1433 bus_delete_resource(dev, res_type, *rid);
1438 /* Probe _HID and _CID for compatible ISA PNP ids. */
1440 acpi_isa_get_logicalid(device_t dev)
1442 ACPI_DEVICE_INFO *devinfo;
1446 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1448 /* Fetch and validate the HID. */
1449 if ((h = acpi_get_handle(dev)) == NULL ||
1450 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1453 pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
1454 devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
1455 PNP_EISAID(devinfo->HardwareId.String) : 0;
1456 AcpiOsFree(devinfo);
1458 return_VALUE (pnpid);
1462 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1464 ACPI_DEVICE_INFO *devinfo;
1465 ACPI_DEVICE_ID *ids;
1470 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1474 /* Fetch and validate the CID */
1475 if ((h = acpi_get_handle(dev)) == NULL ||
1476 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1479 if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
1480 AcpiOsFree(devinfo);
1484 if (devinfo->CompatibleIdList.Count < count)
1485 count = devinfo->CompatibleIdList.Count;
1486 ids = devinfo->CompatibleIdList.Ids;
1487 for (i = 0, valid = 0; i < count; i++)
1488 if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
1489 strncmp(ids[i].String, "PNP", 3) == 0) {
1490 *pnpid++ = PNP_EISAID(ids[i].String);
1493 AcpiOsFree(devinfo);
1495 return_VALUE (valid);
1499 acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1505 h = acpi_get_handle(dev);
1506 if (ids == NULL || h == NULL)
1508 t = acpi_get_type(dev);
1509 if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
1512 /* Try to match one of the array of IDs with a HID or CID. */
1513 for (i = 0; ids[i] != NULL; i++) {
1514 if (acpi_MatchHid(h, ids[i]))
1521 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1522 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1527 h = ACPI_ROOT_OBJECT;
1528 else if ((h = acpi_get_handle(dev)) == NULL)
1529 return (AE_BAD_PARAMETER);
1530 return (AcpiEvaluateObject(h, pathname, parameters, ret));
1534 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
1536 struct acpi_softc *sc;
1541 handle = acpi_get_handle(dev);
1544 * XXX If we find these devices, don't try to power them down.
1545 * The serial and IRDA ports on my T23 hang the system when
1546 * set to D3 and it appears that such legacy devices may
1547 * need special handling in their drivers.
1549 if (dstate == NULL || handle == NULL ||
1550 acpi_MatchHid(handle, "PNP0500") ||
1551 acpi_MatchHid(handle, "PNP0501") ||
1552 acpi_MatchHid(handle, "PNP0502") ||
1553 acpi_MatchHid(handle, "PNP0510") ||
1554 acpi_MatchHid(handle, "PNP0511"))
1558 * Override next state with the value from _SxD, if present.
1559 * Note illegal _S0D is evaluated because some systems expect this.
1561 sc = device_get_softc(bus);
1562 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
1563 status = acpi_GetInteger(handle, sxd, dstate);
1564 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
1565 device_printf(dev, "failed to get %s on %s: %s\n", sxd,
1566 acpi_name(handle), AcpiFormatException(status));
1573 /* Callback arg for our implementation of walking the namespace. */
1574 struct acpi_device_scan_ctx {
1575 acpi_scan_cb_t user_fn;
1581 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1583 struct acpi_device_scan_ctx *ctx;
1584 device_t dev, old_dev;
1586 ACPI_OBJECT_TYPE type;
1589 * Skip this device if we think we'll have trouble with it or it is
1590 * the parent where the scan began.
1592 ctx = (struct acpi_device_scan_ctx *)arg;
1593 if (acpi_avoid(h) || h == ctx->parent)
1596 /* If this is not a valid device type (e.g., a method), skip it. */
1597 if (ACPI_FAILURE(AcpiGetType(h, &type)))
1599 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1600 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1604 * Call the user function with the current device. If it is unchanged
1605 * afterwards, return. Otherwise, we update the handle to the new dev.
1607 old_dev = acpi_get_device(h);
1609 status = ctx->user_fn(h, &dev, level, ctx->arg);
1610 if (ACPI_FAILURE(status) || old_dev == dev)
1613 /* Remove the old child and its connection to the handle. */
1614 if (old_dev != NULL) {
1615 device_delete_child(device_get_parent(old_dev), old_dev);
1616 AcpiDetachData(h, acpi_fake_objhandler);
1619 /* Recreate the handle association if the user created a device. */
1621 AcpiAttachData(h, acpi_fake_objhandler, dev);
1627 acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1628 acpi_scan_cb_t user_fn, void *arg)
1631 struct acpi_device_scan_ctx ctx;
1633 if (acpi_disabled("children"))
1637 h = ACPI_ROOT_OBJECT;
1638 else if ((h = acpi_get_handle(dev)) == NULL)
1639 return (AE_BAD_PARAMETER);
1640 ctx.user_fn = user_fn;
1643 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1644 acpi_device_scan_cb, NULL, &ctx, NULL));
1648 * Even though ACPI devices are not PCI, we use the PCI approach for setting
1649 * device power states since it's close enough to ACPI.
1652 acpi_set_powerstate(device_t child, int state)
1657 h = acpi_get_handle(child);
1658 if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
1663 /* Ignore errors if the power methods aren't present. */
1664 status = acpi_pwr_switch_consumer(h, state);
1665 if (ACPI_SUCCESS(status)) {
1667 device_printf(child, "set ACPI power state D%d on %s\n",
1668 state, acpi_name(h));
1669 } else if (status != AE_NOT_FOUND)
1670 device_printf(child,
1671 "failed to set ACPI power state D%d on %s: %s\n", state,
1672 acpi_name(h), AcpiFormatException(status));
1678 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1680 int result, cid_count, i;
1681 uint32_t lid, cids[8];
1683 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1686 * ISA-style drivers attached to ACPI may persist and
1687 * probe manually if we return ENOENT. We never want
1688 * that to happen, so don't ever return it.
1692 /* Scan the supplied IDs for a match */
1693 lid = acpi_isa_get_logicalid(child);
1694 cid_count = acpi_isa_get_compatid(child, cids, 8);
1695 while (ids && ids->ip_id) {
1696 if (lid == ids->ip_id) {
1700 for (i = 0; i < cid_count; i++) {
1701 if (cids[i] == ids->ip_id) {
1710 if (result == 0 && ids->ip_desc)
1711 device_set_desc(child, ids->ip_desc);
1713 return_VALUE (result);
1716 #if defined(__i386__) || defined(__amd64__)
1718 * Look for a MCFG table. If it is present, use the settings for
1719 * domain (segment) 0 to setup PCI config space access via the memory
1723 acpi_enable_pcie(void)
1725 ACPI_TABLE_HEADER *hdr;
1726 ACPI_MCFG_ALLOCATION *alloc, *end;
1729 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1730 if (ACPI_FAILURE(status))
1733 end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1734 alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1735 while (alloc < end) {
1736 if (alloc->PciSegment == 0) {
1737 pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1738 alloc->EndBusNumber);
1747 * Scan all of the ACPI namespace and attach child devices.
1749 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
1750 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
1751 * However, in violation of the spec, some systems place their PCI link
1752 * devices in \, so we have to walk the whole namespace. We check the
1753 * type of namespace nodes, so this should be ok.
1756 acpi_probe_children(device_t bus)
1759 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1762 * Scan the namespace and insert placeholders for all the devices that
1763 * we find. We also probe/attach any early devices.
1765 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1766 * we want to create nodes for all devices, not just those that are
1767 * currently present. (This assumes that we don't want to create/remove
1768 * devices as they appear, which might be smarter.)
1770 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1771 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1774 /* Pre-allocate resources for our rman from any sysresource devices. */
1775 acpi_sysres_alloc(bus);
1777 /* Reserve resources already allocated to children. */
1778 acpi_reserve_resources(bus);
1780 /* Create any static children by calling device identify methods. */
1781 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1782 bus_generic_probe(bus);
1784 /* Probe/attach all children, created statically and from the namespace. */
1785 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n"));
1786 bus_generic_attach(bus);
1788 /* Attach wake sysctls. */
1789 acpi_wake_sysctl_walk(bus);
1791 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1796 * Determine the probe order for a given device.
1799 acpi_probe_order(ACPI_HANDLE handle, int *order)
1801 ACPI_OBJECT_TYPE type;
1805 * 2. I/O port and memory system resource holders
1806 * 3. Embedded controllers (to handle early accesses)
1807 * 4. PCI Link Devices
1809 AcpiGetType(handle, &type);
1810 if (type == ACPI_TYPE_PROCESSOR)
1812 else if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02"))
1814 else if (acpi_MatchHid(handle, "PNP0C09"))
1816 else if (acpi_MatchHid(handle, "PNP0C0F"))
1821 * Evaluate a child device and determine whether we might attach a device to
1825 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1827 struct acpi_prw_data prw;
1828 ACPI_OBJECT_TYPE type;
1830 device_t bus, child;
1834 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1836 if (acpi_disabled("children"))
1837 return_ACPI_STATUS (AE_OK);
1839 /* Skip this device if we think we'll have trouble with it. */
1840 if (acpi_avoid(handle))
1841 return_ACPI_STATUS (AE_OK);
1843 bus = (device_t)context;
1844 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1845 handle_str = acpi_name(handle);
1847 case ACPI_TYPE_DEVICE:
1849 * Since we scan from \, be sure to skip system scope objects.
1850 * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
1851 * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run
1852 * during the intialization and \_TZ_ is to support Notify() on it.
1854 if (strcmp(handle_str, "\\_SB_") == 0 ||
1855 strcmp(handle_str, "\\_TZ_") == 0)
1857 if (acpi_parse_prw(handle, &prw) == 0)
1858 AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
1861 * Ignore devices that do not have a _HID or _CID. They should
1862 * be discovered by other buses (e.g. the PCI bus driver).
1864 if (!acpi_has_hid(handle))
1867 case ACPI_TYPE_PROCESSOR:
1868 case ACPI_TYPE_THERMAL:
1869 case ACPI_TYPE_POWER:
1871 * Create a placeholder device for this node. Sort the
1872 * placeholder so that the probe/attach passes will run
1873 * breadth-first. Orders less than ACPI_DEV_BASE_ORDER
1874 * are reserved for special objects (i.e., system
1877 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1878 order = level * 10 + 100;
1879 acpi_probe_order(handle, &order);
1880 child = BUS_ADD_CHILD(bus, order, NULL, -1);
1884 /* Associate the handle with the device_t and vice versa. */
1885 acpi_set_handle(child, handle);
1886 AcpiAttachData(handle, acpi_fake_objhandler, child);
1889 * Check that the device is present. If it's not present,
1890 * leave it disabled (so that we have a device_t attached to
1891 * the handle, but we don't probe it).
1893 * XXX PCI link devices sometimes report "present" but not
1894 * "functional" (i.e. if disabled). Go ahead and probe them
1895 * anyway since we may enable them later.
1897 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1898 /* Never disable PCI link devices. */
1899 if (acpi_MatchHid(handle, "PNP0C0F"))
1902 * Docking stations should remain enabled since the system
1903 * may be undocked at boot.
1905 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
1908 device_disable(child);
1913 * Get the device's resource settings and attach them.
1914 * Note that if the device has _PRS but no _CRS, we need
1915 * to decide when it's appropriate to try to configure the
1916 * device. Ignore the return value here; it's OK for the
1917 * device not to have any resources.
1919 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1924 return_ACPI_STATUS (AE_OK);
1928 * AcpiAttachData() requires an object handler but never uses it. This is a
1929 * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
1932 acpi_fake_objhandler(ACPI_HANDLE h, void *data)
1937 acpi_shutdown_final(void *arg, int howto)
1939 struct acpi_softc *sc = (struct acpi_softc *)arg;
1943 * XXX Shutdown code should only run on the BSP (cpuid 0).
1944 * Some chipsets do not power off the system correctly if called from
1947 if ((howto & RB_POWEROFF) != 0) {
1948 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1949 if (ACPI_FAILURE(status)) {
1950 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1951 AcpiFormatException(status));
1954 device_printf(sc->acpi_dev, "Powering system off\n");
1955 ACPI_DISABLE_IRQS();
1956 status = AcpiEnterSleepState(ACPI_STATE_S5);
1957 if (ACPI_FAILURE(status))
1958 device_printf(sc->acpi_dev, "power-off failed - %s\n",
1959 AcpiFormatException(status));
1962 device_printf(sc->acpi_dev, "power-off failed - timeout\n");
1964 } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
1965 /* Reboot using the reset register. */
1966 status = AcpiReset();
1967 if (ACPI_SUCCESS(status)) {
1969 device_printf(sc->acpi_dev, "reset failed - timeout\n");
1970 } else if (status != AE_NOT_EXIST)
1971 device_printf(sc->acpi_dev, "reset failed - %s\n",
1972 AcpiFormatException(status));
1973 } else if (sc->acpi_do_disable && panicstr == NULL) {
1975 * Only disable ACPI if the user requested. On some systems, writing
1976 * the disable value to SMI_CMD hangs the system.
1978 device_printf(sc->acpi_dev, "Shutting down\n");
1984 acpi_enable_fixed_events(struct acpi_softc *sc)
1986 static int first_time = 1;
1988 /* Enable and clear fixed events and install handlers. */
1989 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
1990 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1991 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1992 acpi_event_power_button_sleep, sc);
1994 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
1996 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1997 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1998 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1999 acpi_event_sleep_button_sleep, sc);
2001 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
2008 * Returns true if the device is actually present and should
2009 * be attached to. This requires the present, enabled, UI-visible
2010 * and diagnostics-passed bits to be set.
2013 acpi_DeviceIsPresent(device_t dev)
2015 ACPI_DEVICE_INFO *devinfo;
2019 if ((h = acpi_get_handle(dev)) == NULL ||
2020 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2023 /* If no _STA method, must be present */
2024 present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
2025 ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
2027 AcpiOsFree(devinfo);
2032 * Returns true if the battery is actually present and inserted.
2035 acpi_BatteryIsPresent(device_t dev)
2037 ACPI_DEVICE_INFO *devinfo;
2041 if ((h = acpi_get_handle(dev)) == NULL ||
2042 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2045 /* If no _STA method, must be present */
2046 present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
2047 ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
2049 AcpiOsFree(devinfo);
2054 * Returns true if a device has at least one valid device ID.
2057 acpi_has_hid(ACPI_HANDLE h)
2059 ACPI_DEVICE_INFO *devinfo;
2063 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2067 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2069 else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2070 if (devinfo->CompatibleIdList.Count > 0)
2073 AcpiOsFree(devinfo);
2078 * Match a HID string against a handle
2081 acpi_MatchHid(ACPI_HANDLE h, const char *hid)
2083 ACPI_DEVICE_INFO *devinfo;
2087 if (hid == NULL || h == NULL ||
2088 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2092 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
2093 strcmp(hid, devinfo->HardwareId.String) == 0)
2095 else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2096 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
2097 if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
2103 AcpiOsFree(devinfo);
2108 * Return the handle of a named object within our scope, ie. that of (parent)
2109 * or one if its parents.
2112 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2117 /* Walk back up the tree to the root */
2119 status = AcpiGetHandle(parent, path, &r);
2120 if (ACPI_SUCCESS(status)) {
2124 /* XXX Return error here? */
2125 if (status != AE_NOT_FOUND)
2127 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2128 return (AE_NOT_FOUND);
2134 * Allocate a buffer with a preset data size.
2137 acpi_AllocBuffer(int size)
2141 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2144 buf->Pointer = (void *)(buf + 1);
2149 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2152 ACPI_OBJECT_LIST args;
2154 arg1.Type = ACPI_TYPE_INTEGER;
2155 arg1.Integer.Value = number;
2157 args.Pointer = &arg1;
2159 return (AcpiEvaluateObject(handle, path, &args, NULL));
2163 * Evaluate a path that should return an integer.
2166 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2173 handle = ACPI_ROOT_OBJECT;
2176 * Assume that what we've been pointed at is an Integer object, or
2177 * a method that will return an Integer.
2179 buf.Pointer = ¶m;
2180 buf.Length = sizeof(param);
2181 status = AcpiEvaluateObject(handle, path, NULL, &buf);
2182 if (ACPI_SUCCESS(status)) {
2183 if (param.Type == ACPI_TYPE_INTEGER)
2184 *number = param.Integer.Value;
2190 * In some applications, a method that's expected to return an Integer
2191 * may instead return a Buffer (probably to simplify some internal
2192 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
2193 * convert it into an Integer as best we can.
2197 if (status == AE_BUFFER_OVERFLOW) {
2198 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2199 status = AE_NO_MEMORY;
2201 status = AcpiEvaluateObject(handle, path, NULL, &buf);
2202 if (ACPI_SUCCESS(status))
2203 status = acpi_ConvertBufferToInteger(&buf, number);
2204 AcpiOsFree(buf.Pointer);
2211 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2217 p = (ACPI_OBJECT *)bufp->Pointer;
2218 if (p->Type == ACPI_TYPE_INTEGER) {
2219 *number = p->Integer.Value;
2222 if (p->Type != ACPI_TYPE_BUFFER)
2224 if (p->Buffer.Length > sizeof(int))
2225 return (AE_BAD_DATA);
2228 val = p->Buffer.Pointer;
2229 for (i = 0; i < p->Buffer.Length; i++)
2230 *number += val[i] << (i * 8);
2235 * Iterate over the elements of an a package object, calling the supplied
2236 * function for each element.
2238 * XXX possible enhancement might be to abort traversal on error.
2241 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2242 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2247 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2248 return (AE_BAD_PARAMETER);
2250 /* Iterate over components */
2252 comp = pkg->Package.Elements;
2253 for (; i < pkg->Package.Count; i++, comp++)
2260 * Find the (index)th resource object in a set.
2263 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
2268 rp = (ACPI_RESOURCE *)buf->Pointer;
2272 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2273 return (AE_BAD_PARAMETER);
2275 /* Check for terminator */
2276 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2277 return (AE_NOT_FOUND);
2278 rp = ACPI_NEXT_RESOURCE(rp);
2287 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
2289 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
2290 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
2291 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
2294 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
2297 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
2302 /* Initialise the buffer if necessary. */
2303 if (buf->Pointer == NULL) {
2304 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
2305 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
2306 return (AE_NO_MEMORY);
2307 rp = (ACPI_RESOURCE *)buf->Pointer;
2308 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2315 * Scan the current buffer looking for the terminator.
2316 * This will either find the terminator or hit the end
2317 * of the buffer and return an error.
2319 rp = (ACPI_RESOURCE *)buf->Pointer;
2321 /* Range check, don't go outside the buffer */
2322 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2323 return (AE_BAD_PARAMETER);
2324 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2326 rp = ACPI_NEXT_RESOURCE(rp);
2330 * Check the size of the buffer and expand if required.
2333 * size of existing resources before terminator +
2334 * size of new resource and header +
2335 * size of terminator.
2337 * Note that this loop should really only run once, unless
2338 * for some reason we are stuffing a *really* huge resource.
2340 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2341 res->Length + ACPI_RS_SIZE_NO_DATA +
2342 ACPI_RS_SIZE_MIN) >= buf->Length) {
2343 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
2344 return (AE_NO_MEMORY);
2345 bcopy(buf->Pointer, newp, buf->Length);
2346 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2347 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
2348 AcpiOsFree(buf->Pointer);
2349 buf->Pointer = newp;
2350 buf->Length += buf->Length;
2353 /* Insert the new resource. */
2354 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
2356 /* And add the terminator. */
2357 rp = ACPI_NEXT_RESOURCE(rp);
2358 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2365 * Set interrupt model.
2368 acpi_SetIntrModel(int model)
2371 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2375 * Walk subtables of a table and call a callback routine for each
2376 * subtable. The caller should provide the first subtable and a
2377 * pointer to the end of the table. This can be used to walk tables
2378 * such as MADT and SRAT that use subtable entries.
2381 acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2384 ACPI_SUBTABLE_HEADER *entry;
2386 for (entry = first; (void *)entry < end; ) {
2387 /* Avoid an infinite loop if we hit a bogus entry. */
2388 if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2391 handler(entry, arg);
2392 entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2397 * DEPRECATED. This interface has serious deficiencies and will be
2400 * Immediately enter the sleep state. In the old model, acpiconf(8) ran
2401 * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
2404 acpi_SetSleepState(struct acpi_softc *sc, int state)
2409 device_printf(sc->acpi_dev,
2410 "warning: acpi_SetSleepState() deprecated, need to update your software\n");
2413 return (acpi_EnterSleepState(sc, state));
2416 #if defined(__amd64__) || defined(__i386__)
2418 acpi_sleep_force(void *arg)
2420 struct acpi_softc *sc = (struct acpi_softc *)arg;
2422 device_printf(sc->acpi_dev,
2423 "suspend request timed out, forcing sleep now\n");
2424 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2425 device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
2426 sc->acpi_next_sstate);
2431 * Request that the system enter the given suspend state. All /dev/apm
2432 * devices and devd(8) will be notified. Userland then has a chance to
2433 * save state and acknowledge the request. The system sleeps once all
2437 acpi_ReqSleepState(struct acpi_softc *sc, int state)
2439 #if defined(__amd64__) || defined(__i386__)
2440 struct apm_clone_data *clone;
2443 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
2445 if (!acpi_sleep_states[state])
2446 return (EOPNOTSUPP);
2450 /* If a suspend request is already in progress, just return. */
2451 if (sc->acpi_next_sstate != 0) {
2456 /* S5 (soft-off) should be entered directly with no waiting. */
2457 if (state == ACPI_STATE_S5) {
2459 status = acpi_EnterSleepState(sc, state);
2460 return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2463 /* Record the pending state and notify all apm devices. */
2464 sc->acpi_next_sstate = state;
2465 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
2466 clone->notify_status = APM_EV_NONE;
2467 if ((clone->flags & ACPI_EVF_DEVD) == 0) {
2468 selwakeuppri(&clone->sel_read, PZERO);
2469 KNOTE_LOCKED(&clone->sel_read.si_note, 0);
2473 /* If devd(8) is not running, immediately enter the sleep state. */
2474 if (!devctl_process_running()) {
2476 status = acpi_EnterSleepState(sc, state);
2477 return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2481 * Set a timeout to fire if userland doesn't ack the suspend request
2482 * in time. This way we still eventually go to sleep if we were
2483 * overheating or running low on battery, even if userland is hung.
2484 * We cancel this timeout once all userland acks are in or the
2485 * suspend request is aborted.
2487 callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
2490 /* Now notify devd(8) also. */
2491 acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2495 /* This platform does not support acpi suspend/resume. */
2496 return (EOPNOTSUPP);
2501 * Acknowledge (or reject) a pending sleep state. The caller has
2502 * prepared for suspend and is now ready for it to proceed. If the
2503 * error argument is non-zero, it indicates suspend should be cancelled
2504 * and gives an errno value describing why. Once all votes are in,
2505 * we suspend the system.
2508 acpi_AckSleepState(struct apm_clone_data *clone, int error)
2510 #if defined(__amd64__) || defined(__i386__)
2511 struct acpi_softc *sc;
2514 /* If no pending sleep state, return an error. */
2516 sc = clone->acpi_sc;
2517 if (sc->acpi_next_sstate == 0) {
2522 /* Caller wants to abort suspend process. */
2524 sc->acpi_next_sstate = 0;
2525 callout_stop(&sc->susp_force_to);
2526 device_printf(sc->acpi_dev,
2527 "listener on %s cancelled the pending suspend\n",
2528 devtoname(clone->cdev));
2534 * Mark this device as acking the suspend request. Then, walk through
2535 * all devices, seeing if they agree yet. We only count devices that
2536 * are writable since read-only devices couldn't ack the request.
2539 clone->notify_status = APM_EV_ACKED;
2540 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
2541 if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
2542 clone->notify_status != APM_EV_ACKED) {
2548 /* If all devices have voted "yes", we will suspend now. */
2550 callout_stop(&sc->susp_force_to);
2554 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2559 /* This platform does not support acpi suspend/resume. */
2560 return (EOPNOTSUPP);
2565 acpi_sleep_enable(void *arg)
2567 struct acpi_softc *sc = (struct acpi_softc *)arg;
2569 /* Reschedule if the system is not fully up and running. */
2570 if (!AcpiGbl_SystemAwakeAndRunning) {
2571 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2576 sc->acpi_sleep_disabled = FALSE;
2581 acpi_sleep_disable(struct acpi_softc *sc)
2585 /* Fail if the system is not fully up and running. */
2586 if (!AcpiGbl_SystemAwakeAndRunning)
2590 status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
2591 sc->acpi_sleep_disabled = TRUE;
2597 enum acpi_sleep_state {
2600 ACPI_SS_DEV_SUSPEND,
2606 * Enter the desired system sleep state.
2608 * Currently we support S1-S5 but S4 is only S4BIOS
2611 acpi_EnterSleepState(struct acpi_softc *sc, int state)
2614 enum acpi_sleep_state slp_state;
2616 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2618 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
2619 return_ACPI_STATUS (AE_BAD_PARAMETER);
2620 if (!acpi_sleep_states[state]) {
2621 device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
2623 return (AE_SUPPORT);
2626 /* Re-entry once we're suspending is not allowed. */
2627 status = acpi_sleep_disable(sc);
2628 if (ACPI_FAILURE(status)) {
2629 device_printf(sc->acpi_dev,
2630 "suspend request ignored (not ready yet)\n");
2634 if (state == ACPI_STATE_S5) {
2636 * Shut down cleanly and power off. This will call us back through the
2637 * shutdown handlers.
2639 shutdown_nice(RB_POWEROFF);
2640 return_ACPI_STATUS (AE_OK);
2643 EVENTHANDLER_INVOKE(power_suspend);
2646 thread_lock(curthread);
2647 sched_bind(curthread, 0);
2648 thread_unlock(curthread);
2652 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
2653 * drivers need this.
2657 slp_state = ACPI_SS_NONE;
2659 sc->acpi_sstate = state;
2661 /* Enable any GPEs as appropriate and requested by the user. */
2662 acpi_wake_prep_walk(state);
2663 slp_state = ACPI_SS_GPE_SET;
2666 * Inform all devices that we are going to sleep. If at least one
2667 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
2669 * XXX Note that a better two-pass approach with a 'veto' pass
2670 * followed by a "real thing" pass would be better, but the current
2671 * bus interface does not provide for this.
2673 if (DEVICE_SUSPEND(root_bus) != 0) {
2674 device_printf(sc->acpi_dev, "device_suspend failed\n");
2677 slp_state = ACPI_SS_DEV_SUSPEND;
2679 /* If testing device suspend only, back out of everything here. */
2680 if (acpi_susp_bounce)
2683 status = AcpiEnterSleepStatePrep(state);
2684 if (ACPI_FAILURE(status)) {
2685 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2686 AcpiFormatException(status));
2689 slp_state = ACPI_SS_SLP_PREP;
2691 if (sc->acpi_sleep_delay > 0)
2692 DELAY(sc->acpi_sleep_delay * 1000000);
2694 if (state != ACPI_STATE_S1) {
2695 acpi_sleep_machdep(sc, state);
2697 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
2698 if (state == ACPI_STATE_S4)
2701 ACPI_DISABLE_IRQS();
2702 status = AcpiEnterSleepState(state);
2703 if (ACPI_FAILURE(status)) {
2704 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2705 AcpiFormatException(status));
2709 slp_state = ACPI_SS_SLEPT;
2712 * Back out state according to how far along we got in the suspend
2713 * process. This handles both the error and success cases.
2716 if (slp_state >= ACPI_SS_GPE_SET) {
2717 acpi_wake_prep_walk(state);
2718 sc->acpi_sstate = ACPI_STATE_S0;
2720 if (slp_state >= ACPI_SS_SLP_PREP)
2721 AcpiLeaveSleepState(state);
2722 if (slp_state >= ACPI_SS_DEV_SUSPEND)
2723 DEVICE_RESUME(root_bus);
2724 if (slp_state >= ACPI_SS_SLEPT)
2725 acpi_enable_fixed_events(sc);
2726 sc->acpi_next_sstate = 0;
2731 thread_lock(curthread);
2732 sched_unbind(curthread);
2733 thread_unlock(curthread);
2736 EVENTHANDLER_INVOKE(power_resume);
2738 /* Allow another sleep request after a while. */
2739 timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2741 /* Run /etc/rc.resume after we are back. */
2742 if (devctl_process_running())
2743 acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
2745 return_ACPI_STATUS (status);
2749 acpi_resync_clock(struct acpi_softc *sc)
2752 if (!acpi_reset_clock)
2756 * Warm up timecounter again and reset system clock.
2758 (void)timecounter->tc_get_timecount(timecounter);
2759 (void)timecounter->tc_get_timecount(timecounter);
2760 inittodr(time_second + sc->acpi_sleep_delay);
2763 /* Enable or disable the device's wake GPE. */
2765 acpi_wake_set_enable(device_t dev, int enable)
2767 struct acpi_prw_data prw;
2771 /* Make sure the device supports waking the system and get the GPE. */
2772 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2775 flags = acpi_get_flags(dev);
2777 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
2779 if (ACPI_FAILURE(status)) {
2780 device_printf(dev, "enable wake failed\n");
2783 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2785 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
2787 if (ACPI_FAILURE(status)) {
2788 device_printf(dev, "disable wake failed\n");
2791 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2798 acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2800 struct acpi_prw_data prw;
2803 /* Check that this is a wake-capable device and get its GPE. */
2804 if (acpi_parse_prw(handle, &prw) != 0)
2806 dev = acpi_get_device(handle);
2809 * The destination sleep state must be less than (i.e., higher power)
2810 * or equal to the value specified by _PRW. If this GPE cannot be
2811 * enabled for the next sleep state, then disable it. If it can and
2812 * the user requested it be enabled, turn on any required power resources
2815 if (sstate > prw.lowest_wake) {
2816 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
2818 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2819 acpi_name(handle), sstate);
2820 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2821 acpi_pwr_wake_enable(handle, 1);
2822 acpi_SetInteger(handle, "_PSW", 1);
2824 device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2825 acpi_name(handle), sstate);
2832 acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2834 struct acpi_prw_data prw;
2838 * Check that this is a wake-capable device and get its GPE. Return
2839 * now if the user didn't enable this device for wake.
2841 if (acpi_parse_prw(handle, &prw) != 0)
2843 dev = acpi_get_device(handle);
2844 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2848 * If this GPE couldn't be enabled for the previous sleep state, it was
2849 * disabled before going to sleep so re-enable it. If it was enabled,
2850 * clear _PSW and turn off any power resources it used.
2852 if (sstate > prw.lowest_wake) {
2853 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
2855 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2857 acpi_SetInteger(handle, "_PSW", 0);
2858 acpi_pwr_wake_enable(handle, 0);
2860 device_printf(dev, "run_prep cleaned up for %s\n",
2868 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2872 /* If suspending, run the sleep prep function, otherwise wake. */
2873 sstate = *(int *)context;
2874 if (AcpiGbl_SystemAwakeAndRunning)
2875 acpi_wake_sleep_prep(handle, sstate);
2877 acpi_wake_run_prep(handle, sstate);
2881 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2883 acpi_wake_prep_walk(int sstate)
2885 ACPI_HANDLE sb_handle;
2887 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2888 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
2889 acpi_wake_prep, NULL, &sstate, NULL);
2893 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
2895 acpi_wake_sysctl_walk(device_t dev)
2897 int error, i, numdevs;
2902 error = device_get_children(dev, &devlist, &numdevs);
2903 if (error != 0 || numdevs == 0) {
2905 free(devlist, M_TEMP);
2908 for (i = 0; i < numdevs; i++) {
2910 acpi_wake_sysctl_walk(child);
2911 if (!device_is_attached(child))
2913 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2914 if (ACPI_SUCCESS(status)) {
2915 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
2916 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
2917 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
2918 acpi_wake_set_sysctl, "I", "Device set to wake the system");
2921 free(devlist, M_TEMP);
2926 /* Enable or disable wake from userland. */
2928 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
2933 dev = (device_t)arg1;
2934 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
2936 error = sysctl_handle_int(oidp, &enable, 0, req);
2937 if (error != 0 || req->newptr == NULL)
2939 if (enable != 0 && enable != 1)
2942 return (acpi_wake_set_enable(dev, enable));
2945 /* Parse a device's _PRW into a structure. */
2947 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2950 ACPI_BUFFER prw_buffer;
2951 ACPI_OBJECT *res, *res2;
2952 int error, i, power_count;
2954 if (h == NULL || prw == NULL)
2958 * The _PRW object (7.2.9) is only required for devices that have the
2959 * ability to wake the system from a sleeping state.
2962 prw_buffer.Pointer = NULL;
2963 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2964 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2965 if (ACPI_FAILURE(status))
2967 res = (ACPI_OBJECT *)prw_buffer.Pointer;
2970 if (!ACPI_PKG_VALID(res, 2))
2974 * Element 1 of the _PRW object:
2975 * The lowest power system sleeping state that can be entered while still
2976 * providing wake functionality. The sleeping state being entered must
2977 * be less than (i.e., higher power) or equal to this value.
2979 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2983 * Element 0 of the _PRW object:
2985 switch (res->Package.Elements[0].Type) {
2986 case ACPI_TYPE_INTEGER:
2988 * If the data type of this package element is numeric, then this
2989 * _PRW package element is the bit index in the GPEx_EN, in the
2990 * GPE blocks described in the FADT, of the enable bit that is
2991 * enabled for the wake event.
2993 prw->gpe_handle = NULL;
2994 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2997 case ACPI_TYPE_PACKAGE:
2999 * If the data type of this package element is a package, then this
3000 * _PRW package element is itself a package containing two
3001 * elements. The first is an object reference to the GPE Block
3002 * device that contains the GPE that will be triggered by the wake
3003 * event. The second element is numeric and it contains the bit
3004 * index in the GPEx_EN, in the GPE Block referenced by the
3005 * first element in the package, of the enable bit that is enabled for
3008 * For example, if this field is a package then it is of the form:
3009 * Package() {\_SB.PCI0.ISA.GPE, 2}
3011 res2 = &res->Package.Elements[0];
3012 if (!ACPI_PKG_VALID(res2, 2))
3014 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3015 if (prw->gpe_handle == NULL)
3017 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3025 /* Elements 2 to N of the _PRW object are power resources. */
3026 power_count = res->Package.Count - 2;
3027 if (power_count > ACPI_PRW_MAX_POWERRES) {
3028 printf("ACPI device %s has too many power resources\n", acpi_name(h));
3031 prw->power_res_count = power_count;
3032 for (i = 0; i < power_count; i++)
3033 prw->power_res[i] = res->Package.Elements[i];
3036 if (prw_buffer.Pointer != NULL)
3037 AcpiOsFree(prw_buffer.Pointer);
3042 * ACPI Event Handlers
3045 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
3048 acpi_system_eventhandler_sleep(void *arg, int state)
3050 struct acpi_softc *sc = (struct acpi_softc *)arg;
3053 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
3055 /* Check if button action is disabled or unknown. */
3056 if (state == ACPI_STATE_UNKNOWN)
3059 /* Request that the system prepare to enter the given suspend state. */
3060 ret = acpi_ReqSleepState(sc, state);
3062 device_printf(sc->acpi_dev,
3063 "request to enter state S%d failed (err %d)\n", state, ret);
3069 acpi_system_eventhandler_wakeup(void *arg, int state)
3072 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
3074 /* Currently, nothing to do for wakeup. */
3080 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
3083 acpi_invoke_sleep_eventhandler(void *context)
3086 EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context);
3090 acpi_invoke_wake_eventhandler(void *context)
3093 EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context);
3097 acpi_event_power_button_sleep(void *context)
3099 struct acpi_softc *sc = (struct acpi_softc *)context;
3101 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3103 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3104 acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx)))
3105 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3106 return_VALUE (ACPI_INTERRUPT_HANDLED);
3110 acpi_event_power_button_wake(void *context)
3112 struct acpi_softc *sc = (struct acpi_softc *)context;
3114 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3116 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3117 acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx)))
3118 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3119 return_VALUE (ACPI_INTERRUPT_HANDLED);
3123 acpi_event_sleep_button_sleep(void *context)
3125 struct acpi_softc *sc = (struct acpi_softc *)context;
3127 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3129 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3130 acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx)))
3131 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3132 return_VALUE (ACPI_INTERRUPT_HANDLED);
3136 acpi_event_sleep_button_wake(void *context)
3138 struct acpi_softc *sc = (struct acpi_softc *)context;
3140 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3142 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3143 acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx)))
3144 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3145 return_VALUE (ACPI_INTERRUPT_HANDLED);
3149 * XXX This static buffer is suboptimal. There is no locking so only
3150 * use this for single-threaded callers.
3153 acpi_name(ACPI_HANDLE handle)
3156 static char data[256];
3158 buf.Length = sizeof(data);
3161 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3163 return ("(unknown)");
3167 * Debugging/bug-avoidance. Avoid trying to fetch info on various
3168 * parts of the namespace.
3171 acpi_avoid(ACPI_HANDLE handle)
3173 char *cp, *env, *np;
3176 np = acpi_name(handle);
3179 if ((env = getenv("debug.acpi.avoid")) == NULL)
3182 /* Scan the avoid list checking for a match */
3185 while (*cp != 0 && isspace(*cp))
3190 while (cp[len] != 0 && !isspace(cp[len]))
3192 if (!strncmp(cp, np, len)) {
3204 * Debugging/bug-avoidance. Disable ACPI subsystem components.
3207 acpi_disabled(char *subsys)
3212 if ((env = getenv("debug.acpi.disabled")) == NULL)
3214 if (strcmp(env, "all") == 0) {
3219 /* Scan the disable list, checking for a match. */
3222 while (*cp != '\0' && isspace(*cp))
3227 while (cp[len] != '\0' && !isspace(cp[len]))
3229 if (strncmp(cp, subsys, len) == 0) {
3241 * Control interface.
3243 * We multiplex ioctls for all participating ACPI devices here. Individual
3244 * drivers wanting to be accessible via /dev/acpi should use the
3245 * register/deregister interface to make their handlers visible.
3247 struct acpi_ioctl_hook
3249 TAILQ_ENTRY(acpi_ioctl_hook) link;
3255 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
3256 static int acpi_ioctl_hooks_initted;
3259 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
3261 struct acpi_ioctl_hook *hp;
3263 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
3270 if (acpi_ioctl_hooks_initted == 0) {
3271 TAILQ_INIT(&acpi_ioctl_hooks);
3272 acpi_ioctl_hooks_initted = 1;
3274 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
3281 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
3283 struct acpi_ioctl_hook *hp;
3286 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
3287 if (hp->cmd == cmd && hp->fn == fn)
3291 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
3292 free(hp, M_ACPIDEV);
3298 acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
3304 acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3310 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3312 struct acpi_softc *sc;
3313 struct acpi_ioctl_hook *hp;
3321 * Scan the list of registered ioctls, looking for handlers.
3324 if (acpi_ioctl_hooks_initted)
3325 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3331 return (hp->fn(cmd, addr, hp->arg));
3334 * Core ioctls are not permitted for non-writable user.
3335 * Currently, other ioctls just fetch information.
3336 * Not changing system behavior.
3338 if ((flag & FWRITE) == 0)
3341 /* Core system ioctls. */
3343 case ACPIIO_REQSLPSTATE:
3344 state = *(int *)addr;
3345 if (state != ACPI_STATE_S5)
3346 return (acpi_ReqSleepState(sc, state));
3347 device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
3350 case ACPIIO_ACKSLPSTATE:
3351 error = *(int *)addr;
3352 error = acpi_AckSleepState(sc->acpi_clone, error);
3354 case ACPIIO_SETSLPSTATE: /* DEPRECATED */
3355 state = *(int *)addr;
3356 if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
3358 if (!acpi_sleep_states[state])
3359 return (EOPNOTSUPP);
3360 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
3372 acpi_sname2sstate(const char *sname)
3376 if (toupper(sname[0]) == 'S') {
3377 sstate = sname[1] - '0';
3378 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
3381 } else if (strcasecmp(sname, "NONE") == 0)
3382 return (ACPI_STATE_UNKNOWN);
3387 acpi_sstate2sname(int sstate)
3389 static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
3391 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
3392 return (snames[sstate]);
3393 else if (sstate == ACPI_STATE_UNKNOWN)
3399 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3405 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3406 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
3407 if (acpi_sleep_states[state])
3408 sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
3411 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3417 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3419 char sleep_state[10];
3420 int error, new_state, old_state;
3422 old_state = *(int *)oidp->oid_arg1;
3423 strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
3424 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
3425 if (error == 0 && req->newptr != NULL) {
3426 new_state = acpi_sname2sstate(sleep_state);
3427 if (new_state < ACPI_STATE_S1)
3429 if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
3430 return (EOPNOTSUPP);
3431 if (new_state != old_state)
3432 *(int *)oidp->oid_arg1 = new_state;
3437 /* Inform devctl(4) when we receive a Notify. */
3439 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
3441 char notify_buf[16];
3442 ACPI_BUFFER handle_buf;
3445 if (subsystem == NULL)
3448 handle_buf.Pointer = NULL;
3449 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
3450 status = AcpiNsHandleToPathname(h, &handle_buf);
3451 if (ACPI_FAILURE(status))
3453 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
3454 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
3455 AcpiOsFree(handle_buf.Pointer);
3460 * Support for parsing debug options from the kernel environment.
3462 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
3463 * by specifying the names of the bits in the debug.acpi.layer and
3464 * debug.acpi.level environment variables. Bits may be unset by
3465 * prefixing the bit name with !.
3473 static struct debugtag dbg_layer[] = {
3474 {"ACPI_UTILITIES", ACPI_UTILITIES},
3475 {"ACPI_HARDWARE", ACPI_HARDWARE},
3476 {"ACPI_EVENTS", ACPI_EVENTS},
3477 {"ACPI_TABLES", ACPI_TABLES},
3478 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
3479 {"ACPI_PARSER", ACPI_PARSER},
3480 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
3481 {"ACPI_EXECUTER", ACPI_EXECUTER},
3482 {"ACPI_RESOURCES", ACPI_RESOURCES},
3483 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
3484 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
3485 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
3486 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
3488 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
3489 {"ACPI_BATTERY", ACPI_BATTERY},
3490 {"ACPI_BUS", ACPI_BUS},
3491 {"ACPI_BUTTON", ACPI_BUTTON},
3492 {"ACPI_EC", ACPI_EC},
3493 {"ACPI_FAN", ACPI_FAN},
3494 {"ACPI_POWERRES", ACPI_POWERRES},
3495 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
3496 {"ACPI_THERMAL", ACPI_THERMAL},
3497 {"ACPI_TIMER", ACPI_TIMER},
3498 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
3502 static struct debugtag dbg_level[] = {
3503 {"ACPI_LV_INIT", ACPI_LV_INIT},
3504 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
3505 {"ACPI_LV_INFO", ACPI_LV_INFO},
3506 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
3508 /* Trace verbosity level 1 [Standard Trace Level] */
3509 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
3510 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
3511 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
3512 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
3513 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
3514 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
3515 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
3516 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
3517 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
3518 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
3519 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
3520 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
3521 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
3522 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
3523 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
3525 /* Trace verbosity level 2 [Function tracing and memory allocation] */
3526 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
3527 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
3528 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
3529 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
3530 {"ACPI_LV_ALL", ACPI_LV_ALL},
3532 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3533 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
3534 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
3535 {"ACPI_LV_IO", ACPI_LV_IO},
3536 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
3537 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
3539 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
3540 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
3541 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
3542 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
3543 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
3544 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
3549 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
3561 while (*ep && !isspace(*ep))
3572 for (i = 0; tag[i].name != NULL; i++) {
3573 if (!strncmp(cp, tag[i].name, l)) {
3575 *flag |= tag[i].value;
3577 *flag &= ~tag[i].value;
3585 acpi_set_debugging(void *junk)
3587 char *layer, *level;
3594 layer = getenv("debug.acpi.layer");
3595 level = getenv("debug.acpi.level");
3596 if (layer == NULL && level == NULL)
3599 printf("ACPI set debug");
3600 if (layer != NULL) {
3601 if (strcmp("NONE", layer) != 0)
3602 printf(" layer '%s'", layer);
3603 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
3606 if (level != NULL) {
3607 if (strcmp("NONE", level) != 0)
3608 printf(" level '%s'", level);
3609 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
3615 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3619 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
3622 struct debugtag *tag;
3625 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
3627 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
3628 tag = &dbg_layer[0];
3629 dbg = &AcpiDbgLayer;
3631 tag = &dbg_level[0];
3632 dbg = &AcpiDbgLevel;
3635 /* Get old values if this is a get request. */
3636 ACPI_SERIAL_BEGIN(acpi);
3638 sbuf_cpy(&sb, "NONE");
3639 } else if (req->newptr == NULL) {
3640 for (; tag->name != NULL; tag++) {
3641 if ((*dbg & tag->value) == tag->value)
3642 sbuf_printf(&sb, "%s ", tag->name);
3648 /* Copy out the old values to the user. */
3649 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
3652 /* If the user is setting a string, parse it. */
3653 if (error == 0 && req->newptr != NULL) {
3655 setenv((char *)oidp->oid_arg1, (char *)req->newptr);
3656 acpi_set_debugging(NULL);
3658 ACPI_SERIAL_END(acpi);
3663 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
3664 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
3665 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
3666 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3667 #endif /* ACPI_DEBUG */
3670 acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
3675 old = acpi_debug_objects;
3676 error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
3677 if (error != 0 || req->newptr == NULL)
3679 if (old == acpi_debug_objects || (old && acpi_debug_objects))
3682 ACPI_SERIAL_BEGIN(acpi);
3683 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
3684 ACPI_SERIAL_END(acpi);
3690 acpi_parse_interfaces(char *str, struct acpi_interface *iface)
3697 while (isspace(*p) || *p == ',')
3702 p = strdup(p, M_TEMP);
3703 for (i = 0; i < len; i++)
3708 if (isspace(p[i]) || p[i] == '\0')
3711 i += strlen(p + i) + 1;
3718 iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
3722 if (isspace(p[i]) || p[i] == '\0')
3725 iface->data[j] = p + i;
3726 i += strlen(p + i) + 1;
3734 acpi_free_interfaces(struct acpi_interface *iface)
3737 free(iface->data[0], M_TEMP);
3738 free(iface->data, M_TEMP);
3742 acpi_reset_interfaces(device_t dev)
3744 struct acpi_interface list;
3748 if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
3749 for (i = 0; i < list.num; i++) {
3750 status = AcpiInstallInterface(list.data[i]);
3751 if (ACPI_FAILURE(status))
3753 "failed to install _OSI(\"%s\"): %s\n",
3754 list.data[i], AcpiFormatException(status));
3755 else if (bootverbose)
3756 device_printf(dev, "installed _OSI(\"%s\")\n",
3759 acpi_free_interfaces(&list);
3761 if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
3762 for (i = 0; i < list.num; i++) {
3763 status = AcpiRemoveInterface(list.data[i]);
3764 if (ACPI_FAILURE(status))
3766 "failed to remove _OSI(\"%s\"): %s\n",
3767 list.data[i], AcpiFormatException(status));
3768 else if (bootverbose)
3769 device_printf(dev, "removed _OSI(\"%s\")\n",
3772 acpi_free_interfaces(&list);
3777 acpi_pm_func(u_long cmd, void *arg, ...)
3779 int state, acpi_state;
3781 struct acpi_softc *sc;
3786 case POWER_CMD_SUSPEND:
3787 sc = (struct acpi_softc *)arg;
3794 state = va_arg(ap, int);
3798 case POWER_SLEEP_STATE_STANDBY:
3799 acpi_state = sc->acpi_standby_sx;
3801 case POWER_SLEEP_STATE_SUSPEND:
3802 acpi_state = sc->acpi_suspend_sx;
3804 case POWER_SLEEP_STATE_HIBERNATE:
3805 acpi_state = ACPI_STATE_S4;
3812 if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
3825 acpi_pm_register(void *arg)
3827 if (!cold || resource_disabled("acpi", 0))
3830 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3833 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);