xref: /freebsd/sys/dev/acpica/acpi.c (revision f0a101b7e202b96da01094320a0ae95e13f3356f)
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"
3415e32d5dSMike Smith #include <sys/param.h>
3515e32d5dSMike Smith #include <sys/kernel.h>
36fb919e4dSMark Murray #include <sys/proc.h>
37a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3815e32d5dSMike Smith #include <sys/malloc.h>
39fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4015e32d5dSMike Smith #include <sys/bus.h>
4115e32d5dSMike Smith #include <sys/conf.h>
4215e32d5dSMike Smith #include <sys/ioccom.h>
4315e32d5dSMike Smith #include <sys/reboot.h>
4415e32d5dSMike Smith #include <sys/sysctl.h>
4515e32d5dSMike Smith #include <sys/ctype.h>
461611ea87SMitsuru IWASAKI #include <sys/linker.h>
47f9390180SMitsuru IWASAKI #include <sys/power.h>
4854f6bca0SNate Lawson #include <sys/sbuf.h>
49c66d2b38SJung-uk Kim #include <sys/sched.h>
50eeb3a05fSNate Lawson #include <sys/smp.h>
51c66d2b38SJung-uk Kim #include <sys/timetc.h>
5215e32d5dSMike Smith 
53d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
54d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h>
55d320e05cSJohn Baldwin #endif
5615e32d5dSMike Smith #include <machine/resource.h>
57dc750869SNate Lawson #include <machine/bus.h>
58dc750869SNate Lawson #include <sys/rman.h>
59bc0f2195SMike Smith #include <isa/isavar.h>
60c796e27bSNate Lawson #include <isa/pnpvar.h>
61bc0f2195SMike Smith 
62129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
63129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
64129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
65129d3046SJung-uk Kim 
6615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
6715e32d5dSMike Smith #include <dev/acpica/acpiio.h>
6815e32d5dSMike Smith 
692be4e471SJung-uk Kim #include <vm/vm_param.h>
702be4e471SJung-uk Kim 
71d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
7215e32d5dSMike Smith 
73be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
74cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
75b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
760ae55423SMike Smith 
7715e32d5dSMike Smith static d_open_t		acpiopen;
7815e32d5dSMike Smith static d_close_t	acpiclose;
7915e32d5dSMike Smith static d_ioctl_t	acpiioctl;
8015e32d5dSMike Smith 
8115e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
82dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
837ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
847ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
857ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
867ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
8715e32d5dSMike Smith };
8815e32d5dSMike Smith 
89ae19af49SJung-uk Kim struct acpi_interface {
90ae19af49SJung-uk Kim 	ACPI_STRING	*data;
91ae19af49SJung-uk Kim 	int		num;
92ae19af49SJung-uk Kim };
93ae19af49SJung-uk Kim 
94346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
95cb9b0d80SMike Smith struct mtx	acpi_mutex;
96cb9b0d80SMike Smith 
9789fb5af8SNate Lawson /* Bitmap of device quirks. */
9889fb5af8SNate Lawson int		acpi_quirks;
9989fb5af8SNate Lawson 
1004c52cad2SJung-uk Kim /* Optional ACPI methods for suspend and resume, e.g., _GTS and _BFS. */
1014c52cad2SJung-uk Kim int		acpi_sleep_flags;
1024c52cad2SJung-uk Kim 
103a0e73a12SJung-uk Kim /* Supported sleep states. */
104a0e73a12SJung-uk Kim static BOOLEAN	acpi_sleep_states[ACPI_S_STATE_COUNT];
105a0e73a12SJung-uk Kim 
1066d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10715e32d5dSMike Smith static int	acpi_probe(device_t dev);
10815e32d5dSMike Smith static int	acpi_attach(device_t dev);
10910ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
11010ce62b9SNate Lawson static int	acpi_resume(device_t dev);
111169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
1123d844eddSAndriy Gapon static device_t	acpi_add_child(device_t bus, u_int order, const char *name,
113be2b1797SNate Lawson 			int unit);
11415e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
11510ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
11610ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
117be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
118be2b1797SNate Lawson 			uintptr_t *result);
119be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
120be2b1797SNate Lawson 			uintptr_t value);
12191233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
122ea233199SJohn Baldwin static void	acpi_reserve_resources(device_t dev);
123adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
124ea233199SJohn Baldwin static int	acpi_set_resource(device_t dev, device_t child, int type,
125ea233199SJohn Baldwin 			int rid, u_long start, u_long count);
126be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
127be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
128be2b1797SNate Lawson 			u_long count, u_int flags);
129049dc0d1SJohn Baldwin static int	acpi_adjust_resource(device_t bus, device_t child, int type,
130049dc0d1SJohn Baldwin 			struct resource *r, u_long start, u_long end);
131be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
132be2b1797SNate Lawson 			int rid, struct resource *r);
1338b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1348b28b622SNate Lawson 		    int rid);
1351e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1361e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
137ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
138ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
139ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
140ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
141df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
142df8d2a32SNate Lawson 		    void *context, void **retval);
143df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
144df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
145a7a3177fSJung-uk Kim static int	acpi_set_powerstate(device_t child, int state);
146be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
147be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
14815e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
149d227204dSJohn Baldwin static void	acpi_probe_order(ACPI_HANDLE handle, int *order);
150be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
151be2b1797SNate Lawson 		    void *context, void **status);
152a0e73a12SJung-uk Kim static void	acpi_sleep_enable(void *arg);
153a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
15400a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
15515e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
15613d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
157183c8af3SJohn Baldwin static BOOLEAN	acpi_has_hid(ACPI_HANDLE handle);
158a0a15716SJung-uk Kim static void	acpi_resync_clock(struct acpi_softc *sc);
159d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
160d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
161d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
16288a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
16388a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
16415e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
16515e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
166a0e73a12SJung-uk Kim static int	acpi_sname2sstate(const char *sname);
167a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate);
168d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1691d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1702a18c71dSJung-uk Kim static int	acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
171f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
172879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
173879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
174879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
175879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
176d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
177d320e05cSJohn Baldwin static void	acpi_enable_pcie(void);
178d320e05cSJohn Baldwin #endif
1790d484d24SJohn Baldwin static void	acpi_hint_device_unit(device_t acdev, device_t child,
1800d484d24SJohn Baldwin 		    const char *name, int *unitp);
181ae19af49SJung-uk Kim static void	acpi_reset_interfaces(device_t dev);
182879d6c23STakanori Watanabe 
18315e32d5dSMike Smith static device_method_t acpi_methods[] = {
18415e32d5dSMike Smith     /* Device interface */
18515e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
18615e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
187169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
18856a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
18910ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
19010ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
19115e32d5dSMike Smith 
19215e32d5dSMike Smith     /* Bus interface */
19315e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
19415e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
19510ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
19610ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
19715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
19815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
19991233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
200ea233199SJohn Baldwin     DEVMETHOD(bus_set_resource,		acpi_set_resource),
20191233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
20215e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
203049dc0d1SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	acpi_adjust_resource),
20415e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
2058b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
206879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
207879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
20815e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
20915e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
21015e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
21115e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
2120d484d24SJohn Baldwin     DEVMETHOD(bus_hint_device_unit,	acpi_hint_device_unit),
21315e32d5dSMike Smith 
214ce619b2eSNate Lawson     /* ACPI bus */
215ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
216ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
21710ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
218df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
219ce619b2eSNate Lawson 
220bc0f2195SMike Smith     /* ISA emulation */
221bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
222bc0f2195SMike Smith 
22315e32d5dSMike Smith     {0, 0}
22415e32d5dSMike Smith };
22515e32d5dSMike Smith 
22615e32d5dSMike Smith static driver_t acpi_driver = {
22715e32d5dSMike Smith     "acpi",
22815e32d5dSMike Smith     acpi_methods,
22915e32d5dSMike Smith     sizeof(struct acpi_softc),
23015e32d5dSMike Smith };
23115e32d5dSMike Smith 
2323273b005SMike Smith static devclass_t acpi_devclass;
2336d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
234a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
23515e32d5dSMike Smith 
23615e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
23715e2f34fSNate Lawson 
238adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
239adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
240adad4744SNate Lawson 
241bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
242bee4aa4aSNate Lawson 
2435217af30SJohn Baldwin /* Holds the description of the acpi0 device. */
2445217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
2455217af30SJohn Baldwin 
246197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
2471d7b121cSNate Lawson static char acpi_ca_version[12];
2481d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2491d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
25015e32d5dSMike Smith 
25115e32d5dSMike Smith /*
252ae19af49SJung-uk Kim  * Allow overriding _OSI methods.
253ae19af49SJung-uk Kim  */
254ae19af49SJung-uk Kim static char acpi_install_interface[256];
255ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
256ae19af49SJung-uk Kim     sizeof(acpi_install_interface));
257ae19af49SJung-uk Kim static char acpi_remove_interface[256];
258ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
259ae19af49SJung-uk Kim     sizeof(acpi_remove_interface));
260ae19af49SJung-uk Kim 
261ae19af49SJung-uk Kim /*
262413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
263c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
264c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
265c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
266c9b8d77dSNate Lawson  * some IBMs use such code.
267413081d7SNate Lawson  */
268c9b8d77dSNate Lawson static int acpi_serialize_methods;
269413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
270413081d7SNate Lawson 
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,
2752a18c71dSJung-uk Kim     CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I",
2762a18c71dSJung-uk Kim     "Enable Debug objects");
2772a18c71dSJung-uk Kim 
2782a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */
2792a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1;
2802a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
2812a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
2822a18c71dSJung-uk Kim     &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
2832a18c71dSJung-uk Kim 
284a0a15716SJung-uk Kim #ifdef __amd64__
2850755473bSJung-uk Kim /* Reset system clock while resuming.  XXX Remove once tested. */
2860755473bSJung-uk Kim static int acpi_reset_clock = 1;
2870755473bSJung-uk Kim TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock);
2880755473bSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW,
2890755473bSJung-uk Kim     &acpi_reset_clock, 1, "Reset system clock while resuming.");
290a0a15716SJung-uk Kim #endif
2910755473bSJung-uk Kim 
29239da3eb3SNate Lawson /* Allow users to override quirks. */
29339da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
29439da3eb3SNate Lawson 
2954c52cad2SJung-uk Kim /* Execute optional ACPI methods for suspend and resume. */
2964c52cad2SJung-uk Kim TUNABLE_INT("debug.acpi.sleep_flags", &acpi_sleep_flags);
2974c52cad2SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, sleep_flags, CTLFLAG_RW | CTLFLAG_TUN,
2984c52cad2SJung-uk Kim     &acpi_sleep_flags, 0, "Execute optional ACPI methods for suspend/resume.");
2994c52cad2SJung-uk Kim 
30093bfd059SNate Lawson static int acpi_susp_bounce;
30193bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
30293bfd059SNate Lawson     &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
30393bfd059SNate Lawson 
304c9b8d77dSNate Lawson /*
3056d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
3066d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
3078c0879b6SJohn Baldwin  * It also cannot be unloaded, since the entire system bus hierarchy hangs
308be2b1797SNate Lawson  * off it.
3096d63101aSMike Smith  */
3106d63101aSMike Smith static int
3116d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
3126d63101aSMike Smith {
3136d63101aSMike Smith     switch (event) {
3146d63101aSMike Smith     case MOD_LOAD:
31587b45ed5SMitsuru IWASAKI 	if (!cold) {
31687b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
3176d63101aSMike Smith 	    return (EPERM);
31887b45ed5SMitsuru IWASAKI 	}
3196d63101aSMike Smith 	break;
3206d63101aSMike Smith     case MOD_UNLOAD:
321f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
3226d63101aSMike Smith 	    return (EBUSY);
323f9390180SMitsuru IWASAKI 	break;
3246d63101aSMike Smith     default:
3256d63101aSMike Smith 	break;
3266d63101aSMike Smith     }
3276d63101aSMike Smith     return (0);
3286d63101aSMike Smith }
3296d63101aSMike Smith 
3306d63101aSMike Smith /*
331bbc2815cSJohn Baldwin  * Perform early initialization.
33215e32d5dSMike Smith  */
333bbc2815cSJohn Baldwin ACPI_STATUS
334bbc2815cSJohn Baldwin acpi_Startup(void)
33515e32d5dSMike Smith {
33689fb5af8SNate Lawson     static int started = 0;
3372be4e471SJung-uk Kim     ACPI_STATUS status;
3382be4e471SJung-uk Kim     int val;
33915e32d5dSMike Smith 
3401b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3411b8c233dSPeter Pentchev 
342bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
343bbc2815cSJohn Baldwin     if (started)
3442be4e471SJung-uk Kim 	return_VALUE (AE_OK);
345bbc2815cSJohn Baldwin     started = 1;
34615e32d5dSMike Smith 
347413081d7SNate Lawson     /*
3482be4e471SJung-uk Kim      * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
3492be4e471SJung-uk Kim      * if more tables exist.
350413081d7SNate Lawson      */
3512be4e471SJung-uk Kim     if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
3522be4e471SJung-uk Kim 	printf("ACPI: Table initialisation failed: %s\n",
3532be4e471SJung-uk Kim 	    AcpiFormatException(status));
3542be4e471SJung-uk Kim 	return_VALUE (status);
35515e32d5dSMike Smith     }
3563184cf5aSNate Lawson 
35789fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
3582be4e471SJung-uk Kim     if (acpi_quirks == ACPI_Q_OK)
35989fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
36089fb5af8SNate Lawson 
36139da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
36289fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
36389fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
36489fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
36589fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
3662be4e471SJung-uk Kim 	status = AE_SUPPORT;
36789fb5af8SNate Lawson     }
3683184cf5aSNate Lawson 
3692be4e471SJung-uk Kim     return_VALUE (status);
370bbc2815cSJohn Baldwin }
371bbc2815cSJohn Baldwin 
372bbc2815cSJohn Baldwin /*
3735217af30SJohn Baldwin  * Detect ACPI and perform early initialisation.
374bbc2815cSJohn Baldwin  */
3755217af30SJohn Baldwin int
3765217af30SJohn Baldwin acpi_identify(void)
377bbc2815cSJohn Baldwin {
3785217af30SJohn Baldwin     ACPI_TABLE_RSDP	*rsdp;
3795217af30SJohn Baldwin     ACPI_TABLE_HEADER	*rsdt;
3805217af30SJohn Baldwin     ACPI_PHYSICAL_ADDRESS paddr;
3815217af30SJohn Baldwin     struct sbuf		sb;
382bbc2815cSJohn Baldwin 
383bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
384bbc2815cSJohn Baldwin 
385bbc2815cSJohn Baldwin     if (!cold)
3865217af30SJohn Baldwin 	return (ENXIO);
387bbc2815cSJohn Baldwin 
388bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
389bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
3905217af30SJohn Baldwin 	return (ENXIO);
391bbc2815cSJohn Baldwin 
3925217af30SJohn Baldwin     /* Check for other PM systems. */
3935217af30SJohn Baldwin     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
3945217af30SJohn Baldwin 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
3955217af30SJohn Baldwin 	printf("ACPI identify failed, other PM system enabled.\n");
3965217af30SJohn Baldwin 	return (ENXIO);
3975217af30SJohn Baldwin     }
3984b1ff978SMark Santcroos 
3992be4e471SJung-uk Kim     /* Initialize root tables. */
4002be4e471SJung-uk Kim     if (ACPI_FAILURE(acpi_Startup())) {
4012be4e471SJung-uk Kim 	printf("ACPI: Try disabling either ACPI or apic support.\n");
4025217af30SJohn Baldwin 	return (ENXIO);
4032be4e471SJung-uk Kim     }
40415e32d5dSMike Smith 
4055217af30SJohn Baldwin     if ((paddr = AcpiOsGetRootPointer()) == 0 ||
4065217af30SJohn Baldwin 	(rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
4075217af30SJohn Baldwin 	return (ENXIO);
4085217af30SJohn Baldwin     if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
4095217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
4105217af30SJohn Baldwin     else
4115217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
4125217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
4135217af30SJohn Baldwin 
4145217af30SJohn Baldwin     if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
4155217af30SJohn Baldwin 	return (ENXIO);
4165217af30SJohn Baldwin     sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
4175217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
4185217af30SJohn Baldwin     sbuf_trim(&sb);
4195217af30SJohn Baldwin     sbuf_putc(&sb, ' ');
4205217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
4215217af30SJohn Baldwin     sbuf_trim(&sb);
4225217af30SJohn Baldwin     sbuf_finish(&sb);
4235217af30SJohn Baldwin     sbuf_delete(&sb);
4245217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
4255217af30SJohn Baldwin 
4265217af30SJohn Baldwin     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
4275217af30SJohn Baldwin 
4285217af30SJohn Baldwin     return (0);
42915e32d5dSMike Smith }
43015e32d5dSMike Smith 
43115e32d5dSMike Smith /*
432bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
43315e32d5dSMike Smith  */
43415e32d5dSMike Smith static int
43515e32d5dSMike Smith acpi_probe(device_t dev)
43615e32d5dSMike Smith {
43715e32d5dSMike Smith 
438b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
439f9390180SMitsuru IWASAKI 
4405217af30SJohn Baldwin     device_set_desc(dev, acpi_desc);
441bee4aa4aSNate Lawson 
4422be4e471SJung-uk Kim     return_VALUE (0);
44315e32d5dSMike Smith }
44415e32d5dSMike Smith 
44515e32d5dSMike Smith static int
44615e32d5dSMike Smith acpi_attach(device_t dev)
44715e32d5dSMike Smith {
44815e32d5dSMike Smith     struct acpi_softc	*sc;
449cb9b0d80SMike Smith     ACPI_STATUS		status;
450ea27c63eSNate Lawson     int			error, state;
45132d18aa5SMike Smith     UINT32		flags;
452ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
453498d464fSMitsuru IWASAKI     char		*env;
45415e32d5dSMike Smith 
455b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
456bee4aa4aSNate Lawson 
45715e32d5dSMike Smith     sc = device_get_softc(dev);
45815e32d5dSMike Smith     sc->acpi_dev = dev;
45900a30448SNate Lawson     callout_init(&sc->susp_force_to, TRUE);
46015e32d5dSMike Smith 
4612be4e471SJung-uk Kim     error = ENXIO;
4622be4e471SJung-uk Kim 
46391233413SNate Lawson     /* Initialize resource manager. */
46491233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
46591233413SNate Lawson     acpi_rman_io.rm_start = 0;
46691233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
4670bba6acfSJohn Baldwin     acpi_rman_io.rm_descr = "ACPI I/O ports";
46891233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
46991233413SNate Lawson 	panic("acpi rman_init IO ports failed");
47091233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
47191233413SNate Lawson     acpi_rman_mem.rm_start = 0;
47291233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
4730bba6acfSJohn Baldwin     acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
47491233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
47591233413SNate Lawson 	panic("acpi rman_init memory failed");
47691233413SNate Lawson 
4772be4e471SJung-uk Kim     /* Initialise the ACPI mutex */
4782be4e471SJung-uk Kim     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
4792be4e471SJung-uk Kim 
4802be4e471SJung-uk Kim     /*
4812be4e471SJung-uk Kim      * Set the globals from our tunables.  This is needed because ACPI-CA
4822be4e471SJung-uk Kim      * uses UINT8 for some values and we have no tunable_byte.
4832be4e471SJung-uk Kim      */
4842a18c71dSJung-uk Kim     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods ? TRUE : FALSE;
4852a18c71dSJung-uk Kim     AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
4862a18c71dSJung-uk Kim     AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
4872a18c71dSJung-uk Kim 
4882a18c71dSJung-uk Kim #ifndef ACPI_DEBUG
4892a18c71dSJung-uk Kim     /*
4902a18c71dSJung-uk Kim      * Disable all debugging layers and levels.
4912a18c71dSJung-uk Kim      */
4922a18c71dSJung-uk Kim     AcpiDbgLayer = 0;
4932a18c71dSJung-uk Kim     AcpiDbgLevel = 0;
4942a18c71dSJung-uk Kim #endif
4952be4e471SJung-uk Kim 
4962be4e471SJung-uk Kim     /* Start up the ACPI CA subsystem. */
4972be4e471SJung-uk Kim     status = AcpiInitializeSubsystem();
4982be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4992be4e471SJung-uk Kim 	device_printf(dev, "Could not initialize Subsystem: %s\n",
5002be4e471SJung-uk Kim 		      AcpiFormatException(status));
5012be4e471SJung-uk Kim 	goto out;
5022be4e471SJung-uk Kim     }
5032be4e471SJung-uk Kim 
504ae19af49SJung-uk Kim     /* Override OS interfaces if the user requested. */
505ae19af49SJung-uk Kim     acpi_reset_interfaces(dev);
506ae19af49SJung-uk Kim 
5072be4e471SJung-uk Kim     /* Load ACPI name space. */
5082be4e471SJung-uk Kim     status = AcpiLoadTables();
5092be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
5102be4e471SJung-uk Kim 	device_printf(dev, "Could not load Namespace: %s\n",
5112be4e471SJung-uk Kim 		      AcpiFormatException(status));
5122be4e471SJung-uk Kim 	goto out;
5132be4e471SJung-uk Kim     }
5142be4e471SJung-uk Kim 
515d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
516d320e05cSJohn Baldwin     /* Handle MCFG table if present. */
517d320e05cSJohn Baldwin     acpi_enable_pcie();
518d320e05cSJohn Baldwin #endif
519d320e05cSJohn Baldwin 
52015e32d5dSMike Smith     /*
521be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
522be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
523be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
524be2b1797SNate Lawson      * object init pass.
52515e32d5dSMike Smith      *
52632d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
52732d18aa5SMike Smith      *
528be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
529be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
53015e32d5dSMike Smith      */
53132d18aa5SMike Smith     flags = 0;
532d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
53332d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
534bee4aa4aSNate Lawson 
535bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
536b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
537be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
538be2b1797SNate Lawson 		      AcpiFormatException(status));
539cb9b0d80SMike Smith 	goto out;
54015e32d5dSMike Smith     }
54115e32d5dSMike Smith 
542f8335e3aSNate Lawson     /*
543f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
544f8335e3aSNate Lawson      * the namespace has been evaluated.
545da72d149SNate Lawson      *
546da72d149SNate Lawson      * XXX This happens before the sysresource devices have been probed and
547da72d149SNate Lawson      * attached so its resources come from nexus0.  In practice, this isn't
548da72d149SNate Lawson      * a problem but should be addressed eventually.
549f8335e3aSNate Lawson      */
550f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
551f8335e3aSNate Lawson 
552bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
553b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
554be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
555be2b1797SNate Lawson 		      AcpiFormatException(status));
556b69ed3f4SMitsuru IWASAKI 	goto out;
557b69ed3f4SMitsuru IWASAKI     }
558b69ed3f4SMitsuru IWASAKI 
55915e32d5dSMike Smith     /*
5601d073b1dSJohn Baldwin      * Setup our sysctl tree.
5611d073b1dSJohn Baldwin      *
5621d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
5631d073b1dSJohn Baldwin      */
5641d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
5651d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
5661d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
5671d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
5681d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
569d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
570d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
571d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5721d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5731d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5741d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5751d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5761d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5771d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5781d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
5791d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
580f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
581f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
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),
584f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
585f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5862d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
587197b4dccSNate Lawson 	OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
58831f301d0SChristian Brueffer 	"sleep delay in seconds");
589ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
590197b4dccSNate Lawson 	OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
5911611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
592197b4dccSNate Lawson 	OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
593d85e6785SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
594d85e6785SNate Lawson 	OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
595d85e6785SNate Lawson 	&sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
596d1b16e18SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
597d1b16e18SNate Lawson 	OID_AUTO, "handle_reboot", CTLFLAG_RW,
598d1b16e18SNate Lawson 	&sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
599bf0c18ecSNate Lawson 
600bf0c18ecSNate Lawson     /*
6012b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
602bf0c18ecSNate Lawson      * stabilize.
603bf0c18ecSNate Lawson      */
6042b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
6052d644607SMitsuru IWASAKI     if (bootverbose)
6062d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
607965a34fbSNate Lawson     if ((env = getenv("hw.acpi.verbose")) != NULL) {
608965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
609498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
610498d464fSMitsuru IWASAKI 	freeenv(env);
611498d464fSMitsuru IWASAKI     }
6121d073b1dSJohn Baldwin 
613ac731af5SJung-uk Kim     /* Only enable reboot by default if the FADT says it is available. */
614ac731af5SJung-uk Kim     if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
615ac731af5SJung-uk Kim 	sc->acpi_handle_reboot = 1;
616ac731af5SJung-uk Kim 
6172d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
618129d3046SJung-uk Kim     if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
6192d610c46SNate Lawson 	sc->acpi_s4bios = 1;
6202d610c46SNate Lawson 
621a0e73a12SJung-uk Kim     /* Probe all supported sleep states. */
622a0e73a12SJung-uk Kim     acpi_sleep_states[ACPI_STATE_S0] = TRUE;
623a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
624a0e73a12SJung-uk Kim 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
625a0e73a12SJung-uk Kim 	    acpi_sleep_states[state] = TRUE;
626a0e73a12SJung-uk Kim 
6271d073b1dSJohn Baldwin     /*
628ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
629a0e73a12SJung-uk Kim      * to UNKNOWN by default to avoid surprising users.
63015e32d5dSMike Smith      */
631a0e73a12SJung-uk Kim     sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
632a0e73a12SJung-uk Kim 	ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
633a0e73a12SJung-uk Kim     sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
634a0e73a12SJung-uk Kim     sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
635a0e73a12SJung-uk Kim 	ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
636a0e73a12SJung-uk Kim     sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
637a0e73a12SJung-uk Kim 	ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
63815e32d5dSMike Smith 
639ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
640a0e73a12SJung-uk Kim     sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
64100a30448SNate Lawson     for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
642a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state]) {
643ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
644ea27c63eSNate Lawson 	    break;
645ea27c63eSNate Lawson 	}
646ea27c63eSNate Lawson 
64713d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
64815e32d5dSMike Smith 
64915e32d5dSMike Smith     /*
65015e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
65115e32d5dSMike Smith      */
65215e32d5dSMike Smith 
65359e472e9SNate Lawson     /* Register our shutdown handler. */
654be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
655be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
65615e32d5dSMike Smith 
65715e32d5dSMike Smith     /*
65815e32d5dSMike Smith      * Register our acpi event handlers.
65915e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
66015e32d5dSMike Smith      */
661be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
662be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
663be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
664be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
66515e32d5dSMike Smith 
666be2b1797SNate Lawson     /* Flag our initial states. */
667a0e73a12SJung-uk Kim     sc->acpi_enabled = TRUE;
66815e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
669a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
67015e32d5dSMike Smith 
671be2b1797SNate Lawson     /* Create the control device */
672a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
6732a4689e8SRobert Watson 			      "acpi");
67415e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
67515e32d5dSMike Smith 
676be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
677f86214b6SMitsuru IWASAKI 	goto out;
678f86214b6SMitsuru IWASAKI 
679f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
680f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
681f9390180SMitsuru IWASAKI 
682eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
683eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
684eeb6dba2SJohn Baldwin 
6855a77b11bSJung-uk Kim     /* Update all GPEs and enable runtime GPEs. */
6865a77b11bSJung-uk Kim     status = AcpiUpdateAllGpes();
6875a77b11bSJung-uk Kim     if (ACPI_FAILURE(status))
6885a77b11bSJung-uk Kim 	device_printf(dev, "Could not update all GPEs: %s\n",
6895a77b11bSJung-uk Kim 	    AcpiFormatException(status));
6905a77b11bSJung-uk Kim 
691a0e73a12SJung-uk Kim     /* Allow sleep request after a while. */
692a0e73a12SJung-uk Kim     timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
693a0e73a12SJung-uk Kim 
694cb9b0d80SMike Smith     error = 0;
695cb9b0d80SMike Smith 
696cb9b0d80SMike Smith  out:
697cb9b0d80SMike Smith     return_VALUE (error);
69815e32d5dSMike Smith }
69915e32d5dSMike Smith 
700a7a3177fSJung-uk Kim static void
701a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state)
702a7a3177fSJung-uk Kim {
703a7a3177fSJung-uk Kim 	device_t child, parent;
704a7a3177fSJung-uk Kim 	device_t *devlist;
705a7a3177fSJung-uk Kim 	struct pci_devinfo *dinfo;
706a7a3177fSJung-uk Kim 	int dstate, i, numdevs;
707a7a3177fSJung-uk Kim 
708a7a3177fSJung-uk Kim 	if (device_get_children(dev, &devlist, &numdevs) != 0)
709a7a3177fSJung-uk Kim 		return;
710a7a3177fSJung-uk Kim 
711a7a3177fSJung-uk Kim 	/*
712a7a3177fSJung-uk Kim 	 * Retrieve and set D-state for the sleep state if _SxD is present.
713a7a3177fSJung-uk Kim 	 * Skip children who aren't attached since they are handled separately.
714a7a3177fSJung-uk Kim 	 */
715a7a3177fSJung-uk Kim 	parent = device_get_parent(dev);
716a7a3177fSJung-uk Kim 	for (i = 0; i < numdevs; i++) {
717a7a3177fSJung-uk Kim 		child = devlist[i];
718a7a3177fSJung-uk Kim 		dinfo = device_get_ivars(child);
719a7a3177fSJung-uk Kim 		dstate = state;
720a7a3177fSJung-uk Kim 		if (device_is_attached(child) &&
721a7a3177fSJung-uk Kim 		    acpi_device_pwr_for_sleep(parent, dev, &dstate) == 0)
722a7a3177fSJung-uk Kim 			acpi_set_powerstate(child, dstate);
723a7a3177fSJung-uk Kim 	}
724a7a3177fSJung-uk Kim 	free(devlist, M_TEMP);
725a7a3177fSJung-uk Kim }
726a7a3177fSJung-uk Kim 
727169b539aSNate Lawson static int
72810ce62b9SNate Lawson acpi_suspend(device_t dev)
72910ce62b9SNate Lawson {
730a7a3177fSJung-uk Kim     int error;
73110ce62b9SNate Lawson 
732a56fe095SJohn Baldwin     GIANT_REQUIRED;
733a56fe095SJohn Baldwin 
73410ce62b9SNate Lawson     error = bus_generic_suspend(dev);
735a7a3177fSJung-uk Kim     if (error == 0)
736a7a3177fSJung-uk Kim 	acpi_set_power_children(dev, ACPI_STATE_D3);
73710ce62b9SNate Lawson 
73810ce62b9SNate Lawson     return (error);
73910ce62b9SNate Lawson }
74010ce62b9SNate Lawson 
74110ce62b9SNate Lawson static int
74210ce62b9SNate Lawson acpi_resume(device_t dev)
74310ce62b9SNate Lawson {
74410ce62b9SNate Lawson 
745a56fe095SJohn Baldwin     GIANT_REQUIRED;
746a56fe095SJohn Baldwin 
747a7a3177fSJung-uk Kim     acpi_set_power_children(dev, ACPI_STATE_D0);
74810ce62b9SNate Lawson 
74910ce62b9SNate Lawson     return (bus_generic_resume(dev));
75010ce62b9SNate Lawson }
75110ce62b9SNate Lawson 
75210ce62b9SNate Lawson static int
753169b539aSNate Lawson acpi_shutdown(device_t dev)
754169b539aSNate Lawson {
755169b539aSNate Lawson 
756a56fe095SJohn Baldwin     GIANT_REQUIRED;
757a56fe095SJohn Baldwin 
7580e414868SNate Lawson     /* Allow children to shutdown first. */
7590e414868SNate Lawson     bus_generic_shutdown(dev);
7600e414868SNate Lawson 
761bee4aa4aSNate Lawson     /*
762bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
763bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
764bee4aa4aSNate Lawson      */
765d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
766d4b9ff91SNate Lawson 
767169b539aSNate Lawson     return (0);
768169b539aSNate Lawson }
769169b539aSNate Lawson 
77015e32d5dSMike Smith /*
77115e32d5dSMike Smith  * Handle a new device being added
77215e32d5dSMike Smith  */
77315e32d5dSMike Smith static device_t
7743d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit)
77515e32d5dSMike Smith {
77615e32d5dSMike Smith     struct acpi_device	*ad;
77715e32d5dSMike Smith     device_t		child;
77815e32d5dSMike Smith 
779be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
78015e32d5dSMike Smith 	return (NULL);
78115e32d5dSMike Smith 
78215e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
78315e32d5dSMike Smith 
78415e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
78515e32d5dSMike Smith     if (child != NULL)
78615e32d5dSMike Smith 	device_set_ivars(child, ad);
78743ce1c77SNate Lawson     else
78843ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
78915e32d5dSMike Smith     return (child);
79015e32d5dSMike Smith }
79115e32d5dSMike Smith 
79215e32d5dSMike Smith static int
79315e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
79415e32d5dSMike Smith {
79515e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
79615e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
79715e32d5dSMike Smith     int retval = 0;
79815e32d5dSMike Smith 
79915e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
8009ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
8019ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
8029ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
8039ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
804a91c5fa8SNate Lawson     if (device_get_flags(child))
805a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
8062c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
80715e32d5dSMike Smith 
80815e32d5dSMike Smith     return (retval);
80915e32d5dSMike Smith }
81015e32d5dSMike Smith 
81110ce62b9SNate Lawson /*
81210ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
81310ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
81410ce62b9SNate Lawson  *
81510ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
81610ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
81710ce62b9SNate Lawson  * them to be powered up.
81810ce62b9SNate Lawson  */
81910ce62b9SNate Lawson static void
82010ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
82110ce62b9SNate Lawson {
82296cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
823a7a3177fSJung-uk Kim     acpi_set_powerstate(child, ACPI_STATE_D3);
82496cf0b6dSWarner Losh #endif
82510ce62b9SNate Lawson }
82610ce62b9SNate Lawson 
82710ce62b9SNate Lawson /*
82810ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
82910ce62b9SNate Lawson  *
83010ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
83110ce62b9SNate Lawson  */
83210ce62b9SNate Lawson static void
83310ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
83410ce62b9SNate Lawson {
83510ce62b9SNate Lawson     device_t child, *devlist;
83610ce62b9SNate Lawson     int i, numdevs;
83710ce62b9SNate Lawson 
83810ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
839099ea4b5SWarner Losh     if (device_get_children(dev, &devlist, &numdevs))
840099ea4b5SWarner Losh 	    return;
84110ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
84210ce62b9SNate Lawson 	child = devlist[i];
84310ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
84496cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
845a7a3177fSJung-uk Kim 	    acpi_set_powerstate(child, ACPI_STATE_D0);
84610ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
847a7a3177fSJung-uk Kim 		acpi_set_powerstate(child, ACPI_STATE_D3);
84896cf0b6dSWarner Losh #else
84996cf0b6dSWarner Losh 	    device_probe_and_attach(child);
85096cf0b6dSWarner Losh #endif
85110ce62b9SNate Lawson 	}
85210ce62b9SNate Lawson     }
85310ce62b9SNate Lawson     free(devlist, M_TEMP);
85410ce62b9SNate Lawson }
85510ce62b9SNate Lawson 
85663600cc3SNate Lawson /* Location hint for devctl(8) */
85763600cc3SNate Lawson static int
858247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
859247648afSTakanori Watanabe     size_t buflen)
860247648afSTakanori Watanabe {
861247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
862247648afSTakanori Watanabe 
863247648afSTakanori Watanabe     if (dinfo->ad_handle)
864d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
865247648afSTakanori Watanabe     else
866d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
867247648afSTakanori Watanabe     return (0);
868247648afSTakanori Watanabe }
869247648afSTakanori Watanabe 
87063600cc3SNate Lawson /* PnP information for devctl(8) */
87163600cc3SNate Lawson static int
872247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
873247648afSTakanori Watanabe     size_t buflen)
874247648afSTakanori Watanabe {
87563600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
87692488a57SJung-uk Kim     ACPI_DEVICE_INFO *adinfo;
877247648afSTakanori Watanabe 
87892488a57SJung-uk Kim     if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) {
879d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
88092488a57SJung-uk Kim 	return (0);
88192488a57SJung-uk Kim     }
88292488a57SJung-uk Kim 
883247648afSTakanori Watanabe     snprintf(buf, buflen, "_HID=%s _UID=%lu",
884247648afSTakanori Watanabe 	(adinfo->Valid & ACPI_VALID_HID) ?
88592488a57SJung-uk Kim 	adinfo->HardwareId.String : "none",
88663600cc3SNate Lawson 	(adinfo->Valid & ACPI_VALID_UID) ?
88792488a57SJung-uk Kim 	strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL);
888247648afSTakanori Watanabe     AcpiOsFree(adinfo);
889247648afSTakanori Watanabe 
890247648afSTakanori Watanabe     return (0);
891247648afSTakanori Watanabe }
892247648afSTakanori Watanabe 
893247648afSTakanori Watanabe /*
89415e32d5dSMike Smith  * Handle per-device ivars
89515e32d5dSMike Smith  */
89615e32d5dSMike Smith static int
89715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
89815e32d5dSMike Smith {
89915e32d5dSMike Smith     struct acpi_device	*ad;
90015e32d5dSMike Smith 
90115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9022d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
90315e32d5dSMike Smith 	return (ENOENT);
90415e32d5dSMike Smith     }
90515e32d5dSMike Smith 
906be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
90715e32d5dSMike Smith     switch(index) {
90815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
90915e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
91015e32d5dSMike Smith 	break;
91115e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
91215e32d5dSMike Smith 	*(void **)result = ad->ad_private;
91315e32d5dSMike Smith 	break;
914d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
915d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
916d4b9ff91SNate Lawson 	break;
91732d18aa5SMike Smith     case ISA_IVAR_VENDORID:
91832d18aa5SMike Smith     case ISA_IVAR_SERIAL:
91932d18aa5SMike Smith     case ISA_IVAR_COMPATID:
92032d18aa5SMike Smith 	*(int *)result = -1;
92132d18aa5SMike Smith 	break;
92232d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
92332d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
92432d18aa5SMike Smith 	break;
92515e32d5dSMike Smith     default:
92615e32d5dSMike Smith 	return (ENOENT);
92715e32d5dSMike Smith     }
928be2b1797SNate Lawson 
92915e32d5dSMike Smith     return (0);
93015e32d5dSMike Smith }
93115e32d5dSMike Smith 
93215e32d5dSMike Smith static int
93315e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
93415e32d5dSMike Smith {
93515e32d5dSMike Smith     struct acpi_device	*ad;
93615e32d5dSMike Smith 
93715e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9382d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
93915e32d5dSMike Smith 	return (ENOENT);
94015e32d5dSMike Smith     }
94115e32d5dSMike Smith 
94215e32d5dSMike Smith     switch(index) {
94315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
94415e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
94515e32d5dSMike Smith 	break;
94615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
94715e32d5dSMike Smith 	ad->ad_private = (void *)value;
94815e32d5dSMike Smith 	break;
949d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
950d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
951d4b9ff91SNate Lawson 	break;
95215e32d5dSMike Smith     default:
9532ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
95415e32d5dSMike Smith 	return (ENOENT);
95515e32d5dSMike Smith     }
956be2b1797SNate Lawson 
95715e32d5dSMike Smith     return (0);
95815e32d5dSMike Smith }
95915e32d5dSMike Smith 
96015e32d5dSMike Smith /*
96115e32d5dSMike Smith  * Handle child resource allocation/removal
96215e32d5dSMike Smith  */
96391233413SNate Lawson static struct resource_list *
96491233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
96515e32d5dSMike Smith {
96691233413SNate Lawson     struct acpi_device		*ad;
96715e32d5dSMike Smith 
96891233413SNate Lawson     ad = device_get_ivars(child);
96991233413SNate Lawson     return (&ad->ad_rl);
97015e32d5dSMike Smith }
97115e32d5dSMike Smith 
9720d484d24SJohn Baldwin static int
9730d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value)
9740d484d24SJohn Baldwin {
9750d484d24SJohn Baldwin     struct acpi_device *ad = device_get_ivars(dev);
9760d484d24SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
9770d484d24SJohn Baldwin     struct resource_list_entry *rle;
9780d484d24SJohn Baldwin 
9790d484d24SJohn Baldwin     STAILQ_FOREACH(rle, rl, link) {
9800d484d24SJohn Baldwin 	if (rle->type != type)
9810d484d24SJohn Baldwin 	    continue;
9820d484d24SJohn Baldwin 	if (rle->start <= value && rle->end >= value)
9830d484d24SJohn Baldwin 	    return (1);
9840d484d24SJohn Baldwin     }
9850d484d24SJohn Baldwin     return (0);
9860d484d24SJohn Baldwin }
9870d484d24SJohn Baldwin 
9880d484d24SJohn Baldwin /*
9890d484d24SJohn Baldwin  * Wire device unit numbers based on resource matches in hints.
9900d484d24SJohn Baldwin  */
9910d484d24SJohn Baldwin static void
9920d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
9930d484d24SJohn Baldwin     int *unitp)
9940d484d24SJohn Baldwin {
9950d484d24SJohn Baldwin     const char *s;
9960d484d24SJohn Baldwin     long value;
9970d484d24SJohn Baldwin     int line, matches, unit;
9980d484d24SJohn Baldwin 
9990d484d24SJohn Baldwin     /*
10000d484d24SJohn Baldwin      * Iterate over all the hints for the devices with the specified
10010d484d24SJohn Baldwin      * name to see if one's resources are a subset of this device.
10020d484d24SJohn Baldwin      */
10030d484d24SJohn Baldwin     line = 0;
10040d484d24SJohn Baldwin     for (;;) {
10050d484d24SJohn Baldwin 	if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
10060d484d24SJohn Baldwin 	    break;
10070d484d24SJohn Baldwin 
10080d484d24SJohn Baldwin 	/* Must have an "at" for acpi or isa. */
10090d484d24SJohn Baldwin 	resource_string_value(name, unit, "at", &s);
10100d484d24SJohn Baldwin 	if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
10110d484d24SJohn Baldwin 	    strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0))
10120d484d24SJohn Baldwin 	    continue;
10130d484d24SJohn Baldwin 
10140d484d24SJohn Baldwin 	/*
10152fcd493cSJohn Baldwin 	 * Check for matching resources.  We must have at least one match.
10162fcd493cSJohn Baldwin 	 * Since I/O and memory resources cannot be shared, if we get a
10172fcd493cSJohn Baldwin 	 * match on either of those, ignore any mismatches in IRQs or DRQs.
10180d484d24SJohn Baldwin 	 *
10190d484d24SJohn Baldwin 	 * XXX: We may want to revisit this to be more lenient and wire
10200d484d24SJohn Baldwin 	 * as long as it gets one match.
10210d484d24SJohn Baldwin 	 */
10220d484d24SJohn Baldwin 	matches = 0;
10230d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "port", &value) == 0) {
10242fcd493cSJohn Baldwin 	    /*
10252fcd493cSJohn Baldwin 	     * Floppy drive controllers are notorious for having a
10262fcd493cSJohn Baldwin 	     * wide variety of resources not all of which include the
10272fcd493cSJohn Baldwin 	     * first port that is specified by the hint (typically
10282fcd493cSJohn Baldwin 	     * 0x3f0) (see the comment above fdc_isa_alloc_resources()
10292fcd493cSJohn Baldwin 	     * in fdc_isa.c).  However, they do all seem to include
10302fcd493cSJohn Baldwin 	     * port + 2 (e.g. 0x3f2) so for a floppy device, look for
10312fcd493cSJohn Baldwin 	     * 'value + 2' in the port resources instead of the hint
10322fcd493cSJohn Baldwin 	     * value.
10332fcd493cSJohn Baldwin 	     */
10342fcd493cSJohn Baldwin 	    if (strcmp(name, "fdc") == 0)
10352fcd493cSJohn Baldwin 		value += 2;
10360d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
10370d484d24SJohn Baldwin 		matches++;
10380d484d24SJohn Baldwin 	    else
10390d484d24SJohn Baldwin 		continue;
10400d484d24SJohn Baldwin 	}
10410d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "maddr", &value) == 0) {
10420d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
10430d484d24SJohn Baldwin 		matches++;
10440d484d24SJohn Baldwin 	    else
10450d484d24SJohn Baldwin 		continue;
10460d484d24SJohn Baldwin 	}
10472fcd493cSJohn Baldwin 	if (matches > 0)
10482fcd493cSJohn Baldwin 	    goto matched;
10490d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "irq", &value) == 0) {
10500d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
10510d484d24SJohn Baldwin 		matches++;
10520d484d24SJohn Baldwin 	    else
10530d484d24SJohn Baldwin 		continue;
10540d484d24SJohn Baldwin 	}
10550d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "drq", &value) == 0) {
10560d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
10570d484d24SJohn Baldwin 		matches++;
10580d484d24SJohn Baldwin 	    else
10590d484d24SJohn Baldwin 		continue;
10600d484d24SJohn Baldwin 	}
10610d484d24SJohn Baldwin 
10622fcd493cSJohn Baldwin     matched:
10630d484d24SJohn Baldwin 	if (matches > 0) {
10640d484d24SJohn Baldwin 	    /* We have a winner! */
10650d484d24SJohn Baldwin 	    *unitp = unit;
10660d484d24SJohn Baldwin 	    break;
10670d484d24SJohn Baldwin 	}
10680d484d24SJohn Baldwin     }
10690d484d24SJohn Baldwin }
10700d484d24SJohn Baldwin 
1071adad4744SNate Lawson /*
1072adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
1073adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
1074adad4744SNate Lawson  */
1075adad4744SNate Lawson static int
1076adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
1077adad4744SNate Lawson {
1078adad4744SNate Lawson     struct resource *res;
1079adad4744SNate Lawson     struct resource_list *rl;
1080adad4744SNate Lawson     struct resource_list_entry *rle;
1081adad4744SNate Lawson     struct rman *rm;
1082da72d149SNate Lawson     char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1083da72d149SNate Lawson     device_t *children;
1084da72d149SNate Lawson     int child_count, i;
1085da72d149SNate Lawson 
1086da72d149SNate Lawson     /*
1087da72d149SNate Lawson      * Probe/attach any sysresource devices.  This would be unnecessary if we
1088da72d149SNate Lawson      * had multi-pass probe/attach.
1089da72d149SNate Lawson      */
1090da72d149SNate Lawson     if (device_get_children(dev, &children, &child_count) != 0)
1091da72d149SNate Lawson 	return (ENXIO);
1092da72d149SNate Lawson     for (i = 0; i < child_count; i++) {
1093da72d149SNate Lawson 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1094da72d149SNate Lawson 	    device_probe_and_attach(children[i]);
1095da72d149SNate Lawson     }
1096da72d149SNate Lawson     free(children, M_TEMP);
1097adad4744SNate Lawson 
1098adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
1099be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
1100adad4744SNate Lawson 	if (rle->res != NULL) {
1101adad4744SNate Lawson 	    device_printf(dev, "duplicate resource for %lx\n", rle->start);
1102adad4744SNate Lawson 	    continue;
1103adad4744SNate Lawson 	}
1104adad4744SNate Lawson 
1105adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
1106adad4744SNate Lawson 	switch (rle->type) {
1107adad4744SNate Lawson 	case SYS_RES_IOPORT:
1108adad4744SNate Lawson 	    rm = &acpi_rman_io;
1109adad4744SNate Lawson 	    break;
1110adad4744SNate Lawson 	case SYS_RES_MEMORY:
1111adad4744SNate Lawson 	    rm = &acpi_rman_mem;
1112adad4744SNate Lawson 	    break;
1113adad4744SNate Lawson 	default:
1114adad4744SNate Lawson 	    continue;
1115adad4744SNate Lawson 	}
1116adad4744SNate Lawson 
1117adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
1118adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
1119adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
1120adad4744SNate Lawson 	if (res != NULL) {
1121adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1122adad4744SNate Lawson 	    rle->res = res;
1123adad4744SNate Lawson 	} else
1124adad4744SNate Lawson 	    device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
1125adad4744SNate Lawson 		rle->start, rle->count, rle->type);
1126adad4744SNate Lawson     }
1127adad4744SNate Lawson     return (0);
1128adad4744SNate Lawson }
1129adad4744SNate Lawson 
1130ea233199SJohn Baldwin static char *pcilink_ids[] = { "PNP0C0F", NULL };
1131ea233199SJohn Baldwin static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1132ea233199SJohn Baldwin 
1133ea233199SJohn Baldwin /*
1134ea233199SJohn Baldwin  * Reserve declared resources for devices found during attach once system
1135ea233199SJohn Baldwin  * resources have been allocated.
1136ea233199SJohn Baldwin  */
1137ea233199SJohn Baldwin static void
1138ea233199SJohn Baldwin acpi_reserve_resources(device_t dev)
1139ea233199SJohn Baldwin {
1140ea233199SJohn Baldwin     struct resource_list_entry *rle;
1141ea233199SJohn Baldwin     struct resource_list *rl;
1142ea233199SJohn Baldwin     struct acpi_device *ad;
1143ea233199SJohn Baldwin     struct acpi_softc *sc;
1144ea233199SJohn Baldwin     device_t *children;
1145ea233199SJohn Baldwin     int child_count, i;
1146ea233199SJohn Baldwin 
1147ea233199SJohn Baldwin     sc = device_get_softc(dev);
1148ea233199SJohn Baldwin     if (device_get_children(dev, &children, &child_count) != 0)
1149ea233199SJohn Baldwin 	return;
1150ea233199SJohn Baldwin     for (i = 0; i < child_count; i++) {
1151ea233199SJohn Baldwin 	ad = device_get_ivars(children[i]);
1152ea233199SJohn Baldwin 	rl = &ad->ad_rl;
1153ea233199SJohn Baldwin 
1154ea233199SJohn Baldwin 	/* Don't reserve system resources. */
1155ea233199SJohn Baldwin 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1156ea233199SJohn Baldwin 	    continue;
1157ea233199SJohn Baldwin 
1158ea233199SJohn Baldwin 	STAILQ_FOREACH(rle, rl, link) {
1159ea233199SJohn Baldwin 	    /*
1160ea233199SJohn Baldwin 	     * Don't reserve IRQ resources.  There are many sticky things
1161ea233199SJohn Baldwin 	     * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1162ea233199SJohn Baldwin 	     * when using legacy routing).
1163ea233199SJohn Baldwin 	     */
1164ea233199SJohn Baldwin 	    if (rle->type == SYS_RES_IRQ)
1165ea233199SJohn Baldwin 		continue;
1166ea233199SJohn Baldwin 
1167ea233199SJohn Baldwin 	    /*
11680d81cf12SJohn Baldwin 	     * Don't reserve the resource if it is already allocated.
11690d81cf12SJohn Baldwin 	     * The acpi_ec(4) driver can allocate its resources early
11700d81cf12SJohn Baldwin 	     * if ECDT is present.
11710d81cf12SJohn Baldwin 	     */
11720d81cf12SJohn Baldwin 	    if (rle->res != NULL)
11730d81cf12SJohn Baldwin 		continue;
11740d81cf12SJohn Baldwin 
11750d81cf12SJohn Baldwin 	    /*
1176ea233199SJohn Baldwin 	     * Try to reserve the resource from our parent.  If this
1177ea233199SJohn Baldwin 	     * fails because the resource is a system resource, just
1178ea233199SJohn Baldwin 	     * let it be.  The resource range is already reserved so
1179ea233199SJohn Baldwin 	     * that other devices will not use it.  If the driver
1180ea233199SJohn Baldwin 	     * needs to allocate the resource, then
1181ea233199SJohn Baldwin 	     * acpi_alloc_resource() will sub-alloc from the system
1182ea233199SJohn Baldwin 	     * resource.
1183ea233199SJohn Baldwin 	     */
1184ea233199SJohn Baldwin 	    resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1185ea233199SJohn Baldwin 		rle->start, rle->end, rle->count, 0);
1186ea233199SJohn Baldwin 	}
1187ea233199SJohn Baldwin     }
1188ea233199SJohn Baldwin     free(children, M_TEMP);
1189ea233199SJohn Baldwin     sc->acpi_resources_reserved = 1;
1190ea233199SJohn Baldwin }
1191ea233199SJohn Baldwin 
1192ea233199SJohn Baldwin static int
1193ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid,
1194ea233199SJohn Baldwin     u_long start, u_long count)
1195ea233199SJohn Baldwin {
1196ea233199SJohn Baldwin     struct acpi_softc *sc = device_get_softc(dev);
1197ea233199SJohn Baldwin     struct acpi_device *ad = device_get_ivars(child);
1198ea233199SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
1199ea233199SJohn Baldwin     u_long end;
1200ea233199SJohn Baldwin 
1201ea233199SJohn Baldwin     /* Ignore IRQ resources for PCI link devices. */
1202ea233199SJohn Baldwin     if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL)
1203ea233199SJohn Baldwin 	return (0);
1204ea233199SJohn Baldwin 
1205ea233199SJohn Baldwin     /* If the resource is already allocated, fail. */
1206ea233199SJohn Baldwin     if (resource_list_busy(rl, type, rid))
1207ea233199SJohn Baldwin 	return (EBUSY);
1208ea233199SJohn Baldwin 
1209ea233199SJohn Baldwin     /* If the resource is already reserved, release it. */
1210ea233199SJohn Baldwin     if (resource_list_reserved(rl, type, rid))
1211ea233199SJohn Baldwin 	resource_list_unreserve(rl, dev, child, type, rid);
1212ea233199SJohn Baldwin 
1213ea233199SJohn Baldwin     /* Add the resource. */
1214ea233199SJohn Baldwin     end = (start + count - 1);
1215ea233199SJohn Baldwin     resource_list_add(rl, type, rid, start, end, count);
1216ea233199SJohn Baldwin 
1217ea233199SJohn Baldwin     /* Don't reserve resources until the system resources are allocated. */
1218ea233199SJohn Baldwin     if (!sc->acpi_resources_reserved)
1219ea233199SJohn Baldwin 	return (0);
1220ea233199SJohn Baldwin 
1221ea233199SJohn Baldwin     /* Don't reserve system resources. */
1222ea233199SJohn Baldwin     if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL)
1223ea233199SJohn Baldwin 	return (0);
1224ea233199SJohn Baldwin 
1225ea233199SJohn Baldwin     /*
1226ea233199SJohn Baldwin      * Don't reserve IRQ resources.  There are many sticky things to
1227ea233199SJohn Baldwin      * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when
1228ea233199SJohn Baldwin      * using legacy routing).
1229ea233199SJohn Baldwin      */
1230ea233199SJohn Baldwin     if (type == SYS_RES_IRQ)
1231ea233199SJohn Baldwin 	return (0);
1232ea233199SJohn Baldwin 
1233ea233199SJohn Baldwin     /*
1234ea233199SJohn Baldwin      * Reserve the resource.
1235ea233199SJohn Baldwin      *
1236ea233199SJohn Baldwin      * XXX: Ignores failure for now.  Failure here is probably a
1237ea233199SJohn Baldwin      * BIOS/firmware bug?
1238ea233199SJohn Baldwin      */
1239ea233199SJohn Baldwin     resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0);
1240ea233199SJohn Baldwin     return (0);
1241ea233199SJohn Baldwin }
1242ea233199SJohn Baldwin 
124315e32d5dSMike Smith static struct resource *
124415e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
124515e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
124615e32d5dSMike Smith {
124795957f62SJohn Baldwin     ACPI_RESOURCE ares;
1248ea233199SJohn Baldwin     struct acpi_device *ad;
124991233413SNate Lawson     struct resource_list_entry *rle;
1250ea233199SJohn Baldwin     struct resource_list *rl;
125191233413SNate Lawson     struct resource *res;
1252ea233199SJohn Baldwin     int isdefault = (start == 0UL && end == ~0UL);
125315e2f34fSNate Lawson 
125491233413SNate Lawson     /*
1255ea233199SJohn Baldwin      * First attempt at allocating the resource.  For direct children,
1256ea233199SJohn Baldwin      * use resource_list_alloc() to handle reserved resources.  For
1257f6133e71SJohn Baldwin      * other devices, pass the request up to our parent.
125891233413SNate Lawson      */
1259ea233199SJohn Baldwin     if (bus == device_get_parent(child)) {
1260ea233199SJohn Baldwin 	ad = device_get_ivars(child);
1261ea233199SJohn Baldwin 	rl = &ad->ad_rl;
126291233413SNate Lawson 
1263397c30a8SJohn Baldwin 	/*
1264ea233199SJohn Baldwin 	 * Simulate the behavior of the ISA bus for direct children
1265ea233199SJohn Baldwin 	 * devices.  That is, if a non-default range is specified for
1266ea233199SJohn Baldwin 	 * a resource that doesn't exist, use bus_set_resource() to
1267ea233199SJohn Baldwin 	 * add the resource before allocating it.  Note that these
1268ea233199SJohn Baldwin 	 * resources will not be reserved.
1269397c30a8SJohn Baldwin 	 */
1270ea233199SJohn Baldwin 	if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1271ea233199SJohn Baldwin 		resource_list_add(rl, type, *rid, start, end, count);
1272ea233199SJohn Baldwin 	res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1273ea233199SJohn Baldwin 	    flags);
1274ea233199SJohn Baldwin 	if (res != NULL && type == SYS_RES_IRQ) {
127595957f62SJohn Baldwin 	    /*
127695957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
127795957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
127895957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
127995957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
128095957f62SJohn Baldwin 	     *
128195957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
128295957f62SJohn Baldwin 	     */
128395957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
128495957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
128595957f62SJohn Baldwin 	}
128615e2f34fSNate Lawson 
1287ea233199SJohn Baldwin 	/*
1288ea233199SJohn Baldwin 	 * If this is an allocation of the "default" range for a given
1289ea233199SJohn Baldwin 	 * RID, fetch the exact bounds for this resource from the
1290ea233199SJohn Baldwin 	 * resource list entry to try to allocate the range from the
1291ea233199SJohn Baldwin 	 * system resource regions.
1292ea233199SJohn Baldwin 	 */
1293ea233199SJohn Baldwin 	if (res == NULL && isdefault) {
1294ea233199SJohn Baldwin 	    rle = resource_list_find(rl, type, *rid);
1295ea233199SJohn Baldwin 	    if (rle != NULL) {
1296ea233199SJohn Baldwin 		start = rle->start;
1297ea233199SJohn Baldwin 		end = rle->end;
1298ea233199SJohn Baldwin 		count = rle->count;
1299ea233199SJohn Baldwin 	    }
1300ea233199SJohn Baldwin 	}
1301ea233199SJohn Baldwin     } else
1302ea233199SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
1303ea233199SJohn Baldwin 	    start, end, count, flags);
1304ea233199SJohn Baldwin 
1305ea233199SJohn Baldwin     /*
1306ea233199SJohn Baldwin      * If the first attempt failed and this is an allocation of a
1307ea233199SJohn Baldwin      * specific range, try to satisfy the request via a suballocation
13085d0d779bSJohn Baldwin      * from our system resource regions.
1309ea233199SJohn Baldwin      */
13105d0d779bSJohn Baldwin     if (res == NULL && start + count - 1 == end)
13115d0d779bSJohn Baldwin 	res = acpi_alloc_sysres(child, type, rid, start, end, count, flags);
13125d0d779bSJohn Baldwin     return (res);
13135d0d779bSJohn Baldwin }
13145d0d779bSJohn Baldwin 
13155d0d779bSJohn Baldwin /*
13165d0d779bSJohn Baldwin  * Attempt to allocate a specific resource range from the system
13175d0d779bSJohn Baldwin  * resource ranges.  Note that we only handle memory and I/O port
13185d0d779bSJohn Baldwin  * system resources.
13195d0d779bSJohn Baldwin  */
13205d0d779bSJohn Baldwin struct resource *
13215d0d779bSJohn Baldwin acpi_alloc_sysres(device_t child, int type, int *rid, u_long start, u_long end,
13225d0d779bSJohn Baldwin     u_long count, u_int flags)
13235d0d779bSJohn Baldwin {
13245d0d779bSJohn Baldwin     struct rman *rm;
13255d0d779bSJohn Baldwin     struct resource *res;
13265d0d779bSJohn Baldwin 
1327ea233199SJohn Baldwin     switch (type) {
1328ea233199SJohn Baldwin     case SYS_RES_IOPORT:
1329ea233199SJohn Baldwin 	rm = &acpi_rman_io;
1330ea233199SJohn Baldwin 	break;
1331ea233199SJohn Baldwin     case SYS_RES_MEMORY:
1332ea233199SJohn Baldwin 	rm = &acpi_rman_mem;
1333ea233199SJohn Baldwin 	break;
1334ea233199SJohn Baldwin     default:
1335ea233199SJohn Baldwin 	return (NULL);
1336ea233199SJohn Baldwin     }
1337ea233199SJohn Baldwin 
13385d0d779bSJohn Baldwin     KASSERT(start + count - 1 == end, ("wildcard resource range"));
1339ea233199SJohn Baldwin     res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1340ea233199SJohn Baldwin 	child);
1341ea233199SJohn Baldwin     if (res == NULL)
1342ea233199SJohn Baldwin 	return (NULL);
1343ea233199SJohn Baldwin 
1344ea233199SJohn Baldwin     rman_set_rid(res, *rid);
1345ea233199SJohn Baldwin 
1346ea233199SJohn Baldwin     /* If requested, activate the resource using the parent's method. */
1347ea233199SJohn Baldwin     if (flags & RF_ACTIVE)
1348ea233199SJohn Baldwin 	if (bus_activate_resource(child, type, *rid, res) != 0) {
1349ea233199SJohn Baldwin 	    rman_release_resource(res);
1350ea233199SJohn Baldwin 	    return (NULL);
1351ea233199SJohn Baldwin 	}
1352ea233199SJohn Baldwin 
135391233413SNate Lawson     return (res);
135415e32d5dSMike Smith }
135515e32d5dSMike Smith 
135615e32d5dSMike Smith static int
1357049dc0d1SJohn Baldwin acpi_is_resource_managed(int type, struct resource *r)
135815e32d5dSMike Smith {
135915e32d5dSMike Smith 
1360397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1361397c30a8SJohn Baldwin     switch (type) {
1362397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1363049dc0d1SJohn Baldwin 	return (rman_is_region_manager(r, &acpi_rman_io));
1364397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1365049dc0d1SJohn Baldwin 	return (rman_is_region_manager(r, &acpi_rman_mem));
1366397c30a8SJohn Baldwin     }
1367049dc0d1SJohn Baldwin     return (0);
1368049dc0d1SJohn Baldwin }
1369049dc0d1SJohn Baldwin 
1370049dc0d1SJohn Baldwin static int
1371049dc0d1SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
1372049dc0d1SJohn Baldwin     u_long start, u_long end)
1373049dc0d1SJohn Baldwin {
1374049dc0d1SJohn Baldwin 
1375049dc0d1SJohn Baldwin     if (acpi_is_resource_managed(type, r))
1376049dc0d1SJohn Baldwin 	return (rman_adjust_resource(r, start, end));
1377049dc0d1SJohn Baldwin     return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1378049dc0d1SJohn Baldwin }
1379049dc0d1SJohn Baldwin 
1380049dc0d1SJohn Baldwin static int
1381049dc0d1SJohn Baldwin acpi_release_resource(device_t bus, device_t child, int type, int rid,
1382049dc0d1SJohn Baldwin     struct resource *r)
1383049dc0d1SJohn Baldwin {
1384049dc0d1SJohn Baldwin     int ret;
1385397c30a8SJohn Baldwin 
138691233413SNate Lawson     /*
1387397c30a8SJohn Baldwin      * If this resource belongs to one of our internal managers,
1388ea233199SJohn Baldwin      * deactivate it and release it to the local pool.
138991233413SNate Lawson      */
1390049dc0d1SJohn Baldwin     if (acpi_is_resource_managed(type, r)) {
139191233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
139291233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
139391233413SNate Lawson 	    if (ret != 0)
139491233413SNate Lawson 		return (ret);
139515e32d5dSMike Smith 	}
1396ea233199SJohn Baldwin 	return (rman_release_resource(r));
1397ea233199SJohn Baldwin     }
1398ea233199SJohn Baldwin 
1399ea233199SJohn Baldwin     return (bus_generic_rl_release_resource(bus, child, type, rid, r));
1400ea233199SJohn Baldwin }
140115e32d5dSMike Smith 
14028b28b622SNate Lawson static void
14038b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
14048b28b622SNate Lawson {
14058b28b622SNate Lawson     struct resource_list *rl;
14068b28b622SNate Lawson 
14078b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
1408ea233199SJohn Baldwin     if (resource_list_busy(rl, type, rid)) {
1409ea233199SJohn Baldwin 	device_printf(bus, "delete_resource: Resource still owned by child"
1410ea233199SJohn Baldwin 	    " (type=%d, rid=%d)\n", type, rid);
1411ea233199SJohn Baldwin 	return;
1412ea233199SJohn Baldwin     }
1413ea233199SJohn Baldwin     resource_list_unreserve(rl, bus, child, type, rid);
14148b28b622SNate Lawson     resource_list_delete(rl, type, rid);
14158b28b622SNate Lawson }
14168b28b622SNate Lawson 
1417dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1418e1c4bf3fSNate Lawson int
1419e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1420907b6777SNate Lawson     struct resource **res, u_int flags)
1421dc750869SNate Lawson {
1422e1c4bf3fSNate Lawson     int error, res_type;
1423dc750869SNate Lawson 
1424e1c4bf3fSNate Lawson     error = ENOMEM;
14258f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1426e1c4bf3fSNate Lawson 	return (EINVAL);
1427dc750869SNate Lawson 
14288f118e25SNate Lawson     /* We only support memory and IO spaces. */
14292be4e471SJung-uk Kim     switch (gas->SpaceId) {
1430dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1431e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1432dc750869SNate Lawson 	break;
1433dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1434e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1435dc750869SNate Lawson 	break;
1436dc750869SNate Lawson     default:
1437e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1438dc750869SNate Lawson     }
1439dc750869SNate Lawson 
14402fe912dfSNate Lawson     /*
1441f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1442f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
14432fe912dfSNate Lawson      */
14442be4e471SJung-uk Kim     if (gas->BitWidth && gas->BitWidth < 8)
14452be4e471SJung-uk Kim 	gas->BitWidth = 8;
14462fe912dfSNate Lawson 
14478f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
14482be4e471SJung-uk Kim     if (gas->Address == 0 || gas->BitWidth == 0)
14498f118e25SNate Lawson 	return (EINVAL);
14508f118e25SNate Lawson 
1451e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
14522be4e471SJung-uk Kim 	gas->BitWidth / 8);
1453907b6777SNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1454e1c4bf3fSNate Lawson     if (*res != NULL) {
1455e1c4bf3fSNate Lawson 	*type = res_type;
1456e1c4bf3fSNate Lawson 	error = 0;
1457ca2c69c8SNate Lawson     } else
1458ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1459ca2c69c8SNate Lawson 
1460e1c4bf3fSNate Lawson     return (error);
1461dc750869SNate Lawson }
1462dc750869SNate Lawson 
1463c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
14641e4925e8SNate Lawson static uint32_t
146532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1466bc0f2195SMike Smith {
14671e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1468bc0f2195SMike Smith     ACPI_HANDLE		h;
146992488a57SJung-uk Kim     uint32_t		pnpid;
147032d18aa5SMike Smith 
1471b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
147232d18aa5SMike Smith 
14736fca9360SNate Lawson     /* Fetch and validate the HID. */
147492488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
147592488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
147692488a57SJung-uk Kim 	return_VALUE (0);
147732d18aa5SMike Smith 
147892488a57SJung-uk Kim     pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
147992488a57SJung-uk Kim 	devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
148092488a57SJung-uk Kim 	PNP_EISAID(devinfo->HardwareId.String) : 0;
148192488a57SJung-uk Kim     AcpiOsFree(devinfo);
1482be2b1797SNate Lawson 
148332d18aa5SMike Smith     return_VALUE (pnpid);
148432d18aa5SMike Smith }
148532d18aa5SMike Smith 
14861e4925e8SNate Lawson static int
14871e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1488b2566207STakanori Watanabe {
14891e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
149092488a57SJung-uk Kim     ACPI_DEVICE_ID	*ids;
1491b2566207STakanori Watanabe     ACPI_HANDLE		h;
14921e4925e8SNate Lawson     uint32_t		*pnpid;
149392488a57SJung-uk Kim     int			i, valid;
1494b2566207STakanori Watanabe 
1495b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1496b2566207STakanori Watanabe 
14971e4925e8SNate Lawson     pnpid = cids;
1498bd189fe7SNate Lawson 
14991e4925e8SNate Lawson     /* Fetch and validate the CID */
150092488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
150192488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
150292488a57SJung-uk Kim 	return_VALUE (0);
1503b2566207STakanori Watanabe 
150492488a57SJung-uk Kim     if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
150592488a57SJung-uk Kim 	AcpiOsFree(devinfo);
150692488a57SJung-uk Kim 	return_VALUE (0);
1507b2566207STakanori Watanabe     }
1508b2566207STakanori Watanabe 
150992488a57SJung-uk Kim     if (devinfo->CompatibleIdList.Count < count)
151092488a57SJung-uk Kim 	count = devinfo->CompatibleIdList.Count;
151192488a57SJung-uk Kim     ids = devinfo->CompatibleIdList.Ids;
151292488a57SJung-uk Kim     for (i = 0, valid = 0; i < count; i++)
151392488a57SJung-uk Kim 	if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
151492488a57SJung-uk Kim 	    strncmp(ids[i].String, "PNP", 3) == 0) {
151592488a57SJung-uk Kim 	    *pnpid++ = PNP_EISAID(ids[i].String);
151692488a57SJung-uk Kim 	    valid++;
151792488a57SJung-uk Kim 	}
151892488a57SJung-uk Kim     AcpiOsFree(devinfo);
151992488a57SJung-uk Kim 
15201e4925e8SNate Lawson     return_VALUE (valid);
15211e4925e8SNate Lawson }
1522b2566207STakanori Watanabe 
1523ce619b2eSNate Lawson static char *
1524ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1525ce619b2eSNate Lawson {
1526ce619b2eSNate Lawson     ACPI_HANDLE h;
152792488a57SJung-uk Kim     ACPI_OBJECT_TYPE t;
1528ce619b2eSNate Lawson     int i;
1529ce619b2eSNate Lawson 
1530ce619b2eSNate Lawson     h = acpi_get_handle(dev);
153192488a57SJung-uk Kim     if (ids == NULL || h == NULL)
153292488a57SJung-uk Kim 	return (NULL);
153392488a57SJung-uk Kim     t = acpi_get_type(dev);
153492488a57SJung-uk Kim     if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
1535ce619b2eSNate Lawson 	return (NULL);
1536ce619b2eSNate Lawson 
1537ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1538ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1539ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1540ce619b2eSNate Lawson 	    return (ids[i]);
1541ce619b2eSNate Lawson     }
1542ce619b2eSNate Lawson     return (NULL);
1543ce619b2eSNate Lawson }
1544ce619b2eSNate Lawson 
1545ce619b2eSNate Lawson static ACPI_STATUS
1546ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1547ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1548ce619b2eSNate Lawson {
1549ce619b2eSNate Lawson     ACPI_HANDLE h;
1550ce619b2eSNate Lawson 
1551df8d2a32SNate Lawson     if (dev == NULL)
1552df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1553df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1554ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1555ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1556ce619b2eSNate Lawson }
1557ce619b2eSNate Lawson 
155862508c53SJohn Baldwin int
155910ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
156010ce62b9SNate Lawson {
156110ce62b9SNate Lawson     struct acpi_softc *sc;
156210ce62b9SNate Lawson     ACPI_HANDLE handle;
156310ce62b9SNate Lawson     ACPI_STATUS status;
156410ce62b9SNate Lawson     char sxd[8];
156510ce62b9SNate Lawson 
156610ce62b9SNate Lawson     handle = acpi_get_handle(dev);
156710ce62b9SNate Lawson 
156810ce62b9SNate Lawson     /*
156910ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
157010ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
157110ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
157210ce62b9SNate Lawson      * need special handling in their drivers.
157310ce62b9SNate Lawson      */
1574a7a3177fSJung-uk Kim     if (dstate == NULL || handle == NULL ||
157510ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
157610ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
157710ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
157810ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
157910ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
158010ce62b9SNate Lawson 	return (ENXIO);
158110ce62b9SNate Lawson 
158210ce62b9SNate Lawson     /*
1583a7a3177fSJung-uk Kim      * Override next state with the value from _SxD, if present.
1584a7a3177fSJung-uk Kim      * Note illegal _S0D is evaluated because some systems expect this.
158510ce62b9SNate Lawson      */
1586a7a3177fSJung-uk Kim     sc = device_get_softc(bus);
158710ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
158810ce62b9SNate Lawson     status = acpi_GetInteger(handle, sxd, dstate);
1589a7a3177fSJung-uk Kim     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
1590a7a3177fSJung-uk Kim 	    device_printf(dev, "failed to get %s on %s: %s\n", sxd,
1591a7a3177fSJung-uk Kim 		acpi_name(handle), AcpiFormatException(status));
1592a7a3177fSJung-uk Kim 	    return (ENXIO);
159310ce62b9SNate Lawson     }
159410ce62b9SNate Lawson 
1595a7a3177fSJung-uk Kim     return (0);
159610ce62b9SNate Lawson }
159710ce62b9SNate Lawson 
1598df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1599df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1600df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1601df8d2a32SNate Lawson     void		*arg;
1602df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1603df8d2a32SNate Lawson };
1604df8d2a32SNate Lawson 
1605ce619b2eSNate Lawson static ACPI_STATUS
1606df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1607df8d2a32SNate Lawson {
1608df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1609df8d2a32SNate Lawson     device_t dev, old_dev;
1610df8d2a32SNate Lawson     ACPI_STATUS status;
1611df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1612df8d2a32SNate Lawson 
1613df8d2a32SNate Lawson     /*
1614df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1615df8d2a32SNate Lawson      * the parent where the scan began.
1616df8d2a32SNate Lawson      */
1617df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1618df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1619df8d2a32SNate Lawson 	return (AE_OK);
1620df8d2a32SNate Lawson 
1621df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1622df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1623df8d2a32SNate Lawson 	return (AE_OK);
1624df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1625df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1626df8d2a32SNate Lawson 	return (AE_OK);
1627df8d2a32SNate Lawson 
1628df8d2a32SNate Lawson     /*
1629df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1630df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1631df8d2a32SNate Lawson      */
1632df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1633df8d2a32SNate Lawson     dev = old_dev;
1634df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1635df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1636df8d2a32SNate Lawson 	return (status);
1637df8d2a32SNate Lawson 
1638df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1639df8d2a32SNate Lawson     if (old_dev != NULL) {
1640df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1641df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1642df8d2a32SNate Lawson     }
1643df8d2a32SNate Lawson 
1644df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1645df8d2a32SNate Lawson     if (dev != NULL)
1646df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1647df8d2a32SNate Lawson 
1648df8d2a32SNate Lawson     return (AE_OK);
1649df8d2a32SNate Lawson }
1650df8d2a32SNate Lawson 
1651df8d2a32SNate Lawson static ACPI_STATUS
1652df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1653df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1654ce619b2eSNate Lawson {
1655ce619b2eSNate Lawson     ACPI_HANDLE h;
1656df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1657ce619b2eSNate Lawson 
1658df8d2a32SNate Lawson     if (acpi_disabled("children"))
1659df8d2a32SNate Lawson 	return (AE_OK);
1660df8d2a32SNate Lawson 
1661df8d2a32SNate Lawson     if (dev == NULL)
1662df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1663df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1664ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1665df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1666df8d2a32SNate Lawson     ctx.arg = arg;
1667df8d2a32SNate Lawson     ctx.parent = h;
1668df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
16692272d050SJung-uk Kim 	acpi_device_scan_cb, NULL, &ctx, NULL));
1670ce619b2eSNate Lawson }
1671ce619b2eSNate Lawson 
167210ce62b9SNate Lawson /*
167310ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
167410ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
167510ce62b9SNate Lawson  */
167610ce62b9SNate Lawson static int
1677a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state)
167810ce62b9SNate Lawson {
167910ce62b9SNate Lawson     ACPI_HANDLE h;
168010ce62b9SNate Lawson     ACPI_STATUS status;
168110ce62b9SNate Lawson 
168210ce62b9SNate Lawson     h = acpi_get_handle(child);
1683a0e73a12SJung-uk Kim     if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
168410ce62b9SNate Lawson 	return (EINVAL);
168510ce62b9SNate Lawson     if (h == NULL)
168610ce62b9SNate Lawson 	return (0);
168710ce62b9SNate Lawson 
168810ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
168910ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
1690a7a3177fSJung-uk Kim     if (ACPI_SUCCESS(status)) {
1691a7a3177fSJung-uk Kim 	if (bootverbose)
1692a7a3177fSJung-uk Kim 	    device_printf(child, "set ACPI power state D%d on %s\n",
1693a7a3177fSJung-uk Kim 		state, acpi_name(h));
1694a7a3177fSJung-uk Kim     } else if (status != AE_NOT_FOUND)
1695a7a3177fSJung-uk Kim 	device_printf(child,
1696a7a3177fSJung-uk Kim 	    "failed to set ACPI power state D%d on %s: %s\n", state,
1697a7a3177fSJung-uk Kim 	    acpi_name(h), AcpiFormatException(status));
169810ce62b9SNate Lawson 
1699a7a3177fSJung-uk Kim     return (0);
170010ce62b9SNate Lawson }
170110ce62b9SNate Lawson 
170232d18aa5SMike Smith static int
170332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
170432d18aa5SMike Smith {
17051e4925e8SNate Lawson     int			result, cid_count, i;
17061e4925e8SNate Lawson     uint32_t		lid, cids[8];
1707bc0f2195SMike Smith 
1708b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1709bc0f2195SMike Smith 
1710bc0f2195SMike Smith     /*
1711bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1712bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1713bc0f2195SMike Smith      * that to happen, so don't ever return it.
1714bc0f2195SMike Smith      */
1715bc0f2195SMike Smith     result = ENXIO;
1716bc0f2195SMike Smith 
1717be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1718b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
17191e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1720bc0f2195SMike Smith     while (ids && ids->ip_id) {
17211e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1722bc0f2195SMike Smith 	    result = 0;
1723bc0f2195SMike Smith 	    goto out;
1724bc0f2195SMike Smith 	}
17251e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
17261e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
17271e4925e8SNate Lawson 		result = 0;
17281e4925e8SNate Lawson 		goto out;
17291e4925e8SNate Lawson 	    }
17301e4925e8SNate Lawson 	}
1731bc0f2195SMike Smith 	ids++;
1732bc0f2195SMike Smith     }
1733be2b1797SNate Lawson 
1734bc0f2195SMike Smith  out:
17359e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
17369e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
17379e0dd54fSNate Lawson 
1738bc0f2195SMike Smith     return_VALUE (result);
1739bc0f2195SMike Smith }
1740bc0f2195SMike Smith 
1741d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
1742d320e05cSJohn Baldwin /*
1743d320e05cSJohn Baldwin  * Look for a MCFG table.  If it is present, use the settings for
1744d320e05cSJohn Baldwin  * domain (segment) 0 to setup PCI config space access via the memory
1745d320e05cSJohn Baldwin  * map.
1746d320e05cSJohn Baldwin  */
1747d320e05cSJohn Baldwin static void
1748d320e05cSJohn Baldwin acpi_enable_pcie(void)
1749d320e05cSJohn Baldwin {
1750d320e05cSJohn Baldwin 	ACPI_TABLE_HEADER *hdr;
1751d320e05cSJohn Baldwin 	ACPI_MCFG_ALLOCATION *alloc, *end;
1752d320e05cSJohn Baldwin 	ACPI_STATUS status;
1753d320e05cSJohn Baldwin 
1754d320e05cSJohn Baldwin 	status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1755d320e05cSJohn Baldwin 	if (ACPI_FAILURE(status))
1756d320e05cSJohn Baldwin 		return;
1757d320e05cSJohn Baldwin 
1758d320e05cSJohn Baldwin 	end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1759d320e05cSJohn Baldwin 	alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1760d320e05cSJohn Baldwin 	while (alloc < end) {
1761d320e05cSJohn Baldwin 		if (alloc->PciSegment == 0) {
1762d320e05cSJohn Baldwin 			pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1763d320e05cSJohn Baldwin 			    alloc->EndBusNumber);
1764d320e05cSJohn Baldwin 			return;
1765d320e05cSJohn Baldwin 		}
1766d320e05cSJohn Baldwin 		alloc++;
1767d320e05cSJohn Baldwin 	}
1768d320e05cSJohn Baldwin }
1769d320e05cSJohn Baldwin #endif
1770d320e05cSJohn Baldwin 
1771bc0f2195SMike Smith /*
177233332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
177315e32d5dSMike Smith  *
177433332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
177533332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
177633332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
177733332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
177833332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
177915e32d5dSMike Smith  */
178015e32d5dSMike Smith static void
178115e32d5dSMike Smith acpi_probe_children(device_t bus)
178215e32d5dSMike Smith {
178315e32d5dSMike Smith 
1784b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17850ae55423SMike Smith 
178615e32d5dSMike Smith     /*
178715e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1788edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
178915e32d5dSMike Smith      *
179015e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1791be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1792be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1793be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
179415e32d5dSMike Smith      */
17954c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
179633332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
17972272d050SJung-uk Kim 	NULL, bus, NULL);
179815e32d5dSMike Smith 
1799adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1800adad4744SNate Lawson     acpi_sysres_alloc(bus);
1801adad4744SNate Lawson 
1802ea233199SJohn Baldwin     /* Reserve resources already allocated to children. */
1803ea233199SJohn Baldwin     acpi_reserve_resources(bus);
1804ea233199SJohn Baldwin 
1805edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1806edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1807edc13633SNate Lawson     bus_generic_probe(bus);
1808edc13633SNate Lawson 
18095e66b8e2SJohn Baldwin     /* Probe/attach all children, created statically and from the namespace. */
18100430ba9eSAndriy Gapon     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n"));
181115e32d5dSMike Smith     bus_generic_attach(bus);
18120ae55423SMike Smith 
181388a79fc0SNate Lawson     /* Attach wake sysctls. */
18145c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
181588a79fc0SNate Lawson 
1816bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
18170ae55423SMike Smith     return_VOID;
181815e32d5dSMike Smith }
181915e32d5dSMike Smith 
1820b82ca9a9SNate Lawson /*
1821d227204dSJohn Baldwin  * Determine the probe order for a given device.
1822b82ca9a9SNate Lawson  */
1823d227204dSJohn Baldwin static void
1824b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
182591233413SNate Lawson {
1826463e0f91SJohn Baldwin 	ACPI_OBJECT_TYPE type;
182791233413SNate Lawson 
1828b82ca9a9SNate Lawson 	/*
1829404b0d10SJung-uk Kim 	 * 0. CPUs
1830404b0d10SJung-uk Kim 	 * 1. I/O port and memory system resource holders
1831404b0d10SJung-uk Kim 	 * 2. Clocks and timers (to handle early accesses)
1832b1f9b996SAndriy Gapon 	 * 3. Embedded controllers (to handle early accesses)
1833b1f9b996SAndriy Gapon 	 * 4. PCI Link Devices
1834b82ca9a9SNate Lawson 	 */
1835463e0f91SJohn Baldwin 	AcpiGetType(handle, &type);
1836aa835160SAndriy Gapon 	if (type == ACPI_TYPE_PROCESSOR)
1837404b0d10SJung-uk Kim 		*order = 0;
1838404b0d10SJung-uk Kim 	else if (acpi_MatchHid(handle, "PNP0C01") ||
1839404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0C02"))
184091233413SNate Lawson 		*order = 1;
1841404b0d10SJung-uk Kim 	else if (acpi_MatchHid(handle, "PNP0100") ||
1842404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0103") ||
1843404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0B00"))
184491233413SNate Lawson 		*order = 2;
1845aa835160SAndriy Gapon 	else if (acpi_MatchHid(handle, "PNP0C09"))
1846b460c6f8SJohn Baldwin 		*order = 3;
1847aa835160SAndriy Gapon 	else if (acpi_MatchHid(handle, "PNP0C0F"))
1848aa835160SAndriy Gapon 		*order = 4;
184991233413SNate Lawson }
185091233413SNate Lawson 
185115e32d5dSMike Smith /*
185215e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
185315e32d5dSMike Smith  * it.
185415e32d5dSMike Smith  */
185515e32d5dSMike Smith static ACPI_STATUS
185615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
185715e32d5dSMike Smith {
18585a77b11bSJung-uk Kim     struct acpi_prw_data prw;
185915e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
1860858a52f4SMitsuru IWASAKI     ACPI_HANDLE h;
186133332dc2SNate Lawson     device_t bus, child;
1862495a4144SJung-uk Kim     char *handle_str;
1863e4cd9dcfSJohn Baldwin     int order;
186415e32d5dSMike Smith 
1865b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18660ae55423SMike Smith 
1867495a4144SJung-uk Kim     if (acpi_disabled("children"))
1868495a4144SJung-uk Kim 	return_ACPI_STATUS (AE_OK);
1869495a4144SJung-uk Kim 
1870be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
18710ae55423SMike Smith     if (acpi_avoid(handle))
18720ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
18730ae55423SMike Smith 
187491233413SNate Lawson     bus = (device_t)context;
1875b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1876495a4144SJung-uk Kim 	handle_str = acpi_name(handle);
187715e32d5dSMike Smith 	switch (type) {
187815e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
1879495a4144SJung-uk Kim 	    /*
1880495a4144SJung-uk Kim 	     * Since we scan from \, be sure to skip system scope objects.
1881495a4144SJung-uk Kim 	     * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
1882affa1826SJung-uk Kim 	     * BIOS bugs.  For example, \_SB_ is to allow \_SB_._INI to be run
1883495a4144SJung-uk Kim 	     * during the intialization and \_TZ_ is to support Notify() on it.
1884495a4144SJung-uk Kim 	     */
1885495a4144SJung-uk Kim 	    if (strcmp(handle_str, "\\_SB_") == 0 ||
1886495a4144SJung-uk Kim 		strcmp(handle_str, "\\_TZ_") == 0)
1887495a4144SJung-uk Kim 		break;
18885a77b11bSJung-uk Kim 	    if (acpi_parse_prw(handle, &prw) == 0)
18895a77b11bSJung-uk Kim 		AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
1890183c8af3SJohn Baldwin 
1891183c8af3SJohn Baldwin 	    /*
1892183c8af3SJohn Baldwin 	     * Ignore devices that do not have a _HID or _CID.  They should
1893183c8af3SJohn Baldwin 	     * be discovered by other buses (e.g. the PCI bus driver).
1894183c8af3SJohn Baldwin 	     */
1895183c8af3SJohn Baldwin 	    if (!acpi_has_hid(handle))
1896183c8af3SJohn Baldwin 		break;
1897495a4144SJung-uk Kim 	    /* FALLTHROUGH */
189815e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
189915e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
190015e32d5dSMike Smith 	case ACPI_TYPE_POWER:
190133332dc2SNate Lawson 	    /*
1902463e0f91SJohn Baldwin 	     * Create a placeholder device for this node.  Sort the
1903463e0f91SJohn Baldwin 	     * placeholder so that the probe/attach passes will run
1904463e0f91SJohn Baldwin 	     * breadth-first.  Orders less than ACPI_DEV_BASE_ORDER
1905463e0f91SJohn Baldwin 	     * are reserved for special objects (i.e., system
1906b1f9b996SAndriy Gapon 	     * resources).
190715e32d5dSMike Smith 	     */
190833332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1909404b0d10SJung-uk Kim 	    order = level * 10 + ACPI_DEV_BASE_ORDER;
1910da72d149SNate Lawson 	    acpi_probe_order(handle, &order);
1911e4cd9dcfSJohn Baldwin 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1912bc0f2195SMike Smith 	    if (child == NULL)
1913bc0f2195SMike Smith 		break;
191459a890e6SNate Lawson 
191559a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
191615e32d5dSMike Smith 	    acpi_set_handle(child, handle);
191759a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1918bc0f2195SMike Smith 
1919bc0f2195SMike Smith 	    /*
1920b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1921b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1922b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1923893f750aSNate Lawson 	     *
1924893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
1925893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
1926893f750aSNate Lawson 	     * anyway since we may enable them later.
1927b519f9d6SMike Smith 	     */
1928858a52f4SMitsuru IWASAKI 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1929858a52f4SMitsuru IWASAKI 		/* Never disable PCI link devices. */
1930858a52f4SMitsuru IWASAKI 		if (acpi_MatchHid(handle, "PNP0C0F"))
1931858a52f4SMitsuru IWASAKI 		    break;
1932858a52f4SMitsuru IWASAKI 		/*
1933858a52f4SMitsuru IWASAKI 		 * Docking stations should remain enabled since the system
1934858a52f4SMitsuru IWASAKI 		 * may be undocked at boot.
1935858a52f4SMitsuru IWASAKI 		 */
1936858a52f4SMitsuru IWASAKI 		if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
1937858a52f4SMitsuru IWASAKI 		    break;
1938858a52f4SMitsuru IWASAKI 
1939b519f9d6SMike Smith 		device_disable(child);
1940b519f9d6SMike Smith 		break;
1941b519f9d6SMike Smith 	    }
1942b519f9d6SMike Smith 
1943b519f9d6SMike Smith 	    /*
1944bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1945bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1946bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1947bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1948bc0f2195SMike Smith 	     * device not to have any resources.
1949bc0f2195SMike Smith 	     */
195072ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
19510ae55423SMike Smith 	    break;
195215e32d5dSMike Smith 	}
195315e32d5dSMike Smith     }
1954be2b1797SNate Lawson 
19550ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
195615e32d5dSMike Smith }
195715e32d5dSMike Smith 
195859a890e6SNate Lawson /*
195959a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
196059a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
196159a890e6SNate Lawson  */
196259a890e6SNate Lawson void
196392488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data)
196459a890e6SNate Lawson {
196559a890e6SNate Lawson }
196659a890e6SNate Lawson 
196715e32d5dSMike Smith static void
196815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
196915e32d5dSMike Smith {
19702d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
197171804adcSJung-uk Kim     register_t intr;
1972d33f4987STakanori Watanabe     ACPI_STATUS status;
1973eeb3a05fSNate Lawson 
1974eeb3a05fSNate Lawson     /*
197580f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
197680f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
197780f0e4c2SNate Lawson      * an AP.
1978eeb3a05fSNate Lawson      */
1979eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1980904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1981904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
19822d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1983904bf0c2SNate Lawson 		AcpiFormatException(status));
1984904bf0c2SNate Lawson 	    return;
1985904bf0c2SNate Lawson 	}
19862d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Powering system off\n");
198771804adcSJung-uk Kim 	intr = intr_disable();
19884c52cad2SJung-uk Kim 	status = AcpiEnterSleepState(ACPI_STATE_S5, acpi_sleep_flags);
198971804adcSJung-uk Kim 	if (ACPI_FAILURE(status)) {
199071804adcSJung-uk Kim 	    intr_restore(intr);
19912d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - %s\n",
19922d0c82e8SJung-uk Kim 		AcpiFormatException(status));
199371804adcSJung-uk Kim 	} else {
199415e32d5dSMike Smith 	    DELAY(1000000);
199571804adcSJung-uk Kim 	    intr_restore(intr);
19962d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - timeout\n");
199715e32d5dSMike Smith 	}
1998ac731af5SJung-uk Kim     } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
1999d85e6785SNate Lawson 	/* Reboot using the reset register. */
2000ac731af5SJung-uk Kim 	status = AcpiReset();
2001ac731af5SJung-uk Kim 	if (ACPI_SUCCESS(status)) {
200287a500cdSNate Lawson 	    DELAY(1000000);
20032d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - timeout\n");
2004ac731af5SJung-uk Kim 	} else if (status != AE_NOT_EXIST)
2005ac731af5SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - %s\n",
2006ac731af5SJung-uk Kim 		AcpiFormatException(status));
2007d85e6785SNate Lawson     } else if (sc->acpi_do_disable && panicstr == NULL) {
2008d85e6785SNate Lawson 	/*
2009d85e6785SNate Lawson 	 * Only disable ACPI if the user requested.  On some systems, writing
2010d85e6785SNate Lawson 	 * the disable value to SMI_CMD hangs the system.
2011d85e6785SNate Lawson 	 */
20122d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Shutting down\n");
201380f0e4c2SNate Lawson 	AcpiTerminate();
201480f0e4c2SNate Lawson     }
201515e32d5dSMike Smith }
201615e32d5dSMike Smith 
201713d4f7baSMitsuru IWASAKI static void
201813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
201913d4f7baSMitsuru IWASAKI {
202013d4f7baSMitsuru IWASAKI     static int	first_time = 1;
202113d4f7baSMitsuru IWASAKI 
202213d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
20232be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
202459ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
202513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
202633febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
2027be2b1797SNate Lawson 	if (first_time)
20286c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
202913d4f7baSMitsuru IWASAKI     }
20302be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
203159ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
203213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
203333febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
2034be2b1797SNate Lawson 	if (first_time)
20356c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
203613d4f7baSMitsuru IWASAKI     }
203713d4f7baSMitsuru IWASAKI 
203813d4f7baSMitsuru IWASAKI     first_time = 0;
203913d4f7baSMitsuru IWASAKI }
204013d4f7baSMitsuru IWASAKI 
204115e32d5dSMike Smith /*
2042c5ba0be4SMike Smith  * Returns true if the device is actually present and should
2043c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
2044c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
2045c5ba0be4SMike Smith  */
2046c5ba0be4SMike Smith BOOLEAN
2047c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
2048c5ba0be4SMike Smith {
20491e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
2050c5ba0be4SMike Smith     ACPI_HANDLE		h;
205192488a57SJung-uk Kim     BOOLEAN		present;
2052c5ba0be4SMike Smith 
205392488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
205492488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2055c5ba0be4SMike Smith 	return (FALSE);
2056be2b1797SNate Lawson 
2057be2b1797SNate Lawson     /* If no _STA method, must be present */
205892488a57SJung-uk Kim     present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
205992488a57SJung-uk Kim 	ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
2060be2b1797SNate Lawson 
206192488a57SJung-uk Kim     AcpiOsFree(devinfo);
206292488a57SJung-uk Kim     return (present);
2063c5ba0be4SMike Smith }
2064c5ba0be4SMike Smith 
2065c5ba0be4SMike Smith /*
2066b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
2067b53f2771SMike Smith  */
2068b53f2771SMike Smith BOOLEAN
2069b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
2070b53f2771SMike Smith {
20711e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
2072b53f2771SMike Smith     ACPI_HANDLE		h;
207392488a57SJung-uk Kim     BOOLEAN		present;
2074b53f2771SMike Smith 
207592488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
207692488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2077b53f2771SMike Smith 	return (FALSE);
2078be2b1797SNate Lawson 
2079be2b1797SNate Lawson     /* If no _STA method, must be present */
208092488a57SJung-uk Kim     present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
208192488a57SJung-uk Kim 	ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
2082be2b1797SNate Lawson 
208392488a57SJung-uk Kim     AcpiOsFree(devinfo);
208492488a57SJung-uk Kim     return (present);
2085b53f2771SMike Smith }
2086b53f2771SMike Smith 
2087b53f2771SMike Smith /*
2088183c8af3SJohn Baldwin  * Returns true if a device has at least one valid device ID.
2089183c8af3SJohn Baldwin  */
2090183c8af3SJohn Baldwin static BOOLEAN
2091183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h)
2092183c8af3SJohn Baldwin {
2093183c8af3SJohn Baldwin     ACPI_DEVICE_INFO	*devinfo;
2094183c8af3SJohn Baldwin     BOOLEAN		ret;
2095183c8af3SJohn Baldwin 
2096183c8af3SJohn Baldwin     if (h == NULL ||
2097183c8af3SJohn Baldwin 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2098183c8af3SJohn Baldwin 	return (FALSE);
2099183c8af3SJohn Baldwin 
2100183c8af3SJohn Baldwin     ret = FALSE;
2101183c8af3SJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2102183c8af3SJohn Baldwin 	ret = TRUE;
2103183c8af3SJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2104183c8af3SJohn Baldwin 	if (devinfo->CompatibleIdList.Count > 0)
2105183c8af3SJohn Baldwin 	    ret = TRUE;
2106183c8af3SJohn Baldwin 
2107183c8af3SJohn Baldwin     AcpiOsFree(devinfo);
2108183c8af3SJohn Baldwin     return (ret);
2109183c8af3SJohn Baldwin }
2110183c8af3SJohn Baldwin 
2111183c8af3SJohn Baldwin /*
211291233413SNate Lawson  * Match a HID string against a handle
211315e32d5dSMike Smith  */
21143c4c08dcSAlexander Motin BOOLEAN
2115ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
211615e32d5dSMike Smith {
21171e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
211892488a57SJung-uk Kim     BOOLEAN		ret;
211992488a57SJung-uk Kim     int			i;
212092488a57SJung-uk Kim 
212192488a57SJung-uk Kim     if (hid == NULL || h == NULL ||
212292488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
212392488a57SJung-uk Kim 	return (FALSE);
212415e32d5dSMike Smith 
21251e4925e8SNate Lawson     ret = FALSE;
2126c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
212792488a57SJung-uk Kim 	strcmp(hid, devinfo->HardwareId.String) == 0)
21281e4925e8SNate Lawson 	    ret = TRUE;
212992488a57SJung-uk Kim     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
213092488a57SJung-uk Kim 	for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
213192488a57SJung-uk Kim 	    if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
21321e4925e8SNate Lawson 		ret = TRUE;
21331e4925e8SNate Lawson 		break;
21341e4925e8SNate Lawson 	    }
21351e4925e8SNate Lawson 	}
2136be2b1797SNate Lawson 
213792488a57SJung-uk Kim     AcpiOsFree(devinfo);
21381e4925e8SNate Lawson     return (ret);
213915e32d5dSMike Smith }
214015e32d5dSMike Smith 
214115e32d5dSMike Smith /*
2142c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
2143c5ba0be4SMike Smith  * or one if its parents.
2144c5ba0be4SMike Smith  */
2145c5ba0be4SMike Smith ACPI_STATUS
2146c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2147c5ba0be4SMike Smith {
2148c5ba0be4SMike Smith     ACPI_HANDLE		r;
2149c5ba0be4SMike Smith     ACPI_STATUS		status;
2150c5ba0be4SMike Smith 
2151be2b1797SNate Lawson     /* Walk back up the tree to the root */
2152c5ba0be4SMike Smith     for (;;) {
2153be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
2154be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
2155c5ba0be4SMike Smith 	    *result = r;
2156c5ba0be4SMike Smith 	    return (AE_OK);
2157c5ba0be4SMike Smith 	}
2158bee4aa4aSNate Lawson 	/* XXX Return error here? */
2159c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
2160c5ba0be4SMike Smith 	    return (AE_OK);
2161b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2162c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
2163c5ba0be4SMike Smith 	parent = r;
2164c5ba0be4SMike Smith     }
2165c5ba0be4SMike Smith }
2166c5ba0be4SMike Smith 
2167c5ba0be4SMike Smith /*
2168c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
2169c5ba0be4SMike Smith  */
2170c5ba0be4SMike Smith ACPI_BUFFER *
2171c5ba0be4SMike Smith acpi_AllocBuffer(int size)
2172c5ba0be4SMike Smith {
2173c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
2174c5ba0be4SMike Smith 
2175c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2176c5ba0be4SMike Smith 	return (NULL);
2177c5ba0be4SMike Smith     buf->Length = size;
2178c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
2179c5ba0be4SMike Smith     return (buf);
2180c5ba0be4SMike Smith }
2181c5ba0be4SMike Smith 
2182c310653eSNate Lawson ACPI_STATUS
2183cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2184c310653eSNate Lawson {
2185c310653eSNate Lawson     ACPI_OBJECT arg1;
2186c310653eSNate Lawson     ACPI_OBJECT_LIST args;
2187c310653eSNate Lawson 
2188c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
2189c310653eSNate Lawson     arg1.Integer.Value = number;
2190c310653eSNate Lawson     args.Count = 1;
2191c310653eSNate Lawson     args.Pointer = &arg1;
2192c310653eSNate Lawson 
2193c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
2194c310653eSNate Lawson }
2195c310653eSNate Lawson 
2196c5ba0be4SMike Smith /*
2197c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
2198c5ba0be4SMike Smith  */
2199c5ba0be4SMike Smith ACPI_STATUS
2200cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2201c5ba0be4SMike Smith {
2202be2b1797SNate Lawson     ACPI_STATUS	status;
2203c5ba0be4SMike Smith     ACPI_BUFFER	buf;
2204c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
2205c5ba0be4SMike Smith 
2206c5ba0be4SMike Smith     if (handle == NULL)
2207c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
22080c7da7acSJohn Baldwin 
22090c7da7acSJohn Baldwin     /*
22100c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
22110c7da7acSJohn Baldwin      * a method that will return an Integer.
22120c7da7acSJohn Baldwin      */
2213c5ba0be4SMike Smith     buf.Pointer = &param;
2214c5ba0be4SMike Smith     buf.Length = sizeof(param);
2215be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
2216be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
2217be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
2218c5ba0be4SMike Smith 	    *number = param.Integer.Value;
2219be2b1797SNate Lawson 	else
2220be2b1797SNate Lawson 	    status = AE_TYPE;
2221c5ba0be4SMike Smith     }
22220c7da7acSJohn Baldwin 
22230c7da7acSJohn Baldwin     /*
22240c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
22250c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
22260c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
22270c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
22280c7da7acSJohn Baldwin      *
22290c7da7acSJohn Baldwin      * This is a hack.
22300c7da7acSJohn Baldwin      */
2231be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
2232b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2233be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
22340c7da7acSJohn Baldwin 	} else {
2235be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
2236be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
2237be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
22380c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
22390c7da7acSJohn Baldwin 	}
2240f97739daSNate Lawson     }
2241be2b1797SNate Lawson     return (status);
2242c5ba0be4SMike Smith }
2243c5ba0be4SMike Smith 
2244c573e654SMitsuru IWASAKI ACPI_STATUS
2245cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2246c573e654SMitsuru IWASAKI {
2247c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
2248dba55fa2SNate Lawson     UINT8	*val;
2249c573e654SMitsuru IWASAKI     int		i;
2250c573e654SMitsuru IWASAKI 
2251c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
2252c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
2253c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
2254c573e654SMitsuru IWASAKI 	return (AE_OK);
2255c573e654SMitsuru IWASAKI     }
2256c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
2257c573e654SMitsuru IWASAKI 	return (AE_TYPE);
2258c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
2259c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
2260be2b1797SNate Lawson 
2261c573e654SMitsuru IWASAKI     *number = 0;
2262dba55fa2SNate Lawson     val = p->Buffer.Pointer;
2263c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
2264dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
2265c573e654SMitsuru IWASAKI     return (AE_OK);
2266c573e654SMitsuru IWASAKI }
2267c573e654SMitsuru IWASAKI 
2268c5ba0be4SMike Smith /*
2269c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
2270c5ba0be4SMike Smith  * function for each element.
2271c5ba0be4SMike Smith  *
2272c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
2273c5ba0be4SMike Smith  */
2274c5ba0be4SMike Smith ACPI_STATUS
2275be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2276be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2277c5ba0be4SMike Smith {
2278c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
2279c5ba0be4SMike Smith     int		i;
2280c5ba0be4SMike Smith 
2281be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2282c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
2283c5ba0be4SMike Smith 
2284be2b1797SNate Lawson     /* Iterate over components */
2285be2b1797SNate Lawson     i = 0;
2286be2b1797SNate Lawson     comp = pkg->Package.Elements;
2287be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
2288c5ba0be4SMike Smith 	func(comp, arg);
2289c5ba0be4SMike Smith 
2290c5ba0be4SMike Smith     return (AE_OK);
2291c5ba0be4SMike Smith }
2292c5ba0be4SMike Smith 
22936f69255bSMike Smith /*
22946f69255bSMike Smith  * Find the (index)th resource object in a set.
22956f69255bSMike Smith  */
22966f69255bSMike Smith ACPI_STATUS
22976d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
22986f69255bSMike Smith {
22996d63101aSMike Smith     ACPI_RESOURCE	*rp;
23006f69255bSMike Smith     int			i;
23016f69255bSMike Smith 
23026d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
23036f69255bSMike Smith     i = index;
23046d63101aSMike Smith     while (i-- > 0) {
2305be2b1797SNate Lawson 	/* Range check */
23066d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
23076f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
2308be2b1797SNate Lawson 
2309be2b1797SNate Lawson 	/* Check for terminator */
2310e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
23116d63101aSMike Smith 	    return (AE_NOT_FOUND);
2312abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
23136f69255bSMike Smith     }
23146f69255bSMike Smith     if (resp != NULL)
23156d63101aSMike Smith 	*resp = rp;
2316be2b1797SNate Lawson 
23176d63101aSMike Smith     return (AE_OK);
23186d63101aSMike Smith }
23196d63101aSMike Smith 
23206d63101aSMike Smith /*
23216d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
23226d63101aSMike Smith  *
23236d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
23246d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
23256d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
23266d63101aSMike Smith  * resources.
23276d63101aSMike Smith  */
23286d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
23296d63101aSMike Smith 
23306d63101aSMike Smith ACPI_STATUS
23316d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
23326d63101aSMike Smith {
23336d63101aSMike Smith     ACPI_RESOURCE	*rp;
23346d63101aSMike Smith     void		*newp;
23356d63101aSMike Smith 
2336be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
23376d63101aSMike Smith     if (buf->Pointer == NULL) {
23386d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
23396d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
23406d63101aSMike Smith 	    return (AE_NO_MEMORY);
23416d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2342e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
23436d63101aSMike Smith 	rp->Length = 0;
23446d63101aSMike Smith     }
23456d63101aSMike Smith     if (res == NULL)
23466d63101aSMike Smith 	return (AE_OK);
23476d63101aSMike Smith 
23486d63101aSMike Smith     /*
23496d63101aSMike Smith      * Scan the current buffer looking for the terminator.
23506d63101aSMike Smith      * This will either find the terminator or hit the end
23516d63101aSMike Smith      * of the buffer and return an error.
23526d63101aSMike Smith      */
23536d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
23546d63101aSMike Smith     for (;;) {
2355be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
23566d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
23576d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2358e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
23596d63101aSMike Smith 	    break;
2360abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
23616d63101aSMike Smith     }
23626d63101aSMike Smith 
23636d63101aSMike Smith     /*
23646d63101aSMike Smith      * Check the size of the buffer and expand if required.
23656d63101aSMike Smith      *
23666d63101aSMike Smith      * Required size is:
23676d63101aSMike Smith      *	size of existing resources before terminator +
23686d63101aSMike Smith      *	size of new resource and header +
23696d63101aSMike Smith      * 	size of terminator.
23706d63101aSMike Smith      *
23716d63101aSMike Smith      * Note that this loop should really only run once, unless
23726d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
23736d63101aSMike Smith      */
23746d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2375e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2376e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
23776d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
23786d63101aSMike Smith 	    return (AE_NO_MEMORY);
23796d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2380a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2381a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
23826d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
23836d63101aSMike Smith 	buf->Pointer = newp;
23846d63101aSMike Smith 	buf->Length += buf->Length;
23856d63101aSMike Smith     }
23866d63101aSMike Smith 
2387be2b1797SNate Lawson     /* Insert the new resource. */
2388e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
23896d63101aSMike Smith 
2390be2b1797SNate Lawson     /* And add the terminator. */
2391abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2392e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
23936d63101aSMike Smith     rp->Length = 0;
23946d63101aSMike Smith 
23956f69255bSMike Smith     return (AE_OK);
23966f69255bSMike Smith }
2397c5ba0be4SMike Smith 
2398da14ac9fSJohn Baldwin /*
2399da14ac9fSJohn Baldwin  * Set interrupt model.
2400da14ac9fSJohn Baldwin  */
2401da14ac9fSJohn Baldwin ACPI_STATUS
2402da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2403da14ac9fSJohn Baldwin {
2404da14ac9fSJohn Baldwin 
2405c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2406da14ac9fSJohn Baldwin }
2407da14ac9fSJohn Baldwin 
240800a30448SNate Lawson /*
2409d95e7f5aSJohn Baldwin  * Walk subtables of a table and call a callback routine for each
2410d95e7f5aSJohn Baldwin  * subtable.  The caller should provide the first subtable and a
2411d95e7f5aSJohn Baldwin  * pointer to the end of the table.  This can be used to walk tables
2412d95e7f5aSJohn Baldwin  * such as MADT and SRAT that use subtable entries.
2413d95e7f5aSJohn Baldwin  */
2414d95e7f5aSJohn Baldwin void
2415d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2416d95e7f5aSJohn Baldwin     void *arg)
2417d95e7f5aSJohn Baldwin {
2418d95e7f5aSJohn Baldwin     ACPI_SUBTABLE_HEADER *entry;
2419d95e7f5aSJohn Baldwin 
2420d95e7f5aSJohn Baldwin     for (entry = first; (void *)entry < end; ) {
2421d95e7f5aSJohn Baldwin 	/* Avoid an infinite loop if we hit a bogus entry. */
2422d95e7f5aSJohn Baldwin 	if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2423d95e7f5aSJohn Baldwin 	    return;
2424d95e7f5aSJohn Baldwin 
2425d95e7f5aSJohn Baldwin 	handler(entry, arg);
2426d95e7f5aSJohn Baldwin 	entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2427d95e7f5aSJohn Baldwin     }
2428d95e7f5aSJohn Baldwin }
2429d95e7f5aSJohn Baldwin 
2430d95e7f5aSJohn Baldwin /*
243100a30448SNate Lawson  * DEPRECATED.  This interface has serious deficiencies and will be
243200a30448SNate Lawson  * removed.
243300a30448SNate Lawson  *
243400a30448SNate Lawson  * Immediately enter the sleep state.  In the old model, acpiconf(8) ran
243500a30448SNate Lawson  * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
243600a30448SNate Lawson  */
243700a30448SNate Lawson ACPI_STATUS
243800a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state)
243900a30448SNate Lawson {
244000a30448SNate Lawson     static int once;
244100a30448SNate Lawson 
244200a30448SNate Lawson     if (!once) {
24432d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
244400a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n");
244500a30448SNate Lawson 	once = 1;
244600a30448SNate Lawson     }
244700a30448SNate Lawson     return (acpi_EnterSleepState(sc, state));
244800a30448SNate Lawson }
244900a30448SNate Lawson 
2450c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
245100a30448SNate Lawson static void
2452ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context)
2453ffe3f1f8SMitsuru IWASAKI {
2454ffe3f1f8SMitsuru IWASAKI     struct acpi_softc *sc = (struct acpi_softc *)context;
2455ffe3f1f8SMitsuru IWASAKI 
2456ffe3f1f8SMitsuru IWASAKI     if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2457ffe3f1f8SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
2458ffe3f1f8SMitsuru IWASAKI 	    sc->acpi_next_sstate);
2459ffe3f1f8SMitsuru IWASAKI }
2460ffe3f1f8SMitsuru IWASAKI 
2461ffe3f1f8SMitsuru IWASAKI static void
246200a30448SNate Lawson acpi_sleep_force(void *arg)
246300a30448SNate Lawson {
24642d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
246500a30448SNate Lawson 
24662d0c82e8SJung-uk Kim     device_printf(sc->acpi_dev,
24672d0c82e8SJung-uk Kim 	"suspend request timed out, forcing sleep now\n");
2468ffe3f1f8SMitsuru IWASAKI     /*
2469ffe3f1f8SMitsuru IWASAKI      * XXX Suspending from callout cause the freeze in DEVICE_SUSPEND().
2470ffe3f1f8SMitsuru IWASAKI      * Suspend from acpi_task thread in stead.
2471ffe3f1f8SMitsuru IWASAKI      */
2472ffe3f1f8SMitsuru IWASAKI     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
2473ffe3f1f8SMitsuru IWASAKI 	acpi_sleep_force_task, sc)))
2474ffe3f1f8SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n");
247500a30448SNate Lawson }
2476c66d2b38SJung-uk Kim #endif
247700a30448SNate Lawson 
247800a30448SNate Lawson /*
247900a30448SNate Lawson  * Request that the system enter the given suspend state.  All /dev/apm
248000a30448SNate Lawson  * devices and devd(8) will be notified.  Userland then has a chance to
248100a30448SNate Lawson  * save state and acknowledge the request.  The system sleeps once all
248200a30448SNate Lawson  * acks are in.
248300a30448SNate Lawson  */
248400a30448SNate Lawson int
248500a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state)
248600a30448SNate Lawson {
248771f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
248800a30448SNate Lawson     struct apm_clone_data *clone;
2489337744d9SJung-uk Kim     ACPI_STATUS status;
249000a30448SNate Lawson 
2491a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
249200a30448SNate Lawson 	return (EINVAL);
2493a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state])
2494a0e73a12SJung-uk Kim 	return (EOPNOTSUPP);
249500a30448SNate Lawson 
2496b12bc99fSMitsuru IWASAKI     /* If a suspend request is already in progress, just return. */
2497b12bc99fSMitsuru IWASAKI     if (sc->acpi_next_sstate != 0) {
2498b12bc99fSMitsuru IWASAKI 	return (0);
2499b12bc99fSMitsuru IWASAKI     }
2500b12bc99fSMitsuru IWASAKI 
2501f22377a8SMitsuru IWASAKI     /* Wait until sleep is enabled. */
2502f22377a8SMitsuru IWASAKI     while (sc->acpi_sleep_disabled) {
2503f22377a8SMitsuru IWASAKI 	AcpiOsSleep(1000);
2504f22377a8SMitsuru IWASAKI     }
2505f22377a8SMitsuru IWASAKI 
2506337744d9SJung-uk Kim     ACPI_LOCK(acpi);
250700a30448SNate Lawson 
2508f22377a8SMitsuru IWASAKI     sc->acpi_next_sstate = state;
250900a30448SNate Lawson 
2510337744d9SJung-uk Kim     /* S5 (soft-off) should be entered directly with no waiting. */
2511337744d9SJung-uk Kim     if (state == ACPI_STATE_S5) {
2512337744d9SJung-uk Kim     	ACPI_UNLOCK(acpi);
2513337744d9SJung-uk Kim 	status = acpi_EnterSleepState(sc, state);
2514337744d9SJung-uk Kim 	return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2515337744d9SJung-uk Kim     }
2516337744d9SJung-uk Kim 
251700a30448SNate Lawson     /* Record the pending state and notify all apm devices. */
251800a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
251900a30448SNate Lawson 	clone->notify_status = APM_EV_NONE;
252000a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_DEVD) == 0) {
252100a30448SNate Lawson 	    selwakeuppri(&clone->sel_read, PZERO);
2522374d9c4dSJohn Baldwin 	    KNOTE_LOCKED(&clone->sel_read.si_note, 0);
252300a30448SNate Lawson 	}
252400a30448SNate Lawson     }
252500a30448SNate Lawson 
25260c26519eSMitsuru IWASAKI     /* If devd(8) is not running, immediately enter the sleep state. */
2527d42f0ad8SJung-uk Kim     if (!devctl_process_running()) {
25280c26519eSMitsuru IWASAKI 	ACPI_UNLOCK(acpi);
2529337744d9SJung-uk Kim 	status = acpi_EnterSleepState(sc, state);
2530337744d9SJung-uk Kim 	return (ACPI_SUCCESS(status) ? 0 : ENXIO);
25310c26519eSMitsuru IWASAKI     }
25320c26519eSMitsuru IWASAKI 
253300a30448SNate Lawson     /*
253400a30448SNate Lawson      * Set a timeout to fire if userland doesn't ack the suspend request
253500a30448SNate Lawson      * in time.  This way we still eventually go to sleep if we were
253600a30448SNate Lawson      * overheating or running low on battery, even if userland is hung.
253700a30448SNate Lawson      * We cancel this timeout once all userland acks are in or the
253800a30448SNate Lawson      * suspend request is aborted.
253900a30448SNate Lawson      */
254000a30448SNate Lawson     callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
254100a30448SNate Lawson     ACPI_UNLOCK(acpi);
2542d42f0ad8SJung-uk Kim 
2543d42f0ad8SJung-uk Kim     /* Now notify devd(8) also. */
2544d42f0ad8SJung-uk Kim     acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2545d42f0ad8SJung-uk Kim 
254600a30448SNate Lawson     return (0);
2547c66d2b38SJung-uk Kim #else
2548c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2549c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2550c66d2b38SJung-uk Kim #endif
255100a30448SNate Lawson }
255200a30448SNate Lawson 
255300a30448SNate Lawson /*
255400a30448SNate Lawson  * Acknowledge (or reject) a pending sleep state.  The caller has
255500a30448SNate Lawson  * prepared for suspend and is now ready for it to proceed.  If the
255600a30448SNate Lawson  * error argument is non-zero, it indicates suspend should be cancelled
255700a30448SNate Lawson  * and gives an errno value describing why.  Once all votes are in,
255800a30448SNate Lawson  * we suspend the system.
255900a30448SNate Lawson  */
256000a30448SNate Lawson int
256100a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error)
256200a30448SNate Lawson {
2563c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
256400a30448SNate Lawson     struct acpi_softc *sc;
256500a30448SNate Lawson     int ret, sleeping;
256600a30448SNate Lawson 
256700a30448SNate Lawson     /* If no pending sleep state, return an error. */
256800a30448SNate Lawson     ACPI_LOCK(acpi);
256900a30448SNate Lawson     sc = clone->acpi_sc;
257000a30448SNate Lawson     if (sc->acpi_next_sstate == 0) {
257100a30448SNate Lawson     	ACPI_UNLOCK(acpi);
257200a30448SNate Lawson 	return (ENXIO);
257300a30448SNate Lawson     }
257400a30448SNate Lawson 
257500a30448SNate Lawson     /* Caller wants to abort suspend process. */
257600a30448SNate Lawson     if (error) {
257700a30448SNate Lawson 	sc->acpi_next_sstate = 0;
257800a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
25792d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
25802d0c82e8SJung-uk Kim 	    "listener on %s cancelled the pending suspend\n",
258100a30448SNate Lawson 	    devtoname(clone->cdev));
258200a30448SNate Lawson     	ACPI_UNLOCK(acpi);
258300a30448SNate Lawson 	return (0);
258400a30448SNate Lawson     }
258500a30448SNate Lawson 
258600a30448SNate Lawson     /*
258700a30448SNate Lawson      * Mark this device as acking the suspend request.  Then, walk through
258800a30448SNate Lawson      * all devices, seeing if they agree yet.  We only count devices that
258900a30448SNate Lawson      * are writable since read-only devices couldn't ack the request.
259000a30448SNate Lawson      */
259100a30448SNate Lawson     sleeping = TRUE;
2592c66d2b38SJung-uk Kim     clone->notify_status = APM_EV_ACKED;
259300a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
259400a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
259500a30448SNate Lawson 	    clone->notify_status != APM_EV_ACKED) {
259600a30448SNate Lawson 	    sleeping = FALSE;
259700a30448SNate Lawson 	    break;
259800a30448SNate Lawson 	}
259900a30448SNate Lawson     }
260000a30448SNate Lawson 
260100a30448SNate Lawson     /* If all devices have voted "yes", we will suspend now. */
260200a30448SNate Lawson     if (sleeping)
260300a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
260400a30448SNate Lawson     ACPI_UNLOCK(acpi);
260500a30448SNate Lawson     ret = 0;
260600a30448SNate Lawson     if (sleeping) {
260700a30448SNate Lawson 	if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
260800a30448SNate Lawson 		ret = ENODEV;
260900a30448SNate Lawson     }
261000a30448SNate Lawson     return (ret);
2611c66d2b38SJung-uk Kim #else
2612c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2613c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2614c66d2b38SJung-uk Kim #endif
261500a30448SNate Lawson }
261600a30448SNate Lawson 
2617ece50487SMitsuru IWASAKI static void
2618ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2619ece50487SMitsuru IWASAKI {
2620d42f0ad8SJung-uk Kim     struct acpi_softc	*sc = (struct acpi_softc *)arg;
2621bee4aa4aSNate Lawson 
2622a0e73a12SJung-uk Kim     /* Reschedule if the system is not fully up and running. */
2623a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning) {
2624a0e73a12SJung-uk Kim 	timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2625a0e73a12SJung-uk Kim 	return;
2626a0e73a12SJung-uk Kim     }
2627a0e73a12SJung-uk Kim 
2628d42f0ad8SJung-uk Kim     ACPI_LOCK(acpi);
2629a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = FALSE;
2630d42f0ad8SJung-uk Kim     ACPI_UNLOCK(acpi);
2631d42f0ad8SJung-uk Kim }
2632d42f0ad8SJung-uk Kim 
2633d42f0ad8SJung-uk Kim static ACPI_STATUS
2634d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc)
2635d42f0ad8SJung-uk Kim {
2636d42f0ad8SJung-uk Kim     ACPI_STATUS		status;
2637d42f0ad8SJung-uk Kim 
2638a0e73a12SJung-uk Kim     /* Fail if the system is not fully up and running. */
2639a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning)
2640a0e73a12SJung-uk Kim 	return (AE_ERROR);
2641a0e73a12SJung-uk Kim 
2642d42f0ad8SJung-uk Kim     ACPI_LOCK(acpi);
2643d42f0ad8SJung-uk Kim     status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
2644a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
2645d42f0ad8SJung-uk Kim     ACPI_UNLOCK(acpi);
2646d42f0ad8SJung-uk Kim 
2647d42f0ad8SJung-uk Kim     return (status);
2648ece50487SMitsuru IWASAKI }
2649c30382dfSMitsuru IWASAKI 
265015e2f34fSNate Lawson enum acpi_sleep_state {
265115e2f34fSNate Lawson     ACPI_SS_NONE,
265215e2f34fSNate Lawson     ACPI_SS_GPE_SET,
265315e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
265415e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
265515e2f34fSNate Lawson     ACPI_SS_SLEPT,
265615e2f34fSNate Lawson };
265715e2f34fSNate Lawson 
265815e32d5dSMike Smith /*
265900a30448SNate Lawson  * Enter the desired system sleep state.
266015e32d5dSMike Smith  *
2661be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
266215e32d5dSMike Smith  */
266300a30448SNate Lawson static ACPI_STATUS
266400a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state)
266515e32d5dSMike Smith {
266671804adcSJung-uk Kim     register_t intr;
266715e2f34fSNate Lawson     ACPI_STATUS status;
266815e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
2669*f0a101b7SMitsuru IWASAKI     int sleep_result;
267015e32d5dSMike Smith 
2671b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
26720ae55423SMike Smith 
2673a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
267433c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_BAD_PARAMETER);
2675a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state]) {
2676a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
2677a0e73a12SJung-uk Kim 	    state);
2678a0e73a12SJung-uk Kim 	return (AE_SUPPORT);
2679a0e73a12SJung-uk Kim     }
2680a0e73a12SJung-uk Kim 
2681a0e73a12SJung-uk Kim     /* Re-entry once we're suspending is not allowed. */
2682a0e73a12SJung-uk Kim     status = acpi_sleep_disable(sc);
2683a0e73a12SJung-uk Kim     if (ACPI_FAILURE(status)) {
2684a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev,
2685a0e73a12SJung-uk Kim 	    "suspend request ignored (not ready yet)\n");
2686a0e73a12SJung-uk Kim 	return (status);
2687a0e73a12SJung-uk Kim     }
268833c70d41SAndriy Gapon 
268933c70d41SAndriy Gapon     if (state == ACPI_STATE_S5) {
269033c70d41SAndriy Gapon 	/*
269133c70d41SAndriy Gapon 	 * Shut down cleanly and power off.  This will call us back through the
269233c70d41SAndriy Gapon 	 * shutdown handlers.
269333c70d41SAndriy Gapon 	 */
269433c70d41SAndriy Gapon 	shutdown_nice(RB_POWEROFF);
269533c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_OK);
269633c70d41SAndriy Gapon     }
269733c70d41SAndriy Gapon 
26984a8fa6feSJung-uk Kim     EVENTHANDLER_INVOKE(power_suspend);
26994a8fa6feSJung-uk Kim 
270036a483bbSJung-uk Kim     if (smp_started) {
2701c66d2b38SJung-uk Kim 	thread_lock(curthread);
2702c66d2b38SJung-uk Kim 	sched_bind(curthread, 0);
2703c66d2b38SJung-uk Kim 	thread_unlock(curthread);
270436a483bbSJung-uk Kim     }
2705c66d2b38SJung-uk Kim 
2706a56fe095SJohn Baldwin     /*
2707a56fe095SJohn Baldwin      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
2708a56fe095SJohn Baldwin      * drivers need this.
2709a56fe095SJohn Baldwin      */
2710a56fe095SJohn Baldwin     mtx_lock(&Giant);
2711c66d2b38SJung-uk Kim 
271215e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
271356d8cb57SMitsuru IWASAKI 
2714a1fccb47SMitsuru IWASAKI     sc->acpi_sstate = state;
2715a1fccb47SMitsuru IWASAKI 
2716d4b9ff91SNate Lawson     /* Enable any GPEs as appropriate and requested by the user. */
2717d4b9ff91SNate Lawson     acpi_wake_prep_walk(state);
271815e2f34fSNate Lawson     slp_state = ACPI_SS_GPE_SET;
2719e8b4d56eSNate Lawson 
272015e32d5dSMike Smith     /*
2721c7f88fc3SNate Lawson      * Inform all devices that we are going to sleep.  If at least one
2722c7f88fc3SNate Lawson      * device fails, DEVICE_SUSPEND() automatically resumes the tree.
272315e32d5dSMike Smith      *
2724c7f88fc3SNate Lawson      * XXX Note that a better two-pass approach with a 'veto' pass
2725c7f88fc3SNate Lawson      * followed by a "real thing" pass would be better, but the current
2726c7f88fc3SNate Lawson      * bus interface does not provide for this.
272715e32d5dSMike Smith      */
272815e2f34fSNate Lawson     if (DEVICE_SUSPEND(root_bus) != 0) {
272915e2f34fSNate Lawson 	device_printf(sc->acpi_dev, "device_suspend failed\n");
273033c70d41SAndriy Gapon 	goto backout;
273115e2f34fSNate Lawson     }
273215e2f34fSNate Lawson     slp_state = ACPI_SS_DEV_SUSPEND;
2733b9c780d6SMitsuru IWASAKI 
273493bfd059SNate Lawson     /* If testing device suspend only, back out of everything here. */
273593bfd059SNate Lawson     if (acpi_susp_bounce)
273633c70d41SAndriy Gapon 	goto backout;
273793bfd059SNate Lawson 
2738be2b1797SNate Lawson     status = AcpiEnterSleepStatePrep(state);
2739be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
2740b9c780d6SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2741b9c780d6SMitsuru IWASAKI 		      AcpiFormatException(status));
274233c70d41SAndriy Gapon 	goto backout;
2743b9c780d6SMitsuru IWASAKI     }
274415e2f34fSNate Lawson     slp_state = ACPI_SS_SLP_PREP;
2745b9c780d6SMitsuru IWASAKI 
2746be2b1797SNate Lawson     if (sc->acpi_sleep_delay > 0)
2747ff01efb5SMitsuru IWASAKI 	DELAY(sc->acpi_sleep_delay * 1000000);
2748ff01efb5SMitsuru IWASAKI 
27496161544cSTakanori Watanabe     if (state != ACPI_STATE_S1) {
2750*f0a101b7SMitsuru IWASAKI 	intr = intr_disable();
2751*f0a101b7SMitsuru IWASAKI 	sleep_result = acpi_sleep_machdep(sc, state);
2752*f0a101b7SMitsuru IWASAKI 	acpi_wakeup_machdep(sc, state, sleep_result, 0);
2753*f0a101b7SMitsuru IWASAKI 	AcpiLeaveSleepStatePrep(state, acpi_sleep_flags);
2754*f0a101b7SMitsuru IWASAKI 	intr_restore(intr);
2755*f0a101b7SMitsuru IWASAKI 
2756*f0a101b7SMitsuru IWASAKI 	/* call acpi_wakeup_machdep() again with interrupt enabled */
2757*f0a101b7SMitsuru IWASAKI 	acpi_wakeup_machdep(sc, state, sleep_result, 1);
2758*f0a101b7SMitsuru IWASAKI 
2759*f0a101b7SMitsuru IWASAKI 	if (sleep_result == -1)
2760a159c266SJung-uk Kim 		goto backout;
27616161544cSTakanori Watanabe 
27626161544cSTakanori Watanabe 	/* Re-enable ACPI hardware on wakeup from sleep state 4. */
2763be2b1797SNate Lawson 	if (state == ACPI_STATE_S4)
2764964679ceSMitsuru IWASAKI 	    AcpiEnable();
27656161544cSTakanori Watanabe     } else {
276671804adcSJung-uk Kim 	intr = intr_disable();
27674c52cad2SJung-uk Kim 	status = AcpiEnterSleepState(state, acpi_sleep_flags);
276871804adcSJung-uk Kim 	intr_restore(intr);
2769be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2770be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2771be2b1797SNate Lawson 			  AcpiFormatException(status));
277233c70d41SAndriy Gapon 	    goto backout;
277315e32d5dSMike Smith 	}
27746161544cSTakanori Watanabe     }
277515e2f34fSNate Lawson     slp_state = ACPI_SS_SLEPT;
2776ece50487SMitsuru IWASAKI 
277715e2f34fSNate Lawson     /*
277815e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
277915e2f34fSNate Lawson      * process.  This handles both the error and success cases.
278015e2f34fSNate Lawson      */
278133c70d41SAndriy Gapon backout:
278215e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
278315e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
278415e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
278515e2f34fSNate Lawson     }
2786e3b56b66SMitsuru IWASAKI     if (slp_state >= ACPI_SS_DEV_SUSPEND)
2787e3b56b66SMitsuru IWASAKI 	DEVICE_RESUME(root_bus);
2788*f0a101b7SMitsuru IWASAKI     if (slp_state >= ACPI_SS_SLP_PREP)
278915e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
2790a0a15716SJung-uk Kim     if (slp_state >= ACPI_SS_SLEPT) {
2791a0a15716SJung-uk Kim 	acpi_resync_clock(sc);
279215e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
2793a0a15716SJung-uk Kim     }
2794337744d9SJung-uk Kim     sc->acpi_next_sstate = 0;
279515e2f34fSNate Lawson 
2796a56fe095SJohn Baldwin     mtx_unlock(&Giant);
2797c66d2b38SJung-uk Kim 
279836a483bbSJung-uk Kim     if (smp_started) {
2799c66d2b38SJung-uk Kim 	thread_lock(curthread);
2800c66d2b38SJung-uk Kim 	sched_unbind(curthread);
2801c66d2b38SJung-uk Kim 	thread_unlock(curthread);
280236a483bbSJung-uk Kim     }
2803c66d2b38SJung-uk Kim 
28044a8fa6feSJung-uk Kim     EVENTHANDLER_INVOKE(power_resume);
28054a8fa6feSJung-uk Kim 
2806d42f0ad8SJung-uk Kim     /* Allow another sleep request after a while. */
2807d42f0ad8SJung-uk Kim     timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2808d42f0ad8SJung-uk Kim 
2809d42f0ad8SJung-uk Kim     /* Run /etc/rc.resume after we are back. */
2810d42f0ad8SJung-uk Kim     if (devctl_process_running())
2811d42f0ad8SJung-uk Kim 	acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
2812d42f0ad8SJung-uk Kim 
28130ae55423SMike Smith     return_ACPI_STATUS (status);
281415e32d5dSMike Smith }
281515e32d5dSMike Smith 
2816a0a15716SJung-uk Kim static void
28170755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc)
28180755473bSJung-uk Kim {
2819a0a15716SJung-uk Kim #ifdef __amd64__
28200755473bSJung-uk Kim     if (!acpi_reset_clock)
28210755473bSJung-uk Kim 	return;
28220755473bSJung-uk Kim 
28230755473bSJung-uk Kim     /*
28240755473bSJung-uk Kim      * Warm up timecounter again and reset system clock.
28250755473bSJung-uk Kim      */
28260755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
28270755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
28280755473bSJung-uk Kim     inittodr(time_second + sc->acpi_sleep_delay);
2829a0a15716SJung-uk Kim #endif
28300755473bSJung-uk Kim }
28310755473bSJung-uk Kim 
2832e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
2833e8b4d56eSNate Lawson int
2834e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
2835e8b4d56eSNate Lawson {
2836e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2837e8b4d56eSNate Lawson     ACPI_STATUS status;
2838e8b4d56eSNate Lawson     int flags;
2839e8b4d56eSNate Lawson 
2840d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
28412be4e471SJung-uk Kim     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2842e8b4d56eSNate Lawson 	return (ENXIO);
2843e8b4d56eSNate Lawson 
2844d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
2845e8b4d56eSNate Lawson     if (enable) {
28465a77b11bSJung-uk Kim 	status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
28475a77b11bSJung-uk Kim 	    ACPI_GPE_ENABLE);
2848e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2849e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
2850e8b4d56eSNate Lawson 	    return (ENXIO);
2851e8b4d56eSNate Lawson 	}
2852d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2853e8b4d56eSNate Lawson     } else {
28545a77b11bSJung-uk Kim 	status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
28555a77b11bSJung-uk Kim 	    ACPI_GPE_DISABLE);
2856e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2857e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
2858e8b4d56eSNate Lawson 	    return (ENXIO);
2859e8b4d56eSNate Lawson 	}
2860d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2861e8b4d56eSNate Lawson     }
2862e8b4d56eSNate Lawson 
2863e8b4d56eSNate Lawson     return (0);
2864e8b4d56eSNate Lawson }
2865e8b4d56eSNate Lawson 
2866d4b9ff91SNate Lawson static int
2867d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2868e8b4d56eSNate Lawson {
2869e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2870d4b9ff91SNate Lawson     device_t dev;
2871e8b4d56eSNate Lawson 
2872d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
2873e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2874e8b4d56eSNate Lawson 	return (ENXIO);
2875d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2876e8b4d56eSNate Lawson 
2877e8b4d56eSNate Lawson     /*
2878d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
2879d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
2880d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
2881d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
2882d4b9ff91SNate Lawson      * and set _PSW.
2883e8b4d56eSNate Lawson      */
2884d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
28855a77b11bSJung-uk Kim 	AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
2886e8b4d56eSNate Lawson 	if (bootverbose)
2887d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2888d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2889d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2890d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
2891d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
2892d4b9ff91SNate Lawson 	if (bootverbose)
2893d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2894d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2895d4b9ff91SNate Lawson     }
2896cc85c78cSNate Lawson 
2897cc85c78cSNate Lawson     return (0);
2898e8b4d56eSNate Lawson }
2899e8b4d56eSNate Lawson 
2900d4b9ff91SNate Lawson static int
2901d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2902cc85c78cSNate Lawson {
2903cc85c78cSNate Lawson     struct acpi_prw_data prw;
2904d4b9ff91SNate Lawson     device_t dev;
2905cc85c78cSNate Lawson 
2906cc85c78cSNate Lawson     /*
2907d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
2908d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
2909cc85c78cSNate Lawson      */
2910d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2911d4b9ff91SNate Lawson 	return (ENXIO);
2912d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2913d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2914d4b9ff91SNate Lawson 	return (0);
2915cc85c78cSNate Lawson 
2916d4b9ff91SNate Lawson     /*
2917d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
2918d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
2919d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
2920d4b9ff91SNate Lawson      */
2921d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
29225a77b11bSJung-uk Kim 	AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
2923d4b9ff91SNate Lawson 	if (bootverbose)
2924d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2925d4b9ff91SNate Lawson     } else {
2926d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
2927d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
2928d4b9ff91SNate Lawson 	if (bootverbose)
2929d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
2930d4b9ff91SNate Lawson 		acpi_name(handle));
2931d4b9ff91SNate Lawson     }
2932d4b9ff91SNate Lawson 
2933e8b4d56eSNate Lawson     return (0);
2934e8b4d56eSNate Lawson }
2935e8b4d56eSNate Lawson 
2936e8b4d56eSNate Lawson static ACPI_STATUS
2937d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2938e8b4d56eSNate Lawson {
2939d4b9ff91SNate Lawson     int sstate;
2940e8b4d56eSNate Lawson 
2941d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
2942d4b9ff91SNate Lawson     sstate = *(int *)context;
2943d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
2944d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
2945d4b9ff91SNate Lawson     else
2946d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
2947e8b4d56eSNate Lawson     return (AE_OK);
2948e8b4d56eSNate Lawson }
2949e8b4d56eSNate Lawson 
2950d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2951e8b4d56eSNate Lawson static int
2952d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
2953e8b4d56eSNate Lawson {
2954e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
2955e8b4d56eSNate Lawson 
2956e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2957d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
29582272d050SJung-uk Kim 	    acpi_wake_prep, NULL, &sstate, NULL);
2959e8b4d56eSNate Lawson     return (0);
2960e8b4d56eSNate Lawson }
2961e8b4d56eSNate Lawson 
296288a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
296388a79fc0SNate Lawson static int
296488a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
296588a79fc0SNate Lawson {
296688a79fc0SNate Lawson     int error, i, numdevs;
296788a79fc0SNate Lawson     device_t *devlist;
296888a79fc0SNate Lawson     device_t child;
2969d4b9ff91SNate Lawson     ACPI_STATUS status;
297088a79fc0SNate Lawson 
297188a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
29721c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
29731c9ec538SNate Lawson 	if (numdevs == 0)
29741c9ec538SNate Lawson 	    free(devlist, M_TEMP);
297588a79fc0SNate Lawson 	return (error);
29761c9ec538SNate Lawson     }
297788a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
297888a79fc0SNate Lawson 	child = devlist[i];
2979d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
298088a79fc0SNate Lawson 	if (!device_is_attached(child))
298188a79fc0SNate Lawson 	    continue;
2982d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2983d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
298488a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
298588a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
298688a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
298788a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
298888a79fc0SNate Lawson 	}
298988a79fc0SNate Lawson     }
299088a79fc0SNate Lawson     free(devlist, M_TEMP);
299188a79fc0SNate Lawson 
299288a79fc0SNate Lawson     return (0);
299388a79fc0SNate Lawson }
299488a79fc0SNate Lawson 
299588a79fc0SNate Lawson /* Enable or disable wake from userland. */
299688a79fc0SNate Lawson static int
299788a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
299888a79fc0SNate Lawson {
299988a79fc0SNate Lawson     int enable, error;
300088a79fc0SNate Lawson     device_t dev;
300188a79fc0SNate Lawson 
300288a79fc0SNate Lawson     dev = (device_t)arg1;
3003d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
300488a79fc0SNate Lawson 
300588a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
300688a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
300788a79fc0SNate Lawson 	return (error);
300888a79fc0SNate Lawson     if (enable != 0 && enable != 1)
300988a79fc0SNate Lawson 	return (EINVAL);
301088a79fc0SNate Lawson 
301188a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
301288a79fc0SNate Lawson }
301388a79fc0SNate Lawson 
3014e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
3015d4b9ff91SNate Lawson int
3016e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
3017e8b4d56eSNate Lawson {
3018e8b4d56eSNate Lawson     ACPI_STATUS			status;
3019e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
3020e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
3021d4b9ff91SNate Lawson     int				error, i, power_count;
3022e8b4d56eSNate Lawson 
3023e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
3024e8b4d56eSNate Lawson 	return (EINVAL);
3025e8b4d56eSNate Lawson 
3026e8b4d56eSNate Lawson     /*
3027e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
3028e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
3029e8b4d56eSNate Lawson      */
3030e8b4d56eSNate Lawson     error = EINVAL;
3031e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
3032e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
3033e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
3034e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
3035e8b4d56eSNate Lawson 	return (ENOENT);
3036e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
3037e8b4d56eSNate Lawson     if (res == NULL)
3038e8b4d56eSNate Lawson 	return (ENOENT);
3039e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
3040e8b4d56eSNate Lawson 	goto out;
3041e8b4d56eSNate Lawson 
3042e8b4d56eSNate Lawson     /*
3043e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
3044e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
3045e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
3046e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
3047e8b4d56eSNate Lawson      */
3048e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
3049e8b4d56eSNate Lawson 	goto out;
3050e8b4d56eSNate Lawson 
3051e8b4d56eSNate Lawson     /*
3052e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
3053e8b4d56eSNate Lawson      */
3054e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
3055e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
3056e8b4d56eSNate Lawson 	/*
3057e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
3058e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
3059e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
3060e8b4d56eSNate Lawson 	 * enabled for the wake event.
3061e8b4d56eSNate Lawson 	 */
3062e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
3063e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
3064e8b4d56eSNate Lawson 	error = 0;
3065e8b4d56eSNate Lawson 	break;
3066e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
3067e8b4d56eSNate Lawson 	/*
3068e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
3069e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
3070e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
3071e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
3072e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
3073e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
3074e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
3075e8b4d56eSNate Lawson 	 * the wake event.
3076e8b4d56eSNate Lawson 	 *
3077e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
3078e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
3079e8b4d56eSNate Lawson 	 */
3080e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
3081e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
3082e8b4d56eSNate Lawson 	    goto out;
3083e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3084e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
3085e8b4d56eSNate Lawson 	    goto out;
3086e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3087e8b4d56eSNate Lawson 	    goto out;
3088e8b4d56eSNate Lawson 	error = 0;
3089e8b4d56eSNate Lawson 	break;
3090e8b4d56eSNate Lawson     default:
3091e8b4d56eSNate Lawson 	goto out;
3092e8b4d56eSNate Lawson     }
3093e8b4d56eSNate Lawson 
3094d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
3095d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
3096d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
3097d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
3098d4b9ff91SNate Lawson 	power_count = 0;
3099d4b9ff91SNate Lawson     }
3100d4b9ff91SNate Lawson     prw->power_res_count = power_count;
3101d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
3102d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
3103e8b4d56eSNate Lawson 
3104e8b4d56eSNate Lawson out:
3105e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
3106e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
3107e8b4d56eSNate Lawson     return (error);
3108e8b4d56eSNate Lawson }
3109e8b4d56eSNate Lawson 
311015e32d5dSMike Smith /*
311115e32d5dSMike Smith  * ACPI Event Handlers
311215e32d5dSMike Smith  */
311315e32d5dSMike Smith 
311415e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
311515e32d5dSMike Smith 
311615e32d5dSMike Smith static void
311715e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
311815e32d5dSMike Smith {
31192d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
312000a30448SNate Lawson     int ret;
3121bee4aa4aSNate Lawson 
3122b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
312315e32d5dSMike Smith 
3124a0e73a12SJung-uk Kim     /* Check if button action is disabled or unknown. */
3125a0e73a12SJung-uk Kim     if (state == ACPI_STATE_UNKNOWN)
3126813d6dcaSNate Lawson 	return;
3127813d6dcaSNate Lawson 
312800a30448SNate Lawson     /* Request that the system prepare to enter the given suspend state. */
31292d0c82e8SJung-uk Kim     ret = acpi_ReqSleepState(sc, state);
313000a30448SNate Lawson     if (ret != 0)
31312d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
31322d0c82e8SJung-uk Kim 	    "request to enter state S%d failed (err %d)\n", state, ret);
3133bee4aa4aSNate Lawson 
31340ae55423SMike Smith     return_VOID;
313515e32d5dSMike Smith }
313615e32d5dSMike Smith 
313715e32d5dSMike Smith static void
313815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
313915e32d5dSMike Smith {
3140bee4aa4aSNate Lawson 
3141b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
31420ae55423SMike Smith 
3143bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
3144cb9b0d80SMike Smith 
31450ae55423SMike Smith     return_VOID;
314615e32d5dSMike Smith }
314715e32d5dSMike Smith 
314815e32d5dSMike Smith /*
314915e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
315015e32d5dSMike Smith  */
3151b5854f5fSJung-uk Kim static void
3152b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context)
3153b5854f5fSJung-uk Kim {
3154b5854f5fSJung-uk Kim 
3155b5854f5fSJung-uk Kim     EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context);
3156b5854f5fSJung-uk Kim }
3157b5854f5fSJung-uk Kim 
3158b5854f5fSJung-uk Kim static void
3159b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context)
3160b5854f5fSJung-uk Kim {
3161b5854f5fSJung-uk Kim 
3162b5854f5fSJung-uk Kim     EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context);
3163b5854f5fSJung-uk Kim }
3164b5854f5fSJung-uk Kim 
316515e32d5dSMike Smith UINT32
316633febf93SNate Lawson acpi_event_power_button_sleep(void *context)
316715e32d5dSMike Smith {
316815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
316915e32d5dSMike Smith 
3170b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
31710ae55423SMike Smith 
3172b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3173b5854f5fSJung-uk Kim 	acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx)))
3174b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3175b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
317615e32d5dSMike Smith }
317715e32d5dSMike Smith 
317815e32d5dSMike Smith UINT32
317933febf93SNate Lawson acpi_event_power_button_wake(void *context)
318015e32d5dSMike Smith {
318115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
318215e32d5dSMike Smith 
3183b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
31840ae55423SMike Smith 
3185b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3186b5854f5fSJung-uk Kim 	acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx)))
3187b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3188b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
318915e32d5dSMike Smith }
319015e32d5dSMike Smith 
319115e32d5dSMike Smith UINT32
319233febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
319315e32d5dSMike Smith {
319415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
319515e32d5dSMike Smith 
3196b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
31970ae55423SMike Smith 
3198b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3199b5854f5fSJung-uk Kim 	acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx)))
3200b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3201b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
320215e32d5dSMike Smith }
320315e32d5dSMike Smith 
320415e32d5dSMike Smith UINT32
320533febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
320615e32d5dSMike Smith {
320715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
320815e32d5dSMike Smith 
3209b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
32100ae55423SMike Smith 
3211b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3212b5854f5fSJung-uk Kim 	acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx)))
3213b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3214b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
321515e32d5dSMike Smith }
321615e32d5dSMike Smith 
321715e32d5dSMike Smith /*
3218bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
3219bee4aa4aSNate Lawson  * use this for single-threaded callers.
322015e32d5dSMike Smith  */
322115e32d5dSMike Smith char *
322215e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
322315e32d5dSMike Smith {
3224bee4aa4aSNate Lawson     ACPI_BUFFER buf;
3225bee4aa4aSNate Lawson     static char data[256];
322615e32d5dSMike Smith 
3227bee4aa4aSNate Lawson     buf.Length = sizeof(data);
3228bee4aa4aSNate Lawson     buf.Pointer = data;
3229cb9b0d80SMike Smith 
32300a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3231bee4aa4aSNate Lawson 	return (data);
3232bee4aa4aSNate Lawson     return ("(unknown)");
323315e32d5dSMike Smith }
323415e32d5dSMike Smith 
323515e32d5dSMike Smith /*
323615e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
323715e32d5dSMike Smith  * parts of the namespace.
323815e32d5dSMike Smith  */
323915e32d5dSMike Smith int
324015e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
324115e32d5dSMike Smith {
32423c600cfbSJohn Baldwin     char	*cp, *env, *np;
324315e32d5dSMike Smith     int		len;
324415e32d5dSMike Smith 
324515e32d5dSMike Smith     np = acpi_name(handle);
324615e32d5dSMike Smith     if (*np == '\\')
324715e32d5dSMike Smith 	np++;
32483c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
324915e32d5dSMike Smith 	return (0);
325015e32d5dSMike Smith 
3251be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
32523c600cfbSJohn Baldwin     cp = env;
325315e32d5dSMike Smith     for (;;) {
3254bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
325515e32d5dSMike Smith 	    cp++;
325615e32d5dSMike Smith 	if (*cp == 0)
325715e32d5dSMike Smith 	    break;
325815e32d5dSMike Smith 	len = 0;
3259bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
326015e32d5dSMike Smith 	    len++;
3261d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
32623c600cfbSJohn Baldwin 	    freeenv(env);
32630ae55423SMike Smith 	    return(1);
3264d786139cSMaxime Henrion 	}
32650ae55423SMike Smith 	cp += len;
32660ae55423SMike Smith     }
32673c600cfbSJohn Baldwin     freeenv(env);
3268be2b1797SNate Lawson 
32690ae55423SMike Smith     return (0);
32700ae55423SMike Smith }
32710ae55423SMike Smith 
32720ae55423SMike Smith /*
32730ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
32740ae55423SMike Smith  */
32750ae55423SMike Smith int
32760ae55423SMike Smith acpi_disabled(char *subsys)
32770ae55423SMike Smith {
327878689b15SMaxime Henrion     char	*cp, *env;
32790ae55423SMike Smith     int		len;
32800ae55423SMike Smith 
32813184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
32820ae55423SMike Smith 	return (0);
32833184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
328478689b15SMaxime Henrion 	freeenv(env);
32850ae55423SMike Smith 	return (1);
3286d786139cSMaxime Henrion     }
32870ae55423SMike Smith 
32883184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
328978689b15SMaxime Henrion     cp = env;
32900ae55423SMike Smith     for (;;) {
32913184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
32920ae55423SMike Smith 	    cp++;
32933184cf5aSNate Lawson 	if (*cp == '\0')
32940ae55423SMike Smith 	    break;
32950ae55423SMike Smith 	len = 0;
32963184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
32970ae55423SMike Smith 	    len++;
32983184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
329978689b15SMaxime Henrion 	    freeenv(env);
330015e32d5dSMike Smith 	    return (1);
3301d786139cSMaxime Henrion 	}
330215e32d5dSMike Smith 	cp += len;
330315e32d5dSMike Smith     }
330478689b15SMaxime Henrion     freeenv(env);
3305be2b1797SNate Lawson 
330615e32d5dSMike Smith     return (0);
330715e32d5dSMike Smith }
330815e32d5dSMike Smith 
330915e32d5dSMike Smith /*
331015e32d5dSMike Smith  * Control interface.
331115e32d5dSMike Smith  *
33120ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
3313be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
3314be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
331515e32d5dSMike Smith  */
33160ae55423SMike Smith struct acpi_ioctl_hook
33170ae55423SMike Smith {
33180ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
33190ae55423SMike Smith     u_long			 cmd;
3320be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
33210ae55423SMike Smith     void			 *arg;
33220ae55423SMike Smith };
33230ae55423SMike Smith 
33240ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
33250ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
33260ae55423SMike Smith 
33270ae55423SMike Smith int
3328be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
33290ae55423SMike Smith {
33300ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
33310ae55423SMike Smith 
33320ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
33330ae55423SMike Smith 	return (ENOMEM);
33340ae55423SMike Smith     hp->cmd = cmd;
33350ae55423SMike Smith     hp->fn = fn;
33360ae55423SMike Smith     hp->arg = arg;
333715e2f34fSNate Lawson 
333815e2f34fSNate Lawson     ACPI_LOCK(acpi);
33390ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
33400ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
33410ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
33420ae55423SMike Smith     }
33430ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
334415e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
334515e2f34fSNate Lawson 
33460ae55423SMike Smith     return (0);
33470ae55423SMike Smith }
33480ae55423SMike Smith 
33490ae55423SMike Smith void
3350be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
33510ae55423SMike Smith {
33520ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
33530ae55423SMike Smith 
335415e2f34fSNate Lawson     ACPI_LOCK(acpi);
33550ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
335615e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
33570ae55423SMike Smith 	    break;
33580ae55423SMike Smith 
33590ae55423SMike Smith     if (hp != NULL) {
33600ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
33610ae55423SMike Smith 	free(hp, M_ACPIDEV);
33620ae55423SMike Smith     }
336315e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
33640ae55423SMike Smith }
33650ae55423SMike Smith 
336615e32d5dSMike Smith static int
336700b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
336815e32d5dSMike Smith {
336915e32d5dSMike Smith     return (0);
337015e32d5dSMike Smith }
337115e32d5dSMike Smith 
337215e32d5dSMike Smith static int
337300b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
337415e32d5dSMike Smith {
337515e32d5dSMike Smith     return (0);
337615e32d5dSMike Smith }
337715e32d5dSMike Smith 
337815e32d5dSMike Smith static int
337900b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
338015e32d5dSMike Smith {
338115e32d5dSMike Smith     struct acpi_softc		*sc;
33820ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
3383bee4aa4aSNate Lawson     int				error, state;
338415e32d5dSMike Smith 
3385a2e35898SDavid E. O'Brien     error = 0;
3386a2e35898SDavid E. O'Brien     hp = NULL;
338715e32d5dSMike Smith     sc = dev->si_drv1;
338815e32d5dSMike Smith 
33890ae55423SMike Smith     /*
33900ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
33910ae55423SMike Smith      */
339215e2f34fSNate Lawson     ACPI_LOCK(acpi);
3393bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
33940ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3395bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
3396bee4aa4aSNate Lawson 		break;
33970ae55423SMike Smith 	}
339815e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
3399bee4aa4aSNate Lawson     if (hp)
3400bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
34010ae55423SMike Smith 
34020ae55423SMike Smith     /*
3403a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
3404a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
3405a89fcf28STakanori Watanabe      * Not changing system behavior.
3406a89fcf28STakanori Watanabe      */
3407be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
3408be2b1797SNate Lawson 	return (EPERM);
3409a89fcf28STakanori Watanabe 
3410be2b1797SNate Lawson     /* Core system ioctls. */
341115e32d5dSMike Smith     switch (cmd) {
341200a30448SNate Lawson     case ACPIIO_REQSLPSTATE:
341300a30448SNate Lawson 	state = *(int *)addr;
341400a30448SNate Lawson 	if (state != ACPI_STATE_S5)
3415a0e73a12SJung-uk Kim 	    return (acpi_ReqSleepState(sc, state));
3416a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
3417a0e73a12SJung-uk Kim 	error = EOPNOTSUPP;
341800a30448SNate Lawson 	break;
341900a30448SNate Lawson     case ACPIIO_ACKSLPSTATE:
342000a30448SNate Lawson 	error = *(int *)addr;
342100a30448SNate Lawson 	error = acpi_AckSleepState(sc->acpi_clone, error);
342200a30448SNate Lawson 	break;
342300a30448SNate Lawson     case ACPIIO_SETSLPSTATE:	/* DEPRECATED */
342415e32d5dSMike Smith 	state = *(int *)addr;
3425a0e73a12SJung-uk Kim 	if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
3426a0e73a12SJung-uk Kim 	    return (EINVAL);
3427a0e73a12SJung-uk Kim 	if (!acpi_sleep_states[state])
3428a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3429a0e73a12SJung-uk Kim 	if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
3430a0e73a12SJung-uk Kim 	    error = ENXIO;
343115e32d5dSMike Smith 	break;
343215e32d5dSMike Smith     default:
3433bee4aa4aSNate Lawson 	error = ENXIO;
343415e32d5dSMike Smith 	break;
343515e32d5dSMike Smith     }
3436917d44c8SMitsuru IWASAKI 
343715e32d5dSMike Smith     return (error);
343815e32d5dSMike Smith }
343915e32d5dSMike Smith 
34401d073b1dSJohn Baldwin static int
3441a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname)
3442a0e73a12SJung-uk Kim {
3443a0e73a12SJung-uk Kim     int sstate;
3444a0e73a12SJung-uk Kim 
3445a0e73a12SJung-uk Kim     if (toupper(sname[0]) == 'S') {
3446a0e73a12SJung-uk Kim 	sstate = sname[1] - '0';
3447a0e73a12SJung-uk Kim 	if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
3448a0e73a12SJung-uk Kim 	    sname[2] == '\0')
3449a0e73a12SJung-uk Kim 	    return (sstate);
3450a0e73a12SJung-uk Kim     } else if (strcasecmp(sname, "NONE") == 0)
3451a0e73a12SJung-uk Kim 	return (ACPI_STATE_UNKNOWN);
3452a0e73a12SJung-uk Kim     return (-1);
3453a0e73a12SJung-uk Kim }
3454a0e73a12SJung-uk Kim 
3455a0e73a12SJung-uk Kim static const char *
3456a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate)
3457a0e73a12SJung-uk Kim {
3458a0e73a12SJung-uk Kim     static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
3459a0e73a12SJung-uk Kim 
3460a0e73a12SJung-uk Kim     if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
3461a0e73a12SJung-uk Kim 	return (snames[sstate]);
3462a0e73a12SJung-uk Kim     else if (sstate == ACPI_STATE_UNKNOWN)
3463a0e73a12SJung-uk Kim 	return ("NONE");
3464a0e73a12SJung-uk Kim     return (NULL);
3465a0e73a12SJung-uk Kim }
3466a0e73a12SJung-uk Kim 
3467a0e73a12SJung-uk Kim static int
3468d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3469d75de536SMitsuru IWASAKI {
3470d75de536SMitsuru IWASAKI     int error;
3471bee4aa4aSNate Lawson     struct sbuf sb;
3472a0e73a12SJung-uk Kim     UINT8 state;
3473d75de536SMitsuru IWASAKI 
3474bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3475a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
3476a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state])
3477a0e73a12SJung-uk Kim 	    sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
3478bee4aa4aSNate Lawson     sbuf_trim(&sb);
3479bee4aa4aSNate Lawson     sbuf_finish(&sb);
3480bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3481bee4aa4aSNate Lawson     sbuf_delete(&sb);
3482d75de536SMitsuru IWASAKI     return (error);
3483d75de536SMitsuru IWASAKI }
3484d75de536SMitsuru IWASAKI 
3485d75de536SMitsuru IWASAKI static int
34861d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
34871d073b1dSJohn Baldwin {
34881d073b1dSJohn Baldwin     char sleep_state[10];
3489a0e73a12SJung-uk Kim     int error, new_state, old_state;
34901d073b1dSJohn Baldwin 
3491a0e73a12SJung-uk Kim     old_state = *(int *)oidp->oid_arg1;
3492a0e73a12SJung-uk Kim     strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
34931d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
34941d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
3495a0e73a12SJung-uk Kim 	new_state = acpi_sname2sstate(sleep_state);
3496a0e73a12SJung-uk Kim 	if (new_state < ACPI_STATE_S1)
3497a0e73a12SJung-uk Kim 	    return (EINVAL);
349854b43614SJung-uk Kim 	if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
3499a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3500be2b1797SNate Lawson 	if (new_state != old_state)
3501a0e73a12SJung-uk Kim 	    *(int *)oidp->oid_arg1 = new_state;
35021d073b1dSJohn Baldwin     }
35031d073b1dSJohn Baldwin     return (error);
35041d073b1dSJohn Baldwin }
35051d073b1dSJohn Baldwin 
35069b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
35079b937d48SNate Lawson void
35089b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
35099b937d48SNate Lawson {
35109b937d48SNate Lawson     char		notify_buf[16];
35119b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
35129b937d48SNate Lawson     ACPI_STATUS		status;
35139b937d48SNate Lawson 
35149b937d48SNate Lawson     if (subsystem == NULL)
35159b937d48SNate Lawson 	return;
35169b937d48SNate Lawson 
35179b937d48SNate Lawson     handle_buf.Pointer = NULL;
35189b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
35199b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
35209b937d48SNate Lawson     if (ACPI_FAILURE(status))
35219b937d48SNate Lawson 	return;
35229b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
35239b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
35249b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
35259b937d48SNate Lawson }
35269b937d48SNate Lawson 
352715e32d5dSMike Smith #ifdef ACPI_DEBUG
35280ae55423SMike Smith /*
35290ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
35300ae55423SMike Smith  *
35310ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
35320ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
35330ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
35340ae55423SMike Smith  * prefixing the bit name with !.
35350ae55423SMike Smith  */
353615e32d5dSMike Smith struct debugtag
353715e32d5dSMike Smith {
353815e32d5dSMike Smith     char	*name;
353915e32d5dSMike Smith     UINT32	value;
354015e32d5dSMike Smith };
354115e32d5dSMike Smith 
354215e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
3543c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
3544c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
3545c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
3546c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
3547c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
3548c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
3549c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
3550c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
3551c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
3552d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
35534c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
3554d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
3555297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
35564c1cdee6SMike Smith 
3557c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
3558c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
35593184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
3560c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
35613184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
35623184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
35633184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
35644c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
3565cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
35663184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
3567b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
356815e32d5dSMike Smith     {NULL, 0}
356915e32d5dSMike Smith };
357015e32d5dSMike Smith 
357115e32d5dSMike Smith static struct debugtag dbg_level[] = {
35729501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
35734c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
35749501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
3575cc6455afSJung-uk Kim     {"ACPI_LV_REPAIR",		ACPI_LV_REPAIR},
35764c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
3577d62ab2f4SMitsuru IWASAKI 
3578d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
3579297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
35804c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
35814c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
3582d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
35834c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
35844c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
35854c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
35864c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
35874c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
35884c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
35894c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
35904c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
35914c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
35924c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
3593d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
3594d62ab2f4SMitsuru IWASAKI 
3595d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
3596d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
3597d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
3598d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
3599d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
36004c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
3601d62ab2f4SMitsuru IWASAKI 
3602d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3603d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
3604d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
3605d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
3606d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
3607d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
3608d62ab2f4SMitsuru IWASAKI 
3609d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
361098479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
361198479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
361298479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
361398479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
361498479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
361515e32d5dSMike Smith     {NULL, 0}
361615e32d5dSMike Smith };
361715e32d5dSMike Smith 
361815e32d5dSMike Smith static void
361915e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
362015e32d5dSMike Smith {
362115e32d5dSMike Smith     char	*ep;
362215e32d5dSMike Smith     int		i, l;
36230ae55423SMike Smith     int		set;
362415e32d5dSMike Smith 
362515e32d5dSMike Smith     while (*cp) {
362615e32d5dSMike Smith 	if (isspace(*cp)) {
362715e32d5dSMike Smith 	    cp++;
362815e32d5dSMike Smith 	    continue;
362915e32d5dSMike Smith 	}
363015e32d5dSMike Smith 	ep = cp;
363115e32d5dSMike Smith 	while (*ep && !isspace(*ep))
363215e32d5dSMike Smith 	    ep++;
36330ae55423SMike Smith 	if (*cp == '!') {
36340ae55423SMike Smith 	    set = 0;
36350ae55423SMike Smith 	    cp++;
36360ae55423SMike Smith 	    if (cp == ep)
36370ae55423SMike Smith 		continue;
36380ae55423SMike Smith 	} else {
36390ae55423SMike Smith 	    set = 1;
36400ae55423SMike Smith 	}
364115e32d5dSMike Smith 	l = ep - cp;
364215e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
364315e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
3644be2b1797SNate Lawson 		if (set)
364515e32d5dSMike Smith 		    *flag |= tag[i].value;
3646be2b1797SNate Lawson 		else
36470ae55423SMike Smith 		    *flag &= ~tag[i].value;
364815e32d5dSMike Smith 	    }
364915e32d5dSMike Smith 	}
365015e32d5dSMike Smith 	cp = ep;
365115e32d5dSMike Smith     }
365215e32d5dSMike Smith }
365315e32d5dSMike Smith 
365415e32d5dSMike Smith static void
36552a4ac806SMike Smith acpi_set_debugging(void *junk)
365615e32d5dSMike Smith {
36577e639165SNate Lawson     char	*layer, *level;
365815e32d5dSMike Smith 
36591d7b121cSNate Lawson     if (cold) {
36600ae55423SMike Smith 	AcpiDbgLayer = 0;
36610ae55423SMike Smith 	AcpiDbgLevel = 0;
36621d7b121cSNate Lawson     }
36631d7b121cSNate Lawson 
36647e639165SNate Lawson     layer = getenv("debug.acpi.layer");
36657e639165SNate Lawson     level = getenv("debug.acpi.level");
36667e639165SNate Lawson     if (layer == NULL && level == NULL)
36677e639165SNate Lawson 	return;
36680ae55423SMike Smith 
36697e639165SNate Lawson     printf("ACPI set debug");
36707e639165SNate Lawson     if (layer != NULL) {
36717e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
36727e639165SNate Lawson 	    printf(" layer '%s'", layer);
36737e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
36747e639165SNate Lawson 	freeenv(layer);
36751d7b121cSNate Lawson     }
36767e639165SNate Lawson     if (level != NULL) {
36777e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
36787e639165SNate Lawson 	    printf(" level '%s'", level);
36797e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
36807e639165SNate Lawson 	freeenv(level);
36817e639165SNate Lawson     }
36827e639165SNate Lawson     printf("\n");
368315e32d5dSMike Smith }
3684bee4aa4aSNate Lawson 
3685be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3686be2b1797SNate Lawson 	NULL);
36871d7b121cSNate Lawson 
36881d7b121cSNate Lawson static int
36891d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
36901d7b121cSNate Lawson {
369154f6bca0SNate Lawson     int		 error, *dbg;
36921d7b121cSNate Lawson     struct	 debugtag *tag;
369354f6bca0SNate Lawson     struct	 sbuf sb;
36941d7b121cSNate Lawson 
369554f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
369654f6bca0SNate Lawson 	return (ENOMEM);
36971d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
36981d7b121cSNate Lawson 	tag = &dbg_layer[0];
36991d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
37001d7b121cSNate Lawson     } else {
37011d7b121cSNate Lawson 	tag = &dbg_level[0];
37021d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
37031d7b121cSNate Lawson     }
37041d7b121cSNate Lawson 
37051d7b121cSNate Lawson     /* Get old values if this is a get request. */
370615e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
37071d7b121cSNate Lawson     if (*dbg == 0) {
370854f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
37091d7b121cSNate Lawson     } else if (req->newptr == NULL) {
37101d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
371154f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
371254f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
37131d7b121cSNate Lawson 	}
37141d7b121cSNate Lawson     }
371554f6bca0SNate Lawson     sbuf_trim(&sb);
371654f6bca0SNate Lawson     sbuf_finish(&sb);
37171d7b121cSNate Lawson 
37187e639165SNate Lawson     /* Copy out the old values to the user. */
37197e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
372054f6bca0SNate Lawson     sbuf_delete(&sb);
37211d7b121cSNate Lawson 
37221d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
37231d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
37241d7b121cSNate Lawson 	*dbg = 0;
37251d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
37261d7b121cSNate Lawson 	acpi_set_debugging(NULL);
37271d7b121cSNate Lawson     }
372815e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
37291d7b121cSNate Lawson 
37301d7b121cSNate Lawson     return (error);
37311d7b121cSNate Lawson }
3732bee4aa4aSNate Lawson 
37331d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
37341d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
37351d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
37361d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3737bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3738f9390180SMitsuru IWASAKI 
3739f9390180SMitsuru IWASAKI static int
37402a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
37412a18c71dSJung-uk Kim {
37422a18c71dSJung-uk Kim 	int	error;
37432a18c71dSJung-uk Kim 	int	old;
37442a18c71dSJung-uk Kim 
37452a18c71dSJung-uk Kim 	old = acpi_debug_objects;
37462a18c71dSJung-uk Kim 	error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
37472a18c71dSJung-uk Kim 	if (error != 0 || req->newptr == NULL)
37482a18c71dSJung-uk Kim 		return (error);
37492a18c71dSJung-uk Kim 	if (old == acpi_debug_objects || (old && acpi_debug_objects))
37502a18c71dSJung-uk Kim 		return (0);
37512a18c71dSJung-uk Kim 
37522a18c71dSJung-uk Kim 	ACPI_SERIAL_BEGIN(acpi);
37532a18c71dSJung-uk Kim 	AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
37542a18c71dSJung-uk Kim 	ACPI_SERIAL_END(acpi);
37552a18c71dSJung-uk Kim 
37562a18c71dSJung-uk Kim 	return (0);
37572a18c71dSJung-uk Kim }
37582a18c71dSJung-uk Kim 
37592a18c71dSJung-uk Kim static int
3760ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface)
3761ae19af49SJung-uk Kim {
3762ae19af49SJung-uk Kim 	char *p;
3763ae19af49SJung-uk Kim 	size_t len;
3764ae19af49SJung-uk Kim 	int i, j;
3765ae19af49SJung-uk Kim 
3766ae19af49SJung-uk Kim 	p = str;
3767ae19af49SJung-uk Kim 	while (isspace(*p) || *p == ',')
3768ae19af49SJung-uk Kim 		p++;
3769ae19af49SJung-uk Kim 	len = strlen(p);
3770ae19af49SJung-uk Kim 	if (len == 0)
3771ae19af49SJung-uk Kim 		return (0);
3772ae19af49SJung-uk Kim 	p = strdup(p, M_TEMP);
3773ae19af49SJung-uk Kim 	for (i = 0; i < len; i++)
3774ae19af49SJung-uk Kim 		if (p[i] == ',')
3775ae19af49SJung-uk Kim 			p[i] = '\0';
3776ae19af49SJung-uk Kim 	i = j = 0;
3777ae19af49SJung-uk Kim 	while (i < len)
3778ae19af49SJung-uk Kim 		if (isspace(p[i]) || p[i] == '\0')
3779ae19af49SJung-uk Kim 			i++;
3780ae19af49SJung-uk Kim 		else {
3781ae19af49SJung-uk Kim 			i += strlen(p + i) + 1;
3782ae19af49SJung-uk Kim 			j++;
3783ae19af49SJung-uk Kim 		}
3784ae19af49SJung-uk Kim 	if (j == 0) {
3785ae19af49SJung-uk Kim 		free(p, M_TEMP);
3786ae19af49SJung-uk Kim 		return (0);
3787ae19af49SJung-uk Kim 	}
3788ae19af49SJung-uk Kim 	iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
3789ae19af49SJung-uk Kim 	iface->num = j;
3790ae19af49SJung-uk Kim 	i = j = 0;
3791ae19af49SJung-uk Kim 	while (i < len)
3792ae19af49SJung-uk Kim 		if (isspace(p[i]) || p[i] == '\0')
3793ae19af49SJung-uk Kim 			i++;
3794ae19af49SJung-uk Kim 		else {
3795ae19af49SJung-uk Kim 			iface->data[j] = p + i;
3796ae19af49SJung-uk Kim 			i += strlen(p + i) + 1;
3797ae19af49SJung-uk Kim 			j++;
3798ae19af49SJung-uk Kim 		}
3799ae19af49SJung-uk Kim 
3800ae19af49SJung-uk Kim 	return (j);
3801ae19af49SJung-uk Kim }
3802ae19af49SJung-uk Kim 
3803ae19af49SJung-uk Kim static void
3804ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface)
3805ae19af49SJung-uk Kim {
3806ae19af49SJung-uk Kim 
3807ae19af49SJung-uk Kim 	free(iface->data[0], M_TEMP);
3808ae19af49SJung-uk Kim 	free(iface->data, M_TEMP);
3809ae19af49SJung-uk Kim }
3810ae19af49SJung-uk Kim 
3811ae19af49SJung-uk Kim static void
3812ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev)
3813ae19af49SJung-uk Kim {
3814ae19af49SJung-uk Kim 	struct acpi_interface list;
3815ae19af49SJung-uk Kim 	ACPI_STATUS status;
3816ae19af49SJung-uk Kim 	int i;
3817ae19af49SJung-uk Kim 
3818ae19af49SJung-uk Kim 	if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
3819ae19af49SJung-uk Kim 		for (i = 0; i < list.num; i++) {
3820ae19af49SJung-uk Kim 			status = AcpiInstallInterface(list.data[i]);
3821ae19af49SJung-uk Kim 			if (ACPI_FAILURE(status))
3822ae19af49SJung-uk Kim 				device_printf(dev,
3823ae19af49SJung-uk Kim 				    "failed to install _OSI(\"%s\"): %s\n",
3824ae19af49SJung-uk Kim 				    list.data[i], AcpiFormatException(status));
3825ae19af49SJung-uk Kim 			else if (bootverbose)
3826ae19af49SJung-uk Kim 				device_printf(dev, "installed _OSI(\"%s\")\n",
3827ae19af49SJung-uk Kim 				    list.data[i]);
3828ae19af49SJung-uk Kim 		}
3829ae19af49SJung-uk Kim 		acpi_free_interfaces(&list);
3830ae19af49SJung-uk Kim 	}
3831ae19af49SJung-uk Kim 	if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
3832ae19af49SJung-uk Kim 		for (i = 0; i < list.num; i++) {
3833ae19af49SJung-uk Kim 			status = AcpiRemoveInterface(list.data[i]);
3834ae19af49SJung-uk Kim 			if (ACPI_FAILURE(status))
3835ae19af49SJung-uk Kim 				device_printf(dev,
3836ae19af49SJung-uk Kim 				    "failed to remove _OSI(\"%s\"): %s\n",
3837ae19af49SJung-uk Kim 				    list.data[i], AcpiFormatException(status));
3838ae19af49SJung-uk Kim 			else if (bootverbose)
3839ae19af49SJung-uk Kim 				device_printf(dev, "removed _OSI(\"%s\")\n",
3840ae19af49SJung-uk Kim 				    list.data[i]);
3841ae19af49SJung-uk Kim 		}
3842ae19af49SJung-uk Kim 		acpi_free_interfaces(&list);
3843ae19af49SJung-uk Kim 	}
3844ae19af49SJung-uk Kim }
3845ae19af49SJung-uk Kim 
3846ae19af49SJung-uk Kim static int
3847f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
3848f9390180SMitsuru IWASAKI {
3849f9390180SMitsuru IWASAKI 	int	state, acpi_state;
3850f9390180SMitsuru IWASAKI 	int	error;
3851f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
3852f9390180SMitsuru IWASAKI 	va_list	ap;
3853f9390180SMitsuru IWASAKI 
3854f9390180SMitsuru IWASAKI 	error = 0;
3855f9390180SMitsuru IWASAKI 	switch (cmd) {
3856f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
3857f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
3858f9390180SMitsuru IWASAKI 		if (sc == NULL) {
3859f9390180SMitsuru IWASAKI 			error = EINVAL;
3860f9390180SMitsuru IWASAKI 			goto out;
3861f9390180SMitsuru IWASAKI 		}
3862f9390180SMitsuru IWASAKI 
3863f9390180SMitsuru IWASAKI 		va_start(ap, arg);
3864f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
3865f9390180SMitsuru IWASAKI 		va_end(ap);
3866f9390180SMitsuru IWASAKI 
3867f9390180SMitsuru IWASAKI 		switch (state) {
3868f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
3869f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
3870f9390180SMitsuru IWASAKI 			break;
3871f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
3872f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
3873f9390180SMitsuru IWASAKI 			break;
3874f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
3875f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
3876f9390180SMitsuru IWASAKI 			break;
3877f9390180SMitsuru IWASAKI 		default:
3878f9390180SMitsuru IWASAKI 			error = EINVAL;
3879f9390180SMitsuru IWASAKI 			goto out;
3880f9390180SMitsuru IWASAKI 		}
3881f9390180SMitsuru IWASAKI 
388200a30448SNate Lawson 		if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
388300a30448SNate Lawson 			error = ENXIO;
3884f9390180SMitsuru IWASAKI 		break;
3885f9390180SMitsuru IWASAKI 	default:
3886f9390180SMitsuru IWASAKI 		error = EINVAL;
3887f9390180SMitsuru IWASAKI 		goto out;
3888f9390180SMitsuru IWASAKI 	}
3889f9390180SMitsuru IWASAKI 
3890f9390180SMitsuru IWASAKI out:
3891f9390180SMitsuru IWASAKI 	return (error);
3892f9390180SMitsuru IWASAKI }
3893f9390180SMitsuru IWASAKI 
3894f9390180SMitsuru IWASAKI static void
3895f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
3896f9390180SMitsuru IWASAKI {
3897be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
38986c407052SMitsuru IWASAKI 	return;
3899f9390180SMitsuru IWASAKI 
3900f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3901f9390180SMitsuru IWASAKI }
3902f9390180SMitsuru IWASAKI 
3903f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
3904