xref: /freebsd/sys/dev/acpica/acpi.c (revision 855e49f3b0adf4f47d2fa2a9cf92c83d9e700705)
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>
31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$");
32dad97feeSDavid E. O'Brien 
3315e32d5dSMike Smith #include "opt_acpi.h"
3462d70a81SJohn Baldwin 
3515e32d5dSMike Smith #include <sys/param.h>
36e2e050c8SConrad Meyer #include <sys/eventhandler.h>
3715e32d5dSMike Smith #include <sys/kernel.h>
38fb919e4dSMark Murray #include <sys/proc.h>
39a89fcf28STakanori Watanabe #include <sys/fcntl.h>
4015e32d5dSMike Smith #include <sys/malloc.h>
41fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4215e32d5dSMike Smith #include <sys/bus.h>
4315e32d5dSMike Smith #include <sys/conf.h>
4415e32d5dSMike Smith #include <sys/ioccom.h>
4515e32d5dSMike Smith #include <sys/reboot.h>
4615e32d5dSMike Smith #include <sys/sysctl.h>
4715e32d5dSMike Smith #include <sys/ctype.h>
481611ea87SMitsuru IWASAKI #include <sys/linker.h>
49f9390180SMitsuru IWASAKI #include <sys/power.h>
5054f6bca0SNate Lawson #include <sys/sbuf.h>
51c66d2b38SJung-uk Kim #include <sys/sched.h>
52eeb3a05fSNate Lawson #include <sys/smp.h>
53c66d2b38SJung-uk Kim #include <sys/timetc.h>
5415e32d5dSMike Smith 
55d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
56279be68bSAndriy Gapon #include <machine/clock.h>
57d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h>
58d320e05cSJohn Baldwin #endif
5915e32d5dSMike Smith #include <machine/resource.h>
60dc750869SNate Lawson #include <machine/bus.h>
61dc750869SNate Lawson #include <sys/rman.h>
62bc0f2195SMike Smith #include <isa/isavar.h>
63c796e27bSNate Lawson #include <isa/pnpvar.h>
64bc0f2195SMike Smith 
65129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
66129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
67129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
68129d3046SJung-uk Kim 
6915e32d5dSMike Smith #include <dev/acpica/acpivar.h>
7015e32d5dSMike Smith #include <dev/acpica/acpiio.h>
7115e32d5dSMike Smith 
720e68afe5SAndrew Turner #include <dev/pci/pcivar.h>
730e68afe5SAndrew Turner 
742be4e471SJung-uk Kim #include <vm/vm_param.h>
752be4e471SJung-uk Kim 
76d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
7715e32d5dSMike Smith 
78be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
79cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
80b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
810ae55423SMike Smith 
8215e32d5dSMike Smith static d_open_t		acpiopen;
8315e32d5dSMike Smith static d_close_t	acpiclose;
8415e32d5dSMike Smith static d_ioctl_t	acpiioctl;
8515e32d5dSMike Smith 
8615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
87dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
887ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
897ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
907ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
917ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
9215e32d5dSMike Smith };
9315e32d5dSMike Smith 
94ae19af49SJung-uk Kim struct acpi_interface {
95ae19af49SJung-uk Kim 	ACPI_STRING	*data;
96ae19af49SJung-uk Kim 	int		num;
97ae19af49SJung-uk Kim };
98ae19af49SJung-uk Kim 
99dc8ab16eSWarner Losh static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
100dc8ab16eSWarner Losh static char *pcilink_ids[] = { "PNP0C0F", NULL };
101dc8ab16eSWarner Losh 
102346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
103cb9b0d80SMike Smith struct mtx	acpi_mutex;
104fadf3fb9SJohn Baldwin struct callout	acpi_sleep_timer;
105cb9b0d80SMike Smith 
10689fb5af8SNate Lawson /* Bitmap of device quirks. */
10789fb5af8SNate Lawson int		acpi_quirks;
10889fb5af8SNate Lawson 
109a0e73a12SJung-uk Kim /* Supported sleep states. */
110a0e73a12SJung-uk Kim static BOOLEAN	acpi_sleep_states[ACPI_S_STATE_COUNT];
111a0e73a12SJung-uk Kim 
11264de8019SJohn Baldwin static void	acpi_lookup(void *arg, const char *name, device_t *dev);
1136d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
11415e32d5dSMike Smith static int	acpi_probe(device_t dev);
11515e32d5dSMike Smith static int	acpi_attach(device_t dev);
11610ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
11710ce62b9SNate Lawson static int	acpi_resume(device_t dev);
118169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
1193d844eddSAndriy Gapon static device_t	acpi_add_child(device_t bus, u_int order, const char *name,
120be2b1797SNate Lawson 			int unit);
12115e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
12210ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
12310ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
124709749aaSVladimir Kondratyev static void	acpi_child_deleted(device_t dev, device_t child);
125be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
126be2b1797SNate Lawson 			uintptr_t *result);
127be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
128be2b1797SNate Lawson 			uintptr_t value);
12991233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
130ea233199SJohn Baldwin static void	acpi_reserve_resources(device_t dev);
131adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
132ea233199SJohn Baldwin static int	acpi_set_resource(device_t dev, device_t child, int type,
1332dd1bdf1SJustin Hibbits 			int rid, rman_res_t start, rman_res_t count);
134be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
1352dd1bdf1SJustin Hibbits 			int type, int *rid, rman_res_t start, rman_res_t end,
1362dd1bdf1SJustin Hibbits 			rman_res_t count, u_int flags);
137049dc0d1SJohn Baldwin static int	acpi_adjust_resource(device_t bus, device_t child, int type,
1382dd1bdf1SJustin Hibbits 			struct resource *r, rman_res_t start, rman_res_t end);
139be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
140be2b1797SNate Lawson 			int rid, struct resource *r);
1418b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1428b28b622SNate Lawson 		    int rid);
1431e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1441e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
1455efca36fSTakanori Watanabe static int	acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match);
146ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
147ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
148ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
149df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
150df8d2a32SNate Lawson 		    void *context, void **retval);
151df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
152df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
153be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
154be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
155*855e49f3SAlexander Motin static void	acpi_platform_osc(device_t dev);
15615e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
157d227204dSJohn Baldwin static void	acpi_probe_order(ACPI_HANDLE handle, int *order);
158be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
159be2b1797SNate Lawson 		    void *context, void **status);
160a0e73a12SJung-uk Kim static void	acpi_sleep_enable(void *arg);
161a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
16200a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
16315e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
16413d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
165a0a15716SJung-uk Kim static void	acpi_resync_clock(struct acpi_softc *sc);
166d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
167d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
168d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
16988a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
17088a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
17115e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
17215e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
173a0e73a12SJung-uk Kim static int	acpi_sname2sstate(const char *sname);
174a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate);
175d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1761d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1772a18c71dSJung-uk Kim static int	acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
178f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
179879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
180879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
181879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
182879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
183d320e05cSJohn Baldwin static void	acpi_enable_pcie(void);
1840d484d24SJohn Baldwin static void	acpi_hint_device_unit(device_t acdev, device_t child,
1850d484d24SJohn Baldwin 		    const char *name, int *unitp);
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),
206ea233199SJohn Baldwin     DEVMETHOD(bus_set_resource,		acpi_set_resource),
20791233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
20815e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
209049dc0d1SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	acpi_adjust_resource),
21015e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
2118b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
212879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
213879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
21415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
21515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
21615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
21715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
2180d484d24SJohn Baldwin     DEVMETHOD(bus_hint_device_unit,	acpi_hint_device_unit),
2198d791e5aSJohn Baldwin     DEVMETHOD(bus_get_cpus,		acpi_get_cpus),
220ffcf962dSAdrian Chadd     DEVMETHOD(bus_get_domain,		acpi_get_domain),
22115e32d5dSMike Smith 
222ce619b2eSNate Lawson     /* ACPI bus */
223ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
224ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
22510ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
226df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
227ce619b2eSNate Lawson 
228bc0f2195SMike Smith     /* ISA emulation */
229bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
230bc0f2195SMike Smith 
23161bfd867SSofian Brabez     DEVMETHOD_END
23215e32d5dSMike Smith };
23315e32d5dSMike Smith 
23415e32d5dSMike Smith static driver_t acpi_driver = {
23515e32d5dSMike Smith     "acpi",
23615e32d5dSMike Smith     acpi_methods,
23715e32d5dSMike Smith     sizeof(struct acpi_softc),
23815e32d5dSMike Smith };
23915e32d5dSMike Smith 
2403273b005SMike Smith static devclass_t acpi_devclass;
241334790eaSAndrew Turner EARLY_DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0,
242334790eaSAndrew Turner     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
243a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
24415e32d5dSMike Smith 
24515e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
24615e2f34fSNate Lawson 
247adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
248adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
249adad4744SNate Lawson 
250bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
251bee4aa4aSNate Lawson 
2525217af30SJohn Baldwin /* Holds the description of the acpi0 device. */
2535217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
2545217af30SJohn Baldwin 
2557029da5cSPawel Biernacki SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
2567029da5cSPawel Biernacki     "ACPI debugging");
2571d7b121cSNate Lawson static char acpi_ca_version[12];
2581d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2591d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
26015e32d5dSMike Smith 
26115e32d5dSMike Smith /*
262ae19af49SJung-uk Kim  * Allow overriding _OSI methods.
263ae19af49SJung-uk Kim  */
264ae19af49SJung-uk Kim static char acpi_install_interface[256];
265ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
266ae19af49SJung-uk Kim     sizeof(acpi_install_interface));
267ae19af49SJung-uk Kim static char acpi_remove_interface[256];
268ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
269ae19af49SJung-uk Kim     sizeof(acpi_remove_interface));
270ae19af49SJung-uk Kim 
2712a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */
2722a18c71dSJung-uk Kim static int acpi_debug_objects;
2732a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
2742a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
2757029da5cSPawel Biernacki     CTLFLAG_RW | CTLTYPE_INT | CTLFLAG_NEEDGIANT, NULL, 0,
2767029da5cSPawel Biernacki     acpi_debug_objects_sysctl, "I",
2772a18c71dSJung-uk Kim     "Enable Debug objects");
2782a18c71dSJung-uk Kim 
2792a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */
2802a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1;
2812a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
2822a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
2832a18c71dSJung-uk Kim     &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
2842a18c71dSJung-uk Kim 
285313a0c13SJung-uk Kim /* Ignore register widths set by FADT and use default widths instead. */
286313a0c13SJung-uk Kim static int acpi_ignore_reg_width = 1;
287313a0c13SJung-uk Kim TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width);
288313a0c13SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN,
289313a0c13SJung-uk Kim     &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT");
290313a0c13SJung-uk Kim 
29139da3eb3SNate Lawson /* Allow users to override quirks. */
29239da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
29339da3eb3SNate Lawson 
294e787342eSJung-uk Kim int acpi_susp_bounce;
29593bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
29693bfd059SNate Lawson     &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
29793bfd059SNate Lawson 
298c9b8d77dSNate Lawson /*
2996d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
3006d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
3018c0879b6SJohn Baldwin  * It also cannot be unloaded, since the entire system bus hierarchy hangs
302be2b1797SNate Lawson  * off it.
3036d63101aSMike Smith  */
3046d63101aSMike Smith static int
3056d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
3066d63101aSMike Smith {
3076d63101aSMike Smith     switch (event) {
3086d63101aSMike Smith     case MOD_LOAD:
30987b45ed5SMitsuru IWASAKI 	if (!cold) {
31087b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
3116d63101aSMike Smith 	    return (EPERM);
31287b45ed5SMitsuru IWASAKI 	}
3136d63101aSMike Smith 	break;
3146d63101aSMike Smith     case MOD_UNLOAD:
315f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
3166d63101aSMike Smith 	    return (EBUSY);
317f9390180SMitsuru IWASAKI 	break;
3186d63101aSMike Smith     default:
3196d63101aSMike Smith 	break;
3206d63101aSMike Smith     }
3216d63101aSMike Smith     return (0);
3226d63101aSMike Smith }
3236d63101aSMike Smith 
3246d63101aSMike Smith /*
325bbc2815cSJohn Baldwin  * Perform early initialization.
32615e32d5dSMike Smith  */
327bbc2815cSJohn Baldwin ACPI_STATUS
328bbc2815cSJohn Baldwin acpi_Startup(void)
32915e32d5dSMike Smith {
33089fb5af8SNate Lawson     static int started = 0;
3312be4e471SJung-uk Kim     ACPI_STATUS status;
3322be4e471SJung-uk Kim     int val;
33315e32d5dSMike Smith 
3341b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3351b8c233dSPeter Pentchev 
336bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
337bbc2815cSJohn Baldwin     if (started)
3382be4e471SJung-uk Kim 	return_VALUE (AE_OK);
339bbc2815cSJohn Baldwin     started = 1;
34015e32d5dSMike Smith 
341413081d7SNate Lawson     /*
3425f9b24faSJung-uk Kim      * Initialize the ACPICA subsystem.
3435f9b24faSJung-uk Kim      */
3445f9b24faSJung-uk Kim     if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
3455f9b24faSJung-uk Kim 	printf("ACPI: Could not initialize Subsystem: %s\n",
3465f9b24faSJung-uk Kim 	    AcpiFormatException(status));
3475f9b24faSJung-uk Kim 	return_VALUE (status);
3485f9b24faSJung-uk Kim     }
3495f9b24faSJung-uk Kim 
3505f9b24faSJung-uk Kim     /*
3512be4e471SJung-uk Kim      * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
3522be4e471SJung-uk Kim      * if more tables exist.
353413081d7SNate Lawson      */
3542be4e471SJung-uk Kim     if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
3552be4e471SJung-uk Kim 	printf("ACPI: Table initialisation failed: %s\n",
3562be4e471SJung-uk Kim 	    AcpiFormatException(status));
3572be4e471SJung-uk Kim 	return_VALUE (status);
35815e32d5dSMike Smith     }
3593184cf5aSNate Lawson 
36089fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
3612be4e471SJung-uk Kim     if (acpi_quirks == ACPI_Q_OK)
36289fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
36389fb5af8SNate Lawson 
36439da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
36589fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
36689fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
36789fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
36889fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
3692be4e471SJung-uk Kim 	status = AE_SUPPORT;
37089fb5af8SNate Lawson     }
3713184cf5aSNate Lawson 
3722be4e471SJung-uk Kim     return_VALUE (status);
373bbc2815cSJohn Baldwin }
374bbc2815cSJohn Baldwin 
375bbc2815cSJohn Baldwin /*
3765217af30SJohn Baldwin  * Detect ACPI and perform early initialisation.
377bbc2815cSJohn Baldwin  */
3785217af30SJohn Baldwin int
3795217af30SJohn Baldwin acpi_identify(void)
380bbc2815cSJohn Baldwin {
3815217af30SJohn Baldwin     ACPI_TABLE_RSDP	*rsdp;
3825217af30SJohn Baldwin     ACPI_TABLE_HEADER	*rsdt;
3835217af30SJohn Baldwin     ACPI_PHYSICAL_ADDRESS paddr;
3845217af30SJohn Baldwin     struct sbuf		sb;
385bbc2815cSJohn Baldwin 
386bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
387bbc2815cSJohn Baldwin 
388bbc2815cSJohn Baldwin     if (!cold)
3895217af30SJohn Baldwin 	return (ENXIO);
390bbc2815cSJohn Baldwin 
391a8de37b0SEitan Adler     /* Check that we haven't been disabled with a hint. */
392a8de37b0SEitan Adler     if (resource_disabled("acpi", 0))
393a8de37b0SEitan Adler 	return (ENXIO);
394a8de37b0SEitan Adler 
3955217af30SJohn Baldwin     /* Check for other PM systems. */
3965217af30SJohn Baldwin     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
3975217af30SJohn Baldwin 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
3985217af30SJohn Baldwin 	printf("ACPI identify failed, other PM system enabled.\n");
3995217af30SJohn Baldwin 	return (ENXIO);
4005217af30SJohn Baldwin     }
4014b1ff978SMark Santcroos 
4022be4e471SJung-uk Kim     /* Initialize root tables. */
4032be4e471SJung-uk Kim     if (ACPI_FAILURE(acpi_Startup())) {
4042be4e471SJung-uk Kim 	printf("ACPI: Try disabling either ACPI or apic support.\n");
4055217af30SJohn Baldwin 	return (ENXIO);
4062be4e471SJung-uk Kim     }
40715e32d5dSMike Smith 
4085217af30SJohn Baldwin     if ((paddr = AcpiOsGetRootPointer()) == 0 ||
4095217af30SJohn Baldwin 	(rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
4105217af30SJohn Baldwin 	return (ENXIO);
4115217af30SJohn Baldwin     if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
4125217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
4135217af30SJohn Baldwin     else
4145217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
4155217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
4165217af30SJohn Baldwin 
4175217af30SJohn Baldwin     if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
4185217af30SJohn Baldwin 	return (ENXIO);
4195217af30SJohn Baldwin     sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
4205217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
4215217af30SJohn Baldwin     sbuf_trim(&sb);
4225217af30SJohn Baldwin     sbuf_putc(&sb, ' ');
4235217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
4245217af30SJohn Baldwin     sbuf_trim(&sb);
4255217af30SJohn Baldwin     sbuf_finish(&sb);
4265217af30SJohn Baldwin     sbuf_delete(&sb);
4275217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
4285217af30SJohn Baldwin 
4295217af30SJohn Baldwin     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
4305217af30SJohn Baldwin 
4315217af30SJohn Baldwin     return (0);
43215e32d5dSMike Smith }
43315e32d5dSMike Smith 
43415e32d5dSMike Smith /*
435bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
43615e32d5dSMike Smith  */
43715e32d5dSMike Smith static int
43815e32d5dSMike Smith acpi_probe(device_t dev)
43915e32d5dSMike Smith {
44015e32d5dSMike Smith 
441b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
442f9390180SMitsuru IWASAKI 
4435217af30SJohn Baldwin     device_set_desc(dev, acpi_desc);
444bee4aa4aSNate Lawson 
445feeec74dSNathan Whitehorn     return_VALUE (BUS_PROBE_NOWILDCARD);
44615e32d5dSMike Smith }
44715e32d5dSMike Smith 
44815e32d5dSMike Smith static int
44915e32d5dSMike Smith acpi_attach(device_t dev)
45015e32d5dSMike Smith {
45115e32d5dSMike Smith     struct acpi_softc	*sc;
452cb9b0d80SMike Smith     ACPI_STATUS		status;
453ea27c63eSNate Lawson     int			error, state;
45432d18aa5SMike Smith     UINT32		flags;
455ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
456498d464fSMitsuru IWASAKI     char		*env;
45715e32d5dSMike Smith 
458b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
459bee4aa4aSNate Lawson 
46015e32d5dSMike Smith     sc = device_get_softc(dev);
46115e32d5dSMike Smith     sc->acpi_dev = dev;
462fd90e2edSJung-uk Kim     callout_init(&sc->susp_force_to, 1);
46315e32d5dSMike Smith 
4642be4e471SJung-uk Kim     error = ENXIO;
4652be4e471SJung-uk Kim 
46691233413SNate Lawson     /* Initialize resource manager. */
46791233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
46891233413SNate Lawson     acpi_rman_io.rm_start = 0;
46991233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
4700bba6acfSJohn Baldwin     acpi_rman_io.rm_descr = "ACPI I/O ports";
47191233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
47291233413SNate Lawson 	panic("acpi rman_init IO ports failed");
47391233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
4740bba6acfSJohn Baldwin     acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
47591233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
47691233413SNate Lawson 	panic("acpi rman_init memory failed");
47791233413SNate Lawson 
4782be4e471SJung-uk Kim     /* Initialise the ACPI mutex */
4792be4e471SJung-uk Kim     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
4802be4e471SJung-uk Kim 
4812be4e471SJung-uk Kim     /*
4822be4e471SJung-uk Kim      * Set the globals from our tunables.  This is needed because ACPI-CA
4832be4e471SJung-uk Kim      * uses UINT8 for some values and we have no tunable_byte.
4842be4e471SJung-uk Kim      */
4852a18c71dSJung-uk Kim     AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
4862a18c71dSJung-uk Kim     AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
487313a0c13SJung-uk Kim     AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE;
4882a18c71dSJung-uk Kim 
4892a18c71dSJung-uk Kim #ifndef ACPI_DEBUG
4902a18c71dSJung-uk Kim     /*
4912a18c71dSJung-uk Kim      * Disable all debugging layers and levels.
4922a18c71dSJung-uk Kim      */
4932a18c71dSJung-uk Kim     AcpiDbgLayer = 0;
4942a18c71dSJung-uk Kim     AcpiDbgLevel = 0;
4952a18c71dSJung-uk Kim #endif
4962be4e471SJung-uk Kim 
497ae19af49SJung-uk Kim     /* Override OS interfaces if the user requested. */
498ae19af49SJung-uk Kim     acpi_reset_interfaces(dev);
499ae19af49SJung-uk Kim 
5002be4e471SJung-uk Kim     /* Load ACPI name space. */
5012be4e471SJung-uk Kim     status = AcpiLoadTables();
5022be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
5032be4e471SJung-uk Kim 	device_printf(dev, "Could not load Namespace: %s\n",
5042be4e471SJung-uk Kim 		      AcpiFormatException(status));
5052be4e471SJung-uk Kim 	goto out;
5062be4e471SJung-uk Kim     }
5072be4e471SJung-uk Kim 
508d320e05cSJohn Baldwin     /* Handle MCFG table if present. */
509d320e05cSJohn Baldwin     acpi_enable_pcie();
510d320e05cSJohn Baldwin 
51115e32d5dSMike Smith     /*
512be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
513be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
514be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
515be2b1797SNate Lawson      * object init pass.
51615e32d5dSMike Smith      *
51732d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
51832d18aa5SMike Smith      *
519be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
520be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
52115e32d5dSMike Smith      */
52232d18aa5SMike Smith     flags = 0;
523d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
52432d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
525bee4aa4aSNate Lawson 
526bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
527b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
528be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
529be2b1797SNate Lawson 		      AcpiFormatException(status));
530cb9b0d80SMike Smith 	goto out;
53115e32d5dSMike Smith     }
53215e32d5dSMike Smith 
533f8335e3aSNate Lawson     /*
534f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
535f8335e3aSNate Lawson      * the namespace has been evaluated.
536da72d149SNate Lawson      *
537da72d149SNate Lawson      * XXX This happens before the sysresource devices have been probed and
538da72d149SNate Lawson      * attached so its resources come from nexus0.  In practice, this isn't
539da72d149SNate Lawson      * a problem but should be addressed eventually.
540f8335e3aSNate Lawson      */
541f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
542f8335e3aSNate Lawson 
543bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
544b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
545be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
546be2b1797SNate Lawson 		      AcpiFormatException(status));
547b69ed3f4SMitsuru IWASAKI 	goto out;
548b69ed3f4SMitsuru IWASAKI     }
549b69ed3f4SMitsuru IWASAKI 
55015e32d5dSMike Smith     /*
5511d073b1dSJohn Baldwin      * Setup our sysctl tree.
5521d073b1dSJohn Baldwin      *
5531d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
5541d073b1dSJohn Baldwin      */
5551d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
5561d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
5577029da5cSPawel Biernacki         SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, device_get_name(dev),
5587029da5cSPawel Biernacki 	CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
5591d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5607029da5cSPawel Biernacki 	OID_AUTO, "supported_sleep_state",
5617029da5cSPawel Biernacki 	CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
562ad73b301SAdrian Chadd 	0, 0, acpi_supported_sleep_state_sysctl, "A",
563ad73b301SAdrian Chadd 	"List supported ACPI sleep states.");
564d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5657029da5cSPawel Biernacki 	OID_AUTO, "power_button_state",
5667029da5cSPawel Biernacki 	CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
567ad73b301SAdrian Chadd 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A",
568ad73b301SAdrian Chadd 	"Power button ACPI sleep state.");
5691d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5707029da5cSPawel Biernacki 	OID_AUTO, "sleep_button_state",
5717029da5cSPawel Biernacki 	CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
572ad73b301SAdrian Chadd 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A",
573ad73b301SAdrian Chadd 	"Sleep button ACPI sleep state.");
5741d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5757029da5cSPawel Biernacki 	OID_AUTO, "lid_switch_state",
5767029da5cSPawel Biernacki 	CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
577ad73b301SAdrian Chadd 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A",
578ad73b301SAdrian Chadd 	"Lid ACPI sleep state. Set to S3 if you want to suspend your laptop when close the Lid.");
579f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5807029da5cSPawel Biernacki 	OID_AUTO, "standby_state",
5817029da5cSPawel Biernacki 	CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
582f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
583f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5847029da5cSPawel Biernacki 	OID_AUTO, "suspend_state",
5857029da5cSPawel Biernacki 	CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
586f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5872d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
588197b4dccSNate Lawson 	OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
58931f301d0SChristian Brueffer 	"sleep delay in seconds");
590ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
591197b4dccSNate Lawson 	OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
5921611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
593197b4dccSNate Lawson 	OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
594d85e6785SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
595d85e6785SNate Lawson 	OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
596d85e6785SNate Lawson 	&sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
597d1b16e18SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
598d1b16e18SNate Lawson 	OID_AUTO, "handle_reboot", CTLFLAG_RW,
599d1b16e18SNate Lawson 	&sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
600bf0c18ecSNate Lawson 
601bf0c18ecSNate Lawson     /*
6022b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
603bf0c18ecSNate Lawson      * stabilize.
604bf0c18ecSNate Lawson      */
6052b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
6062d644607SMitsuru IWASAKI     if (bootverbose)
6072d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
6082be111bfSDavide Italiano     if ((env = kern_getenv("hw.acpi.verbose")) != NULL) {
609965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
610498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
611498d464fSMitsuru IWASAKI 	freeenv(env);
612498d464fSMitsuru IWASAKI     }
6131d073b1dSJohn Baldwin 
614ac731af5SJung-uk Kim     /* Only enable reboot by default if the FADT says it is available. */
615ac731af5SJung-uk Kim     if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
616ac731af5SJung-uk Kim 	sc->acpi_handle_reboot = 1;
617ac731af5SJung-uk Kim 
618fe5d5d7dSAndrew Turner #if !ACPI_REDUCED_HARDWARE
6192d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
620d8c37c3aSAndrew Turner     if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
6212d610c46SNate Lawson 	sc->acpi_s4bios = 1;
622fe5d5d7dSAndrew Turner #endif
6232d610c46SNate Lawson 
624a0e73a12SJung-uk Kim     /* Probe all supported sleep states. */
625a0e73a12SJung-uk Kim     acpi_sleep_states[ACPI_STATE_S0] = TRUE;
626a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
627efcc2a30SJung-uk Kim 	if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT,
628efcc2a30SJung-uk Kim 	    __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) &&
629efcc2a30SJung-uk Kim 	    ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
630a0e73a12SJung-uk Kim 	    acpi_sleep_states[state] = TRUE;
631a0e73a12SJung-uk Kim 
6321d073b1dSJohn Baldwin     /*
633ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
634a0e73a12SJung-uk Kim      * to UNKNOWN by default to avoid surprising users.
63515e32d5dSMike Smith      */
636a0e73a12SJung-uk Kim     sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
637a0e73a12SJung-uk Kim 	ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
638a0e73a12SJung-uk Kim     sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
639a0e73a12SJung-uk Kim     sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
640a0e73a12SJung-uk Kim 	ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
641a0e73a12SJung-uk Kim     sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
642a0e73a12SJung-uk Kim 	ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
64315e32d5dSMike Smith 
644ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
645a0e73a12SJung-uk Kim     sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
64600a30448SNate Lawson     for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
647a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state]) {
648ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
649ea27c63eSNate Lawson 	    break;
650ea27c63eSNate Lawson 	}
651ea27c63eSNate Lawson 
65213d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
65315e32d5dSMike Smith 
65415e32d5dSMike Smith     /*
65515e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
65615e32d5dSMike Smith      */
65715e32d5dSMike Smith 
65859e472e9SNate Lawson     /* Register our shutdown handler. */
659be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
660be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
66115e32d5dSMike Smith 
66215e32d5dSMike Smith     /*
66315e32d5dSMike Smith      * Register our acpi event handlers.
66415e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
66515e32d5dSMike Smith      */
666be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
667be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
668be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
669be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
67015e32d5dSMike Smith 
671be2b1797SNate Lawson     /* Flag our initial states. */
672a0e73a12SJung-uk Kim     sc->acpi_enabled = TRUE;
67315e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
674a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
67515e32d5dSMike Smith 
676be2b1797SNate Lawson     /* Create the control device */
677d1655c6fSEdward Tomasz Napierala     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664,
6782a4689e8SRobert Watson 			      "acpi");
67915e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
68015e32d5dSMike Smith 
681be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
682f86214b6SMitsuru IWASAKI 	goto out;
683f86214b6SMitsuru IWASAKI 
684f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
685f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
686f9390180SMitsuru IWASAKI 
687*855e49f3SAlexander Motin     acpi_platform_osc(dev);
688*855e49f3SAlexander Motin 
68964de8019SJohn Baldwin     if (!acpi_disabled("bus")) {
69064de8019SJohn Baldwin 	EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000);
691eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
69264de8019SJohn Baldwin     }
693eeb6dba2SJohn Baldwin 
6945a77b11bSJung-uk Kim     /* Update all GPEs and enable runtime GPEs. */
6955a77b11bSJung-uk Kim     status = AcpiUpdateAllGpes();
6965a77b11bSJung-uk Kim     if (ACPI_FAILURE(status))
6975a77b11bSJung-uk Kim 	device_printf(dev, "Could not update all GPEs: %s\n",
6985a77b11bSJung-uk Kim 	    AcpiFormatException(status));
6995a77b11bSJung-uk Kim 
700a0e73a12SJung-uk Kim     /* Allow sleep request after a while. */
701fadf3fb9SJohn Baldwin     callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0);
702fadf3fb9SJohn Baldwin     callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
703fadf3fb9SJohn Baldwin 	acpi_sleep_enable, sc);
704a0e73a12SJung-uk Kim 
705cb9b0d80SMike Smith     error = 0;
706cb9b0d80SMike Smith 
707cb9b0d80SMike Smith  out:
708cb9b0d80SMike Smith     return_VALUE (error);
70915e32d5dSMike Smith }
71015e32d5dSMike Smith 
711a7a3177fSJung-uk Kim static void
712a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state)
713a7a3177fSJung-uk Kim {
71444aba0f6SJung-uk Kim 	device_t child;
715a7a3177fSJung-uk Kim 	device_t *devlist;
716a7a3177fSJung-uk Kim 	int dstate, i, numdevs;
717a7a3177fSJung-uk Kim 
718a7a3177fSJung-uk Kim 	if (device_get_children(dev, &devlist, &numdevs) != 0)
719a7a3177fSJung-uk Kim 		return;
720a7a3177fSJung-uk Kim 
721a7a3177fSJung-uk Kim 	/*
722a7a3177fSJung-uk Kim 	 * Retrieve and set D-state for the sleep state if _SxD is present.
723a7a3177fSJung-uk Kim 	 * Skip children who aren't attached since they are handled separately.
724a7a3177fSJung-uk Kim 	 */
725a7a3177fSJung-uk Kim 	for (i = 0; i < numdevs; i++) {
726a7a3177fSJung-uk Kim 		child = devlist[i];
727a7a3177fSJung-uk Kim 		dstate = state;
728a7a3177fSJung-uk Kim 		if (device_is_attached(child) &&
72944aba0f6SJung-uk Kim 		    acpi_device_pwr_for_sleep(dev, child, &dstate) == 0)
730a7a3177fSJung-uk Kim 			acpi_set_powerstate(child, dstate);
731a7a3177fSJung-uk Kim 	}
732a7a3177fSJung-uk Kim 	free(devlist, M_TEMP);
733a7a3177fSJung-uk Kim }
734a7a3177fSJung-uk Kim 
735169b539aSNate Lawson static int
73610ce62b9SNate Lawson acpi_suspend(device_t dev)
73710ce62b9SNate Lawson {
738a7a3177fSJung-uk Kim     int error;
73910ce62b9SNate Lawson 
740a56fe095SJohn Baldwin     GIANT_REQUIRED;
741a56fe095SJohn Baldwin 
74210ce62b9SNate Lawson     error = bus_generic_suspend(dev);
743a7a3177fSJung-uk Kim     if (error == 0)
744a7a3177fSJung-uk Kim 	acpi_set_power_children(dev, ACPI_STATE_D3);
74510ce62b9SNate Lawson 
74610ce62b9SNate Lawson     return (error);
74710ce62b9SNate Lawson }
74810ce62b9SNate Lawson 
74910ce62b9SNate Lawson static int
75010ce62b9SNate Lawson acpi_resume(device_t dev)
75110ce62b9SNate Lawson {
75210ce62b9SNate Lawson 
753a56fe095SJohn Baldwin     GIANT_REQUIRED;
754a56fe095SJohn Baldwin 
755a7a3177fSJung-uk Kim     acpi_set_power_children(dev, ACPI_STATE_D0);
75610ce62b9SNate Lawson 
75710ce62b9SNate Lawson     return (bus_generic_resume(dev));
75810ce62b9SNate Lawson }
75910ce62b9SNate Lawson 
76010ce62b9SNate Lawson static int
761169b539aSNate Lawson acpi_shutdown(device_t dev)
762169b539aSNate Lawson {
763169b539aSNate Lawson 
764a56fe095SJohn Baldwin     GIANT_REQUIRED;
765a56fe095SJohn Baldwin 
7660e414868SNate Lawson     /* Allow children to shutdown first. */
7670e414868SNate Lawson     bus_generic_shutdown(dev);
7680e414868SNate Lawson 
769bee4aa4aSNate Lawson     /*
770bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
771bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
772bee4aa4aSNate Lawson      */
773d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
774d4b9ff91SNate Lawson 
775169b539aSNate Lawson     return (0);
776169b539aSNate Lawson }
777169b539aSNate Lawson 
77815e32d5dSMike Smith /*
77915e32d5dSMike Smith  * Handle a new device being added
78015e32d5dSMike Smith  */
78115e32d5dSMike Smith static device_t
7823d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit)
78315e32d5dSMike Smith {
78415e32d5dSMike Smith     struct acpi_device	*ad;
78515e32d5dSMike Smith     device_t		child;
78615e32d5dSMike Smith 
787be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
78815e32d5dSMike Smith 	return (NULL);
78915e32d5dSMike Smith 
79015e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
79115e32d5dSMike Smith 
79215e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
79315e32d5dSMike Smith     if (child != NULL)
79415e32d5dSMike Smith 	device_set_ivars(child, ad);
79543ce1c77SNate Lawson     else
79643ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
79715e32d5dSMike Smith     return (child);
79815e32d5dSMike Smith }
79915e32d5dSMike Smith 
80015e32d5dSMike Smith static int
80115e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
80215e32d5dSMike Smith {
80315e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
80415e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
80515e32d5dSMike Smith     int retval = 0;
80615e32d5dSMike Smith 
80715e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
808da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#jx");
809da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
810da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%jd");
811da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%jd");
812a91c5fa8SNate Lawson     if (device_get_flags(child))
813a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
814ffcf962dSAdrian Chadd     retval += bus_print_child_domain(bus, child);
8152c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
81615e32d5dSMike Smith 
81715e32d5dSMike Smith     return (retval);
81815e32d5dSMike Smith }
81915e32d5dSMike Smith 
82010ce62b9SNate Lawson /*
82110ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
82210ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
82310ce62b9SNate Lawson  *
82410ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
82510ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
82610ce62b9SNate Lawson  * them to be powered up.
82710ce62b9SNate Lawson  */
82810ce62b9SNate Lawson static void
82910ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
83010ce62b9SNate Lawson {
83196cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
832a7a3177fSJung-uk Kim     acpi_set_powerstate(child, ACPI_STATE_D3);
83396cf0b6dSWarner Losh #endif
83410ce62b9SNate Lawson }
83510ce62b9SNate Lawson 
83610ce62b9SNate Lawson /*
83710ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
83810ce62b9SNate Lawson  *
83910ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
84010ce62b9SNate Lawson  */
84110ce62b9SNate Lawson static void
84210ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
84310ce62b9SNate Lawson {
84410ce62b9SNate Lawson     device_t child, *devlist;
84510ce62b9SNate Lawson     int i, numdevs;
84610ce62b9SNate Lawson 
84710ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
848099ea4b5SWarner Losh     if (device_get_children(dev, &devlist, &numdevs))
849099ea4b5SWarner Losh 	    return;
85010ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
85110ce62b9SNate Lawson 	child = devlist[i];
85210ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
85396cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
854a7a3177fSJung-uk Kim 	    acpi_set_powerstate(child, ACPI_STATE_D0);
85510ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
856a7a3177fSJung-uk Kim 		acpi_set_powerstate(child, ACPI_STATE_D3);
85796cf0b6dSWarner Losh #else
85896cf0b6dSWarner Losh 	    device_probe_and_attach(child);
85996cf0b6dSWarner Losh #endif
86010ce62b9SNate Lawson 	}
86110ce62b9SNate Lawson     }
86210ce62b9SNate Lawson     free(devlist, M_TEMP);
86310ce62b9SNate Lawson }
86410ce62b9SNate Lawson 
86563600cc3SNate Lawson /* Location hint for devctl(8) */
86663600cc3SNate Lawson static int
867247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
868247648afSTakanori Watanabe     size_t buflen)
869247648afSTakanori Watanabe {
870247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
8719cf5a6aaSAdrian Chadd     char buf2[32];
8729cf5a6aaSAdrian Chadd     int pxm;
873247648afSTakanori Watanabe 
8749cf5a6aaSAdrian Chadd     if (dinfo->ad_handle) {
875d40c0f53SNate Lawson         snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
8769cf5a6aaSAdrian Chadd         if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) {
8779cf5a6aaSAdrian Chadd                 snprintf(buf2, 32, " _PXM=%d", pxm);
8789cf5a6aaSAdrian Chadd                 strlcat(buf, buf2, buflen);
8799cf5a6aaSAdrian Chadd         }
8809cf5a6aaSAdrian Chadd     } else {
8811974d7a4SD Scott Phillips         snprintf(buf, buflen, "");
8829cf5a6aaSAdrian Chadd     }
883247648afSTakanori Watanabe     return (0);
884247648afSTakanori Watanabe }
885247648afSTakanori Watanabe 
88663600cc3SNate Lawson /* PnP information for devctl(8) */
887ddf8c230SVladimir Kondratyev int
888ddf8c230SVladimir Kondratyev acpi_pnpinfo_str(ACPI_HANDLE handle, char *buf, size_t buflen)
889247648afSTakanori Watanabe {
89092488a57SJung-uk Kim     ACPI_DEVICE_INFO *adinfo;
891247648afSTakanori Watanabe 
892ddf8c230SVladimir Kondratyev     if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &adinfo))) {
893d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
89492488a57SJung-uk Kim 	return (0);
89592488a57SJung-uk Kim     }
89692488a57SJung-uk Kim 
897359a5f96SConrad Meyer     snprintf(buf, buflen, "_HID=%s _UID=%lu _CID=%s",
898247648afSTakanori Watanabe 	(adinfo->Valid & ACPI_VALID_HID) ?
89992488a57SJung-uk Kim 	adinfo->HardwareId.String : "none",
90063600cc3SNate Lawson 	(adinfo->Valid & ACPI_VALID_UID) ?
901359a5f96SConrad Meyer 	strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL,
902359a5f96SConrad Meyer 	((adinfo->Valid & ACPI_VALID_CID) &&
903359a5f96SConrad Meyer 	 adinfo->CompatibleIdList.Count > 0) ?
904359a5f96SConrad Meyer 	adinfo->CompatibleIdList.Ids[0].String : "none");
905247648afSTakanori Watanabe     AcpiOsFree(adinfo);
906247648afSTakanori Watanabe 
907247648afSTakanori Watanabe     return (0);
908247648afSTakanori Watanabe }
909247648afSTakanori Watanabe 
910ddf8c230SVladimir Kondratyev static int
911ddf8c230SVladimir Kondratyev acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
912ddf8c230SVladimir Kondratyev     size_t buflen)
913ddf8c230SVladimir Kondratyev {
914ddf8c230SVladimir Kondratyev     struct acpi_device *dinfo = device_get_ivars(child);
915ddf8c230SVladimir Kondratyev 
916ddf8c230SVladimir Kondratyev     return (acpi_pnpinfo_str(dinfo->ad_handle, buf, buflen));
917ddf8c230SVladimir Kondratyev }
918ddf8c230SVladimir Kondratyev 
919247648afSTakanori Watanabe /*
920709749aaSVladimir Kondratyev  * Handle device deletion.
921709749aaSVladimir Kondratyev  */
922709749aaSVladimir Kondratyev static void
923709749aaSVladimir Kondratyev acpi_child_deleted(device_t dev, device_t child)
924709749aaSVladimir Kondratyev {
925709749aaSVladimir Kondratyev     struct acpi_device *dinfo = device_get_ivars(child);
926709749aaSVladimir Kondratyev 
927709749aaSVladimir Kondratyev     if (acpi_get_device(dinfo->ad_handle) == child)
928709749aaSVladimir Kondratyev 	AcpiDetachData(dinfo->ad_handle, acpi_fake_objhandler);
929709749aaSVladimir Kondratyev }
930709749aaSVladimir Kondratyev 
931709749aaSVladimir Kondratyev /*
93215e32d5dSMike Smith  * Handle per-device ivars
93315e32d5dSMike Smith  */
93415e32d5dSMike Smith static int
93515e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
93615e32d5dSMike Smith {
93715e32d5dSMike Smith     struct acpi_device	*ad;
93815e32d5dSMike Smith 
93915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9402d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
94115e32d5dSMike Smith 	return (ENOENT);
94215e32d5dSMike Smith     }
94315e32d5dSMike Smith 
944be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
94515e32d5dSMike Smith     switch(index) {
94615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
94715e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
94815e32d5dSMike Smith 	break;
94915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
95015e32d5dSMike Smith 	*(void **)result = ad->ad_private;
95115e32d5dSMike Smith 	break;
952d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
953d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
954d4b9ff91SNate Lawson 	break;
95532d18aa5SMike Smith     case ISA_IVAR_VENDORID:
95632d18aa5SMike Smith     case ISA_IVAR_SERIAL:
95732d18aa5SMike Smith     case ISA_IVAR_COMPATID:
95832d18aa5SMike Smith 	*(int *)result = -1;
95932d18aa5SMike Smith 	break;
96032d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
96132d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
96232d18aa5SMike Smith 	break;
9630e68afe5SAndrew Turner     case PCI_IVAR_CLASS:
9640e68afe5SAndrew Turner 	*(uint8_t*)result = (ad->ad_cls_class >> 16) & 0xff;
9650e68afe5SAndrew Turner 	break;
9660e68afe5SAndrew Turner     case PCI_IVAR_SUBCLASS:
9670e68afe5SAndrew Turner 	*(uint8_t*)result = (ad->ad_cls_class >> 8) & 0xff;
9680e68afe5SAndrew Turner 	break;
9690e68afe5SAndrew Turner     case PCI_IVAR_PROGIF:
9700e68afe5SAndrew Turner 	*(uint8_t*)result = (ad->ad_cls_class >> 0) & 0xff;
9710e68afe5SAndrew Turner 	break;
97215e32d5dSMike Smith     default:
97315e32d5dSMike Smith 	return (ENOENT);
97415e32d5dSMike Smith     }
975be2b1797SNate Lawson 
97615e32d5dSMike Smith     return (0);
97715e32d5dSMike Smith }
97815e32d5dSMike Smith 
97915e32d5dSMike Smith static int
98015e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
98115e32d5dSMike Smith {
98215e32d5dSMike Smith     struct acpi_device	*ad;
98315e32d5dSMike Smith 
98415e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9852d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
98615e32d5dSMike Smith 	return (ENOENT);
98715e32d5dSMike Smith     }
98815e32d5dSMike Smith 
98915e32d5dSMike Smith     switch(index) {
99015e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
99115e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
99215e32d5dSMike Smith 	break;
99315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
99415e32d5dSMike Smith 	ad->ad_private = (void *)value;
99515e32d5dSMike Smith 	break;
996d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
997d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
998d4b9ff91SNate Lawson 	break;
99915e32d5dSMike Smith     default:
10002ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
100115e32d5dSMike Smith 	return (ENOENT);
100215e32d5dSMike Smith     }
1003be2b1797SNate Lawson 
100415e32d5dSMike Smith     return (0);
100515e32d5dSMike Smith }
100615e32d5dSMike Smith 
100715e32d5dSMike Smith /*
100815e32d5dSMike Smith  * Handle child resource allocation/removal
100915e32d5dSMike Smith  */
101091233413SNate Lawson static struct resource_list *
101191233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
101215e32d5dSMike Smith {
101391233413SNate Lawson     struct acpi_device		*ad;
101415e32d5dSMike Smith 
101591233413SNate Lawson     ad = device_get_ivars(child);
101691233413SNate Lawson     return (&ad->ad_rl);
101715e32d5dSMike Smith }
101815e32d5dSMike Smith 
10190d484d24SJohn Baldwin static int
10200d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value)
10210d484d24SJohn Baldwin {
10220d484d24SJohn Baldwin     struct acpi_device *ad = device_get_ivars(dev);
10230d484d24SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
10240d484d24SJohn Baldwin     struct resource_list_entry *rle;
10250d484d24SJohn Baldwin 
10260d484d24SJohn Baldwin     STAILQ_FOREACH(rle, rl, link) {
10270d484d24SJohn Baldwin 	if (rle->type != type)
10280d484d24SJohn Baldwin 	    continue;
10290d484d24SJohn Baldwin 	if (rle->start <= value && rle->end >= value)
10300d484d24SJohn Baldwin 	    return (1);
10310d484d24SJohn Baldwin     }
10320d484d24SJohn Baldwin     return (0);
10330d484d24SJohn Baldwin }
10340d484d24SJohn Baldwin 
10350d484d24SJohn Baldwin /*
10360d484d24SJohn Baldwin  * Wire device unit numbers based on resource matches in hints.
10370d484d24SJohn Baldwin  */
10380d484d24SJohn Baldwin static void
10390d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
10400d484d24SJohn Baldwin     int *unitp)
10410d484d24SJohn Baldwin {
10420d484d24SJohn Baldwin     const char *s;
10430d484d24SJohn Baldwin     long value;
10440d484d24SJohn Baldwin     int line, matches, unit;
10450d484d24SJohn Baldwin 
10460d484d24SJohn Baldwin     /*
10470d484d24SJohn Baldwin      * Iterate over all the hints for the devices with the specified
10480d484d24SJohn Baldwin      * name to see if one's resources are a subset of this device.
10490d484d24SJohn Baldwin      */
10500d484d24SJohn Baldwin     line = 0;
10514571c92fSWarner Losh     while (resource_find_dev(&line, name, &unit, "at", NULL) == 0) {
10520d484d24SJohn Baldwin 	/* Must have an "at" for acpi or isa. */
10530d484d24SJohn Baldwin 	resource_string_value(name, unit, "at", &s);
10540d484d24SJohn Baldwin 	if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
10550d484d24SJohn Baldwin 	    strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0))
10560d484d24SJohn Baldwin 	    continue;
10570d484d24SJohn Baldwin 
10580d484d24SJohn Baldwin 	/*
10592fcd493cSJohn Baldwin 	 * Check for matching resources.  We must have at least one match.
10602fcd493cSJohn Baldwin 	 * Since I/O and memory resources cannot be shared, if we get a
10612fcd493cSJohn Baldwin 	 * match on either of those, ignore any mismatches in IRQs or DRQs.
10620d484d24SJohn Baldwin 	 *
10630d484d24SJohn Baldwin 	 * XXX: We may want to revisit this to be more lenient and wire
10640d484d24SJohn Baldwin 	 * as long as it gets one match.
10650d484d24SJohn Baldwin 	 */
10660d484d24SJohn Baldwin 	matches = 0;
10670d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "port", &value) == 0) {
10682fcd493cSJohn Baldwin 	    /*
10692fcd493cSJohn Baldwin 	     * Floppy drive controllers are notorious for having a
10702fcd493cSJohn Baldwin 	     * wide variety of resources not all of which include the
10712fcd493cSJohn Baldwin 	     * first port that is specified by the hint (typically
10722fcd493cSJohn Baldwin 	     * 0x3f0) (see the comment above fdc_isa_alloc_resources()
10732fcd493cSJohn Baldwin 	     * in fdc_isa.c).  However, they do all seem to include
10742fcd493cSJohn Baldwin 	     * port + 2 (e.g. 0x3f2) so for a floppy device, look for
10752fcd493cSJohn Baldwin 	     * 'value + 2' in the port resources instead of the hint
10762fcd493cSJohn Baldwin 	     * value.
10772fcd493cSJohn Baldwin 	     */
10782fcd493cSJohn Baldwin 	    if (strcmp(name, "fdc") == 0)
10792fcd493cSJohn Baldwin 		value += 2;
10800d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
10810d484d24SJohn Baldwin 		matches++;
10820d484d24SJohn Baldwin 	    else
10830d484d24SJohn Baldwin 		continue;
10840d484d24SJohn Baldwin 	}
10850d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "maddr", &value) == 0) {
10860d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
10870d484d24SJohn Baldwin 		matches++;
10880d484d24SJohn Baldwin 	    else
10890d484d24SJohn Baldwin 		continue;
10900d484d24SJohn Baldwin 	}
10912fcd493cSJohn Baldwin 	if (matches > 0)
10922fcd493cSJohn Baldwin 	    goto matched;
10930d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "irq", &value) == 0) {
10940d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
10950d484d24SJohn Baldwin 		matches++;
10960d484d24SJohn Baldwin 	    else
10970d484d24SJohn Baldwin 		continue;
10980d484d24SJohn Baldwin 	}
10990d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "drq", &value) == 0) {
11000d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
11010d484d24SJohn Baldwin 		matches++;
11020d484d24SJohn Baldwin 	    else
11030d484d24SJohn Baldwin 		continue;
11040d484d24SJohn Baldwin 	}
11050d484d24SJohn Baldwin 
11062fcd493cSJohn Baldwin     matched:
11070d484d24SJohn Baldwin 	if (matches > 0) {
11080d484d24SJohn Baldwin 	    /* We have a winner! */
11090d484d24SJohn Baldwin 	    *unitp = unit;
11100d484d24SJohn Baldwin 	    break;
11110d484d24SJohn Baldwin 	}
11120d484d24SJohn Baldwin     }
11130d484d24SJohn Baldwin }
11140d484d24SJohn Baldwin 
1115adad4744SNate Lawson /*
11168d791e5aSJohn Baldwin  * Fetch the NUMA domain for a device by mapping the value returned by
11178d791e5aSJohn Baldwin  * _PXM to a NUMA domain.  If the device does not have a _PXM method,
11188d791e5aSJohn Baldwin  * -2 is returned.  If any other error occurs, -1 is returned.
11195d18c60aSAdrian Chadd  */
11208d791e5aSJohn Baldwin static int
11218d791e5aSJohn Baldwin acpi_parse_pxm(device_t dev)
11225d18c60aSAdrian Chadd {
1123b6715dabSJeff Roberson #ifdef NUMA
1124bcc51ef4SMark Johnston #if defined(__i386__) || defined(__amd64__)
11258d791e5aSJohn Baldwin 	ACPI_HANDLE handle;
11268d791e5aSJohn Baldwin 	ACPI_STATUS status;
11278d791e5aSJohn Baldwin 	int pxm;
11285d18c60aSAdrian Chadd 
11298d791e5aSJohn Baldwin 	handle = acpi_get_handle(dev);
11308d791e5aSJohn Baldwin 	if (handle == NULL)
11318d791e5aSJohn Baldwin 		return (-2);
11328d791e5aSJohn Baldwin 	status = acpi_GetInteger(handle, "_PXM", &pxm);
11338d791e5aSJohn Baldwin 	if (ACPI_SUCCESS(status))
11348d791e5aSJohn Baldwin 		return (acpi_map_pxm_to_vm_domainid(pxm));
11358d791e5aSJohn Baldwin 	if (status == AE_NOT_FOUND)
11368d791e5aSJohn Baldwin 		return (-2);
11378a08b7d3SJohn Baldwin #endif
1138bcc51ef4SMark Johnston #endif
11398d791e5aSJohn Baldwin 	return (-1);
11408d791e5aSJohn Baldwin }
11418a08b7d3SJohn Baldwin 
11428d791e5aSJohn Baldwin int
11438d791e5aSJohn Baldwin acpi_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
11448d791e5aSJohn Baldwin     cpuset_t *cpuset)
11458d791e5aSJohn Baldwin {
11468d791e5aSJohn Baldwin 	int d, error;
11478d791e5aSJohn Baldwin 
11488d791e5aSJohn Baldwin 	d = acpi_parse_pxm(child);
11498d791e5aSJohn Baldwin 	if (d < 0)
11508d791e5aSJohn Baldwin 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
11518d791e5aSJohn Baldwin 
11528d791e5aSJohn Baldwin 	switch (op) {
11538d791e5aSJohn Baldwin 	case LOCAL_CPUS:
11548d791e5aSJohn Baldwin 		if (setsize != sizeof(cpuset_t))
11558d791e5aSJohn Baldwin 			return (EINVAL);
11568d791e5aSJohn Baldwin 		*cpuset = cpuset_domain[d];
11578a08b7d3SJohn Baldwin 		return (0);
11588d791e5aSJohn Baldwin 	case INTR_CPUS:
11598d791e5aSJohn Baldwin 		error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
11608d791e5aSJohn Baldwin 		if (error != 0)
11618d791e5aSJohn Baldwin 			return (error);
11628d791e5aSJohn Baldwin 		if (setsize != sizeof(cpuset_t))
11638d791e5aSJohn Baldwin 			return (EINVAL);
11648d791e5aSJohn Baldwin 		CPU_AND(cpuset, &cpuset_domain[d]);
11658d791e5aSJohn Baldwin 		return (0);
11668d791e5aSJohn Baldwin 	default:
11678d791e5aSJohn Baldwin 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
11688d791e5aSJohn Baldwin 	}
11695d18c60aSAdrian Chadd }
11705d18c60aSAdrian Chadd 
11715d18c60aSAdrian Chadd /*
11728d791e5aSJohn Baldwin  * Fetch the NUMA domain for the given device 'dev'.
1173ffcf962dSAdrian Chadd  *
1174ffcf962dSAdrian Chadd  * If a device has a _PXM method, map that to a NUMA domain.
11758d791e5aSJohn Baldwin  * Otherwise, pass the request up to the parent.
11768d791e5aSJohn Baldwin  * If there's no matching domain or the domain cannot be
11778d791e5aSJohn Baldwin  * determined, return ENOENT.
1178ffcf962dSAdrian Chadd  */
1179ffcf962dSAdrian Chadd int
1180ffcf962dSAdrian Chadd acpi_get_domain(device_t dev, device_t child, int *domain)
1181ffcf962dSAdrian Chadd {
11828d791e5aSJohn Baldwin 	int d;
1183ffcf962dSAdrian Chadd 
11848d791e5aSJohn Baldwin 	d = acpi_parse_pxm(child);
11858d791e5aSJohn Baldwin 	if (d >= 0) {
11868d791e5aSJohn Baldwin 		*domain = d;
11878a08b7d3SJohn Baldwin 		return (0);
11888d791e5aSJohn Baldwin 	}
11898d791e5aSJohn Baldwin 	if (d == -1)
11908d791e5aSJohn Baldwin 		return (ENOENT);
11915d18c60aSAdrian Chadd 
1192ffcf962dSAdrian Chadd 	/* No _PXM node; go up a level */
1193ffcf962dSAdrian Chadd 	return (bus_generic_get_domain(dev, child, domain));
1194ffcf962dSAdrian Chadd }
1195ffcf962dSAdrian Chadd 
1196ffcf962dSAdrian Chadd /*
1197adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
1198adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
1199adad4744SNate Lawson  */
1200adad4744SNate Lawson static int
1201adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
1202adad4744SNate Lawson {
1203adad4744SNate Lawson     struct resource *res;
1204adad4744SNate Lawson     struct resource_list *rl;
1205adad4744SNate Lawson     struct resource_list_entry *rle;
1206adad4744SNate Lawson     struct rman *rm;
1207da72d149SNate Lawson     device_t *children;
1208da72d149SNate Lawson     int child_count, i;
1209da72d149SNate Lawson 
1210da72d149SNate Lawson     /*
1211da72d149SNate Lawson      * Probe/attach any sysresource devices.  This would be unnecessary if we
1212da72d149SNate Lawson      * had multi-pass probe/attach.
1213da72d149SNate Lawson      */
1214da72d149SNate Lawson     if (device_get_children(dev, &children, &child_count) != 0)
1215da72d149SNate Lawson 	return (ENXIO);
1216da72d149SNate Lawson     for (i = 0; i < child_count; i++) {
12175efca36fSTakanori Watanabe 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1218da72d149SNate Lawson 	    device_probe_and_attach(children[i]);
1219da72d149SNate Lawson     }
1220da72d149SNate Lawson     free(children, M_TEMP);
1221adad4744SNate Lawson 
1222adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
1223be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
1224adad4744SNate Lawson 	if (rle->res != NULL) {
1225da1b038aSJustin Hibbits 	    device_printf(dev, "duplicate resource for %jx\n", rle->start);
1226adad4744SNate Lawson 	    continue;
1227adad4744SNate Lawson 	}
1228adad4744SNate Lawson 
1229adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
1230adad4744SNate Lawson 	switch (rle->type) {
1231adad4744SNate Lawson 	case SYS_RES_IOPORT:
1232adad4744SNate Lawson 	    rm = &acpi_rman_io;
1233adad4744SNate Lawson 	    break;
1234adad4744SNate Lawson 	case SYS_RES_MEMORY:
1235adad4744SNate Lawson 	    rm = &acpi_rman_mem;
1236adad4744SNate Lawson 	    break;
1237adad4744SNate Lawson 	default:
1238adad4744SNate Lawson 	    continue;
1239adad4744SNate Lawson 	}
1240adad4744SNate Lawson 
1241adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
1242adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
1243adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
1244adad4744SNate Lawson 	if (res != NULL) {
1245adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1246adad4744SNate Lawson 	    rle->res = res;
124744947d3aSJohn Baldwin 	} else if (bootverbose)
1248da1b038aSJustin Hibbits 	    device_printf(dev, "reservation of %jx, %jx (%d) failed\n",
1249adad4744SNate Lawson 		rle->start, rle->count, rle->type);
1250adad4744SNate Lawson     }
1251adad4744SNate Lawson     return (0);
1252adad4744SNate Lawson }
1253adad4744SNate Lawson 
1254ea233199SJohn Baldwin /*
1255ea233199SJohn Baldwin  * Reserve declared resources for devices found during attach once system
1256ea233199SJohn Baldwin  * resources have been allocated.
1257ea233199SJohn Baldwin  */
1258ea233199SJohn Baldwin static void
1259ea233199SJohn Baldwin acpi_reserve_resources(device_t dev)
1260ea233199SJohn Baldwin {
1261ea233199SJohn Baldwin     struct resource_list_entry *rle;
1262ea233199SJohn Baldwin     struct resource_list *rl;
1263ea233199SJohn Baldwin     struct acpi_device *ad;
1264ea233199SJohn Baldwin     struct acpi_softc *sc;
1265ea233199SJohn Baldwin     device_t *children;
1266ea233199SJohn Baldwin     int child_count, i;
1267ea233199SJohn Baldwin 
1268ea233199SJohn Baldwin     sc = device_get_softc(dev);
1269ea233199SJohn Baldwin     if (device_get_children(dev, &children, &child_count) != 0)
1270ea233199SJohn Baldwin 	return;
1271ea233199SJohn Baldwin     for (i = 0; i < child_count; i++) {
1272ea233199SJohn Baldwin 	ad = device_get_ivars(children[i]);
1273ea233199SJohn Baldwin 	rl = &ad->ad_rl;
1274ea233199SJohn Baldwin 
1275ea233199SJohn Baldwin 	/* Don't reserve system resources. */
12765efca36fSTakanori Watanabe 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0)
1277ea233199SJohn Baldwin 	    continue;
1278ea233199SJohn Baldwin 
1279ea233199SJohn Baldwin 	STAILQ_FOREACH(rle, rl, link) {
1280ea233199SJohn Baldwin 	    /*
1281ea233199SJohn Baldwin 	     * Don't reserve IRQ resources.  There are many sticky things
1282ea233199SJohn Baldwin 	     * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1283ea233199SJohn Baldwin 	     * when using legacy routing).
1284ea233199SJohn Baldwin 	     */
1285ea233199SJohn Baldwin 	    if (rle->type == SYS_RES_IRQ)
1286ea233199SJohn Baldwin 		continue;
1287ea233199SJohn Baldwin 
1288ea233199SJohn Baldwin 	    /*
12890d81cf12SJohn Baldwin 	     * Don't reserve the resource if it is already allocated.
12900d81cf12SJohn Baldwin 	     * The acpi_ec(4) driver can allocate its resources early
12910d81cf12SJohn Baldwin 	     * if ECDT is present.
12920d81cf12SJohn Baldwin 	     */
12930d81cf12SJohn Baldwin 	    if (rle->res != NULL)
12940d81cf12SJohn Baldwin 		continue;
12950d81cf12SJohn Baldwin 
12960d81cf12SJohn Baldwin 	    /*
1297ea233199SJohn Baldwin 	     * Try to reserve the resource from our parent.  If this
1298ea233199SJohn Baldwin 	     * fails because the resource is a system resource, just
1299ea233199SJohn Baldwin 	     * let it be.  The resource range is already reserved so
1300ea233199SJohn Baldwin 	     * that other devices will not use it.  If the driver
1301ea233199SJohn Baldwin 	     * needs to allocate the resource, then
1302ea233199SJohn Baldwin 	     * acpi_alloc_resource() will sub-alloc from the system
1303ea233199SJohn Baldwin 	     * resource.
1304ea233199SJohn Baldwin 	     */
1305ea233199SJohn Baldwin 	    resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1306ea233199SJohn Baldwin 		rle->start, rle->end, rle->count, 0);
1307ea233199SJohn Baldwin 	}
1308ea233199SJohn Baldwin     }
1309ea233199SJohn Baldwin     free(children, M_TEMP);
1310ea233199SJohn Baldwin     sc->acpi_resources_reserved = 1;
1311ea233199SJohn Baldwin }
1312ea233199SJohn Baldwin 
1313ea233199SJohn Baldwin static int
1314ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid,
13152dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t count)
1316ea233199SJohn Baldwin {
1317ea233199SJohn Baldwin     struct acpi_softc *sc = device_get_softc(dev);
1318ea233199SJohn Baldwin     struct acpi_device *ad = device_get_ivars(child);
1319ea233199SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
13207e3343bfSJohn Baldwin     ACPI_DEVICE_INFO *devinfo;
13212dd1bdf1SJustin Hibbits     rman_res_t end;
1322185c34f7SJayachandran C.     int allow;
1323ea233199SJohn Baldwin 
1324ea233199SJohn Baldwin     /* Ignore IRQ resources for PCI link devices. */
13255efca36fSTakanori Watanabe     if (type == SYS_RES_IRQ &&
13265efca36fSTakanori Watanabe 	ACPI_ID_PROBE(dev, child, pcilink_ids, NULL) <= 0)
1327ea233199SJohn Baldwin 	return (0);
1328ea233199SJohn Baldwin 
13297e3343bfSJohn Baldwin     /*
1330d2dc06caSJohn Baldwin      * Ignore most resources for PCI root bridges.  Some BIOSes
13317e3343bfSJohn Baldwin      * incorrectly enumerate the memory ranges they decode as plain
1332d2dc06caSJohn Baldwin      * memory resources instead of as ResourceProducer ranges.  Other
1333d2dc06caSJohn Baldwin      * BIOSes incorrectly list system resource entries for I/O ranges
1334d2dc06caSJohn Baldwin      * under the PCI bridge.  Do allow the one known-correct case on
1335d2dc06caSJohn Baldwin      * x86 of a PCI bridge claiming the I/O ports used for PCI config
1336d2dc06caSJohn Baldwin      * access.
13377e3343bfSJohn Baldwin      */
1338d2dc06caSJohn Baldwin     if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
13397e3343bfSJohn Baldwin 	if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) {
13407e3343bfSJohn Baldwin 	    if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) {
1341185c34f7SJayachandran C. #if defined(__i386__) || defined(__amd64__)
1342185c34f7SJayachandran C. 		allow = (type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT);
1343185c34f7SJayachandran C. #else
1344185c34f7SJayachandran C. 		allow = 0;
1345185c34f7SJayachandran C. #endif
1346185c34f7SJayachandran C. 		if (!allow) {
13477e3343bfSJohn Baldwin 		    AcpiOsFree(devinfo);
13487e3343bfSJohn Baldwin 		    return (0);
13497e3343bfSJohn Baldwin 		}
1350d2dc06caSJohn Baldwin 	    }
13517e3343bfSJohn Baldwin 	    AcpiOsFree(devinfo);
13527e3343bfSJohn Baldwin 	}
13537e3343bfSJohn Baldwin     }
13547e3343bfSJohn Baldwin 
1355d4d6ad3fSJayachandran C. #ifdef INTRNG
1356d4d6ad3fSJayachandran C.     /* map with default for now */
1357d4d6ad3fSJayachandran C.     if (type == SYS_RES_IRQ)
1358d4d6ad3fSJayachandran C. 	start = (rman_res_t)acpi_map_intr(child, (u_int)start,
1359d4d6ad3fSJayachandran C. 			acpi_get_handle(child));
1360d4d6ad3fSJayachandran C. #endif
1361d4d6ad3fSJayachandran C. 
1362ea233199SJohn Baldwin     /* If the resource is already allocated, fail. */
1363ea233199SJohn Baldwin     if (resource_list_busy(rl, type, rid))
1364ea233199SJohn Baldwin 	return (EBUSY);
1365ea233199SJohn Baldwin 
1366ea233199SJohn Baldwin     /* If the resource is already reserved, release it. */
1367ea233199SJohn Baldwin     if (resource_list_reserved(rl, type, rid))
1368ea233199SJohn Baldwin 	resource_list_unreserve(rl, dev, child, type, rid);
1369ea233199SJohn Baldwin 
1370ea233199SJohn Baldwin     /* Add the resource. */
1371ea233199SJohn Baldwin     end = (start + count - 1);
1372ea233199SJohn Baldwin     resource_list_add(rl, type, rid, start, end, count);
1373ea233199SJohn Baldwin 
1374ea233199SJohn Baldwin     /* Don't reserve resources until the system resources are allocated. */
1375ea233199SJohn Baldwin     if (!sc->acpi_resources_reserved)
1376ea233199SJohn Baldwin 	return (0);
1377ea233199SJohn Baldwin 
1378ea233199SJohn Baldwin     /* Don't reserve system resources. */
13795efca36fSTakanori Watanabe     if (ACPI_ID_PROBE(dev, child, sysres_ids, NULL) <= 0)
1380ea233199SJohn Baldwin 	return (0);
1381ea233199SJohn Baldwin 
1382ea233199SJohn Baldwin     /*
1383ea233199SJohn Baldwin      * Don't reserve IRQ resources.  There are many sticky things to
1384ea233199SJohn Baldwin      * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when
1385ea233199SJohn Baldwin      * using legacy routing).
1386ea233199SJohn Baldwin      */
1387ea233199SJohn Baldwin     if (type == SYS_RES_IRQ)
1388ea233199SJohn Baldwin 	return (0);
1389ea233199SJohn Baldwin 
1390ea233199SJohn Baldwin     /*
13911fe39413SJohn Baldwin      * Don't reserve resources for CPU devices.  Some of these
13921fe39413SJohn Baldwin      * resources need to be allocated as shareable, but reservations
13931fe39413SJohn Baldwin      * are always non-shareable.
13941fe39413SJohn Baldwin      */
13951fe39413SJohn Baldwin     if (device_get_devclass(child) == devclass_find("cpu"))
13961fe39413SJohn Baldwin 	return (0);
13971fe39413SJohn Baldwin 
13981fe39413SJohn Baldwin     /*
1399ea233199SJohn Baldwin      * Reserve the resource.
1400ea233199SJohn Baldwin      *
1401ea233199SJohn Baldwin      * XXX: Ignores failure for now.  Failure here is probably a
1402ea233199SJohn Baldwin      * BIOS/firmware bug?
1403ea233199SJohn Baldwin      */
1404ea233199SJohn Baldwin     resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0);
1405ea233199SJohn Baldwin     return (0);
1406ea233199SJohn Baldwin }
1407ea233199SJohn Baldwin 
140815e32d5dSMike Smith static struct resource *
140915e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
14102dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
141115e32d5dSMike Smith {
1412224c3776SAndrew Turner #ifndef INTRNG
141395957f62SJohn Baldwin     ACPI_RESOURCE ares;
1414224c3776SAndrew Turner #endif
1415ea233199SJohn Baldwin     struct acpi_device *ad;
141691233413SNate Lawson     struct resource_list_entry *rle;
1417ea233199SJohn Baldwin     struct resource_list *rl;
141891233413SNate Lawson     struct resource *res;
14197915adb5SJustin Hibbits     int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
142015e2f34fSNate Lawson 
142191233413SNate Lawson     /*
1422ea233199SJohn Baldwin      * First attempt at allocating the resource.  For direct children,
1423ea233199SJohn Baldwin      * use resource_list_alloc() to handle reserved resources.  For
1424f6133e71SJohn Baldwin      * other devices, pass the request up to our parent.
142591233413SNate Lawson      */
1426ea233199SJohn Baldwin     if (bus == device_get_parent(child)) {
1427ea233199SJohn Baldwin 	ad = device_get_ivars(child);
1428ea233199SJohn Baldwin 	rl = &ad->ad_rl;
142991233413SNate Lawson 
1430397c30a8SJohn Baldwin 	/*
1431ea233199SJohn Baldwin 	 * Simulate the behavior of the ISA bus for direct children
1432ea233199SJohn Baldwin 	 * devices.  That is, if a non-default range is specified for
1433ea233199SJohn Baldwin 	 * a resource that doesn't exist, use bus_set_resource() to
1434ea233199SJohn Baldwin 	 * add the resource before allocating it.  Note that these
1435ea233199SJohn Baldwin 	 * resources will not be reserved.
1436397c30a8SJohn Baldwin 	 */
1437ea233199SJohn Baldwin 	if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1438ea233199SJohn Baldwin 		resource_list_add(rl, type, *rid, start, end, count);
1439ea233199SJohn Baldwin 	res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1440ea233199SJohn Baldwin 	    flags);
1441224c3776SAndrew Turner #ifndef INTRNG
1442ea233199SJohn Baldwin 	if (res != NULL && type == SYS_RES_IRQ) {
144395957f62SJohn Baldwin 	    /*
144495957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
144595957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
144695957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
144795957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
144895957f62SJohn Baldwin 	     *
144995957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
145095957f62SJohn Baldwin 	     */
145195957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
145295957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
145395957f62SJohn Baldwin 	}
1454224c3776SAndrew Turner #endif
145515e2f34fSNate Lawson 
1456ea233199SJohn Baldwin 	/*
1457ea233199SJohn Baldwin 	 * If this is an allocation of the "default" range for a given
1458ea233199SJohn Baldwin 	 * RID, fetch the exact bounds for this resource from the
1459ea233199SJohn Baldwin 	 * resource list entry to try to allocate the range from the
1460ea233199SJohn Baldwin 	 * system resource regions.
1461ea233199SJohn Baldwin 	 */
1462ea233199SJohn Baldwin 	if (res == NULL && isdefault) {
1463ea233199SJohn Baldwin 	    rle = resource_list_find(rl, type, *rid);
1464ea233199SJohn Baldwin 	    if (rle != NULL) {
1465ea233199SJohn Baldwin 		start = rle->start;
1466ea233199SJohn Baldwin 		end = rle->end;
1467ea233199SJohn Baldwin 		count = rle->count;
1468ea233199SJohn Baldwin 	    }
1469ea233199SJohn Baldwin 	}
1470ea233199SJohn Baldwin     } else
1471ea233199SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
1472ea233199SJohn Baldwin 	    start, end, count, flags);
1473ea233199SJohn Baldwin 
1474ea233199SJohn Baldwin     /*
1475ea233199SJohn Baldwin      * If the first attempt failed and this is an allocation of a
1476ea233199SJohn Baldwin      * specific range, try to satisfy the request via a suballocation
14775d0d779bSJohn Baldwin      * from our system resource regions.
1478ea233199SJohn Baldwin      */
14795d0d779bSJohn Baldwin     if (res == NULL && start + count - 1 == end)
14805d0d779bSJohn Baldwin 	res = acpi_alloc_sysres(child, type, rid, start, end, count, flags);
14815d0d779bSJohn Baldwin     return (res);
14825d0d779bSJohn Baldwin }
14835d0d779bSJohn Baldwin 
14845d0d779bSJohn Baldwin /*
14855d0d779bSJohn Baldwin  * Attempt to allocate a specific resource range from the system
14865d0d779bSJohn Baldwin  * resource ranges.  Note that we only handle memory and I/O port
14875d0d779bSJohn Baldwin  * system resources.
14885d0d779bSJohn Baldwin  */
14895d0d779bSJohn Baldwin struct resource *
14902dd1bdf1SJustin Hibbits acpi_alloc_sysres(device_t child, int type, int *rid, rman_res_t start,
14912dd1bdf1SJustin Hibbits     rman_res_t end, rman_res_t count, u_int flags)
14925d0d779bSJohn Baldwin {
14935d0d779bSJohn Baldwin     struct rman *rm;
14945d0d779bSJohn Baldwin     struct resource *res;
14955d0d779bSJohn Baldwin 
1496ea233199SJohn Baldwin     switch (type) {
1497ea233199SJohn Baldwin     case SYS_RES_IOPORT:
1498ea233199SJohn Baldwin 	rm = &acpi_rman_io;
1499ea233199SJohn Baldwin 	break;
1500ea233199SJohn Baldwin     case SYS_RES_MEMORY:
1501ea233199SJohn Baldwin 	rm = &acpi_rman_mem;
1502ea233199SJohn Baldwin 	break;
1503ea233199SJohn Baldwin     default:
1504ea233199SJohn Baldwin 	return (NULL);
1505ea233199SJohn Baldwin     }
1506ea233199SJohn Baldwin 
15075d0d779bSJohn Baldwin     KASSERT(start + count - 1 == end, ("wildcard resource range"));
1508ea233199SJohn Baldwin     res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1509ea233199SJohn Baldwin 	child);
1510ea233199SJohn Baldwin     if (res == NULL)
1511ea233199SJohn Baldwin 	return (NULL);
1512ea233199SJohn Baldwin 
1513ea233199SJohn Baldwin     rman_set_rid(res, *rid);
1514ea233199SJohn Baldwin 
1515ea233199SJohn Baldwin     /* If requested, activate the resource using the parent's method. */
1516ea233199SJohn Baldwin     if (flags & RF_ACTIVE)
1517ea233199SJohn Baldwin 	if (bus_activate_resource(child, type, *rid, res) != 0) {
1518ea233199SJohn Baldwin 	    rman_release_resource(res);
1519ea233199SJohn Baldwin 	    return (NULL);
1520ea233199SJohn Baldwin 	}
1521ea233199SJohn Baldwin 
152291233413SNate Lawson     return (res);
152315e32d5dSMike Smith }
152415e32d5dSMike Smith 
152515e32d5dSMike Smith static int
1526049dc0d1SJohn Baldwin acpi_is_resource_managed(int type, struct resource *r)
152715e32d5dSMike Smith {
152815e32d5dSMike Smith 
1529397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1530397c30a8SJohn Baldwin     switch (type) {
1531397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1532049dc0d1SJohn Baldwin 	return (rman_is_region_manager(r, &acpi_rman_io));
1533397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1534049dc0d1SJohn Baldwin 	return (rman_is_region_manager(r, &acpi_rman_mem));
1535397c30a8SJohn Baldwin     }
1536049dc0d1SJohn Baldwin     return (0);
1537049dc0d1SJohn Baldwin }
1538049dc0d1SJohn Baldwin 
1539049dc0d1SJohn Baldwin static int
1540049dc0d1SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
15412dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end)
1542049dc0d1SJohn Baldwin {
1543049dc0d1SJohn Baldwin 
1544049dc0d1SJohn Baldwin     if (acpi_is_resource_managed(type, r))
1545049dc0d1SJohn Baldwin 	return (rman_adjust_resource(r, start, end));
1546049dc0d1SJohn Baldwin     return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1547049dc0d1SJohn Baldwin }
1548049dc0d1SJohn Baldwin 
1549049dc0d1SJohn Baldwin static int
1550049dc0d1SJohn Baldwin acpi_release_resource(device_t bus, device_t child, int type, int rid,
1551049dc0d1SJohn Baldwin     struct resource *r)
1552049dc0d1SJohn Baldwin {
1553049dc0d1SJohn Baldwin     int ret;
1554397c30a8SJohn Baldwin 
155591233413SNate Lawson     /*
1556397c30a8SJohn Baldwin      * If this resource belongs to one of our internal managers,
1557ea233199SJohn Baldwin      * deactivate it and release it to the local pool.
155891233413SNate Lawson      */
1559049dc0d1SJohn Baldwin     if (acpi_is_resource_managed(type, r)) {
156091233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
156191233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
156291233413SNate Lawson 	    if (ret != 0)
156391233413SNate Lawson 		return (ret);
156415e32d5dSMike Smith 	}
1565ea233199SJohn Baldwin 	return (rman_release_resource(r));
1566ea233199SJohn Baldwin     }
1567ea233199SJohn Baldwin 
1568ea233199SJohn Baldwin     return (bus_generic_rl_release_resource(bus, child, type, rid, r));
1569ea233199SJohn Baldwin }
157015e32d5dSMike Smith 
15718b28b622SNate Lawson static void
15728b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
15738b28b622SNate Lawson {
15748b28b622SNate Lawson     struct resource_list *rl;
15758b28b622SNate Lawson 
15768b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
1577ea233199SJohn Baldwin     if (resource_list_busy(rl, type, rid)) {
1578ea233199SJohn Baldwin 	device_printf(bus, "delete_resource: Resource still owned by child"
1579ea233199SJohn Baldwin 	    " (type=%d, rid=%d)\n", type, rid);
1580ea233199SJohn Baldwin 	return;
1581ea233199SJohn Baldwin     }
1582ea233199SJohn Baldwin     resource_list_unreserve(rl, bus, child, type, rid);
15838b28b622SNate Lawson     resource_list_delete(rl, type, rid);
15848b28b622SNate Lawson }
15858b28b622SNate Lawson 
1586dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1587e1c4bf3fSNate Lawson int
1588e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1589907b6777SNate Lawson     struct resource **res, u_int flags)
1590dc750869SNate Lawson {
1591e1c4bf3fSNate Lawson     int error, res_type;
1592dc750869SNate Lawson 
1593e1c4bf3fSNate Lawson     error = ENOMEM;
15948f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1595e1c4bf3fSNate Lawson 	return (EINVAL);
1596dc750869SNate Lawson 
15978f118e25SNate Lawson     /* We only support memory and IO spaces. */
15982be4e471SJung-uk Kim     switch (gas->SpaceId) {
1599dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1600e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1601dc750869SNate Lawson 	break;
1602dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1603e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1604dc750869SNate Lawson 	break;
1605dc750869SNate Lawson     default:
1606e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1607dc750869SNate Lawson     }
1608dc750869SNate Lawson 
16092fe912dfSNate Lawson     /*
1610f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1611f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
16122fe912dfSNate Lawson      */
16132be4e471SJung-uk Kim     if (gas->BitWidth && gas->BitWidth < 8)
16142be4e471SJung-uk Kim 	gas->BitWidth = 8;
16152fe912dfSNate Lawson 
16168f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
16172be4e471SJung-uk Kim     if (gas->Address == 0 || gas->BitWidth == 0)
16188f118e25SNate Lawson 	return (EINVAL);
16198f118e25SNate Lawson 
1620e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
16212be4e471SJung-uk Kim 	gas->BitWidth / 8);
1622907b6777SNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1623e1c4bf3fSNate Lawson     if (*res != NULL) {
1624e1c4bf3fSNate Lawson 	*type = res_type;
1625e1c4bf3fSNate Lawson 	error = 0;
1626ca2c69c8SNate Lawson     } else
1627ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1628ca2c69c8SNate Lawson 
1629e1c4bf3fSNate Lawson     return (error);
1630dc750869SNate Lawson }
1631dc750869SNate Lawson 
1632c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
16331e4925e8SNate Lawson static uint32_t
163432d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1635bc0f2195SMike Smith {
16361e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1637bc0f2195SMike Smith     ACPI_HANDLE		h;
163892488a57SJung-uk Kim     uint32_t		pnpid;
163932d18aa5SMike Smith 
1640b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
164132d18aa5SMike Smith 
16426fca9360SNate Lawson     /* Fetch and validate the HID. */
164392488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
164492488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
164592488a57SJung-uk Kim 	return_VALUE (0);
164632d18aa5SMike Smith 
164792488a57SJung-uk Kim     pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
164892488a57SJung-uk Kim 	devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
164992488a57SJung-uk Kim 	PNP_EISAID(devinfo->HardwareId.String) : 0;
165092488a57SJung-uk Kim     AcpiOsFree(devinfo);
1651be2b1797SNate Lawson 
165232d18aa5SMike Smith     return_VALUE (pnpid);
165332d18aa5SMike Smith }
165432d18aa5SMike Smith 
16551e4925e8SNate Lawson static int
16561e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1657b2566207STakanori Watanabe {
16581e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
16598ef1a331SJung-uk Kim     ACPI_PNP_DEVICE_ID	*ids;
1660b2566207STakanori Watanabe     ACPI_HANDLE		h;
16611e4925e8SNate Lawson     uint32_t		*pnpid;
166292488a57SJung-uk Kim     int			i, valid;
1663b2566207STakanori Watanabe 
1664b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1665b2566207STakanori Watanabe 
16661e4925e8SNate Lawson     pnpid = cids;
1667bd189fe7SNate Lawson 
16681e4925e8SNate Lawson     /* Fetch and validate the CID */
166992488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
167092488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
167192488a57SJung-uk Kim 	return_VALUE (0);
1672b2566207STakanori Watanabe 
167392488a57SJung-uk Kim     if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
167492488a57SJung-uk Kim 	AcpiOsFree(devinfo);
167592488a57SJung-uk Kim 	return_VALUE (0);
1676b2566207STakanori Watanabe     }
1677b2566207STakanori Watanabe 
167892488a57SJung-uk Kim     if (devinfo->CompatibleIdList.Count < count)
167992488a57SJung-uk Kim 	count = devinfo->CompatibleIdList.Count;
168092488a57SJung-uk Kim     ids = devinfo->CompatibleIdList.Ids;
168192488a57SJung-uk Kim     for (i = 0, valid = 0; i < count; i++)
168292488a57SJung-uk Kim 	if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
168392488a57SJung-uk Kim 	    strncmp(ids[i].String, "PNP", 3) == 0) {
168492488a57SJung-uk Kim 	    *pnpid++ = PNP_EISAID(ids[i].String);
168592488a57SJung-uk Kim 	    valid++;
168692488a57SJung-uk Kim 	}
168792488a57SJung-uk Kim     AcpiOsFree(devinfo);
168892488a57SJung-uk Kim 
16891e4925e8SNate Lawson     return_VALUE (valid);
16901e4925e8SNate Lawson }
1691b2566207STakanori Watanabe 
16925efca36fSTakanori Watanabe static int
16935efca36fSTakanori Watanabe acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match)
1694ce619b2eSNate Lawson {
1695ce619b2eSNate Lawson     ACPI_HANDLE h;
169692488a57SJung-uk Kim     ACPI_OBJECT_TYPE t;
16975efca36fSTakanori Watanabe     int rv;
1698ce619b2eSNate Lawson     int i;
1699ce619b2eSNate Lawson 
1700ce619b2eSNate Lawson     h = acpi_get_handle(dev);
170192488a57SJung-uk Kim     if (ids == NULL || h == NULL)
17025efca36fSTakanori Watanabe 	return (ENXIO);
170392488a57SJung-uk Kim     t = acpi_get_type(dev);
170492488a57SJung-uk Kim     if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
17055efca36fSTakanori Watanabe 	return (ENXIO);
1706ce619b2eSNate Lawson 
1707ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1708ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
17095efca36fSTakanori Watanabe 	rv = acpi_MatchHid(h, ids[i]);
17105efca36fSTakanori Watanabe 	if (rv == ACPI_MATCHHID_NOMATCH)
17115efca36fSTakanori Watanabe 	    continue;
17125efca36fSTakanori Watanabe 
17135efca36fSTakanori Watanabe 	if (match != NULL) {
17145efca36fSTakanori Watanabe 	    *match = ids[i];
1715ce619b2eSNate Lawson 	}
17165efca36fSTakanori Watanabe 	return ((rv == ACPI_MATCHHID_HID)?
17175efca36fSTakanori Watanabe 		    BUS_PROBE_DEFAULT : BUS_PROBE_LOW_PRIORITY);
17185efca36fSTakanori Watanabe     }
17195efca36fSTakanori Watanabe     return (ENXIO);
1720ce619b2eSNate Lawson }
1721ce619b2eSNate Lawson 
1722ce619b2eSNate Lawson static ACPI_STATUS
1723ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1724ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1725ce619b2eSNate Lawson {
1726ce619b2eSNate Lawson     ACPI_HANDLE h;
1727ce619b2eSNate Lawson 
1728df8d2a32SNate Lawson     if (dev == NULL)
1729df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1730df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1731ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1732ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1733ce619b2eSNate Lawson }
1734ce619b2eSNate Lawson 
173562508c53SJohn Baldwin int
173610ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
173710ce62b9SNate Lawson {
173810ce62b9SNate Lawson     struct acpi_softc *sc;
173910ce62b9SNate Lawson     ACPI_HANDLE handle;
174010ce62b9SNate Lawson     ACPI_STATUS status;
174110ce62b9SNate Lawson     char sxd[8];
174210ce62b9SNate Lawson 
174310ce62b9SNate Lawson     handle = acpi_get_handle(dev);
174410ce62b9SNate Lawson 
174510ce62b9SNate Lawson     /*
174610ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
174710ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
174810ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
174910ce62b9SNate Lawson      * need special handling in their drivers.
175010ce62b9SNate Lawson      */
1751a7a3177fSJung-uk Kim     if (dstate == NULL || handle == NULL ||
175210ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
175310ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
175410ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
175510ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
175610ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
175710ce62b9SNate Lawson 	return (ENXIO);
175810ce62b9SNate Lawson 
175910ce62b9SNate Lawson     /*
1760a7a3177fSJung-uk Kim      * Override next state with the value from _SxD, if present.
1761a7a3177fSJung-uk Kim      * Note illegal _S0D is evaluated because some systems expect this.
176210ce62b9SNate Lawson      */
1763a7a3177fSJung-uk Kim     sc = device_get_softc(bus);
176410ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
176510ce62b9SNate Lawson     status = acpi_GetInteger(handle, sxd, dstate);
1766a7a3177fSJung-uk Kim     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
1767a7a3177fSJung-uk Kim 	    device_printf(dev, "failed to get %s on %s: %s\n", sxd,
1768a7a3177fSJung-uk Kim 		acpi_name(handle), AcpiFormatException(status));
1769a7a3177fSJung-uk Kim 	    return (ENXIO);
177010ce62b9SNate Lawson     }
177110ce62b9SNate Lawson 
1772a7a3177fSJung-uk Kim     return (0);
177310ce62b9SNate Lawson }
177410ce62b9SNate Lawson 
1775df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1776df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1777df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1778df8d2a32SNate Lawson     void		*arg;
1779df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1780df8d2a32SNate Lawson };
1781df8d2a32SNate Lawson 
1782ce619b2eSNate Lawson static ACPI_STATUS
1783df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1784df8d2a32SNate Lawson {
1785df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1786df8d2a32SNate Lawson     device_t dev, old_dev;
1787df8d2a32SNate Lawson     ACPI_STATUS status;
1788df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1789df8d2a32SNate Lawson 
1790df8d2a32SNate Lawson     /*
1791df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1792df8d2a32SNate Lawson      * the parent where the scan began.
1793df8d2a32SNate Lawson      */
1794df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1795df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1796df8d2a32SNate Lawson 	return (AE_OK);
1797df8d2a32SNate Lawson 
1798df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1799df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1800df8d2a32SNate Lawson 	return (AE_OK);
1801df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1802df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1803df8d2a32SNate Lawson 	return (AE_OK);
1804df8d2a32SNate Lawson 
1805df8d2a32SNate Lawson     /*
1806df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1807df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1808df8d2a32SNate Lawson      */
1809df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1810df8d2a32SNate Lawson     dev = old_dev;
1811df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1812df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1813df8d2a32SNate Lawson 	return (status);
1814df8d2a32SNate Lawson 
1815df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1816709749aaSVladimir Kondratyev     if (old_dev != NULL)
1817df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1818df8d2a32SNate Lawson 
1819df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1820df8d2a32SNate Lawson     if (dev != NULL)
1821df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1822df8d2a32SNate Lawson 
1823df8d2a32SNate Lawson     return (AE_OK);
1824df8d2a32SNate Lawson }
1825df8d2a32SNate Lawson 
1826df8d2a32SNate Lawson static ACPI_STATUS
1827df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1828df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1829ce619b2eSNate Lawson {
1830ce619b2eSNate Lawson     ACPI_HANDLE h;
1831df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1832ce619b2eSNate Lawson 
1833df8d2a32SNate Lawson     if (acpi_disabled("children"))
1834df8d2a32SNate Lawson 	return (AE_OK);
1835df8d2a32SNate Lawson 
1836df8d2a32SNate Lawson     if (dev == NULL)
1837df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1838df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1839ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1840df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1841df8d2a32SNate Lawson     ctx.arg = arg;
1842df8d2a32SNate Lawson     ctx.parent = h;
1843df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
18442272d050SJung-uk Kim 	acpi_device_scan_cb, NULL, &ctx, NULL));
1845ce619b2eSNate Lawson }
1846ce619b2eSNate Lawson 
184710ce62b9SNate Lawson /*
184810ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
184910ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
185010ce62b9SNate Lawson  */
1851ddf8c230SVladimir Kondratyev int
1852a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state)
185310ce62b9SNate Lawson {
185410ce62b9SNate Lawson     ACPI_HANDLE h;
185510ce62b9SNate Lawson     ACPI_STATUS status;
185610ce62b9SNate Lawson 
185710ce62b9SNate Lawson     h = acpi_get_handle(child);
1858a0e73a12SJung-uk Kim     if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
185910ce62b9SNate Lawson 	return (EINVAL);
186010ce62b9SNate Lawson     if (h == NULL)
186110ce62b9SNate Lawson 	return (0);
186210ce62b9SNate Lawson 
186310ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
186410ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
1865a7a3177fSJung-uk Kim     if (ACPI_SUCCESS(status)) {
1866a7a3177fSJung-uk Kim 	if (bootverbose)
1867a7a3177fSJung-uk Kim 	    device_printf(child, "set ACPI power state D%d on %s\n",
1868a7a3177fSJung-uk Kim 		state, acpi_name(h));
1869a7a3177fSJung-uk Kim     } else if (status != AE_NOT_FOUND)
1870a7a3177fSJung-uk Kim 	device_printf(child,
1871a7a3177fSJung-uk Kim 	    "failed to set ACPI power state D%d on %s: %s\n", state,
1872a7a3177fSJung-uk Kim 	    acpi_name(h), AcpiFormatException(status));
187310ce62b9SNate Lawson 
1874a7a3177fSJung-uk Kim     return (0);
187510ce62b9SNate Lawson }
187610ce62b9SNate Lawson 
187732d18aa5SMike Smith static int
187832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
187932d18aa5SMike Smith {
18801e4925e8SNate Lawson     int			result, cid_count, i;
18811e4925e8SNate Lawson     uint32_t		lid, cids[8];
1882bc0f2195SMike Smith 
1883b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1884bc0f2195SMike Smith 
1885bc0f2195SMike Smith     /*
1886bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1887bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1888bc0f2195SMike Smith      * that to happen, so don't ever return it.
1889bc0f2195SMike Smith      */
1890bc0f2195SMike Smith     result = ENXIO;
1891bc0f2195SMike Smith 
1892be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1893b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
18941e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1895bc0f2195SMike Smith     while (ids && ids->ip_id) {
18961e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1897bc0f2195SMike Smith 	    result = 0;
1898bc0f2195SMike Smith 	    goto out;
1899bc0f2195SMike Smith 	}
19001e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
19011e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
19021e4925e8SNate Lawson 		result = 0;
19031e4925e8SNate Lawson 		goto out;
19041e4925e8SNate Lawson 	    }
19051e4925e8SNate Lawson 	}
1906bc0f2195SMike Smith 	ids++;
1907bc0f2195SMike Smith     }
1908be2b1797SNate Lawson 
1909bc0f2195SMike Smith  out:
19109e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
19119e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
19129e0dd54fSNate Lawson 
1913bc0f2195SMike Smith     return_VALUE (result);
1914bc0f2195SMike Smith }
1915bc0f2195SMike Smith 
1916d320e05cSJohn Baldwin /*
1917d320e05cSJohn Baldwin  * Look for a MCFG table.  If it is present, use the settings for
1918d320e05cSJohn Baldwin  * domain (segment) 0 to setup PCI config space access via the memory
1919d320e05cSJohn Baldwin  * map.
1920185c34f7SJayachandran C.  *
1921185c34f7SJayachandran C.  * On non-x86 architectures (arm64 for now), this will be done from the
1922185c34f7SJayachandran C.  * PCI host bridge driver.
1923d320e05cSJohn Baldwin  */
1924d320e05cSJohn Baldwin static void
1925d320e05cSJohn Baldwin acpi_enable_pcie(void)
1926d320e05cSJohn Baldwin {
1927185c34f7SJayachandran C. #if defined(__i386__) || defined(__amd64__)
1928d320e05cSJohn Baldwin 	ACPI_TABLE_HEADER *hdr;
1929d320e05cSJohn Baldwin 	ACPI_MCFG_ALLOCATION *alloc, *end;
1930d320e05cSJohn Baldwin 	ACPI_STATUS status;
1931d320e05cSJohn Baldwin 
1932d320e05cSJohn Baldwin 	status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1933d320e05cSJohn Baldwin 	if (ACPI_FAILURE(status))
1934d320e05cSJohn Baldwin 		return;
1935d320e05cSJohn Baldwin 
1936d320e05cSJohn Baldwin 	end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1937d320e05cSJohn Baldwin 	alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1938d320e05cSJohn Baldwin 	while (alloc < end) {
1939d320e05cSJohn Baldwin 		if (alloc->PciSegment == 0) {
1940d320e05cSJohn Baldwin 			pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1941d320e05cSJohn Baldwin 			    alloc->EndBusNumber);
1942d320e05cSJohn Baldwin 			return;
1943d320e05cSJohn Baldwin 		}
1944d320e05cSJohn Baldwin 		alloc++;
1945d320e05cSJohn Baldwin 	}
1946d320e05cSJohn Baldwin #endif
1947185c34f7SJayachandran C. }
1948d320e05cSJohn Baldwin 
1949*855e49f3SAlexander Motin static void
1950*855e49f3SAlexander Motin acpi_platform_osc(device_t dev)
1951*855e49f3SAlexander Motin {
1952*855e49f3SAlexander Motin 	ACPI_HANDLE sb_handle;
1953*855e49f3SAlexander Motin 	ACPI_STATUS status;
1954*855e49f3SAlexander Motin 	uint32_t cap_set[2];
1955*855e49f3SAlexander Motin 
1956*855e49f3SAlexander Motin 	/* 0811B06E-4A27-44F9-8D60-3CBBC22E7B48 */
1957*855e49f3SAlexander Motin 	static uint8_t acpi_platform_uuid[ACPI_UUID_LENGTH] = {
1958*855e49f3SAlexander Motin 		0x6e, 0xb0, 0x11, 0x08, 0x27, 0x4a, 0xf9, 0x44,
1959*855e49f3SAlexander Motin 		0x8d, 0x60, 0x3c, 0xbb, 0xc2, 0x2e, 0x7b, 0x48
1960*855e49f3SAlexander Motin 	};
1961*855e49f3SAlexander Motin 
1962*855e49f3SAlexander Motin 	if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
1963*855e49f3SAlexander Motin 		return;
1964*855e49f3SAlexander Motin 
1965*855e49f3SAlexander Motin 	cap_set[1] = 0x10;	/* APEI Support */
1966*855e49f3SAlexander Motin 	status = acpi_EvaluateOSC(sb_handle, acpi_platform_uuid, 1,
1967*855e49f3SAlexander Motin 	    nitems(cap_set), cap_set, cap_set, false);
1968*855e49f3SAlexander Motin 	if (ACPI_FAILURE(status)) {
1969*855e49f3SAlexander Motin 		if (status == AE_NOT_FOUND)
1970*855e49f3SAlexander Motin 			return;
1971*855e49f3SAlexander Motin 		device_printf(dev, "_OSC failed: %s\n",
1972*855e49f3SAlexander Motin 		    AcpiFormatException(status));
1973*855e49f3SAlexander Motin 		return;
1974*855e49f3SAlexander Motin 	}
1975*855e49f3SAlexander Motin }
1976*855e49f3SAlexander Motin 
1977bc0f2195SMike Smith /*
197833332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
197915e32d5dSMike Smith  *
198033332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
198133332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
198233332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
198333332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
198433332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
198515e32d5dSMike Smith  */
198615e32d5dSMike Smith static void
198715e32d5dSMike Smith acpi_probe_children(device_t bus)
198815e32d5dSMike Smith {
198915e32d5dSMike Smith 
1990b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
19910ae55423SMike Smith 
199215e32d5dSMike Smith     /*
199315e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1994edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
199515e32d5dSMike Smith      *
199615e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1997be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1998be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1999be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
200015e32d5dSMike Smith      */
20014c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
200233332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
20032272d050SJung-uk Kim 	NULL, bus, NULL);
200415e32d5dSMike Smith 
2005adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
2006adad4744SNate Lawson     acpi_sysres_alloc(bus);
2007adad4744SNate Lawson 
2008ea233199SJohn Baldwin     /* Reserve resources already allocated to children. */
2009ea233199SJohn Baldwin     acpi_reserve_resources(bus);
2010ea233199SJohn Baldwin 
2011edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
2012edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
2013edc13633SNate Lawson     bus_generic_probe(bus);
2014edc13633SNate Lawson 
20155e66b8e2SJohn Baldwin     /* Probe/attach all children, created statically and from the namespace. */
20160430ba9eSAndriy Gapon     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n"));
201715e32d5dSMike Smith     bus_generic_attach(bus);
20180ae55423SMike Smith 
201988a79fc0SNate Lawson     /* Attach wake sysctls. */
20205c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
202188a79fc0SNate Lawson 
2022bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
20230ae55423SMike Smith     return_VOID;
202415e32d5dSMike Smith }
202515e32d5dSMike Smith 
2026b82ca9a9SNate Lawson /*
2027d227204dSJohn Baldwin  * Determine the probe order for a given device.
2028b82ca9a9SNate Lawson  */
2029d227204dSJohn Baldwin static void
2030b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
203191233413SNate Lawson {
2032463e0f91SJohn Baldwin 	ACPI_OBJECT_TYPE type;
203391233413SNate Lawson 
2034b82ca9a9SNate Lawson 	/*
2035404b0d10SJung-uk Kim 	 * 0. CPUs
2036404b0d10SJung-uk Kim 	 * 1. I/O port and memory system resource holders
2037404b0d10SJung-uk Kim 	 * 2. Clocks and timers (to handle early accesses)
2038b1f9b996SAndriy Gapon 	 * 3. Embedded controllers (to handle early accesses)
2039b1f9b996SAndriy Gapon 	 * 4. PCI Link Devices
2040b82ca9a9SNate Lawson 	 */
2041463e0f91SJohn Baldwin 	AcpiGetType(handle, &type);
2042aa835160SAndriy Gapon 	if (type == ACPI_TYPE_PROCESSOR)
2043404b0d10SJung-uk Kim 		*order = 0;
2044404b0d10SJung-uk Kim 	else if (acpi_MatchHid(handle, "PNP0C01") ||
2045404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0C02"))
204691233413SNate Lawson 		*order = 1;
2047404b0d10SJung-uk Kim 	else if (acpi_MatchHid(handle, "PNP0100") ||
2048404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0103") ||
2049404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0B00"))
205091233413SNate Lawson 		*order = 2;
2051aa835160SAndriy Gapon 	else if (acpi_MatchHid(handle, "PNP0C09"))
2052b460c6f8SJohn Baldwin 		*order = 3;
2053aa835160SAndriy Gapon 	else if (acpi_MatchHid(handle, "PNP0C0F"))
2054aa835160SAndriy Gapon 		*order = 4;
205591233413SNate Lawson }
205691233413SNate Lawson 
205715e32d5dSMike Smith /*
205815e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
205915e32d5dSMike Smith  * it.
206015e32d5dSMike Smith  */
206115e32d5dSMike Smith static ACPI_STATUS
206215e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
206315e32d5dSMike Smith {
20640e68afe5SAndrew Turner     ACPI_DEVICE_INFO *devinfo;
20650e68afe5SAndrew Turner     struct acpi_device	*ad;
20665a77b11bSJung-uk Kim     struct acpi_prw_data prw;
206715e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
2068858a52f4SMitsuru IWASAKI     ACPI_HANDLE h;
206933332dc2SNate Lawson     device_t bus, child;
2070495a4144SJung-uk Kim     char *handle_str;
2071e4cd9dcfSJohn Baldwin     int order;
207215e32d5dSMike Smith 
2073b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
20740ae55423SMike Smith 
2075495a4144SJung-uk Kim     if (acpi_disabled("children"))
2076495a4144SJung-uk Kim 	return_ACPI_STATUS (AE_OK);
2077495a4144SJung-uk Kim 
2078be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
20790ae55423SMike Smith     if (acpi_avoid(handle))
20800ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
20810ae55423SMike Smith 
208291233413SNate Lawson     bus = (device_t)context;
2083b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
2084495a4144SJung-uk Kim 	handle_str = acpi_name(handle);
208515e32d5dSMike Smith 	switch (type) {
208615e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
2087495a4144SJung-uk Kim 	    /*
2088495a4144SJung-uk Kim 	     * Since we scan from \, be sure to skip system scope objects.
2089495a4144SJung-uk Kim 	     * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
2090affa1826SJung-uk Kim 	     * BIOS bugs.  For example, \_SB_ is to allow \_SB_._INI to be run
2091453130d9SPedro F. Giffuni 	     * during the initialization and \_TZ_ is to support Notify() on it.
2092495a4144SJung-uk Kim 	     */
2093495a4144SJung-uk Kim 	    if (strcmp(handle_str, "\\_SB_") == 0 ||
2094495a4144SJung-uk Kim 		strcmp(handle_str, "\\_TZ_") == 0)
2095495a4144SJung-uk Kim 		break;
20965a77b11bSJung-uk Kim 	    if (acpi_parse_prw(handle, &prw) == 0)
20975a77b11bSJung-uk Kim 		AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
2098183c8af3SJohn Baldwin 
2099183c8af3SJohn Baldwin 	    /*
2100183c8af3SJohn Baldwin 	     * Ignore devices that do not have a _HID or _CID.  They should
2101183c8af3SJohn Baldwin 	     * be discovered by other buses (e.g. the PCI bus driver).
2102183c8af3SJohn Baldwin 	     */
2103183c8af3SJohn Baldwin 	    if (!acpi_has_hid(handle))
2104183c8af3SJohn Baldwin 		break;
2105495a4144SJung-uk Kim 	    /* FALLTHROUGH */
210615e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
210715e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
210815e32d5dSMike Smith 	case ACPI_TYPE_POWER:
210933332dc2SNate Lawson 	    /*
2110463e0f91SJohn Baldwin 	     * Create a placeholder device for this node.  Sort the
2111463e0f91SJohn Baldwin 	     * placeholder so that the probe/attach passes will run
2112463e0f91SJohn Baldwin 	     * breadth-first.  Orders less than ACPI_DEV_BASE_ORDER
2113463e0f91SJohn Baldwin 	     * are reserved for special objects (i.e., system
2114b1f9b996SAndriy Gapon 	     * resources).
211515e32d5dSMike Smith 	     */
211633332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
2117404b0d10SJung-uk Kim 	    order = level * 10 + ACPI_DEV_BASE_ORDER;
2118da72d149SNate Lawson 	    acpi_probe_order(handle, &order);
2119e4cd9dcfSJohn Baldwin 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
2120bc0f2195SMike Smith 	    if (child == NULL)
2121bc0f2195SMike Smith 		break;
212259a890e6SNate Lawson 
212359a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
212415e32d5dSMike Smith 	    acpi_set_handle(child, handle);
212559a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
2126bc0f2195SMike Smith 
2127bc0f2195SMike Smith 	    /*
2128b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
2129b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
2130b519f9d6SMike Smith 	     * the handle, but we don't probe it).
2131893f750aSNate Lawson 	     *
2132893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
2133893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
2134893f750aSNate Lawson 	     * anyway since we may enable them later.
2135b519f9d6SMike Smith 	     */
2136858a52f4SMitsuru IWASAKI 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
2137858a52f4SMitsuru IWASAKI 		/* Never disable PCI link devices. */
2138858a52f4SMitsuru IWASAKI 		if (acpi_MatchHid(handle, "PNP0C0F"))
2139858a52f4SMitsuru IWASAKI 		    break;
2140858a52f4SMitsuru IWASAKI 		/*
2141858a52f4SMitsuru IWASAKI 		 * Docking stations should remain enabled since the system
2142858a52f4SMitsuru IWASAKI 		 * may be undocked at boot.
2143858a52f4SMitsuru IWASAKI 		 */
2144858a52f4SMitsuru IWASAKI 		if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
2145858a52f4SMitsuru IWASAKI 		    break;
2146858a52f4SMitsuru IWASAKI 
2147b519f9d6SMike Smith 		device_disable(child);
2148b519f9d6SMike Smith 		break;
2149b519f9d6SMike Smith 	    }
2150b519f9d6SMike Smith 
2151b519f9d6SMike Smith 	    /*
2152bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
2153bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
2154bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
2155bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
2156bc0f2195SMike Smith 	     * device not to have any resources.
2157bc0f2195SMike Smith 	     */
215872ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
21590e68afe5SAndrew Turner 
21600e68afe5SAndrew Turner 	    ad = device_get_ivars(child);
21610e68afe5SAndrew Turner 	    ad->ad_cls_class = 0xffffff;
21620e68afe5SAndrew Turner 	    if (ACPI_SUCCESS(AcpiGetObjectInfo(handle, &devinfo))) {
21630e68afe5SAndrew Turner 		if ((devinfo->Valid & ACPI_VALID_CLS) != 0 &&
21640e68afe5SAndrew Turner 		    devinfo->ClassCode.Length >= ACPI_PCICLS_STRING_SIZE) {
21650e68afe5SAndrew Turner 		    ad->ad_cls_class = strtoul(devinfo->ClassCode.String,
21660e68afe5SAndrew Turner 			NULL, 16);
21670e68afe5SAndrew Turner 		}
21680e68afe5SAndrew Turner 		AcpiOsFree(devinfo);
21690e68afe5SAndrew Turner 	    }
21700ae55423SMike Smith 	    break;
217115e32d5dSMike Smith 	}
217215e32d5dSMike Smith     }
2173be2b1797SNate Lawson 
21740ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
217515e32d5dSMike Smith }
217615e32d5dSMike Smith 
217759a890e6SNate Lawson /*
217859a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
217959a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
218059a890e6SNate Lawson  */
218159a890e6SNate Lawson void
218292488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data)
218359a890e6SNate Lawson {
218459a890e6SNate Lawson }
218559a890e6SNate Lawson 
218615e32d5dSMike Smith static void
218715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
218815e32d5dSMike Smith {
21892d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
219071804adcSJung-uk Kim     register_t intr;
2191d33f4987STakanori Watanabe     ACPI_STATUS status;
2192eeb3a05fSNate Lawson 
2193eeb3a05fSNate Lawson     /*
219480f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
219580f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
219680f0e4c2SNate Lawson      * an AP.
2197eeb3a05fSNate Lawson      */
2198eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
2199904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
2200904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
22012d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2202904bf0c2SNate Lawson 		AcpiFormatException(status));
2203904bf0c2SNate Lawson 	    return;
2204904bf0c2SNate Lawson 	}
22052d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Powering system off\n");
220671804adcSJung-uk Kim 	intr = intr_disable();
22071df130f1SJung-uk Kim 	status = AcpiEnterSleepState(ACPI_STATE_S5);
220871804adcSJung-uk Kim 	if (ACPI_FAILURE(status)) {
220971804adcSJung-uk Kim 	    intr_restore(intr);
22102d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - %s\n",
22112d0c82e8SJung-uk Kim 		AcpiFormatException(status));
221271804adcSJung-uk Kim 	} else {
221315e32d5dSMike Smith 	    DELAY(1000000);
221471804adcSJung-uk Kim 	    intr_restore(intr);
22152d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - timeout\n");
221615e32d5dSMike Smith 	}
2217ac731af5SJung-uk Kim     } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
2218d85e6785SNate Lawson 	/* Reboot using the reset register. */
2219ac731af5SJung-uk Kim 	status = AcpiReset();
2220ac731af5SJung-uk Kim 	if (ACPI_SUCCESS(status)) {
222187a500cdSNate Lawson 	    DELAY(1000000);
22222d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - timeout\n");
2223ac731af5SJung-uk Kim 	} else if (status != AE_NOT_EXIST)
2224ac731af5SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - %s\n",
2225ac731af5SJung-uk Kim 		AcpiFormatException(status));
2226879e0604SMateusz Guzik     } else if (sc->acpi_do_disable && !KERNEL_PANICKED()) {
2227d85e6785SNate Lawson 	/*
2228d85e6785SNate Lawson 	 * Only disable ACPI if the user requested.  On some systems, writing
2229d85e6785SNate Lawson 	 * the disable value to SMI_CMD hangs the system.
2230d85e6785SNate Lawson 	 */
22312d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Shutting down\n");
223280f0e4c2SNate Lawson 	AcpiTerminate();
223380f0e4c2SNate Lawson     }
223415e32d5dSMike Smith }
223515e32d5dSMike Smith 
223613d4f7baSMitsuru IWASAKI static void
223713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
223813d4f7baSMitsuru IWASAKI {
223913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
224013d4f7baSMitsuru IWASAKI 
224113d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
22422be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
224359ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
224413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
224533febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
2246be2b1797SNate Lawson 	if (first_time)
22476c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
224813d4f7baSMitsuru IWASAKI     }
22492be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
225059ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
225113d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
225233febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
2253be2b1797SNate Lawson 	if (first_time)
22546c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
225513d4f7baSMitsuru IWASAKI     }
225613d4f7baSMitsuru IWASAKI 
225713d4f7baSMitsuru IWASAKI     first_time = 0;
225813d4f7baSMitsuru IWASAKI }
225913d4f7baSMitsuru IWASAKI 
226015e32d5dSMike Smith /*
2261c5ba0be4SMike Smith  * Returns true if the device is actually present and should
2262c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
2263c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
2264c5ba0be4SMike Smith  */
2265c5ba0be4SMike Smith BOOLEAN
2266c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
2267c5ba0be4SMike Smith {
2268c5ba0be4SMike Smith 	ACPI_HANDLE h;
22698438a7a8SJung-uk Kim 	UINT32 s;
22708438a7a8SJung-uk Kim 	ACPI_STATUS status;
2271c5ba0be4SMike Smith 
22728438a7a8SJung-uk Kim 	h = acpi_get_handle(dev);
22738438a7a8SJung-uk Kim 	if (h == NULL)
2274c5ba0be4SMike Smith 		return (FALSE);
2275381388b9SMatt Macy 	/*
2276bf15eaf2SWarner Losh 	 * Certain Treadripper boards always returns 0 for FreeBSD because it
2277bf15eaf2SWarner Losh 	 * only returns non-zero for the OS string "Windows 2015". Otherwise it
2278bf15eaf2SWarner Losh 	 * will return zero. Force them to always be treated as present.
2279bf15eaf2SWarner Losh 	 * Beata versions were worse: they always returned 0.
2280381388b9SMatt Macy 	 */
2281381388b9SMatt Macy 	if (acpi_MatchHid(h, "AMDI0020") || acpi_MatchHid(h, "AMDI0010"))
2282381388b9SMatt Macy 		return (TRUE);
2283381388b9SMatt Macy 
2284817b71bbSAndriy Gapon 	status = acpi_GetInteger(h, "_STA", &s);
2285817b71bbSAndriy Gapon 
2286817b71bbSAndriy Gapon 	/*
2287817b71bbSAndriy Gapon 	 * If no _STA method or if it failed, then assume that
2288817b71bbSAndriy Gapon 	 * the device is present.
2289817b71bbSAndriy Gapon 	 */
22908438a7a8SJung-uk Kim 	if (ACPI_FAILURE(status))
2291817b71bbSAndriy Gapon 		return (TRUE);
2292be2b1797SNate Lawson 
22938438a7a8SJung-uk Kim 	return (ACPI_DEVICE_PRESENT(s) ? TRUE : FALSE);
2294c5ba0be4SMike Smith }
2295c5ba0be4SMike Smith 
2296c5ba0be4SMike Smith /*
2297b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
2298b53f2771SMike Smith  */
2299b53f2771SMike Smith BOOLEAN
2300b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
2301b53f2771SMike Smith {
2302b53f2771SMike Smith 	ACPI_HANDLE h;
23038438a7a8SJung-uk Kim 	UINT32 s;
23048438a7a8SJung-uk Kim 	ACPI_STATUS status;
2305b53f2771SMike Smith 
23068438a7a8SJung-uk Kim 	h = acpi_get_handle(dev);
23078438a7a8SJung-uk Kim 	if (h == NULL)
2308b53f2771SMike Smith 		return (FALSE);
23098438a7a8SJung-uk Kim 	status = acpi_GetInteger(h, "_STA", &s);
2310be2b1797SNate Lawson 
2311817b71bbSAndriy Gapon 	/*
2312817b71bbSAndriy Gapon 	 * If no _STA method or if it failed, then assume that
2313817b71bbSAndriy Gapon 	 * the device is present.
2314817b71bbSAndriy Gapon 	 */
23158438a7a8SJung-uk Kim 	if (ACPI_FAILURE(status))
2316817b71bbSAndriy Gapon 		return (TRUE);
2317be2b1797SNate Lawson 
23188438a7a8SJung-uk Kim 	return (ACPI_BATTERY_PRESENT(s) ? TRUE : FALSE);
2319b53f2771SMike Smith }
2320b53f2771SMike Smith 
2321b53f2771SMike Smith /*
2322183c8af3SJohn Baldwin  * Returns true if a device has at least one valid device ID.
2323183c8af3SJohn Baldwin  */
2324ddf8c230SVladimir Kondratyev BOOLEAN
2325183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h)
2326183c8af3SJohn Baldwin {
2327183c8af3SJohn Baldwin     ACPI_DEVICE_INFO	*devinfo;
2328183c8af3SJohn Baldwin     BOOLEAN		ret;
2329183c8af3SJohn Baldwin 
2330183c8af3SJohn Baldwin     if (h == NULL ||
2331183c8af3SJohn Baldwin 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2332183c8af3SJohn Baldwin 	return (FALSE);
2333183c8af3SJohn Baldwin 
2334183c8af3SJohn Baldwin     ret = FALSE;
2335183c8af3SJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2336183c8af3SJohn Baldwin 	ret = TRUE;
2337183c8af3SJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2338183c8af3SJohn Baldwin 	if (devinfo->CompatibleIdList.Count > 0)
2339183c8af3SJohn Baldwin 	    ret = TRUE;
2340183c8af3SJohn Baldwin 
2341183c8af3SJohn Baldwin     AcpiOsFree(devinfo);
2342183c8af3SJohn Baldwin     return (ret);
2343183c8af3SJohn Baldwin }
2344183c8af3SJohn Baldwin 
2345183c8af3SJohn Baldwin /*
234691233413SNate Lawson  * Match a HID string against a handle
23475efca36fSTakanori Watanabe  * returns ACPI_MATCHHID_HID if _HID match
23485efca36fSTakanori Watanabe  *         ACPI_MATCHHID_CID if _CID match and not _HID match.
23495efca36fSTakanori Watanabe  *         ACPI_MATCHHID_NOMATCH=0 if no match.
235015e32d5dSMike Smith  */
23515efca36fSTakanori Watanabe int
2352ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
235315e32d5dSMike Smith {
23541e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
235592488a57SJung-uk Kim     BOOLEAN		ret;
235692488a57SJung-uk Kim     int			i;
235792488a57SJung-uk Kim 
235892488a57SJung-uk Kim     if (hid == NULL || h == NULL ||
235992488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
23605efca36fSTakanori Watanabe 	return (ACPI_MATCHHID_NOMATCH);
236115e32d5dSMike Smith 
23626d29ba58SAndriy Gapon     ret = ACPI_MATCHHID_NOMATCH;
2363c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
236492488a57SJung-uk Kim 	strcmp(hid, devinfo->HardwareId.String) == 0)
23655efca36fSTakanori Watanabe 	    ret = ACPI_MATCHHID_HID;
236692488a57SJung-uk Kim     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
236792488a57SJung-uk Kim 	for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
236892488a57SJung-uk Kim 	    if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
23695efca36fSTakanori Watanabe 		ret = ACPI_MATCHHID_CID;
23701e4925e8SNate Lawson 		break;
23711e4925e8SNate Lawson 	    }
23721e4925e8SNate Lawson 	}
2373be2b1797SNate Lawson 
237492488a57SJung-uk Kim     AcpiOsFree(devinfo);
23751e4925e8SNate Lawson     return (ret);
237615e32d5dSMike Smith }
237715e32d5dSMike Smith 
237815e32d5dSMike Smith /*
2379c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
2380c5ba0be4SMike Smith  * or one if its parents.
2381c5ba0be4SMike Smith  */
2382c5ba0be4SMike Smith ACPI_STATUS
2383c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2384c5ba0be4SMike Smith {
2385c5ba0be4SMike Smith     ACPI_HANDLE		r;
2386c5ba0be4SMike Smith     ACPI_STATUS		status;
2387c5ba0be4SMike Smith 
2388be2b1797SNate Lawson     /* Walk back up the tree to the root */
2389c5ba0be4SMike Smith     for (;;) {
2390be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
2391be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
2392c5ba0be4SMike Smith 	    *result = r;
2393c5ba0be4SMike Smith 	    return (AE_OK);
2394c5ba0be4SMike Smith 	}
2395bee4aa4aSNate Lawson 	/* XXX Return error here? */
2396c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
2397c5ba0be4SMike Smith 	    return (AE_OK);
2398b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2399c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
2400c5ba0be4SMike Smith 	parent = r;
2401c5ba0be4SMike Smith     }
2402c5ba0be4SMike Smith }
2403c5ba0be4SMike Smith 
2404c5ba0be4SMike Smith /*
2405c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
2406c5ba0be4SMike Smith  */
2407c5ba0be4SMike Smith ACPI_BUFFER *
2408c5ba0be4SMike Smith acpi_AllocBuffer(int size)
2409c5ba0be4SMike Smith {
2410c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
2411c5ba0be4SMike Smith 
2412c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2413c5ba0be4SMike Smith 	return (NULL);
2414c5ba0be4SMike Smith     buf->Length = size;
2415c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
2416c5ba0be4SMike Smith     return (buf);
2417c5ba0be4SMike Smith }
2418c5ba0be4SMike Smith 
2419c310653eSNate Lawson ACPI_STATUS
2420cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2421c310653eSNate Lawson {
2422c310653eSNate Lawson     ACPI_OBJECT arg1;
2423c310653eSNate Lawson     ACPI_OBJECT_LIST args;
2424c310653eSNate Lawson 
2425c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
2426c310653eSNate Lawson     arg1.Integer.Value = number;
2427c310653eSNate Lawson     args.Count = 1;
2428c310653eSNate Lawson     args.Pointer = &arg1;
2429c310653eSNate Lawson 
2430c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
2431c310653eSNate Lawson }
2432c310653eSNate Lawson 
2433c5ba0be4SMike Smith /*
2434c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
2435c5ba0be4SMike Smith  */
2436c5ba0be4SMike Smith ACPI_STATUS
2437cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2438c5ba0be4SMike Smith {
2439be2b1797SNate Lawson     ACPI_STATUS	status;
2440c5ba0be4SMike Smith     ACPI_BUFFER	buf;
2441c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
2442c5ba0be4SMike Smith 
2443c5ba0be4SMike Smith     if (handle == NULL)
2444c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
24450c7da7acSJohn Baldwin 
24460c7da7acSJohn Baldwin     /*
24470c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
24480c7da7acSJohn Baldwin      * a method that will return an Integer.
24490c7da7acSJohn Baldwin      */
2450c5ba0be4SMike Smith     buf.Pointer = &param;
2451c5ba0be4SMike Smith     buf.Length = sizeof(param);
2452be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
2453be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
2454be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
2455c5ba0be4SMike Smith 	    *number = param.Integer.Value;
2456be2b1797SNate Lawson 	else
2457be2b1797SNate Lawson 	    status = AE_TYPE;
2458c5ba0be4SMike Smith     }
24590c7da7acSJohn Baldwin 
24600c7da7acSJohn Baldwin     /*
24610c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
24620c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
24630c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
24640c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
24650c7da7acSJohn Baldwin      *
24660c7da7acSJohn Baldwin      * This is a hack.
24670c7da7acSJohn Baldwin      */
2468be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
2469b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2470be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
24710c7da7acSJohn Baldwin 	} else {
2472be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
2473be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
2474be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
24750c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
24760c7da7acSJohn Baldwin 	}
2477f97739daSNate Lawson     }
2478be2b1797SNate Lawson     return (status);
2479c5ba0be4SMike Smith }
2480c5ba0be4SMike Smith 
2481c573e654SMitsuru IWASAKI ACPI_STATUS
2482cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2483c573e654SMitsuru IWASAKI {
2484c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
2485dba55fa2SNate Lawson     UINT8	*val;
2486c573e654SMitsuru IWASAKI     int		i;
2487c573e654SMitsuru IWASAKI 
2488c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
2489c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
2490c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
2491c573e654SMitsuru IWASAKI 	return (AE_OK);
2492c573e654SMitsuru IWASAKI     }
2493c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
2494c573e654SMitsuru IWASAKI 	return (AE_TYPE);
2495c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
2496c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
2497be2b1797SNate Lawson 
2498c573e654SMitsuru IWASAKI     *number = 0;
2499dba55fa2SNate Lawson     val = p->Buffer.Pointer;
2500c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
2501dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
2502c573e654SMitsuru IWASAKI     return (AE_OK);
2503c573e654SMitsuru IWASAKI }
2504c573e654SMitsuru IWASAKI 
2505c5ba0be4SMike Smith /*
2506c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
2507c5ba0be4SMike Smith  * function for each element.
2508c5ba0be4SMike Smith  *
2509c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
2510c5ba0be4SMike Smith  */
2511c5ba0be4SMike Smith ACPI_STATUS
2512be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2513be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2514c5ba0be4SMike Smith {
2515c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
2516c5ba0be4SMike Smith     int		i;
2517c5ba0be4SMike Smith 
2518be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2519c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
2520c5ba0be4SMike Smith 
2521be2b1797SNate Lawson     /* Iterate over components */
2522be2b1797SNate Lawson     i = 0;
2523be2b1797SNate Lawson     comp = pkg->Package.Elements;
2524be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
2525c5ba0be4SMike Smith 	func(comp, arg);
2526c5ba0be4SMike Smith 
2527c5ba0be4SMike Smith     return (AE_OK);
2528c5ba0be4SMike Smith }
2529c5ba0be4SMike Smith 
25306f69255bSMike Smith /*
25316f69255bSMike Smith  * Find the (index)th resource object in a set.
25326f69255bSMike Smith  */
25336f69255bSMike Smith ACPI_STATUS
25346d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
25356f69255bSMike Smith {
25366d63101aSMike Smith     ACPI_RESOURCE	*rp;
25376f69255bSMike Smith     int			i;
25386f69255bSMike Smith 
25396d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
25406f69255bSMike Smith     i = index;
25416d63101aSMike Smith     while (i-- > 0) {
2542be2b1797SNate Lawson 	/* Range check */
25436d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
25446f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
2545be2b1797SNate Lawson 
2546be2b1797SNate Lawson 	/* Check for terminator */
2547e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
25486d63101aSMike Smith 	    return (AE_NOT_FOUND);
2549abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
25506f69255bSMike Smith     }
25516f69255bSMike Smith     if (resp != NULL)
25526d63101aSMike Smith 	*resp = rp;
2553be2b1797SNate Lawson 
25546d63101aSMike Smith     return (AE_OK);
25556d63101aSMike Smith }
25566d63101aSMike Smith 
25576d63101aSMike Smith /*
25586d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
25596d63101aSMike Smith  *
25606d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
25616d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
25626d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
25636d63101aSMike Smith  * resources.
25646d63101aSMike Smith  */
25656d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
25666d63101aSMike Smith 
25676d63101aSMike Smith ACPI_STATUS
25686d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
25696d63101aSMike Smith {
25706d63101aSMike Smith     ACPI_RESOURCE	*rp;
25716d63101aSMike Smith     void		*newp;
25726d63101aSMike Smith 
2573be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
25746d63101aSMike Smith     if (buf->Pointer == NULL) {
25756d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
25766d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
25776d63101aSMike Smith 	    return (AE_NO_MEMORY);
25786d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2579e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
258060d306f0SJohn Baldwin 	rp->Length = ACPI_RS_SIZE_MIN;
25816d63101aSMike Smith     }
25826d63101aSMike Smith     if (res == NULL)
25836d63101aSMike Smith 	return (AE_OK);
25846d63101aSMike Smith 
25856d63101aSMike Smith     /*
25866d63101aSMike Smith      * Scan the current buffer looking for the terminator.
25876d63101aSMike Smith      * This will either find the terminator or hit the end
25886d63101aSMike Smith      * of the buffer and return an error.
25896d63101aSMike Smith      */
25906d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
25916d63101aSMike Smith     for (;;) {
2592be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
25936d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
25946d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2595e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
25966d63101aSMike Smith 	    break;
2597abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
25986d63101aSMike Smith     }
25996d63101aSMike Smith 
26006d63101aSMike Smith     /*
26016d63101aSMike Smith      * Check the size of the buffer and expand if required.
26026d63101aSMike Smith      *
26036d63101aSMike Smith      * Required size is:
26046d63101aSMike Smith      *	size of existing resources before terminator +
26056d63101aSMike Smith      *	size of new resource and header +
26066d63101aSMike Smith      * 	size of terminator.
26076d63101aSMike Smith      *
26086d63101aSMike Smith      * Note that this loop should really only run once, unless
26096d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
26106d63101aSMike Smith      */
26116d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2612e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2613e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
26146d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
26156d63101aSMike Smith 	    return (AE_NO_MEMORY);
26166d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2617a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2618a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
26196d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
26206d63101aSMike Smith 	buf->Pointer = newp;
26216d63101aSMike Smith 	buf->Length += buf->Length;
26226d63101aSMike Smith     }
26236d63101aSMike Smith 
2624be2b1797SNate Lawson     /* Insert the new resource. */
2625e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
26266d63101aSMike Smith 
2627be2b1797SNate Lawson     /* And add the terminator. */
2628abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2629e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
263060d306f0SJohn Baldwin     rp->Length = ACPI_RS_SIZE_MIN;
26316d63101aSMike Smith 
26326f69255bSMike Smith     return (AE_OK);
26336f69255bSMike Smith }
2634c5ba0be4SMike Smith 
26358fd10880SBen Widawsky UINT8
26368fd10880SBen Widawsky acpi_DSMQuery(ACPI_HANDLE h, uint8_t *uuid, int revision)
26378fd10880SBen Widawsky {
26388fd10880SBen Widawsky     /*
26398fd10880SBen Widawsky      * ACPI spec 9.1.1 defines this.
26408fd10880SBen Widawsky      *
26418fd10880SBen Widawsky      * "Arg2: Function Index Represents a specific function whose meaning is
26428fd10880SBen Widawsky      * specific to the UUID and Revision ID. Function indices should start
26438fd10880SBen Widawsky      * with 1. Function number zero is a query function (see the special
26448fd10880SBen Widawsky      * return code defined below)."
26458fd10880SBen Widawsky      */
26468fd10880SBen Widawsky     ACPI_BUFFER buf;
26478fd10880SBen Widawsky     ACPI_OBJECT *obj;
26488fd10880SBen Widawsky     UINT8 ret = 0;
26498fd10880SBen Widawsky 
26508fd10880SBen Widawsky     if (!ACPI_SUCCESS(acpi_EvaluateDSM(h, uuid, revision, 0, NULL, &buf))) {
26518fd10880SBen Widawsky 	ACPI_INFO(("Failed to enumerate DSM functions\n"));
26528fd10880SBen Widawsky 	return (0);
26538fd10880SBen Widawsky     }
26548fd10880SBen Widawsky 
26558fd10880SBen Widawsky     obj = (ACPI_OBJECT *)buf.Pointer;
26568fd10880SBen Widawsky     KASSERT(obj, ("Object not allowed to be NULL\n"));
26578fd10880SBen Widawsky 
26588fd10880SBen Widawsky     /*
26598fd10880SBen Widawsky      * From ACPI 6.2 spec 9.1.1:
26608fd10880SBen Widawsky      * If Function Index = 0, a Buffer containing a function index bitfield.
26618fd10880SBen Widawsky      * Otherwise, the return value and type depends on the UUID and revision
26628fd10880SBen Widawsky      * ID (see below).
26638fd10880SBen Widawsky      */
26648fd10880SBen Widawsky     switch (obj->Type) {
26658fd10880SBen Widawsky     case ACPI_TYPE_BUFFER:
26668fd10880SBen Widawsky 	ret = *(uint8_t *)obj->Buffer.Pointer;
26678fd10880SBen Widawsky 	break;
26688fd10880SBen Widawsky     case ACPI_TYPE_INTEGER:
26698fd10880SBen Widawsky 	ACPI_BIOS_WARNING((AE_INFO,
26708fd10880SBen Widawsky 	    "Possibly buggy BIOS with ACPI_TYPE_INTEGER for function enumeration\n"));
26718fd10880SBen Widawsky 	ret = obj->Integer.Value & 0xFF;
26728fd10880SBen Widawsky 	break;
26738fd10880SBen Widawsky     default:
26748fd10880SBen Widawsky 	ACPI_WARNING((AE_INFO, "Unexpected return type %u\n", obj->Type));
26758fd10880SBen Widawsky     };
26768fd10880SBen Widawsky 
26778fd10880SBen Widawsky     AcpiOsFree(obj);
26788fd10880SBen Widawsky     return ret;
26798fd10880SBen Widawsky }
26808fd10880SBen Widawsky 
26818fd10880SBen Widawsky /*
26828fd10880SBen Widawsky  * DSM may return multiple types depending on the function. It is therefore
26838fd10880SBen Widawsky  * unsafe to use the typed evaluation. It is highly recommended that the caller
26848fd10880SBen Widawsky  * check the type of the returned object.
26858fd10880SBen Widawsky  */
26868fd10880SBen Widawsky ACPI_STATUS
26878fd10880SBen Widawsky acpi_EvaluateDSM(ACPI_HANDLE handle, uint8_t *uuid, int revision,
26888fd10880SBen Widawsky     uint64_t function, union acpi_object *package, ACPI_BUFFER *out_buf)
26898fd10880SBen Widawsky {
26908fd10880SBen Widawsky     ACPI_OBJECT arg[4];
26918fd10880SBen Widawsky     ACPI_OBJECT_LIST arglist;
26928fd10880SBen Widawsky     ACPI_BUFFER buf;
26938fd10880SBen Widawsky     ACPI_STATUS status;
26948fd10880SBen Widawsky 
26958fd10880SBen Widawsky     if (out_buf == NULL)
26968fd10880SBen Widawsky 	return (AE_NO_MEMORY);
26978fd10880SBen Widawsky 
26988fd10880SBen Widawsky     arg[0].Type = ACPI_TYPE_BUFFER;
26998fd10880SBen Widawsky     arg[0].Buffer.Length = ACPI_UUID_LENGTH;
27008fd10880SBen Widawsky     arg[0].Buffer.Pointer = uuid;
27018fd10880SBen Widawsky     arg[1].Type = ACPI_TYPE_INTEGER;
27028fd10880SBen Widawsky     arg[1].Integer.Value = revision;
27038fd10880SBen Widawsky     arg[2].Type = ACPI_TYPE_INTEGER;
27048fd10880SBen Widawsky     arg[2].Integer.Value = function;
27058fd10880SBen Widawsky     if (package) {
27068fd10880SBen Widawsky 	arg[3] = *package;
27078fd10880SBen Widawsky     } else {
27088fd10880SBen Widawsky 	arg[3].Type = ACPI_TYPE_PACKAGE;
27098fd10880SBen Widawsky 	arg[3].Package.Count = 0;
27108fd10880SBen Widawsky 	arg[3].Package.Elements = NULL;
27118fd10880SBen Widawsky     }
27128fd10880SBen Widawsky 
27138fd10880SBen Widawsky     arglist.Pointer = arg;
27148fd10880SBen Widawsky     arglist.Count = 4;
27158fd10880SBen Widawsky     buf.Pointer = NULL;
27168fd10880SBen Widawsky     buf.Length = ACPI_ALLOCATE_BUFFER;
27178fd10880SBen Widawsky     status = AcpiEvaluateObject(handle, "_DSM", &arglist, &buf);
27188fd10880SBen Widawsky     if (ACPI_FAILURE(status))
27198fd10880SBen Widawsky 	return (status);
27208fd10880SBen Widawsky 
27218fd10880SBen Widawsky     KASSERT(ACPI_SUCCESS(status), ("Unexpected status"));
27228fd10880SBen Widawsky 
27238fd10880SBen Widawsky     *out_buf = buf;
27248fd10880SBen Widawsky     return (status);
27258fd10880SBen Widawsky }
27268fd10880SBen Widawsky 
27275f3dd91aSJohn Baldwin ACPI_STATUS
27285f3dd91aSJohn Baldwin acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count,
27294c26ac69SJohn Baldwin     uint32_t *caps_in, uint32_t *caps_out, bool query)
27305f3dd91aSJohn Baldwin {
27314c26ac69SJohn Baldwin 	ACPI_OBJECT arg[4], *ret;
27325f3dd91aSJohn Baldwin 	ACPI_OBJECT_LIST arglist;
27334c26ac69SJohn Baldwin 	ACPI_BUFFER buf;
27344c26ac69SJohn Baldwin 	ACPI_STATUS status;
27355f3dd91aSJohn Baldwin 
27365f3dd91aSJohn Baldwin 	arglist.Pointer = arg;
27375f3dd91aSJohn Baldwin 	arglist.Count = 4;
27385f3dd91aSJohn Baldwin 	arg[0].Type = ACPI_TYPE_BUFFER;
27395f3dd91aSJohn Baldwin 	arg[0].Buffer.Length = ACPI_UUID_LENGTH;
27405f3dd91aSJohn Baldwin 	arg[0].Buffer.Pointer = uuid;
27415f3dd91aSJohn Baldwin 	arg[1].Type = ACPI_TYPE_INTEGER;
27425f3dd91aSJohn Baldwin 	arg[1].Integer.Value = revision;
27435f3dd91aSJohn Baldwin 	arg[2].Type = ACPI_TYPE_INTEGER;
27445f3dd91aSJohn Baldwin 	arg[2].Integer.Value = count;
27455f3dd91aSJohn Baldwin 	arg[3].Type = ACPI_TYPE_BUFFER;
27464c26ac69SJohn Baldwin 	arg[3].Buffer.Length = count * sizeof(*caps_in);
27474c26ac69SJohn Baldwin 	arg[3].Buffer.Pointer = (uint8_t *)caps_in;
27484c26ac69SJohn Baldwin 	caps_in[0] = query ? 1 : 0;
27494c26ac69SJohn Baldwin 	buf.Pointer = NULL;
27504c26ac69SJohn Baldwin 	buf.Length = ACPI_ALLOCATE_BUFFER;
27514c26ac69SJohn Baldwin 	status = AcpiEvaluateObjectTyped(handle, "_OSC", &arglist, &buf,
27524c26ac69SJohn Baldwin 	    ACPI_TYPE_BUFFER);
27534c26ac69SJohn Baldwin 	if (ACPI_FAILURE(status))
27544c26ac69SJohn Baldwin 		return (status);
27554c26ac69SJohn Baldwin 	if (caps_out != NULL) {
27564c26ac69SJohn Baldwin 		ret = buf.Pointer;
27574c26ac69SJohn Baldwin 		if (ret->Buffer.Length != count * sizeof(*caps_out)) {
27584c26ac69SJohn Baldwin 			AcpiOsFree(buf.Pointer);
27594c26ac69SJohn Baldwin 			return (AE_BUFFER_OVERFLOW);
27604c26ac69SJohn Baldwin 		}
27614c26ac69SJohn Baldwin 		bcopy(ret->Buffer.Pointer, caps_out, ret->Buffer.Length);
27624c26ac69SJohn Baldwin 	}
27634c26ac69SJohn Baldwin 	AcpiOsFree(buf.Pointer);
27644c26ac69SJohn Baldwin 	return (status);
27655f3dd91aSJohn Baldwin }
27665f3dd91aSJohn Baldwin 
2767da14ac9fSJohn Baldwin /*
2768da14ac9fSJohn Baldwin  * Set interrupt model.
2769da14ac9fSJohn Baldwin  */
2770da14ac9fSJohn Baldwin ACPI_STATUS
2771da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2772da14ac9fSJohn Baldwin {
2773da14ac9fSJohn Baldwin 
2774c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2775da14ac9fSJohn Baldwin }
2776da14ac9fSJohn Baldwin 
277700a30448SNate Lawson /*
2778d95e7f5aSJohn Baldwin  * Walk subtables of a table and call a callback routine for each
2779d95e7f5aSJohn Baldwin  * subtable.  The caller should provide the first subtable and a
2780d95e7f5aSJohn Baldwin  * pointer to the end of the table.  This can be used to walk tables
2781d95e7f5aSJohn Baldwin  * such as MADT and SRAT that use subtable entries.
2782d95e7f5aSJohn Baldwin  */
2783d95e7f5aSJohn Baldwin void
2784d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2785d95e7f5aSJohn Baldwin     void *arg)
2786d95e7f5aSJohn Baldwin {
2787d95e7f5aSJohn Baldwin     ACPI_SUBTABLE_HEADER *entry;
2788d95e7f5aSJohn Baldwin 
2789d95e7f5aSJohn Baldwin     for (entry = first; (void *)entry < end; ) {
2790d95e7f5aSJohn Baldwin 	/* Avoid an infinite loop if we hit a bogus entry. */
2791d95e7f5aSJohn Baldwin 	if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2792d95e7f5aSJohn Baldwin 	    return;
2793d95e7f5aSJohn Baldwin 
2794d95e7f5aSJohn Baldwin 	handler(entry, arg);
2795d95e7f5aSJohn Baldwin 	entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2796d95e7f5aSJohn Baldwin     }
2797d95e7f5aSJohn Baldwin }
2798d95e7f5aSJohn Baldwin 
2799d95e7f5aSJohn Baldwin /*
280000a30448SNate Lawson  * DEPRECATED.  This interface has serious deficiencies and will be
280100a30448SNate Lawson  * removed.
280200a30448SNate Lawson  *
280300a30448SNate Lawson  * Immediately enter the sleep state.  In the old model, acpiconf(8) ran
280400a30448SNate Lawson  * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
280500a30448SNate Lawson  */
280600a30448SNate Lawson ACPI_STATUS
280700a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state)
280800a30448SNate Lawson {
280900a30448SNate Lawson     static int once;
281000a30448SNate Lawson 
281100a30448SNate Lawson     if (!once) {
28122d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
281300a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n");
281400a30448SNate Lawson 	once = 1;
281500a30448SNate Lawson     }
281600a30448SNate Lawson     return (acpi_EnterSleepState(sc, state));
281700a30448SNate Lawson }
281800a30448SNate Lawson 
2819c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
282000a30448SNate Lawson static void
2821ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context)
2822ffe3f1f8SMitsuru IWASAKI {
2823ffe3f1f8SMitsuru IWASAKI     struct acpi_softc *sc = (struct acpi_softc *)context;
2824ffe3f1f8SMitsuru IWASAKI 
2825ffe3f1f8SMitsuru IWASAKI     if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2826ffe3f1f8SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
2827ffe3f1f8SMitsuru IWASAKI 	    sc->acpi_next_sstate);
2828ffe3f1f8SMitsuru IWASAKI }
2829ffe3f1f8SMitsuru IWASAKI 
2830ffe3f1f8SMitsuru IWASAKI static void
283100a30448SNate Lawson acpi_sleep_force(void *arg)
283200a30448SNate Lawson {
28332d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
283400a30448SNate Lawson 
28352d0c82e8SJung-uk Kim     device_printf(sc->acpi_dev,
28362d0c82e8SJung-uk Kim 	"suspend request timed out, forcing sleep now\n");
2837ffe3f1f8SMitsuru IWASAKI     /*
2838fadf3fb9SJohn Baldwin      * XXX Suspending from callout causes freezes in DEVICE_SUSPEND().
2839ffe3f1f8SMitsuru IWASAKI      * Suspend from acpi_task thread instead.
2840ffe3f1f8SMitsuru IWASAKI      */
2841ffe3f1f8SMitsuru IWASAKI     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
2842ffe3f1f8SMitsuru IWASAKI 	acpi_sleep_force_task, sc)))
2843ffe3f1f8SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n");
284400a30448SNate Lawson }
2845c66d2b38SJung-uk Kim #endif
284600a30448SNate Lawson 
284700a30448SNate Lawson /*
284800a30448SNate Lawson  * Request that the system enter the given suspend state.  All /dev/apm
284900a30448SNate Lawson  * devices and devd(8) will be notified.  Userland then has a chance to
285000a30448SNate Lawson  * save state and acknowledge the request.  The system sleeps once all
285100a30448SNate Lawson  * acks are in.
285200a30448SNate Lawson  */
285300a30448SNate Lawson int
285400a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state)
285500a30448SNate Lawson {
285671f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
285700a30448SNate Lawson     struct apm_clone_data *clone;
2858337744d9SJung-uk Kim     ACPI_STATUS status;
285900a30448SNate Lawson 
2860a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
286100a30448SNate Lawson 	return (EINVAL);
2862a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state])
2863a0e73a12SJung-uk Kim 	return (EOPNOTSUPP);
286400a30448SNate Lawson 
28652eb0015aSColin Percival     /*
28662eb0015aSColin Percival      * If a reboot/shutdown/suspend request is already in progress or
28672eb0015aSColin Percival      * suspend is blocked due to an upcoming shutdown, just return.
28682eb0015aSColin Percival      */
28692eb0015aSColin Percival     if (rebooting || sc->acpi_next_sstate != 0 || suspend_blocked) {
2870b12bc99fSMitsuru IWASAKI 	return (0);
2871b12bc99fSMitsuru IWASAKI     }
2872b12bc99fSMitsuru IWASAKI 
2873f22377a8SMitsuru IWASAKI     /* Wait until sleep is enabled. */
2874f22377a8SMitsuru IWASAKI     while (sc->acpi_sleep_disabled) {
2875f22377a8SMitsuru IWASAKI 	AcpiOsSleep(1000);
2876f22377a8SMitsuru IWASAKI     }
2877f22377a8SMitsuru IWASAKI 
2878337744d9SJung-uk Kim     ACPI_LOCK(acpi);
287900a30448SNate Lawson 
2880f22377a8SMitsuru IWASAKI     sc->acpi_next_sstate = state;
288100a30448SNate Lawson 
2882337744d9SJung-uk Kim     /* S5 (soft-off) should be entered directly with no waiting. */
2883337744d9SJung-uk Kim     if (state == ACPI_STATE_S5) {
2884337744d9SJung-uk Kim     	ACPI_UNLOCK(acpi);
2885337744d9SJung-uk Kim 	status = acpi_EnterSleepState(sc, state);
2886337744d9SJung-uk Kim 	return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2887337744d9SJung-uk Kim     }
2888337744d9SJung-uk Kim 
288900a30448SNate Lawson     /* Record the pending state and notify all apm devices. */
289000a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
289100a30448SNate Lawson 	clone->notify_status = APM_EV_NONE;
289200a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_DEVD) == 0) {
289300a30448SNate Lawson 	    selwakeuppri(&clone->sel_read, PZERO);
2894374d9c4dSJohn Baldwin 	    KNOTE_LOCKED(&clone->sel_read.si_note, 0);
289500a30448SNate Lawson 	}
289600a30448SNate Lawson     }
289700a30448SNate Lawson 
28980c26519eSMitsuru IWASAKI     /* If devd(8) is not running, immediately enter the sleep state. */
2899d42f0ad8SJung-uk Kim     if (!devctl_process_running()) {
29000c26519eSMitsuru IWASAKI 	ACPI_UNLOCK(acpi);
2901337744d9SJung-uk Kim 	status = acpi_EnterSleepState(sc, state);
2902337744d9SJung-uk Kim 	return (ACPI_SUCCESS(status) ? 0 : ENXIO);
29030c26519eSMitsuru IWASAKI     }
29040c26519eSMitsuru IWASAKI 
290500a30448SNate Lawson     /*
290600a30448SNate Lawson      * Set a timeout to fire if userland doesn't ack the suspend request
290700a30448SNate Lawson      * in time.  This way we still eventually go to sleep if we were
290800a30448SNate Lawson      * overheating or running low on battery, even if userland is hung.
290900a30448SNate Lawson      * We cancel this timeout once all userland acks are in or the
291000a30448SNate Lawson      * suspend request is aborted.
291100a30448SNate Lawson      */
291200a30448SNate Lawson     callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
291300a30448SNate Lawson     ACPI_UNLOCK(acpi);
2914d42f0ad8SJung-uk Kim 
2915d42f0ad8SJung-uk Kim     /* Now notify devd(8) also. */
2916d42f0ad8SJung-uk Kim     acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2917d42f0ad8SJung-uk Kim 
291800a30448SNate Lawson     return (0);
2919c66d2b38SJung-uk Kim #else
2920c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2921c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2922c66d2b38SJung-uk Kim #endif
292300a30448SNate Lawson }
292400a30448SNate Lawson 
292500a30448SNate Lawson /*
292600a30448SNate Lawson  * Acknowledge (or reject) a pending sleep state.  The caller has
292700a30448SNate Lawson  * prepared for suspend and is now ready for it to proceed.  If the
292800a30448SNate Lawson  * error argument is non-zero, it indicates suspend should be cancelled
292900a30448SNate Lawson  * and gives an errno value describing why.  Once all votes are in,
293000a30448SNate Lawson  * we suspend the system.
293100a30448SNate Lawson  */
293200a30448SNate Lawson int
293300a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error)
293400a30448SNate Lawson {
2935c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
293600a30448SNate Lawson     struct acpi_softc *sc;
293700a30448SNate Lawson     int ret, sleeping;
293800a30448SNate Lawson 
293900a30448SNate Lawson     /* If no pending sleep state, return an error. */
294000a30448SNate Lawson     ACPI_LOCK(acpi);
294100a30448SNate Lawson     sc = clone->acpi_sc;
294200a30448SNate Lawson     if (sc->acpi_next_sstate == 0) {
294300a30448SNate Lawson     	ACPI_UNLOCK(acpi);
294400a30448SNate Lawson 	return (ENXIO);
294500a30448SNate Lawson     }
294600a30448SNate Lawson 
294700a30448SNate Lawson     /* Caller wants to abort suspend process. */
294800a30448SNate Lawson     if (error) {
294900a30448SNate Lawson 	sc->acpi_next_sstate = 0;
295000a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
29512d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
29522d0c82e8SJung-uk Kim 	    "listener on %s cancelled the pending suspend\n",
295300a30448SNate Lawson 	    devtoname(clone->cdev));
295400a30448SNate Lawson     	ACPI_UNLOCK(acpi);
295500a30448SNate Lawson 	return (0);
295600a30448SNate Lawson     }
295700a30448SNate Lawson 
295800a30448SNate Lawson     /*
295900a30448SNate Lawson      * Mark this device as acking the suspend request.  Then, walk through
296000a30448SNate Lawson      * all devices, seeing if they agree yet.  We only count devices that
296100a30448SNate Lawson      * are writable since read-only devices couldn't ack the request.
296200a30448SNate Lawson      */
296300a30448SNate Lawson     sleeping = TRUE;
2964c66d2b38SJung-uk Kim     clone->notify_status = APM_EV_ACKED;
296500a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
296600a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
296700a30448SNate Lawson 	    clone->notify_status != APM_EV_ACKED) {
296800a30448SNate Lawson 	    sleeping = FALSE;
296900a30448SNate Lawson 	    break;
297000a30448SNate Lawson 	}
297100a30448SNate Lawson     }
297200a30448SNate Lawson 
297300a30448SNate Lawson     /* If all devices have voted "yes", we will suspend now. */
297400a30448SNate Lawson     if (sleeping)
297500a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
297600a30448SNate Lawson     ACPI_UNLOCK(acpi);
297700a30448SNate Lawson     ret = 0;
297800a30448SNate Lawson     if (sleeping) {
297900a30448SNate Lawson 	if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
298000a30448SNate Lawson 		ret = ENODEV;
298100a30448SNate Lawson     }
298200a30448SNate Lawson     return (ret);
2983c66d2b38SJung-uk Kim #else
2984c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2985c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2986c66d2b38SJung-uk Kim #endif
298700a30448SNate Lawson }
298800a30448SNate Lawson 
2989ece50487SMitsuru IWASAKI static void
2990ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2991ece50487SMitsuru IWASAKI {
2992d42f0ad8SJung-uk Kim     struct acpi_softc	*sc = (struct acpi_softc *)arg;
2993bee4aa4aSNate Lawson 
2994fadf3fb9SJohn Baldwin     ACPI_LOCK_ASSERT(acpi);
2995fadf3fb9SJohn Baldwin 
2996a0e73a12SJung-uk Kim     /* Reschedule if the system is not fully up and running. */
2997a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning) {
2998fadf3fb9SJohn Baldwin 	callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
2999a0e73a12SJung-uk Kim 	return;
3000a0e73a12SJung-uk Kim     }
3001a0e73a12SJung-uk Kim 
3002a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = FALSE;
3003d42f0ad8SJung-uk Kim }
3004d42f0ad8SJung-uk Kim 
3005d42f0ad8SJung-uk Kim static ACPI_STATUS
3006d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc)
3007d42f0ad8SJung-uk Kim {
3008d42f0ad8SJung-uk Kim     ACPI_STATUS		status;
3009d42f0ad8SJung-uk Kim 
3010a0e73a12SJung-uk Kim     /* Fail if the system is not fully up and running. */
3011a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning)
3012a0e73a12SJung-uk Kim 	return (AE_ERROR);
3013a0e73a12SJung-uk Kim 
3014d42f0ad8SJung-uk Kim     ACPI_LOCK(acpi);
3015d42f0ad8SJung-uk Kim     status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
3016a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
3017d42f0ad8SJung-uk Kim     ACPI_UNLOCK(acpi);
3018d42f0ad8SJung-uk Kim 
3019d42f0ad8SJung-uk Kim     return (status);
3020ece50487SMitsuru IWASAKI }
3021c30382dfSMitsuru IWASAKI 
302215e2f34fSNate Lawson enum acpi_sleep_state {
302315e2f34fSNate Lawson     ACPI_SS_NONE,
302415e2f34fSNate Lawson     ACPI_SS_GPE_SET,
302515e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
302615e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
302715e2f34fSNate Lawson     ACPI_SS_SLEPT,
302815e2f34fSNate Lawson };
302915e2f34fSNate Lawson 
303015e32d5dSMike Smith /*
303100a30448SNate Lawson  * Enter the desired system sleep state.
303215e32d5dSMike Smith  *
3033be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
303415e32d5dSMike Smith  */
303500a30448SNate Lawson static ACPI_STATUS
303600a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state)
303715e32d5dSMike Smith {
303871804adcSJung-uk Kim     register_t intr;
303915e2f34fSNate Lawson     ACPI_STATUS status;
3040b913a7d5SAndriy Gapon     ACPI_EVENT_STATUS power_button_status;
304115e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
3042f0a101b7SMitsuru IWASAKI     int sleep_result;
304315e32d5dSMike Smith 
3044b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
30450ae55423SMike Smith 
3046a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
304733c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_BAD_PARAMETER);
3048a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state]) {
3049a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
3050a0e73a12SJung-uk Kim 	    state);
3051a0e73a12SJung-uk Kim 	return (AE_SUPPORT);
3052a0e73a12SJung-uk Kim     }
3053a0e73a12SJung-uk Kim 
3054a0e73a12SJung-uk Kim     /* Re-entry once we're suspending is not allowed. */
3055a0e73a12SJung-uk Kim     status = acpi_sleep_disable(sc);
3056a0e73a12SJung-uk Kim     if (ACPI_FAILURE(status)) {
3057a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev,
3058a0e73a12SJung-uk Kim 	    "suspend request ignored (not ready yet)\n");
3059a0e73a12SJung-uk Kim 	return (status);
3060a0e73a12SJung-uk Kim     }
306133c70d41SAndriy Gapon 
306233c70d41SAndriy Gapon     if (state == ACPI_STATE_S5) {
306333c70d41SAndriy Gapon 	/*
306433c70d41SAndriy Gapon 	 * Shut down cleanly and power off.  This will call us back through the
306533c70d41SAndriy Gapon 	 * shutdown handlers.
306633c70d41SAndriy Gapon 	 */
306733c70d41SAndriy Gapon 	shutdown_nice(RB_POWEROFF);
306833c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_OK);
306933c70d41SAndriy Gapon     }
307033c70d41SAndriy Gapon 
307185f95fffSAndriy Gapon     EVENTHANDLER_INVOKE(power_suspend_early);
307285f95fffSAndriy Gapon     stop_all_proc();
30734a8fa6feSJung-uk Kim     EVENTHANDLER_INVOKE(power_suspend);
30744a8fa6feSJung-uk Kim 
3075fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
3076fdce57a0SJohn Baldwin     MPASS(mp_ncpus == 1 || smp_started);
3077fdce57a0SJohn Baldwin     thread_lock(curthread);
3078fdce57a0SJohn Baldwin     sched_bind(curthread, 0);
3079fdce57a0SJohn Baldwin     thread_unlock(curthread);
3080fdce57a0SJohn Baldwin #else
308136a483bbSJung-uk Kim     if (smp_started) {
3082c66d2b38SJung-uk Kim 	thread_lock(curthread);
3083c66d2b38SJung-uk Kim 	sched_bind(curthread, 0);
3084c66d2b38SJung-uk Kim 	thread_unlock(curthread);
308536a483bbSJung-uk Kim     }
3086fdce57a0SJohn Baldwin #endif
3087c66d2b38SJung-uk Kim 
3088a56fe095SJohn Baldwin     /*
3089a56fe095SJohn Baldwin      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
3090a56fe095SJohn Baldwin      * drivers need this.
3091a56fe095SJohn Baldwin      */
3092a56fe095SJohn Baldwin     mtx_lock(&Giant);
3093c66d2b38SJung-uk Kim 
309415e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
309556d8cb57SMitsuru IWASAKI 
3096a1fccb47SMitsuru IWASAKI     sc->acpi_sstate = state;
3097a1fccb47SMitsuru IWASAKI 
3098d4b9ff91SNate Lawson     /* Enable any GPEs as appropriate and requested by the user. */
3099d4b9ff91SNate Lawson     acpi_wake_prep_walk(state);
310015e2f34fSNate Lawson     slp_state = ACPI_SS_GPE_SET;
3101e8b4d56eSNate Lawson 
310215e32d5dSMike Smith     /*
3103c7f88fc3SNate Lawson      * Inform all devices that we are going to sleep.  If at least one
3104c7f88fc3SNate Lawson      * device fails, DEVICE_SUSPEND() automatically resumes the tree.
310515e32d5dSMike Smith      *
3106c7f88fc3SNate Lawson      * XXX Note that a better two-pass approach with a 'veto' pass
3107c7f88fc3SNate Lawson      * followed by a "real thing" pass would be better, but the current
3108c7f88fc3SNate Lawson      * bus interface does not provide for this.
310915e32d5dSMike Smith      */
311015e2f34fSNate Lawson     if (DEVICE_SUSPEND(root_bus) != 0) {
311115e2f34fSNate Lawson 	device_printf(sc->acpi_dev, "device_suspend failed\n");
311233c70d41SAndriy Gapon 	goto backout;
311315e2f34fSNate Lawson     }
311415e2f34fSNate Lawson     slp_state = ACPI_SS_DEV_SUSPEND;
3115b9c780d6SMitsuru IWASAKI 
3116be2b1797SNate Lawson     status = AcpiEnterSleepStatePrep(state);
3117be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
3118b9c780d6SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
3119b9c780d6SMitsuru IWASAKI 		      AcpiFormatException(status));
312033c70d41SAndriy Gapon 	goto backout;
3121b9c780d6SMitsuru IWASAKI     }
312215e2f34fSNate Lawson     slp_state = ACPI_SS_SLP_PREP;
3123b9c780d6SMitsuru IWASAKI 
3124be2b1797SNate Lawson     if (sc->acpi_sleep_delay > 0)
3125ff01efb5SMitsuru IWASAKI 	DELAY(sc->acpi_sleep_delay * 1000000);
3126ff01efb5SMitsuru IWASAKI 
312727dca831SAndriy Gapon     suspendclock();
3128f0a101b7SMitsuru IWASAKI     intr = intr_disable();
31294d46ef51SJung-uk Kim     if (state != ACPI_STATE_S1) {
3130f0a101b7SMitsuru IWASAKI 	sleep_result = acpi_sleep_machdep(sc, state);
3131f0a101b7SMitsuru IWASAKI 	acpi_wakeup_machdep(sc, state, sleep_result, 0);
3132b1a5c017SAndriy Gapon 
3133b1a5c017SAndriy Gapon 	/*
3134b1a5c017SAndriy Gapon 	 * XXX According to ACPI specification SCI_EN bit should be restored
3135b1a5c017SAndriy Gapon 	 * by ACPI platform (BIOS, firmware) to its pre-sleep state.
3136b1a5c017SAndriy Gapon 	 * Unfortunately some BIOSes fail to do that and that leads to
3137b1a5c017SAndriy Gapon 	 * unexpected and serious consequences during wake up like a system
3138b1a5c017SAndriy Gapon 	 * getting stuck in SMI handlers.
3139b1a5c017SAndriy Gapon 	 * This hack is picked up from Linux, which claims that it follows
3140b1a5c017SAndriy Gapon 	 * Windows behavior.
3141b1a5c017SAndriy Gapon 	 */
3142b1a5c017SAndriy Gapon 	if (sleep_result == 1 && state != ACPI_STATE_S4)
3143b1a5c017SAndriy Gapon 	    AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT);
3144b1a5c017SAndriy Gapon 
3145b913a7d5SAndriy Gapon 	if (sleep_result == 1 && state == ACPI_STATE_S3) {
3146b913a7d5SAndriy Gapon 	    /*
3147b913a7d5SAndriy Gapon 	     * Prevent mis-interpretation of the wakeup by power button
3148b913a7d5SAndriy Gapon 	     * as a request for power off.
3149b913a7d5SAndriy Gapon 	     * Ideally we should post an appropriate wakeup event,
3150b913a7d5SAndriy Gapon 	     * perhaps using acpi_event_power_button_wake or alike.
3151b913a7d5SAndriy Gapon 	     *
3152b913a7d5SAndriy Gapon 	     * Clearing of power button status after wakeup is mandated
3153b913a7d5SAndriy Gapon 	     * by ACPI specification in section "Fixed Power Button".
3154b913a7d5SAndriy Gapon 	     *
3155b913a7d5SAndriy Gapon 	     * XXX As of ACPICA 20121114 AcpiGetEventStatus provides
3156b913a7d5SAndriy Gapon 	     * status as 0/1 corressponding to inactive/active despite
3157b913a7d5SAndriy Gapon 	     * its type being ACPI_EVENT_STATUS.  In other words,
3158b913a7d5SAndriy Gapon 	     * we should not test for ACPI_EVENT_FLAG_SET for time being.
3159b913a7d5SAndriy Gapon 	     */
3160b913a7d5SAndriy Gapon 	    if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON,
3161b913a7d5SAndriy Gapon 		&power_button_status)) && power_button_status != 0) {
3162b913a7d5SAndriy Gapon 		AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
3163b913a7d5SAndriy Gapon 		device_printf(sc->acpi_dev,
3164b913a7d5SAndriy Gapon 		    "cleared fixed power button status\n");
3165b913a7d5SAndriy Gapon 	    }
3166b913a7d5SAndriy Gapon 	}
3167b913a7d5SAndriy Gapon 
3168f0a101b7SMitsuru IWASAKI 	intr_restore(intr);
3169f0a101b7SMitsuru IWASAKI 
3170f0a101b7SMitsuru IWASAKI 	/* call acpi_wakeup_machdep() again with interrupt enabled */
3171f0a101b7SMitsuru IWASAKI 	acpi_wakeup_machdep(sc, state, sleep_result, 1);
3172f0a101b7SMitsuru IWASAKI 
31730a15ff37SAndriy Gapon 	AcpiLeaveSleepStatePrep(state);
31740a15ff37SAndriy Gapon 
3175f0a101b7SMitsuru IWASAKI 	if (sleep_result == -1)
3176a159c266SJung-uk Kim 		goto backout;
31776161544cSTakanori Watanabe 
31786161544cSTakanori Watanabe 	/* Re-enable ACPI hardware on wakeup from sleep state 4. */
3179be2b1797SNate Lawson 	if (state == ACPI_STATE_S4)
3180964679ceSMitsuru IWASAKI 	    AcpiEnable();
31816161544cSTakanori Watanabe     } else {
31821df130f1SJung-uk Kim 	status = AcpiEnterSleepState(state);
318371804adcSJung-uk Kim 	intr_restore(intr);
31840a15ff37SAndriy Gapon 	AcpiLeaveSleepStatePrep(state);
3185be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
3186be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
3187be2b1797SNate Lawson 			  AcpiFormatException(status));
318833c70d41SAndriy Gapon 	    goto backout;
318915e32d5dSMike Smith 	}
31906161544cSTakanori Watanabe     }
319115e2f34fSNate Lawson     slp_state = ACPI_SS_SLEPT;
3192ece50487SMitsuru IWASAKI 
319315e2f34fSNate Lawson     /*
319415e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
319515e2f34fSNate Lawson      * process.  This handles both the error and success cases.
319615e2f34fSNate Lawson      */
319733c70d41SAndriy Gapon backout:
319827dca831SAndriy Gapon     if (slp_state >= ACPI_SS_SLP_PREP)
319927dca831SAndriy Gapon 	resumeclock();
320015e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
320115e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
320215e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
320315e2f34fSNate Lawson     }
3204e3b56b66SMitsuru IWASAKI     if (slp_state >= ACPI_SS_DEV_SUSPEND)
3205e3b56b66SMitsuru IWASAKI 	DEVICE_RESUME(root_bus);
3206f0a101b7SMitsuru IWASAKI     if (slp_state >= ACPI_SS_SLP_PREP)
320715e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
3208a0a15716SJung-uk Kim     if (slp_state >= ACPI_SS_SLEPT) {
3209279be68bSAndriy Gapon #if defined(__i386__) || defined(__amd64__)
3210279be68bSAndriy Gapon 	/* NB: we are still using ACPI timecounter at this point. */
3211279be68bSAndriy Gapon 	resume_TSC();
3212279be68bSAndriy Gapon #endif
3213a0a15716SJung-uk Kim 	acpi_resync_clock(sc);
321415e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
3215a0a15716SJung-uk Kim     }
3216337744d9SJung-uk Kim     sc->acpi_next_sstate = 0;
321715e2f34fSNate Lawson 
3218a56fe095SJohn Baldwin     mtx_unlock(&Giant);
3219c66d2b38SJung-uk Kim 
3220fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
3221fdce57a0SJohn Baldwin     thread_lock(curthread);
3222fdce57a0SJohn Baldwin     sched_unbind(curthread);
3223fdce57a0SJohn Baldwin     thread_unlock(curthread);
3224fdce57a0SJohn Baldwin #else
322536a483bbSJung-uk Kim     if (smp_started) {
3226c66d2b38SJung-uk Kim 	thread_lock(curthread);
3227c66d2b38SJung-uk Kim 	sched_unbind(curthread);
3228c66d2b38SJung-uk Kim 	thread_unlock(curthread);
322936a483bbSJung-uk Kim     }
3230fdce57a0SJohn Baldwin #endif
3231c66d2b38SJung-uk Kim 
323285f95fffSAndriy Gapon     resume_all_proc();
323385f95fffSAndriy Gapon 
32344a8fa6feSJung-uk Kim     EVENTHANDLER_INVOKE(power_resume);
32354a8fa6feSJung-uk Kim 
3236d42f0ad8SJung-uk Kim     /* Allow another sleep request after a while. */
3237fadf3fb9SJohn Baldwin     callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3238d42f0ad8SJung-uk Kim 
3239d42f0ad8SJung-uk Kim     /* Run /etc/rc.resume after we are back. */
3240d42f0ad8SJung-uk Kim     if (devctl_process_running())
3241d42f0ad8SJung-uk Kim 	acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
3242d42f0ad8SJung-uk Kim 
32430ae55423SMike Smith     return_ACPI_STATUS (status);
324415e32d5dSMike Smith }
324515e32d5dSMike Smith 
3246a0a15716SJung-uk Kim static void
32470755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc)
32480755473bSJung-uk Kim {
32490755473bSJung-uk Kim 
32500755473bSJung-uk Kim     /*
32510755473bSJung-uk Kim      * Warm up timecounter again and reset system clock.
32520755473bSJung-uk Kim      */
32530755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
32540755473bSJung-uk Kim     inittodr(time_second + sc->acpi_sleep_delay);
32550755473bSJung-uk Kim }
32560755473bSJung-uk Kim 
3257e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
3258e8b4d56eSNate Lawson int
3259e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
3260e8b4d56eSNate Lawson {
3261e8b4d56eSNate Lawson     struct acpi_prw_data prw;
3262e8b4d56eSNate Lawson     ACPI_STATUS status;
3263e8b4d56eSNate Lawson     int flags;
3264e8b4d56eSNate Lawson 
3265d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
32662be4e471SJung-uk Kim     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
3267e8b4d56eSNate Lawson 	return (ENXIO);
3268e8b4d56eSNate Lawson 
3269d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
3270e8b4d56eSNate Lawson     if (enable) {
32715a77b11bSJung-uk Kim 	status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
32725a77b11bSJung-uk Kim 	    ACPI_GPE_ENABLE);
3273e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
3274e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
3275e8b4d56eSNate Lawson 	    return (ENXIO);
3276e8b4d56eSNate Lawson 	}
3277d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
3278e8b4d56eSNate Lawson     } else {
32795a77b11bSJung-uk Kim 	status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
32805a77b11bSJung-uk Kim 	    ACPI_GPE_DISABLE);
3281e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
3282e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
3283e8b4d56eSNate Lawson 	    return (ENXIO);
3284e8b4d56eSNate Lawson 	}
3285d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
3286e8b4d56eSNate Lawson     }
3287e8b4d56eSNate Lawson 
3288e8b4d56eSNate Lawson     return (0);
3289e8b4d56eSNate Lawson }
3290e8b4d56eSNate Lawson 
3291d4b9ff91SNate Lawson static int
3292d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
3293e8b4d56eSNate Lawson {
3294e8b4d56eSNate Lawson     struct acpi_prw_data prw;
3295d4b9ff91SNate Lawson     device_t dev;
3296e8b4d56eSNate Lawson 
3297d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
3298e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
3299e8b4d56eSNate Lawson 	return (ENXIO);
3300d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
3301e8b4d56eSNate Lawson 
3302e8b4d56eSNate Lawson     /*
3303d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
3304d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
3305d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
3306d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
3307d4b9ff91SNate Lawson      * and set _PSW.
3308e8b4d56eSNate Lawson      */
3309d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
33105a77b11bSJung-uk Kim 	AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
3311e8b4d56eSNate Lawson 	if (bootverbose)
3312d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
3313d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
3314d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
3315d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
3316d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
3317d4b9ff91SNate Lawson 	if (bootverbose)
3318d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
3319d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
3320d4b9ff91SNate Lawson     }
3321cc85c78cSNate Lawson 
3322cc85c78cSNate Lawson     return (0);
3323e8b4d56eSNate Lawson }
3324e8b4d56eSNate Lawson 
3325d4b9ff91SNate Lawson static int
3326d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
3327cc85c78cSNate Lawson {
3328cc85c78cSNate Lawson     struct acpi_prw_data prw;
3329d4b9ff91SNate Lawson     device_t dev;
3330cc85c78cSNate Lawson 
3331cc85c78cSNate Lawson     /*
3332d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
3333d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
3334cc85c78cSNate Lawson      */
3335d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
3336d4b9ff91SNate Lawson 	return (ENXIO);
3337d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
3338d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
3339d4b9ff91SNate Lawson 	return (0);
3340cc85c78cSNate Lawson 
3341d4b9ff91SNate Lawson     /*
3342d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
3343d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
3344d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
3345d4b9ff91SNate Lawson      */
3346d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
33475a77b11bSJung-uk Kim 	AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
3348d4b9ff91SNate Lawson 	if (bootverbose)
3349d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
3350d4b9ff91SNate Lawson     } else {
3351d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
3352d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
3353d4b9ff91SNate Lawson 	if (bootverbose)
3354d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
3355d4b9ff91SNate Lawson 		acpi_name(handle));
3356d4b9ff91SNate Lawson     }
3357d4b9ff91SNate Lawson 
3358e8b4d56eSNate Lawson     return (0);
3359e8b4d56eSNate Lawson }
3360e8b4d56eSNate Lawson 
3361e8b4d56eSNate Lawson static ACPI_STATUS
3362d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
3363e8b4d56eSNate Lawson {
3364d4b9ff91SNate Lawson     int sstate;
3365e8b4d56eSNate Lawson 
3366d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
3367d4b9ff91SNate Lawson     sstate = *(int *)context;
3368d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
3369d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
3370d4b9ff91SNate Lawson     else
3371d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
3372e8b4d56eSNate Lawson     return (AE_OK);
3373e8b4d56eSNate Lawson }
3374e8b4d56eSNate Lawson 
3375d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
3376e8b4d56eSNate Lawson static int
3377d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
3378e8b4d56eSNate Lawson {
3379e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
3380e8b4d56eSNate Lawson 
3381e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
3382d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
33832272d050SJung-uk Kim 	    acpi_wake_prep, NULL, &sstate, NULL);
3384e8b4d56eSNate Lawson     return (0);
3385e8b4d56eSNate Lawson }
3386e8b4d56eSNate Lawson 
338788a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
338888a79fc0SNate Lawson static int
338988a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
339088a79fc0SNate Lawson {
339188a79fc0SNate Lawson     int error, i, numdevs;
339288a79fc0SNate Lawson     device_t *devlist;
339388a79fc0SNate Lawson     device_t child;
3394d4b9ff91SNate Lawson     ACPI_STATUS status;
339588a79fc0SNate Lawson 
339688a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
33971c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
33981c9ec538SNate Lawson 	if (numdevs == 0)
33991c9ec538SNate Lawson 	    free(devlist, M_TEMP);
340088a79fc0SNate Lawson 	return (error);
34011c9ec538SNate Lawson     }
340288a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
340388a79fc0SNate Lawson 	child = devlist[i];
3404d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
340588a79fc0SNate Lawson 	if (!device_is_attached(child))
340688a79fc0SNate Lawson 	    continue;
3407d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
3408d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
340988a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
341088a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
34117029da5cSPawel Biernacki 		"wake", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, child, 0,
341288a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
341388a79fc0SNate Lawson 	}
341488a79fc0SNate Lawson     }
341588a79fc0SNate Lawson     free(devlist, M_TEMP);
341688a79fc0SNate Lawson 
341788a79fc0SNate Lawson     return (0);
341888a79fc0SNate Lawson }
341988a79fc0SNate Lawson 
342088a79fc0SNate Lawson /* Enable or disable wake from userland. */
342188a79fc0SNate Lawson static int
342288a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
342388a79fc0SNate Lawson {
342488a79fc0SNate Lawson     int enable, error;
342588a79fc0SNate Lawson     device_t dev;
342688a79fc0SNate Lawson 
342788a79fc0SNate Lawson     dev = (device_t)arg1;
3428d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
342988a79fc0SNate Lawson 
343088a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
343188a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
343288a79fc0SNate Lawson 	return (error);
343388a79fc0SNate Lawson     if (enable != 0 && enable != 1)
343488a79fc0SNate Lawson 	return (EINVAL);
343588a79fc0SNate Lawson 
343688a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
343788a79fc0SNate Lawson }
343888a79fc0SNate Lawson 
3439e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
3440d4b9ff91SNate Lawson int
3441e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
3442e8b4d56eSNate Lawson {
3443e8b4d56eSNate Lawson     ACPI_STATUS			status;
3444e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
3445e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
3446d4b9ff91SNate Lawson     int				error, i, power_count;
3447e8b4d56eSNate Lawson 
3448e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
3449e8b4d56eSNate Lawson 	return (EINVAL);
3450e8b4d56eSNate Lawson 
3451e8b4d56eSNate Lawson     /*
3452e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
3453e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
3454e8b4d56eSNate Lawson      */
3455e8b4d56eSNate Lawson     error = EINVAL;
3456e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
3457e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
3458e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
3459e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
3460e8b4d56eSNate Lawson 	return (ENOENT);
3461e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
3462e8b4d56eSNate Lawson     if (res == NULL)
3463e8b4d56eSNate Lawson 	return (ENOENT);
3464e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
3465e8b4d56eSNate Lawson 	goto out;
3466e8b4d56eSNate Lawson 
3467e8b4d56eSNate Lawson     /*
3468e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
3469e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
3470e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
3471e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
3472e8b4d56eSNate Lawson      */
3473e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
3474e8b4d56eSNate Lawson 	goto out;
3475e8b4d56eSNate Lawson 
3476e8b4d56eSNate Lawson     /*
3477e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
3478e8b4d56eSNate Lawson      */
3479e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
3480e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
3481e8b4d56eSNate Lawson 	/*
3482e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
3483e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
3484e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
3485e8b4d56eSNate Lawson 	 * enabled for the wake event.
3486e8b4d56eSNate Lawson 	 */
3487e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
3488e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
3489e8b4d56eSNate Lawson 	error = 0;
3490e8b4d56eSNate Lawson 	break;
3491e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
3492e8b4d56eSNate Lawson 	/*
3493e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
3494e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
3495e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
3496e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
3497e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
3498e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
3499e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
3500e8b4d56eSNate Lawson 	 * the wake event.
3501e8b4d56eSNate Lawson 	 *
3502e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
3503e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
3504e8b4d56eSNate Lawson 	 */
3505e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
3506e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
3507e8b4d56eSNate Lawson 	    goto out;
3508e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3509e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
3510e8b4d56eSNate Lawson 	    goto out;
3511e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3512e8b4d56eSNate Lawson 	    goto out;
3513e8b4d56eSNate Lawson 	error = 0;
3514e8b4d56eSNate Lawson 	break;
3515e8b4d56eSNate Lawson     default:
3516e8b4d56eSNate Lawson 	goto out;
3517e8b4d56eSNate Lawson     }
3518e8b4d56eSNate Lawson 
3519d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
3520d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
3521d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
3522d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
3523d4b9ff91SNate Lawson 	power_count = 0;
3524d4b9ff91SNate Lawson     }
3525d4b9ff91SNate Lawson     prw->power_res_count = power_count;
3526d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
3527d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
3528e8b4d56eSNate Lawson 
3529e8b4d56eSNate Lawson out:
3530e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
3531e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
3532e8b4d56eSNate Lawson     return (error);
3533e8b4d56eSNate Lawson }
3534e8b4d56eSNate Lawson 
353515e32d5dSMike Smith /*
353615e32d5dSMike Smith  * ACPI Event Handlers
353715e32d5dSMike Smith  */
353815e32d5dSMike Smith 
353915e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
354015e32d5dSMike Smith 
354115e32d5dSMike Smith static void
354215e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
354315e32d5dSMike Smith {
35442d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
354500a30448SNate Lawson     int ret;
3546bee4aa4aSNate Lawson 
3547b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
354815e32d5dSMike Smith 
3549a0e73a12SJung-uk Kim     /* Check if button action is disabled or unknown. */
3550a0e73a12SJung-uk Kim     if (state == ACPI_STATE_UNKNOWN)
3551813d6dcaSNate Lawson 	return;
3552813d6dcaSNate Lawson 
355300a30448SNate Lawson     /* Request that the system prepare to enter the given suspend state. */
35542d0c82e8SJung-uk Kim     ret = acpi_ReqSleepState(sc, state);
355500a30448SNate Lawson     if (ret != 0)
35562d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
35572d0c82e8SJung-uk Kim 	    "request to enter state S%d failed (err %d)\n", state, ret);
3558bee4aa4aSNate Lawson 
35590ae55423SMike Smith     return_VOID;
356015e32d5dSMike Smith }
356115e32d5dSMike Smith 
356215e32d5dSMike Smith static void
356315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
356415e32d5dSMike Smith {
3565bee4aa4aSNate Lawson 
3566b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
35670ae55423SMike Smith 
3568bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
3569cb9b0d80SMike Smith 
35700ae55423SMike Smith     return_VOID;
357115e32d5dSMike Smith }
357215e32d5dSMike Smith 
357315e32d5dSMike Smith /*
357415e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
357515e32d5dSMike Smith  */
3576b5854f5fSJung-uk Kim static void
3577b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context)
3578b5854f5fSJung-uk Kim {
3579b5854f5fSJung-uk Kim 
3580b5854f5fSJung-uk Kim     EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context);
3581b5854f5fSJung-uk Kim }
3582b5854f5fSJung-uk Kim 
3583b5854f5fSJung-uk Kim static void
3584b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context)
3585b5854f5fSJung-uk Kim {
3586b5854f5fSJung-uk Kim 
3587b5854f5fSJung-uk Kim     EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context);
3588b5854f5fSJung-uk Kim }
3589b5854f5fSJung-uk Kim 
359015e32d5dSMike Smith UINT32
359133febf93SNate Lawson acpi_event_power_button_sleep(void *context)
359215e32d5dSMike Smith {
359315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
359415e32d5dSMike Smith 
3595b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
35960ae55423SMike Smith 
3597b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3598b5854f5fSJung-uk Kim 	acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx)))
3599b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3600b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
360115e32d5dSMike Smith }
360215e32d5dSMike Smith 
360315e32d5dSMike Smith UINT32
360433febf93SNate Lawson acpi_event_power_button_wake(void *context)
360515e32d5dSMike Smith {
360615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
360715e32d5dSMike Smith 
3608b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
36090ae55423SMike Smith 
3610b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3611b5854f5fSJung-uk Kim 	acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx)))
3612b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3613b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
361415e32d5dSMike Smith }
361515e32d5dSMike Smith 
361615e32d5dSMike Smith UINT32
361733febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
361815e32d5dSMike Smith {
361915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
362015e32d5dSMike Smith 
3621b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
36220ae55423SMike Smith 
3623b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3624b5854f5fSJung-uk Kim 	acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx)))
3625b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3626b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
362715e32d5dSMike Smith }
362815e32d5dSMike Smith 
362915e32d5dSMike Smith UINT32
363033febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
363115e32d5dSMike Smith {
363215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
363315e32d5dSMike Smith 
3634b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
36350ae55423SMike Smith 
3636b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3637b5854f5fSJung-uk Kim 	acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx)))
3638b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3639b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
364015e32d5dSMike Smith }
364115e32d5dSMike Smith 
364215e32d5dSMike Smith /*
3643bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
3644bee4aa4aSNate Lawson  * use this for single-threaded callers.
364515e32d5dSMike Smith  */
364615e32d5dSMike Smith char *
364715e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
364815e32d5dSMike Smith {
3649bee4aa4aSNate Lawson     ACPI_BUFFER buf;
3650bee4aa4aSNate Lawson     static char data[256];
365115e32d5dSMike Smith 
3652bee4aa4aSNate Lawson     buf.Length = sizeof(data);
3653bee4aa4aSNate Lawson     buf.Pointer = data;
3654cb9b0d80SMike Smith 
36550a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3656bee4aa4aSNate Lawson 	return (data);
3657bee4aa4aSNate Lawson     return ("(unknown)");
365815e32d5dSMike Smith }
365915e32d5dSMike Smith 
366015e32d5dSMike Smith /*
366115e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
366215e32d5dSMike Smith  * parts of the namespace.
366315e32d5dSMike Smith  */
366415e32d5dSMike Smith int
366515e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
366615e32d5dSMike Smith {
36673c600cfbSJohn Baldwin     char	*cp, *env, *np;
366815e32d5dSMike Smith     int		len;
366915e32d5dSMike Smith 
367015e32d5dSMike Smith     np = acpi_name(handle);
367115e32d5dSMike Smith     if (*np == '\\')
367215e32d5dSMike Smith 	np++;
36732be111bfSDavide Italiano     if ((env = kern_getenv("debug.acpi.avoid")) == NULL)
367415e32d5dSMike Smith 	return (0);
367515e32d5dSMike Smith 
3676be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
36773c600cfbSJohn Baldwin     cp = env;
367815e32d5dSMike Smith     for (;;) {
3679bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
368015e32d5dSMike Smith 	    cp++;
368115e32d5dSMike Smith 	if (*cp == 0)
368215e32d5dSMike Smith 	    break;
368315e32d5dSMike Smith 	len = 0;
3684bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
368515e32d5dSMike Smith 	    len++;
3686d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
36873c600cfbSJohn Baldwin 	    freeenv(env);
36880ae55423SMike Smith 	    return(1);
3689d786139cSMaxime Henrion 	}
36900ae55423SMike Smith 	cp += len;
36910ae55423SMike Smith     }
36923c600cfbSJohn Baldwin     freeenv(env);
3693be2b1797SNate Lawson 
36940ae55423SMike Smith     return (0);
36950ae55423SMike Smith }
36960ae55423SMike Smith 
36970ae55423SMike Smith /*
36980ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
36990ae55423SMike Smith  */
37000ae55423SMike Smith int
37010ae55423SMike Smith acpi_disabled(char *subsys)
37020ae55423SMike Smith {
370378689b15SMaxime Henrion     char	*cp, *env;
37040ae55423SMike Smith     int		len;
37050ae55423SMike Smith 
37062be111bfSDavide Italiano     if ((env = kern_getenv("debug.acpi.disabled")) == NULL)
37070ae55423SMike Smith 	return (0);
37083184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
370978689b15SMaxime Henrion 	freeenv(env);
37100ae55423SMike Smith 	return (1);
3711d786139cSMaxime Henrion     }
37120ae55423SMike Smith 
37133184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
371478689b15SMaxime Henrion     cp = env;
37150ae55423SMike Smith     for (;;) {
37163184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
37170ae55423SMike Smith 	    cp++;
37183184cf5aSNate Lawson 	if (*cp == '\0')
37190ae55423SMike Smith 	    break;
37200ae55423SMike Smith 	len = 0;
37213184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
37220ae55423SMike Smith 	    len++;
37233184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
372478689b15SMaxime Henrion 	    freeenv(env);
372515e32d5dSMike Smith 	    return (1);
3726d786139cSMaxime Henrion 	}
372715e32d5dSMike Smith 	cp += len;
372815e32d5dSMike Smith     }
372978689b15SMaxime Henrion     freeenv(env);
3730be2b1797SNate Lawson 
373115e32d5dSMike Smith     return (0);
373215e32d5dSMike Smith }
373315e32d5dSMike Smith 
373464de8019SJohn Baldwin static void
373564de8019SJohn Baldwin acpi_lookup(void *arg, const char *name, device_t *dev)
373664de8019SJohn Baldwin {
373764de8019SJohn Baldwin     ACPI_HANDLE handle;
373864de8019SJohn Baldwin 
373964de8019SJohn Baldwin     if (*dev != NULL)
374064de8019SJohn Baldwin 	return;
374164de8019SJohn Baldwin 
374264de8019SJohn Baldwin     /*
374364de8019SJohn Baldwin      * Allow any handle name that is specified as an absolute path and
374464de8019SJohn Baldwin      * starts with '\'.  We could restrict this to \_SB and friends,
374564de8019SJohn Baldwin      * but see acpi_probe_children() for notes on why we scan the entire
374664de8019SJohn Baldwin      * namespace for devices.
374764de8019SJohn Baldwin      *
374864de8019SJohn Baldwin      * XXX: The pathname argument to AcpiGetHandle() should be fixed to
374964de8019SJohn Baldwin      * be const.
375064de8019SJohn Baldwin      */
375164de8019SJohn Baldwin     if (name[0] != '\\')
375264de8019SJohn Baldwin 	return;
375364de8019SJohn Baldwin     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name),
375464de8019SJohn Baldwin 	&handle)))
375564de8019SJohn Baldwin 	return;
375664de8019SJohn Baldwin     *dev = acpi_get_device(handle);
375764de8019SJohn Baldwin }
375864de8019SJohn Baldwin 
375915e32d5dSMike Smith /*
376015e32d5dSMike Smith  * Control interface.
376115e32d5dSMike Smith  *
37620ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
3763be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
3764be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
376515e32d5dSMike Smith  */
37660ae55423SMike Smith struct acpi_ioctl_hook
37670ae55423SMike Smith {
37680ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
37690ae55423SMike Smith     u_long			 cmd;
3770be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
37710ae55423SMike Smith     void			 *arg;
37720ae55423SMike Smith };
37730ae55423SMike Smith 
37740ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
37750ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
37760ae55423SMike Smith 
37770ae55423SMike Smith int
3778be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
37790ae55423SMike Smith {
37800ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
37810ae55423SMike Smith 
37820ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
37830ae55423SMike Smith 	return (ENOMEM);
37840ae55423SMike Smith     hp->cmd = cmd;
37850ae55423SMike Smith     hp->fn = fn;
37860ae55423SMike Smith     hp->arg = arg;
378715e2f34fSNate Lawson 
378815e2f34fSNate Lawson     ACPI_LOCK(acpi);
37890ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
37900ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
37910ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
37920ae55423SMike Smith     }
37930ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
379415e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
379515e2f34fSNate Lawson 
37960ae55423SMike Smith     return (0);
37970ae55423SMike Smith }
37980ae55423SMike Smith 
37990ae55423SMike Smith void
3800be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
38010ae55423SMike Smith {
38020ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
38030ae55423SMike Smith 
380415e2f34fSNate Lawson     ACPI_LOCK(acpi);
38050ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
380615e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
38070ae55423SMike Smith 	    break;
38080ae55423SMike Smith 
38090ae55423SMike Smith     if (hp != NULL) {
38100ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
38110ae55423SMike Smith 	free(hp, M_ACPIDEV);
38120ae55423SMike Smith     }
381315e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
38140ae55423SMike Smith }
38150ae55423SMike Smith 
381615e32d5dSMike Smith static int
381700b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
381815e32d5dSMike Smith {
381915e32d5dSMike Smith     return (0);
382015e32d5dSMike Smith }
382115e32d5dSMike Smith 
382215e32d5dSMike Smith static int
382300b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
382415e32d5dSMike Smith {
382515e32d5dSMike Smith     return (0);
382615e32d5dSMike Smith }
382715e32d5dSMike Smith 
382815e32d5dSMike Smith static int
382900b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
383015e32d5dSMike Smith {
383115e32d5dSMike Smith     struct acpi_softc		*sc;
38320ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
3833bee4aa4aSNate Lawson     int				error, state;
383415e32d5dSMike Smith 
3835a2e35898SDavid E. O'Brien     error = 0;
3836a2e35898SDavid E. O'Brien     hp = NULL;
383715e32d5dSMike Smith     sc = dev->si_drv1;
383815e32d5dSMike Smith 
38390ae55423SMike Smith     /*
38400ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
38410ae55423SMike Smith      */
384215e2f34fSNate Lawson     ACPI_LOCK(acpi);
3843bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
38440ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3845bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
3846bee4aa4aSNate Lawson 		break;
38470ae55423SMike Smith 	}
384815e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
3849bee4aa4aSNate Lawson     if (hp)
3850bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
38510ae55423SMike Smith 
38520ae55423SMike Smith     /*
3853a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
3854a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
3855a89fcf28STakanori Watanabe      * Not changing system behavior.
3856a89fcf28STakanori Watanabe      */
3857be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
3858be2b1797SNate Lawson 	return (EPERM);
3859a89fcf28STakanori Watanabe 
3860be2b1797SNate Lawson     /* Core system ioctls. */
386115e32d5dSMike Smith     switch (cmd) {
386200a30448SNate Lawson     case ACPIIO_REQSLPSTATE:
386300a30448SNate Lawson 	state = *(int *)addr;
386400a30448SNate Lawson 	if (state != ACPI_STATE_S5)
3865a0e73a12SJung-uk Kim 	    return (acpi_ReqSleepState(sc, state));
3866a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
3867a0e73a12SJung-uk Kim 	error = EOPNOTSUPP;
386800a30448SNate Lawson 	break;
386900a30448SNate Lawson     case ACPIIO_ACKSLPSTATE:
387000a30448SNate Lawson 	error = *(int *)addr;
387100a30448SNate Lawson 	error = acpi_AckSleepState(sc->acpi_clone, error);
387200a30448SNate Lawson 	break;
387300a30448SNate Lawson     case ACPIIO_SETSLPSTATE:	/* DEPRECATED */
387415e32d5dSMike Smith 	state = *(int *)addr;
3875a0e73a12SJung-uk Kim 	if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
3876a0e73a12SJung-uk Kim 	    return (EINVAL);
3877a0e73a12SJung-uk Kim 	if (!acpi_sleep_states[state])
3878a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3879a0e73a12SJung-uk Kim 	if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
3880a0e73a12SJung-uk Kim 	    error = ENXIO;
388115e32d5dSMike Smith 	break;
388215e32d5dSMike Smith     default:
3883bee4aa4aSNate Lawson 	error = ENXIO;
388415e32d5dSMike Smith 	break;
388515e32d5dSMike Smith     }
3886917d44c8SMitsuru IWASAKI 
388715e32d5dSMike Smith     return (error);
388815e32d5dSMike Smith }
388915e32d5dSMike Smith 
38901d073b1dSJohn Baldwin static int
3891a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname)
3892a0e73a12SJung-uk Kim {
3893a0e73a12SJung-uk Kim     int sstate;
3894a0e73a12SJung-uk Kim 
3895a0e73a12SJung-uk Kim     if (toupper(sname[0]) == 'S') {
3896a0e73a12SJung-uk Kim 	sstate = sname[1] - '0';
3897a0e73a12SJung-uk Kim 	if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
3898a0e73a12SJung-uk Kim 	    sname[2] == '\0')
3899a0e73a12SJung-uk Kim 	    return (sstate);
3900a0e73a12SJung-uk Kim     } else if (strcasecmp(sname, "NONE") == 0)
3901a0e73a12SJung-uk Kim 	return (ACPI_STATE_UNKNOWN);
3902a0e73a12SJung-uk Kim     return (-1);
3903a0e73a12SJung-uk Kim }
3904a0e73a12SJung-uk Kim 
3905a0e73a12SJung-uk Kim static const char *
3906a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate)
3907a0e73a12SJung-uk Kim {
3908a0e73a12SJung-uk Kim     static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
3909a0e73a12SJung-uk Kim 
3910a0e73a12SJung-uk Kim     if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
3911a0e73a12SJung-uk Kim 	return (snames[sstate]);
3912a0e73a12SJung-uk Kim     else if (sstate == ACPI_STATE_UNKNOWN)
3913a0e73a12SJung-uk Kim 	return ("NONE");
3914a0e73a12SJung-uk Kim     return (NULL);
3915a0e73a12SJung-uk Kim }
3916a0e73a12SJung-uk Kim 
3917a0e73a12SJung-uk Kim static int
3918d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3919d75de536SMitsuru IWASAKI {
3920d75de536SMitsuru IWASAKI     int error;
3921bee4aa4aSNate Lawson     struct sbuf sb;
3922a0e73a12SJung-uk Kim     UINT8 state;
3923d75de536SMitsuru IWASAKI 
3924bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3925a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
3926a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state])
3927a0e73a12SJung-uk Kim 	    sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
3928bee4aa4aSNate Lawson     sbuf_trim(&sb);
3929bee4aa4aSNate Lawson     sbuf_finish(&sb);
3930bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3931bee4aa4aSNate Lawson     sbuf_delete(&sb);
3932d75de536SMitsuru IWASAKI     return (error);
3933d75de536SMitsuru IWASAKI }
3934d75de536SMitsuru IWASAKI 
3935d75de536SMitsuru IWASAKI static int
39361d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
39371d073b1dSJohn Baldwin {
39381d073b1dSJohn Baldwin     char sleep_state[10];
3939a0e73a12SJung-uk Kim     int error, new_state, old_state;
39401d073b1dSJohn Baldwin 
3941a0e73a12SJung-uk Kim     old_state = *(int *)oidp->oid_arg1;
3942a0e73a12SJung-uk Kim     strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
39431d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
39441d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
3945a0e73a12SJung-uk Kim 	new_state = acpi_sname2sstate(sleep_state);
3946a0e73a12SJung-uk Kim 	if (new_state < ACPI_STATE_S1)
3947a0e73a12SJung-uk Kim 	    return (EINVAL);
394854b43614SJung-uk Kim 	if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
3949a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3950be2b1797SNate Lawson 	if (new_state != old_state)
3951a0e73a12SJung-uk Kim 	    *(int *)oidp->oid_arg1 = new_state;
39521d073b1dSJohn Baldwin     }
39531d073b1dSJohn Baldwin     return (error);
39541d073b1dSJohn Baldwin }
39551d073b1dSJohn Baldwin 
39569b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
39579b937d48SNate Lawson void
39589b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
39599b937d48SNate Lawson {
39609b937d48SNate Lawson     char		notify_buf[16];
39619b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
39629b937d48SNate Lawson     ACPI_STATUS		status;
39639b937d48SNate Lawson 
39649b937d48SNate Lawson     if (subsystem == NULL)
39659b937d48SNate Lawson 	return;
39669b937d48SNate Lawson 
39679b937d48SNate Lawson     handle_buf.Pointer = NULL;
39689b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
39690594dadeSJung-uk Kim     status = AcpiNsHandleToPathname(h, &handle_buf, FALSE);
39709b937d48SNate Lawson     if (ACPI_FAILURE(status))
39719b937d48SNate Lawson 	return;
39729b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
39739b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
39749b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
39759b937d48SNate Lawson }
39769b937d48SNate Lawson 
397715e32d5dSMike Smith #ifdef ACPI_DEBUG
39780ae55423SMike Smith /*
39790ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
39800ae55423SMike Smith  *
39810ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
39820ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
39830ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
39840ae55423SMike Smith  * prefixing the bit name with !.
39850ae55423SMike Smith  */
398615e32d5dSMike Smith struct debugtag
398715e32d5dSMike Smith {
398815e32d5dSMike Smith     char	*name;
398915e32d5dSMike Smith     UINT32	value;
399015e32d5dSMike Smith };
399115e32d5dSMike Smith 
399215e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
3993c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
3994c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
3995c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
3996c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
3997c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
3998c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
3999c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
4000c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
4001c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
4002d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
40034c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
4004d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
4005297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
40064c1cdee6SMike Smith 
4007c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
4008c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
40093184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
4010c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
40113184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
40123184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
40133184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
40144c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
4015cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
40163184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
4017b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
401815e32d5dSMike Smith     {NULL, 0}
401915e32d5dSMike Smith };
402015e32d5dSMike Smith 
402115e32d5dSMike Smith static struct debugtag dbg_level[] = {
40229501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
40234c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
40249501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
4025cc6455afSJung-uk Kim     {"ACPI_LV_REPAIR",		ACPI_LV_REPAIR},
40264c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
4027d62ab2f4SMitsuru IWASAKI 
4028d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
4029297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
40304c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
40314c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
4032d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
40334c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
40344c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
40354c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
40364c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
40374c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
40384c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
40394c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
40404c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
40414c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
40424c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
4043d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
4044d62ab2f4SMitsuru IWASAKI 
4045d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
4046d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
4047d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
4048d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
4049d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
40504c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
4051d62ab2f4SMitsuru IWASAKI 
4052d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
4053d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
4054d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
4055d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
4056d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
4057d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
4058d62ab2f4SMitsuru IWASAKI 
4059d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
406098479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
406198479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
406298479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
406398479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
406498479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
406515e32d5dSMike Smith     {NULL, 0}
406615e32d5dSMike Smith };
406715e32d5dSMike Smith 
406815e32d5dSMike Smith static void
406915e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
407015e32d5dSMike Smith {
407115e32d5dSMike Smith     char	*ep;
407215e32d5dSMike Smith     int		i, l;
40730ae55423SMike Smith     int		set;
407415e32d5dSMike Smith 
407515e32d5dSMike Smith     while (*cp) {
407615e32d5dSMike Smith 	if (isspace(*cp)) {
407715e32d5dSMike Smith 	    cp++;
407815e32d5dSMike Smith 	    continue;
407915e32d5dSMike Smith 	}
408015e32d5dSMike Smith 	ep = cp;
408115e32d5dSMike Smith 	while (*ep && !isspace(*ep))
408215e32d5dSMike Smith 	    ep++;
40830ae55423SMike Smith 	if (*cp == '!') {
40840ae55423SMike Smith 	    set = 0;
40850ae55423SMike Smith 	    cp++;
40860ae55423SMike Smith 	    if (cp == ep)
40870ae55423SMike Smith 		continue;
40880ae55423SMike Smith 	} else {
40890ae55423SMike Smith 	    set = 1;
40900ae55423SMike Smith 	}
409115e32d5dSMike Smith 	l = ep - cp;
409215e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
409315e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
4094be2b1797SNate Lawson 		if (set)
409515e32d5dSMike Smith 		    *flag |= tag[i].value;
4096be2b1797SNate Lawson 		else
40970ae55423SMike Smith 		    *flag &= ~tag[i].value;
409815e32d5dSMike Smith 	    }
409915e32d5dSMike Smith 	}
410015e32d5dSMike Smith 	cp = ep;
410115e32d5dSMike Smith     }
410215e32d5dSMike Smith }
410315e32d5dSMike Smith 
410415e32d5dSMike Smith static void
41052a4ac806SMike Smith acpi_set_debugging(void *junk)
410615e32d5dSMike Smith {
41077e639165SNate Lawson     char	*layer, *level;
410815e32d5dSMike Smith 
41091d7b121cSNate Lawson     if (cold) {
41100ae55423SMike Smith 	AcpiDbgLayer = 0;
41110ae55423SMike Smith 	AcpiDbgLevel = 0;
41121d7b121cSNate Lawson     }
41131d7b121cSNate Lawson 
41142be111bfSDavide Italiano     layer = kern_getenv("debug.acpi.layer");
41152be111bfSDavide Italiano     level = kern_getenv("debug.acpi.level");
41167e639165SNate Lawson     if (layer == NULL && level == NULL)
41177e639165SNate Lawson 	return;
41180ae55423SMike Smith 
41197e639165SNate Lawson     printf("ACPI set debug");
41207e639165SNate Lawson     if (layer != NULL) {
41217e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
41227e639165SNate Lawson 	    printf(" layer '%s'", layer);
41237e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
41247e639165SNate Lawson 	freeenv(layer);
41251d7b121cSNate Lawson     }
41267e639165SNate Lawson     if (level != NULL) {
41277e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
41287e639165SNate Lawson 	    printf(" level '%s'", level);
41297e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
41307e639165SNate Lawson 	freeenv(level);
41317e639165SNate Lawson     }
41327e639165SNate Lawson     printf("\n");
413315e32d5dSMike Smith }
4134bee4aa4aSNate Lawson 
4135be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
4136be2b1797SNate Lawson 	NULL);
41371d7b121cSNate Lawson 
41381d7b121cSNate Lawson static int
41391d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
41401d7b121cSNate Lawson {
414154f6bca0SNate Lawson     int		 error, *dbg;
41421d7b121cSNate Lawson     struct	 debugtag *tag;
414354f6bca0SNate Lawson     struct	 sbuf sb;
41440e1152fcSHans Petter Selasky     char	 temp[128];
41451d7b121cSNate Lawson 
414654f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
414754f6bca0SNate Lawson 	return (ENOMEM);
41481d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
41491d7b121cSNate Lawson 	tag = &dbg_layer[0];
41501d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
41511d7b121cSNate Lawson     } else {
41521d7b121cSNate Lawson 	tag = &dbg_level[0];
41531d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
41541d7b121cSNate Lawson     }
41551d7b121cSNate Lawson 
41561d7b121cSNate Lawson     /* Get old values if this is a get request. */
415715e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
41581d7b121cSNate Lawson     if (*dbg == 0) {
415954f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
41601d7b121cSNate Lawson     } else if (req->newptr == NULL) {
41611d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
416254f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
416354f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
41641d7b121cSNate Lawson 	}
41651d7b121cSNate Lawson     }
416654f6bca0SNate Lawson     sbuf_trim(&sb);
416754f6bca0SNate Lawson     sbuf_finish(&sb);
41680e1152fcSHans Petter Selasky     strlcpy(temp, sbuf_data(&sb), sizeof(temp));
416954f6bca0SNate Lawson     sbuf_delete(&sb);
41701d7b121cSNate Lawson 
41710e1152fcSHans Petter Selasky     error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
41720e1152fcSHans Petter Selasky 
41730e1152fcSHans Petter Selasky     /* Check for error or no change */
41741d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
41751d7b121cSNate Lawson 	*dbg = 0;
41760e1152fcSHans Petter Selasky 	kern_setenv((char *)oidp->oid_arg1, temp);
41771d7b121cSNate Lawson 	acpi_set_debugging(NULL);
41781d7b121cSNate Lawson     }
417915e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
41801d7b121cSNate Lawson 
41811d7b121cSNate Lawson     return (error);
41821d7b121cSNate Lawson }
4183bee4aa4aSNate Lawson 
41847029da5cSPawel Biernacki SYSCTL_PROC(_debug_acpi, OID_AUTO, layer,
41857029da5cSPawel Biernacki     CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_NEEDGIANT, "debug.acpi.layer", 0,
41867029da5cSPawel Biernacki     acpi_debug_sysctl, "A",
41877029da5cSPawel Biernacki     "");
41887029da5cSPawel Biernacki SYSCTL_PROC(_debug_acpi, OID_AUTO, level,
41897029da5cSPawel Biernacki     CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_NEEDGIANT, "debug.acpi.level", 0,
41907029da5cSPawel Biernacki     acpi_debug_sysctl, "A",
41917029da5cSPawel Biernacki     "");
4192bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
4193f9390180SMitsuru IWASAKI 
4194f9390180SMitsuru IWASAKI static int
41952a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
41962a18c71dSJung-uk Kim {
41972a18c71dSJung-uk Kim 	int	error;
41982a18c71dSJung-uk Kim 	int	old;
41992a18c71dSJung-uk Kim 
42002a18c71dSJung-uk Kim 	old = acpi_debug_objects;
42012a18c71dSJung-uk Kim 	error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
42022a18c71dSJung-uk Kim 	if (error != 0 || req->newptr == NULL)
42032a18c71dSJung-uk Kim 		return (error);
42042a18c71dSJung-uk Kim 	if (old == acpi_debug_objects || (old && acpi_debug_objects))
42052a18c71dSJung-uk Kim 		return (0);
42062a18c71dSJung-uk Kim 
42072a18c71dSJung-uk Kim 	ACPI_SERIAL_BEGIN(acpi);
42082a18c71dSJung-uk Kim 	AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
42092a18c71dSJung-uk Kim 	ACPI_SERIAL_END(acpi);
42102a18c71dSJung-uk Kim 
42112a18c71dSJung-uk Kim 	return (0);
42122a18c71dSJung-uk Kim }
42132a18c71dSJung-uk Kim 
42142a18c71dSJung-uk Kim static int
4215ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface)
4216ae19af49SJung-uk Kim {
4217ae19af49SJung-uk Kim 	char *p;
4218ae19af49SJung-uk Kim 	size_t len;
4219ae19af49SJung-uk Kim 	int i, j;
4220ae19af49SJung-uk Kim 
4221ae19af49SJung-uk Kim 	p = str;
4222ae19af49SJung-uk Kim 	while (isspace(*p) || *p == ',')
4223ae19af49SJung-uk Kim 		p++;
4224ae19af49SJung-uk Kim 	len = strlen(p);
4225ae19af49SJung-uk Kim 	if (len == 0)
4226ae19af49SJung-uk Kim 		return (0);
4227ae19af49SJung-uk Kim 	p = strdup(p, M_TEMP);
4228ae19af49SJung-uk Kim 	for (i = 0; i < len; i++)
4229ae19af49SJung-uk Kim 		if (p[i] == ',')
4230ae19af49SJung-uk Kim 			p[i] = '\0';
4231ae19af49SJung-uk Kim 	i = j = 0;
4232ae19af49SJung-uk Kim 	while (i < len)
4233ae19af49SJung-uk Kim 		if (isspace(p[i]) || p[i] == '\0')
4234ae19af49SJung-uk Kim 			i++;
4235ae19af49SJung-uk Kim 		else {
4236ae19af49SJung-uk Kim 			i += strlen(p + i) + 1;
4237ae19af49SJung-uk Kim 			j++;
4238ae19af49SJung-uk Kim 		}
4239ae19af49SJung-uk Kim 	if (j == 0) {
4240ae19af49SJung-uk Kim 		free(p, M_TEMP);
4241ae19af49SJung-uk Kim 		return (0);
4242ae19af49SJung-uk Kim 	}
4243ae19af49SJung-uk Kim 	iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
4244ae19af49SJung-uk Kim 	iface->num = j;
4245ae19af49SJung-uk Kim 	i = j = 0;
4246ae19af49SJung-uk Kim 	while (i < len)
4247ae19af49SJung-uk Kim 		if (isspace(p[i]) || p[i] == '\0')
4248ae19af49SJung-uk Kim 			i++;
4249ae19af49SJung-uk Kim 		else {
4250ae19af49SJung-uk Kim 			iface->data[j] = p + i;
4251ae19af49SJung-uk Kim 			i += strlen(p + i) + 1;
4252ae19af49SJung-uk Kim 			j++;
4253ae19af49SJung-uk Kim 		}
4254ae19af49SJung-uk Kim 
4255ae19af49SJung-uk Kim 	return (j);
4256ae19af49SJung-uk Kim }
4257ae19af49SJung-uk Kim 
4258ae19af49SJung-uk Kim static void
4259ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface)
4260ae19af49SJung-uk Kim {
4261ae19af49SJung-uk Kim 
4262ae19af49SJung-uk Kim 	free(iface->data[0], M_TEMP);
4263ae19af49SJung-uk Kim 	free(iface->data, M_TEMP);
4264ae19af49SJung-uk Kim }
4265ae19af49SJung-uk Kim 
4266ae19af49SJung-uk Kim static void
4267ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev)
4268ae19af49SJung-uk Kim {
4269ae19af49SJung-uk Kim 	struct acpi_interface list;
4270ae19af49SJung-uk Kim 	ACPI_STATUS status;
4271ae19af49SJung-uk Kim 	int i;
4272ae19af49SJung-uk Kim 
4273ae19af49SJung-uk Kim 	if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
4274ae19af49SJung-uk Kim 		for (i = 0; i < list.num; i++) {
4275ae19af49SJung-uk Kim 			status = AcpiInstallInterface(list.data[i]);
4276ae19af49SJung-uk Kim 			if (ACPI_FAILURE(status))
4277ae19af49SJung-uk Kim 				device_printf(dev,
4278ae19af49SJung-uk Kim 				    "failed to install _OSI(\"%s\"): %s\n",
4279ae19af49SJung-uk Kim 				    list.data[i], AcpiFormatException(status));
4280ae19af49SJung-uk Kim 			else if (bootverbose)
4281ae19af49SJung-uk Kim 				device_printf(dev, "installed _OSI(\"%s\")\n",
4282ae19af49SJung-uk Kim 				    list.data[i]);
4283ae19af49SJung-uk Kim 		}
4284ae19af49SJung-uk Kim 		acpi_free_interfaces(&list);
4285ae19af49SJung-uk Kim 	}
4286ae19af49SJung-uk Kim 	if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
4287ae19af49SJung-uk Kim 		for (i = 0; i < list.num; i++) {
4288ae19af49SJung-uk Kim 			status = AcpiRemoveInterface(list.data[i]);
4289ae19af49SJung-uk Kim 			if (ACPI_FAILURE(status))
4290ae19af49SJung-uk Kim 				device_printf(dev,
4291ae19af49SJung-uk Kim 				    "failed to remove _OSI(\"%s\"): %s\n",
4292ae19af49SJung-uk Kim 				    list.data[i], AcpiFormatException(status));
4293ae19af49SJung-uk Kim 			else if (bootverbose)
4294ae19af49SJung-uk Kim 				device_printf(dev, "removed _OSI(\"%s\")\n",
4295ae19af49SJung-uk Kim 				    list.data[i]);
4296ae19af49SJung-uk Kim 		}
4297ae19af49SJung-uk Kim 		acpi_free_interfaces(&list);
4298ae19af49SJung-uk Kim 	}
4299ae19af49SJung-uk Kim }
4300ae19af49SJung-uk Kim 
4301ae19af49SJung-uk Kim static int
4302f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
4303f9390180SMitsuru IWASAKI {
4304f9390180SMitsuru IWASAKI 	int	state, acpi_state;
4305f9390180SMitsuru IWASAKI 	int	error;
4306f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
4307f9390180SMitsuru IWASAKI 	va_list	ap;
4308f9390180SMitsuru IWASAKI 
4309f9390180SMitsuru IWASAKI 	error = 0;
4310f9390180SMitsuru IWASAKI 	switch (cmd) {
4311f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
4312f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
4313f9390180SMitsuru IWASAKI 		if (sc == NULL) {
4314f9390180SMitsuru IWASAKI 			error = EINVAL;
4315f9390180SMitsuru IWASAKI 			goto out;
4316f9390180SMitsuru IWASAKI 		}
4317f9390180SMitsuru IWASAKI 
4318f9390180SMitsuru IWASAKI 		va_start(ap, arg);
4319f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
4320f9390180SMitsuru IWASAKI 		va_end(ap);
4321f9390180SMitsuru IWASAKI 
4322f9390180SMitsuru IWASAKI 		switch (state) {
4323f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
4324f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
4325f9390180SMitsuru IWASAKI 			break;
4326f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
4327f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
4328f9390180SMitsuru IWASAKI 			break;
4329f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
4330f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
4331f9390180SMitsuru IWASAKI 			break;
4332f9390180SMitsuru IWASAKI 		default:
4333f9390180SMitsuru IWASAKI 			error = EINVAL;
4334f9390180SMitsuru IWASAKI 			goto out;
4335f9390180SMitsuru IWASAKI 		}
4336f9390180SMitsuru IWASAKI 
433700a30448SNate Lawson 		if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
433800a30448SNate Lawson 			error = ENXIO;
4339f9390180SMitsuru IWASAKI 		break;
4340f9390180SMitsuru IWASAKI 	default:
4341f9390180SMitsuru IWASAKI 		error = EINVAL;
4342f9390180SMitsuru IWASAKI 		goto out;
4343f9390180SMitsuru IWASAKI 	}
4344f9390180SMitsuru IWASAKI 
4345f9390180SMitsuru IWASAKI out:
4346f9390180SMitsuru IWASAKI 	return (error);
4347f9390180SMitsuru IWASAKI }
4348f9390180SMitsuru IWASAKI 
4349f9390180SMitsuru IWASAKI static void
4350f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
4351f9390180SMitsuru IWASAKI {
4352be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
43536c407052SMitsuru IWASAKI 	return;
4354f9390180SMitsuru IWASAKI 
4355f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
4356f9390180SMitsuru IWASAKI }
4357f9390180SMitsuru IWASAKI 
4358891cf3edSEd Maste SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, NULL);
4359