115e32d5dSMike Smith /*-
215e32d5dSMike Smith * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith * Copyright (c) 2000 BSDi
615e32d5dSMike Smith * All rights reserved.
715e32d5dSMike Smith *
815e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith * are met:
1115e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith * documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith *
1715e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith * SUCH DAMAGE.
2815e32d5dSMike Smith */
2915e32d5dSMike Smith
30dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
3115e32d5dSMike Smith #include "opt_acpi.h"
3262d70a81SJohn Baldwin
3315e32d5dSMike Smith #include <sys/param.h>
34e2e050c8SConrad Meyer #include <sys/eventhandler.h>
3515e32d5dSMike Smith #include <sys/kernel.h>
36fb919e4dSMark Murray #include <sys/proc.h>
37a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3815e32d5dSMike Smith #include <sys/malloc.h>
39fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4015e32d5dSMike Smith #include <sys/bus.h>
4115e32d5dSMike Smith #include <sys/conf.h>
4215e32d5dSMike Smith #include <sys/ioccom.h>
4315e32d5dSMike Smith #include <sys/reboot.h>
4415e32d5dSMike Smith #include <sys/sysctl.h>
4515e32d5dSMike Smith #include <sys/ctype.h>
461611ea87SMitsuru IWASAKI #include <sys/linker.h>
47f1084587SKonstantin Belousov #include <sys/mount.h>
48f9390180SMitsuru IWASAKI #include <sys/power.h>
4954f6bca0SNate Lawson #include <sys/sbuf.h>
50c66d2b38SJung-uk Kim #include <sys/sched.h>
51eeb3a05fSNate Lawson #include <sys/smp.h>
52c66d2b38SJung-uk Kim #include <sys/timetc.h>
53b91fc6c4SBartlomiej Grzesik #include <sys/uuid.h>
5415e32d5dSMike Smith
55d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
56279be68bSAndriy Gapon #include <machine/clock.h>
57d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h>
580a34d050SJohn Baldwin #include <x86/cputypes.h>
590a34d050SJohn Baldwin #include <x86/x86_var.h>
60d320e05cSJohn Baldwin #endif
6115e32d5dSMike Smith #include <machine/resource.h>
62dc750869SNate Lawson #include <machine/bus.h>
63dc750869SNate Lawson #include <sys/rman.h>
64bc0f2195SMike Smith #include <isa/isavar.h>
65c796e27bSNate Lawson #include <isa/pnpvar.h>
66bc0f2195SMike Smith
67129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
68129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
69129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
70129d3046SJung-uk Kim
7115e32d5dSMike Smith #include <dev/acpica/acpivar.h>
7215e32d5dSMike Smith #include <dev/acpica/acpiio.h>
7315e32d5dSMike Smith
740e68afe5SAndrew Turner #include <dev/pci/pcivar.h>
750e68afe5SAndrew Turner
762be4e471SJung-uk Kim #include <vm/vm_param.h>
772be4e471SJung-uk Kim
78d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
7915e32d5dSMike Smith
80be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
81cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS
82b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
830ae55423SMike Smith
8415e32d5dSMike Smith static d_open_t acpiopen;
8515e32d5dSMike Smith static d_close_t acpiclose;
8615e32d5dSMike Smith static d_ioctl_t acpiioctl;
8715e32d5dSMike Smith
8815e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
89dc08ffecSPoul-Henning Kamp .d_version = D_VERSION,
907ac40f5fSPoul-Henning Kamp .d_open = acpiopen,
917ac40f5fSPoul-Henning Kamp .d_close = acpiclose,
927ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl,
937ac40f5fSPoul-Henning Kamp .d_name = "acpi",
9415e32d5dSMike Smith };
9515e32d5dSMike Smith
96ae19af49SJung-uk Kim struct acpi_interface {
97ae19af49SJung-uk Kim ACPI_STRING *data;
98ae19af49SJung-uk Kim int num;
99ae19af49SJung-uk Kim };
100ae19af49SJung-uk Kim
101dc8ab16eSWarner Losh static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
102dc8ab16eSWarner Losh
103346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
104cb9b0d80SMike Smith struct mtx acpi_mutex;
105fadf3fb9SJohn Baldwin struct callout acpi_sleep_timer;
106cb9b0d80SMike Smith
10789fb5af8SNate Lawson /* Bitmap of device quirks. */
10889fb5af8SNate Lawson int acpi_quirks;
10989fb5af8SNate Lawson
110a0e73a12SJung-uk Kim /* Supported sleep states. */
111a0e73a12SJung-uk Kim static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT];
112a0e73a12SJung-uk Kim
11364de8019SJohn Baldwin static void acpi_lookup(void *arg, const char *name, device_t *dev);
1146d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk);
1150e72b8d3SJohn Baldwin
1160e72b8d3SJohn Baldwin static device_probe_t acpi_probe;
1170e72b8d3SJohn Baldwin static device_attach_t acpi_attach;
1180e72b8d3SJohn Baldwin static device_suspend_t acpi_suspend;
1190e72b8d3SJohn Baldwin static device_resume_t acpi_resume;
1200e72b8d3SJohn Baldwin static device_shutdown_t acpi_shutdown;
1210e72b8d3SJohn Baldwin
1220e72b8d3SJohn Baldwin static bus_add_child_t acpi_add_child;
1230e72b8d3SJohn Baldwin static bus_print_child_t acpi_print_child;
1240e72b8d3SJohn Baldwin static bus_probe_nomatch_t acpi_probe_nomatch;
1250e72b8d3SJohn Baldwin static bus_driver_added_t acpi_driver_added;
1260e72b8d3SJohn Baldwin static bus_child_deleted_t acpi_child_deleted;
1270e72b8d3SJohn Baldwin static bus_read_ivar_t acpi_read_ivar;
1280e72b8d3SJohn Baldwin static bus_write_ivar_t acpi_write_ivar;
1290e72b8d3SJohn Baldwin static bus_get_resource_list_t acpi_get_rlist;
1300e1246e3SJohn Baldwin static bus_get_rman_t acpi_get_rman;
1310e72b8d3SJohn Baldwin static bus_set_resource_t acpi_set_resource;
1320e72b8d3SJohn Baldwin static bus_alloc_resource_t acpi_alloc_resource;
1330e72b8d3SJohn Baldwin static bus_adjust_resource_t acpi_adjust_resource;
1340e72b8d3SJohn Baldwin static bus_release_resource_t acpi_release_resource;
1350e72b8d3SJohn Baldwin static bus_delete_resource_t acpi_delete_resource;
1360e1246e3SJohn Baldwin static bus_activate_resource_t acpi_activate_resource;
1370e1246e3SJohn Baldwin static bus_deactivate_resource_t acpi_deactivate_resource;
1380e1246e3SJohn Baldwin static bus_map_resource_t acpi_map_resource;
1390e1246e3SJohn Baldwin static bus_unmap_resource_t acpi_unmap_resource;
1400e72b8d3SJohn Baldwin static bus_child_pnpinfo_t acpi_child_pnpinfo_method;
1410e72b8d3SJohn Baldwin static bus_child_location_t acpi_child_location_method;
1420e72b8d3SJohn Baldwin static bus_hint_device_unit_t acpi_hint_device_unit;
1430e72b8d3SJohn Baldwin static bus_get_property_t acpi_bus_get_prop;
1440e72b8d3SJohn Baldwin static bus_get_device_path_t acpi_get_device_path;
1457dd1f0dcSKonstantin Belousov static bus_get_domain_t acpi_get_domain_method;
1460e72b8d3SJohn Baldwin
1470e72b8d3SJohn Baldwin static acpi_id_probe_t acpi_device_id_probe;
1480e72b8d3SJohn Baldwin static acpi_evaluate_object_t acpi_device_eval_obj;
1490e72b8d3SJohn Baldwin static acpi_get_property_t acpi_device_get_prop;
1500e72b8d3SJohn Baldwin static acpi_scan_children_t acpi_device_scan_children;
1510e72b8d3SJohn Baldwin
1520e72b8d3SJohn Baldwin static isa_pnp_probe_t acpi_isa_pnp_probe;
1530e72b8d3SJohn Baldwin
154ea233199SJohn Baldwin static void acpi_reserve_resources(device_t dev);
155adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev);
1561e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev);
1571e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
158df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
159df8d2a32SNate Lawson void *context, void **retval);
160945eaca1SBjoern A. Zeeb static ACPI_STATUS acpi_find_dsd(struct acpi_device *ad);
161855e49f3SAlexander Motin static void acpi_platform_osc(device_t dev);
16215e32d5dSMike Smith static void acpi_probe_children(device_t bus);
163d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order);
164be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
165be2b1797SNate Lawson void *context, void **status);
166a0e73a12SJung-uk Kim static void acpi_sleep_enable(void *arg);
167a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
16800a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
16915e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto);
17013d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc);
171a0a15716SJung-uk Kim static void acpi_resync_clock(struct acpi_softc *sc);
172d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
173d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
174d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate);
17588a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev);
17688a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
17715e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state);
17815e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state);
179a0e73a12SJung-uk Kim static int acpi_sname2sstate(const char *sname);
180a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate);
181d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1821d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1832a18c71dSJung-uk Kim static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
184f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...);
185d320e05cSJohn Baldwin static void acpi_enable_pcie(void);
186ae19af49SJung-uk Kim static void acpi_reset_interfaces(device_t dev);
187879d6c23STakanori Watanabe
18815e32d5dSMike Smith static device_method_t acpi_methods[] = {
18915e32d5dSMike Smith /* Device interface */
19015e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe),
19115e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach),
192169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown),
19356a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach),
19410ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend),
19510ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume),
19615e32d5dSMike Smith
19715e32d5dSMike Smith /* Bus interface */
19815e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child),
19915e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child),
20010ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch),
20110ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added),
202709749aaSVladimir Kondratyev DEVMETHOD(bus_child_deleted, acpi_child_deleted),
20315e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar),
20415e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar),
20591233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist),
2060e1246e3SJohn Baldwin DEVMETHOD(bus_get_rman, acpi_get_rman),
207ea233199SJohn Baldwin DEVMETHOD(bus_set_resource, acpi_set_resource),
20891233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
20915e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
210049dc0d1SJohn Baldwin DEVMETHOD(bus_adjust_resource, acpi_adjust_resource),
21115e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource),
2128b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource),
2130e1246e3SJohn Baldwin DEVMETHOD(bus_activate_resource, acpi_activate_resource),
2140e1246e3SJohn Baldwin DEVMETHOD(bus_deactivate_resource, acpi_deactivate_resource),
2150e1246e3SJohn Baldwin DEVMETHOD(bus_map_resource, acpi_map_resource),
2160e1246e3SJohn Baldwin DEVMETHOD(bus_unmap_resource, acpi_unmap_resource),
217ddfc9c4cSWarner Losh DEVMETHOD(bus_child_pnpinfo, acpi_child_pnpinfo_method),
218ddfc9c4cSWarner Losh DEVMETHOD(bus_child_location, acpi_child_location_method),
21915e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
22015e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
2210d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit),
2228d791e5aSJohn Baldwin DEVMETHOD(bus_get_cpus, acpi_get_cpus),
2237dd1f0dcSKonstantin Belousov DEVMETHOD(bus_get_domain, acpi_get_domain_method),
2243f9a00e3SBartlomiej Grzesik DEVMETHOD(bus_get_property, acpi_bus_get_prop),
225cae7d9ecSWarner Losh DEVMETHOD(bus_get_device_path, acpi_get_device_path),
22615e32d5dSMike Smith
227ce619b2eSNate Lawson /* ACPI bus */
228ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe),
229ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj),
230b91fc6c4SBartlomiej Grzesik DEVMETHOD(acpi_get_property, acpi_device_get_prop),
23110ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep),
232df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children),
233ce619b2eSNate Lawson
234bc0f2195SMike Smith /* ISA emulation */
235bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe),
236bc0f2195SMike Smith
23761bfd867SSofian Brabez DEVMETHOD_END
23815e32d5dSMike Smith };
23915e32d5dSMike Smith
24015e32d5dSMike Smith static driver_t acpi_driver = {
24115e32d5dSMike Smith "acpi",
24215e32d5dSMike Smith acpi_methods,
24315e32d5dSMike Smith sizeof(struct acpi_softc),
24415e32d5dSMike Smith };
24515e32d5dSMike Smith
246916a5d8aSJohn Baldwin EARLY_DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_modevent, 0,
247334790eaSAndrew Turner BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
248a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
24915e32d5dSMike Smith
25015e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
25115e2f34fSNate Lawson
252adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
253adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
254adad4744SNate Lawson
255bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5
256bee4aa4aSNate Lawson
2575217af30SJohn Baldwin /* Holds the description of the acpi0 device. */
2585217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
2595217af30SJohn Baldwin
2607029da5cSPawel Biernacki SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
2617029da5cSPawel Biernacki "ACPI debugging");
2621d7b121cSNate Lawson static char acpi_ca_version[12];
2631d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2641d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA");
26515e32d5dSMike Smith
26615e32d5dSMike Smith /*
267ae19af49SJung-uk Kim * Allow overriding _OSI methods.
268ae19af49SJung-uk Kim */
269ae19af49SJung-uk Kim static char acpi_install_interface[256];
270ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
271ae19af49SJung-uk Kim sizeof(acpi_install_interface));
272ae19af49SJung-uk Kim static char acpi_remove_interface[256];
273ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
274ae19af49SJung-uk Kim sizeof(acpi_remove_interface));
275ae19af49SJung-uk Kim
2762a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */
2772a18c71dSJung-uk Kim static int acpi_debug_objects;
2782a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
2792a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
2803e68d2c5SAlexander Motin CTLFLAG_RW | CTLTYPE_INT | CTLFLAG_MPSAFE, NULL, 0,
2817029da5cSPawel Biernacki acpi_debug_objects_sysctl, "I",
2822a18c71dSJung-uk Kim "Enable Debug objects");
2832a18c71dSJung-uk Kim
2842a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */
2852a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1;
2862a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
2872a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
2882a18c71dSJung-uk Kim &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
2892a18c71dSJung-uk Kim
290313a0c13SJung-uk Kim /* Ignore register widths set by FADT and use default widths instead. */
291313a0c13SJung-uk Kim static int acpi_ignore_reg_width = 1;
292313a0c13SJung-uk Kim TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width);
293313a0c13SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN,
294313a0c13SJung-uk Kim &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT");
295313a0c13SJung-uk Kim
29639da3eb3SNate Lawson /* Allow users to override quirks. */
29739da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
29839da3eb3SNate Lawson
299e787342eSJung-uk Kim int acpi_susp_bounce;
30093bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
30193bfd059SNate Lawson &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
30293bfd059SNate Lawson
3030a34d050SJohn Baldwin #if defined(__amd64__) || defined(__i386__)
3040a34d050SJohn Baldwin int acpi_override_isa_irq_polarity;
3050a34d050SJohn Baldwin #endif
3060a34d050SJohn Baldwin
307c9b8d77dSNate Lawson /*
308b91fc6c4SBartlomiej Grzesik * ACPI standard UUID for Device Specific Data Package
309b91fc6c4SBartlomiej Grzesik * "Device Properties UUID for _DSD" Rev. 2.0
310b91fc6c4SBartlomiej Grzesik */
311b91fc6c4SBartlomiej Grzesik static const struct uuid acpi_dsd_uuid = {
312b91fc6c4SBartlomiej Grzesik 0xdaffd814, 0x6eba, 0x4d8c, 0x8a, 0x91,
313b91fc6c4SBartlomiej Grzesik { 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01 }
314b91fc6c4SBartlomiej Grzesik };
315b91fc6c4SBartlomiej Grzesik
316b91fc6c4SBartlomiej Grzesik /*
3176d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after
3186d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system.
3198c0879b6SJohn Baldwin * It also cannot be unloaded, since the entire system bus hierarchy hangs
320be2b1797SNate Lawson * off it.
3216d63101aSMike Smith */
3226d63101aSMike Smith static int
acpi_modevent(struct module * mod,int event,void * junk)3236d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
3246d63101aSMike Smith {
3256d63101aSMike Smith switch (event) {
3266d63101aSMike Smith case MOD_LOAD:
32787b45ed5SMitsuru IWASAKI if (!cold) {
32887b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n");
3296d63101aSMike Smith return (EPERM);
33087b45ed5SMitsuru IWASAKI }
3316d63101aSMike Smith break;
3326d63101aSMike Smith case MOD_UNLOAD:
333f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
3346d63101aSMike Smith return (EBUSY);
335f9390180SMitsuru IWASAKI break;
3366d63101aSMike Smith default:
3376d63101aSMike Smith break;
3386d63101aSMike Smith }
3396d63101aSMike Smith return (0);
3406d63101aSMike Smith }
3416d63101aSMike Smith
3426d63101aSMike Smith /*
343bbc2815cSJohn Baldwin * Perform early initialization.
34415e32d5dSMike Smith */
345bbc2815cSJohn Baldwin ACPI_STATUS
acpi_Startup(void)346bbc2815cSJohn Baldwin acpi_Startup(void)
34715e32d5dSMike Smith {
34889fb5af8SNate Lawson static int started = 0;
3492be4e471SJung-uk Kim ACPI_STATUS status;
3502be4e471SJung-uk Kim int val;
35115e32d5dSMike Smith
3521b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3531b8c233dSPeter Pentchev
354bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */
355bbc2815cSJohn Baldwin if (started)
3562be4e471SJung-uk Kim return_VALUE (AE_OK);
357bbc2815cSJohn Baldwin started = 1;
35815e32d5dSMike Smith
359413081d7SNate Lawson /*
3605f9b24faSJung-uk Kim * Initialize the ACPICA subsystem.
3615f9b24faSJung-uk Kim */
3625f9b24faSJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
3635f9b24faSJung-uk Kim printf("ACPI: Could not initialize Subsystem: %s\n",
3645f9b24faSJung-uk Kim AcpiFormatException(status));
3655f9b24faSJung-uk Kim return_VALUE (status);
3665f9b24faSJung-uk Kim }
3675f9b24faSJung-uk Kim
3685f9b24faSJung-uk Kim /*
3692be4e471SJung-uk Kim * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
3702be4e471SJung-uk Kim * if more tables exist.
371413081d7SNate Lawson */
3722be4e471SJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
3732be4e471SJung-uk Kim printf("ACPI: Table initialisation failed: %s\n",
3742be4e471SJung-uk Kim AcpiFormatException(status));
3752be4e471SJung-uk Kim return_VALUE (status);
37615e32d5dSMike Smith }
3773184cf5aSNate Lawson
37889fb5af8SNate Lawson /* Set up any quirks we have for this system. */
3792be4e471SJung-uk Kim if (acpi_quirks == ACPI_Q_OK)
38089fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks);
38189fb5af8SNate Lawson
38239da3eb3SNate Lawson /* If the user manually set the disabled hint to 0, force-enable ACPI. */
38389fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
38489fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN;
38589fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) {
38689fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n");
3872be4e471SJung-uk Kim status = AE_SUPPORT;
38889fb5af8SNate Lawson }
3893184cf5aSNate Lawson
3902be4e471SJung-uk Kim return_VALUE (status);
391bbc2815cSJohn Baldwin }
392bbc2815cSJohn Baldwin
393bbc2815cSJohn Baldwin /*
3945217af30SJohn Baldwin * Detect ACPI and perform early initialisation.
395bbc2815cSJohn Baldwin */
3965217af30SJohn Baldwin int
acpi_identify(void)3975217af30SJohn Baldwin acpi_identify(void)
398bbc2815cSJohn Baldwin {
3995217af30SJohn Baldwin ACPI_TABLE_RSDP *rsdp;
4005217af30SJohn Baldwin ACPI_TABLE_HEADER *rsdt;
4015217af30SJohn Baldwin ACPI_PHYSICAL_ADDRESS paddr;
4025217af30SJohn Baldwin struct sbuf sb;
403bbc2815cSJohn Baldwin
404bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
405bbc2815cSJohn Baldwin
406bbc2815cSJohn Baldwin if (!cold)
4075217af30SJohn Baldwin return (ENXIO);
408bbc2815cSJohn Baldwin
409a8de37b0SEitan Adler /* Check that we haven't been disabled with a hint. */
410a8de37b0SEitan Adler if (resource_disabled("acpi", 0))
411a8de37b0SEitan Adler return (ENXIO);
412a8de37b0SEitan Adler
4135217af30SJohn Baldwin /* Check for other PM systems. */
4145217af30SJohn Baldwin if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
4155217af30SJohn Baldwin power_pm_get_type() != POWER_PM_TYPE_ACPI) {
4165217af30SJohn Baldwin printf("ACPI identify failed, other PM system enabled.\n");
4175217af30SJohn Baldwin return (ENXIO);
4185217af30SJohn Baldwin }
4194b1ff978SMark Santcroos
4202be4e471SJung-uk Kim /* Initialize root tables. */
4212be4e471SJung-uk Kim if (ACPI_FAILURE(acpi_Startup())) {
4222be4e471SJung-uk Kim printf("ACPI: Try disabling either ACPI or apic support.\n");
4235217af30SJohn Baldwin return (ENXIO);
4242be4e471SJung-uk Kim }
42515e32d5dSMike Smith
4265217af30SJohn Baldwin if ((paddr = AcpiOsGetRootPointer()) == 0 ||
4275217af30SJohn Baldwin (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
4285217af30SJohn Baldwin return (ENXIO);
4295217af30SJohn Baldwin if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
4305217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
4315217af30SJohn Baldwin else
4325217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
4335217af30SJohn Baldwin AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
4345217af30SJohn Baldwin
4355217af30SJohn Baldwin if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
4365217af30SJohn Baldwin return (ENXIO);
4375217af30SJohn Baldwin sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
4385217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
4395217af30SJohn Baldwin sbuf_trim(&sb);
4405217af30SJohn Baldwin sbuf_putc(&sb, ' ');
4415217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
4425217af30SJohn Baldwin sbuf_trim(&sb);
4435217af30SJohn Baldwin sbuf_finish(&sb);
4445217af30SJohn Baldwin sbuf_delete(&sb);
4455217af30SJohn Baldwin AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
4465217af30SJohn Baldwin
4475217af30SJohn Baldwin snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
4485217af30SJohn Baldwin
4495217af30SJohn Baldwin return (0);
45015e32d5dSMike Smith }
45115e32d5dSMike Smith
45215e32d5dSMike Smith /*
453bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message.
45415e32d5dSMike Smith */
45515e32d5dSMike Smith static int
acpi_probe(device_t dev)45615e32d5dSMike Smith acpi_probe(device_t dev)
45715e32d5dSMike Smith {
45815e32d5dSMike Smith
459b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
460f9390180SMitsuru IWASAKI
4615217af30SJohn Baldwin device_set_desc(dev, acpi_desc);
462bee4aa4aSNate Lawson
463feeec74dSNathan Whitehorn return_VALUE (BUS_PROBE_NOWILDCARD);
46415e32d5dSMike Smith }
46515e32d5dSMike Smith
46615e32d5dSMike Smith static int
acpi_attach(device_t dev)46715e32d5dSMike Smith acpi_attach(device_t dev)
46815e32d5dSMike Smith {
46915e32d5dSMike Smith struct acpi_softc *sc;
470cb9b0d80SMike Smith ACPI_STATUS status;
471ea27c63eSNate Lawson int error, state;
47232d18aa5SMike Smith UINT32 flags;
473ea27c63eSNate Lawson UINT8 TypeA, TypeB;
474498d464fSMitsuru IWASAKI char *env;
47515e32d5dSMike Smith
476b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
477bee4aa4aSNate Lawson
47815e32d5dSMike Smith sc = device_get_softc(dev);
47915e32d5dSMike Smith sc->acpi_dev = dev;
480fd90e2edSJung-uk Kim callout_init(&sc->susp_force_to, 1);
48115e32d5dSMike Smith
4822be4e471SJung-uk Kim error = ENXIO;
4832be4e471SJung-uk Kim
48491233413SNate Lawson /* Initialize resource manager. */
48591233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY;
48691233413SNate Lawson acpi_rman_io.rm_start = 0;
48791233413SNate Lawson acpi_rman_io.rm_end = 0xffff;
4880bba6acfSJohn Baldwin acpi_rman_io.rm_descr = "ACPI I/O ports";
48991233413SNate Lawson if (rman_init(&acpi_rman_io) != 0)
49091233413SNate Lawson panic("acpi rman_init IO ports failed");
49191233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY;
4920bba6acfSJohn Baldwin acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
49391233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0)
49491233413SNate Lawson panic("acpi rman_init memory failed");
49591233413SNate Lawson
4960e1246e3SJohn Baldwin resource_list_init(&sc->sysres_rl);
4970e1246e3SJohn Baldwin
4982be4e471SJung-uk Kim /* Initialise the ACPI mutex */
4992be4e471SJung-uk Kim mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
5002be4e471SJung-uk Kim
5012be4e471SJung-uk Kim /*
5022be4e471SJung-uk Kim * Set the globals from our tunables. This is needed because ACPI-CA
5032be4e471SJung-uk Kim * uses UINT8 for some values and we have no tunable_byte.
5042be4e471SJung-uk Kim */
5052a18c71dSJung-uk Kim AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
5062a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
507313a0c13SJung-uk Kim AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE;
5082a18c71dSJung-uk Kim
5092a18c71dSJung-uk Kim #ifndef ACPI_DEBUG
5102a18c71dSJung-uk Kim /*
5112a18c71dSJung-uk Kim * Disable all debugging layers and levels.
5122a18c71dSJung-uk Kim */
5132a18c71dSJung-uk Kim AcpiDbgLayer = 0;
5142a18c71dSJung-uk Kim AcpiDbgLevel = 0;
5152a18c71dSJung-uk Kim #endif
5162be4e471SJung-uk Kim
517ae19af49SJung-uk Kim /* Override OS interfaces if the user requested. */
518ae19af49SJung-uk Kim acpi_reset_interfaces(dev);
519ae19af49SJung-uk Kim
5202be4e471SJung-uk Kim /* Load ACPI name space. */
5212be4e471SJung-uk Kim status = AcpiLoadTables();
5222be4e471SJung-uk Kim if (ACPI_FAILURE(status)) {
5232be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n",
5242be4e471SJung-uk Kim AcpiFormatException(status));
5252be4e471SJung-uk Kim goto out;
5262be4e471SJung-uk Kim }
5272be4e471SJung-uk Kim
528d320e05cSJohn Baldwin /* Handle MCFG table if present. */
529d320e05cSJohn Baldwin acpi_enable_pcie();
530d320e05cSJohn Baldwin
53115e32d5dSMike Smith /*
532be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation
533be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must
534be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final
535be2b1797SNate Lawson * object init pass.
53615e32d5dSMike Smith *
53732d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
53832d18aa5SMike Smith *
539be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached
540be2b1797SNate Lawson * all our child devices, but on many systems it works here.
54115e32d5dSMike Smith */
54232d18aa5SMike Smith flags = 0;
543d786139cSMaxime Henrion if (testenv("debug.acpi.avoid"))
54432d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
545bee4aa4aSNate Lawson
546bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */
547b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
548be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n",
549be2b1797SNate Lawson AcpiFormatException(status));
550cb9b0d80SMike Smith goto out;
55115e32d5dSMike Smith }
55215e32d5dSMike Smith
553f8335e3aSNate Lawson /*
554f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before
555f8335e3aSNate Lawson * the namespace has been evaluated.
556da72d149SNate Lawson *
557da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and
558da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't
559da72d149SNate Lawson * a problem but should be addressed eventually.
560f8335e3aSNate Lawson */
561f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev);
562f8335e3aSNate Lawson
563bee4aa4aSNate Lawson /* Bring device objects and regions online. */
564b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
565be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n",
566be2b1797SNate Lawson AcpiFormatException(status));
567b69ed3f4SMitsuru IWASAKI goto out;
568b69ed3f4SMitsuru IWASAKI }
569b69ed3f4SMitsuru IWASAKI
57015e32d5dSMike Smith /*
5711d073b1dSJohn Baldwin * Setup our sysctl tree.
5721d073b1dSJohn Baldwin *
5731d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail.
5741d073b1dSJohn Baldwin */
5751d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx);
5761d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
5777029da5cSPawel Biernacki SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, device_get_name(dev),
5787029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
5791d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5807029da5cSPawel Biernacki OID_AUTO, "supported_sleep_state",
5813e68d2c5SAlexander Motin CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
582ad73b301SAdrian Chadd 0, 0, acpi_supported_sleep_state_sysctl, "A",
583ad73b301SAdrian Chadd "List supported ACPI sleep states.");
584d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5857029da5cSPawel Biernacki OID_AUTO, "power_button_state",
5863e68d2c5SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
587ad73b301SAdrian Chadd &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A",
588ad73b301SAdrian Chadd "Power button ACPI sleep state.");
5891d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5907029da5cSPawel Biernacki OID_AUTO, "sleep_button_state",
5913e68d2c5SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
592ad73b301SAdrian Chadd &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A",
593ad73b301SAdrian Chadd "Sleep button ACPI sleep state.");
5941d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5957029da5cSPawel Biernacki OID_AUTO, "lid_switch_state",
5963e68d2c5SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
597ad73b301SAdrian Chadd &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A",
598ad73b301SAdrian Chadd "Lid ACPI sleep state. Set to S3 if you want to suspend your laptop when close the Lid.");
599f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
6007029da5cSPawel Biernacki OID_AUTO, "standby_state",
6013e68d2c5SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
602f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
603f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
6047029da5cSPawel Biernacki OID_AUTO, "suspend_state",
6053e68d2c5SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
606f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
6072d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
608197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
60931f301d0SChristian Brueffer "sleep delay in seconds");
610ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
611197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
6121611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
613197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
614d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
615d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
616d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
617d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
618d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW,
619d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
620bf0c18ecSNate Lawson
6210a34d050SJohn Baldwin #if defined(__amd64__) || defined(__i386__)
6220a34d050SJohn Baldwin /*
6230a34d050SJohn Baldwin * Enable workaround for incorrect ISA IRQ polarity by default on
6240a34d050SJohn Baldwin * systems with Intel CPUs.
6250a34d050SJohn Baldwin */
6260a34d050SJohn Baldwin if (cpu_vendor_id == CPU_VENDOR_INTEL)
6270a34d050SJohn Baldwin acpi_override_isa_irq_polarity = 1;
6280a34d050SJohn Baldwin SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
6290a34d050SJohn Baldwin OID_AUTO, "override_isa_irq_polarity", CTLFLAG_RDTUN,
6300a34d050SJohn Baldwin &acpi_override_isa_irq_polarity, 0,
6310a34d050SJohn Baldwin "Force active-hi polarity for edge-triggered ISA IRQs");
6320a34d050SJohn Baldwin #endif
6330a34d050SJohn Baldwin
634bf0c18ecSNate Lawson /*
6352b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to
636bf0c18ecSNate Lawson * stabilize.
637bf0c18ecSNate Lawson */
6382b9609abSNate Lawson sc->acpi_sleep_delay = 1;
6392d644607SMitsuru IWASAKI if (bootverbose)
6402d644607SMitsuru IWASAKI sc->acpi_verbose = 1;
6412be111bfSDavide Italiano if ((env = kern_getenv("hw.acpi.verbose")) != NULL) {
642965a34fbSNate Lawson if (strcmp(env, "0") != 0)
643498d464fSMitsuru IWASAKI sc->acpi_verbose = 1;
644498d464fSMitsuru IWASAKI freeenv(env);
645498d464fSMitsuru IWASAKI }
6461d073b1dSJohn Baldwin
647ac731af5SJung-uk Kim /* Only enable reboot by default if the FADT says it is available. */
648ac731af5SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
649ac731af5SJung-uk Kim sc->acpi_handle_reboot = 1;
650ac731af5SJung-uk Kim
651fe5d5d7dSAndrew Turner #if !ACPI_REDUCED_HARDWARE
6522d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */
653d8c37c3aSAndrew Turner if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
6542d610c46SNate Lawson sc->acpi_s4bios = 1;
655fe5d5d7dSAndrew Turner #endif
6562d610c46SNate Lawson
657a0e73a12SJung-uk Kim /* Probe all supported sleep states. */
658a0e73a12SJung-uk Kim acpi_sleep_states[ACPI_STATE_S0] = TRUE;
659a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
660efcc2a30SJung-uk Kim if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT,
661efcc2a30SJung-uk Kim __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) &&
662efcc2a30SJung-uk Kim ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
663a0e73a12SJung-uk Kim acpi_sleep_states[state] = TRUE;
664a0e73a12SJung-uk Kim
6651d073b1dSJohn Baldwin /*
666ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set
667a0e73a12SJung-uk Kim * to UNKNOWN by default to avoid surprising users.
66815e32d5dSMike Smith */
669a0e73a12SJung-uk Kim sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
670a0e73a12SJung-uk Kim ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
671a0e73a12SJung-uk Kim sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
672a0e73a12SJung-uk Kim sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
673a0e73a12SJung-uk Kim ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
674a0e73a12SJung-uk Kim sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
675a0e73a12SJung-uk Kim ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
67615e32d5dSMike Smith
677ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */
678a0e73a12SJung-uk Kim sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
67900a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
680a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) {
681ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state;
682ea27c63eSNate Lawson break;
683ea27c63eSNate Lawson }
684ea27c63eSNate Lawson
68513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc);
68615e32d5dSMike Smith
68715e32d5dSMike Smith /*
68815e32d5dSMike Smith * Scan the namespace and attach/initialise children.
68915e32d5dSMike Smith */
69015e32d5dSMike Smith
69159e472e9SNate Lawson /* Register our shutdown handler. */
692be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
6939cdf326bSAndriy Gapon SHUTDOWN_PRI_LAST + 150);
69415e32d5dSMike Smith
69515e32d5dSMike Smith /*
69615e32d5dSMike Smith * Register our acpi event handlers.
69715e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager.
69815e32d5dSMike Smith */
699be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
700be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST);
701be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
702be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST);
70315e32d5dSMike Smith
704be2b1797SNate Lawson /* Flag our initial states. */
705a0e73a12SJung-uk Kim sc->acpi_enabled = TRUE;
70615e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0;
707a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE;
70815e32d5dSMike Smith
709be2b1797SNate Lawson /* Create the control device */
710d1655c6fSEdward Tomasz Napierala sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664,
7112a4689e8SRobert Watson "acpi");
71215e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc;
71315e32d5dSMike Smith
714be2b1797SNate Lawson if ((error = acpi_machdep_init(dev)))
715f86214b6SMitsuru IWASAKI goto out;
716f86214b6SMitsuru IWASAKI
717f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */
718f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
719f9390180SMitsuru IWASAKI
720855e49f3SAlexander Motin acpi_platform_osc(dev);
721855e49f3SAlexander Motin
72264de8019SJohn Baldwin if (!acpi_disabled("bus")) {
72364de8019SJohn Baldwin EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000);
724eeb6dba2SJohn Baldwin acpi_probe_children(dev);
72564de8019SJohn Baldwin }
726eeb6dba2SJohn Baldwin
7275a77b11bSJung-uk Kim /* Update all GPEs and enable runtime GPEs. */
7285a77b11bSJung-uk Kim status = AcpiUpdateAllGpes();
7295a77b11bSJung-uk Kim if (ACPI_FAILURE(status))
7305a77b11bSJung-uk Kim device_printf(dev, "Could not update all GPEs: %s\n",
7315a77b11bSJung-uk Kim AcpiFormatException(status));
7325a77b11bSJung-uk Kim
733a0e73a12SJung-uk Kim /* Allow sleep request after a while. */
734fadf3fb9SJohn Baldwin callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0);
735fadf3fb9SJohn Baldwin callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
736fadf3fb9SJohn Baldwin acpi_sleep_enable, sc);
737a0e73a12SJung-uk Kim
738cb9b0d80SMike Smith error = 0;
739cb9b0d80SMike Smith
740cb9b0d80SMike Smith out:
741cb9b0d80SMike Smith return_VALUE (error);
74215e32d5dSMike Smith }
74315e32d5dSMike Smith
744a7a3177fSJung-uk Kim static void
acpi_set_power_children(device_t dev,int state)745a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state)
746a7a3177fSJung-uk Kim {
74744aba0f6SJung-uk Kim device_t child;
748a7a3177fSJung-uk Kim device_t *devlist;
749a7a3177fSJung-uk Kim int dstate, i, numdevs;
750a7a3177fSJung-uk Kim
751a7a3177fSJung-uk Kim if (device_get_children(dev, &devlist, &numdevs) != 0)
752a7a3177fSJung-uk Kim return;
753a7a3177fSJung-uk Kim
754a7a3177fSJung-uk Kim /*
755a7a3177fSJung-uk Kim * Retrieve and set D-state for the sleep state if _SxD is present.
756a7a3177fSJung-uk Kim * Skip children who aren't attached since they are handled separately.
757a7a3177fSJung-uk Kim */
758a7a3177fSJung-uk Kim for (i = 0; i < numdevs; i++) {
759a7a3177fSJung-uk Kim child = devlist[i];
760a7a3177fSJung-uk Kim dstate = state;
761a7a3177fSJung-uk Kim if (device_is_attached(child) &&
76244aba0f6SJung-uk Kim acpi_device_pwr_for_sleep(dev, child, &dstate) == 0)
763a7a3177fSJung-uk Kim acpi_set_powerstate(child, dstate);
764a7a3177fSJung-uk Kim }
765a7a3177fSJung-uk Kim free(devlist, M_TEMP);
766a7a3177fSJung-uk Kim }
767a7a3177fSJung-uk Kim
768169b539aSNate Lawson static int
acpi_suspend(device_t dev)76910ce62b9SNate Lawson acpi_suspend(device_t dev)
77010ce62b9SNate Lawson {
771a7a3177fSJung-uk Kim int error;
77210ce62b9SNate Lawson
773d14bc723SWarner Losh bus_topo_assert();
774a56fe095SJohn Baldwin
77510ce62b9SNate Lawson error = bus_generic_suspend(dev);
776a7a3177fSJung-uk Kim if (error == 0)
777a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D3);
77810ce62b9SNate Lawson
77910ce62b9SNate Lawson return (error);
78010ce62b9SNate Lawson }
78110ce62b9SNate Lawson
78210ce62b9SNate Lawson static int
acpi_resume(device_t dev)78310ce62b9SNate Lawson acpi_resume(device_t dev)
78410ce62b9SNate Lawson {
78510ce62b9SNate Lawson
786d14bc723SWarner Losh bus_topo_assert();
787a56fe095SJohn Baldwin
788a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D0);
78910ce62b9SNate Lawson
79010ce62b9SNate Lawson return (bus_generic_resume(dev));
79110ce62b9SNate Lawson }
79210ce62b9SNate Lawson
79310ce62b9SNate Lawson static int
acpi_shutdown(device_t dev)794169b539aSNate Lawson acpi_shutdown(device_t dev)
795169b539aSNate Lawson {
796169b539aSNate Lawson
797d14bc723SWarner Losh bus_topo_assert();
798a56fe095SJohn Baldwin
7990e414868SNate Lawson /* Allow children to shutdown first. */
8000e414868SNate Lawson bus_generic_shutdown(dev);
8010e414868SNate Lawson
802bee4aa4aSNate Lawson /*
803bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC).
804bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most).
805bee4aa4aSNate Lawson */
806d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5);
807d4b9ff91SNate Lawson
808169b539aSNate Lawson return (0);
809169b539aSNate Lawson }
810169b539aSNate Lawson
81115e32d5dSMike Smith /*
81215e32d5dSMike Smith * Handle a new device being added
81315e32d5dSMike Smith */
81415e32d5dSMike Smith static device_t
acpi_add_child(device_t bus,u_int order,const char * name,int unit)8153d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit)
81615e32d5dSMike Smith {
81715e32d5dSMike Smith struct acpi_device *ad;
81815e32d5dSMike Smith device_t child;
81915e32d5dSMike Smith
820be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
82115e32d5dSMike Smith return (NULL);
82215e32d5dSMike Smith
8237dd1f0dcSKonstantin Belousov ad->ad_domain = ACPI_DEV_DOMAIN_UNKNOWN;
82415e32d5dSMike Smith resource_list_init(&ad->ad_rl);
82515e32d5dSMike Smith
82615e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit);
82715e32d5dSMike Smith if (child != NULL)
82815e32d5dSMike Smith device_set_ivars(child, ad);
82943ce1c77SNate Lawson else
83043ce1c77SNate Lawson free(ad, M_ACPIDEV);
83115e32d5dSMike Smith return (child);
83215e32d5dSMike Smith }
83315e32d5dSMike Smith
83415e32d5dSMike Smith static int
acpi_print_child(device_t bus,device_t child)83515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
83615e32d5dSMike Smith {
83715e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child);
83815e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl;
83915e32d5dSMike Smith int retval = 0;
84015e32d5dSMike Smith
84115e32d5dSMike Smith retval += bus_print_child_header(bus, child);
842da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
843da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
844da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
845da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
846a91c5fa8SNate Lawson if (device_get_flags(child))
847a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child));
848ffcf962dSAdrian Chadd retval += bus_print_child_domain(bus, child);
8492c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child);
85015e32d5dSMike Smith
85115e32d5dSMike Smith return (retval);
85215e32d5dSMike Smith }
85315e32d5dSMike Smith
85410ce62b9SNate Lawson /*
85510ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt
85610ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added.
85710ce62b9SNate Lawson *
85810ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and
85910ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect
86010ce62b9SNate Lawson * them to be powered up.
86110ce62b9SNate Lawson */
86210ce62b9SNate Lawson static void
acpi_probe_nomatch(device_t bus,device_t child)86310ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
86410ce62b9SNate Lawson {
86596cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
866a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3);
86796cf0b6dSWarner Losh #endif
86810ce62b9SNate Lawson }
86910ce62b9SNate Lawson
87010ce62b9SNate Lawson /*
87110ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up.
87210ce62b9SNate Lawson *
87310ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details).
87410ce62b9SNate Lawson */
87510ce62b9SNate Lawson static void
acpi_driver_added(device_t dev,driver_t * driver)87610ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
87710ce62b9SNate Lawson {
87810ce62b9SNate Lawson device_t child, *devlist;
87910ce62b9SNate Lawson int i, numdevs;
88010ce62b9SNate Lawson
88110ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev);
882099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs))
883099ea4b5SWarner Losh return;
88410ce62b9SNate Lawson for (i = 0; i < numdevs; i++) {
88510ce62b9SNate Lawson child = devlist[i];
88610ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) {
88796cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
888a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D0);
88910ce62b9SNate Lawson if (device_probe_and_attach(child) != 0)
890a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3);
89196cf0b6dSWarner Losh #else
89296cf0b6dSWarner Losh device_probe_and_attach(child);
89396cf0b6dSWarner Losh #endif
89410ce62b9SNate Lawson }
89510ce62b9SNate Lawson }
89610ce62b9SNate Lawson free(devlist, M_TEMP);
89710ce62b9SNate Lawson }
89810ce62b9SNate Lawson
89963600cc3SNate Lawson /* Location hint for devctl(8) */
90063600cc3SNate Lawson static int
acpi_child_location_method(device_t cbdev,device_t child,struct sbuf * sb)901ddfc9c4cSWarner Losh acpi_child_location_method(device_t cbdev, device_t child, struct sbuf *sb)
902247648afSTakanori Watanabe {
903247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child);
9049cf5a6aaSAdrian Chadd int pxm;
905247648afSTakanori Watanabe
9069cf5a6aaSAdrian Chadd if (dinfo->ad_handle) {
907ddfc9c4cSWarner Losh sbuf_printf(sb, "handle=%s", acpi_name(dinfo->ad_handle));
9089cf5a6aaSAdrian Chadd if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) {
909ddfc9c4cSWarner Losh sbuf_printf(sb, " _PXM=%d", pxm);
9109cf5a6aaSAdrian Chadd }
9119cf5a6aaSAdrian Chadd }
912247648afSTakanori Watanabe return (0);
913247648afSTakanori Watanabe }
914247648afSTakanori Watanabe
91563600cc3SNate Lawson /* PnP information for devctl(8) */
916ddf8c230SVladimir Kondratyev int
acpi_pnpinfo(ACPI_HANDLE handle,struct sbuf * sb)917ddfc9c4cSWarner Losh acpi_pnpinfo(ACPI_HANDLE handle, struct sbuf *sb)
918247648afSTakanori Watanabe {
91992488a57SJung-uk Kim ACPI_DEVICE_INFO *adinfo;
920247648afSTakanori Watanabe
921ddf8c230SVladimir Kondratyev if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &adinfo))) {
922ddfc9c4cSWarner Losh sbuf_printf(sb, "unknown");
92392488a57SJung-uk Kim return (0);
92492488a57SJung-uk Kim }
92592488a57SJung-uk Kim
926ddfc9c4cSWarner Losh sbuf_printf(sb, "_HID=%s _UID=%lu _CID=%s",
927247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ?
92892488a57SJung-uk Kim adinfo->HardwareId.String : "none",
92963600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ?
930359a5f96SConrad Meyer strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL,
931359a5f96SConrad Meyer ((adinfo->Valid & ACPI_VALID_CID) &&
932359a5f96SConrad Meyer adinfo->CompatibleIdList.Count > 0) ?
933359a5f96SConrad Meyer adinfo->CompatibleIdList.Ids[0].String : "none");
934247648afSTakanori Watanabe AcpiOsFree(adinfo);
935247648afSTakanori Watanabe
936247648afSTakanori Watanabe return (0);
937247648afSTakanori Watanabe }
938247648afSTakanori Watanabe
939ddf8c230SVladimir Kondratyev static int
acpi_child_pnpinfo_method(device_t cbdev,device_t child,struct sbuf * sb)940ddfc9c4cSWarner Losh acpi_child_pnpinfo_method(device_t cbdev, device_t child, struct sbuf *sb)
941ddf8c230SVladimir Kondratyev {
942ddf8c230SVladimir Kondratyev struct acpi_device *dinfo = device_get_ivars(child);
943ddf8c230SVladimir Kondratyev
944ddfc9c4cSWarner Losh return (acpi_pnpinfo(dinfo->ad_handle, sb));
945ddf8c230SVladimir Kondratyev }
946ddf8c230SVladimir Kondratyev
947247648afSTakanori Watanabe /*
948cc538081SGordon Bergling * Note: the check for ACPI locator may be redundant. However, this routine is
949cae7d9ecSWarner Losh * suitable for both busses whose only locator is ACPI and as a building block
950cae7d9ecSWarner Losh * for busses that have multiple locators to cope with.
951cae7d9ecSWarner Losh */
952cae7d9ecSWarner Losh int
acpi_get_acpi_device_path(device_t bus,device_t child,const char * locator,struct sbuf * sb)953cae7d9ecSWarner Losh acpi_get_acpi_device_path(device_t bus, device_t child, const char *locator, struct sbuf *sb)
954cae7d9ecSWarner Losh {
955cae7d9ecSWarner Losh if (strcmp(locator, BUS_LOCATOR_ACPI) == 0) {
956cae7d9ecSWarner Losh ACPI_HANDLE *handle = acpi_get_handle(child);
957cae7d9ecSWarner Losh
958cae7d9ecSWarner Losh if (handle != NULL)
959cae7d9ecSWarner Losh sbuf_printf(sb, "%s", acpi_name(handle));
960cae7d9ecSWarner Losh return (0);
961cae7d9ecSWarner Losh }
962cae7d9ecSWarner Losh
963cae7d9ecSWarner Losh return (bus_generic_get_device_path(bus, child, locator, sb));
964cae7d9ecSWarner Losh }
965cae7d9ecSWarner Losh
966cae7d9ecSWarner Losh static int
acpi_get_device_path(device_t bus,device_t child,const char * locator,struct sbuf * sb)967cae7d9ecSWarner Losh acpi_get_device_path(device_t bus, device_t child, const char *locator, struct sbuf *sb)
968cae7d9ecSWarner Losh {
969cae7d9ecSWarner Losh struct acpi_device *dinfo = device_get_ivars(child);
970cae7d9ecSWarner Losh
971cae7d9ecSWarner Losh if (strcmp(locator, BUS_LOCATOR_ACPI) == 0)
972cae7d9ecSWarner Losh return (acpi_get_acpi_device_path(bus, child, locator, sb));
973cae7d9ecSWarner Losh
974d0a20e40SWarner Losh if (strcmp(locator, BUS_LOCATOR_UEFI) == 0) {
975d0a20e40SWarner Losh ACPI_DEVICE_INFO *adinfo;
976d0a20e40SWarner Losh if (!ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo)) &&
977d0a20e40SWarner Losh dinfo->ad_handle != 0 && (adinfo->Valid & ACPI_VALID_HID)) {
978d0a20e40SWarner Losh const char *hid = adinfo->HardwareId.String;
979d0a20e40SWarner Losh u_long uid = (adinfo->Valid & ACPI_VALID_UID) ?
980d0a20e40SWarner Losh strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL;
981d0a20e40SWarner Losh u_long hidval;
982d0a20e40SWarner Losh
983d0a20e40SWarner Losh /*
984d0a20e40SWarner Losh * In UEFI Stanard Version 2.6, Section 9.6.1.6 Text
985d0a20e40SWarner Losh * Device Node Reference, there's an insanely long table
986d0a20e40SWarner Losh * 98. This implements the relevant bits from that
987d0a20e40SWarner Losh * table. Newer versions appear to have not required
988d0a20e40SWarner Losh * anything new. The EDK2 firmware presents both PciRoot
989d0a20e40SWarner Losh * and PcieRoot as PciRoot. Follow the EDK2 standard.
990d0a20e40SWarner Losh */
991d0a20e40SWarner Losh if (strncmp("PNP", hid, 3) != 0)
992d0a20e40SWarner Losh goto nomatch;
993d0a20e40SWarner Losh hidval = strtoul(hid + 3, NULL, 16);
994d0a20e40SWarner Losh switch (hidval) {
995d0a20e40SWarner Losh case 0x0301:
996d0a20e40SWarner Losh sbuf_printf(sb, "Keyboard(0x%lx)", uid);
997d0a20e40SWarner Losh break;
998d0a20e40SWarner Losh case 0x0401:
999d0a20e40SWarner Losh sbuf_printf(sb, "ParallelPort(0x%lx)", uid);
1000d0a20e40SWarner Losh break;
1001d0a20e40SWarner Losh case 0x0501:
1002d0a20e40SWarner Losh sbuf_printf(sb, "Serial(0x%lx)", uid);
1003d0a20e40SWarner Losh break;
1004d0a20e40SWarner Losh case 0x0604:
1005d0a20e40SWarner Losh sbuf_printf(sb, "Floppy(0x%lx)", uid);
1006d0a20e40SWarner Losh break;
1007d0a20e40SWarner Losh case 0x0a03:
1008d0a20e40SWarner Losh case 0x0a08:
1009d0a20e40SWarner Losh sbuf_printf(sb, "PciRoot(0x%lx)", uid);
1010d0a20e40SWarner Losh break;
1011d0a20e40SWarner Losh default: /* Everything else gets a generic encode */
1012d0a20e40SWarner Losh nomatch:
1013d0a20e40SWarner Losh sbuf_printf(sb, "Acpi(%s,0x%lx)", hid, uid);
1014d0a20e40SWarner Losh break;
1015d0a20e40SWarner Losh }
1016d0a20e40SWarner Losh }
1017d0a20e40SWarner Losh /* Not handled: AcpiAdr... unsure how to know it's one */
1018d0a20e40SWarner Losh }
1019d0a20e40SWarner Losh
1020cae7d9ecSWarner Losh /* For the rest, punt to the default handler */
1021cae7d9ecSWarner Losh return (bus_generic_get_device_path(bus, child, locator, sb));
1022cae7d9ecSWarner Losh }
1023cae7d9ecSWarner Losh
1024cae7d9ecSWarner Losh /*
1025709749aaSVladimir Kondratyev * Handle device deletion.
1026709749aaSVladimir Kondratyev */
1027709749aaSVladimir Kondratyev static void
acpi_child_deleted(device_t dev,device_t child)1028709749aaSVladimir Kondratyev acpi_child_deleted(device_t dev, device_t child)
1029709749aaSVladimir Kondratyev {
1030709749aaSVladimir Kondratyev struct acpi_device *dinfo = device_get_ivars(child);
1031709749aaSVladimir Kondratyev
1032709749aaSVladimir Kondratyev if (acpi_get_device(dinfo->ad_handle) == child)
1033709749aaSVladimir Kondratyev AcpiDetachData(dinfo->ad_handle, acpi_fake_objhandler);
1034709749aaSVladimir Kondratyev }
1035709749aaSVladimir Kondratyev
1036709749aaSVladimir Kondratyev /*
103715e32d5dSMike Smith * Handle per-device ivars
103815e32d5dSMike Smith */
103915e32d5dSMike Smith static int
acpi_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)104015e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
104115e32d5dSMike Smith {
104215e32d5dSMike Smith struct acpi_device *ad;
104315e32d5dSMike Smith
104415e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) {
10452d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n");
104615e32d5dSMike Smith return (ENOENT);
104715e32d5dSMike Smith }
104815e32d5dSMike Smith
1049be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */
105015e32d5dSMike Smith switch(index) {
105115e32d5dSMike Smith case ACPI_IVAR_HANDLE:
105215e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle;
105315e32d5dSMike Smith break;
105415e32d5dSMike Smith case ACPI_IVAR_PRIVATE:
105515e32d5dSMike Smith *(void **)result = ad->ad_private;
105615e32d5dSMike Smith break;
1057d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS:
1058d4b9ff91SNate Lawson *(int *)result = ad->ad_flags;
1059d4b9ff91SNate Lawson break;
10607dd1f0dcSKonstantin Belousov case ACPI_IVAR_DOMAIN:
10617dd1f0dcSKonstantin Belousov *(int *)result = ad->ad_domain;
10627dd1f0dcSKonstantin Belousov break;
106332d18aa5SMike Smith case ISA_IVAR_VENDORID:
106432d18aa5SMike Smith case ISA_IVAR_SERIAL:
106532d18aa5SMike Smith case ISA_IVAR_COMPATID:
106632d18aa5SMike Smith *(int *)result = -1;
106732d18aa5SMike Smith break;
106832d18aa5SMike Smith case ISA_IVAR_LOGICALID:
106932d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child);
107032d18aa5SMike Smith break;
10710e68afe5SAndrew Turner case PCI_IVAR_CLASS:
10720e68afe5SAndrew Turner *(uint8_t*)result = (ad->ad_cls_class >> 16) & 0xff;
10730e68afe5SAndrew Turner break;
10740e68afe5SAndrew Turner case PCI_IVAR_SUBCLASS:
10750e68afe5SAndrew Turner *(uint8_t*)result = (ad->ad_cls_class >> 8) & 0xff;
10760e68afe5SAndrew Turner break;
10770e68afe5SAndrew Turner case PCI_IVAR_PROGIF:
10780e68afe5SAndrew Turner *(uint8_t*)result = (ad->ad_cls_class >> 0) & 0xff;
10790e68afe5SAndrew Turner break;
108015e32d5dSMike Smith default:
108115e32d5dSMike Smith return (ENOENT);
108215e32d5dSMike Smith }
1083be2b1797SNate Lawson
108415e32d5dSMike Smith return (0);
108515e32d5dSMike Smith }
108615e32d5dSMike Smith
108715e32d5dSMike Smith static int
acpi_write_ivar(device_t dev,device_t child,int index,uintptr_t value)108815e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
108915e32d5dSMike Smith {
109015e32d5dSMike Smith struct acpi_device *ad;
109115e32d5dSMike Smith
109215e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) {
10932d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n");
109415e32d5dSMike Smith return (ENOENT);
109515e32d5dSMike Smith }
109615e32d5dSMike Smith
109715e32d5dSMike Smith switch(index) {
109815e32d5dSMike Smith case ACPI_IVAR_HANDLE:
109915e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value;
110015e32d5dSMike Smith break;
110115e32d5dSMike Smith case ACPI_IVAR_PRIVATE:
110215e32d5dSMike Smith ad->ad_private = (void *)value;
110315e32d5dSMike Smith break;
1104d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS:
1105d4b9ff91SNate Lawson ad->ad_flags = (int)value;
1106d4b9ff91SNate Lawson break;
11077dd1f0dcSKonstantin Belousov case ACPI_IVAR_DOMAIN:
11087dd1f0dcSKonstantin Belousov ad->ad_domain = (int)value;
11097dd1f0dcSKonstantin Belousov break;
111015e32d5dSMike Smith default:
11112ab060eeSWarner Losh panic("bad ivar write request (%d)", index);
111215e32d5dSMike Smith return (ENOENT);
111315e32d5dSMike Smith }
1114be2b1797SNate Lawson
111515e32d5dSMike Smith return (0);
111615e32d5dSMike Smith }
111715e32d5dSMike Smith
111815e32d5dSMike Smith /*
111915e32d5dSMike Smith * Handle child resource allocation/removal
112015e32d5dSMike Smith */
112191233413SNate Lawson static struct resource_list *
acpi_get_rlist(device_t dev,device_t child)112291233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
112315e32d5dSMike Smith {
112491233413SNate Lawson struct acpi_device *ad;
112515e32d5dSMike Smith
112691233413SNate Lawson ad = device_get_ivars(child);
112791233413SNate Lawson return (&ad->ad_rl);
112815e32d5dSMike Smith }
112915e32d5dSMike Smith
11300d484d24SJohn Baldwin static int
acpi_match_resource_hint(device_t dev,int type,long value)11310d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value)
11320d484d24SJohn Baldwin {
11330d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev);
11340d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl;
11350d484d24SJohn Baldwin struct resource_list_entry *rle;
11360d484d24SJohn Baldwin
11370d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) {
11380d484d24SJohn Baldwin if (rle->type != type)
11390d484d24SJohn Baldwin continue;
11400d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value)
11410d484d24SJohn Baldwin return (1);
11420d484d24SJohn Baldwin }
11430d484d24SJohn Baldwin return (0);
11440d484d24SJohn Baldwin }
11450d484d24SJohn Baldwin
11460d484d24SJohn Baldwin /*
11476837d9d7SWarner Losh * Does this device match because the resources match?
11480d484d24SJohn Baldwin */
11496837d9d7SWarner Losh static bool
acpi_hint_device_matches_resources(device_t child,const char * name,int unit)11506837d9d7SWarner Losh acpi_hint_device_matches_resources(device_t child, const char *name,
11516837d9d7SWarner Losh int unit)
11520d484d24SJohn Baldwin {
11530d484d24SJohn Baldwin long value;
11543278bf92SWarner Losh bool matches;
11550d484d24SJohn Baldwin
11560d484d24SJohn Baldwin /*
11572fcd493cSJohn Baldwin * Check for matching resources. We must have at least one match.
11582fcd493cSJohn Baldwin * Since I/O and memory resources cannot be shared, if we get a
11592fcd493cSJohn Baldwin * match on either of those, ignore any mismatches in IRQs or DRQs.
11600d484d24SJohn Baldwin *
11610d484d24SJohn Baldwin * XXX: We may want to revisit this to be more lenient and wire
11620d484d24SJohn Baldwin * as long as it gets one match.
11630d484d24SJohn Baldwin */
11643278bf92SWarner Losh matches = false;
11650d484d24SJohn Baldwin if (resource_long_value(name, unit, "port", &value) == 0) {
11662fcd493cSJohn Baldwin /*
11672fcd493cSJohn Baldwin * Floppy drive controllers are notorious for having a
11682fcd493cSJohn Baldwin * wide variety of resources not all of which include the
11692fcd493cSJohn Baldwin * first port that is specified by the hint (typically
11702fcd493cSJohn Baldwin * 0x3f0) (see the comment above fdc_isa_alloc_resources()
11712fcd493cSJohn Baldwin * in fdc_isa.c). However, they do all seem to include
11722fcd493cSJohn Baldwin * port + 2 (e.g. 0x3f2) so for a floppy device, look for
11732fcd493cSJohn Baldwin * 'value + 2' in the port resources instead of the hint
11742fcd493cSJohn Baldwin * value.
11752fcd493cSJohn Baldwin */
11762fcd493cSJohn Baldwin if (strcmp(name, "fdc") == 0)
11772fcd493cSJohn Baldwin value += 2;
11780d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
11793278bf92SWarner Losh matches = true;
11800d484d24SJohn Baldwin else
11816837d9d7SWarner Losh return false;
11820d484d24SJohn Baldwin }
11830d484d24SJohn Baldwin if (resource_long_value(name, unit, "maddr", &value) == 0) {
11840d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
11853278bf92SWarner Losh matches = true;
11860d484d24SJohn Baldwin else
11876837d9d7SWarner Losh return false;
11880d484d24SJohn Baldwin }
11896837d9d7SWarner Losh
11906837d9d7SWarner Losh /*
11916837d9d7SWarner Losh * If either the I/O address and/or the memory address matched, then
11926837d9d7SWarner Losh * assumed this devices matches and that any mismatch in other resources
11936837d9d7SWarner Losh * will be resolved by siltently ignoring those other resources. Otherwise
11946837d9d7SWarner Losh * all further resources must match.
11956837d9d7SWarner Losh */
11966837d9d7SWarner Losh if (matches) {
11976837d9d7SWarner Losh return (true);
11986837d9d7SWarner Losh }
11990d484d24SJohn Baldwin if (resource_long_value(name, unit, "irq", &value) == 0) {
12000d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
12013278bf92SWarner Losh matches = true;
12020d484d24SJohn Baldwin else
12036837d9d7SWarner Losh return false;
12040d484d24SJohn Baldwin }
12050d484d24SJohn Baldwin if (resource_long_value(name, unit, "drq", &value) == 0) {
12060d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
12073278bf92SWarner Losh matches = true;
12080d484d24SJohn Baldwin else
12096837d9d7SWarner Losh return false;
12106837d9d7SWarner Losh }
12116837d9d7SWarner Losh return matches;
12120d484d24SJohn Baldwin }
12130d484d24SJohn Baldwin
12146837d9d7SWarner Losh
12156837d9d7SWarner Losh /*
12166837d9d7SWarner Losh * Wire device unit numbers based on resource matches in hints.
12176837d9d7SWarner Losh */
12186837d9d7SWarner Losh static void
acpi_hint_device_unit(device_t acdev,device_t child,const char * name,int * unitp)12196837d9d7SWarner Losh acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
12206837d9d7SWarner Losh int *unitp)
12216837d9d7SWarner Losh {
1222dbee7944SWarner Losh device_location_cache_t *cache;
12236837d9d7SWarner Losh const char *s;
12246837d9d7SWarner Losh int line, unit;
12256837d9d7SWarner Losh bool matches;
12266837d9d7SWarner Losh
12276837d9d7SWarner Losh /*
12286837d9d7SWarner Losh * Iterate over all the hints for the devices with the specified
12296837d9d7SWarner Losh * name to see if one's resources are a subset of this device.
12306837d9d7SWarner Losh */
12316837d9d7SWarner Losh line = 0;
1232dbee7944SWarner Losh cache = dev_wired_cache_init();
12336837d9d7SWarner Losh while (resource_find_dev(&line, name, &unit, "at", NULL) == 0) {
12346837d9d7SWarner Losh /* Must have an "at" for acpi or isa. */
12356837d9d7SWarner Losh resource_string_value(name, unit, "at", &s);
12366837d9d7SWarner Losh matches = false;
12376837d9d7SWarner Losh if (strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
12386837d9d7SWarner Losh strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)
12396837d9d7SWarner Losh matches = acpi_hint_device_matches_resources(child, name, unit);
1240dbee7944SWarner Losh else
1241dbee7944SWarner Losh matches = dev_wired_cache_match(cache, child, s);
12426837d9d7SWarner Losh
12433278bf92SWarner Losh if (matches) {
12440d484d24SJohn Baldwin /* We have a winner! */
12450d484d24SJohn Baldwin *unitp = unit;
12460d484d24SJohn Baldwin break;
12470d484d24SJohn Baldwin }
12480d484d24SJohn Baldwin }
1249dbee7944SWarner Losh dev_wired_cache_fini(cache);
12500d484d24SJohn Baldwin }
12510d484d24SJohn Baldwin
1252adad4744SNate Lawson /*
12538d791e5aSJohn Baldwin * Fetch the NUMA domain for a device by mapping the value returned by
12548d791e5aSJohn Baldwin * _PXM to a NUMA domain. If the device does not have a _PXM method,
12558d791e5aSJohn Baldwin * -2 is returned. If any other error occurs, -1 is returned.
12565d18c60aSAdrian Chadd */
12576f423295SKonstantin Belousov int
acpi_pxm_parse(device_t dev)12586f423295SKonstantin Belousov acpi_pxm_parse(device_t dev)
12595d18c60aSAdrian Chadd {
1260b6715dabSJeff Roberson #ifdef NUMA
1261523a67bbSOleksandr Tymoshenko #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
12628d791e5aSJohn Baldwin ACPI_HANDLE handle;
12638d791e5aSJohn Baldwin ACPI_STATUS status;
12648d791e5aSJohn Baldwin int pxm;
12655d18c60aSAdrian Chadd
12668d791e5aSJohn Baldwin handle = acpi_get_handle(dev);
12678d791e5aSJohn Baldwin if (handle == NULL)
12688d791e5aSJohn Baldwin return (-2);
12698d791e5aSJohn Baldwin status = acpi_GetInteger(handle, "_PXM", &pxm);
12708d791e5aSJohn Baldwin if (ACPI_SUCCESS(status))
12718d791e5aSJohn Baldwin return (acpi_map_pxm_to_vm_domainid(pxm));
12728d791e5aSJohn Baldwin if (status == AE_NOT_FOUND)
12738d791e5aSJohn Baldwin return (-2);
12748a08b7d3SJohn Baldwin #endif
1275bcc51ef4SMark Johnston #endif
12768d791e5aSJohn Baldwin return (-1);
12778d791e5aSJohn Baldwin }
12788a08b7d3SJohn Baldwin
12798d791e5aSJohn Baldwin int
acpi_get_cpus(device_t dev,device_t child,enum cpu_sets op,size_t setsize,cpuset_t * cpuset)12808d791e5aSJohn Baldwin acpi_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
12818d791e5aSJohn Baldwin cpuset_t *cpuset)
12828d791e5aSJohn Baldwin {
12838d791e5aSJohn Baldwin int d, error;
12848d791e5aSJohn Baldwin
12856f423295SKonstantin Belousov d = acpi_pxm_parse(child);
12868d791e5aSJohn Baldwin if (d < 0)
12878d791e5aSJohn Baldwin return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
12888d791e5aSJohn Baldwin
12898d791e5aSJohn Baldwin switch (op) {
12908d791e5aSJohn Baldwin case LOCAL_CPUS:
12918d791e5aSJohn Baldwin if (setsize != sizeof(cpuset_t))
12928d791e5aSJohn Baldwin return (EINVAL);
12938d791e5aSJohn Baldwin *cpuset = cpuset_domain[d];
12948a08b7d3SJohn Baldwin return (0);
12958d791e5aSJohn Baldwin case INTR_CPUS:
12968d791e5aSJohn Baldwin error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
12978d791e5aSJohn Baldwin if (error != 0)
12988d791e5aSJohn Baldwin return (error);
12998d791e5aSJohn Baldwin if (setsize != sizeof(cpuset_t))
13008d791e5aSJohn Baldwin return (EINVAL);
1301e2650af1SStefan Eßer CPU_AND(cpuset, cpuset, &cpuset_domain[d]);
13028d791e5aSJohn Baldwin return (0);
13038d791e5aSJohn Baldwin default:
13048d791e5aSJohn Baldwin return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
13058d791e5aSJohn Baldwin }
13065d18c60aSAdrian Chadd }
13075d18c60aSAdrian Chadd
13087dd1f0dcSKonstantin Belousov static int
acpi_get_domain_method(device_t dev,device_t child,int * domain)13097dd1f0dcSKonstantin Belousov acpi_get_domain_method(device_t dev, device_t child, int *domain)
1310ffcf962dSAdrian Chadd {
13117dd1f0dcSKonstantin Belousov int error;
1312ffcf962dSAdrian Chadd
13137dd1f0dcSKonstantin Belousov error = acpi_read_ivar(dev, child, ACPI_IVAR_DOMAIN,
13147dd1f0dcSKonstantin Belousov (uintptr_t *)domain);
13157dd1f0dcSKonstantin Belousov if (error == 0 && *domain != ACPI_DEV_DOMAIN_UNKNOWN)
13168a08b7d3SJohn Baldwin return (0);
13178d791e5aSJohn Baldwin return (ENOENT);
1318ffcf962dSAdrian Chadd }
1319ffcf962dSAdrian Chadd
13200e1246e3SJohn Baldwin static struct rman *
acpi_get_rman(device_t bus,int type,u_int flags)13210e1246e3SJohn Baldwin acpi_get_rman(device_t bus, int type, u_int flags)
13220e1246e3SJohn Baldwin {
13230e1246e3SJohn Baldwin /* Only memory and IO resources are managed. */
13240e1246e3SJohn Baldwin switch (type) {
13250e1246e3SJohn Baldwin case SYS_RES_IOPORT:
13260e1246e3SJohn Baldwin return (&acpi_rman_io);
13270e1246e3SJohn Baldwin case SYS_RES_MEMORY:
13280e1246e3SJohn Baldwin return (&acpi_rman_mem);
13290e1246e3SJohn Baldwin default:
13300e1246e3SJohn Baldwin return (NULL);
13310e1246e3SJohn Baldwin }
13320e1246e3SJohn Baldwin }
13330e1246e3SJohn Baldwin
1334ffcf962dSAdrian Chadd /*
1335adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle
1336adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine.
1337adad4744SNate Lawson */
1338adad4744SNate Lawson static int
acpi_sysres_alloc(device_t dev)1339adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
1340adad4744SNate Lawson {
13410e1246e3SJohn Baldwin struct acpi_softc *sc = device_get_softc(dev);
1342adad4744SNate Lawson struct resource *res;
1343adad4744SNate Lawson struct resource_list_entry *rle;
1344adad4744SNate Lawson struct rman *rm;
1345da72d149SNate Lawson device_t *children;
1346da72d149SNate Lawson int child_count, i;
1347da72d149SNate Lawson
1348da72d149SNate Lawson /*
1349da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we
1350da72d149SNate Lawson * had multi-pass probe/attach.
1351da72d149SNate Lawson */
1352da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0)
1353da72d149SNate Lawson return (ENXIO);
1354da72d149SNate Lawson for (i = 0; i < child_count; i++) {
13555efca36fSTakanori Watanabe if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1356da72d149SNate Lawson device_probe_and_attach(children[i]);
1357da72d149SNate Lawson }
1358da72d149SNate Lawson free(children, M_TEMP);
1359adad4744SNate Lawson
13600e1246e3SJohn Baldwin STAILQ_FOREACH(rle, &sc->sysres_rl, link) {
1361adad4744SNate Lawson if (rle->res != NULL) {
1362da1b038aSJustin Hibbits device_printf(dev, "duplicate resource for %jx\n", rle->start);
1363adad4744SNate Lawson continue;
1364adad4744SNate Lawson }
1365adad4744SNate Lawson
1366adad4744SNate Lawson /* Only memory and IO resources are valid here. */
13670e1246e3SJohn Baldwin rm = acpi_get_rman(dev, rle->type, 0);
13680e1246e3SJohn Baldwin if (rm == NULL)
1369adad4744SNate Lawson continue;
1370adad4744SNate Lawson
1371adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */
13720e1246e3SJohn Baldwin res = bus_alloc_resource(dev, rle->type,
13730e1246e3SJohn Baldwin &rle->rid, rle->start, rle->start + rle->count - 1, rle->count,
13740e1246e3SJohn Baldwin RF_ACTIVE | RF_UNMAPPED);
1375adad4744SNate Lawson if (res != NULL) {
1376adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1377adad4744SNate Lawson rle->res = res;
137844947d3aSJohn Baldwin } else if (bootverbose)
1379da1b038aSJustin Hibbits device_printf(dev, "reservation of %jx, %jx (%d) failed\n",
1380adad4744SNate Lawson rle->start, rle->count, rle->type);
1381adad4744SNate Lawson }
1382adad4744SNate Lawson return (0);
1383adad4744SNate Lawson }
1384adad4744SNate Lawson
1385ea233199SJohn Baldwin /*
1386f2fcb680SJohn Baldwin * Reserve declared resources for active devices found during the
1387f2fcb680SJohn Baldwin * namespace scan once the boot-time attach of devices has completed.
1388f2fcb680SJohn Baldwin *
1389f2fcb680SJohn Baldwin * Ideally reserving firmware-assigned resources would work in a
1390f2fcb680SJohn Baldwin * depth-first traversal of the device namespace, but this is
1391f2fcb680SJohn Baldwin * complicated. In particular, not all resources are enumerated by
1392f2fcb680SJohn Baldwin * ACPI (e.g. PCI bridges and devices enumerate their resources via
1393f2fcb680SJohn Baldwin * other means). Some systems also enumerate devices via ACPI behind
1394f2fcb680SJohn Baldwin * PCI bridges but without a matching a PCI device_t enumerated via
1395f2fcb680SJohn Baldwin * PCI bus scanning, the device_t's end up as direct children of
1396f2fcb680SJohn Baldwin * acpi0. Doing this scan late is not ideal, but works for now.
1397ea233199SJohn Baldwin */
1398ea233199SJohn Baldwin static void
acpi_reserve_resources(device_t dev)1399ea233199SJohn Baldwin acpi_reserve_resources(device_t dev)
1400ea233199SJohn Baldwin {
1401ea233199SJohn Baldwin struct resource_list_entry *rle;
1402ea233199SJohn Baldwin struct resource_list *rl;
1403ea233199SJohn Baldwin struct acpi_device *ad;
1404ea233199SJohn Baldwin device_t *children;
1405ea233199SJohn Baldwin int child_count, i;
1406ea233199SJohn Baldwin
1407ea233199SJohn Baldwin if (device_get_children(dev, &children, &child_count) != 0)
1408ea233199SJohn Baldwin return;
1409ea233199SJohn Baldwin for (i = 0; i < child_count; i++) {
1410ea233199SJohn Baldwin ad = device_get_ivars(children[i]);
1411ea233199SJohn Baldwin rl = &ad->ad_rl;
1412ea233199SJohn Baldwin
1413ea233199SJohn Baldwin /* Don't reserve system resources. */
14145efca36fSTakanori Watanabe if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1415ea233199SJohn Baldwin continue;
1416ea233199SJohn Baldwin
1417ea233199SJohn Baldwin STAILQ_FOREACH(rle, rl, link) {
1418ea233199SJohn Baldwin /*
1419ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things
1420ea233199SJohn Baldwin * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1421ea233199SJohn Baldwin * when using legacy routing).
1422ea233199SJohn Baldwin */
1423ea233199SJohn Baldwin if (rle->type == SYS_RES_IRQ)
1424ea233199SJohn Baldwin continue;
1425ea233199SJohn Baldwin
1426ea233199SJohn Baldwin /*
14270d81cf12SJohn Baldwin * Don't reserve the resource if it is already allocated.
14280d81cf12SJohn Baldwin * The acpi_ec(4) driver can allocate its resources early
14290d81cf12SJohn Baldwin * if ECDT is present.
14300d81cf12SJohn Baldwin */
14310d81cf12SJohn Baldwin if (rle->res != NULL)
14320d81cf12SJohn Baldwin continue;
14330d81cf12SJohn Baldwin
14340d81cf12SJohn Baldwin /*
1435ea233199SJohn Baldwin * Try to reserve the resource from our parent. If this
1436ea233199SJohn Baldwin * fails because the resource is a system resource, just
1437ea233199SJohn Baldwin * let it be. The resource range is already reserved so
1438ea233199SJohn Baldwin * that other devices will not use it. If the driver
1439ea233199SJohn Baldwin * needs to allocate the resource, then
1440ea233199SJohn Baldwin * acpi_alloc_resource() will sub-alloc from the system
1441ea233199SJohn Baldwin * resource.
1442ea233199SJohn Baldwin */
1443ea233199SJohn Baldwin resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1444ea233199SJohn Baldwin rle->start, rle->end, rle->count, 0);
1445ea233199SJohn Baldwin }
1446ea233199SJohn Baldwin }
1447ea233199SJohn Baldwin free(children, M_TEMP);
1448ea233199SJohn Baldwin }
1449ea233199SJohn Baldwin
1450ea233199SJohn Baldwin static int
acpi_set_resource(device_t dev,device_t child,int type,int rid,rman_res_t start,rman_res_t count)1451ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid,
14522dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t count)
1453ea233199SJohn Baldwin {
1454ea233199SJohn Baldwin struct acpi_device *ad = device_get_ivars(child);
1455ea233199SJohn Baldwin struct resource_list *rl = &ad->ad_rl;
14562dd1bdf1SJustin Hibbits rman_res_t end;
14577e3343bfSJohn Baldwin
1458d4d6ad3fSJayachandran C. #ifdef INTRNG
1459d4d6ad3fSJayachandran C. /* map with default for now */
1460d4d6ad3fSJayachandran C. if (type == SYS_RES_IRQ)
1461d4d6ad3fSJayachandran C. start = (rman_res_t)acpi_map_intr(child, (u_int)start,
1462d4d6ad3fSJayachandran C. acpi_get_handle(child));
1463d4d6ad3fSJayachandran C. #endif
1464d4d6ad3fSJayachandran C.
1465ea233199SJohn Baldwin /* If the resource is already allocated, fail. */
1466ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid))
1467ea233199SJohn Baldwin return (EBUSY);
1468ea233199SJohn Baldwin
1469ea233199SJohn Baldwin /* If the resource is already reserved, release it. */
1470ea233199SJohn Baldwin if (resource_list_reserved(rl, type, rid))
1471ea233199SJohn Baldwin resource_list_unreserve(rl, dev, child, type, rid);
1472ea233199SJohn Baldwin
1473ea233199SJohn Baldwin /* Add the resource. */
1474ea233199SJohn Baldwin end = (start + count - 1);
1475ea233199SJohn Baldwin resource_list_add(rl, type, rid, start, end, count);
1476ea233199SJohn Baldwin return (0);
1477ea233199SJohn Baldwin }
1478ea233199SJohn Baldwin
147915e32d5dSMike Smith static struct resource *
acpi_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)148015e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
14812dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
148215e32d5dSMike Smith {
1483224c3776SAndrew Turner #ifndef INTRNG
148495957f62SJohn Baldwin ACPI_RESOURCE ares;
1485224c3776SAndrew Turner #endif
1486ea233199SJohn Baldwin struct acpi_device *ad;
148791233413SNate Lawson struct resource_list_entry *rle;
1488ea233199SJohn Baldwin struct resource_list *rl;
148991233413SNate Lawson struct resource *res;
14907915adb5SJustin Hibbits int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
149115e2f34fSNate Lawson
149291233413SNate Lawson /*
1493ea233199SJohn Baldwin * First attempt at allocating the resource. For direct children,
1494ea233199SJohn Baldwin * use resource_list_alloc() to handle reserved resources. For
1495f6133e71SJohn Baldwin * other devices, pass the request up to our parent.
149691233413SNate Lawson */
1497ea233199SJohn Baldwin if (bus == device_get_parent(child)) {
1498ea233199SJohn Baldwin ad = device_get_ivars(child);
1499ea233199SJohn Baldwin rl = &ad->ad_rl;
150091233413SNate Lawson
1501397c30a8SJohn Baldwin /*
1502ea233199SJohn Baldwin * Simulate the behavior of the ISA bus for direct children
1503ea233199SJohn Baldwin * devices. That is, if a non-default range is specified for
1504ea233199SJohn Baldwin * a resource that doesn't exist, use bus_set_resource() to
1505ea233199SJohn Baldwin * add the resource before allocating it. Note that these
1506ea233199SJohn Baldwin * resources will not be reserved.
1507397c30a8SJohn Baldwin */
1508ea233199SJohn Baldwin if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1509ea233199SJohn Baldwin resource_list_add(rl, type, *rid, start, end, count);
1510ea233199SJohn Baldwin res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1511ea233199SJohn Baldwin flags);
1512224c3776SAndrew Turner #ifndef INTRNG
1513ea233199SJohn Baldwin if (res != NULL && type == SYS_RES_IRQ) {
151495957f62SJohn Baldwin /*
151595957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot
151695957f62SJohn Baldwin * configure the interrupt associated with a device when we
151795957f62SJohn Baldwin * parse the resources but have to defer it until a driver
151895957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource().
151995957f62SJohn Baldwin *
152095957f62SJohn Baldwin * XXX: Should we handle the lookup failing?
152195957f62SJohn Baldwin */
152295957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
152395957f62SJohn Baldwin acpi_config_intr(child, &ares);
152495957f62SJohn Baldwin }
1525224c3776SAndrew Turner #endif
152615e2f34fSNate Lawson
1527ea233199SJohn Baldwin /*
1528ea233199SJohn Baldwin * If this is an allocation of the "default" range for a given
1529ea233199SJohn Baldwin * RID, fetch the exact bounds for this resource from the
1530ea233199SJohn Baldwin * resource list entry to try to allocate the range from the
1531ea233199SJohn Baldwin * system resource regions.
1532ea233199SJohn Baldwin */
1533ea233199SJohn Baldwin if (res == NULL && isdefault) {
1534ea233199SJohn Baldwin rle = resource_list_find(rl, type, *rid);
1535ea233199SJohn Baldwin if (rle != NULL) {
1536ea233199SJohn Baldwin start = rle->start;
1537ea233199SJohn Baldwin end = rle->end;
1538ea233199SJohn Baldwin count = rle->count;
1539ea233199SJohn Baldwin }
1540ea233199SJohn Baldwin }
1541ea233199SJohn Baldwin } else
15421fb54452SJohn Baldwin res = bus_generic_alloc_resource(bus, child, type, rid,
1543ea233199SJohn Baldwin start, end, count, flags);
1544ea233199SJohn Baldwin
1545ea233199SJohn Baldwin /*
1546ea233199SJohn Baldwin * If the first attempt failed and this is an allocation of a
1547ea233199SJohn Baldwin * specific range, try to satisfy the request via a suballocation
15485d0d779bSJohn Baldwin * from our system resource regions.
1549ea233199SJohn Baldwin */
15505d0d779bSJohn Baldwin if (res == NULL && start + count - 1 == end)
15510e1246e3SJohn Baldwin res = bus_generic_rman_alloc_resource(bus, child, type, rid, start, end,
15520e1246e3SJohn Baldwin count, flags);
15535d0d779bSJohn Baldwin return (res);
15545d0d779bSJohn Baldwin }
15555d0d779bSJohn Baldwin
15560e1246e3SJohn Baldwin static bool
acpi_is_resource_managed(device_t bus,struct resource * r)15570ecee160SJohn Baldwin acpi_is_resource_managed(device_t bus, struct resource *r)
15585d0d779bSJohn Baldwin {
15595d0d779bSJohn Baldwin struct rman *rm;
15605d0d779bSJohn Baldwin
15610ecee160SJohn Baldwin rm = acpi_get_rman(bus, rman_get_type(r), rman_get_flags(r));
15620e1246e3SJohn Baldwin if (rm == NULL)
15630e1246e3SJohn Baldwin return (false);
15640e1246e3SJohn Baldwin return (rman_is_region_manager(r, rm));
1565ea233199SJohn Baldwin }
1566ea233199SJohn Baldwin
15670e1246e3SJohn Baldwin static struct resource *
acpi_managed_resource(device_t bus,struct resource * r)1568d77f2092SJohn Baldwin acpi_managed_resource(device_t bus, struct resource *r)
156915e32d5dSMike Smith {
15700e1246e3SJohn Baldwin struct acpi_softc *sc = device_get_softc(bus);
15710e1246e3SJohn Baldwin struct resource_list_entry *rle;
157215e32d5dSMike Smith
15730ecee160SJohn Baldwin KASSERT(acpi_is_resource_managed(bus, r),
15740e1246e3SJohn Baldwin ("resource %p is not suballocated", r));
15750e1246e3SJohn Baldwin
15760e1246e3SJohn Baldwin STAILQ_FOREACH(rle, &sc->sysres_rl, link) {
1577d77f2092SJohn Baldwin if (rle->type != rman_get_type(r) || rle->res == NULL)
15780e1246e3SJohn Baldwin continue;
15790e1246e3SJohn Baldwin if (rman_get_start(r) >= rman_get_start(rle->res) &&
15800e1246e3SJohn Baldwin rman_get_end(r) <= rman_get_end(rle->res))
15810e1246e3SJohn Baldwin return (rle->res);
1582397c30a8SJohn Baldwin }
15830e1246e3SJohn Baldwin return (NULL);
1584049dc0d1SJohn Baldwin }
1585049dc0d1SJohn Baldwin
1586049dc0d1SJohn Baldwin static int
acpi_adjust_resource(device_t bus,device_t child,struct resource * r,rman_res_t start,rman_res_t end)1587fef01f04SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, struct resource *r,
15882dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end)
1589049dc0d1SJohn Baldwin {
1590049dc0d1SJohn Baldwin
15910ecee160SJohn Baldwin if (acpi_is_resource_managed(bus, r))
1592049dc0d1SJohn Baldwin return (rman_adjust_resource(r, start, end));
1593fef01f04SJohn Baldwin return (bus_generic_adjust_resource(bus, child, r, start, end));
1594049dc0d1SJohn Baldwin }
1595049dc0d1SJohn Baldwin
1596049dc0d1SJohn Baldwin static int
acpi_release_resource(device_t bus,device_t child,struct resource * r)15979dbf5b0eSJohn Baldwin acpi_release_resource(device_t bus, device_t child, struct resource *r)
1598049dc0d1SJohn Baldwin {
159991233413SNate Lawson /*
1600397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers,
1601ea233199SJohn Baldwin * deactivate it and release it to the local pool.
160291233413SNate Lawson */
16030ecee160SJohn Baldwin if (acpi_is_resource_managed(bus, r))
16049dbf5b0eSJohn Baldwin return (bus_generic_rman_release_resource(bus, child, r));
1605ea233199SJohn Baldwin
16069dbf5b0eSJohn Baldwin return (bus_generic_rl_release_resource(bus, child, r));
1607ea233199SJohn Baldwin }
160815e32d5dSMike Smith
16098b28b622SNate Lawson static void
acpi_delete_resource(device_t bus,device_t child,int type,int rid)16108b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
16118b28b622SNate Lawson {
16128b28b622SNate Lawson struct resource_list *rl;
16138b28b622SNate Lawson
16148b28b622SNate Lawson rl = acpi_get_rlist(bus, child);
1615ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) {
1616ea233199SJohn Baldwin device_printf(bus, "delete_resource: Resource still owned by child"
1617ea233199SJohn Baldwin " (type=%d, rid=%d)\n", type, rid);
1618ea233199SJohn Baldwin return;
1619ea233199SJohn Baldwin }
1620e05436d5SJohn Baldwin if (resource_list_reserved(rl, type, rid))
1621ea233199SJohn Baldwin resource_list_unreserve(rl, bus, child, type, rid);
16228b28b622SNate Lawson resource_list_delete(rl, type, rid);
16238b28b622SNate Lawson }
16248b28b622SNate Lawson
16250e1246e3SJohn Baldwin static int
acpi_activate_resource(device_t bus,device_t child,struct resource * r)16262baed46eSJohn Baldwin acpi_activate_resource(device_t bus, device_t child, struct resource *r)
16270e1246e3SJohn Baldwin {
16280ecee160SJohn Baldwin if (acpi_is_resource_managed(bus, r))
16292baed46eSJohn Baldwin return (bus_generic_rman_activate_resource(bus, child, r));
16302baed46eSJohn Baldwin return (bus_generic_activate_resource(bus, child, r));
16310e1246e3SJohn Baldwin }
16320e1246e3SJohn Baldwin
16330e1246e3SJohn Baldwin static int
acpi_deactivate_resource(device_t bus,device_t child,struct resource * r)16342baed46eSJohn Baldwin acpi_deactivate_resource(device_t bus, device_t child, struct resource *r)
16350e1246e3SJohn Baldwin {
16360ecee160SJohn Baldwin if (acpi_is_resource_managed(bus, r))
16372baed46eSJohn Baldwin return (bus_generic_rman_deactivate_resource(bus, child, r));
16382baed46eSJohn Baldwin return (bus_generic_deactivate_resource(bus, child, r));
16390e1246e3SJohn Baldwin }
16400e1246e3SJohn Baldwin
16410e1246e3SJohn Baldwin static int
acpi_map_resource(device_t bus,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)1642d77f2092SJohn Baldwin acpi_map_resource(device_t bus, device_t child, struct resource *r,
16430e1246e3SJohn Baldwin struct resource_map_request *argsp, struct resource_map *map)
16440e1246e3SJohn Baldwin {
16450e1246e3SJohn Baldwin struct resource_map_request args;
16460e1246e3SJohn Baldwin struct resource *sysres;
16470e1246e3SJohn Baldwin rman_res_t length, start;
16480e1246e3SJohn Baldwin int error;
16490e1246e3SJohn Baldwin
16500ecee160SJohn Baldwin if (!acpi_is_resource_managed(bus, r))
1651d77f2092SJohn Baldwin return (bus_generic_map_resource(bus, child, r, argsp, map));
16520e1246e3SJohn Baldwin
16530e1246e3SJohn Baldwin /* Resources must be active to be mapped. */
16540e1246e3SJohn Baldwin if (!(rman_get_flags(r) & RF_ACTIVE))
16550e1246e3SJohn Baldwin return (ENXIO);
16560e1246e3SJohn Baldwin
16570e1246e3SJohn Baldwin resource_init_map_request(&args);
16580e1246e3SJohn Baldwin error = resource_validate_map_request(r, argsp, &args, &start, &length);
16590e1246e3SJohn Baldwin if (error)
16600e1246e3SJohn Baldwin return (error);
16610e1246e3SJohn Baldwin
1662d77f2092SJohn Baldwin sysres = acpi_managed_resource(bus, r);
16630e1246e3SJohn Baldwin if (sysres == NULL)
16640e1246e3SJohn Baldwin return (ENOENT);
16650e1246e3SJohn Baldwin
16660e1246e3SJohn Baldwin args.offset = start - rman_get_start(sysres);
16670e1246e3SJohn Baldwin args.length = length;
166898056127SJohn Baldwin return (bus_map_resource(bus, sysres, &args, map));
16690e1246e3SJohn Baldwin }
16700e1246e3SJohn Baldwin
16710e1246e3SJohn Baldwin static int
acpi_unmap_resource(device_t bus,device_t child,struct resource * r,struct resource_map * map)1672d77f2092SJohn Baldwin acpi_unmap_resource(device_t bus, device_t child, struct resource *r,
16730e1246e3SJohn Baldwin struct resource_map *map)
16740e1246e3SJohn Baldwin {
167598056127SJohn Baldwin struct resource *sysres;
167698056127SJohn Baldwin
167798056127SJohn Baldwin if (!acpi_is_resource_managed(bus, r))
1678d77f2092SJohn Baldwin return (bus_generic_unmap_resource(bus, child, r, map));
167998056127SJohn Baldwin
168098056127SJohn Baldwin sysres = acpi_managed_resource(bus, r);
168198056127SJohn Baldwin if (sysres == NULL)
168298056127SJohn Baldwin return (ENOENT);
168398056127SJohn Baldwin return (bus_unmap_resource(bus, sysres, map));
16840e1246e3SJohn Baldwin }
16850e1246e3SJohn Baldwin
1686dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1687e1c4bf3fSNate Lawson int
acpi_bus_alloc_gas(device_t dev,int * type,int * rid,ACPI_GENERIC_ADDRESS * gas,struct resource ** res,u_int flags)1688e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1689907b6777SNate Lawson struct resource **res, u_int flags)
1690dc750869SNate Lawson {
1691e1c4bf3fSNate Lawson int error, res_type;
1692dc750869SNate Lawson
1693e1c4bf3fSNate Lawson error = ENOMEM;
16948f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1695e1c4bf3fSNate Lawson return (EINVAL);
1696dc750869SNate Lawson
16978f118e25SNate Lawson /* We only support memory and IO spaces. */
16982be4e471SJung-uk Kim switch (gas->SpaceId) {
1699dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1700e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY;
1701dc750869SNate Lawson break;
1702dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO:
1703e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT;
1704dc750869SNate Lawson break;
1705dc750869SNate Lawson default:
1706e1c4bf3fSNate Lawson return (EOPNOTSUPP);
1707dc750869SNate Lawson }
1708dc750869SNate Lawson
17092fe912dfSNate Lawson /*
1710f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means
1711f45fc848SNate Lawson * it is a bit field and just allocate a byte.
17122fe912dfSNate Lawson */
17132be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8)
17142be4e471SJung-uk Kim gas->BitWidth = 8;
17152fe912dfSNate Lawson
17168f118e25SNate Lawson /* Validate the address after we're sure we support the space. */
17172be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0)
17188f118e25SNate Lawson return (EINVAL);
17198f118e25SNate Lawson
1720e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address,
17212be4e471SJung-uk Kim gas->BitWidth / 8);
1722907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1723e1c4bf3fSNate Lawson if (*res != NULL) {
1724e1c4bf3fSNate Lawson *type = res_type;
1725e1c4bf3fSNate Lawson error = 0;
1726ca2c69c8SNate Lawson } else
1727ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid);
1728ca2c69c8SNate Lawson
1729e1c4bf3fSNate Lawson return (error);
1730dc750869SNate Lawson }
1731dc750869SNate Lawson
1732c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
17331e4925e8SNate Lawson static uint32_t
acpi_isa_get_logicalid(device_t dev)173432d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1735bc0f2195SMike Smith {
17361e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo;
1737bc0f2195SMike Smith ACPI_HANDLE h;
173892488a57SJung-uk Kim uint32_t pnpid;
173932d18aa5SMike Smith
1740b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
174132d18aa5SMike Smith
17426fca9360SNate Lawson /* Fetch and validate the HID. */
174392488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL ||
174492488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
174592488a57SJung-uk Kim return_VALUE (0);
174632d18aa5SMike Smith
174792488a57SJung-uk Kim pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
174892488a57SJung-uk Kim devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
174992488a57SJung-uk Kim PNP_EISAID(devinfo->HardwareId.String) : 0;
175092488a57SJung-uk Kim AcpiOsFree(devinfo);
1751be2b1797SNate Lawson
175232d18aa5SMike Smith return_VALUE (pnpid);
175332d18aa5SMike Smith }
175432d18aa5SMike Smith
17551e4925e8SNate Lawson static int
acpi_isa_get_compatid(device_t dev,uint32_t * cids,int count)17561e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1757b2566207STakanori Watanabe {
17581e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo;
17598ef1a331SJung-uk Kim ACPI_PNP_DEVICE_ID *ids;
1760b2566207STakanori Watanabe ACPI_HANDLE h;
17611e4925e8SNate Lawson uint32_t *pnpid;
176292488a57SJung-uk Kim int i, valid;
1763b2566207STakanori Watanabe
1764b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1765b2566207STakanori Watanabe
17661e4925e8SNate Lawson pnpid = cids;
1767bd189fe7SNate Lawson
17681e4925e8SNate Lawson /* Fetch and validate the CID */
176992488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL ||
177092488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
177192488a57SJung-uk Kim return_VALUE (0);
1772b2566207STakanori Watanabe
177392488a57SJung-uk Kim if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
177492488a57SJung-uk Kim AcpiOsFree(devinfo);
177592488a57SJung-uk Kim return_VALUE (0);
1776b2566207STakanori Watanabe }
1777b2566207STakanori Watanabe
177892488a57SJung-uk Kim if (devinfo->CompatibleIdList.Count < count)
177992488a57SJung-uk Kim count = devinfo->CompatibleIdList.Count;
178092488a57SJung-uk Kim ids = devinfo->CompatibleIdList.Ids;
178192488a57SJung-uk Kim for (i = 0, valid = 0; i < count; i++)
178292488a57SJung-uk Kim if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
178392488a57SJung-uk Kim strncmp(ids[i].String, "PNP", 3) == 0) {
178492488a57SJung-uk Kim *pnpid++ = PNP_EISAID(ids[i].String);
178592488a57SJung-uk Kim valid++;
178692488a57SJung-uk Kim }
178792488a57SJung-uk Kim AcpiOsFree(devinfo);
178892488a57SJung-uk Kim
17891e4925e8SNate Lawson return_VALUE (valid);
17901e4925e8SNate Lawson }
1791b2566207STakanori Watanabe
17925efca36fSTakanori Watanabe static int
acpi_device_id_probe(device_t bus,device_t dev,char ** ids,char ** match)17935efca36fSTakanori Watanabe acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match)
1794ce619b2eSNate Lawson {
1795ce619b2eSNate Lawson ACPI_HANDLE h;
179692488a57SJung-uk Kim ACPI_OBJECT_TYPE t;
17975efca36fSTakanori Watanabe int rv;
1798ce619b2eSNate Lawson int i;
1799ce619b2eSNate Lawson
1800ce619b2eSNate Lawson h = acpi_get_handle(dev);
180192488a57SJung-uk Kim if (ids == NULL || h == NULL)
18025efca36fSTakanori Watanabe return (ENXIO);
180392488a57SJung-uk Kim t = acpi_get_type(dev);
180492488a57SJung-uk Kim if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
18055efca36fSTakanori Watanabe return (ENXIO);
1806ce619b2eSNate Lawson
1807ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */
1808ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) {
18095efca36fSTakanori Watanabe rv = acpi_MatchHid(h, ids[i]);
18105efca36fSTakanori Watanabe if (rv == ACPI_MATCHHID_NOMATCH)
18115efca36fSTakanori Watanabe continue;
18125efca36fSTakanori Watanabe
18135efca36fSTakanori Watanabe if (match != NULL) {
18145efca36fSTakanori Watanabe *match = ids[i];
1815ce619b2eSNate Lawson }
18165efca36fSTakanori Watanabe return ((rv == ACPI_MATCHHID_HID)?
18175efca36fSTakanori Watanabe BUS_PROBE_DEFAULT : BUS_PROBE_LOW_PRIORITY);
18185efca36fSTakanori Watanabe }
18195efca36fSTakanori Watanabe return (ENXIO);
1820ce619b2eSNate Lawson }
1821ce619b2eSNate Lawson
1822ce619b2eSNate Lawson static ACPI_STATUS
acpi_device_eval_obj(device_t bus,device_t dev,ACPI_STRING pathname,ACPI_OBJECT_LIST * parameters,ACPI_BUFFER * ret)1823ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1824ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1825ce619b2eSNate Lawson {
1826ce619b2eSNate Lawson ACPI_HANDLE h;
1827ce619b2eSNate Lawson
1828df8d2a32SNate Lawson if (dev == NULL)
1829df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT;
1830df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL)
1831ce619b2eSNate Lawson return (AE_BAD_PARAMETER);
1832ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret));
1833ce619b2eSNate Lawson }
1834ce619b2eSNate Lawson
1835b91fc6c4SBartlomiej Grzesik static ACPI_STATUS
acpi_device_get_prop(device_t bus,device_t dev,ACPI_STRING propname,const ACPI_OBJECT ** value)1836b91fc6c4SBartlomiej Grzesik acpi_device_get_prop(device_t bus, device_t dev, ACPI_STRING propname,
1837b91fc6c4SBartlomiej Grzesik const ACPI_OBJECT **value)
1838b91fc6c4SBartlomiej Grzesik {
1839b91fc6c4SBartlomiej Grzesik const ACPI_OBJECT *pkg, *name, *val;
1840b91fc6c4SBartlomiej Grzesik struct acpi_device *ad;
1841b91fc6c4SBartlomiej Grzesik ACPI_STATUS status;
1842b91fc6c4SBartlomiej Grzesik int i;
1843b91fc6c4SBartlomiej Grzesik
1844b91fc6c4SBartlomiej Grzesik ad = device_get_ivars(dev);
1845b91fc6c4SBartlomiej Grzesik
1846b91fc6c4SBartlomiej Grzesik if (ad == NULL || propname == NULL)
1847b91fc6c4SBartlomiej Grzesik return (AE_BAD_PARAMETER);
1848b91fc6c4SBartlomiej Grzesik if (ad->dsd_pkg == NULL) {
1849b91fc6c4SBartlomiej Grzesik if (ad->dsd.Pointer == NULL) {
1850945eaca1SBjoern A. Zeeb status = acpi_find_dsd(ad);
1851b91fc6c4SBartlomiej Grzesik if (ACPI_FAILURE(status))
1852b91fc6c4SBartlomiej Grzesik return (status);
1853b91fc6c4SBartlomiej Grzesik } else {
1854b91fc6c4SBartlomiej Grzesik return (AE_NOT_FOUND);
1855b91fc6c4SBartlomiej Grzesik }
1856b91fc6c4SBartlomiej Grzesik }
1857b91fc6c4SBartlomiej Grzesik
1858b91fc6c4SBartlomiej Grzesik for (i = 0; i < ad->dsd_pkg->Package.Count; i ++) {
1859b91fc6c4SBartlomiej Grzesik pkg = &ad->dsd_pkg->Package.Elements[i];
1860b91fc6c4SBartlomiej Grzesik if (pkg->Type != ACPI_TYPE_PACKAGE || pkg->Package.Count != 2)
1861b91fc6c4SBartlomiej Grzesik continue;
1862b91fc6c4SBartlomiej Grzesik
1863b91fc6c4SBartlomiej Grzesik name = &pkg->Package.Elements[0];
1864b91fc6c4SBartlomiej Grzesik val = &pkg->Package.Elements[1];
1865b91fc6c4SBartlomiej Grzesik if (name->Type != ACPI_TYPE_STRING)
1866b91fc6c4SBartlomiej Grzesik continue;
1867b91fc6c4SBartlomiej Grzesik if (strncmp(propname, name->String.Pointer, name->String.Length) == 0) {
1868b91fc6c4SBartlomiej Grzesik if (value != NULL)
1869b91fc6c4SBartlomiej Grzesik *value = val;
1870b91fc6c4SBartlomiej Grzesik
1871b91fc6c4SBartlomiej Grzesik return (AE_OK);
1872b91fc6c4SBartlomiej Grzesik }
1873b91fc6c4SBartlomiej Grzesik }
1874b91fc6c4SBartlomiej Grzesik
1875b91fc6c4SBartlomiej Grzesik return (AE_NOT_FOUND);
1876b91fc6c4SBartlomiej Grzesik }
1877b91fc6c4SBartlomiej Grzesik
1878b91fc6c4SBartlomiej Grzesik static ACPI_STATUS
acpi_find_dsd(struct acpi_device * ad)1879945eaca1SBjoern A. Zeeb acpi_find_dsd(struct acpi_device *ad)
1880b91fc6c4SBartlomiej Grzesik {
1881b91fc6c4SBartlomiej Grzesik const ACPI_OBJECT *dsd, *guid, *pkg;
1882b91fc6c4SBartlomiej Grzesik ACPI_STATUS status;
1883b91fc6c4SBartlomiej Grzesik
1884b91fc6c4SBartlomiej Grzesik ad->dsd.Length = ACPI_ALLOCATE_BUFFER;
1885b91fc6c4SBartlomiej Grzesik ad->dsd.Pointer = NULL;
1886b91fc6c4SBartlomiej Grzesik ad->dsd_pkg = NULL;
1887b91fc6c4SBartlomiej Grzesik
1888945eaca1SBjoern A. Zeeb status = AcpiEvaluateObject(ad->ad_handle, "_DSD", NULL, &ad->dsd);
1889b91fc6c4SBartlomiej Grzesik if (ACPI_FAILURE(status))
1890b91fc6c4SBartlomiej Grzesik return (status);
1891b91fc6c4SBartlomiej Grzesik
1892b91fc6c4SBartlomiej Grzesik dsd = ad->dsd.Pointer;
1893b91fc6c4SBartlomiej Grzesik guid = &dsd->Package.Elements[0];
1894b91fc6c4SBartlomiej Grzesik pkg = &dsd->Package.Elements[1];
1895b91fc6c4SBartlomiej Grzesik
1896b91fc6c4SBartlomiej Grzesik if (guid->Type != ACPI_TYPE_BUFFER || pkg->Type != ACPI_TYPE_PACKAGE ||
1897b91fc6c4SBartlomiej Grzesik guid->Buffer.Length != sizeof(acpi_dsd_uuid))
1898b91fc6c4SBartlomiej Grzesik return (AE_NOT_FOUND);
1899b91fc6c4SBartlomiej Grzesik if (memcmp(guid->Buffer.Pointer, &acpi_dsd_uuid,
1900b91fc6c4SBartlomiej Grzesik sizeof(acpi_dsd_uuid)) == 0) {
1901b91fc6c4SBartlomiej Grzesik
1902b91fc6c4SBartlomiej Grzesik ad->dsd_pkg = pkg;
1903b91fc6c4SBartlomiej Grzesik return (AE_OK);
1904b91fc6c4SBartlomiej Grzesik }
1905b91fc6c4SBartlomiej Grzesik
1906b91fc6c4SBartlomiej Grzesik return (AE_NOT_FOUND);
1907b91fc6c4SBartlomiej Grzesik }
1908b91fc6c4SBartlomiej Grzesik
19093f9a00e3SBartlomiej Grzesik static ssize_t
acpi_bus_get_prop_handle(const ACPI_OBJECT * hobj,void * propvalue,size_t size)191099e6980fSBjoern A. Zeeb acpi_bus_get_prop_handle(const ACPI_OBJECT *hobj, void *propvalue, size_t size)
191199e6980fSBjoern A. Zeeb {
191299e6980fSBjoern A. Zeeb ACPI_OBJECT *pobj;
191399e6980fSBjoern A. Zeeb ACPI_HANDLE h;
191499e6980fSBjoern A. Zeeb
191599e6980fSBjoern A. Zeeb if (hobj->Type != ACPI_TYPE_PACKAGE)
191699e6980fSBjoern A. Zeeb goto err;
191799e6980fSBjoern A. Zeeb if (hobj->Package.Count != 1)
191899e6980fSBjoern A. Zeeb goto err;
191999e6980fSBjoern A. Zeeb
192099e6980fSBjoern A. Zeeb pobj = &hobj->Package.Elements[0];
192199e6980fSBjoern A. Zeeb if (pobj == NULL)
192299e6980fSBjoern A. Zeeb goto err;
192399e6980fSBjoern A. Zeeb if (pobj->Type != ACPI_TYPE_LOCAL_REFERENCE)
192499e6980fSBjoern A. Zeeb goto err;
192599e6980fSBjoern A. Zeeb
192699e6980fSBjoern A. Zeeb h = acpi_GetReference(NULL, pobj);
192799e6980fSBjoern A. Zeeb if (h == NULL)
192899e6980fSBjoern A. Zeeb goto err;
192999e6980fSBjoern A. Zeeb
193099e6980fSBjoern A. Zeeb if (propvalue != NULL && size >= sizeof(ACPI_HANDLE))
193199e6980fSBjoern A. Zeeb *(ACPI_HANDLE *)propvalue = h;
193299e6980fSBjoern A. Zeeb return (sizeof(ACPI_HANDLE));
193399e6980fSBjoern A. Zeeb
193499e6980fSBjoern A. Zeeb err:
193599e6980fSBjoern A. Zeeb return (-1);
193699e6980fSBjoern A. Zeeb }
193799e6980fSBjoern A. Zeeb
193899e6980fSBjoern A. Zeeb static ssize_t
acpi_bus_get_prop(device_t bus,device_t child,const char * propname,void * propvalue,size_t size,device_property_type_t type)19393f9a00e3SBartlomiej Grzesik acpi_bus_get_prop(device_t bus, device_t child, const char *propname,
1940b344de4dSKornel Duleba void *propvalue, size_t size, device_property_type_t type)
19413f9a00e3SBartlomiej Grzesik {
19423f9a00e3SBartlomiej Grzesik ACPI_STATUS status;
19433f9a00e3SBartlomiej Grzesik const ACPI_OBJECT *obj;
19443f9a00e3SBartlomiej Grzesik
19453f9a00e3SBartlomiej Grzesik status = acpi_device_get_prop(bus, child, __DECONST(char *, propname),
19463f9a00e3SBartlomiej Grzesik &obj);
19473f9a00e3SBartlomiej Grzesik if (ACPI_FAILURE(status))
19483f9a00e3SBartlomiej Grzesik return (-1);
19493f9a00e3SBartlomiej Grzesik
1950b344de4dSKornel Duleba switch (type) {
1951b344de4dSKornel Duleba case DEVICE_PROP_ANY:
1952b344de4dSKornel Duleba case DEVICE_PROP_BUFFER:
1953b344de4dSKornel Duleba case DEVICE_PROP_UINT32:
1954b344de4dSKornel Duleba case DEVICE_PROP_UINT64:
1955b344de4dSKornel Duleba break;
195699e6980fSBjoern A. Zeeb case DEVICE_PROP_HANDLE:
195799e6980fSBjoern A. Zeeb return (acpi_bus_get_prop_handle(obj, propvalue, size));
1958b344de4dSKornel Duleba default:
1959b344de4dSKornel Duleba return (-1);
1960b344de4dSKornel Duleba }
1961b344de4dSKornel Duleba
19623f9a00e3SBartlomiej Grzesik switch (obj->Type) {
19633f9a00e3SBartlomiej Grzesik case ACPI_TYPE_INTEGER:
1964b344de4dSKornel Duleba if (type == DEVICE_PROP_UINT32) {
1965b344de4dSKornel Duleba if (propvalue != NULL && size >= sizeof(uint32_t))
1966b344de4dSKornel Duleba *((uint32_t *)propvalue) = obj->Integer.Value;
1967b344de4dSKornel Duleba return (sizeof(uint32_t));
1968b344de4dSKornel Duleba }
19693f9a00e3SBartlomiej Grzesik if (propvalue != NULL && size >= sizeof(uint64_t))
19703f9a00e3SBartlomiej Grzesik *((uint64_t *) propvalue) = obj->Integer.Value;
19713f9a00e3SBartlomiej Grzesik return (sizeof(uint64_t));
19723f9a00e3SBartlomiej Grzesik
19733f9a00e3SBartlomiej Grzesik case ACPI_TYPE_STRING:
1974b344de4dSKornel Duleba if (type != DEVICE_PROP_ANY &&
1975b344de4dSKornel Duleba type != DEVICE_PROP_BUFFER)
1976b344de4dSKornel Duleba return (-1);
1977b344de4dSKornel Duleba
19783f9a00e3SBartlomiej Grzesik if (propvalue != NULL && size > 0)
19793f9a00e3SBartlomiej Grzesik memcpy(propvalue, obj->String.Pointer,
19803f9a00e3SBartlomiej Grzesik MIN(size, obj->String.Length));
19813f9a00e3SBartlomiej Grzesik return (obj->String.Length);
19823f9a00e3SBartlomiej Grzesik
19833f9a00e3SBartlomiej Grzesik case ACPI_TYPE_BUFFER:
19843f9a00e3SBartlomiej Grzesik if (propvalue != NULL && size > 0)
19853f9a00e3SBartlomiej Grzesik memcpy(propvalue, obj->Buffer.Pointer,
19863f9a00e3SBartlomiej Grzesik MIN(size, obj->Buffer.Length));
19873f9a00e3SBartlomiej Grzesik return (obj->Buffer.Length);
19883f9a00e3SBartlomiej Grzesik
198999e6980fSBjoern A. Zeeb case ACPI_TYPE_PACKAGE:
199099e6980fSBjoern A. Zeeb if (propvalue != NULL && size >= sizeof(ACPI_OBJECT *)) {
199199e6980fSBjoern A. Zeeb *((ACPI_OBJECT **) propvalue) =
199299e6980fSBjoern A. Zeeb __DECONST(ACPI_OBJECT *, obj);
199399e6980fSBjoern A. Zeeb }
199499e6980fSBjoern A. Zeeb return (sizeof(ACPI_OBJECT *));
199599e6980fSBjoern A. Zeeb
199699e6980fSBjoern A. Zeeb case ACPI_TYPE_LOCAL_REFERENCE:
199799e6980fSBjoern A. Zeeb if (propvalue != NULL && size >= sizeof(ACPI_HANDLE)) {
199899e6980fSBjoern A. Zeeb ACPI_HANDLE h;
199999e6980fSBjoern A. Zeeb
200099e6980fSBjoern A. Zeeb h = acpi_GetReference(NULL,
200199e6980fSBjoern A. Zeeb __DECONST(ACPI_OBJECT *, obj));
200299e6980fSBjoern A. Zeeb memcpy(propvalue, h, sizeof(ACPI_HANDLE));
200399e6980fSBjoern A. Zeeb }
200499e6980fSBjoern A. Zeeb return (sizeof(ACPI_HANDLE));
2005d9ed1dccSBartlomiej Grzesik default:
2006d9ed1dccSBartlomiej Grzesik return (0);
2007d9ed1dccSBartlomiej Grzesik }
20083f9a00e3SBartlomiej Grzesik }
20093f9a00e3SBartlomiej Grzesik
201062508c53SJohn Baldwin int
acpi_device_pwr_for_sleep(device_t bus,device_t dev,int * dstate)201110ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
201210ce62b9SNate Lawson {
201310ce62b9SNate Lawson struct acpi_softc *sc;
201410ce62b9SNate Lawson ACPI_HANDLE handle;
201510ce62b9SNate Lawson ACPI_STATUS status;
201610ce62b9SNate Lawson char sxd[8];
201710ce62b9SNate Lawson
201810ce62b9SNate Lawson handle = acpi_get_handle(dev);
201910ce62b9SNate Lawson
202010ce62b9SNate Lawson /*
202110ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down.
202210ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when
202310ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may
202410ce62b9SNate Lawson * need special handling in their drivers.
202510ce62b9SNate Lawson */
2026a7a3177fSJung-uk Kim if (dstate == NULL || handle == NULL ||
202710ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") ||
202810ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") ||
202910ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") ||
203010ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") ||
203110ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511"))
203210ce62b9SNate Lawson return (ENXIO);
203310ce62b9SNate Lawson
203410ce62b9SNate Lawson /*
2035a7a3177fSJung-uk Kim * Override next state with the value from _SxD, if present.
2036a7a3177fSJung-uk Kim * Note illegal _S0D is evaluated because some systems expect this.
203710ce62b9SNate Lawson */
2038a7a3177fSJung-uk Kim sc = device_get_softc(bus);
203910ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
204010ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate);
2041a7a3177fSJung-uk Kim if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
2042a7a3177fSJung-uk Kim device_printf(dev, "failed to get %s on %s: %s\n", sxd,
2043a7a3177fSJung-uk Kim acpi_name(handle), AcpiFormatException(status));
2044a7a3177fSJung-uk Kim return (ENXIO);
204510ce62b9SNate Lawson }
204610ce62b9SNate Lawson
2047a7a3177fSJung-uk Kim return (0);
204810ce62b9SNate Lawson }
204910ce62b9SNate Lawson
2050df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
2051df8d2a32SNate Lawson struct acpi_device_scan_ctx {
2052df8d2a32SNate Lawson acpi_scan_cb_t user_fn;
2053df8d2a32SNate Lawson void *arg;
2054df8d2a32SNate Lawson ACPI_HANDLE parent;
2055df8d2a32SNate Lawson };
2056df8d2a32SNate Lawson
2057ce619b2eSNate Lawson static ACPI_STATUS
acpi_device_scan_cb(ACPI_HANDLE h,UINT32 level,void * arg,void ** retval)2058df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
2059df8d2a32SNate Lawson {
2060df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx;
2061df8d2a32SNate Lawson device_t dev, old_dev;
2062df8d2a32SNate Lawson ACPI_STATUS status;
2063df8d2a32SNate Lawson ACPI_OBJECT_TYPE type;
2064df8d2a32SNate Lawson
2065df8d2a32SNate Lawson /*
2066df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is
2067df8d2a32SNate Lawson * the parent where the scan began.
2068df8d2a32SNate Lawson */
2069df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg;
2070df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent)
2071df8d2a32SNate Lawson return (AE_OK);
2072df8d2a32SNate Lawson
2073df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */
2074df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type)))
2075df8d2a32SNate Lawson return (AE_OK);
2076df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
2077df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
2078df8d2a32SNate Lawson return (AE_OK);
2079df8d2a32SNate Lawson
2080df8d2a32SNate Lawson /*
2081df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged
2082df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev.
2083df8d2a32SNate Lawson */
2084df8d2a32SNate Lawson old_dev = acpi_get_device(h);
2085df8d2a32SNate Lawson dev = old_dev;
2086df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg);
2087df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev)
2088df8d2a32SNate Lawson return (status);
2089df8d2a32SNate Lawson
2090df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */
2091709749aaSVladimir Kondratyev if (old_dev != NULL)
2092df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev);
2093df8d2a32SNate Lawson
2094df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */
2095df8d2a32SNate Lawson if (dev != NULL)
2096df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev);
2097df8d2a32SNate Lawson
2098df8d2a32SNate Lawson return (AE_OK);
2099df8d2a32SNate Lawson }
2100df8d2a32SNate Lawson
2101df8d2a32SNate Lawson static ACPI_STATUS
acpi_device_scan_children(device_t bus,device_t dev,int max_depth,acpi_scan_cb_t user_fn,void * arg)2102df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
2103df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg)
2104ce619b2eSNate Lawson {
2105ce619b2eSNate Lawson ACPI_HANDLE h;
2106df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx;
2107ce619b2eSNate Lawson
2108df8d2a32SNate Lawson if (acpi_disabled("children"))
2109df8d2a32SNate Lawson return (AE_OK);
2110df8d2a32SNate Lawson
2111df8d2a32SNate Lawson if (dev == NULL)
2112df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT;
2113df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL)
2114ce619b2eSNate Lawson return (AE_BAD_PARAMETER);
2115df8d2a32SNate Lawson ctx.user_fn = user_fn;
2116df8d2a32SNate Lawson ctx.arg = arg;
2117df8d2a32SNate Lawson ctx.parent = h;
2118df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
21192272d050SJung-uk Kim acpi_device_scan_cb, NULL, &ctx, NULL));
2120ce619b2eSNate Lawson }
2121ce619b2eSNate Lawson
212210ce62b9SNate Lawson /*
212310ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting
212410ce62b9SNate Lawson * device power states since it's close enough to ACPI.
212510ce62b9SNate Lawson */
2126ddf8c230SVladimir Kondratyev int
acpi_set_powerstate(device_t child,int state)2127a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state)
212810ce62b9SNate Lawson {
212910ce62b9SNate Lawson ACPI_HANDLE h;
213010ce62b9SNate Lawson ACPI_STATUS status;
213110ce62b9SNate Lawson
213210ce62b9SNate Lawson h = acpi_get_handle(child);
2133a0e73a12SJung-uk Kim if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
213410ce62b9SNate Lawson return (EINVAL);
213510ce62b9SNate Lawson if (h == NULL)
213610ce62b9SNate Lawson return (0);
213710ce62b9SNate Lawson
213810ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */
213910ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state);
2140a7a3177fSJung-uk Kim if (ACPI_SUCCESS(status)) {
2141a7a3177fSJung-uk Kim if (bootverbose)
2142a7a3177fSJung-uk Kim device_printf(child, "set ACPI power state D%d on %s\n",
2143a7a3177fSJung-uk Kim state, acpi_name(h));
2144a7a3177fSJung-uk Kim } else if (status != AE_NOT_FOUND)
2145a7a3177fSJung-uk Kim device_printf(child,
2146a7a3177fSJung-uk Kim "failed to set ACPI power state D%d on %s: %s\n", state,
2147a7a3177fSJung-uk Kim acpi_name(h), AcpiFormatException(status));
214810ce62b9SNate Lawson
2149a7a3177fSJung-uk Kim return (0);
215010ce62b9SNate Lawson }
215110ce62b9SNate Lawson
215232d18aa5SMike Smith static int
acpi_isa_pnp_probe(device_t bus,device_t child,struct isa_pnp_id * ids)215332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
215432d18aa5SMike Smith {
21551e4925e8SNate Lawson int result, cid_count, i;
21561e4925e8SNate Lawson uint32_t lid, cids[8];
2157bc0f2195SMike Smith
2158b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2159bc0f2195SMike Smith
2160bc0f2195SMike Smith /*
2161bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and
2162bc0f2195SMike Smith * probe manually if we return ENOENT. We never want
2163bc0f2195SMike Smith * that to happen, so don't ever return it.
2164bc0f2195SMike Smith */
2165bc0f2195SMike Smith result = ENXIO;
2166bc0f2195SMike Smith
2167be2b1797SNate Lawson /* Scan the supplied IDs for a match */
2168b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child);
21691e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8);
2170bc0f2195SMike Smith while (ids && ids->ip_id) {
21711e4925e8SNate Lawson if (lid == ids->ip_id) {
2172bc0f2195SMike Smith result = 0;
2173bc0f2195SMike Smith goto out;
2174bc0f2195SMike Smith }
21751e4925e8SNate Lawson for (i = 0; i < cid_count; i++) {
21761e4925e8SNate Lawson if (cids[i] == ids->ip_id) {
21771e4925e8SNate Lawson result = 0;
21781e4925e8SNate Lawson goto out;
21791e4925e8SNate Lawson }
21801e4925e8SNate Lawson }
2181bc0f2195SMike Smith ids++;
2182bc0f2195SMike Smith }
2183be2b1797SNate Lawson
2184bc0f2195SMike Smith out:
21859e0dd54fSNate Lawson if (result == 0 && ids->ip_desc)
21869e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc);
21879e0dd54fSNate Lawson
2188bc0f2195SMike Smith return_VALUE (result);
2189bc0f2195SMike Smith }
2190bc0f2195SMike Smith
2191d320e05cSJohn Baldwin /*
2192d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for
2193d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory
2194d320e05cSJohn Baldwin * map.
2195185c34f7SJayachandran C. *
2196185c34f7SJayachandran C. * On non-x86 architectures (arm64 for now), this will be done from the
2197185c34f7SJayachandran C. * PCI host bridge driver.
2198d320e05cSJohn Baldwin */
2199d320e05cSJohn Baldwin static void
acpi_enable_pcie(void)2200d320e05cSJohn Baldwin acpi_enable_pcie(void)
2201d320e05cSJohn Baldwin {
2202185c34f7SJayachandran C. #if defined(__i386__) || defined(__amd64__)
2203d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr;
2204d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end;
2205d320e05cSJohn Baldwin ACPI_STATUS status;
2206d320e05cSJohn Baldwin
2207d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
2208d320e05cSJohn Baldwin if (ACPI_FAILURE(status))
2209d320e05cSJohn Baldwin return;
2210d320e05cSJohn Baldwin
2211d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
2212d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
2213d320e05cSJohn Baldwin while (alloc < end) {
2214f54a3890SJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->PciSegment,
2215f54a3890SJohn Baldwin alloc->StartBusNumber, alloc->EndBusNumber);
2216d320e05cSJohn Baldwin alloc++;
2217d320e05cSJohn Baldwin }
2218d320e05cSJohn Baldwin #endif
2219185c34f7SJayachandran C. }
2220d320e05cSJohn Baldwin
2221855e49f3SAlexander Motin static void
acpi_platform_osc(device_t dev)2222855e49f3SAlexander Motin acpi_platform_osc(device_t dev)
2223855e49f3SAlexander Motin {
2224855e49f3SAlexander Motin ACPI_HANDLE sb_handle;
2225855e49f3SAlexander Motin ACPI_STATUS status;
2226855e49f3SAlexander Motin uint32_t cap_set[2];
2227855e49f3SAlexander Motin
2228855e49f3SAlexander Motin /* 0811B06E-4A27-44F9-8D60-3CBBC22E7B48 */
2229855e49f3SAlexander Motin static uint8_t acpi_platform_uuid[ACPI_UUID_LENGTH] = {
2230855e49f3SAlexander Motin 0x6e, 0xb0, 0x11, 0x08, 0x27, 0x4a, 0xf9, 0x44,
2231855e49f3SAlexander Motin 0x8d, 0x60, 0x3c, 0xbb, 0xc2, 0x2e, 0x7b, 0x48
2232855e49f3SAlexander Motin };
2233855e49f3SAlexander Motin
2234855e49f3SAlexander Motin if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2235855e49f3SAlexander Motin return;
2236855e49f3SAlexander Motin
2237855e49f3SAlexander Motin cap_set[1] = 0x10; /* APEI Support */
2238855e49f3SAlexander Motin status = acpi_EvaluateOSC(sb_handle, acpi_platform_uuid, 1,
2239855e49f3SAlexander Motin nitems(cap_set), cap_set, cap_set, false);
2240855e49f3SAlexander Motin if (ACPI_FAILURE(status)) {
2241855e49f3SAlexander Motin if (status == AE_NOT_FOUND)
2242855e49f3SAlexander Motin return;
2243855e49f3SAlexander Motin device_printf(dev, "_OSC failed: %s\n",
2244855e49f3SAlexander Motin AcpiFormatException(status));
2245855e49f3SAlexander Motin return;
2246855e49f3SAlexander Motin }
2247855e49f3SAlexander Motin }
2248855e49f3SAlexander Motin
2249bc0f2195SMike Smith /*
225033332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices.
225115e32d5dSMike Smith *
225233332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
225333332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
225433332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link
225533332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the
225633332dc2SNate Lawson * type of namespace nodes, so this should be ok.
225715e32d5dSMike Smith */
225815e32d5dSMike Smith static void
acpi_probe_children(device_t bus)225915e32d5dSMike Smith acpi_probe_children(device_t bus)
226015e32d5dSMike Smith {
226115e32d5dSMike Smith
2262b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
22630ae55423SMike Smith
226415e32d5dSMike Smith /*
226515e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that
2266edc13633SNate Lawson * we find. We also probe/attach any early devices.
226715e32d5dSMike Smith *
226815e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
2269be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are
2270be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove
2271be2b1797SNate Lawson * devices as they appear, which might be smarter.)
227215e32d5dSMike Smith */
22734c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
227433332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
22752272d050SJung-uk Kim NULL, bus, NULL);
227615e32d5dSMike Smith
2277adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */
2278adad4744SNate Lawson acpi_sysres_alloc(bus);
2279adad4744SNate Lawson
2280edc13633SNate Lawson /* Create any static children by calling device identify methods. */
2281edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
2282723da5d9SJohn Baldwin bus_identify_children(bus);
2283edc13633SNate Lawson
22845e66b8e2SJohn Baldwin /* Probe/attach all children, created statically and from the namespace. */
2285*18250ec6SJohn Baldwin ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_attach_children\n"));
2286*18250ec6SJohn Baldwin bus_attach_children(bus);
22870ae55423SMike Smith
2288f2fcb680SJohn Baldwin /*
2289f2fcb680SJohn Baldwin * Reserve resources allocated to children but not yet allocated
2290f2fcb680SJohn Baldwin * by a driver.
2291f2fcb680SJohn Baldwin */
2292f2fcb680SJohn Baldwin acpi_reserve_resources(bus);
2293f2fcb680SJohn Baldwin
229488a79fc0SNate Lawson /* Attach wake sysctls. */
22955c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus);
229688a79fc0SNate Lawson
2297bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
22980ae55423SMike Smith return_VOID;
229915e32d5dSMike Smith }
230015e32d5dSMike Smith
2301b82ca9a9SNate Lawson /*
2302d227204dSJohn Baldwin * Determine the probe order for a given device.
2303b82ca9a9SNate Lawson */
2304d227204dSJohn Baldwin static void
acpi_probe_order(ACPI_HANDLE handle,int * order)2305b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
230691233413SNate Lawson {
2307463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type;
230891233413SNate Lawson
2309b82ca9a9SNate Lawson /*
2310404b0d10SJung-uk Kim * 0. CPUs
2311404b0d10SJung-uk Kim * 1. I/O port and memory system resource holders
2312404b0d10SJung-uk Kim * 2. Clocks and timers (to handle early accesses)
2313b1f9b996SAndriy Gapon * 3. Embedded controllers (to handle early accesses)
2314b1f9b996SAndriy Gapon * 4. PCI Link Devices
2315b82ca9a9SNate Lawson */
2316463e0f91SJohn Baldwin AcpiGetType(handle, &type);
2317aa835160SAndriy Gapon if (type == ACPI_TYPE_PROCESSOR)
2318404b0d10SJung-uk Kim *order = 0;
2319404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0C01") ||
2320404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0C02"))
232191233413SNate Lawson *order = 1;
2322404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0100") ||
2323404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0103") ||
2324404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0B00"))
232591233413SNate Lawson *order = 2;
2326aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C09"))
2327b460c6f8SJohn Baldwin *order = 3;
2328aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C0F"))
2329aa835160SAndriy Gapon *order = 4;
233091233413SNate Lawson }
233191233413SNate Lawson
233215e32d5dSMike Smith /*
233315e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to
233415e32d5dSMike Smith * it.
233515e32d5dSMike Smith */
233615e32d5dSMike Smith static ACPI_STATUS
acpi_probe_child(ACPI_HANDLE handle,UINT32 level,void * context,void ** status)233715e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
233815e32d5dSMike Smith {
23390e68afe5SAndrew Turner ACPI_DEVICE_INFO *devinfo;
23400e68afe5SAndrew Turner struct acpi_device *ad;
23415a77b11bSJung-uk Kim struct acpi_prw_data prw;
234215e32d5dSMike Smith ACPI_OBJECT_TYPE type;
2343858a52f4SMitsuru IWASAKI ACPI_HANDLE h;
234433332dc2SNate Lawson device_t bus, child;
2345495a4144SJung-uk Kim char *handle_str;
23467dd1f0dcSKonstantin Belousov int d, order;
234715e32d5dSMike Smith
2348b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
23490ae55423SMike Smith
2350495a4144SJung-uk Kim if (acpi_disabled("children"))
2351495a4144SJung-uk Kim return_ACPI_STATUS (AE_OK);
2352495a4144SJung-uk Kim
2353be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */
23540ae55423SMike Smith if (acpi_avoid(handle))
23550ae55423SMike Smith return_ACPI_STATUS (AE_OK);
23560ae55423SMike Smith
235791233413SNate Lawson bus = (device_t)context;
2358b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
2359495a4144SJung-uk Kim handle_str = acpi_name(handle);
236015e32d5dSMike Smith switch (type) {
236115e32d5dSMike Smith case ACPI_TYPE_DEVICE:
2362495a4144SJung-uk Kim /*
2363495a4144SJung-uk Kim * Since we scan from \, be sure to skip system scope objects.
2364495a4144SJung-uk Kim * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
2365affa1826SJung-uk Kim * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run
2366453130d9SPedro F. Giffuni * during the initialization and \_TZ_ is to support Notify() on it.
2367495a4144SJung-uk Kim */
2368495a4144SJung-uk Kim if (strcmp(handle_str, "\\_SB_") == 0 ||
2369495a4144SJung-uk Kim strcmp(handle_str, "\\_TZ_") == 0)
2370495a4144SJung-uk Kim break;
23715a77b11bSJung-uk Kim if (acpi_parse_prw(handle, &prw) == 0)
23725a77b11bSJung-uk Kim AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
2373183c8af3SJohn Baldwin
2374183c8af3SJohn Baldwin /*
2375183c8af3SJohn Baldwin * Ignore devices that do not have a _HID or _CID. They should
2376183c8af3SJohn Baldwin * be discovered by other buses (e.g. the PCI bus driver).
2377183c8af3SJohn Baldwin */
2378183c8af3SJohn Baldwin if (!acpi_has_hid(handle))
2379183c8af3SJohn Baldwin break;
2380495a4144SJung-uk Kim /* FALLTHROUGH */
238115e32d5dSMike Smith case ACPI_TYPE_PROCESSOR:
238215e32d5dSMike Smith case ACPI_TYPE_THERMAL:
238315e32d5dSMike Smith case ACPI_TYPE_POWER:
238433332dc2SNate Lawson /*
2385463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the
2386463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run
2387463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER
2388463e0f91SJohn Baldwin * are reserved for special objects (i.e., system
2389b1f9b996SAndriy Gapon * resources).
239015e32d5dSMike Smith */
239133332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
2392404b0d10SJung-uk Kim order = level * 10 + ACPI_DEV_BASE_ORDER;
2393da72d149SNate Lawson acpi_probe_order(handle, &order);
2394a05a6804SWarner Losh child = BUS_ADD_CHILD(bus, order, NULL, DEVICE_UNIT_ANY);
2395bc0f2195SMike Smith if (child == NULL)
2396bc0f2195SMike Smith break;
239759a890e6SNate Lawson
239859a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */
239915e32d5dSMike Smith acpi_set_handle(child, handle);
240059a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child);
2401bc0f2195SMike Smith
2402bc0f2195SMike Smith /*
2403b519f9d6SMike Smith * Check that the device is present. If it's not present,
2404b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to
2405b519f9d6SMike Smith * the handle, but we don't probe it).
2406893f750aSNate Lawson *
2407893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not
2408893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them
2409893f750aSNate Lawson * anyway since we may enable them later.
2410b519f9d6SMike Smith */
2411858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
2412858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */
2413858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F"))
2414858a52f4SMitsuru IWASAKI break;
24155c69be70STakanori Watanabe
24165c69be70STakanori Watanabe /*
24175c69be70STakanori Watanabe * RTC Device should be enabled for CMOS register space
24185c69be70STakanori Watanabe * unless FADT indicate it is not present.
24195c69be70STakanori Watanabe * (checked in RTC probe routine.)
24205c69be70STakanori Watanabe */
24215c69be70STakanori Watanabe if (acpi_MatchHid(handle, "PNP0B00"))
24225c69be70STakanori Watanabe break;
24235c69be70STakanori Watanabe
2424858a52f4SMitsuru IWASAKI /*
2425858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system
2426858a52f4SMitsuru IWASAKI * may be undocked at boot.
2427858a52f4SMitsuru IWASAKI */
2428858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
2429858a52f4SMitsuru IWASAKI break;
2430858a52f4SMitsuru IWASAKI
2431b519f9d6SMike Smith device_disable(child);
2432b519f9d6SMike Smith break;
2433b519f9d6SMike Smith }
2434b519f9d6SMike Smith
2435b519f9d6SMike Smith /*
2436bc0f2195SMike Smith * Get the device's resource settings and attach them.
2437bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need
2438bc0f2195SMike Smith * to decide when it's appropriate to try to configure the
2439bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the
2440bc0f2195SMike Smith * device not to have any resources.
2441bc0f2195SMike Smith */
244272ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
24430e68afe5SAndrew Turner
24440e68afe5SAndrew Turner ad = device_get_ivars(child);
24450e68afe5SAndrew Turner ad->ad_cls_class = 0xffffff;
24460e68afe5SAndrew Turner if (ACPI_SUCCESS(AcpiGetObjectInfo(handle, &devinfo))) {
24470e68afe5SAndrew Turner if ((devinfo->Valid & ACPI_VALID_CLS) != 0 &&
24480e68afe5SAndrew Turner devinfo->ClassCode.Length >= ACPI_PCICLS_STRING_SIZE) {
24490e68afe5SAndrew Turner ad->ad_cls_class = strtoul(devinfo->ClassCode.String,
24500e68afe5SAndrew Turner NULL, 16);
24510e68afe5SAndrew Turner }
24520e68afe5SAndrew Turner AcpiOsFree(devinfo);
24530e68afe5SAndrew Turner }
24547dd1f0dcSKonstantin Belousov
24557dd1f0dcSKonstantin Belousov d = acpi_pxm_parse(child);
24567dd1f0dcSKonstantin Belousov if (d >= 0)
24577dd1f0dcSKonstantin Belousov ad->ad_domain = d;
24580ae55423SMike Smith break;
245915e32d5dSMike Smith }
246015e32d5dSMike Smith }
2461be2b1797SNate Lawson
24620ae55423SMike Smith return_ACPI_STATUS (AE_OK);
246315e32d5dSMike Smith }
246415e32d5dSMike Smith
246559a890e6SNate Lawson /*
246659a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a
246759a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
246859a890e6SNate Lawson */
246959a890e6SNate Lawson void
acpi_fake_objhandler(ACPI_HANDLE h,void * data)247092488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data)
247159a890e6SNate Lawson {
247259a890e6SNate Lawson }
247359a890e6SNate Lawson
247415e32d5dSMike Smith static void
acpi_shutdown_final(void * arg,int howto)247515e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
247615e32d5dSMike Smith {
24772d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg;
247871804adcSJung-uk Kim register_t intr;
2479d33f4987STakanori Watanabe ACPI_STATUS status;
2480eeb3a05fSNate Lawson
2481eeb3a05fSNate Lawson /*
248280f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0).
248380f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from
248480f0e4c2SNate Lawson * an AP.
2485eeb3a05fSNate Lawson */
2486eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) {
2487904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
2488904bf0c2SNate Lawson if (ACPI_FAILURE(status)) {
24892d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2490904bf0c2SNate Lawson AcpiFormatException(status));
2491904bf0c2SNate Lawson return;
2492904bf0c2SNate Lawson }
24932d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Powering system off\n");
249471804adcSJung-uk Kim intr = intr_disable();
24951df130f1SJung-uk Kim status = AcpiEnterSleepState(ACPI_STATE_S5);
249671804adcSJung-uk Kim if (ACPI_FAILURE(status)) {
249771804adcSJung-uk Kim intr_restore(intr);
24982d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - %s\n",
24992d0c82e8SJung-uk Kim AcpiFormatException(status));
250071804adcSJung-uk Kim } else {
250115e32d5dSMike Smith DELAY(1000000);
250271804adcSJung-uk Kim intr_restore(intr);
25032d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - timeout\n");
250415e32d5dSMike Smith }
2505ac731af5SJung-uk Kim } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
2506d85e6785SNate Lawson /* Reboot using the reset register. */
2507ac731af5SJung-uk Kim status = AcpiReset();
2508ac731af5SJung-uk Kim if (ACPI_SUCCESS(status)) {
250987a500cdSNate Lawson DELAY(1000000);
25102d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - timeout\n");
2511ac731af5SJung-uk Kim } else if (status != AE_NOT_EXIST)
2512ac731af5SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - %s\n",
2513ac731af5SJung-uk Kim AcpiFormatException(status));
2514879e0604SMateusz Guzik } else if (sc->acpi_do_disable && !KERNEL_PANICKED()) {
2515d85e6785SNate Lawson /*
2516d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing
2517d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system.
2518d85e6785SNate Lawson */
25192d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Shutting down\n");
252080f0e4c2SNate Lawson AcpiTerminate();
252180f0e4c2SNate Lawson }
252215e32d5dSMike Smith }
252315e32d5dSMike Smith
252413d4f7baSMitsuru IWASAKI static void
acpi_enable_fixed_events(struct acpi_softc * sc)252513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
252613d4f7baSMitsuru IWASAKI {
252713d4f7baSMitsuru IWASAKI static int first_time = 1;
252813d4f7baSMitsuru IWASAKI
252913d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */
25302be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
253159ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
253213d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
253333febf93SNate Lawson acpi_event_power_button_sleep, sc);
2534be2b1797SNate Lawson if (first_time)
25356c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n");
253613d4f7baSMitsuru IWASAKI }
25372be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
253859ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
253913d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
254033febf93SNate Lawson acpi_event_sleep_button_sleep, sc);
2541be2b1797SNate Lawson if (first_time)
25426c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
254313d4f7baSMitsuru IWASAKI }
254413d4f7baSMitsuru IWASAKI
254513d4f7baSMitsuru IWASAKI first_time = 0;
254613d4f7baSMitsuru IWASAKI }
254713d4f7baSMitsuru IWASAKI
254815e32d5dSMike Smith /*
2549c5ba0be4SMike Smith * Returns true if the device is actually present and should
2550c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible
2551c5ba0be4SMike Smith * and diagnostics-passed bits to be set.
2552c5ba0be4SMike Smith */
2553c5ba0be4SMike Smith BOOLEAN
acpi_DeviceIsPresent(device_t dev)2554c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
2555c5ba0be4SMike Smith {
2556c5ba0be4SMike Smith ACPI_HANDLE h;
25578438a7a8SJung-uk Kim UINT32 s;
25588438a7a8SJung-uk Kim ACPI_STATUS status;
2559c5ba0be4SMike Smith
25608438a7a8SJung-uk Kim h = acpi_get_handle(dev);
25618438a7a8SJung-uk Kim if (h == NULL)
2562c5ba0be4SMike Smith return (FALSE);
256335af9331SWarner Losh
256435af9331SWarner Losh #ifdef ACPI_EARLY_EPYC_WAR
2565381388b9SMatt Macy /*
2566bf15eaf2SWarner Losh * Certain Treadripper boards always returns 0 for FreeBSD because it
2567bf15eaf2SWarner Losh * only returns non-zero for the OS string "Windows 2015". Otherwise it
2568bf15eaf2SWarner Losh * will return zero. Force them to always be treated as present.
2569bf15eaf2SWarner Losh * Beata versions were worse: they always returned 0.
2570381388b9SMatt Macy */
2571381388b9SMatt Macy if (acpi_MatchHid(h, "AMDI0020") || acpi_MatchHid(h, "AMDI0010"))
2572381388b9SMatt Macy return (TRUE);
257335af9331SWarner Losh #endif
2574381388b9SMatt Macy
2575817b71bbSAndriy Gapon status = acpi_GetInteger(h, "_STA", &s);
2576817b71bbSAndriy Gapon
2577817b71bbSAndriy Gapon /*
2578817b71bbSAndriy Gapon * If no _STA method or if it failed, then assume that
2579817b71bbSAndriy Gapon * the device is present.
2580817b71bbSAndriy Gapon */
25818438a7a8SJung-uk Kim if (ACPI_FAILURE(status))
2582817b71bbSAndriy Gapon return (TRUE);
2583be2b1797SNate Lawson
25848438a7a8SJung-uk Kim return (ACPI_DEVICE_PRESENT(s) ? TRUE : FALSE);
2585c5ba0be4SMike Smith }
2586c5ba0be4SMike Smith
2587c5ba0be4SMike Smith /*
2588b53f2771SMike Smith * Returns true if the battery is actually present and inserted.
2589b53f2771SMike Smith */
2590b53f2771SMike Smith BOOLEAN
acpi_BatteryIsPresent(device_t dev)2591b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
2592b53f2771SMike Smith {
2593b53f2771SMike Smith ACPI_HANDLE h;
25948438a7a8SJung-uk Kim UINT32 s;
25958438a7a8SJung-uk Kim ACPI_STATUS status;
2596b53f2771SMike Smith
25978438a7a8SJung-uk Kim h = acpi_get_handle(dev);
25988438a7a8SJung-uk Kim if (h == NULL)
2599b53f2771SMike Smith return (FALSE);
26008438a7a8SJung-uk Kim status = acpi_GetInteger(h, "_STA", &s);
2601be2b1797SNate Lawson
2602817b71bbSAndriy Gapon /*
2603817b71bbSAndriy Gapon * If no _STA method or if it failed, then assume that
2604817b71bbSAndriy Gapon * the device is present.
2605817b71bbSAndriy Gapon */
26068438a7a8SJung-uk Kim if (ACPI_FAILURE(status))
2607817b71bbSAndriy Gapon return (TRUE);
2608be2b1797SNate Lawson
26098438a7a8SJung-uk Kim return (ACPI_BATTERY_PRESENT(s) ? TRUE : FALSE);
2610b53f2771SMike Smith }
2611b53f2771SMike Smith
2612b53f2771SMike Smith /*
2613183c8af3SJohn Baldwin * Returns true if a device has at least one valid device ID.
2614183c8af3SJohn Baldwin */
2615ddf8c230SVladimir Kondratyev BOOLEAN
acpi_has_hid(ACPI_HANDLE h)2616183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h)
2617183c8af3SJohn Baldwin {
2618183c8af3SJohn Baldwin ACPI_DEVICE_INFO *devinfo;
2619183c8af3SJohn Baldwin BOOLEAN ret;
2620183c8af3SJohn Baldwin
2621183c8af3SJohn Baldwin if (h == NULL ||
2622183c8af3SJohn Baldwin ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2623183c8af3SJohn Baldwin return (FALSE);
2624183c8af3SJohn Baldwin
2625183c8af3SJohn Baldwin ret = FALSE;
2626183c8af3SJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2627183c8af3SJohn Baldwin ret = TRUE;
2628183c8af3SJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2629183c8af3SJohn Baldwin if (devinfo->CompatibleIdList.Count > 0)
2630183c8af3SJohn Baldwin ret = TRUE;
2631183c8af3SJohn Baldwin
2632183c8af3SJohn Baldwin AcpiOsFree(devinfo);
2633183c8af3SJohn Baldwin return (ret);
2634183c8af3SJohn Baldwin }
2635183c8af3SJohn Baldwin
2636183c8af3SJohn Baldwin /*
263791233413SNate Lawson * Match a HID string against a handle
26385efca36fSTakanori Watanabe * returns ACPI_MATCHHID_HID if _HID match
26395efca36fSTakanori Watanabe * ACPI_MATCHHID_CID if _CID match and not _HID match.
26405efca36fSTakanori Watanabe * ACPI_MATCHHID_NOMATCH=0 if no match.
264115e32d5dSMike Smith */
26425efca36fSTakanori Watanabe int
acpi_MatchHid(ACPI_HANDLE h,const char * hid)2643ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
264415e32d5dSMike Smith {
26451e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo;
264692488a57SJung-uk Kim BOOLEAN ret;
264792488a57SJung-uk Kim int i;
264892488a57SJung-uk Kim
264992488a57SJung-uk Kim if (hid == NULL || h == NULL ||
265092488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
26515efca36fSTakanori Watanabe return (ACPI_MATCHHID_NOMATCH);
265215e32d5dSMike Smith
26536d29ba58SAndriy Gapon ret = ACPI_MATCHHID_NOMATCH;
2654c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
265592488a57SJung-uk Kim strcmp(hid, devinfo->HardwareId.String) == 0)
26565efca36fSTakanori Watanabe ret = ACPI_MATCHHID_HID;
265792488a57SJung-uk Kim else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
265892488a57SJung-uk Kim for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
265992488a57SJung-uk Kim if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
26605efca36fSTakanori Watanabe ret = ACPI_MATCHHID_CID;
26611e4925e8SNate Lawson break;
26621e4925e8SNate Lawson }
26631e4925e8SNate Lawson }
2664be2b1797SNate Lawson
266592488a57SJung-uk Kim AcpiOsFree(devinfo);
26661e4925e8SNate Lawson return (ret);
266715e32d5dSMike Smith }
266815e32d5dSMike Smith
266915e32d5dSMike Smith /*
2670c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent)
2671c5ba0be4SMike Smith * or one if its parents.
2672c5ba0be4SMike Smith */
2673c5ba0be4SMike Smith ACPI_STATUS
acpi_GetHandleInScope(ACPI_HANDLE parent,char * path,ACPI_HANDLE * result)2674c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2675c5ba0be4SMike Smith {
2676c5ba0be4SMike Smith ACPI_HANDLE r;
2677c5ba0be4SMike Smith ACPI_STATUS status;
2678c5ba0be4SMike Smith
2679be2b1797SNate Lawson /* Walk back up the tree to the root */
2680c5ba0be4SMike Smith for (;;) {
2681be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r);
2682be2b1797SNate Lawson if (ACPI_SUCCESS(status)) {
2683c5ba0be4SMike Smith *result = r;
2684c5ba0be4SMike Smith return (AE_OK);
2685c5ba0be4SMike Smith }
2686bee4aa4aSNate Lawson /* XXX Return error here? */
2687c5ba0be4SMike Smith if (status != AE_NOT_FOUND)
2688c5ba0be4SMike Smith return (AE_OK);
2689b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2690c5ba0be4SMike Smith return (AE_NOT_FOUND);
2691c5ba0be4SMike Smith parent = r;
2692c5ba0be4SMike Smith }
2693c5ba0be4SMike Smith }
2694c5ba0be4SMike Smith
2695b91fc6c4SBartlomiej Grzesik ACPI_STATUS
acpi_GetProperty(device_t dev,ACPI_STRING propname,const ACPI_OBJECT ** value)2696b91fc6c4SBartlomiej Grzesik acpi_GetProperty(device_t dev, ACPI_STRING propname,
2697b91fc6c4SBartlomiej Grzesik const ACPI_OBJECT **value)
2698b91fc6c4SBartlomiej Grzesik {
2699b91fc6c4SBartlomiej Grzesik device_t bus = device_get_parent(dev);
2700b91fc6c4SBartlomiej Grzesik
2701b91fc6c4SBartlomiej Grzesik return (ACPI_GET_PROPERTY(bus, dev, propname, value));
2702b91fc6c4SBartlomiej Grzesik }
2703b91fc6c4SBartlomiej Grzesik
2704c5ba0be4SMike Smith /*
2705c5ba0be4SMike Smith * Allocate a buffer with a preset data size.
2706c5ba0be4SMike Smith */
2707c5ba0be4SMike Smith ACPI_BUFFER *
acpi_AllocBuffer(int size)2708c5ba0be4SMike Smith acpi_AllocBuffer(int size)
2709c5ba0be4SMike Smith {
2710c5ba0be4SMike Smith ACPI_BUFFER *buf;
2711c5ba0be4SMike Smith
2712c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2713c5ba0be4SMike Smith return (NULL);
2714c5ba0be4SMike Smith buf->Length = size;
2715c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1);
2716c5ba0be4SMike Smith return (buf);
2717c5ba0be4SMike Smith }
2718c5ba0be4SMike Smith
2719c310653eSNate Lawson ACPI_STATUS
acpi_SetInteger(ACPI_HANDLE handle,char * path,UINT32 number)2720cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2721c310653eSNate Lawson {
2722c310653eSNate Lawson ACPI_OBJECT arg1;
2723c310653eSNate Lawson ACPI_OBJECT_LIST args;
2724c310653eSNate Lawson
2725c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER;
2726c310653eSNate Lawson arg1.Integer.Value = number;
2727c310653eSNate Lawson args.Count = 1;
2728c310653eSNate Lawson args.Pointer = &arg1;
2729c310653eSNate Lawson
2730c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL));
2731c310653eSNate Lawson }
2732c310653eSNate Lawson
2733c5ba0be4SMike Smith /*
2734c5ba0be4SMike Smith * Evaluate a path that should return an integer.
2735c5ba0be4SMike Smith */
2736c5ba0be4SMike Smith ACPI_STATUS
acpi_GetInteger(ACPI_HANDLE handle,char * path,UINT32 * number)2737cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2738c5ba0be4SMike Smith {
2739be2b1797SNate Lawson ACPI_STATUS status;
2740c5ba0be4SMike Smith ACPI_BUFFER buf;
2741c573e654SMitsuru IWASAKI ACPI_OBJECT param;
2742c5ba0be4SMike Smith
2743c5ba0be4SMike Smith if (handle == NULL)
2744c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT;
27450c7da7acSJohn Baldwin
27460c7da7acSJohn Baldwin /*
27470c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or
27480c7da7acSJohn Baldwin * a method that will return an Integer.
27490c7da7acSJohn Baldwin */
2750c5ba0be4SMike Smith buf.Pointer = ¶m;
2751c5ba0be4SMike Smith buf.Length = sizeof(param);
2752be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf);
2753be2b1797SNate Lawson if (ACPI_SUCCESS(status)) {
2754be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER)
2755c5ba0be4SMike Smith *number = param.Integer.Value;
2756be2b1797SNate Lawson else
2757be2b1797SNate Lawson status = AE_TYPE;
2758c5ba0be4SMike Smith }
27590c7da7acSJohn Baldwin
27600c7da7acSJohn Baldwin /*
27610c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer
27620c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal
27630c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer,
27640c7da7acSJohn Baldwin * convert it into an Integer as best we can.
27650c7da7acSJohn Baldwin *
27660c7da7acSJohn Baldwin * This is a hack.
27670c7da7acSJohn Baldwin */
2768be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) {
2769b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2770be2b1797SNate Lawson status = AE_NO_MEMORY;
27710c7da7acSJohn Baldwin } else {
2772be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf);
2773be2b1797SNate Lawson if (ACPI_SUCCESS(status))
2774be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number);
27750c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer);
27760c7da7acSJohn Baldwin }
2777f97739daSNate Lawson }
2778be2b1797SNate Lawson return (status);
2779c5ba0be4SMike Smith }
2780c5ba0be4SMike Smith
2781c573e654SMitsuru IWASAKI ACPI_STATUS
acpi_ConvertBufferToInteger(ACPI_BUFFER * bufp,UINT32 * number)2782cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2783c573e654SMitsuru IWASAKI {
2784c573e654SMitsuru IWASAKI ACPI_OBJECT *p;
2785dba55fa2SNate Lawson UINT8 *val;
2786c573e654SMitsuru IWASAKI int i;
2787c573e654SMitsuru IWASAKI
2788c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer;
2789c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) {
2790c573e654SMitsuru IWASAKI *number = p->Integer.Value;
2791c573e654SMitsuru IWASAKI return (AE_OK);
2792c573e654SMitsuru IWASAKI }
2793c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER)
2794c573e654SMitsuru IWASAKI return (AE_TYPE);
2795c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int))
2796c573e654SMitsuru IWASAKI return (AE_BAD_DATA);
2797be2b1797SNate Lawson
2798c573e654SMitsuru IWASAKI *number = 0;
2799dba55fa2SNate Lawson val = p->Buffer.Pointer;
2800c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++)
2801dba55fa2SNate Lawson *number += val[i] << (i * 8);
2802c573e654SMitsuru IWASAKI return (AE_OK);
2803c573e654SMitsuru IWASAKI }
2804c573e654SMitsuru IWASAKI
2805c5ba0be4SMike Smith /*
2806c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied
2807c5ba0be4SMike Smith * function for each element.
2808c5ba0be4SMike Smith *
2809c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error.
2810c5ba0be4SMike Smith */
2811c5ba0be4SMike Smith ACPI_STATUS
acpi_ForeachPackageObject(ACPI_OBJECT * pkg,void (* func)(ACPI_OBJECT * comp,void * arg),void * arg)2812be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2813be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2814c5ba0be4SMike Smith {
2815c5ba0be4SMike Smith ACPI_OBJECT *comp;
2816c5ba0be4SMike Smith int i;
2817c5ba0be4SMike Smith
2818be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2819c5ba0be4SMike Smith return (AE_BAD_PARAMETER);
2820c5ba0be4SMike Smith
2821be2b1797SNate Lawson /* Iterate over components */
2822be2b1797SNate Lawson i = 0;
2823be2b1797SNate Lawson comp = pkg->Package.Elements;
2824be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++)
2825c5ba0be4SMike Smith func(comp, arg);
2826c5ba0be4SMike Smith
2827c5ba0be4SMike Smith return (AE_OK);
2828c5ba0be4SMike Smith }
2829c5ba0be4SMike Smith
28306f69255bSMike Smith /*
28316f69255bSMike Smith * Find the (index)th resource object in a set.
28326f69255bSMike Smith */
28336f69255bSMike Smith ACPI_STATUS
acpi_FindIndexedResource(ACPI_BUFFER * buf,int index,ACPI_RESOURCE ** resp)28346d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
28356f69255bSMike Smith {
28366d63101aSMike Smith ACPI_RESOURCE *rp;
28376f69255bSMike Smith int i;
28386f69255bSMike Smith
28396d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer;
28406f69255bSMike Smith i = index;
28416d63101aSMike Smith while (i-- > 0) {
2842be2b1797SNate Lawson /* Range check */
28436d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
28446f69255bSMike Smith return (AE_BAD_PARAMETER);
2845be2b1797SNate Lawson
2846be2b1797SNate Lawson /* Check for terminator */
2847e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
28486d63101aSMike Smith return (AE_NOT_FOUND);
2849abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp);
28506f69255bSMike Smith }
28516f69255bSMike Smith if (resp != NULL)
28526d63101aSMike Smith *resp = rp;
2853be2b1797SNate Lawson
28546d63101aSMike Smith return (AE_OK);
28556d63101aSMike Smith }
28566d63101aSMike Smith
28576d63101aSMike Smith /*
28586d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER.
28596d63101aSMike Smith *
28606d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
28616d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
28626d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
28636d63101aSMike Smith * resources.
28646d63101aSMike Smith */
28656d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
28666d63101aSMike Smith
28676d63101aSMike Smith ACPI_STATUS
acpi_AppendBufferResource(ACPI_BUFFER * buf,ACPI_RESOURCE * res)28686d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
28696d63101aSMike Smith {
28706d63101aSMike Smith ACPI_RESOURCE *rp;
28716d63101aSMike Smith void *newp;
28726d63101aSMike Smith
2873be2b1797SNate Lawson /* Initialise the buffer if necessary. */
28746d63101aSMike Smith if (buf->Pointer == NULL) {
28756d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
28766d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
28776d63101aSMike Smith return (AE_NO_MEMORY);
28786d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer;
2879e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
288060d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN;
28816d63101aSMike Smith }
28826d63101aSMike Smith if (res == NULL)
28836d63101aSMike Smith return (AE_OK);
28846d63101aSMike Smith
28856d63101aSMike Smith /*
28866d63101aSMike Smith * Scan the current buffer looking for the terminator.
28876d63101aSMike Smith * This will either find the terminator or hit the end
28886d63101aSMike Smith * of the buffer and return an error.
28896d63101aSMike Smith */
28906d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer;
28916d63101aSMike Smith for (;;) {
2892be2b1797SNate Lawson /* Range check, don't go outside the buffer */
28936d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
28946d63101aSMike Smith return (AE_BAD_PARAMETER);
2895e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
28966d63101aSMike Smith break;
2897abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp);
28986d63101aSMike Smith }
28996d63101aSMike Smith
29006d63101aSMike Smith /*
29016d63101aSMike Smith * Check the size of the buffer and expand if required.
29026d63101aSMike Smith *
29036d63101aSMike Smith * Required size is:
29046d63101aSMike Smith * size of existing resources before terminator +
29056d63101aSMike Smith * size of new resource and header +
29066d63101aSMike Smith * size of terminator.
29076d63101aSMike Smith *
29086d63101aSMike Smith * Note that this loop should really only run once, unless
29096d63101aSMike Smith * for some reason we are stuffing a *really* huge resource.
29106d63101aSMike Smith */
29116d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2912e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA +
2913e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) {
29146d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
29156d63101aSMike Smith return (AE_NO_MEMORY);
29166d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length);
2917a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2918a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
29196d63101aSMike Smith AcpiOsFree(buf->Pointer);
29206d63101aSMike Smith buf->Pointer = newp;
29216d63101aSMike Smith buf->Length += buf->Length;
29226d63101aSMike Smith }
29236d63101aSMike Smith
2924be2b1797SNate Lawson /* Insert the new resource. */
2925e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
29266d63101aSMike Smith
2927be2b1797SNate Lawson /* And add the terminator. */
2928abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp);
2929e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
293060d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN;
29316d63101aSMike Smith
29326f69255bSMike Smith return (AE_OK);
29336f69255bSMike Smith }
2934c5ba0be4SMike Smith
2935fe64ff3cSVladimir Kondratyev UINT64
acpi_DSMQuery(ACPI_HANDLE h,const uint8_t * uuid,int revision)2936fe64ff3cSVladimir Kondratyev acpi_DSMQuery(ACPI_HANDLE h, const uint8_t *uuid, int revision)
29378fd10880SBen Widawsky {
29388fd10880SBen Widawsky /*
29398fd10880SBen Widawsky * ACPI spec 9.1.1 defines this.
29408fd10880SBen Widawsky *
29418fd10880SBen Widawsky * "Arg2: Function Index Represents a specific function whose meaning is
29428fd10880SBen Widawsky * specific to the UUID and Revision ID. Function indices should start
29438fd10880SBen Widawsky * with 1. Function number zero is a query function (see the special
29448fd10880SBen Widawsky * return code defined below)."
29458fd10880SBen Widawsky */
29468fd10880SBen Widawsky ACPI_BUFFER buf;
29478fd10880SBen Widawsky ACPI_OBJECT *obj;
2948fe64ff3cSVladimir Kondratyev UINT64 ret = 0;
2949fe64ff3cSVladimir Kondratyev int i;
29508fd10880SBen Widawsky
29518fd10880SBen Widawsky if (!ACPI_SUCCESS(acpi_EvaluateDSM(h, uuid, revision, 0, NULL, &buf))) {
29528fd10880SBen Widawsky ACPI_INFO(("Failed to enumerate DSM functions\n"));
29538fd10880SBen Widawsky return (0);
29548fd10880SBen Widawsky }
29558fd10880SBen Widawsky
29568fd10880SBen Widawsky obj = (ACPI_OBJECT *)buf.Pointer;
29578fd10880SBen Widawsky KASSERT(obj, ("Object not allowed to be NULL\n"));
29588fd10880SBen Widawsky
29598fd10880SBen Widawsky /*
29608fd10880SBen Widawsky * From ACPI 6.2 spec 9.1.1:
29618fd10880SBen Widawsky * If Function Index = 0, a Buffer containing a function index bitfield.
29628fd10880SBen Widawsky * Otherwise, the return value and type depends on the UUID and revision
29638fd10880SBen Widawsky * ID (see below).
29648fd10880SBen Widawsky */
29658fd10880SBen Widawsky switch (obj->Type) {
29668fd10880SBen Widawsky case ACPI_TYPE_BUFFER:
2967fe64ff3cSVladimir Kondratyev for (i = 0; i < MIN(obj->Buffer.Length, sizeof(ret)); i++)
2968fe64ff3cSVladimir Kondratyev ret |= (((uint64_t)obj->Buffer.Pointer[i]) << (i * 8));
29698fd10880SBen Widawsky break;
29708fd10880SBen Widawsky case ACPI_TYPE_INTEGER:
29718fd10880SBen Widawsky ACPI_BIOS_WARNING((AE_INFO,
29728fd10880SBen Widawsky "Possibly buggy BIOS with ACPI_TYPE_INTEGER for function enumeration\n"));
2973fe64ff3cSVladimir Kondratyev ret = obj->Integer.Value;
29748fd10880SBen Widawsky break;
29758fd10880SBen Widawsky default:
29768fd10880SBen Widawsky ACPI_WARNING((AE_INFO, "Unexpected return type %u\n", obj->Type));
29778fd10880SBen Widawsky };
29788fd10880SBen Widawsky
29798fd10880SBen Widawsky AcpiOsFree(obj);
29808fd10880SBen Widawsky return ret;
29818fd10880SBen Widawsky }
29828fd10880SBen Widawsky
29838fd10880SBen Widawsky /*
29848fd10880SBen Widawsky * DSM may return multiple types depending on the function. It is therefore
29858fd10880SBen Widawsky * unsafe to use the typed evaluation. It is highly recommended that the caller
29868fd10880SBen Widawsky * check the type of the returned object.
29878fd10880SBen Widawsky */
29888fd10880SBen Widawsky ACPI_STATUS
acpi_EvaluateDSM(ACPI_HANDLE handle,const uint8_t * uuid,int revision,UINT64 function,ACPI_OBJECT * package,ACPI_BUFFER * out_buf)2989fe64ff3cSVladimir Kondratyev acpi_EvaluateDSM(ACPI_HANDLE handle, const uint8_t *uuid, int revision,
2990fe64ff3cSVladimir Kondratyev UINT64 function, ACPI_OBJECT *package, ACPI_BUFFER *out_buf)
2991fe64ff3cSVladimir Kondratyev {
2992fe64ff3cSVladimir Kondratyev return (acpi_EvaluateDSMTyped(handle, uuid, revision, function,
2993fe64ff3cSVladimir Kondratyev package, out_buf, ACPI_TYPE_ANY));
2994fe64ff3cSVladimir Kondratyev }
2995fe64ff3cSVladimir Kondratyev
2996fe64ff3cSVladimir Kondratyev ACPI_STATUS
acpi_EvaluateDSMTyped(ACPI_HANDLE handle,const uint8_t * uuid,int revision,UINT64 function,ACPI_OBJECT * package,ACPI_BUFFER * out_buf,ACPI_OBJECT_TYPE type)2997fe64ff3cSVladimir Kondratyev acpi_EvaluateDSMTyped(ACPI_HANDLE handle, const uint8_t *uuid, int revision,
2998fe64ff3cSVladimir Kondratyev UINT64 function, ACPI_OBJECT *package, ACPI_BUFFER *out_buf,
2999fe64ff3cSVladimir Kondratyev ACPI_OBJECT_TYPE type)
30008fd10880SBen Widawsky {
30018fd10880SBen Widawsky ACPI_OBJECT arg[4];
30028fd10880SBen Widawsky ACPI_OBJECT_LIST arglist;
30038fd10880SBen Widawsky ACPI_BUFFER buf;
30048fd10880SBen Widawsky ACPI_STATUS status;
30058fd10880SBen Widawsky
30068fd10880SBen Widawsky if (out_buf == NULL)
30078fd10880SBen Widawsky return (AE_NO_MEMORY);
30088fd10880SBen Widawsky
30098fd10880SBen Widawsky arg[0].Type = ACPI_TYPE_BUFFER;
30108fd10880SBen Widawsky arg[0].Buffer.Length = ACPI_UUID_LENGTH;
3011fe64ff3cSVladimir Kondratyev arg[0].Buffer.Pointer = __DECONST(uint8_t *, uuid);
30128fd10880SBen Widawsky arg[1].Type = ACPI_TYPE_INTEGER;
30138fd10880SBen Widawsky arg[1].Integer.Value = revision;
30148fd10880SBen Widawsky arg[2].Type = ACPI_TYPE_INTEGER;
30158fd10880SBen Widawsky arg[2].Integer.Value = function;
30168fd10880SBen Widawsky if (package) {
30178fd10880SBen Widawsky arg[3] = *package;
30188fd10880SBen Widawsky } else {
30198fd10880SBen Widawsky arg[3].Type = ACPI_TYPE_PACKAGE;
30208fd10880SBen Widawsky arg[3].Package.Count = 0;
30218fd10880SBen Widawsky arg[3].Package.Elements = NULL;
30228fd10880SBen Widawsky }
30238fd10880SBen Widawsky
30248fd10880SBen Widawsky arglist.Pointer = arg;
30258fd10880SBen Widawsky arglist.Count = 4;
30268fd10880SBen Widawsky buf.Pointer = NULL;
30278fd10880SBen Widawsky buf.Length = ACPI_ALLOCATE_BUFFER;
3028fe64ff3cSVladimir Kondratyev status = AcpiEvaluateObjectTyped(handle, "_DSM", &arglist, &buf, type);
30298fd10880SBen Widawsky if (ACPI_FAILURE(status))
30308fd10880SBen Widawsky return (status);
30318fd10880SBen Widawsky
30328fd10880SBen Widawsky KASSERT(ACPI_SUCCESS(status), ("Unexpected status"));
30338fd10880SBen Widawsky
30348fd10880SBen Widawsky *out_buf = buf;
30358fd10880SBen Widawsky return (status);
30368fd10880SBen Widawsky }
30378fd10880SBen Widawsky
30385f3dd91aSJohn Baldwin ACPI_STATUS
acpi_EvaluateOSC(ACPI_HANDLE handle,uint8_t * uuid,int revision,int count,uint32_t * caps_in,uint32_t * caps_out,bool query)30395f3dd91aSJohn Baldwin acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count,
30404c26ac69SJohn Baldwin uint32_t *caps_in, uint32_t *caps_out, bool query)
30415f3dd91aSJohn Baldwin {
30424c26ac69SJohn Baldwin ACPI_OBJECT arg[4], *ret;
30435f3dd91aSJohn Baldwin ACPI_OBJECT_LIST arglist;
30444c26ac69SJohn Baldwin ACPI_BUFFER buf;
30454c26ac69SJohn Baldwin ACPI_STATUS status;
30465f3dd91aSJohn Baldwin
30475f3dd91aSJohn Baldwin arglist.Pointer = arg;
30485f3dd91aSJohn Baldwin arglist.Count = 4;
30495f3dd91aSJohn Baldwin arg[0].Type = ACPI_TYPE_BUFFER;
30505f3dd91aSJohn Baldwin arg[0].Buffer.Length = ACPI_UUID_LENGTH;
30515f3dd91aSJohn Baldwin arg[0].Buffer.Pointer = uuid;
30525f3dd91aSJohn Baldwin arg[1].Type = ACPI_TYPE_INTEGER;
30535f3dd91aSJohn Baldwin arg[1].Integer.Value = revision;
30545f3dd91aSJohn Baldwin arg[2].Type = ACPI_TYPE_INTEGER;
30555f3dd91aSJohn Baldwin arg[2].Integer.Value = count;
30565f3dd91aSJohn Baldwin arg[3].Type = ACPI_TYPE_BUFFER;
30574c26ac69SJohn Baldwin arg[3].Buffer.Length = count * sizeof(*caps_in);
30584c26ac69SJohn Baldwin arg[3].Buffer.Pointer = (uint8_t *)caps_in;
30594c26ac69SJohn Baldwin caps_in[0] = query ? 1 : 0;
30604c26ac69SJohn Baldwin buf.Pointer = NULL;
30614c26ac69SJohn Baldwin buf.Length = ACPI_ALLOCATE_BUFFER;
30624c26ac69SJohn Baldwin status = AcpiEvaluateObjectTyped(handle, "_OSC", &arglist, &buf,
30634c26ac69SJohn Baldwin ACPI_TYPE_BUFFER);
30644c26ac69SJohn Baldwin if (ACPI_FAILURE(status))
30654c26ac69SJohn Baldwin return (status);
30664c26ac69SJohn Baldwin if (caps_out != NULL) {
30674c26ac69SJohn Baldwin ret = buf.Pointer;
30684c26ac69SJohn Baldwin if (ret->Buffer.Length != count * sizeof(*caps_out)) {
30694c26ac69SJohn Baldwin AcpiOsFree(buf.Pointer);
30704c26ac69SJohn Baldwin return (AE_BUFFER_OVERFLOW);
30714c26ac69SJohn Baldwin }
30724c26ac69SJohn Baldwin bcopy(ret->Buffer.Pointer, caps_out, ret->Buffer.Length);
30734c26ac69SJohn Baldwin }
30744c26ac69SJohn Baldwin AcpiOsFree(buf.Pointer);
30754c26ac69SJohn Baldwin return (status);
30765f3dd91aSJohn Baldwin }
30775f3dd91aSJohn Baldwin
3078da14ac9fSJohn Baldwin /*
3079da14ac9fSJohn Baldwin * Set interrupt model.
3080da14ac9fSJohn Baldwin */
3081da14ac9fSJohn Baldwin ACPI_STATUS
acpi_SetIntrModel(int model)3082da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
3083da14ac9fSJohn Baldwin {
3084da14ac9fSJohn Baldwin
3085c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
3086da14ac9fSJohn Baldwin }
3087da14ac9fSJohn Baldwin
308800a30448SNate Lawson /*
3089d95e7f5aSJohn Baldwin * Walk subtables of a table and call a callback routine for each
3090d95e7f5aSJohn Baldwin * subtable. The caller should provide the first subtable and a
3091d95e7f5aSJohn Baldwin * pointer to the end of the table. This can be used to walk tables
3092d95e7f5aSJohn Baldwin * such as MADT and SRAT that use subtable entries.
3093d95e7f5aSJohn Baldwin */
3094d95e7f5aSJohn Baldwin void
acpi_walk_subtables(void * first,void * end,acpi_subtable_handler * handler,void * arg)3095d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
3096d95e7f5aSJohn Baldwin void *arg)
3097d95e7f5aSJohn Baldwin {
3098d95e7f5aSJohn Baldwin ACPI_SUBTABLE_HEADER *entry;
3099d95e7f5aSJohn Baldwin
3100d95e7f5aSJohn Baldwin for (entry = first; (void *)entry < end; ) {
3101d95e7f5aSJohn Baldwin /* Avoid an infinite loop if we hit a bogus entry. */
3102d95e7f5aSJohn Baldwin if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
3103d95e7f5aSJohn Baldwin return;
3104d95e7f5aSJohn Baldwin
3105d95e7f5aSJohn Baldwin handler(entry, arg);
3106d95e7f5aSJohn Baldwin entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
3107d95e7f5aSJohn Baldwin }
3108d95e7f5aSJohn Baldwin }
3109d95e7f5aSJohn Baldwin
3110d95e7f5aSJohn Baldwin /*
311100a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be
311200a30448SNate Lawson * removed.
311300a30448SNate Lawson *
311400a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran
311500a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
311600a30448SNate Lawson */
311700a30448SNate Lawson ACPI_STATUS
acpi_SetSleepState(struct acpi_softc * sc,int state)311800a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state)
311900a30448SNate Lawson {
312000a30448SNate Lawson static int once;
312100a30448SNate Lawson
312200a30448SNate Lawson if (!once) {
31232d0c82e8SJung-uk Kim device_printf(sc->acpi_dev,
312400a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n");
312500a30448SNate Lawson once = 1;
312600a30448SNate Lawson }
312700a30448SNate Lawson return (acpi_EnterSleepState(sc, state));
312800a30448SNate Lawson }
312900a30448SNate Lawson
3130c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
313100a30448SNate Lawson static void
acpi_sleep_force_task(void * context)3132ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context)
3133ffe3f1f8SMitsuru IWASAKI {
3134ffe3f1f8SMitsuru IWASAKI struct acpi_softc *sc = (struct acpi_softc *)context;
3135ffe3f1f8SMitsuru IWASAKI
3136ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
3137ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
3138ffe3f1f8SMitsuru IWASAKI sc->acpi_next_sstate);
3139ffe3f1f8SMitsuru IWASAKI }
3140ffe3f1f8SMitsuru IWASAKI
3141ffe3f1f8SMitsuru IWASAKI static void
acpi_sleep_force(void * arg)314200a30448SNate Lawson acpi_sleep_force(void *arg)
314300a30448SNate Lawson {
31442d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg;
314500a30448SNate Lawson
31462d0c82e8SJung-uk Kim device_printf(sc->acpi_dev,
31472d0c82e8SJung-uk Kim "suspend request timed out, forcing sleep now\n");
3148ffe3f1f8SMitsuru IWASAKI /*
3149fadf3fb9SJohn Baldwin * XXX Suspending from callout causes freezes in DEVICE_SUSPEND().
3150ffe3f1f8SMitsuru IWASAKI * Suspend from acpi_task thread instead.
3151ffe3f1f8SMitsuru IWASAKI */
3152ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3153ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task, sc)))
3154ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n");
315500a30448SNate Lawson }
3156c66d2b38SJung-uk Kim #endif
315700a30448SNate Lawson
315800a30448SNate Lawson /*
315900a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm
316000a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to
316100a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all
316200a30448SNate Lawson * acks are in.
316300a30448SNate Lawson */
316400a30448SNate Lawson int
acpi_ReqSleepState(struct acpi_softc * sc,int state)316500a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state)
316600a30448SNate Lawson {
316771f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
316800a30448SNate Lawson struct apm_clone_data *clone;
3169337744d9SJung-uk Kim ACPI_STATUS status;
317000a30448SNate Lawson
3171a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
317200a30448SNate Lawson return (EINVAL);
3173a0e73a12SJung-uk Kim if (!acpi_sleep_states[state])
3174a0e73a12SJung-uk Kim return (EOPNOTSUPP);
317500a30448SNate Lawson
31762eb0015aSColin Percival /*
31772eb0015aSColin Percival * If a reboot/shutdown/suspend request is already in progress or
31782eb0015aSColin Percival * suspend is blocked due to an upcoming shutdown, just return.
31792eb0015aSColin Percival */
31802eb0015aSColin Percival if (rebooting || sc->acpi_next_sstate != 0 || suspend_blocked) {
3181b12bc99fSMitsuru IWASAKI return (0);
3182b12bc99fSMitsuru IWASAKI }
3183b12bc99fSMitsuru IWASAKI
3184f22377a8SMitsuru IWASAKI /* Wait until sleep is enabled. */
3185f22377a8SMitsuru IWASAKI while (sc->acpi_sleep_disabled) {
3186f22377a8SMitsuru IWASAKI AcpiOsSleep(1000);
3187f22377a8SMitsuru IWASAKI }
3188f22377a8SMitsuru IWASAKI
3189337744d9SJung-uk Kim ACPI_LOCK(acpi);
319000a30448SNate Lawson
3191f22377a8SMitsuru IWASAKI sc->acpi_next_sstate = state;
319200a30448SNate Lawson
3193337744d9SJung-uk Kim /* S5 (soft-off) should be entered directly with no waiting. */
3194337744d9SJung-uk Kim if (state == ACPI_STATE_S5) {
3195337744d9SJung-uk Kim ACPI_UNLOCK(acpi);
3196337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state);
3197337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO);
3198337744d9SJung-uk Kim }
3199337744d9SJung-uk Kim
320000a30448SNate Lawson /* Record the pending state and notify all apm devices. */
320100a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
320200a30448SNate Lawson clone->notify_status = APM_EV_NONE;
320300a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) {
320400a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO);
3205374d9c4dSJohn Baldwin KNOTE_LOCKED(&clone->sel_read.si_note, 0);
320600a30448SNate Lawson }
320700a30448SNate Lawson }
320800a30448SNate Lawson
32090c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */
3210d42f0ad8SJung-uk Kim if (!devctl_process_running()) {
32110c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi);
3212337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state);
3213337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO);
32140c26519eSMitsuru IWASAKI }
32150c26519eSMitsuru IWASAKI
321600a30448SNate Lawson /*
321700a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request
321800a30448SNate Lawson * in time. This way we still eventually go to sleep if we were
321900a30448SNate Lawson * overheating or running low on battery, even if userland is hung.
322000a30448SNate Lawson * We cancel this timeout once all userland acks are in or the
322100a30448SNate Lawson * suspend request is aborted.
322200a30448SNate Lawson */
322300a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
322400a30448SNate Lawson ACPI_UNLOCK(acpi);
3225d42f0ad8SJung-uk Kim
3226d42f0ad8SJung-uk Kim /* Now notify devd(8) also. */
3227d42f0ad8SJung-uk Kim acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
3228d42f0ad8SJung-uk Kim
322900a30448SNate Lawson return (0);
3230c66d2b38SJung-uk Kim #else
3231c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */
3232c66d2b38SJung-uk Kim return (EOPNOTSUPP);
3233c66d2b38SJung-uk Kim #endif
323400a30448SNate Lawson }
323500a30448SNate Lawson
323600a30448SNate Lawson /*
323700a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has
323800a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the
323900a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled
324000a30448SNate Lawson * and gives an errno value describing why. Once all votes are in,
324100a30448SNate Lawson * we suspend the system.
324200a30448SNate Lawson */
324300a30448SNate Lawson int
acpi_AckSleepState(struct apm_clone_data * clone,int error)324400a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error)
324500a30448SNate Lawson {
3246c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
324700a30448SNate Lawson struct acpi_softc *sc;
324800a30448SNate Lawson int ret, sleeping;
324900a30448SNate Lawson
325000a30448SNate Lawson /* If no pending sleep state, return an error. */
325100a30448SNate Lawson ACPI_LOCK(acpi);
325200a30448SNate Lawson sc = clone->acpi_sc;
325300a30448SNate Lawson if (sc->acpi_next_sstate == 0) {
325400a30448SNate Lawson ACPI_UNLOCK(acpi);
325500a30448SNate Lawson return (ENXIO);
325600a30448SNate Lawson }
325700a30448SNate Lawson
325800a30448SNate Lawson /* Caller wants to abort suspend process. */
325900a30448SNate Lawson if (error) {
326000a30448SNate Lawson sc->acpi_next_sstate = 0;
326100a30448SNate Lawson callout_stop(&sc->susp_force_to);
32622d0c82e8SJung-uk Kim device_printf(sc->acpi_dev,
32632d0c82e8SJung-uk Kim "listener on %s cancelled the pending suspend\n",
326400a30448SNate Lawson devtoname(clone->cdev));
326500a30448SNate Lawson ACPI_UNLOCK(acpi);
326600a30448SNate Lawson return (0);
326700a30448SNate Lawson }
326800a30448SNate Lawson
326900a30448SNate Lawson /*
327000a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through
327100a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that
327200a30448SNate Lawson * are writable since read-only devices couldn't ack the request.
327300a30448SNate Lawson */
327400a30448SNate Lawson sleeping = TRUE;
3275c66d2b38SJung-uk Kim clone->notify_status = APM_EV_ACKED;
327600a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
327700a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
327800a30448SNate Lawson clone->notify_status != APM_EV_ACKED) {
327900a30448SNate Lawson sleeping = FALSE;
328000a30448SNate Lawson break;
328100a30448SNate Lawson }
328200a30448SNate Lawson }
328300a30448SNate Lawson
328400a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */
328500a30448SNate Lawson if (sleeping)
328600a30448SNate Lawson callout_stop(&sc->susp_force_to);
328700a30448SNate Lawson ACPI_UNLOCK(acpi);
328800a30448SNate Lawson ret = 0;
328900a30448SNate Lawson if (sleeping) {
329000a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
329100a30448SNate Lawson ret = ENODEV;
329200a30448SNate Lawson }
329300a30448SNate Lawson return (ret);
3294c66d2b38SJung-uk Kim #else
3295c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */
3296c66d2b38SJung-uk Kim return (EOPNOTSUPP);
3297c66d2b38SJung-uk Kim #endif
329800a30448SNate Lawson }
329900a30448SNate Lawson
3300ece50487SMitsuru IWASAKI static void
acpi_sleep_enable(void * arg)3301ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
3302ece50487SMitsuru IWASAKI {
3303d42f0ad8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg;
3304bee4aa4aSNate Lawson
3305fadf3fb9SJohn Baldwin ACPI_LOCK_ASSERT(acpi);
3306fadf3fb9SJohn Baldwin
3307a0e73a12SJung-uk Kim /* Reschedule if the system is not fully up and running. */
3308a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) {
3309fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3310a0e73a12SJung-uk Kim return;
3311a0e73a12SJung-uk Kim }
3312a0e73a12SJung-uk Kim
3313a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = FALSE;
3314d42f0ad8SJung-uk Kim }
3315d42f0ad8SJung-uk Kim
3316d42f0ad8SJung-uk Kim static ACPI_STATUS
acpi_sleep_disable(struct acpi_softc * sc)3317d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc)
3318d42f0ad8SJung-uk Kim {
3319d42f0ad8SJung-uk Kim ACPI_STATUS status;
3320d42f0ad8SJung-uk Kim
3321a0e73a12SJung-uk Kim /* Fail if the system is not fully up and running. */
3322a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning)
3323a0e73a12SJung-uk Kim return (AE_ERROR);
3324a0e73a12SJung-uk Kim
3325d42f0ad8SJung-uk Kim ACPI_LOCK(acpi);
3326d42f0ad8SJung-uk Kim status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
3327a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE;
3328d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi);
3329d42f0ad8SJung-uk Kim
3330d42f0ad8SJung-uk Kim return (status);
3331ece50487SMitsuru IWASAKI }
3332c30382dfSMitsuru IWASAKI
333315e2f34fSNate Lawson enum acpi_sleep_state {
333415e2f34fSNate Lawson ACPI_SS_NONE,
333515e2f34fSNate Lawson ACPI_SS_GPE_SET,
333615e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND,
333715e2f34fSNate Lawson ACPI_SS_SLP_PREP,
333815e2f34fSNate Lawson ACPI_SS_SLEPT,
333915e2f34fSNate Lawson };
334015e2f34fSNate Lawson
334115e32d5dSMike Smith /*
334200a30448SNate Lawson * Enter the desired system sleep state.
334315e32d5dSMike Smith *
3344be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS
334515e32d5dSMike Smith */
334600a30448SNate Lawson static ACPI_STATUS
acpi_EnterSleepState(struct acpi_softc * sc,int state)334700a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state)
334815e32d5dSMike Smith {
334971804adcSJung-uk Kim register_t intr;
335015e2f34fSNate Lawson ACPI_STATUS status;
3351b913a7d5SAndriy Gapon ACPI_EVENT_STATUS power_button_status;
335215e2f34fSNate Lawson enum acpi_sleep_state slp_state;
3353f0a101b7SMitsuru IWASAKI int sleep_result;
335415e32d5dSMike Smith
3355b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
33560ae55423SMike Smith
3357a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
335833c70d41SAndriy Gapon return_ACPI_STATUS (AE_BAD_PARAMETER);
3359a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) {
3360a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
3361a0e73a12SJung-uk Kim state);
3362a0e73a12SJung-uk Kim return (AE_SUPPORT);
3363a0e73a12SJung-uk Kim }
3364a0e73a12SJung-uk Kim
3365a0e73a12SJung-uk Kim /* Re-entry once we're suspending is not allowed. */
3366a0e73a12SJung-uk Kim status = acpi_sleep_disable(sc);
3367a0e73a12SJung-uk Kim if (ACPI_FAILURE(status)) {
3368a0e73a12SJung-uk Kim device_printf(sc->acpi_dev,
3369a0e73a12SJung-uk Kim "suspend request ignored (not ready yet)\n");
3370a0e73a12SJung-uk Kim return (status);
3371a0e73a12SJung-uk Kim }
337233c70d41SAndriy Gapon
337333c70d41SAndriy Gapon if (state == ACPI_STATE_S5) {
337433c70d41SAndriy Gapon /*
337533c70d41SAndriy Gapon * Shut down cleanly and power off. This will call us back through the
337633c70d41SAndriy Gapon * shutdown handlers.
337733c70d41SAndriy Gapon */
337833c70d41SAndriy Gapon shutdown_nice(RB_POWEROFF);
337933c70d41SAndriy Gapon return_ACPI_STATUS (AE_OK);
338033c70d41SAndriy Gapon }
338133c70d41SAndriy Gapon
338285f95fffSAndriy Gapon EVENTHANDLER_INVOKE(power_suspend_early);
338385f95fffSAndriy Gapon stop_all_proc();
3384f1084587SKonstantin Belousov suspend_all_fs();
33854a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_suspend);
33864a8fa6feSJung-uk Kim
3387fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
3388fdce57a0SJohn Baldwin MPASS(mp_ncpus == 1 || smp_started);
3389fdce57a0SJohn Baldwin thread_lock(curthread);
3390fdce57a0SJohn Baldwin sched_bind(curthread, 0);
3391fdce57a0SJohn Baldwin thread_unlock(curthread);
3392fdce57a0SJohn Baldwin #else
339336a483bbSJung-uk Kim if (smp_started) {
3394c66d2b38SJung-uk Kim thread_lock(curthread);
3395c66d2b38SJung-uk Kim sched_bind(curthread, 0);
3396c66d2b38SJung-uk Kim thread_unlock(curthread);
339736a483bbSJung-uk Kim }
3398fdce57a0SJohn Baldwin #endif
3399c66d2b38SJung-uk Kim
3400a56fe095SJohn Baldwin /*
3401c6df6f53SWarner Losh * Be sure to hold Giant across DEVICE_SUSPEND/RESUME
3402a56fe095SJohn Baldwin */
3403c6df6f53SWarner Losh bus_topo_lock();
3404c66d2b38SJung-uk Kim
340515e2f34fSNate Lawson slp_state = ACPI_SS_NONE;
340656d8cb57SMitsuru IWASAKI
3407a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state;
3408a1fccb47SMitsuru IWASAKI
3409d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */
3410d4b9ff91SNate Lawson acpi_wake_prep_walk(state);
341115e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET;
3412e8b4d56eSNate Lawson
341315e32d5dSMike Smith /*
3414c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one
3415c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree.
341615e32d5dSMike Smith *
3417c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass
3418c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current
3419c7f88fc3SNate Lawson * bus interface does not provide for this.
342015e32d5dSMike Smith */
342115e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) {
342215e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n");
342333c70d41SAndriy Gapon goto backout;
342415e2f34fSNate Lawson }
342515e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND;
3426b9c780d6SMitsuru IWASAKI
3427be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state);
3428be2b1797SNate Lawson if (ACPI_FAILURE(status)) {
3429b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
3430b9c780d6SMitsuru IWASAKI AcpiFormatException(status));
343133c70d41SAndriy Gapon goto backout;
3432b9c780d6SMitsuru IWASAKI }
343315e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP;
3434b9c780d6SMitsuru IWASAKI
3435be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0)
3436ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000);
3437ff01efb5SMitsuru IWASAKI
343827dca831SAndriy Gapon suspendclock();
3439f0a101b7SMitsuru IWASAKI intr = intr_disable();
34404d46ef51SJung-uk Kim if (state != ACPI_STATE_S1) {
3441f0a101b7SMitsuru IWASAKI sleep_result = acpi_sleep_machdep(sc, state);
3442f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 0);
3443b1a5c017SAndriy Gapon
3444b1a5c017SAndriy Gapon /*
3445b1a5c017SAndriy Gapon * XXX According to ACPI specification SCI_EN bit should be restored
3446b1a5c017SAndriy Gapon * by ACPI platform (BIOS, firmware) to its pre-sleep state.
3447b1a5c017SAndriy Gapon * Unfortunately some BIOSes fail to do that and that leads to
3448b1a5c017SAndriy Gapon * unexpected and serious consequences during wake up like a system
3449b1a5c017SAndriy Gapon * getting stuck in SMI handlers.
3450b1a5c017SAndriy Gapon * This hack is picked up from Linux, which claims that it follows
3451b1a5c017SAndriy Gapon * Windows behavior.
3452b1a5c017SAndriy Gapon */
3453b1a5c017SAndriy Gapon if (sleep_result == 1 && state != ACPI_STATE_S4)
3454b1a5c017SAndriy Gapon AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT);
3455b1a5c017SAndriy Gapon
3456b913a7d5SAndriy Gapon if (sleep_result == 1 && state == ACPI_STATE_S3) {
3457b913a7d5SAndriy Gapon /*
3458b913a7d5SAndriy Gapon * Prevent mis-interpretation of the wakeup by power button
3459b913a7d5SAndriy Gapon * as a request for power off.
3460b913a7d5SAndriy Gapon * Ideally we should post an appropriate wakeup event,
3461b913a7d5SAndriy Gapon * perhaps using acpi_event_power_button_wake or alike.
3462b913a7d5SAndriy Gapon *
3463b913a7d5SAndriy Gapon * Clearing of power button status after wakeup is mandated
3464b913a7d5SAndriy Gapon * by ACPI specification in section "Fixed Power Button".
3465b913a7d5SAndriy Gapon *
3466b913a7d5SAndriy Gapon * XXX As of ACPICA 20121114 AcpiGetEventStatus provides
3467b913a7d5SAndriy Gapon * status as 0/1 corressponding to inactive/active despite
3468b913a7d5SAndriy Gapon * its type being ACPI_EVENT_STATUS. In other words,
3469b913a7d5SAndriy Gapon * we should not test for ACPI_EVENT_FLAG_SET for time being.
3470b913a7d5SAndriy Gapon */
3471b913a7d5SAndriy Gapon if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON,
3472b913a7d5SAndriy Gapon &power_button_status)) && power_button_status != 0) {
3473b913a7d5SAndriy Gapon AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
3474b913a7d5SAndriy Gapon device_printf(sc->acpi_dev,
3475b913a7d5SAndriy Gapon "cleared fixed power button status\n");
3476b913a7d5SAndriy Gapon }
3477b913a7d5SAndriy Gapon }
3478b913a7d5SAndriy Gapon
3479f0a101b7SMitsuru IWASAKI intr_restore(intr);
3480f0a101b7SMitsuru IWASAKI
3481f0a101b7SMitsuru IWASAKI /* call acpi_wakeup_machdep() again with interrupt enabled */
3482f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 1);
3483f0a101b7SMitsuru IWASAKI
34840a15ff37SAndriy Gapon AcpiLeaveSleepStatePrep(state);
34850a15ff37SAndriy Gapon
3486f0a101b7SMitsuru IWASAKI if (sleep_result == -1)
3487a159c266SJung-uk Kim goto backout;
34886161544cSTakanori Watanabe
34896161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */
3490be2b1797SNate Lawson if (state == ACPI_STATE_S4)
3491964679ceSMitsuru IWASAKI AcpiEnable();
34926161544cSTakanori Watanabe } else {
34931df130f1SJung-uk Kim status = AcpiEnterSleepState(state);
349471804adcSJung-uk Kim intr_restore(intr);
34950a15ff37SAndriy Gapon AcpiLeaveSleepStatePrep(state);
3496be2b1797SNate Lawson if (ACPI_FAILURE(status)) {
3497be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
3498be2b1797SNate Lawson AcpiFormatException(status));
349933c70d41SAndriy Gapon goto backout;
350015e32d5dSMike Smith }
35016161544cSTakanori Watanabe }
350215e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT;
3503ece50487SMitsuru IWASAKI
350415e2f34fSNate Lawson /*
350515e2f34fSNate Lawson * Back out state according to how far along we got in the suspend
350615e2f34fSNate Lawson * process. This handles both the error and success cases.
350715e2f34fSNate Lawson */
350833c70d41SAndriy Gapon backout:
350927dca831SAndriy Gapon if (slp_state >= ACPI_SS_SLP_PREP)
351027dca831SAndriy Gapon resumeclock();
351115e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) {
351215e2f34fSNate Lawson acpi_wake_prep_walk(state);
351315e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0;
351415e2f34fSNate Lawson }
3515e3b56b66SMitsuru IWASAKI if (slp_state >= ACPI_SS_DEV_SUSPEND)
3516e3b56b66SMitsuru IWASAKI DEVICE_RESUME(root_bus);
3517f0a101b7SMitsuru IWASAKI if (slp_state >= ACPI_SS_SLP_PREP)
351815e2f34fSNate Lawson AcpiLeaveSleepState(state);
3519a0a15716SJung-uk Kim if (slp_state >= ACPI_SS_SLEPT) {
3520279be68bSAndriy Gapon #if defined(__i386__) || defined(__amd64__)
3521279be68bSAndriy Gapon /* NB: we are still using ACPI timecounter at this point. */
3522279be68bSAndriy Gapon resume_TSC();
3523279be68bSAndriy Gapon #endif
3524a0a15716SJung-uk Kim acpi_resync_clock(sc);
352515e2f34fSNate Lawson acpi_enable_fixed_events(sc);
3526a0a15716SJung-uk Kim }
3527337744d9SJung-uk Kim sc->acpi_next_sstate = 0;
352815e2f34fSNate Lawson
3529c6df6f53SWarner Losh bus_topo_unlock();
3530c66d2b38SJung-uk Kim
3531fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
3532fdce57a0SJohn Baldwin thread_lock(curthread);
3533fdce57a0SJohn Baldwin sched_unbind(curthread);
3534fdce57a0SJohn Baldwin thread_unlock(curthread);
3535fdce57a0SJohn Baldwin #else
353636a483bbSJung-uk Kim if (smp_started) {
3537c66d2b38SJung-uk Kim thread_lock(curthread);
3538c66d2b38SJung-uk Kim sched_unbind(curthread);
3539c66d2b38SJung-uk Kim thread_unlock(curthread);
354036a483bbSJung-uk Kim }
3541fdce57a0SJohn Baldwin #endif
3542c66d2b38SJung-uk Kim
3543f1084587SKonstantin Belousov resume_all_fs();
354485f95fffSAndriy Gapon resume_all_proc();
354585f95fffSAndriy Gapon
35464a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_resume);
35474a8fa6feSJung-uk Kim
3548d42f0ad8SJung-uk Kim /* Allow another sleep request after a while. */
3549fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3550d42f0ad8SJung-uk Kim
3551d42f0ad8SJung-uk Kim /* Run /etc/rc.resume after we are back. */
3552d42f0ad8SJung-uk Kim if (devctl_process_running())
3553d42f0ad8SJung-uk Kim acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
3554d42f0ad8SJung-uk Kim
35550ae55423SMike Smith return_ACPI_STATUS (status);
355615e32d5dSMike Smith }
355715e32d5dSMike Smith
3558a0a15716SJung-uk Kim static void
acpi_resync_clock(struct acpi_softc * sc)35590755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc)
35600755473bSJung-uk Kim {
35610755473bSJung-uk Kim
35620755473bSJung-uk Kim /*
35630755473bSJung-uk Kim * Warm up timecounter again and reset system clock.
35640755473bSJung-uk Kim */
35650755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter);
35660755473bSJung-uk Kim inittodr(time_second + sc->acpi_sleep_delay);
35670755473bSJung-uk Kim }
35680755473bSJung-uk Kim
3569e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
3570e8b4d56eSNate Lawson int
acpi_wake_set_enable(device_t dev,int enable)3571e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
3572e8b4d56eSNate Lawson {
3573e8b4d56eSNate Lawson struct acpi_prw_data prw;
3574e8b4d56eSNate Lawson ACPI_STATUS status;
3575e8b4d56eSNate Lawson int flags;
3576e8b4d56eSNate Lawson
3577d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */
35782be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
3579e8b4d56eSNate Lawson return (ENXIO);
3580e8b4d56eSNate Lawson
3581d4b9ff91SNate Lawson flags = acpi_get_flags(dev);
3582e8b4d56eSNate Lawson if (enable) {
35835a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
35845a77b11bSJung-uk Kim ACPI_GPE_ENABLE);
3585e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) {
3586e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n");
3587e8b4d56eSNate Lawson return (ENXIO);
3588e8b4d56eSNate Lawson }
3589d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
3590e8b4d56eSNate Lawson } else {
35915a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
35925a77b11bSJung-uk Kim ACPI_GPE_DISABLE);
3593e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) {
3594e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n");
3595e8b4d56eSNate Lawson return (ENXIO);
3596e8b4d56eSNate Lawson }
3597d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
3598e8b4d56eSNate Lawson }
3599e8b4d56eSNate Lawson
3600e8b4d56eSNate Lawson return (0);
3601e8b4d56eSNate Lawson }
3602e8b4d56eSNate Lawson
3603d4b9ff91SNate Lawson static int
acpi_wake_sleep_prep(ACPI_HANDLE handle,int sstate)3604d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
3605e8b4d56eSNate Lawson {
3606e8b4d56eSNate Lawson struct acpi_prw_data prw;
3607d4b9ff91SNate Lawson device_t dev;
3608e8b4d56eSNate Lawson
3609d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */
3610e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0)
3611e8b4d56eSNate Lawson return (ENXIO);
3612d4b9ff91SNate Lawson dev = acpi_get_device(handle);
3613e8b4d56eSNate Lawson
3614e8b4d56eSNate Lawson /*
3615d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power)
3616d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be
3617d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and
3618d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources
3619d4b9ff91SNate Lawson * and set _PSW.
3620e8b4d56eSNate Lawson */
3621d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) {
36225a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
3623e8b4d56eSNate Lawson if (bootverbose)
3624d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
3625d4b9ff91SNate Lawson acpi_name(handle), sstate);
3626d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
3627d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1);
3628d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1);
3629d4b9ff91SNate Lawson if (bootverbose)
3630d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n",
3631d4b9ff91SNate Lawson acpi_name(handle), sstate);
3632d4b9ff91SNate Lawson }
3633cc85c78cSNate Lawson
3634cc85c78cSNate Lawson return (0);
3635e8b4d56eSNate Lawson }
3636e8b4d56eSNate Lawson
3637d4b9ff91SNate Lawson static int
acpi_wake_run_prep(ACPI_HANDLE handle,int sstate)3638d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
3639cc85c78cSNate Lawson {
3640cc85c78cSNate Lawson struct acpi_prw_data prw;
3641d4b9ff91SNate Lawson device_t dev;
3642cc85c78cSNate Lawson
3643cc85c78cSNate Lawson /*
3644d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return
3645d4b9ff91SNate Lawson * now if the user didn't enable this device for wake.
3646cc85c78cSNate Lawson */
3647d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0)
3648d4b9ff91SNate Lawson return (ENXIO);
3649d4b9ff91SNate Lawson dev = acpi_get_device(handle);
3650d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
3651d4b9ff91SNate Lawson return (0);
3652cc85c78cSNate Lawson
3653d4b9ff91SNate Lawson /*
3654d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was
3655d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled,
3656d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used.
3657d4b9ff91SNate Lawson */
3658d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) {
36595a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
3660d4b9ff91SNate Lawson if (bootverbose)
3661d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
3662d4b9ff91SNate Lawson } else {
3663d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0);
3664d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0);
3665d4b9ff91SNate Lawson if (bootverbose)
3666d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n",
3667d4b9ff91SNate Lawson acpi_name(handle));
3668d4b9ff91SNate Lawson }
3669d4b9ff91SNate Lawson
3670e8b4d56eSNate Lawson return (0);
3671e8b4d56eSNate Lawson }
3672e8b4d56eSNate Lawson
3673e8b4d56eSNate Lawson static ACPI_STATUS
acpi_wake_prep(ACPI_HANDLE handle,UINT32 level,void * context,void ** status)3674d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
3675e8b4d56eSNate Lawson {
3676d4b9ff91SNate Lawson int sstate;
3677e8b4d56eSNate Lawson
3678d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */
3679d4b9ff91SNate Lawson sstate = *(int *)context;
3680d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning)
3681d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate);
3682d4b9ff91SNate Lawson else
3683d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate);
3684e8b4d56eSNate Lawson return (AE_OK);
3685e8b4d56eSNate Lawson }
3686e8b4d56eSNate Lawson
3687d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
3688e8b4d56eSNate Lawson static int
acpi_wake_prep_walk(int sstate)3689d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
3690e8b4d56eSNate Lawson {
3691e8b4d56eSNate Lawson ACPI_HANDLE sb_handle;
3692e8b4d56eSNate Lawson
3693e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
3694d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
36952272d050SJung-uk Kim acpi_wake_prep, NULL, &sstate, NULL);
3696e8b4d56eSNate Lawson return (0);
3697e8b4d56eSNate Lawson }
3698e8b4d56eSNate Lawson
369988a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
370088a79fc0SNate Lawson static int
acpi_wake_sysctl_walk(device_t dev)370188a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
370288a79fc0SNate Lawson {
370388a79fc0SNate Lawson int error, i, numdevs;
370488a79fc0SNate Lawson device_t *devlist;
370588a79fc0SNate Lawson device_t child;
3706d4b9ff91SNate Lawson ACPI_STATUS status;
370788a79fc0SNate Lawson
370888a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs);
37091c9ec538SNate Lawson if (error != 0 || numdevs == 0) {
37101c9ec538SNate Lawson if (numdevs == 0)
37111c9ec538SNate Lawson free(devlist, M_TEMP);
371288a79fc0SNate Lawson return (error);
37131c9ec538SNate Lawson }
371488a79fc0SNate Lawson for (i = 0; i < numdevs; i++) {
371588a79fc0SNate Lawson child = devlist[i];
3716d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child);
371788a79fc0SNate Lawson if (!device_is_attached(child))
371888a79fc0SNate Lawson continue;
3719d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
3720d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) {
372188a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
372288a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
37237029da5cSPawel Biernacki "wake", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, child, 0,
372488a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system");
372588a79fc0SNate Lawson }
372688a79fc0SNate Lawson }
372788a79fc0SNate Lawson free(devlist, M_TEMP);
372888a79fc0SNate Lawson
372988a79fc0SNate Lawson return (0);
373088a79fc0SNate Lawson }
373188a79fc0SNate Lawson
373288a79fc0SNate Lawson /* Enable or disable wake from userland. */
373388a79fc0SNate Lawson static int
acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)373488a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
373588a79fc0SNate Lawson {
373688a79fc0SNate Lawson int enable, error;
373788a79fc0SNate Lawson device_t dev;
373888a79fc0SNate Lawson
373988a79fc0SNate Lawson dev = (device_t)arg1;
3740d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
374188a79fc0SNate Lawson
374288a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req);
374388a79fc0SNate Lawson if (error != 0 || req->newptr == NULL)
374488a79fc0SNate Lawson return (error);
374588a79fc0SNate Lawson if (enable != 0 && enable != 1)
374688a79fc0SNate Lawson return (EINVAL);
374788a79fc0SNate Lawson
374888a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable));
374988a79fc0SNate Lawson }
375088a79fc0SNate Lawson
3751e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
3752d4b9ff91SNate Lawson int
acpi_parse_prw(ACPI_HANDLE h,struct acpi_prw_data * prw)3753e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
3754e8b4d56eSNate Lawson {
3755e8b4d56eSNate Lawson ACPI_STATUS status;
3756e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer;
3757e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2;
3758d4b9ff91SNate Lawson int error, i, power_count;
3759e8b4d56eSNate Lawson
3760e8b4d56eSNate Lawson if (h == NULL || prw == NULL)
3761e8b4d56eSNate Lawson return (EINVAL);
3762e8b4d56eSNate Lawson
3763e8b4d56eSNate Lawson /*
3764e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the
3765e8b4d56eSNate Lawson * ability to wake the system from a sleeping state.
3766e8b4d56eSNate Lawson */
3767e8b4d56eSNate Lawson error = EINVAL;
3768e8b4d56eSNate Lawson prw_buffer.Pointer = NULL;
3769e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
3770e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
3771e8b4d56eSNate Lawson if (ACPI_FAILURE(status))
3772e8b4d56eSNate Lawson return (ENOENT);
3773e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer;
3774e8b4d56eSNate Lawson if (res == NULL)
3775e8b4d56eSNate Lawson return (ENOENT);
3776e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2))
3777e8b4d56eSNate Lawson goto out;
3778e8b4d56eSNate Lawson
3779e8b4d56eSNate Lawson /*
3780e8b4d56eSNate Lawson * Element 1 of the _PRW object:
3781e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still
3782e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must
3783e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value.
3784e8b4d56eSNate Lawson */
3785e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
3786e8b4d56eSNate Lawson goto out;
3787e8b4d56eSNate Lawson
3788e8b4d56eSNate Lawson /*
3789e8b4d56eSNate Lawson * Element 0 of the _PRW object:
3790e8b4d56eSNate Lawson */
3791e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) {
3792e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER:
3793e8b4d56eSNate Lawson /*
3794e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this
3795e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the
3796e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is
3797e8b4d56eSNate Lawson * enabled for the wake event.
3798e8b4d56eSNate Lawson */
3799e8b4d56eSNate Lawson prw->gpe_handle = NULL;
3800e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value;
3801e8b4d56eSNate Lawson error = 0;
3802e8b4d56eSNate Lawson break;
3803e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE:
3804e8b4d56eSNate Lawson /*
3805e8b4d56eSNate Lawson * If the data type of this package element is a package, then this
3806e8b4d56eSNate Lawson * _PRW package element is itself a package containing two
3807e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block
3808e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake
3809e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit
3810e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the
3811e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for
3812e8b4d56eSNate Lawson * the wake event.
3813e8b4d56eSNate Lawson *
3814e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form:
3815e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2}
3816e8b4d56eSNate Lawson */
3817e8b4d56eSNate Lawson res2 = &res->Package.Elements[0];
3818e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2))
3819e8b4d56eSNate Lawson goto out;
3820e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3821e8b4d56eSNate Lawson if (prw->gpe_handle == NULL)
3822e8b4d56eSNate Lawson goto out;
3823e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3824e8b4d56eSNate Lawson goto out;
3825e8b4d56eSNate Lawson error = 0;
3826e8b4d56eSNate Lawson break;
3827e8b4d56eSNate Lawson default:
3828e8b4d56eSNate Lawson goto out;
3829e8b4d56eSNate Lawson }
3830e8b4d56eSNate Lawson
3831d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */
3832d4b9ff91SNate Lawson power_count = res->Package.Count - 2;
3833d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) {
3834d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h));
3835d4b9ff91SNate Lawson power_count = 0;
3836d4b9ff91SNate Lawson }
3837d4b9ff91SNate Lawson prw->power_res_count = power_count;
3838d4b9ff91SNate Lawson for (i = 0; i < power_count; i++)
3839d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i];
3840e8b4d56eSNate Lawson
3841e8b4d56eSNate Lawson out:
3842e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL)
3843e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer);
3844e8b4d56eSNate Lawson return (error);
3845e8b4d56eSNate Lawson }
3846e8b4d56eSNate Lawson
384715e32d5dSMike Smith /*
384815e32d5dSMike Smith * ACPI Event Handlers
384915e32d5dSMike Smith */
385015e32d5dSMike Smith
385115e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
385215e32d5dSMike Smith
385315e32d5dSMike Smith static void
acpi_system_eventhandler_sleep(void * arg,int state)385415e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
385515e32d5dSMike Smith {
38562d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg;
385700a30448SNate Lawson int ret;
3858bee4aa4aSNate Lawson
3859b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
386015e32d5dSMike Smith
3861a0e73a12SJung-uk Kim /* Check if button action is disabled or unknown. */
3862a0e73a12SJung-uk Kim if (state == ACPI_STATE_UNKNOWN)
3863813d6dcaSNate Lawson return;
3864813d6dcaSNate Lawson
386500a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */
38662d0c82e8SJung-uk Kim ret = acpi_ReqSleepState(sc, state);
386700a30448SNate Lawson if (ret != 0)
38682d0c82e8SJung-uk Kim device_printf(sc->acpi_dev,
38692d0c82e8SJung-uk Kim "request to enter state S%d failed (err %d)\n", state, ret);
3870bee4aa4aSNate Lawson
38710ae55423SMike Smith return_VOID;
387215e32d5dSMike Smith }
387315e32d5dSMike Smith
387415e32d5dSMike Smith static void
acpi_system_eventhandler_wakeup(void * arg,int state)387515e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
387615e32d5dSMike Smith {
3877bee4aa4aSNate Lawson
3878b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
38790ae55423SMike Smith
3880bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */
3881cb9b0d80SMike Smith
38820ae55423SMike Smith return_VOID;
388315e32d5dSMike Smith }
388415e32d5dSMike Smith
388515e32d5dSMike Smith /*
388615e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
388715e32d5dSMike Smith */
3888b5854f5fSJung-uk Kim static void
acpi_invoke_sleep_eventhandler(void * context)3889b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context)
3890b5854f5fSJung-uk Kim {
3891b5854f5fSJung-uk Kim
3892b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context);
3893b5854f5fSJung-uk Kim }
3894b5854f5fSJung-uk Kim
3895b5854f5fSJung-uk Kim static void
acpi_invoke_wake_eventhandler(void * context)3896b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context)
3897b5854f5fSJung-uk Kim {
3898b5854f5fSJung-uk Kim
3899b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context);
3900b5854f5fSJung-uk Kim }
3901b5854f5fSJung-uk Kim
390215e32d5dSMike Smith UINT32
acpi_event_power_button_sleep(void * context)390333febf93SNate Lawson acpi_event_power_button_sleep(void *context)
390415e32d5dSMike Smith {
3905f41ef9d8SColin Percival #if defined(__amd64__) || defined(__i386__)
390615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context;
3907f41ef9d8SColin Percival #else
3908f41ef9d8SColin Percival (void)context;
3909f41ef9d8SColin Percival #endif
391015e32d5dSMike Smith
3911b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
39120ae55423SMike Smith
3913f41ef9d8SColin Percival #if defined(__amd64__) || defined(__i386__)
3914b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3915b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx)))
3916b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3917f41ef9d8SColin Percival #else
3918f41ef9d8SColin Percival shutdown_nice(RB_POWEROFF);
3919f41ef9d8SColin Percival #endif
3920f41ef9d8SColin Percival
3921b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED);
392215e32d5dSMike Smith }
392315e32d5dSMike Smith
392415e32d5dSMike Smith UINT32
acpi_event_power_button_wake(void * context)392533febf93SNate Lawson acpi_event_power_button_wake(void *context)
392615e32d5dSMike Smith {
392715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context;
392815e32d5dSMike Smith
3929b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
39300ae55423SMike Smith
3931b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3932b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx)))
3933b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3934b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED);
393515e32d5dSMike Smith }
393615e32d5dSMike Smith
393715e32d5dSMike Smith UINT32
acpi_event_sleep_button_sleep(void * context)393833febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
393915e32d5dSMike Smith {
394015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context;
394115e32d5dSMike Smith
3942b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
39430ae55423SMike Smith
3944b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3945b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx)))
3946b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3947b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED);
394815e32d5dSMike Smith }
394915e32d5dSMike Smith
395015e32d5dSMike Smith UINT32
acpi_event_sleep_button_wake(void * context)395133febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
395215e32d5dSMike Smith {
395315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context;
395415e32d5dSMike Smith
3955b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
39560ae55423SMike Smith
3957b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3958b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx)))
3959b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3960b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED);
396115e32d5dSMike Smith }
396215e32d5dSMike Smith
396315e32d5dSMike Smith /*
3964bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only
3965bee4aa4aSNate Lawson * use this for single-threaded callers.
396615e32d5dSMike Smith */
396715e32d5dSMike Smith char *
acpi_name(ACPI_HANDLE handle)396815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
396915e32d5dSMike Smith {
3970bee4aa4aSNate Lawson ACPI_BUFFER buf;
3971bee4aa4aSNate Lawson static char data[256];
397215e32d5dSMike Smith
3973bee4aa4aSNate Lawson buf.Length = sizeof(data);
3974bee4aa4aSNate Lawson buf.Pointer = data;
3975cb9b0d80SMike Smith
39760a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3977bee4aa4aSNate Lawson return (data);
3978bee4aa4aSNate Lawson return ("(unknown)");
397915e32d5dSMike Smith }
398015e32d5dSMike Smith
398115e32d5dSMike Smith /*
398215e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various
398315e32d5dSMike Smith * parts of the namespace.
398415e32d5dSMike Smith */
398515e32d5dSMike Smith int
acpi_avoid(ACPI_HANDLE handle)398615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
398715e32d5dSMike Smith {
39883c600cfbSJohn Baldwin char *cp, *env, *np;
398915e32d5dSMike Smith int len;
399015e32d5dSMike Smith
399115e32d5dSMike Smith np = acpi_name(handle);
399215e32d5dSMike Smith if (*np == '\\')
399315e32d5dSMike Smith np++;
39942be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.avoid")) == NULL)
399515e32d5dSMike Smith return (0);
399615e32d5dSMike Smith
3997be2b1797SNate Lawson /* Scan the avoid list checking for a match */
39983c600cfbSJohn Baldwin cp = env;
399915e32d5dSMike Smith for (;;) {
4000bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp))
400115e32d5dSMike Smith cp++;
400215e32d5dSMike Smith if (*cp == 0)
400315e32d5dSMike Smith break;
400415e32d5dSMike Smith len = 0;
4005bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len]))
400615e32d5dSMike Smith len++;
4007d786139cSMaxime Henrion if (!strncmp(cp, np, len)) {
40083c600cfbSJohn Baldwin freeenv(env);
40090ae55423SMike Smith return(1);
4010d786139cSMaxime Henrion }
40110ae55423SMike Smith cp += len;
40120ae55423SMike Smith }
40133c600cfbSJohn Baldwin freeenv(env);
4014be2b1797SNate Lawson
40150ae55423SMike Smith return (0);
40160ae55423SMike Smith }
40170ae55423SMike Smith
40180ae55423SMike Smith /*
40190ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components.
40200ae55423SMike Smith */
40210ae55423SMike Smith int
acpi_disabled(char * subsys)40220ae55423SMike Smith acpi_disabled(char *subsys)
40230ae55423SMike Smith {
402478689b15SMaxime Henrion char *cp, *env;
40250ae55423SMike Smith int len;
40260ae55423SMike Smith
40272be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.disabled")) == NULL)
40280ae55423SMike Smith return (0);
40293184cf5aSNate Lawson if (strcmp(env, "all") == 0) {
403078689b15SMaxime Henrion freeenv(env);
40310ae55423SMike Smith return (1);
4032d786139cSMaxime Henrion }
40330ae55423SMike Smith
40343184cf5aSNate Lawson /* Scan the disable list, checking for a match. */
403578689b15SMaxime Henrion cp = env;
40360ae55423SMike Smith for (;;) {
40373184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp))
40380ae55423SMike Smith cp++;
40393184cf5aSNate Lawson if (*cp == '\0')
40400ae55423SMike Smith break;
40410ae55423SMike Smith len = 0;
40423184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len]))
40430ae55423SMike Smith len++;
40443184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) {
404578689b15SMaxime Henrion freeenv(env);
404615e32d5dSMike Smith return (1);
4047d786139cSMaxime Henrion }
404815e32d5dSMike Smith cp += len;
404915e32d5dSMike Smith }
405078689b15SMaxime Henrion freeenv(env);
4051be2b1797SNate Lawson
405215e32d5dSMike Smith return (0);
405315e32d5dSMike Smith }
405415e32d5dSMike Smith
405564de8019SJohn Baldwin static void
acpi_lookup(void * arg,const char * name,device_t * dev)405664de8019SJohn Baldwin acpi_lookup(void *arg, const char *name, device_t *dev)
405764de8019SJohn Baldwin {
405864de8019SJohn Baldwin ACPI_HANDLE handle;
405964de8019SJohn Baldwin
406064de8019SJohn Baldwin if (*dev != NULL)
406164de8019SJohn Baldwin return;
406264de8019SJohn Baldwin
406364de8019SJohn Baldwin /*
406464de8019SJohn Baldwin * Allow any handle name that is specified as an absolute path and
406564de8019SJohn Baldwin * starts with '\'. We could restrict this to \_SB and friends,
406664de8019SJohn Baldwin * but see acpi_probe_children() for notes on why we scan the entire
406764de8019SJohn Baldwin * namespace for devices.
406864de8019SJohn Baldwin *
406964de8019SJohn Baldwin * XXX: The pathname argument to AcpiGetHandle() should be fixed to
407064de8019SJohn Baldwin * be const.
407164de8019SJohn Baldwin */
407264de8019SJohn Baldwin if (name[0] != '\\')
407364de8019SJohn Baldwin return;
407464de8019SJohn Baldwin if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name),
407564de8019SJohn Baldwin &handle)))
407664de8019SJohn Baldwin return;
407764de8019SJohn Baldwin *dev = acpi_get_device(handle);
407864de8019SJohn Baldwin }
407964de8019SJohn Baldwin
408015e32d5dSMike Smith /*
408115e32d5dSMike Smith * Control interface.
408215e32d5dSMike Smith *
40830ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual
4084be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the
4085be2b1797SNate Lawson * register/deregister interface to make their handlers visible.
408615e32d5dSMike Smith */
40870ae55423SMike Smith struct acpi_ioctl_hook
40880ae55423SMike Smith {
40890ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link;
40900ae55423SMike Smith u_long cmd;
4091be2b1797SNate Lawson acpi_ioctl_fn fn;
40920ae55423SMike Smith void *arg;
40930ae55423SMike Smith };
40940ae55423SMike Smith
40950ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
40960ae55423SMike Smith static int acpi_ioctl_hooks_initted;
40970ae55423SMike Smith
40980ae55423SMike Smith int
acpi_register_ioctl(u_long cmd,acpi_ioctl_fn fn,void * arg)4099be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
41000ae55423SMike Smith {
41010ae55423SMike Smith struct acpi_ioctl_hook *hp;
41020ae55423SMike Smith
41030ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
41040ae55423SMike Smith return (ENOMEM);
41050ae55423SMike Smith hp->cmd = cmd;
41060ae55423SMike Smith hp->fn = fn;
41070ae55423SMike Smith hp->arg = arg;
410815e2f34fSNate Lawson
410915e2f34fSNate Lawson ACPI_LOCK(acpi);
41100ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) {
41110ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks);
41120ae55423SMike Smith acpi_ioctl_hooks_initted = 1;
41130ae55423SMike Smith }
41140ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
411515e2f34fSNate Lawson ACPI_UNLOCK(acpi);
411615e2f34fSNate Lawson
41170ae55423SMike Smith return (0);
41180ae55423SMike Smith }
41190ae55423SMike Smith
41200ae55423SMike Smith void
acpi_deregister_ioctl(u_long cmd,acpi_ioctl_fn fn)4121be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
41220ae55423SMike Smith {
41230ae55423SMike Smith struct acpi_ioctl_hook *hp;
41240ae55423SMike Smith
412515e2f34fSNate Lawson ACPI_LOCK(acpi);
41260ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
412715e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn)
41280ae55423SMike Smith break;
41290ae55423SMike Smith
41300ae55423SMike Smith if (hp != NULL) {
41310ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
41320ae55423SMike Smith free(hp, M_ACPIDEV);
41330ae55423SMike Smith }
413415e2f34fSNate Lawson ACPI_UNLOCK(acpi);
41350ae55423SMike Smith }
41360ae55423SMike Smith
413715e32d5dSMike Smith static int
acpiopen(struct cdev * dev,int flag,int fmt,struct thread * td)413800b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
413915e32d5dSMike Smith {
414015e32d5dSMike Smith return (0);
414115e32d5dSMike Smith }
414215e32d5dSMike Smith
414315e32d5dSMike Smith static int
acpiclose(struct cdev * dev,int flag,int fmt,struct thread * td)414400b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
414515e32d5dSMike Smith {
414615e32d5dSMike Smith return (0);
414715e32d5dSMike Smith }
414815e32d5dSMike Smith
414915e32d5dSMike Smith static int
acpiioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)415000b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
415115e32d5dSMike Smith {
415215e32d5dSMike Smith struct acpi_softc *sc;
41530ae55423SMike Smith struct acpi_ioctl_hook *hp;
4154bee4aa4aSNate Lawson int error, state;
415515e32d5dSMike Smith
4156a2e35898SDavid E. O'Brien error = 0;
4157a2e35898SDavid E. O'Brien hp = NULL;
415815e32d5dSMike Smith sc = dev->si_drv1;
415915e32d5dSMike Smith
41600ae55423SMike Smith /*
41610ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers.
41620ae55423SMike Smith */
416315e2f34fSNate Lawson ACPI_LOCK(acpi);
4164bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted)
41650ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
4166bee4aa4aSNate Lawson if (hp->cmd == cmd)
4167bee4aa4aSNate Lawson break;
41680ae55423SMike Smith }
416915e2f34fSNate Lawson ACPI_UNLOCK(acpi);
4170bee4aa4aSNate Lawson if (hp)
4171bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg));
41720ae55423SMike Smith
41730ae55423SMike Smith /*
4174a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user.
4175a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information.
4176a89fcf28STakanori Watanabe * Not changing system behavior.
4177a89fcf28STakanori Watanabe */
4178be2b1797SNate Lawson if ((flag & FWRITE) == 0)
4179be2b1797SNate Lawson return (EPERM);
4180a89fcf28STakanori Watanabe
4181be2b1797SNate Lawson /* Core system ioctls. */
418215e32d5dSMike Smith switch (cmd) {
418300a30448SNate Lawson case ACPIIO_REQSLPSTATE:
418400a30448SNate Lawson state = *(int *)addr;
418500a30448SNate Lawson if (state != ACPI_STATE_S5)
4186a0e73a12SJung-uk Kim return (acpi_ReqSleepState(sc, state));
4187a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
4188a0e73a12SJung-uk Kim error = EOPNOTSUPP;
418900a30448SNate Lawson break;
419000a30448SNate Lawson case ACPIIO_ACKSLPSTATE:
419100a30448SNate Lawson error = *(int *)addr;
419200a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error);
419300a30448SNate Lawson break;
419400a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */
419515e32d5dSMike Smith state = *(int *)addr;
4196a0e73a12SJung-uk Kim if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
4197a0e73a12SJung-uk Kim return (EINVAL);
4198a0e73a12SJung-uk Kim if (!acpi_sleep_states[state])
4199a0e73a12SJung-uk Kim return (EOPNOTSUPP);
4200a0e73a12SJung-uk Kim if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
4201a0e73a12SJung-uk Kim error = ENXIO;
420215e32d5dSMike Smith break;
420315e32d5dSMike Smith default:
4204bee4aa4aSNate Lawson error = ENXIO;
420515e32d5dSMike Smith break;
420615e32d5dSMike Smith }
4207917d44c8SMitsuru IWASAKI
420815e32d5dSMike Smith return (error);
420915e32d5dSMike Smith }
421015e32d5dSMike Smith
42111d073b1dSJohn Baldwin static int
acpi_sname2sstate(const char * sname)4212a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname)
4213a0e73a12SJung-uk Kim {
4214a0e73a12SJung-uk Kim int sstate;
4215a0e73a12SJung-uk Kim
4216a0e73a12SJung-uk Kim if (toupper(sname[0]) == 'S') {
4217a0e73a12SJung-uk Kim sstate = sname[1] - '0';
4218a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
4219a0e73a12SJung-uk Kim sname[2] == '\0')
4220a0e73a12SJung-uk Kim return (sstate);
4221a0e73a12SJung-uk Kim } else if (strcasecmp(sname, "NONE") == 0)
4222a0e73a12SJung-uk Kim return (ACPI_STATE_UNKNOWN);
4223a0e73a12SJung-uk Kim return (-1);
4224a0e73a12SJung-uk Kim }
4225a0e73a12SJung-uk Kim
4226a0e73a12SJung-uk Kim static const char *
acpi_sstate2sname(int sstate)4227a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate)
4228a0e73a12SJung-uk Kim {
4229a0e73a12SJung-uk Kim static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
4230a0e73a12SJung-uk Kim
4231a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
4232a0e73a12SJung-uk Kim return (snames[sstate]);
4233a0e73a12SJung-uk Kim else if (sstate == ACPI_STATE_UNKNOWN)
4234a0e73a12SJung-uk Kim return ("NONE");
4235a0e73a12SJung-uk Kim return (NULL);
4236a0e73a12SJung-uk Kim }
4237a0e73a12SJung-uk Kim
4238a0e73a12SJung-uk Kim static int
acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)4239d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
4240d75de536SMitsuru IWASAKI {
4241d75de536SMitsuru IWASAKI int error;
4242bee4aa4aSNate Lawson struct sbuf sb;
4243a0e73a12SJung-uk Kim UINT8 state;
4244d75de536SMitsuru IWASAKI
4245bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
4246a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
4247a0e73a12SJung-uk Kim if (acpi_sleep_states[state])
4248a0e73a12SJung-uk Kim sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
4249bee4aa4aSNate Lawson sbuf_trim(&sb);
4250bee4aa4aSNate Lawson sbuf_finish(&sb);
4251bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
4252bee4aa4aSNate Lawson sbuf_delete(&sb);
4253d75de536SMitsuru IWASAKI return (error);
4254d75de536SMitsuru IWASAKI }
4255d75de536SMitsuru IWASAKI
4256d75de536SMitsuru IWASAKI static int
acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)42571d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
42581d073b1dSJohn Baldwin {
42591d073b1dSJohn Baldwin char sleep_state[10];
4260a0e73a12SJung-uk Kim int error, new_state, old_state;
42611d073b1dSJohn Baldwin
4262a0e73a12SJung-uk Kim old_state = *(int *)oidp->oid_arg1;
4263a0e73a12SJung-uk Kim strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
42641d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
42651d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) {
4266a0e73a12SJung-uk Kim new_state = acpi_sname2sstate(sleep_state);
4267a0e73a12SJung-uk Kim if (new_state < ACPI_STATE_S1)
4268a0e73a12SJung-uk Kim return (EINVAL);
426954b43614SJung-uk Kim if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
4270a0e73a12SJung-uk Kim return (EOPNOTSUPP);
4271be2b1797SNate Lawson if (new_state != old_state)
4272a0e73a12SJung-uk Kim *(int *)oidp->oid_arg1 = new_state;
42731d073b1dSJohn Baldwin }
42741d073b1dSJohn Baldwin return (error);
42751d073b1dSJohn Baldwin }
42761d073b1dSJohn Baldwin
42779b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
42789b937d48SNate Lawson void
acpi_UserNotify(const char * subsystem,ACPI_HANDLE h,uint8_t notify)42799b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
42809b937d48SNate Lawson {
42819b937d48SNate Lawson char notify_buf[16];
42829b937d48SNate Lawson ACPI_BUFFER handle_buf;
42839b937d48SNate Lawson ACPI_STATUS status;
42849b937d48SNate Lawson
42859b937d48SNate Lawson if (subsystem == NULL)
42869b937d48SNate Lawson return;
42879b937d48SNate Lawson
42889b937d48SNate Lawson handle_buf.Pointer = NULL;
42899b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER;
42900594dadeSJung-uk Kim status = AcpiNsHandleToPathname(h, &handle_buf, FALSE);
42919b937d48SNate Lawson if (ACPI_FAILURE(status))
42929b937d48SNate Lawson return;
42939b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
42949b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
42959b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer);
42969b937d48SNate Lawson }
42979b937d48SNate Lawson
429815e32d5dSMike Smith #ifdef ACPI_DEBUG
42990ae55423SMike Smith /*
43000ae55423SMike Smith * Support for parsing debug options from the kernel environment.
43010ae55423SMike Smith *
43020ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
43030ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and
43040ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by
43050ae55423SMike Smith * prefixing the bit name with !.
43060ae55423SMike Smith */
430715e32d5dSMike Smith struct debugtag
430815e32d5dSMike Smith {
430915e32d5dSMike Smith char *name;
431015e32d5dSMike Smith UINT32 value;
431115e32d5dSMike Smith };
431215e32d5dSMike Smith
431315e32d5dSMike Smith static struct debugtag dbg_layer[] = {
4314c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES},
4315c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE},
4316c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS},
4317c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES},
4318c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE},
4319c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER},
4320c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER},
4321c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER},
4322c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES},
4323d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER},
43244c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES},
4325d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER},
4326297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS},
43274c1cdee6SMike Smith
4328c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER},
4329c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY},
43303184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS},
4331c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON},
43323184cf5aSNate Lawson {"ACPI_EC", ACPI_EC},
43333184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN},
43343184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES},
43354c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR},
4336cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL},
43373184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER},
4338b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS},
433915e32d5dSMike Smith {NULL, 0}
434015e32d5dSMike Smith };
434115e32d5dSMike Smith
434215e32d5dSMike Smith static struct debugtag dbg_level[] = {
43439501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT},
43444c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT},
43459501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO},
4346cc6455afSJung-uk Kim {"ACPI_LV_REPAIR", ACPI_LV_REPAIR},
43474c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS},
4348d62ab2f4SMitsuru IWASAKI
4349d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */
4350297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES},
43514c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE},
43524c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD},
4353d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH},
43544c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC},
43554c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES},
43564c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION},
43574c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD},
43584c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES},
43594c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES},
43604c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS},
43614c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES},
43624c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS},
43634c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE},
4364d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1},
4365d62ab2f4SMitsuru IWASAKI
4366d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */
4367d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS},
4368d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS},
4369d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS},
4370d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2},
43714c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL},
4372d62ab2f4SMitsuru IWASAKI
4373d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
4374d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX},
4375d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS},
4376d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO},
4377d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS},
4378d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3},
4379d62ab2f4SMitsuru IWASAKI
4380d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */
438198479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
438298479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO},
438398479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES},
438498479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS},
438598479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE},
438615e32d5dSMike Smith {NULL, 0}
438715e32d5dSMike Smith };
438815e32d5dSMike Smith
438915e32d5dSMike Smith static void
acpi_parse_debug(char * cp,struct debugtag * tag,UINT32 * flag)439015e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
439115e32d5dSMike Smith {
439215e32d5dSMike Smith char *ep;
439315e32d5dSMike Smith int i, l;
43940ae55423SMike Smith int set;
439515e32d5dSMike Smith
439615e32d5dSMike Smith while (*cp) {
439715e32d5dSMike Smith if (isspace(*cp)) {
439815e32d5dSMike Smith cp++;
439915e32d5dSMike Smith continue;
440015e32d5dSMike Smith }
440115e32d5dSMike Smith ep = cp;
440215e32d5dSMike Smith while (*ep && !isspace(*ep))
440315e32d5dSMike Smith ep++;
44040ae55423SMike Smith if (*cp == '!') {
44050ae55423SMike Smith set = 0;
44060ae55423SMike Smith cp++;
44070ae55423SMike Smith if (cp == ep)
44080ae55423SMike Smith continue;
44090ae55423SMike Smith } else {
44100ae55423SMike Smith set = 1;
44110ae55423SMike Smith }
441215e32d5dSMike Smith l = ep - cp;
441315e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) {
441415e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) {
4415be2b1797SNate Lawson if (set)
441615e32d5dSMike Smith *flag |= tag[i].value;
4417be2b1797SNate Lawson else
44180ae55423SMike Smith *flag &= ~tag[i].value;
441915e32d5dSMike Smith }
442015e32d5dSMike Smith }
442115e32d5dSMike Smith cp = ep;
442215e32d5dSMike Smith }
442315e32d5dSMike Smith }
442415e32d5dSMike Smith
442515e32d5dSMike Smith static void
acpi_set_debugging(void * junk)44262a4ac806SMike Smith acpi_set_debugging(void *junk)
442715e32d5dSMike Smith {
44287e639165SNate Lawson char *layer, *level;
442915e32d5dSMike Smith
44301d7b121cSNate Lawson if (cold) {
44310ae55423SMike Smith AcpiDbgLayer = 0;
44320ae55423SMike Smith AcpiDbgLevel = 0;
44331d7b121cSNate Lawson }
44341d7b121cSNate Lawson
44352be111bfSDavide Italiano layer = kern_getenv("debug.acpi.layer");
44362be111bfSDavide Italiano level = kern_getenv("debug.acpi.level");
44377e639165SNate Lawson if (layer == NULL && level == NULL)
44387e639165SNate Lawson return;
44390ae55423SMike Smith
44407e639165SNate Lawson printf("ACPI set debug");
44417e639165SNate Lawson if (layer != NULL) {
44427e639165SNate Lawson if (strcmp("NONE", layer) != 0)
44437e639165SNate Lawson printf(" layer '%s'", layer);
44447e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
44457e639165SNate Lawson freeenv(layer);
44461d7b121cSNate Lawson }
44477e639165SNate Lawson if (level != NULL) {
44487e639165SNate Lawson if (strcmp("NONE", level) != 0)
44497e639165SNate Lawson printf(" level '%s'", level);
44507e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
44517e639165SNate Lawson freeenv(level);
44527e639165SNate Lawson }
44537e639165SNate Lawson printf("\n");
445415e32d5dSMike Smith }
4455bee4aa4aSNate Lawson
4456be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
4457be2b1797SNate Lawson NULL);
44581d7b121cSNate Lawson
44591d7b121cSNate Lawson static int
acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)44601d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
44611d7b121cSNate Lawson {
446254f6bca0SNate Lawson int error, *dbg;
44631d7b121cSNate Lawson struct debugtag *tag;
446454f6bca0SNate Lawson struct sbuf sb;
44650e1152fcSHans Petter Selasky char temp[128];
44661d7b121cSNate Lawson
446754f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
446854f6bca0SNate Lawson return (ENOMEM);
44691d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
44701d7b121cSNate Lawson tag = &dbg_layer[0];
44711d7b121cSNate Lawson dbg = &AcpiDbgLayer;
44721d7b121cSNate Lawson } else {
44731d7b121cSNate Lawson tag = &dbg_level[0];
44741d7b121cSNate Lawson dbg = &AcpiDbgLevel;
44751d7b121cSNate Lawson }
44761d7b121cSNate Lawson
44771d7b121cSNate Lawson /* Get old values if this is a get request. */
447815e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi);
44791d7b121cSNate Lawson if (*dbg == 0) {
448054f6bca0SNate Lawson sbuf_cpy(&sb, "NONE");
44811d7b121cSNate Lawson } else if (req->newptr == NULL) {
44821d7b121cSNate Lawson for (; tag->name != NULL; tag++) {
448354f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value)
448454f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name);
44851d7b121cSNate Lawson }
44861d7b121cSNate Lawson }
448754f6bca0SNate Lawson sbuf_trim(&sb);
448854f6bca0SNate Lawson sbuf_finish(&sb);
44890e1152fcSHans Petter Selasky strlcpy(temp, sbuf_data(&sb), sizeof(temp));
449054f6bca0SNate Lawson sbuf_delete(&sb);
44911d7b121cSNate Lawson
44920e1152fcSHans Petter Selasky error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
44930e1152fcSHans Petter Selasky
44940e1152fcSHans Petter Selasky /* Check for error or no change */
44951d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) {
44961d7b121cSNate Lawson *dbg = 0;
44970e1152fcSHans Petter Selasky kern_setenv((char *)oidp->oid_arg1, temp);
44981d7b121cSNate Lawson acpi_set_debugging(NULL);
44991d7b121cSNate Lawson }
450015e2f34fSNate Lawson ACPI_SERIAL_END(acpi);
45011d7b121cSNate Lawson
45021d7b121cSNate Lawson return (error);
45031d7b121cSNate Lawson }
4504bee4aa4aSNate Lawson
45057029da5cSPawel Biernacki SYSCTL_PROC(_debug_acpi, OID_AUTO, layer,
45063e68d2c5SAlexander Motin CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, "debug.acpi.layer", 0,
45077029da5cSPawel Biernacki acpi_debug_sysctl, "A",
45087029da5cSPawel Biernacki "");
45097029da5cSPawel Biernacki SYSCTL_PROC(_debug_acpi, OID_AUTO, level,
45103e68d2c5SAlexander Motin CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, "debug.acpi.level", 0,
45117029da5cSPawel Biernacki acpi_debug_sysctl, "A",
45127029da5cSPawel Biernacki "");
4513bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
4514f9390180SMitsuru IWASAKI
4515f9390180SMitsuru IWASAKI static int
acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)45162a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
45172a18c71dSJung-uk Kim {
45182a18c71dSJung-uk Kim int error;
45192a18c71dSJung-uk Kim int old;
45202a18c71dSJung-uk Kim
45212a18c71dSJung-uk Kim old = acpi_debug_objects;
45222a18c71dSJung-uk Kim error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
45232a18c71dSJung-uk Kim if (error != 0 || req->newptr == NULL)
45242a18c71dSJung-uk Kim return (error);
45252a18c71dSJung-uk Kim if (old == acpi_debug_objects || (old && acpi_debug_objects))
45262a18c71dSJung-uk Kim return (0);
45272a18c71dSJung-uk Kim
45282a18c71dSJung-uk Kim ACPI_SERIAL_BEGIN(acpi);
45292a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
45302a18c71dSJung-uk Kim ACPI_SERIAL_END(acpi);
45312a18c71dSJung-uk Kim
45322a18c71dSJung-uk Kim return (0);
45332a18c71dSJung-uk Kim }
45342a18c71dSJung-uk Kim
45352a18c71dSJung-uk Kim static int
acpi_parse_interfaces(char * str,struct acpi_interface * iface)4536ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface)
4537ae19af49SJung-uk Kim {
4538ae19af49SJung-uk Kim char *p;
4539ae19af49SJung-uk Kim size_t len;
4540ae19af49SJung-uk Kim int i, j;
4541ae19af49SJung-uk Kim
4542ae19af49SJung-uk Kim p = str;
4543ae19af49SJung-uk Kim while (isspace(*p) || *p == ',')
4544ae19af49SJung-uk Kim p++;
4545ae19af49SJung-uk Kim len = strlen(p);
4546ae19af49SJung-uk Kim if (len == 0)
4547ae19af49SJung-uk Kim return (0);
4548ae19af49SJung-uk Kim p = strdup(p, M_TEMP);
4549ae19af49SJung-uk Kim for (i = 0; i < len; i++)
4550ae19af49SJung-uk Kim if (p[i] == ',')
4551ae19af49SJung-uk Kim p[i] = '\0';
4552ae19af49SJung-uk Kim i = j = 0;
4553ae19af49SJung-uk Kim while (i < len)
4554ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0')
4555ae19af49SJung-uk Kim i++;
4556ae19af49SJung-uk Kim else {
4557ae19af49SJung-uk Kim i += strlen(p + i) + 1;
4558ae19af49SJung-uk Kim j++;
4559ae19af49SJung-uk Kim }
4560ae19af49SJung-uk Kim if (j == 0) {
4561ae19af49SJung-uk Kim free(p, M_TEMP);
4562ae19af49SJung-uk Kim return (0);
4563ae19af49SJung-uk Kim }
4564ae19af49SJung-uk Kim iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
4565ae19af49SJung-uk Kim iface->num = j;
4566ae19af49SJung-uk Kim i = j = 0;
4567ae19af49SJung-uk Kim while (i < len)
4568ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0')
4569ae19af49SJung-uk Kim i++;
4570ae19af49SJung-uk Kim else {
4571ae19af49SJung-uk Kim iface->data[j] = p + i;
4572ae19af49SJung-uk Kim i += strlen(p + i) + 1;
4573ae19af49SJung-uk Kim j++;
4574ae19af49SJung-uk Kim }
4575ae19af49SJung-uk Kim
4576ae19af49SJung-uk Kim return (j);
4577ae19af49SJung-uk Kim }
4578ae19af49SJung-uk Kim
4579ae19af49SJung-uk Kim static void
acpi_free_interfaces(struct acpi_interface * iface)4580ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface)
4581ae19af49SJung-uk Kim {
4582ae19af49SJung-uk Kim
4583ae19af49SJung-uk Kim free(iface->data[0], M_TEMP);
4584ae19af49SJung-uk Kim free(iface->data, M_TEMP);
4585ae19af49SJung-uk Kim }
4586ae19af49SJung-uk Kim
4587ae19af49SJung-uk Kim static void
acpi_reset_interfaces(device_t dev)4588ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev)
4589ae19af49SJung-uk Kim {
4590ae19af49SJung-uk Kim struct acpi_interface list;
4591ae19af49SJung-uk Kim ACPI_STATUS status;
4592ae19af49SJung-uk Kim int i;
4593ae19af49SJung-uk Kim
4594ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
4595ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) {
4596ae19af49SJung-uk Kim status = AcpiInstallInterface(list.data[i]);
4597ae19af49SJung-uk Kim if (ACPI_FAILURE(status))
4598ae19af49SJung-uk Kim device_printf(dev,
4599ae19af49SJung-uk Kim "failed to install _OSI(\"%s\"): %s\n",
4600ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status));
4601ae19af49SJung-uk Kim else if (bootverbose)
4602ae19af49SJung-uk Kim device_printf(dev, "installed _OSI(\"%s\")\n",
4603ae19af49SJung-uk Kim list.data[i]);
4604ae19af49SJung-uk Kim }
4605ae19af49SJung-uk Kim acpi_free_interfaces(&list);
4606ae19af49SJung-uk Kim }
4607ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
4608ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) {
4609ae19af49SJung-uk Kim status = AcpiRemoveInterface(list.data[i]);
4610ae19af49SJung-uk Kim if (ACPI_FAILURE(status))
4611ae19af49SJung-uk Kim device_printf(dev,
4612ae19af49SJung-uk Kim "failed to remove _OSI(\"%s\"): %s\n",
4613ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status));
4614ae19af49SJung-uk Kim else if (bootverbose)
4615ae19af49SJung-uk Kim device_printf(dev, "removed _OSI(\"%s\")\n",
4616ae19af49SJung-uk Kim list.data[i]);
4617ae19af49SJung-uk Kim }
4618ae19af49SJung-uk Kim acpi_free_interfaces(&list);
4619ae19af49SJung-uk Kim }
4620ae19af49SJung-uk Kim }
4621ae19af49SJung-uk Kim
4622ae19af49SJung-uk Kim static int
acpi_pm_func(u_long cmd,void * arg,...)4623f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
4624f9390180SMitsuru IWASAKI {
4625f9390180SMitsuru IWASAKI int state, acpi_state;
4626f9390180SMitsuru IWASAKI int error;
4627f9390180SMitsuru IWASAKI struct acpi_softc *sc;
4628f9390180SMitsuru IWASAKI va_list ap;
4629f9390180SMitsuru IWASAKI
4630f9390180SMitsuru IWASAKI error = 0;
4631f9390180SMitsuru IWASAKI switch (cmd) {
4632f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND:
4633f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg;
4634f9390180SMitsuru IWASAKI if (sc == NULL) {
4635f9390180SMitsuru IWASAKI error = EINVAL;
4636f9390180SMitsuru IWASAKI goto out;
4637f9390180SMitsuru IWASAKI }
4638f9390180SMitsuru IWASAKI
4639f9390180SMitsuru IWASAKI va_start(ap, arg);
4640f9390180SMitsuru IWASAKI state = va_arg(ap, int);
4641f9390180SMitsuru IWASAKI va_end(ap);
4642f9390180SMitsuru IWASAKI
4643f9390180SMitsuru IWASAKI switch (state) {
4644f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY:
4645f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx;
4646f9390180SMitsuru IWASAKI break;
4647f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND:
4648f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx;
4649f9390180SMitsuru IWASAKI break;
4650f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE:
4651f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4;
4652f9390180SMitsuru IWASAKI break;
4653f9390180SMitsuru IWASAKI default:
4654f9390180SMitsuru IWASAKI error = EINVAL;
4655f9390180SMitsuru IWASAKI goto out;
4656f9390180SMitsuru IWASAKI }
4657f9390180SMitsuru IWASAKI
465800a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
465900a30448SNate Lawson error = ENXIO;
4660f9390180SMitsuru IWASAKI break;
4661f9390180SMitsuru IWASAKI default:
4662f9390180SMitsuru IWASAKI error = EINVAL;
4663f9390180SMitsuru IWASAKI goto out;
4664f9390180SMitsuru IWASAKI }
4665f9390180SMitsuru IWASAKI
4666f9390180SMitsuru IWASAKI out:
4667f9390180SMitsuru IWASAKI return (error);
4668f9390180SMitsuru IWASAKI }
4669f9390180SMitsuru IWASAKI
4670f9390180SMitsuru IWASAKI static void
acpi_pm_register(void * arg)4671f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
4672f9390180SMitsuru IWASAKI {
4673be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0))
46746c407052SMitsuru IWASAKI return;
4675f9390180SMitsuru IWASAKI
4676f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
4677f9390180SMitsuru IWASAKI }
4678f9390180SMitsuru IWASAKI
4679891cf3edSEd Maste SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, NULL);
4680