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$");
35 #include <sys/param.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
44 #include <sys/ioccom.h>
45 #include <sys/reboot.h>
46 #include <sys/sysctl.h>
47 #include <sys/ctype.h>
48 #include <sys/linker.h>
49 #include <sys/power.h>
51 #include <sys/sched.h>
53 #include <sys/timetc.h>
55 #if defined(__i386__) || defined(__amd64__)
56 #include <machine/clock.h>
57 #include <machine/pci_cfgreg.h>
59 #include <machine/resource.h>
60 #include <machine/bus.h>
62 #include <isa/isavar.h>
63 #include <isa/pnpvar.h>
65 #include <contrib/dev/acpica/include/acpi.h>
66 #include <contrib/dev/acpica/include/accommon.h>
67 #include <contrib/dev/acpica/include/acnamesp.h>
69 #include <dev/acpica/acpivar.h>
70 #include <dev/acpica/acpiio.h>
72 #include <dev/pci/pcivar.h>
74 #include <vm/vm_param.h>
76 static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
78 /* Hooks for the ACPI CA debugging infrastructure */
79 #define _COMPONENT ACPI_BUS
80 ACPI_MODULE_NAME("ACPI")
82 static d_open_t acpiopen;
83 static d_close_t acpiclose;
84 static d_ioctl_t acpiioctl;
86 static struct cdevsw acpi_cdevsw = {
87 .d_version = D_VERSION,
94 struct acpi_interface {
99 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
100 static char *pcilink_ids[] = { "PNP0C0F", NULL };
102 /* Global mutex for locking access to the ACPI subsystem. */
103 struct mtx acpi_mutex;
104 struct callout acpi_sleep_timer;
106 /* Bitmap of device quirks. */
109 /* Supported sleep states. */
110 static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT];
112 static void acpi_lookup(void *arg, const char *name, device_t *dev);
113 static int acpi_modevent(struct module *mod, int event, void *junk);
114 static int acpi_probe(device_t dev);
115 static int acpi_attach(device_t dev);
116 static int acpi_suspend(device_t dev);
117 static int acpi_resume(device_t dev);
118 static int acpi_shutdown(device_t dev);
119 static device_t acpi_add_child(device_t bus, u_int order, const char *name,
121 static int acpi_print_child(device_t bus, device_t child);
122 static void acpi_probe_nomatch(device_t bus, device_t child);
123 static void acpi_driver_added(device_t dev, driver_t *driver);
124 static int acpi_read_ivar(device_t dev, device_t child, int index,
126 static int acpi_write_ivar(device_t dev, device_t child, int index,
128 static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
129 static void acpi_reserve_resources(device_t dev);
130 static int acpi_sysres_alloc(device_t dev);
131 static int acpi_set_resource(device_t dev, device_t child, int type,
132 int rid, rman_res_t start, rman_res_t count);
133 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
134 int type, int *rid, rman_res_t start, rman_res_t end,
135 rman_res_t count, u_int flags);
136 static int acpi_adjust_resource(device_t bus, device_t child, int type,
137 struct resource *r, rman_res_t start, rman_res_t end);
138 static int acpi_release_resource(device_t bus, device_t child, int type,
139 int rid, struct resource *r);
140 static void acpi_delete_resource(device_t bus, device_t child, int type,
142 static uint32_t acpi_isa_get_logicalid(device_t dev);
143 static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
144 static int acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match);
145 static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
146 ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
148 static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
149 void *context, void **retval);
150 static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
151 int max_depth, acpi_scan_cb_t user_fn, void *arg);
152 static int acpi_set_powerstate(device_t child, int state);
153 static int acpi_isa_pnp_probe(device_t bus, device_t child,
154 struct isa_pnp_id *ids);
155 static void acpi_probe_children(device_t bus);
156 static void acpi_probe_order(ACPI_HANDLE handle, int *order);
157 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
158 void *context, void **status);
159 static void acpi_sleep_enable(void *arg);
160 static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
161 static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
162 static void acpi_shutdown_final(void *arg, int howto);
163 static void acpi_enable_fixed_events(struct acpi_softc *sc);
164 static BOOLEAN acpi_has_hid(ACPI_HANDLE handle);
165 static void acpi_resync_clock(struct acpi_softc *sc);
166 static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
167 static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
168 static int acpi_wake_prep_walk(int sstate);
169 static int acpi_wake_sysctl_walk(device_t dev);
170 static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
171 static void acpi_system_eventhandler_sleep(void *arg, int state);
172 static void acpi_system_eventhandler_wakeup(void *arg, int state);
173 static int acpi_sname2sstate(const char *sname);
174 static const char *acpi_sstate2sname(int sstate);
175 static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
176 static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
177 static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
178 static int acpi_pm_func(u_long cmd, void *arg, ...);
179 static int acpi_child_location_str_method(device_t acdev, device_t child,
180 char *buf, size_t buflen);
181 static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
182 char *buf, size_t buflen);
183 static void acpi_enable_pcie(void);
184 static void acpi_hint_device_unit(device_t acdev, device_t child,
185 const char *name, int *unitp);
186 static void acpi_reset_interfaces(device_t dev);
188 static device_method_t acpi_methods[] = {
189 /* Device interface */
190 DEVMETHOD(device_probe, acpi_probe),
191 DEVMETHOD(device_attach, acpi_attach),
192 DEVMETHOD(device_shutdown, acpi_shutdown),
193 DEVMETHOD(device_detach, bus_generic_detach),
194 DEVMETHOD(device_suspend, acpi_suspend),
195 DEVMETHOD(device_resume, acpi_resume),
198 DEVMETHOD(bus_add_child, acpi_add_child),
199 DEVMETHOD(bus_print_child, acpi_print_child),
200 DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch),
201 DEVMETHOD(bus_driver_added, acpi_driver_added),
202 DEVMETHOD(bus_read_ivar, acpi_read_ivar),
203 DEVMETHOD(bus_write_ivar, acpi_write_ivar),
204 DEVMETHOD(bus_get_resource_list, acpi_get_rlist),
205 DEVMETHOD(bus_set_resource, acpi_set_resource),
206 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
207 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
208 DEVMETHOD(bus_adjust_resource, acpi_adjust_resource),
209 DEVMETHOD(bus_release_resource, acpi_release_resource),
210 DEVMETHOD(bus_delete_resource, acpi_delete_resource),
211 DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method),
212 DEVMETHOD(bus_child_location_str, acpi_child_location_str_method),
213 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
214 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
215 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
216 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
217 DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit),
218 DEVMETHOD(bus_get_cpus, acpi_get_cpus),
219 DEVMETHOD(bus_get_domain, acpi_get_domain),
222 DEVMETHOD(acpi_id_probe, acpi_device_id_probe),
223 DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj),
224 DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep),
225 DEVMETHOD(acpi_scan_children, acpi_device_scan_children),
228 DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
233 static driver_t acpi_driver = {
236 sizeof(struct acpi_softc),
239 static devclass_t acpi_devclass;
240 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
241 MODULE_VERSION(acpi, 1);
243 ACPI_SERIAL_DECL(acpi, "ACPI root bus");
245 /* Local pools for managing system resources for ACPI child devices. */
246 static struct rman acpi_rman_io, acpi_rman_mem;
248 #define ACPI_MINIMUM_AWAKETIME 5
250 /* Holds the description of the acpi0 device. */
251 static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
253 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
254 static char acpi_ca_version[12];
255 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
256 acpi_ca_version, 0, "Version of Intel ACPI-CA");
259 * Allow overriding _OSI methods.
261 static char acpi_install_interface[256];
262 TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
263 sizeof(acpi_install_interface));
264 static char acpi_remove_interface[256];
265 TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
266 sizeof(acpi_remove_interface));
268 /* Allow users to dump Debug objects without ACPI debugger. */
269 static int acpi_debug_objects;
270 TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
271 SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
272 CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I",
273 "Enable Debug objects");
275 /* Allow the interpreter to ignore common mistakes in BIOS. */
276 static int acpi_interpreter_slack = 1;
277 TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
278 SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
279 &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
281 /* Ignore register widths set by FADT and use default widths instead. */
282 static int acpi_ignore_reg_width = 1;
283 TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width);
284 SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN,
285 &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT");
287 /* Allow users to override quirks. */
288 TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
290 int acpi_susp_bounce;
291 SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
292 &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
295 * ACPI can only be loaded as a module by the loader; activating it after
296 * system bootstrap time is not useful, and can be fatal to the system.
297 * It also cannot be unloaded, since the entire system bus hierarchy hangs
301 acpi_modevent(struct module *mod, int event, void *junk)
306 printf("The ACPI driver cannot be loaded after boot.\n");
311 if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
321 * Perform early initialization.
326 static int started = 0;
330 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
332 /* Only run the startup code once. The MADT driver also calls this. */
334 return_VALUE (AE_OK);
338 * Initialize the ACPICA subsystem.
340 if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
341 printf("ACPI: Could not initialize Subsystem: %s\n",
342 AcpiFormatException(status));
343 return_VALUE (status);
347 * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
348 * if more tables exist.
350 if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
351 printf("ACPI: Table initialisation failed: %s\n",
352 AcpiFormatException(status));
353 return_VALUE (status);
356 /* Set up any quirks we have for this system. */
357 if (acpi_quirks == ACPI_Q_OK)
358 acpi_table_quirks(&acpi_quirks);
360 /* If the user manually set the disabled hint to 0, force-enable ACPI. */
361 if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
362 acpi_quirks &= ~ACPI_Q_BROKEN;
363 if (acpi_quirks & ACPI_Q_BROKEN) {
364 printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n");
368 return_VALUE (status);
372 * Detect ACPI and perform early initialisation.
377 ACPI_TABLE_RSDP *rsdp;
378 ACPI_TABLE_HEADER *rsdt;
379 ACPI_PHYSICAL_ADDRESS paddr;
382 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
387 /* Check that we haven't been disabled with a hint. */
388 if (resource_disabled("acpi", 0))
391 /* Check for other PM systems. */
392 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
393 power_pm_get_type() != POWER_PM_TYPE_ACPI) {
394 printf("ACPI identify failed, other PM system enabled.\n");
398 /* Initialize root tables. */
399 if (ACPI_FAILURE(acpi_Startup())) {
400 printf("ACPI: Try disabling either ACPI or apic support.\n");
404 if ((paddr = AcpiOsGetRootPointer()) == 0 ||
405 (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
407 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
408 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
410 paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
411 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
413 if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
415 sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
416 sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
419 sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
423 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
425 snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
431 * Fetch some descriptive data from ACPI to put in our attach message.
434 acpi_probe(device_t dev)
437 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
439 device_set_desc(dev, acpi_desc);
441 return_VALUE (BUS_PROBE_NOWILDCARD);
445 acpi_attach(device_t dev)
447 struct acpi_softc *sc;
454 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
456 sc = device_get_softc(dev);
458 callout_init(&sc->susp_force_to, 1);
462 /* Initialize resource manager. */
463 acpi_rman_io.rm_type = RMAN_ARRAY;
464 acpi_rman_io.rm_start = 0;
465 acpi_rman_io.rm_end = 0xffff;
466 acpi_rman_io.rm_descr = "ACPI I/O ports";
467 if (rman_init(&acpi_rman_io) != 0)
468 panic("acpi rman_init IO ports failed");
469 acpi_rman_mem.rm_type = RMAN_ARRAY;
470 acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
471 if (rman_init(&acpi_rman_mem) != 0)
472 panic("acpi rman_init memory failed");
474 /* Initialise the ACPI mutex */
475 mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
478 * Set the globals from our tunables. This is needed because ACPI-CA
479 * uses UINT8 for some values and we have no tunable_byte.
481 AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
482 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
483 AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE;
487 * Disable all debugging layers and levels.
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 /* Handle MCFG table if present. */
508 * Note that some systems (specifically, those with namespace evaluation
509 * issues that require the avoidance of parts of the namespace) must
510 * avoid running _INI and _STA on everything, as well as dodging the final
513 * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
515 * XXX We should arrange for the object init pass after we have attached
516 * all our child devices, but on many systems it works here.
519 if (testenv("debug.acpi.avoid"))
520 flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
522 /* Bring the hardware and basic handlers online. */
523 if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
524 device_printf(dev, "Could not enable ACPI: %s\n",
525 AcpiFormatException(status));
530 * Call the ECDT probe function to provide EC functionality before
531 * the namespace has been evaluated.
533 * XXX This happens before the sysresource devices have been probed and
534 * attached so its resources come from nexus0. In practice, this isn't
535 * a problem but should be addressed eventually.
537 acpi_ec_ecdt_probe(dev);
539 /* Bring device objects and regions online. */
540 if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
541 device_printf(dev, "Could not initialize ACPI objects: %s\n",
542 AcpiFormatException(status));
547 * Setup our sysctl tree.
549 * XXX: This doesn't check to make sure that none of these fail.
551 sysctl_ctx_init(&sc->acpi_sysctl_ctx);
552 sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
553 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
554 device_get_name(dev), CTLFLAG_RD, 0, "");
555 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
556 OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
557 0, 0, acpi_supported_sleep_state_sysctl, "A",
558 "List supported ACPI sleep states.");
559 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
560 OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
561 &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A",
562 "Power button ACPI sleep state.");
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 "Sleep button ACPI sleep state.");
567 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
568 OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
569 &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A",
570 "Lid ACPI sleep state. Set to S3 if you want to suspend your laptop when close the Lid.");
571 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
572 OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
573 &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
574 SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
575 OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
576 &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
577 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
578 OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
579 "sleep delay in seconds");
580 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
581 OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
582 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
583 OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
584 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
585 OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
586 &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
587 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
588 OID_AUTO, "handle_reboot", CTLFLAG_RW,
589 &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
592 * Default to 1 second before sleeping to give some machines time to
595 sc->acpi_sleep_delay = 1;
597 sc->acpi_verbose = 1;
598 if ((env = kern_getenv("hw.acpi.verbose")) != NULL) {
599 if (strcmp(env, "0") != 0)
600 sc->acpi_verbose = 1;
604 /* Only enable reboot by default if the FADT says it is available. */
605 if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
606 sc->acpi_handle_reboot = 1;
608 #if !ACPI_REDUCED_HARDWARE
609 /* Only enable S4BIOS by default if the FACS says it is available. */
610 if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
614 /* Probe all supported sleep states. */
615 acpi_sleep_states[ACPI_STATE_S0] = TRUE;
616 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
617 if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT,
618 __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) &&
619 ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
620 acpi_sleep_states[state] = TRUE;
623 * Dispatch the default sleep state to devices. The lid switch is set
624 * to UNKNOWN by default to avoid surprising users.
626 sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
627 ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
628 sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
629 sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
630 ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
631 sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
632 ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
634 /* Pick the first valid sleep state for the sleep button default. */
635 sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
636 for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
637 if (acpi_sleep_states[state]) {
638 sc->acpi_sleep_button_sx = state;
642 acpi_enable_fixed_events(sc);
645 * Scan the namespace and attach/initialise children.
648 /* Register our shutdown handler. */
649 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
653 * Register our acpi event handlers.
654 * XXX should be configurable eg. via userland policy manager.
656 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
657 sc, ACPI_EVENT_PRI_LAST);
658 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
659 sc, ACPI_EVENT_PRI_LAST);
661 /* Flag our initial states. */
662 sc->acpi_enabled = TRUE;
663 sc->acpi_sstate = ACPI_STATE_S0;
664 sc->acpi_sleep_disabled = TRUE;
666 /* Create the control device */
667 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664,
669 sc->acpi_dev_t->si_drv1 = sc;
671 if ((error = acpi_machdep_init(dev)))
674 /* Register ACPI again to pass the correct argument of pm_func. */
675 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
677 if (!acpi_disabled("bus")) {
678 EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000);
679 acpi_probe_children(dev);
682 /* Update all GPEs and enable runtime GPEs. */
683 status = AcpiUpdateAllGpes();
684 if (ACPI_FAILURE(status))
685 device_printf(dev, "Could not update all GPEs: %s\n",
686 AcpiFormatException(status));
688 /* Allow sleep request after a while. */
689 callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0);
690 callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
691 acpi_sleep_enable, sc);
696 return_VALUE (error);
700 acpi_set_power_children(device_t dev, int state)
704 int dstate, i, numdevs;
706 if (device_get_children(dev, &devlist, &numdevs) != 0)
710 * Retrieve and set D-state for the sleep state if _SxD is present.
711 * Skip children who aren't attached since they are handled separately.
713 for (i = 0; i < numdevs; i++) {
716 if (device_is_attached(child) &&
717 acpi_device_pwr_for_sleep(dev, child, &dstate) == 0)
718 acpi_set_powerstate(child, dstate);
720 free(devlist, M_TEMP);
724 acpi_suspend(device_t dev)
730 error = bus_generic_suspend(dev);
732 acpi_set_power_children(dev, ACPI_STATE_D3);
738 acpi_resume(device_t dev)
743 acpi_set_power_children(dev, ACPI_STATE_D0);
745 return (bus_generic_resume(dev));
749 acpi_shutdown(device_t dev)
754 /* Allow children to shutdown first. */
755 bus_generic_shutdown(dev);
758 * Enable any GPEs that are able to power-on the system (i.e., RTC).
759 * Also, disable any that are not valid for this state (most).
761 acpi_wake_prep_walk(ACPI_STATE_S5);
767 * Handle a new device being added
770 acpi_add_child(device_t bus, u_int order, const char *name, int unit)
772 struct acpi_device *ad;
775 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
778 resource_list_init(&ad->ad_rl);
780 child = device_add_child_ordered(bus, order, name, unit);
782 device_set_ivars(child, ad);
789 acpi_print_child(device_t bus, device_t child)
791 struct acpi_device *adev = device_get_ivars(child);
792 struct resource_list *rl = &adev->ad_rl;
795 retval += bus_print_child_header(bus, child);
796 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
797 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
798 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
799 retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
800 if (device_get_flags(child))
801 retval += printf(" flags %#x", device_get_flags(child));
802 retval += bus_print_child_domain(bus, child);
803 retval += bus_print_child_footer(bus, child);
809 * If this device is an ACPI child but no one claimed it, attempt
810 * to power it off. We'll power it back up when a driver is added.
812 * XXX Disabled for now since many necessary devices (like fdc and
813 * ATA) don't claim the devices we created for them but still expect
814 * them to be powered up.
817 acpi_probe_nomatch(device_t bus, device_t child)
819 #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
820 acpi_set_powerstate(child, ACPI_STATE_D3);
825 * If a new driver has a chance to probe a child, first power it up.
827 * XXX Disabled for now (see acpi_probe_nomatch for details).
830 acpi_driver_added(device_t dev, driver_t *driver)
832 device_t child, *devlist;
835 DEVICE_IDENTIFY(driver, dev);
836 if (device_get_children(dev, &devlist, &numdevs))
838 for (i = 0; i < numdevs; i++) {
840 if (device_get_state(child) == DS_NOTPRESENT) {
841 #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
842 acpi_set_powerstate(child, ACPI_STATE_D0);
843 if (device_probe_and_attach(child) != 0)
844 acpi_set_powerstate(child, ACPI_STATE_D3);
846 device_probe_and_attach(child);
850 free(devlist, M_TEMP);
853 /* Location hint for devctl(8) */
855 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
858 struct acpi_device *dinfo = device_get_ivars(child);
862 if (dinfo->ad_handle) {
863 snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
864 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) {
865 snprintf(buf2, 32, " _PXM=%d", pxm);
866 strlcat(buf, buf2, buflen);
869 snprintf(buf, buflen, "");
874 /* PnP information for devctl(8) */
876 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
879 struct acpi_device *dinfo = device_get_ivars(child);
880 ACPI_DEVICE_INFO *adinfo;
882 if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) {
883 snprintf(buf, buflen, "unknown");
887 snprintf(buf, buflen, "_HID=%s _UID=%lu _CID=%s",
888 (adinfo->Valid & ACPI_VALID_HID) ?
889 adinfo->HardwareId.String : "none",
890 (adinfo->Valid & ACPI_VALID_UID) ?
891 strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL,
892 ((adinfo->Valid & ACPI_VALID_CID) &&
893 adinfo->CompatibleIdList.Count > 0) ?
894 adinfo->CompatibleIdList.Ids[0].String : "none");
901 * Handle per-device ivars
904 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
906 struct acpi_device *ad;
908 if ((ad = device_get_ivars(child)) == NULL) {
909 device_printf(child, "device has no ivars\n");
913 /* ACPI and ISA compatibility ivars */
915 case ACPI_IVAR_HANDLE:
916 *(ACPI_HANDLE *)result = ad->ad_handle;
918 case ACPI_IVAR_PRIVATE:
919 *(void **)result = ad->ad_private;
921 case ACPI_IVAR_FLAGS:
922 *(int *)result = ad->ad_flags;
924 case ISA_IVAR_VENDORID:
925 case ISA_IVAR_SERIAL:
926 case ISA_IVAR_COMPATID:
929 case ISA_IVAR_LOGICALID:
930 *(int *)result = acpi_isa_get_logicalid(child);
933 *(uint8_t*)result = (ad->ad_cls_class >> 16) & 0xff;
935 case PCI_IVAR_SUBCLASS:
936 *(uint8_t*)result = (ad->ad_cls_class >> 8) & 0xff;
938 case PCI_IVAR_PROGIF:
939 *(uint8_t*)result = (ad->ad_cls_class >> 0) & 0xff;
949 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
951 struct acpi_device *ad;
953 if ((ad = device_get_ivars(child)) == NULL) {
954 device_printf(child, "device has no ivars\n");
959 case ACPI_IVAR_HANDLE:
960 ad->ad_handle = (ACPI_HANDLE)value;
962 case ACPI_IVAR_PRIVATE:
963 ad->ad_private = (void *)value;
965 case ACPI_IVAR_FLAGS:
966 ad->ad_flags = (int)value;
969 panic("bad ivar write request (%d)", index);
977 * Handle child resource allocation/removal
979 static struct resource_list *
980 acpi_get_rlist(device_t dev, device_t child)
982 struct acpi_device *ad;
984 ad = device_get_ivars(child);
989 acpi_match_resource_hint(device_t dev, int type, long value)
991 struct acpi_device *ad = device_get_ivars(dev);
992 struct resource_list *rl = &ad->ad_rl;
993 struct resource_list_entry *rle;
995 STAILQ_FOREACH(rle, rl, link) {
996 if (rle->type != type)
998 if (rle->start <= value && rle->end >= value)
1005 * Wire device unit numbers based on resource matches in hints.
1008 acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
1013 int line, matches, unit;
1016 * Iterate over all the hints for the devices with the specified
1017 * name to see if one's resources are a subset of this device.
1020 while (resource_find_dev(&line, name, &unit, "at", NULL) == 0) {
1021 /* Must have an "at" for acpi or isa. */
1022 resource_string_value(name, unit, "at", &s);
1023 if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
1024 strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0))
1028 * Check for matching resources. We must have at least one match.
1029 * Since I/O and memory resources cannot be shared, if we get a
1030 * match on either of those, ignore any mismatches in IRQs or DRQs.
1032 * XXX: We may want to revisit this to be more lenient and wire
1033 * as long as it gets one match.
1036 if (resource_long_value(name, unit, "port", &value) == 0) {
1038 * Floppy drive controllers are notorious for having a
1039 * wide variety of resources not all of which include the
1040 * first port that is specified by the hint (typically
1041 * 0x3f0) (see the comment above fdc_isa_alloc_resources()
1042 * in fdc_isa.c). However, they do all seem to include
1043 * port + 2 (e.g. 0x3f2) so for a floppy device, look for
1044 * 'value + 2' in the port resources instead of the hint
1047 if (strcmp(name, "fdc") == 0)
1049 if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
1054 if (resource_long_value(name, unit, "maddr", &value) == 0) {
1055 if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
1062 if (resource_long_value(name, unit, "irq", &value) == 0) {
1063 if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
1068 if (resource_long_value(name, unit, "drq", &value) == 0) {
1069 if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
1077 /* We have a winner! */
1085 * Fetch the NUMA domain for a device by mapping the value returned by
1086 * _PXM to a NUMA domain. If the device does not have a _PXM method,
1087 * -2 is returned. If any other error occurs, -1 is returned.
1090 acpi_parse_pxm(device_t dev)
1093 #if defined(__i386__) || defined(__amd64__)
1098 handle = acpi_get_handle(dev);
1101 status = acpi_GetInteger(handle, "_PXM", &pxm);
1102 if (ACPI_SUCCESS(status))
1103 return (acpi_map_pxm_to_vm_domainid(pxm));
1104 if (status == AE_NOT_FOUND)
1112 acpi_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
1117 d = acpi_parse_pxm(child);
1119 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
1123 if (setsize != sizeof(cpuset_t))
1125 *cpuset = cpuset_domain[d];
1128 error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
1131 if (setsize != sizeof(cpuset_t))
1133 CPU_AND(cpuset, &cpuset_domain[d]);
1136 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
1141 * Fetch the NUMA domain for the given device 'dev'.
1143 * If a device has a _PXM method, map that to a NUMA domain.
1144 * Otherwise, pass the request up to the parent.
1145 * If there's no matching domain or the domain cannot be
1146 * determined, return ENOENT.
1149 acpi_get_domain(device_t dev, device_t child, int *domain)
1153 d = acpi_parse_pxm(child);
1161 /* No _PXM node; go up a level */
1162 return (bus_generic_get_domain(dev, child, domain));
1166 * Pre-allocate/manage all memory and IO resources. Since rman can't handle
1167 * duplicates, we merge any in the sysresource attach routine.
1170 acpi_sysres_alloc(device_t dev)
1172 struct resource *res;
1173 struct resource_list *rl;
1174 struct resource_list_entry *rle;
1180 * Probe/attach any sysresource devices. This would be unnecessary if we
1181 * had multi-pass probe/attach.
1183 if (device_get_children(dev, &children, &child_count) != 0)
1185 for (i = 0; i < child_count; i++) {
1186 if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1187 device_probe_and_attach(children[i]);
1189 free(children, M_TEMP);
1191 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
1192 STAILQ_FOREACH(rle, rl, link) {
1193 if (rle->res != NULL) {
1194 device_printf(dev, "duplicate resource for %jx\n", rle->start);
1198 /* Only memory and IO resources are valid here. */
1199 switch (rle->type) {
1200 case SYS_RES_IOPORT:
1203 case SYS_RES_MEMORY:
1204 rm = &acpi_rman_mem;
1210 /* Pre-allocate resource and add to our rman pool. */
1211 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
1212 &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
1214 rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1216 } else if (bootverbose)
1217 device_printf(dev, "reservation of %jx, %jx (%d) failed\n",
1218 rle->start, rle->count, rle->type);
1224 * Reserve declared resources for devices found during attach once system
1225 * resources have been allocated.
1228 acpi_reserve_resources(device_t dev)
1230 struct resource_list_entry *rle;
1231 struct resource_list *rl;
1232 struct acpi_device *ad;
1233 struct acpi_softc *sc;
1237 sc = device_get_softc(dev);
1238 if (device_get_children(dev, &children, &child_count) != 0)
1240 for (i = 0; i < child_count; i++) {
1241 ad = device_get_ivars(children[i]);
1244 /* Don't reserve system resources. */
1245 if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1248 STAILQ_FOREACH(rle, rl, link) {
1250 * Don't reserve IRQ resources. There are many sticky things
1251 * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1252 * when using legacy routing).
1254 if (rle->type == SYS_RES_IRQ)
1258 * Don't reserve the resource if it is already allocated.
1259 * The acpi_ec(4) driver can allocate its resources early
1260 * if ECDT is present.
1262 if (rle->res != NULL)
1266 * Try to reserve the resource from our parent. If this
1267 * fails because the resource is a system resource, just
1268 * let it be. The resource range is already reserved so
1269 * that other devices will not use it. If the driver
1270 * needs to allocate the resource, then
1271 * acpi_alloc_resource() will sub-alloc from the system
1274 resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1275 rle->start, rle->end, rle->count, 0);
1278 free(children, M_TEMP);
1279 sc->acpi_resources_reserved = 1;
1283 acpi_set_resource(device_t dev, device_t child, int type, int rid,
1284 rman_res_t start, rman_res_t count)
1286 struct acpi_softc *sc = device_get_softc(dev);
1287 struct acpi_device *ad = device_get_ivars(child);
1288 struct resource_list *rl = &ad->ad_rl;
1289 ACPI_DEVICE_INFO *devinfo;
1293 /* Ignore IRQ resources for PCI link devices. */
1294 if (type == SYS_RES_IRQ &&
1295 ACPI_ID_PROBE(dev, child, pcilink_ids, NULL) <= 0)
1299 * Ignore most resources for PCI root bridges. Some BIOSes
1300 * incorrectly enumerate the memory ranges they decode as plain
1301 * memory resources instead of as ResourceProducer ranges. Other
1302 * BIOSes incorrectly list system resource entries for I/O ranges
1303 * under the PCI bridge. Do allow the one known-correct case on
1304 * x86 of a PCI bridge claiming the I/O ports used for PCI config
1307 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
1308 if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) {
1309 if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) {
1310 #if defined(__i386__) || defined(__amd64__)
1311 allow = (type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT);
1316 AcpiOsFree(devinfo);
1320 AcpiOsFree(devinfo);
1325 /* map with default for now */
1326 if (type == SYS_RES_IRQ)
1327 start = (rman_res_t)acpi_map_intr(child, (u_int)start,
1328 acpi_get_handle(child));
1331 /* If the resource is already allocated, fail. */
1332 if (resource_list_busy(rl, type, rid))
1335 /* If the resource is already reserved, release it. */
1336 if (resource_list_reserved(rl, type, rid))
1337 resource_list_unreserve(rl, dev, child, type, rid);
1339 /* Add the resource. */
1340 end = (start + count - 1);
1341 resource_list_add(rl, type, rid, start, end, count);
1343 /* Don't reserve resources until the system resources are allocated. */
1344 if (!sc->acpi_resources_reserved)
1347 /* Don't reserve system resources. */
1348 if (ACPI_ID_PROBE(dev, child, sysres_ids, NULL) <= 0)
1352 * Don't reserve IRQ resources. There are many sticky things to
1353 * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when
1354 * using legacy routing).
1356 if (type == SYS_RES_IRQ)
1360 * Don't reserve resources for CPU devices. Some of these
1361 * resources need to be allocated as shareable, but reservations
1362 * are always non-shareable.
1364 if (device_get_devclass(child) == devclass_find("cpu"))
1368 * Reserve the resource.
1370 * XXX: Ignores failure for now. Failure here is probably a
1371 * BIOS/firmware bug?
1373 resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0);
1377 static struct resource *
1378 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
1379 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1384 struct acpi_device *ad;
1385 struct resource_list_entry *rle;
1386 struct resource_list *rl;
1387 struct resource *res;
1388 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
1391 * First attempt at allocating the resource. For direct children,
1392 * use resource_list_alloc() to handle reserved resources. For
1393 * other devices, pass the request up to our parent.
1395 if (bus == device_get_parent(child)) {
1396 ad = device_get_ivars(child);
1400 * Simulate the behavior of the ISA bus for direct children
1401 * devices. That is, if a non-default range is specified for
1402 * a resource that doesn't exist, use bus_set_resource() to
1403 * add the resource before allocating it. Note that these
1404 * resources will not be reserved.
1406 if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1407 resource_list_add(rl, type, *rid, start, end, count);
1408 res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1411 if (res != NULL && type == SYS_RES_IRQ) {
1413 * Since bus_config_intr() takes immediate effect, we cannot
1414 * configure the interrupt associated with a device when we
1415 * parse the resources but have to defer it until a driver
1416 * actually allocates the interrupt via bus_alloc_resource().
1418 * XXX: Should we handle the lookup failing?
1420 if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
1421 acpi_config_intr(child, &ares);
1426 * If this is an allocation of the "default" range for a given
1427 * RID, fetch the exact bounds for this resource from the
1428 * resource list entry to try to allocate the range from the
1429 * system resource regions.
1431 if (res == NULL && isdefault) {
1432 rle = resource_list_find(rl, type, *rid);
1440 res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
1441 start, end, count, flags);
1444 * If the first attempt failed and this is an allocation of a
1445 * specific range, try to satisfy the request via a suballocation
1446 * from our system resource regions.
1448 if (res == NULL && start + count - 1 == end)
1449 res = acpi_alloc_sysres(child, type, rid, start, end, count, flags);
1454 * Attempt to allocate a specific resource range from the system
1455 * resource ranges. Note that we only handle memory and I/O port
1459 acpi_alloc_sysres(device_t child, int type, int *rid, rman_res_t start,
1460 rman_res_t end, rman_res_t count, u_int flags)
1463 struct resource *res;
1466 case SYS_RES_IOPORT:
1469 case SYS_RES_MEMORY:
1470 rm = &acpi_rman_mem;
1476 KASSERT(start + count - 1 == end, ("wildcard resource range"));
1477 res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1482 rman_set_rid(res, *rid);
1484 /* If requested, activate the resource using the parent's method. */
1485 if (flags & RF_ACTIVE)
1486 if (bus_activate_resource(child, type, *rid, res) != 0) {
1487 rman_release_resource(res);
1495 acpi_is_resource_managed(int type, struct resource *r)
1498 /* We only handle memory and IO resources through rman. */
1500 case SYS_RES_IOPORT:
1501 return (rman_is_region_manager(r, &acpi_rman_io));
1502 case SYS_RES_MEMORY:
1503 return (rman_is_region_manager(r, &acpi_rman_mem));
1509 acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
1510 rman_res_t start, rman_res_t end)
1513 if (acpi_is_resource_managed(type, r))
1514 return (rman_adjust_resource(r, start, end));
1515 return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1519 acpi_release_resource(device_t bus, device_t child, int type, int rid,
1525 * If this resource belongs to one of our internal managers,
1526 * deactivate it and release it to the local pool.
1528 if (acpi_is_resource_managed(type, r)) {
1529 if (rman_get_flags(r) & RF_ACTIVE) {
1530 ret = bus_deactivate_resource(child, type, rid, r);
1534 return (rman_release_resource(r));
1537 return (bus_generic_rl_release_resource(bus, child, type, rid, r));
1541 acpi_delete_resource(device_t bus, device_t child, int type, int rid)
1543 struct resource_list *rl;
1545 rl = acpi_get_rlist(bus, child);
1546 if (resource_list_busy(rl, type, rid)) {
1547 device_printf(bus, "delete_resource: Resource still owned by child"
1548 " (type=%d, rid=%d)\n", type, rid);
1551 resource_list_unreserve(rl, bus, child, type, rid);
1552 resource_list_delete(rl, type, rid);
1555 /* Allocate an IO port or memory resource, given its GAS. */
1557 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1558 struct resource **res, u_int flags)
1560 int error, res_type;
1563 if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1566 /* We only support memory and IO spaces. */
1567 switch (gas->SpaceId) {
1568 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1569 res_type = SYS_RES_MEMORY;
1571 case ACPI_ADR_SPACE_SYSTEM_IO:
1572 res_type = SYS_RES_IOPORT;
1575 return (EOPNOTSUPP);
1579 * If the register width is less than 8, assume the BIOS author means
1580 * it is a bit field and just allocate a byte.
1582 if (gas->BitWidth && gas->BitWidth < 8)
1585 /* Validate the address after we're sure we support the space. */
1586 if (gas->Address == 0 || gas->BitWidth == 0)
1589 bus_set_resource(dev, res_type, *rid, gas->Address,
1591 *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1596 bus_delete_resource(dev, res_type, *rid);
1601 /* Probe _HID and _CID for compatible ISA PNP ids. */
1603 acpi_isa_get_logicalid(device_t dev)
1605 ACPI_DEVICE_INFO *devinfo;
1609 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1611 /* Fetch and validate the HID. */
1612 if ((h = acpi_get_handle(dev)) == NULL ||
1613 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1616 pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
1617 devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
1618 PNP_EISAID(devinfo->HardwareId.String) : 0;
1619 AcpiOsFree(devinfo);
1621 return_VALUE (pnpid);
1625 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1627 ACPI_DEVICE_INFO *devinfo;
1628 ACPI_PNP_DEVICE_ID *ids;
1633 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1637 /* Fetch and validate the CID */
1638 if ((h = acpi_get_handle(dev)) == NULL ||
1639 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1642 if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
1643 AcpiOsFree(devinfo);
1647 if (devinfo->CompatibleIdList.Count < count)
1648 count = devinfo->CompatibleIdList.Count;
1649 ids = devinfo->CompatibleIdList.Ids;
1650 for (i = 0, valid = 0; i < count; i++)
1651 if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
1652 strncmp(ids[i].String, "PNP", 3) == 0) {
1653 *pnpid++ = PNP_EISAID(ids[i].String);
1656 AcpiOsFree(devinfo);
1658 return_VALUE (valid);
1662 acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match)
1669 h = acpi_get_handle(dev);
1670 if (ids == NULL || h == NULL)
1672 t = acpi_get_type(dev);
1673 if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
1676 /* Try to match one of the array of IDs with a HID or CID. */
1677 for (i = 0; ids[i] != NULL; i++) {
1678 rv = acpi_MatchHid(h, ids[i]);
1679 if (rv == ACPI_MATCHHID_NOMATCH)
1682 if (match != NULL) {
1685 return ((rv == ACPI_MATCHHID_HID)?
1686 BUS_PROBE_DEFAULT : BUS_PROBE_LOW_PRIORITY);
1692 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1693 ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1698 h = ACPI_ROOT_OBJECT;
1699 else if ((h = acpi_get_handle(dev)) == NULL)
1700 return (AE_BAD_PARAMETER);
1701 return (AcpiEvaluateObject(h, pathname, parameters, ret));
1705 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
1707 struct acpi_softc *sc;
1712 handle = acpi_get_handle(dev);
1715 * XXX If we find these devices, don't try to power them down.
1716 * The serial and IRDA ports on my T23 hang the system when
1717 * set to D3 and it appears that such legacy devices may
1718 * need special handling in their drivers.
1720 if (dstate == NULL || handle == NULL ||
1721 acpi_MatchHid(handle, "PNP0500") ||
1722 acpi_MatchHid(handle, "PNP0501") ||
1723 acpi_MatchHid(handle, "PNP0502") ||
1724 acpi_MatchHid(handle, "PNP0510") ||
1725 acpi_MatchHid(handle, "PNP0511"))
1729 * Override next state with the value from _SxD, if present.
1730 * Note illegal _S0D is evaluated because some systems expect this.
1732 sc = device_get_softc(bus);
1733 snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
1734 status = acpi_GetInteger(handle, sxd, dstate);
1735 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
1736 device_printf(dev, "failed to get %s on %s: %s\n", sxd,
1737 acpi_name(handle), AcpiFormatException(status));
1744 /* Callback arg for our implementation of walking the namespace. */
1745 struct acpi_device_scan_ctx {
1746 acpi_scan_cb_t user_fn;
1752 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1754 struct acpi_device_scan_ctx *ctx;
1755 device_t dev, old_dev;
1757 ACPI_OBJECT_TYPE type;
1760 * Skip this device if we think we'll have trouble with it or it is
1761 * the parent where the scan began.
1763 ctx = (struct acpi_device_scan_ctx *)arg;
1764 if (acpi_avoid(h) || h == ctx->parent)
1767 /* If this is not a valid device type (e.g., a method), skip it. */
1768 if (ACPI_FAILURE(AcpiGetType(h, &type)))
1770 if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1771 type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1775 * Call the user function with the current device. If it is unchanged
1776 * afterwards, return. Otherwise, we update the handle to the new dev.
1778 old_dev = acpi_get_device(h);
1780 status = ctx->user_fn(h, &dev, level, ctx->arg);
1781 if (ACPI_FAILURE(status) || old_dev == dev)
1784 /* Remove the old child and its connection to the handle. */
1785 if (old_dev != NULL) {
1786 device_delete_child(device_get_parent(old_dev), old_dev);
1787 AcpiDetachData(h, acpi_fake_objhandler);
1790 /* Recreate the handle association if the user created a device. */
1792 AcpiAttachData(h, acpi_fake_objhandler, dev);
1798 acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1799 acpi_scan_cb_t user_fn, void *arg)
1802 struct acpi_device_scan_ctx ctx;
1804 if (acpi_disabled("children"))
1808 h = ACPI_ROOT_OBJECT;
1809 else if ((h = acpi_get_handle(dev)) == NULL)
1810 return (AE_BAD_PARAMETER);
1811 ctx.user_fn = user_fn;
1814 return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1815 acpi_device_scan_cb, NULL, &ctx, NULL));
1819 * Even though ACPI devices are not PCI, we use the PCI approach for setting
1820 * device power states since it's close enough to ACPI.
1823 acpi_set_powerstate(device_t child, int state)
1828 h = acpi_get_handle(child);
1829 if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
1834 /* Ignore errors if the power methods aren't present. */
1835 status = acpi_pwr_switch_consumer(h, state);
1836 if (ACPI_SUCCESS(status)) {
1838 device_printf(child, "set ACPI power state D%d on %s\n",
1839 state, acpi_name(h));
1840 } else if (status != AE_NOT_FOUND)
1841 device_printf(child,
1842 "failed to set ACPI power state D%d on %s: %s\n", state,
1843 acpi_name(h), AcpiFormatException(status));
1849 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
1851 int result, cid_count, i;
1852 uint32_t lid, cids[8];
1854 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1857 * ISA-style drivers attached to ACPI may persist and
1858 * probe manually if we return ENOENT. We never want
1859 * that to happen, so don't ever return it.
1863 /* Scan the supplied IDs for a match */
1864 lid = acpi_isa_get_logicalid(child);
1865 cid_count = acpi_isa_get_compatid(child, cids, 8);
1866 while (ids && ids->ip_id) {
1867 if (lid == ids->ip_id) {
1871 for (i = 0; i < cid_count; i++) {
1872 if (cids[i] == ids->ip_id) {
1881 if (result == 0 && ids->ip_desc)
1882 device_set_desc(child, ids->ip_desc);
1884 return_VALUE (result);
1888 * Look for a MCFG table. If it is present, use the settings for
1889 * domain (segment) 0 to setup PCI config space access via the memory
1892 * On non-x86 architectures (arm64 for now), this will be done from the
1893 * PCI host bridge driver.
1896 acpi_enable_pcie(void)
1898 #if defined(__i386__) || defined(__amd64__)
1899 ACPI_TABLE_HEADER *hdr;
1900 ACPI_MCFG_ALLOCATION *alloc, *end;
1903 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1904 if (ACPI_FAILURE(status))
1907 end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1908 alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1909 while (alloc < end) {
1910 if (alloc->PciSegment == 0) {
1911 pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1912 alloc->EndBusNumber);
1921 * Scan all of the ACPI namespace and attach child devices.
1923 * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
1924 * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
1925 * However, in violation of the spec, some systems place their PCI link
1926 * devices in \, so we have to walk the whole namespace. We check the
1927 * type of namespace nodes, so this should be ok.
1930 acpi_probe_children(device_t bus)
1933 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1936 * Scan the namespace and insert placeholders for all the devices that
1937 * we find. We also probe/attach any early devices.
1939 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1940 * we want to create nodes for all devices, not just those that are
1941 * currently present. (This assumes that we don't want to create/remove
1942 * devices as they appear, which might be smarter.)
1944 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1945 AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1948 /* Pre-allocate resources for our rman from any sysresource devices. */
1949 acpi_sysres_alloc(bus);
1951 /* Reserve resources already allocated to children. */
1952 acpi_reserve_resources(bus);
1954 /* Create any static children by calling device identify methods. */
1955 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1956 bus_generic_probe(bus);
1958 /* Probe/attach all children, created statically and from the namespace. */
1959 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n"));
1960 bus_generic_attach(bus);
1962 /* Attach wake sysctls. */
1963 acpi_wake_sysctl_walk(bus);
1965 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
1970 * Determine the probe order for a given device.
1973 acpi_probe_order(ACPI_HANDLE handle, int *order)
1975 ACPI_OBJECT_TYPE type;
1979 * 1. I/O port and memory system resource holders
1980 * 2. Clocks and timers (to handle early accesses)
1981 * 3. Embedded controllers (to handle early accesses)
1982 * 4. PCI Link Devices
1984 AcpiGetType(handle, &type);
1985 if (type == ACPI_TYPE_PROCESSOR)
1987 else if (acpi_MatchHid(handle, "PNP0C01") ||
1988 acpi_MatchHid(handle, "PNP0C02"))
1990 else if (acpi_MatchHid(handle, "PNP0100") ||
1991 acpi_MatchHid(handle, "PNP0103") ||
1992 acpi_MatchHid(handle, "PNP0B00"))
1994 else if (acpi_MatchHid(handle, "PNP0C09"))
1996 else if (acpi_MatchHid(handle, "PNP0C0F"))
2001 * Evaluate a child device and determine whether we might attach a device to
2005 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2007 ACPI_DEVICE_INFO *devinfo;
2008 struct acpi_device *ad;
2009 struct acpi_prw_data prw;
2010 ACPI_OBJECT_TYPE type;
2012 device_t bus, child;
2016 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2018 if (acpi_disabled("children"))
2019 return_ACPI_STATUS (AE_OK);
2021 /* Skip this device if we think we'll have trouble with it. */
2022 if (acpi_avoid(handle))
2023 return_ACPI_STATUS (AE_OK);
2025 bus = (device_t)context;
2026 if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
2027 handle_str = acpi_name(handle);
2029 case ACPI_TYPE_DEVICE:
2031 * Since we scan from \, be sure to skip system scope objects.
2032 * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
2033 * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run
2034 * during the initialization and \_TZ_ is to support Notify() on it.
2036 if (strcmp(handle_str, "\\_SB_") == 0 ||
2037 strcmp(handle_str, "\\_TZ_") == 0)
2039 if (acpi_parse_prw(handle, &prw) == 0)
2040 AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
2043 * Ignore devices that do not have a _HID or _CID. They should
2044 * be discovered by other buses (e.g. the PCI bus driver).
2046 if (!acpi_has_hid(handle))
2049 case ACPI_TYPE_PROCESSOR:
2050 case ACPI_TYPE_THERMAL:
2051 case ACPI_TYPE_POWER:
2053 * Create a placeholder device for this node. Sort the
2054 * placeholder so that the probe/attach passes will run
2055 * breadth-first. Orders less than ACPI_DEV_BASE_ORDER
2056 * are reserved for special objects (i.e., system
2059 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
2060 order = level * 10 + ACPI_DEV_BASE_ORDER;
2061 acpi_probe_order(handle, &order);
2062 child = BUS_ADD_CHILD(bus, order, NULL, -1);
2066 /* Associate the handle with the device_t and vice versa. */
2067 acpi_set_handle(child, handle);
2068 AcpiAttachData(handle, acpi_fake_objhandler, child);
2071 * Check that the device is present. If it's not present,
2072 * leave it disabled (so that we have a device_t attached to
2073 * the handle, but we don't probe it).
2075 * XXX PCI link devices sometimes report "present" but not
2076 * "functional" (i.e. if disabled). Go ahead and probe them
2077 * anyway since we may enable them later.
2079 if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
2080 /* Never disable PCI link devices. */
2081 if (acpi_MatchHid(handle, "PNP0C0F"))
2084 * Docking stations should remain enabled since the system
2085 * may be undocked at boot.
2087 if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
2090 device_disable(child);
2095 * Get the device's resource settings and attach them.
2096 * Note that if the device has _PRS but no _CRS, we need
2097 * to decide when it's appropriate to try to configure the
2098 * device. Ignore the return value here; it's OK for the
2099 * device not to have any resources.
2101 acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
2103 ad = device_get_ivars(child);
2104 ad->ad_cls_class = 0xffffff;
2105 if (ACPI_SUCCESS(AcpiGetObjectInfo(handle, &devinfo))) {
2106 if ((devinfo->Valid & ACPI_VALID_CLS) != 0 &&
2107 devinfo->ClassCode.Length >= ACPI_PCICLS_STRING_SIZE) {
2108 ad->ad_cls_class = strtoul(devinfo->ClassCode.String,
2111 AcpiOsFree(devinfo);
2117 return_ACPI_STATUS (AE_OK);
2121 * AcpiAttachData() requires an object handler but never uses it. This is a
2122 * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
2125 acpi_fake_objhandler(ACPI_HANDLE h, void *data)
2130 acpi_shutdown_final(void *arg, int howto)
2132 struct acpi_softc *sc = (struct acpi_softc *)arg;
2137 * XXX Shutdown code should only run on the BSP (cpuid 0).
2138 * Some chipsets do not power off the system correctly if called from
2141 if ((howto & RB_POWEROFF) != 0) {
2142 status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
2143 if (ACPI_FAILURE(status)) {
2144 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2145 AcpiFormatException(status));
2148 device_printf(sc->acpi_dev, "Powering system off\n");
2149 intr = intr_disable();
2150 status = AcpiEnterSleepState(ACPI_STATE_S5);
2151 if (ACPI_FAILURE(status)) {
2153 device_printf(sc->acpi_dev, "power-off failed - %s\n",
2154 AcpiFormatException(status));
2158 device_printf(sc->acpi_dev, "power-off failed - timeout\n");
2160 } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
2161 /* Reboot using the reset register. */
2162 status = AcpiReset();
2163 if (ACPI_SUCCESS(status)) {
2165 device_printf(sc->acpi_dev, "reset failed - timeout\n");
2166 } else if (status != AE_NOT_EXIST)
2167 device_printf(sc->acpi_dev, "reset failed - %s\n",
2168 AcpiFormatException(status));
2169 } else if (sc->acpi_do_disable && !KERNEL_PANICKED()) {
2171 * Only disable ACPI if the user requested. On some systems, writing
2172 * the disable value to SMI_CMD hangs the system.
2174 device_printf(sc->acpi_dev, "Shutting down\n");
2180 acpi_enable_fixed_events(struct acpi_softc *sc)
2182 static int first_time = 1;
2184 /* Enable and clear fixed events and install handlers. */
2185 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
2186 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
2187 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
2188 acpi_event_power_button_sleep, sc);
2190 device_printf(sc->acpi_dev, "Power Button (fixed)\n");
2192 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
2193 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
2194 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
2195 acpi_event_sleep_button_sleep, sc);
2197 device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
2204 * Returns true if the device is actually present and should
2205 * be attached to. This requires the present, enabled, UI-visible
2206 * and diagnostics-passed bits to be set.
2209 acpi_DeviceIsPresent(device_t dev)
2215 h = acpi_get_handle(dev);
2219 * Certain Treadripper boards always returns 0 for FreeBSD because it
2220 * only returns non-zero for the OS string "Windows 2015". Otherwise it
2221 * will return zero. Force them to always be treated as present.
2222 * Beata versions were worse: they always returned 0.
2224 if (acpi_MatchHid(h, "AMDI0020") || acpi_MatchHid(h, "AMDI0010"))
2227 status = acpi_GetInteger(h, "_STA", &s);
2230 * If no _STA method or if it failed, then assume that
2231 * the device is present.
2233 if (ACPI_FAILURE(status))
2236 return (ACPI_DEVICE_PRESENT(s) ? TRUE : FALSE);
2240 * Returns true if the battery is actually present and inserted.
2243 acpi_BatteryIsPresent(device_t dev)
2249 h = acpi_get_handle(dev);
2252 status = acpi_GetInteger(h, "_STA", &s);
2255 * If no _STA method or if it failed, then assume that
2256 * the device is present.
2258 if (ACPI_FAILURE(status))
2261 return (ACPI_BATTERY_PRESENT(s) ? TRUE : FALSE);
2265 * Returns true if a device has at least one valid device ID.
2268 acpi_has_hid(ACPI_HANDLE h)
2270 ACPI_DEVICE_INFO *devinfo;
2274 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2278 if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2280 else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2281 if (devinfo->CompatibleIdList.Count > 0)
2284 AcpiOsFree(devinfo);
2289 * Match a HID string against a handle
2290 * returns ACPI_MATCHHID_HID if _HID match
2291 * ACPI_MATCHHID_CID if _CID match and not _HID match.
2292 * ACPI_MATCHHID_NOMATCH=0 if no match.
2295 acpi_MatchHid(ACPI_HANDLE h, const char *hid)
2297 ACPI_DEVICE_INFO *devinfo;
2301 if (hid == NULL || h == NULL ||
2302 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2303 return (ACPI_MATCHHID_NOMATCH);
2305 ret = ACPI_MATCHHID_NOMATCH;
2306 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
2307 strcmp(hid, devinfo->HardwareId.String) == 0)
2308 ret = ACPI_MATCHHID_HID;
2309 else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2310 for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
2311 if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
2312 ret = ACPI_MATCHHID_CID;
2317 AcpiOsFree(devinfo);
2322 * Return the handle of a named object within our scope, ie. that of (parent)
2323 * or one if its parents.
2326 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2331 /* Walk back up the tree to the root */
2333 status = AcpiGetHandle(parent, path, &r);
2334 if (ACPI_SUCCESS(status)) {
2338 /* XXX Return error here? */
2339 if (status != AE_NOT_FOUND)
2341 if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2342 return (AE_NOT_FOUND);
2348 * Allocate a buffer with a preset data size.
2351 acpi_AllocBuffer(int size)
2355 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2358 buf->Pointer = (void *)(buf + 1);
2363 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2366 ACPI_OBJECT_LIST args;
2368 arg1.Type = ACPI_TYPE_INTEGER;
2369 arg1.Integer.Value = number;
2371 args.Pointer = &arg1;
2373 return (AcpiEvaluateObject(handle, path, &args, NULL));
2377 * Evaluate a path that should return an integer.
2380 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2387 handle = ACPI_ROOT_OBJECT;
2390 * Assume that what we've been pointed at is an Integer object, or
2391 * a method that will return an Integer.
2393 buf.Pointer = ¶m;
2394 buf.Length = sizeof(param);
2395 status = AcpiEvaluateObject(handle, path, NULL, &buf);
2396 if (ACPI_SUCCESS(status)) {
2397 if (param.Type == ACPI_TYPE_INTEGER)
2398 *number = param.Integer.Value;
2404 * In some applications, a method that's expected to return an Integer
2405 * may instead return a Buffer (probably to simplify some internal
2406 * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
2407 * convert it into an Integer as best we can.
2411 if (status == AE_BUFFER_OVERFLOW) {
2412 if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2413 status = AE_NO_MEMORY;
2415 status = AcpiEvaluateObject(handle, path, NULL, &buf);
2416 if (ACPI_SUCCESS(status))
2417 status = acpi_ConvertBufferToInteger(&buf, number);
2418 AcpiOsFree(buf.Pointer);
2425 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2431 p = (ACPI_OBJECT *)bufp->Pointer;
2432 if (p->Type == ACPI_TYPE_INTEGER) {
2433 *number = p->Integer.Value;
2436 if (p->Type != ACPI_TYPE_BUFFER)
2438 if (p->Buffer.Length > sizeof(int))
2439 return (AE_BAD_DATA);
2442 val = p->Buffer.Pointer;
2443 for (i = 0; i < p->Buffer.Length; i++)
2444 *number += val[i] << (i * 8);
2449 * Iterate over the elements of an a package object, calling the supplied
2450 * function for each element.
2452 * XXX possible enhancement might be to abort traversal on error.
2455 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2456 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2461 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2462 return (AE_BAD_PARAMETER);
2464 /* Iterate over components */
2466 comp = pkg->Package.Elements;
2467 for (; i < pkg->Package.Count; i++, comp++)
2474 * Find the (index)th resource object in a set.
2477 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
2482 rp = (ACPI_RESOURCE *)buf->Pointer;
2486 if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2487 return (AE_BAD_PARAMETER);
2489 /* Check for terminator */
2490 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2491 return (AE_NOT_FOUND);
2492 rp = ACPI_NEXT_RESOURCE(rp);
2501 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
2503 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
2504 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
2505 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
2508 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
2511 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
2516 /* Initialise the buffer if necessary. */
2517 if (buf->Pointer == NULL) {
2518 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
2519 if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
2520 return (AE_NO_MEMORY);
2521 rp = (ACPI_RESOURCE *)buf->Pointer;
2522 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2523 rp->Length = ACPI_RS_SIZE_MIN;
2529 * Scan the current buffer looking for the terminator.
2530 * This will either find the terminator or hit the end
2531 * of the buffer and return an error.
2533 rp = (ACPI_RESOURCE *)buf->Pointer;
2535 /* Range check, don't go outside the buffer */
2536 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
2537 return (AE_BAD_PARAMETER);
2538 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
2540 rp = ACPI_NEXT_RESOURCE(rp);
2544 * Check the size of the buffer and expand if required.
2547 * size of existing resources before terminator +
2548 * size of new resource and header +
2549 * size of terminator.
2551 * Note that this loop should really only run once, unless
2552 * for some reason we are stuffing a *really* huge resource.
2554 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2555 res->Length + ACPI_RS_SIZE_NO_DATA +
2556 ACPI_RS_SIZE_MIN) >= buf->Length) {
2557 if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
2558 return (AE_NO_MEMORY);
2559 bcopy(buf->Pointer, newp, buf->Length);
2560 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2561 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
2562 AcpiOsFree(buf->Pointer);
2563 buf->Pointer = newp;
2564 buf->Length += buf->Length;
2567 /* Insert the new resource. */
2568 bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
2570 /* And add the terminator. */
2571 rp = ACPI_NEXT_RESOURCE(rp);
2572 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
2573 rp->Length = ACPI_RS_SIZE_MIN;
2579 acpi_DSMQuery(ACPI_HANDLE h, uint8_t *uuid, int revision)
2582 * ACPI spec 9.1.1 defines this.
2584 * "Arg2: Function Index Represents a specific function whose meaning is
2585 * specific to the UUID and Revision ID. Function indices should start
2586 * with 1. Function number zero is a query function (see the special
2587 * return code defined below)."
2593 if (!ACPI_SUCCESS(acpi_EvaluateDSM(h, uuid, revision, 0, NULL, &buf))) {
2594 ACPI_INFO(("Failed to enumerate DSM functions\n"));
2598 obj = (ACPI_OBJECT *)buf.Pointer;
2599 KASSERT(obj, ("Object not allowed to be NULL\n"));
2602 * From ACPI 6.2 spec 9.1.1:
2603 * If Function Index = 0, a Buffer containing a function index bitfield.
2604 * Otherwise, the return value and type depends on the UUID and revision
2607 switch (obj->Type) {
2608 case ACPI_TYPE_BUFFER:
2609 ret = *(uint8_t *)obj->Buffer.Pointer;
2611 case ACPI_TYPE_INTEGER:
2612 ACPI_BIOS_WARNING((AE_INFO,
2613 "Possibly buggy BIOS with ACPI_TYPE_INTEGER for function enumeration\n"));
2614 ret = obj->Integer.Value & 0xFF;
2617 ACPI_WARNING((AE_INFO, "Unexpected return type %u\n", obj->Type));
2625 * DSM may return multiple types depending on the function. It is therefore
2626 * unsafe to use the typed evaluation. It is highly recommended that the caller
2627 * check the type of the returned object.
2630 acpi_EvaluateDSM(ACPI_HANDLE handle, uint8_t *uuid, int revision,
2631 uint64_t function, union acpi_object *package, ACPI_BUFFER *out_buf)
2634 ACPI_OBJECT_LIST arglist;
2638 if (out_buf == NULL)
2639 return (AE_NO_MEMORY);
2641 arg[0].Type = ACPI_TYPE_BUFFER;
2642 arg[0].Buffer.Length = ACPI_UUID_LENGTH;
2643 arg[0].Buffer.Pointer = uuid;
2644 arg[1].Type = ACPI_TYPE_INTEGER;
2645 arg[1].Integer.Value = revision;
2646 arg[2].Type = ACPI_TYPE_INTEGER;
2647 arg[2].Integer.Value = function;
2651 arg[3].Type = ACPI_TYPE_PACKAGE;
2652 arg[3].Package.Count = 0;
2653 arg[3].Package.Elements = NULL;
2656 arglist.Pointer = arg;
2659 buf.Length = ACPI_ALLOCATE_BUFFER;
2660 status = AcpiEvaluateObject(handle, "_DSM", &arglist, &buf);
2661 if (ACPI_FAILURE(status))
2664 KASSERT(ACPI_SUCCESS(status), ("Unexpected status"));
2671 acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count,
2672 uint32_t *caps_in, uint32_t *caps_out, bool query)
2674 ACPI_OBJECT arg[4], *ret;
2675 ACPI_OBJECT_LIST arglist;
2679 arglist.Pointer = arg;
2681 arg[0].Type = ACPI_TYPE_BUFFER;
2682 arg[0].Buffer.Length = ACPI_UUID_LENGTH;
2683 arg[0].Buffer.Pointer = uuid;
2684 arg[1].Type = ACPI_TYPE_INTEGER;
2685 arg[1].Integer.Value = revision;
2686 arg[2].Type = ACPI_TYPE_INTEGER;
2687 arg[2].Integer.Value = count;
2688 arg[3].Type = ACPI_TYPE_BUFFER;
2689 arg[3].Buffer.Length = count * sizeof(*caps_in);
2690 arg[3].Buffer.Pointer = (uint8_t *)caps_in;
2691 caps_in[0] = query ? 1 : 0;
2693 buf.Length = ACPI_ALLOCATE_BUFFER;
2694 status = AcpiEvaluateObjectTyped(handle, "_OSC", &arglist, &buf,
2696 if (ACPI_FAILURE(status))
2698 if (caps_out != NULL) {
2700 if (ret->Buffer.Length != count * sizeof(*caps_out)) {
2701 AcpiOsFree(buf.Pointer);
2702 return (AE_BUFFER_OVERFLOW);
2704 bcopy(ret->Buffer.Pointer, caps_out, ret->Buffer.Length);
2706 AcpiOsFree(buf.Pointer);
2711 * Set interrupt model.
2714 acpi_SetIntrModel(int model)
2717 return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2721 * Walk subtables of a table and call a callback routine for each
2722 * subtable. The caller should provide the first subtable and a
2723 * pointer to the end of the table. This can be used to walk tables
2724 * such as MADT and SRAT that use subtable entries.
2727 acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2730 ACPI_SUBTABLE_HEADER *entry;
2732 for (entry = first; (void *)entry < end; ) {
2733 /* Avoid an infinite loop if we hit a bogus entry. */
2734 if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2737 handler(entry, arg);
2738 entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2743 * DEPRECATED. This interface has serious deficiencies and will be
2746 * Immediately enter the sleep state. In the old model, acpiconf(8) ran
2747 * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
2750 acpi_SetSleepState(struct acpi_softc *sc, int state)
2755 device_printf(sc->acpi_dev,
2756 "warning: acpi_SetSleepState() deprecated, need to update your software\n");
2759 return (acpi_EnterSleepState(sc, state));
2762 #if defined(__amd64__) || defined(__i386__)
2764 acpi_sleep_force_task(void *context)
2766 struct acpi_softc *sc = (struct acpi_softc *)context;
2768 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2769 device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
2770 sc->acpi_next_sstate);
2774 acpi_sleep_force(void *arg)
2776 struct acpi_softc *sc = (struct acpi_softc *)arg;
2778 device_printf(sc->acpi_dev,
2779 "suspend request timed out, forcing sleep now\n");
2781 * XXX Suspending from callout causes freezes in DEVICE_SUSPEND().
2782 * Suspend from acpi_task thread instead.
2784 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
2785 acpi_sleep_force_task, sc)))
2786 device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n");
2791 * Request that the system enter the given suspend state. All /dev/apm
2792 * devices and devd(8) will be notified. Userland then has a chance to
2793 * save state and acknowledge the request. The system sleeps once all
2797 acpi_ReqSleepState(struct acpi_softc *sc, int state)
2799 #if defined(__amd64__) || defined(__i386__)
2800 struct apm_clone_data *clone;
2803 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
2805 if (!acpi_sleep_states[state])
2806 return (EOPNOTSUPP);
2809 * If a reboot/shutdown/suspend request is already in progress or
2810 * suspend is blocked due to an upcoming shutdown, just return.
2812 if (rebooting || sc->acpi_next_sstate != 0 || suspend_blocked) {
2816 /* Wait until sleep is enabled. */
2817 while (sc->acpi_sleep_disabled) {
2823 sc->acpi_next_sstate = state;
2825 /* S5 (soft-off) should be entered directly with no waiting. */
2826 if (state == ACPI_STATE_S5) {
2828 status = acpi_EnterSleepState(sc, state);
2829 return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2832 /* Record the pending state and notify all apm devices. */
2833 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
2834 clone->notify_status = APM_EV_NONE;
2835 if ((clone->flags & ACPI_EVF_DEVD) == 0) {
2836 selwakeuppri(&clone->sel_read, PZERO);
2837 KNOTE_LOCKED(&clone->sel_read.si_note, 0);
2841 /* If devd(8) is not running, immediately enter the sleep state. */
2842 if (!devctl_process_running()) {
2844 status = acpi_EnterSleepState(sc, state);
2845 return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2849 * Set a timeout to fire if userland doesn't ack the suspend request
2850 * in time. This way we still eventually go to sleep if we were
2851 * overheating or running low on battery, even if userland is hung.
2852 * We cancel this timeout once all userland acks are in or the
2853 * suspend request is aborted.
2855 callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
2858 /* Now notify devd(8) also. */
2859 acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2863 /* This platform does not support acpi suspend/resume. */
2864 return (EOPNOTSUPP);
2869 * Acknowledge (or reject) a pending sleep state. The caller has
2870 * prepared for suspend and is now ready for it to proceed. If the
2871 * error argument is non-zero, it indicates suspend should be cancelled
2872 * and gives an errno value describing why. Once all votes are in,
2873 * we suspend the system.
2876 acpi_AckSleepState(struct apm_clone_data *clone, int error)
2878 #if defined(__amd64__) || defined(__i386__)
2879 struct acpi_softc *sc;
2882 /* If no pending sleep state, return an error. */
2884 sc = clone->acpi_sc;
2885 if (sc->acpi_next_sstate == 0) {
2890 /* Caller wants to abort suspend process. */
2892 sc->acpi_next_sstate = 0;
2893 callout_stop(&sc->susp_force_to);
2894 device_printf(sc->acpi_dev,
2895 "listener on %s cancelled the pending suspend\n",
2896 devtoname(clone->cdev));
2902 * Mark this device as acking the suspend request. Then, walk through
2903 * all devices, seeing if they agree yet. We only count devices that
2904 * are writable since read-only devices couldn't ack the request.
2907 clone->notify_status = APM_EV_ACKED;
2908 STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
2909 if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
2910 clone->notify_status != APM_EV_ACKED) {
2916 /* If all devices have voted "yes", we will suspend now. */
2918 callout_stop(&sc->susp_force_to);
2922 if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2927 /* This platform does not support acpi suspend/resume. */
2928 return (EOPNOTSUPP);
2933 acpi_sleep_enable(void *arg)
2935 struct acpi_softc *sc = (struct acpi_softc *)arg;
2937 ACPI_LOCK_ASSERT(acpi);
2939 /* Reschedule if the system is not fully up and running. */
2940 if (!AcpiGbl_SystemAwakeAndRunning) {
2941 callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
2945 sc->acpi_sleep_disabled = FALSE;
2949 acpi_sleep_disable(struct acpi_softc *sc)
2953 /* Fail if the system is not fully up and running. */
2954 if (!AcpiGbl_SystemAwakeAndRunning)
2958 status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
2959 sc->acpi_sleep_disabled = TRUE;
2965 enum acpi_sleep_state {
2968 ACPI_SS_DEV_SUSPEND,
2974 * Enter the desired system sleep state.
2976 * Currently we support S1-S5 but S4 is only S4BIOS
2979 acpi_EnterSleepState(struct acpi_softc *sc, int state)
2983 ACPI_EVENT_STATUS power_button_status;
2984 enum acpi_sleep_state slp_state;
2987 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
2989 if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
2990 return_ACPI_STATUS (AE_BAD_PARAMETER);
2991 if (!acpi_sleep_states[state]) {
2992 device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
2994 return (AE_SUPPORT);
2997 /* Re-entry once we're suspending is not allowed. */
2998 status = acpi_sleep_disable(sc);
2999 if (ACPI_FAILURE(status)) {
3000 device_printf(sc->acpi_dev,
3001 "suspend request ignored (not ready yet)\n");
3005 if (state == ACPI_STATE_S5) {
3007 * Shut down cleanly and power off. This will call us back through the
3008 * shutdown handlers.
3010 shutdown_nice(RB_POWEROFF);
3011 return_ACPI_STATUS (AE_OK);
3014 EVENTHANDLER_INVOKE(power_suspend_early);
3016 EVENTHANDLER_INVOKE(power_suspend);
3018 #ifdef EARLY_AP_STARTUP
3019 MPASS(mp_ncpus == 1 || smp_started);
3020 thread_lock(curthread);
3021 sched_bind(curthread, 0);
3022 thread_unlock(curthread);
3025 thread_lock(curthread);
3026 sched_bind(curthread, 0);
3027 thread_unlock(curthread);
3032 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
3033 * drivers need this.
3037 slp_state = ACPI_SS_NONE;
3039 sc->acpi_sstate = state;
3041 /* Enable any GPEs as appropriate and requested by the user. */
3042 acpi_wake_prep_walk(state);
3043 slp_state = ACPI_SS_GPE_SET;
3046 * Inform all devices that we are going to sleep. If at least one
3047 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
3049 * XXX Note that a better two-pass approach with a 'veto' pass
3050 * followed by a "real thing" pass would be better, but the current
3051 * bus interface does not provide for this.
3053 if (DEVICE_SUSPEND(root_bus) != 0) {
3054 device_printf(sc->acpi_dev, "device_suspend failed\n");
3057 slp_state = ACPI_SS_DEV_SUSPEND;
3059 status = AcpiEnterSleepStatePrep(state);
3060 if (ACPI_FAILURE(status)) {
3061 device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
3062 AcpiFormatException(status));
3065 slp_state = ACPI_SS_SLP_PREP;
3067 if (sc->acpi_sleep_delay > 0)
3068 DELAY(sc->acpi_sleep_delay * 1000000);
3071 intr = intr_disable();
3072 if (state != ACPI_STATE_S1) {
3073 sleep_result = acpi_sleep_machdep(sc, state);
3074 acpi_wakeup_machdep(sc, state, sleep_result, 0);
3077 * XXX According to ACPI specification SCI_EN bit should be restored
3078 * by ACPI platform (BIOS, firmware) to its pre-sleep state.
3079 * Unfortunately some BIOSes fail to do that and that leads to
3080 * unexpected and serious consequences during wake up like a system
3081 * getting stuck in SMI handlers.
3082 * This hack is picked up from Linux, which claims that it follows
3085 if (sleep_result == 1 && state != ACPI_STATE_S4)
3086 AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT);
3088 if (sleep_result == 1 && state == ACPI_STATE_S3) {
3090 * Prevent mis-interpretation of the wakeup by power button
3091 * as a request for power off.
3092 * Ideally we should post an appropriate wakeup event,
3093 * perhaps using acpi_event_power_button_wake or alike.
3095 * Clearing of power button status after wakeup is mandated
3096 * by ACPI specification in section "Fixed Power Button".
3098 * XXX As of ACPICA 20121114 AcpiGetEventStatus provides
3099 * status as 0/1 corressponding to inactive/active despite
3100 * its type being ACPI_EVENT_STATUS. In other words,
3101 * we should not test for ACPI_EVENT_FLAG_SET for time being.
3103 if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON,
3104 &power_button_status)) && power_button_status != 0) {
3105 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
3106 device_printf(sc->acpi_dev,
3107 "cleared fixed power button status\n");
3113 /* call acpi_wakeup_machdep() again with interrupt enabled */
3114 acpi_wakeup_machdep(sc, state, sleep_result, 1);
3116 AcpiLeaveSleepStatePrep(state);
3118 if (sleep_result == -1)
3121 /* Re-enable ACPI hardware on wakeup from sleep state 4. */
3122 if (state == ACPI_STATE_S4)
3125 status = AcpiEnterSleepState(state);
3127 AcpiLeaveSleepStatePrep(state);
3128 if (ACPI_FAILURE(status)) {
3129 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
3130 AcpiFormatException(status));
3134 slp_state = ACPI_SS_SLEPT;
3137 * Back out state according to how far along we got in the suspend
3138 * process. This handles both the error and success cases.
3141 if (slp_state >= ACPI_SS_SLP_PREP)
3143 if (slp_state >= ACPI_SS_GPE_SET) {
3144 acpi_wake_prep_walk(state);
3145 sc->acpi_sstate = ACPI_STATE_S0;
3147 if (slp_state >= ACPI_SS_DEV_SUSPEND)
3148 DEVICE_RESUME(root_bus);
3149 if (slp_state >= ACPI_SS_SLP_PREP)
3150 AcpiLeaveSleepState(state);
3151 if (slp_state >= ACPI_SS_SLEPT) {
3152 #if defined(__i386__) || defined(__amd64__)
3153 /* NB: we are still using ACPI timecounter at this point. */
3156 acpi_resync_clock(sc);
3157 acpi_enable_fixed_events(sc);
3159 sc->acpi_next_sstate = 0;
3163 #ifdef EARLY_AP_STARTUP
3164 thread_lock(curthread);
3165 sched_unbind(curthread);
3166 thread_unlock(curthread);
3169 thread_lock(curthread);
3170 sched_unbind(curthread);
3171 thread_unlock(curthread);
3177 EVENTHANDLER_INVOKE(power_resume);
3179 /* Allow another sleep request after a while. */
3180 callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3182 /* Run /etc/rc.resume after we are back. */
3183 if (devctl_process_running())
3184 acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
3186 return_ACPI_STATUS (status);
3190 acpi_resync_clock(struct acpi_softc *sc)
3194 * Warm up timecounter again and reset system clock.
3196 (void)timecounter->tc_get_timecount(timecounter);
3197 (void)timecounter->tc_get_timecount(timecounter);
3198 inittodr(time_second + sc->acpi_sleep_delay);
3201 /* Enable or disable the device's wake GPE. */
3203 acpi_wake_set_enable(device_t dev, int enable)
3205 struct acpi_prw_data prw;
3209 /* Make sure the device supports waking the system and get the GPE. */
3210 if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
3213 flags = acpi_get_flags(dev);
3215 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
3217 if (ACPI_FAILURE(status)) {
3218 device_printf(dev, "enable wake failed\n");
3221 acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
3223 status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
3225 if (ACPI_FAILURE(status)) {
3226 device_printf(dev, "disable wake failed\n");
3229 acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
3236 acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
3238 struct acpi_prw_data prw;
3241 /* Check that this is a wake-capable device and get its GPE. */
3242 if (acpi_parse_prw(handle, &prw) != 0)
3244 dev = acpi_get_device(handle);
3247 * The destination sleep state must be less than (i.e., higher power)
3248 * or equal to the value specified by _PRW. If this GPE cannot be
3249 * enabled for the next sleep state, then disable it. If it can and
3250 * the user requested it be enabled, turn on any required power resources
3253 if (sstate > prw.lowest_wake) {
3254 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
3256 device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
3257 acpi_name(handle), sstate);
3258 } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
3259 acpi_pwr_wake_enable(handle, 1);
3260 acpi_SetInteger(handle, "_PSW", 1);
3262 device_printf(dev, "wake_prep enabled for %s (S%d)\n",
3263 acpi_name(handle), sstate);
3270 acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
3272 struct acpi_prw_data prw;
3276 * Check that this is a wake-capable device and get its GPE. Return
3277 * now if the user didn't enable this device for wake.
3279 if (acpi_parse_prw(handle, &prw) != 0)
3281 dev = acpi_get_device(handle);
3282 if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
3286 * If this GPE couldn't be enabled for the previous sleep state, it was
3287 * disabled before going to sleep so re-enable it. If it was enabled,
3288 * clear _PSW and turn off any power resources it used.
3290 if (sstate > prw.lowest_wake) {
3291 AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
3293 device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
3295 acpi_SetInteger(handle, "_PSW", 0);
3296 acpi_pwr_wake_enable(handle, 0);
3298 device_printf(dev, "run_prep cleaned up for %s\n",
3306 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
3310 /* If suspending, run the sleep prep function, otherwise wake. */
3311 sstate = *(int *)context;
3312 if (AcpiGbl_SystemAwakeAndRunning)
3313 acpi_wake_sleep_prep(handle, sstate);
3315 acpi_wake_run_prep(handle, sstate);
3319 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
3321 acpi_wake_prep_walk(int sstate)
3323 ACPI_HANDLE sb_handle;
3325 if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
3326 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
3327 acpi_wake_prep, NULL, &sstate, NULL);
3331 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
3333 acpi_wake_sysctl_walk(device_t dev)
3335 int error, i, numdevs;
3340 error = device_get_children(dev, &devlist, &numdevs);
3341 if (error != 0 || numdevs == 0) {
3343 free(devlist, M_TEMP);
3346 for (i = 0; i < numdevs; i++) {
3348 acpi_wake_sysctl_walk(child);
3349 if (!device_is_attached(child))
3351 status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
3352 if (ACPI_SUCCESS(status)) {
3353 SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
3354 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
3355 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
3356 acpi_wake_set_sysctl, "I", "Device set to wake the system");
3359 free(devlist, M_TEMP);
3364 /* Enable or disable wake from userland. */
3366 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
3371 dev = (device_t)arg1;
3372 enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
3374 error = sysctl_handle_int(oidp, &enable, 0, req);
3375 if (error != 0 || req->newptr == NULL)
3377 if (enable != 0 && enable != 1)
3380 return (acpi_wake_set_enable(dev, enable));
3383 /* Parse a device's _PRW into a structure. */
3385 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
3388 ACPI_BUFFER prw_buffer;
3389 ACPI_OBJECT *res, *res2;
3390 int error, i, power_count;
3392 if (h == NULL || prw == NULL)
3396 * The _PRW object (7.2.9) is only required for devices that have the
3397 * ability to wake the system from a sleeping state.
3400 prw_buffer.Pointer = NULL;
3401 prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
3402 status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
3403 if (ACPI_FAILURE(status))
3405 res = (ACPI_OBJECT *)prw_buffer.Pointer;
3408 if (!ACPI_PKG_VALID(res, 2))
3412 * Element 1 of the _PRW object:
3413 * The lowest power system sleeping state that can be entered while still
3414 * providing wake functionality. The sleeping state being entered must
3415 * be less than (i.e., higher power) or equal to this value.
3417 if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
3421 * Element 0 of the _PRW object:
3423 switch (res->Package.Elements[0].Type) {
3424 case ACPI_TYPE_INTEGER:
3426 * If the data type of this package element is numeric, then this
3427 * _PRW package element is the bit index in the GPEx_EN, in the
3428 * GPE blocks described in the FADT, of the enable bit that is
3429 * enabled for the wake event.
3431 prw->gpe_handle = NULL;
3432 prw->gpe_bit = res->Package.Elements[0].Integer.Value;
3435 case ACPI_TYPE_PACKAGE:
3437 * If the data type of this package element is a package, then this
3438 * _PRW package element is itself a package containing two
3439 * elements. The first is an object reference to the GPE Block
3440 * device that contains the GPE that will be triggered by the wake
3441 * event. The second element is numeric and it contains the bit
3442 * index in the GPEx_EN, in the GPE Block referenced by the
3443 * first element in the package, of the enable bit that is enabled for
3446 * For example, if this field is a package then it is of the form:
3447 * Package() {\_SB.PCI0.ISA.GPE, 2}
3449 res2 = &res->Package.Elements[0];
3450 if (!ACPI_PKG_VALID(res2, 2))
3452 prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3453 if (prw->gpe_handle == NULL)
3455 if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3463 /* Elements 2 to N of the _PRW object are power resources. */
3464 power_count = res->Package.Count - 2;
3465 if (power_count > ACPI_PRW_MAX_POWERRES) {
3466 printf("ACPI device %s has too many power resources\n", acpi_name(h));
3469 prw->power_res_count = power_count;
3470 for (i = 0; i < power_count; i++)
3471 prw->power_res[i] = res->Package.Elements[i];
3474 if (prw_buffer.Pointer != NULL)
3475 AcpiOsFree(prw_buffer.Pointer);
3480 * ACPI Event Handlers
3483 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
3486 acpi_system_eventhandler_sleep(void *arg, int state)
3488 struct acpi_softc *sc = (struct acpi_softc *)arg;
3491 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
3493 /* Check if button action is disabled or unknown. */
3494 if (state == ACPI_STATE_UNKNOWN)
3497 /* Request that the system prepare to enter the given suspend state. */
3498 ret = acpi_ReqSleepState(sc, state);
3500 device_printf(sc->acpi_dev,
3501 "request to enter state S%d failed (err %d)\n", state, ret);
3507 acpi_system_eventhandler_wakeup(void *arg, int state)
3510 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
3512 /* Currently, nothing to do for wakeup. */
3518 * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
3521 acpi_invoke_sleep_eventhandler(void *context)
3524 EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context);
3528 acpi_invoke_wake_eventhandler(void *context)
3531 EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context);
3535 acpi_event_power_button_sleep(void *context)
3537 struct acpi_softc *sc = (struct acpi_softc *)context;
3539 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3541 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3542 acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx)))
3543 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3544 return_VALUE (ACPI_INTERRUPT_HANDLED);
3548 acpi_event_power_button_wake(void *context)
3550 struct acpi_softc *sc = (struct acpi_softc *)context;
3552 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3554 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3555 acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx)))
3556 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3557 return_VALUE (ACPI_INTERRUPT_HANDLED);
3561 acpi_event_sleep_button_sleep(void *context)
3563 struct acpi_softc *sc = (struct acpi_softc *)context;
3565 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3567 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3568 acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx)))
3569 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3570 return_VALUE (ACPI_INTERRUPT_HANDLED);
3574 acpi_event_sleep_button_wake(void *context)
3576 struct acpi_softc *sc = (struct acpi_softc *)context;
3578 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3580 if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3581 acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx)))
3582 return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3583 return_VALUE (ACPI_INTERRUPT_HANDLED);
3587 * XXX This static buffer is suboptimal. There is no locking so only
3588 * use this for single-threaded callers.
3591 acpi_name(ACPI_HANDLE handle)
3594 static char data[256];
3596 buf.Length = sizeof(data);
3599 if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3601 return ("(unknown)");
3605 * Debugging/bug-avoidance. Avoid trying to fetch info on various
3606 * parts of the namespace.
3609 acpi_avoid(ACPI_HANDLE handle)
3611 char *cp, *env, *np;
3614 np = acpi_name(handle);
3617 if ((env = kern_getenv("debug.acpi.avoid")) == NULL)
3620 /* Scan the avoid list checking for a match */
3623 while (*cp != 0 && isspace(*cp))
3628 while (cp[len] != 0 && !isspace(cp[len]))
3630 if (!strncmp(cp, np, len)) {
3642 * Debugging/bug-avoidance. Disable ACPI subsystem components.
3645 acpi_disabled(char *subsys)
3650 if ((env = kern_getenv("debug.acpi.disabled")) == NULL)
3652 if (strcmp(env, "all") == 0) {
3657 /* Scan the disable list, checking for a match. */
3660 while (*cp != '\0' && isspace(*cp))
3665 while (cp[len] != '\0' && !isspace(cp[len]))
3667 if (strncmp(cp, subsys, len) == 0) {
3679 acpi_lookup(void *arg, const char *name, device_t *dev)
3687 * Allow any handle name that is specified as an absolute path and
3688 * starts with '\'. We could restrict this to \_SB and friends,
3689 * but see acpi_probe_children() for notes on why we scan the entire
3690 * namespace for devices.
3692 * XXX: The pathname argument to AcpiGetHandle() should be fixed to
3695 if (name[0] != '\\')
3697 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name),
3700 *dev = acpi_get_device(handle);
3704 * Control interface.
3706 * We multiplex ioctls for all participating ACPI devices here. Individual
3707 * drivers wanting to be accessible via /dev/acpi should use the
3708 * register/deregister interface to make their handlers visible.
3710 struct acpi_ioctl_hook
3712 TAILQ_ENTRY(acpi_ioctl_hook) link;
3718 static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
3719 static int acpi_ioctl_hooks_initted;
3722 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
3724 struct acpi_ioctl_hook *hp;
3726 if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
3733 if (acpi_ioctl_hooks_initted == 0) {
3734 TAILQ_INIT(&acpi_ioctl_hooks);
3735 acpi_ioctl_hooks_initted = 1;
3737 TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
3744 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
3746 struct acpi_ioctl_hook *hp;
3749 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
3750 if (hp->cmd == cmd && hp->fn == fn)
3754 TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
3755 free(hp, M_ACPIDEV);
3761 acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
3767 acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3773 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3775 struct acpi_softc *sc;
3776 struct acpi_ioctl_hook *hp;
3784 * Scan the list of registered ioctls, looking for handlers.
3787 if (acpi_ioctl_hooks_initted)
3788 TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3794 return (hp->fn(cmd, addr, hp->arg));
3797 * Core ioctls are not permitted for non-writable user.
3798 * Currently, other ioctls just fetch information.
3799 * Not changing system behavior.
3801 if ((flag & FWRITE) == 0)
3804 /* Core system ioctls. */
3806 case ACPIIO_REQSLPSTATE:
3807 state = *(int *)addr;
3808 if (state != ACPI_STATE_S5)
3809 return (acpi_ReqSleepState(sc, state));
3810 device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
3813 case ACPIIO_ACKSLPSTATE:
3814 error = *(int *)addr;
3815 error = acpi_AckSleepState(sc->acpi_clone, error);
3817 case ACPIIO_SETSLPSTATE: /* DEPRECATED */
3818 state = *(int *)addr;
3819 if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
3821 if (!acpi_sleep_states[state])
3822 return (EOPNOTSUPP);
3823 if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
3835 acpi_sname2sstate(const char *sname)
3839 if (toupper(sname[0]) == 'S') {
3840 sstate = sname[1] - '0';
3841 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
3844 } else if (strcasecmp(sname, "NONE") == 0)
3845 return (ACPI_STATE_UNKNOWN);
3850 acpi_sstate2sname(int sstate)
3852 static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
3854 if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
3855 return (snames[sstate]);
3856 else if (sstate == ACPI_STATE_UNKNOWN)
3862 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3868 sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3869 for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
3870 if (acpi_sleep_states[state])
3871 sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
3874 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3880 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3882 char sleep_state[10];
3883 int error, new_state, old_state;
3885 old_state = *(int *)oidp->oid_arg1;
3886 strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
3887 error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
3888 if (error == 0 && req->newptr != NULL) {
3889 new_state = acpi_sname2sstate(sleep_state);
3890 if (new_state < ACPI_STATE_S1)
3892 if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
3893 return (EOPNOTSUPP);
3894 if (new_state != old_state)
3895 *(int *)oidp->oid_arg1 = new_state;
3900 /* Inform devctl(4) when we receive a Notify. */
3902 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
3904 char notify_buf[16];
3905 ACPI_BUFFER handle_buf;
3908 if (subsystem == NULL)
3911 handle_buf.Pointer = NULL;
3912 handle_buf.Length = ACPI_ALLOCATE_BUFFER;
3913 status = AcpiNsHandleToPathname(h, &handle_buf, FALSE);
3914 if (ACPI_FAILURE(status))
3916 snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
3917 devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
3918 AcpiOsFree(handle_buf.Pointer);
3923 * Support for parsing debug options from the kernel environment.
3925 * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
3926 * by specifying the names of the bits in the debug.acpi.layer and
3927 * debug.acpi.level environment variables. Bits may be unset by
3928 * prefixing the bit name with !.
3936 static struct debugtag dbg_layer[] = {
3937 {"ACPI_UTILITIES", ACPI_UTILITIES},
3938 {"ACPI_HARDWARE", ACPI_HARDWARE},
3939 {"ACPI_EVENTS", ACPI_EVENTS},
3940 {"ACPI_TABLES", ACPI_TABLES},
3941 {"ACPI_NAMESPACE", ACPI_NAMESPACE},
3942 {"ACPI_PARSER", ACPI_PARSER},
3943 {"ACPI_DISPATCHER", ACPI_DISPATCHER},
3944 {"ACPI_EXECUTER", ACPI_EXECUTER},
3945 {"ACPI_RESOURCES", ACPI_RESOURCES},
3946 {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
3947 {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
3948 {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
3949 {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
3951 {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
3952 {"ACPI_BATTERY", ACPI_BATTERY},
3953 {"ACPI_BUS", ACPI_BUS},
3954 {"ACPI_BUTTON", ACPI_BUTTON},
3955 {"ACPI_EC", ACPI_EC},
3956 {"ACPI_FAN", ACPI_FAN},
3957 {"ACPI_POWERRES", ACPI_POWERRES},
3958 {"ACPI_PROCESSOR", ACPI_PROCESSOR},
3959 {"ACPI_THERMAL", ACPI_THERMAL},
3960 {"ACPI_TIMER", ACPI_TIMER},
3961 {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
3965 static struct debugtag dbg_level[] = {
3966 {"ACPI_LV_INIT", ACPI_LV_INIT},
3967 {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
3968 {"ACPI_LV_INFO", ACPI_LV_INFO},
3969 {"ACPI_LV_REPAIR", ACPI_LV_REPAIR},
3970 {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
3972 /* Trace verbosity level 1 [Standard Trace Level] */
3973 {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
3974 {"ACPI_LV_PARSE", ACPI_LV_PARSE},
3975 {"ACPI_LV_LOAD", ACPI_LV_LOAD},
3976 {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
3977 {"ACPI_LV_EXEC", ACPI_LV_EXEC},
3978 {"ACPI_LV_NAMES", ACPI_LV_NAMES},
3979 {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
3980 {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
3981 {"ACPI_LV_TABLES", ACPI_LV_TABLES},
3982 {"ACPI_LV_VALUES", ACPI_LV_VALUES},
3983 {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
3984 {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
3985 {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
3986 {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
3987 {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
3989 /* Trace verbosity level 2 [Function tracing and memory allocation] */
3990 {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
3991 {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
3992 {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
3993 {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
3994 {"ACPI_LV_ALL", ACPI_LV_ALL},
3996 /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3997 {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
3998 {"ACPI_LV_THREADS", ACPI_LV_THREADS},
3999 {"ACPI_LV_IO", ACPI_LV_IO},
4000 {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
4001 {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
4003 /* Exceptionally verbose output -- also used in the global "DebugLevel" */
4004 {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
4005 {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
4006 {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
4007 {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
4008 {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
4013 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
4025 while (*ep && !isspace(*ep))
4036 for (i = 0; tag[i].name != NULL; i++) {
4037 if (!strncmp(cp, tag[i].name, l)) {
4039 *flag |= tag[i].value;
4041 *flag &= ~tag[i].value;
4049 acpi_set_debugging(void *junk)
4051 char *layer, *level;
4058 layer = kern_getenv("debug.acpi.layer");
4059 level = kern_getenv("debug.acpi.level");
4060 if (layer == NULL && level == NULL)
4063 printf("ACPI set debug");
4064 if (layer != NULL) {
4065 if (strcmp("NONE", layer) != 0)
4066 printf(" layer '%s'", layer);
4067 acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
4070 if (level != NULL) {
4071 if (strcmp("NONE", level) != 0)
4072 printf(" level '%s'", level);
4073 acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
4079 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
4083 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
4086 struct debugtag *tag;
4090 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
4092 if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
4093 tag = &dbg_layer[0];
4094 dbg = &AcpiDbgLayer;
4096 tag = &dbg_level[0];
4097 dbg = &AcpiDbgLevel;
4100 /* Get old values if this is a get request. */
4101 ACPI_SERIAL_BEGIN(acpi);
4103 sbuf_cpy(&sb, "NONE");
4104 } else if (req->newptr == NULL) {
4105 for (; tag->name != NULL; tag++) {
4106 if ((*dbg & tag->value) == tag->value)
4107 sbuf_printf(&sb, "%s ", tag->name);
4112 strlcpy(temp, sbuf_data(&sb), sizeof(temp));
4115 error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
4117 /* Check for error or no change */
4118 if (error == 0 && req->newptr != NULL) {
4120 kern_setenv((char *)oidp->oid_arg1, temp);
4121 acpi_set_debugging(NULL);
4123 ACPI_SERIAL_END(acpi);
4128 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
4129 "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
4130 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
4131 "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
4132 #endif /* ACPI_DEBUG */
4135 acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
4140 old = acpi_debug_objects;
4141 error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
4142 if (error != 0 || req->newptr == NULL)
4144 if (old == acpi_debug_objects || (old && acpi_debug_objects))
4147 ACPI_SERIAL_BEGIN(acpi);
4148 AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
4149 ACPI_SERIAL_END(acpi);
4155 acpi_parse_interfaces(char *str, struct acpi_interface *iface)
4162 while (isspace(*p) || *p == ',')
4167 p = strdup(p, M_TEMP);
4168 for (i = 0; i < len; i++)
4173 if (isspace(p[i]) || p[i] == '\0')
4176 i += strlen(p + i) + 1;
4183 iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
4187 if (isspace(p[i]) || p[i] == '\0')
4190 iface->data[j] = p + i;
4191 i += strlen(p + i) + 1;
4199 acpi_free_interfaces(struct acpi_interface *iface)
4202 free(iface->data[0], M_TEMP);
4203 free(iface->data, M_TEMP);
4207 acpi_reset_interfaces(device_t dev)
4209 struct acpi_interface list;
4213 if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
4214 for (i = 0; i < list.num; i++) {
4215 status = AcpiInstallInterface(list.data[i]);
4216 if (ACPI_FAILURE(status))
4218 "failed to install _OSI(\"%s\"): %s\n",
4219 list.data[i], AcpiFormatException(status));
4220 else if (bootverbose)
4221 device_printf(dev, "installed _OSI(\"%s\")\n",
4224 acpi_free_interfaces(&list);
4226 if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
4227 for (i = 0; i < list.num; i++) {
4228 status = AcpiRemoveInterface(list.data[i]);
4229 if (ACPI_FAILURE(status))
4231 "failed to remove _OSI(\"%s\"): %s\n",
4232 list.data[i], AcpiFormatException(status));
4233 else if (bootverbose)
4234 device_printf(dev, "removed _OSI(\"%s\")\n",
4237 acpi_free_interfaces(&list);
4242 acpi_pm_func(u_long cmd, void *arg, ...)
4244 int state, acpi_state;
4246 struct acpi_softc *sc;
4251 case POWER_CMD_SUSPEND:
4252 sc = (struct acpi_softc *)arg;
4259 state = va_arg(ap, int);
4263 case POWER_SLEEP_STATE_STANDBY:
4264 acpi_state = sc->acpi_standby_sx;
4266 case POWER_SLEEP_STATE_SUSPEND:
4267 acpi_state = sc->acpi_suspend_sx;
4269 case POWER_SLEEP_STATE_HIBERNATE:
4270 acpi_state = ACPI_STATE_S4;
4277 if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
4290 acpi_pm_register(void *arg)
4292 if (!cold || resource_disabled("acpi", 0))
4295 power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
4298 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, NULL);