xref: /freebsd/sys/dev/acpica/acpi.c (revision 0e68afe553d9ede70ce83eaa697a5517b1defb31)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  */
2915e32d5dSMike Smith 
30dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$");
32dad97feeSDavid E. O'Brien 
3315e32d5dSMike Smith #include "opt_acpi.h"
3462d70a81SJohn Baldwin #include "opt_device_numa.h"
3562d70a81SJohn Baldwin 
3615e32d5dSMike Smith #include <sys/param.h>
3715e32d5dSMike Smith #include <sys/kernel.h>
38fb919e4dSMark Murray #include <sys/proc.h>
39a89fcf28STakanori Watanabe #include <sys/fcntl.h>
4015e32d5dSMike Smith #include <sys/malloc.h>
41fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4215e32d5dSMike Smith #include <sys/bus.h>
4315e32d5dSMike Smith #include <sys/conf.h>
4415e32d5dSMike Smith #include <sys/ioccom.h>
4515e32d5dSMike Smith #include <sys/reboot.h>
4615e32d5dSMike Smith #include <sys/sysctl.h>
4715e32d5dSMike Smith #include <sys/ctype.h>
481611ea87SMitsuru IWASAKI #include <sys/linker.h>
49f9390180SMitsuru IWASAKI #include <sys/power.h>
5054f6bca0SNate Lawson #include <sys/sbuf.h>
51c66d2b38SJung-uk Kim #include <sys/sched.h>
52eeb3a05fSNate Lawson #include <sys/smp.h>
53c66d2b38SJung-uk Kim #include <sys/timetc.h>
5415e32d5dSMike Smith 
55d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
56d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h>
57d320e05cSJohn Baldwin #endif
5815e32d5dSMike Smith #include <machine/resource.h>
59dc750869SNate Lawson #include <machine/bus.h>
60dc750869SNate Lawson #include <sys/rman.h>
61bc0f2195SMike Smith #include <isa/isavar.h>
62c796e27bSNate Lawson #include <isa/pnpvar.h>
63bc0f2195SMike Smith 
64129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
65129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
66129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
67129d3046SJung-uk Kim 
6815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
6915e32d5dSMike Smith #include <dev/acpica/acpiio.h>
7015e32d5dSMike Smith 
71*0e68afe5SAndrew Turner #include <dev/pci/pcivar.h>
72*0e68afe5SAndrew Turner 
732be4e471SJung-uk Kim #include <vm/vm_param.h>
742be4e471SJung-uk Kim 
75d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
7615e32d5dSMike Smith 
77be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
78cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
79b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
800ae55423SMike Smith 
8115e32d5dSMike Smith static d_open_t		acpiopen;
8215e32d5dSMike Smith static d_close_t	acpiclose;
8315e32d5dSMike Smith static d_ioctl_t	acpiioctl;
8415e32d5dSMike Smith 
8515e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
86dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
877ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
887ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
897ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
907ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
9115e32d5dSMike Smith };
9215e32d5dSMike Smith 
93ae19af49SJung-uk Kim struct acpi_interface {
94ae19af49SJung-uk Kim 	ACPI_STRING	*data;
95ae19af49SJung-uk Kim 	int		num;
96ae19af49SJung-uk Kim };
97ae19af49SJung-uk Kim 
98346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
99cb9b0d80SMike Smith struct mtx	acpi_mutex;
100fadf3fb9SJohn Baldwin struct callout	acpi_sleep_timer;
101cb9b0d80SMike Smith 
10289fb5af8SNate Lawson /* Bitmap of device quirks. */
10389fb5af8SNate Lawson int		acpi_quirks;
10489fb5af8SNate Lawson 
105a0e73a12SJung-uk Kim /* Supported sleep states. */
106a0e73a12SJung-uk Kim static BOOLEAN	acpi_sleep_states[ACPI_S_STATE_COUNT];
107a0e73a12SJung-uk Kim 
10864de8019SJohn Baldwin static void	acpi_lookup(void *arg, const char *name, device_t *dev);
1096d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
11015e32d5dSMike Smith static int	acpi_probe(device_t dev);
11115e32d5dSMike Smith static int	acpi_attach(device_t dev);
11210ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
11310ce62b9SNate Lawson static int	acpi_resume(device_t dev);
114169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
1153d844eddSAndriy Gapon static device_t	acpi_add_child(device_t bus, u_int order, const char *name,
116be2b1797SNate Lawson 			int unit);
11715e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
11810ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
11910ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
120be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
121be2b1797SNate Lawson 			uintptr_t *result);
122be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
123be2b1797SNate Lawson 			uintptr_t value);
12491233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
125ea233199SJohn Baldwin static void	acpi_reserve_resources(device_t dev);
126adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
127ea233199SJohn Baldwin static int	acpi_set_resource(device_t dev, device_t child, int type,
1282dd1bdf1SJustin Hibbits 			int rid, rman_res_t start, rman_res_t count);
129be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
1302dd1bdf1SJustin Hibbits 			int type, int *rid, rman_res_t start, rman_res_t end,
1312dd1bdf1SJustin Hibbits 			rman_res_t count, u_int flags);
132049dc0d1SJohn Baldwin static int	acpi_adjust_resource(device_t bus, device_t child, int type,
1332dd1bdf1SJustin Hibbits 			struct resource *r, rman_res_t start, rman_res_t end);
134be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
135be2b1797SNate Lawson 			int rid, struct resource *r);
1368b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1378b28b622SNate Lawson 		    int rid);
1381e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1391e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
140ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
141ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
142ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
143ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
144df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
145df8d2a32SNate Lawson 		    void *context, void **retval);
146df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
147df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
148a7a3177fSJung-uk Kim static int	acpi_set_powerstate(device_t child, int state);
149be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
150be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
15115e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
152d227204dSJohn Baldwin static void	acpi_probe_order(ACPI_HANDLE handle, int *order);
153be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
154be2b1797SNate Lawson 		    void *context, void **status);
155a0e73a12SJung-uk Kim static void	acpi_sleep_enable(void *arg);
156a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
15700a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
15815e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
15913d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
160183c8af3SJohn Baldwin static BOOLEAN	acpi_has_hid(ACPI_HANDLE handle);
161a0a15716SJung-uk Kim static void	acpi_resync_clock(struct acpi_softc *sc);
162d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
163d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
164d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
16588a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
16688a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
16715e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
16815e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
169a0e73a12SJung-uk Kim static int	acpi_sname2sstate(const char *sname);
170a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate);
171d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1721d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1732a18c71dSJung-uk Kim static int	acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
174f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
175879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
176879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
177879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
178879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
179d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
180d320e05cSJohn Baldwin static void	acpi_enable_pcie(void);
181d320e05cSJohn Baldwin #endif
1820d484d24SJohn Baldwin static void	acpi_hint_device_unit(device_t acdev, device_t child,
1830d484d24SJohn Baldwin 		    const char *name, int *unitp);
184ae19af49SJung-uk Kim static void	acpi_reset_interfaces(device_t dev);
185879d6c23STakanori Watanabe 
18615e32d5dSMike Smith static device_method_t acpi_methods[] = {
18715e32d5dSMike Smith     /* Device interface */
18815e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
18915e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
190169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
19156a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
19210ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
19310ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
19415e32d5dSMike Smith 
19515e32d5dSMike Smith     /* Bus interface */
19615e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
19715e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
19810ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
19910ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
20015e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
20115e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
20291233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
203ea233199SJohn Baldwin     DEVMETHOD(bus_set_resource,		acpi_set_resource),
20491233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
20515e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
206049dc0d1SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	acpi_adjust_resource),
20715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
2088b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
209879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
210879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
21115e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
21215e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
21315e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
21415e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
2150d484d24SJohn Baldwin     DEVMETHOD(bus_hint_device_unit,	acpi_hint_device_unit),
2168d791e5aSJohn Baldwin     DEVMETHOD(bus_get_cpus,		acpi_get_cpus),
217ffcf962dSAdrian Chadd     DEVMETHOD(bus_get_domain,		acpi_get_domain),
21815e32d5dSMike Smith 
219ce619b2eSNate Lawson     /* ACPI bus */
220ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
221ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
22210ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
223df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
224ce619b2eSNate Lawson 
225bc0f2195SMike Smith     /* ISA emulation */
226bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
227bc0f2195SMike Smith 
22861bfd867SSofian Brabez     DEVMETHOD_END
22915e32d5dSMike Smith };
23015e32d5dSMike Smith 
23115e32d5dSMike Smith static driver_t acpi_driver = {
23215e32d5dSMike Smith     "acpi",
23315e32d5dSMike Smith     acpi_methods,
23415e32d5dSMike Smith     sizeof(struct acpi_softc),
23515e32d5dSMike Smith };
23615e32d5dSMike Smith 
2373273b005SMike Smith static devclass_t acpi_devclass;
2386d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
239a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
24015e32d5dSMike Smith 
24115e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
24215e2f34fSNate Lawson 
243adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
244adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
245adad4744SNate Lawson 
246bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
247bee4aa4aSNate Lawson 
2485217af30SJohn Baldwin /* Holds the description of the acpi0 device. */
2495217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
2505217af30SJohn Baldwin 
251197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
2521d7b121cSNate Lawson static char acpi_ca_version[12];
2531d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2541d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
25515e32d5dSMike Smith 
25615e32d5dSMike Smith /*
257ae19af49SJung-uk Kim  * Allow overriding _OSI methods.
258ae19af49SJung-uk Kim  */
259ae19af49SJung-uk Kim static char acpi_install_interface[256];
260ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface,
261ae19af49SJung-uk Kim     sizeof(acpi_install_interface));
262ae19af49SJung-uk Kim static char acpi_remove_interface[256];
263ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface,
264ae19af49SJung-uk Kim     sizeof(acpi_remove_interface));
265ae19af49SJung-uk Kim 
2662a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */
2672a18c71dSJung-uk Kim static int acpi_debug_objects;
2682a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
2692a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
2702a18c71dSJung-uk Kim     CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I",
2712a18c71dSJung-uk Kim     "Enable Debug objects");
2722a18c71dSJung-uk Kim 
2732a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */
2742a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1;
2752a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
2762a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
2772a18c71dSJung-uk Kim     &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
2782a18c71dSJung-uk Kim 
279313a0c13SJung-uk Kim /* Ignore register widths set by FADT and use default widths instead. */
280313a0c13SJung-uk Kim static int acpi_ignore_reg_width = 1;
281313a0c13SJung-uk Kim TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width);
282313a0c13SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN,
283313a0c13SJung-uk Kim     &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT");
284313a0c13SJung-uk Kim 
28539da3eb3SNate Lawson /* Allow users to override quirks. */
28639da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
28739da3eb3SNate Lawson 
28893bfd059SNate Lawson static int acpi_susp_bounce;
28993bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
29093bfd059SNate Lawson     &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
29193bfd059SNate Lawson 
292c9b8d77dSNate Lawson /*
2936d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2946d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
2958c0879b6SJohn Baldwin  * It also cannot be unloaded, since the entire system bus hierarchy hangs
296be2b1797SNate Lawson  * off it.
2976d63101aSMike Smith  */
2986d63101aSMike Smith static int
2996d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
3006d63101aSMike Smith {
3016d63101aSMike Smith     switch (event) {
3026d63101aSMike Smith     case MOD_LOAD:
30387b45ed5SMitsuru IWASAKI 	if (!cold) {
30487b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
3056d63101aSMike Smith 	    return (EPERM);
30687b45ed5SMitsuru IWASAKI 	}
3076d63101aSMike Smith 	break;
3086d63101aSMike Smith     case MOD_UNLOAD:
309f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
3106d63101aSMike Smith 	    return (EBUSY);
311f9390180SMitsuru IWASAKI 	break;
3126d63101aSMike Smith     default:
3136d63101aSMike Smith 	break;
3146d63101aSMike Smith     }
3156d63101aSMike Smith     return (0);
3166d63101aSMike Smith }
3176d63101aSMike Smith 
3186d63101aSMike Smith /*
319bbc2815cSJohn Baldwin  * Perform early initialization.
32015e32d5dSMike Smith  */
321bbc2815cSJohn Baldwin ACPI_STATUS
322bbc2815cSJohn Baldwin acpi_Startup(void)
32315e32d5dSMike Smith {
32489fb5af8SNate Lawson     static int started = 0;
3252be4e471SJung-uk Kim     ACPI_STATUS status;
3262be4e471SJung-uk Kim     int val;
32715e32d5dSMike Smith 
3281b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3291b8c233dSPeter Pentchev 
330bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
331bbc2815cSJohn Baldwin     if (started)
3322be4e471SJung-uk Kim 	return_VALUE (AE_OK);
333bbc2815cSJohn Baldwin     started = 1;
33415e32d5dSMike Smith 
335413081d7SNate Lawson     /*
3362be4e471SJung-uk Kim      * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
3372be4e471SJung-uk Kim      * if more tables exist.
338413081d7SNate Lawson      */
3392be4e471SJung-uk Kim     if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
3402be4e471SJung-uk Kim 	printf("ACPI: Table initialisation failed: %s\n",
3412be4e471SJung-uk Kim 	    AcpiFormatException(status));
3422be4e471SJung-uk Kim 	return_VALUE (status);
34315e32d5dSMike Smith     }
3443184cf5aSNate Lawson 
34589fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
3462be4e471SJung-uk Kim     if (acpi_quirks == ACPI_Q_OK)
34789fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
34889fb5af8SNate Lawson 
34939da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
35089fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
35189fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
35289fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
35389fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
3542be4e471SJung-uk Kim 	status = AE_SUPPORT;
35589fb5af8SNate Lawson     }
3563184cf5aSNate Lawson 
3572be4e471SJung-uk Kim     return_VALUE (status);
358bbc2815cSJohn Baldwin }
359bbc2815cSJohn Baldwin 
360bbc2815cSJohn Baldwin /*
3615217af30SJohn Baldwin  * Detect ACPI and perform early initialisation.
362bbc2815cSJohn Baldwin  */
3635217af30SJohn Baldwin int
3645217af30SJohn Baldwin acpi_identify(void)
365bbc2815cSJohn Baldwin {
3665217af30SJohn Baldwin     ACPI_TABLE_RSDP	*rsdp;
3675217af30SJohn Baldwin     ACPI_TABLE_HEADER	*rsdt;
3685217af30SJohn Baldwin     ACPI_PHYSICAL_ADDRESS paddr;
3695217af30SJohn Baldwin     struct sbuf		sb;
370bbc2815cSJohn Baldwin 
371bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
372bbc2815cSJohn Baldwin 
373bbc2815cSJohn Baldwin     if (!cold)
3745217af30SJohn Baldwin 	return (ENXIO);
375bbc2815cSJohn Baldwin 
376a8de37b0SEitan Adler     /* Check that we haven't been disabled with a hint. */
377a8de37b0SEitan Adler     if (resource_disabled("acpi", 0))
378a8de37b0SEitan Adler 	return (ENXIO);
379a8de37b0SEitan Adler 
3805217af30SJohn Baldwin     /* Check for other PM systems. */
3815217af30SJohn Baldwin     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
3825217af30SJohn Baldwin 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
3835217af30SJohn Baldwin 	printf("ACPI identify failed, other PM system enabled.\n");
3845217af30SJohn Baldwin 	return (ENXIO);
3855217af30SJohn Baldwin     }
3864b1ff978SMark Santcroos 
3872be4e471SJung-uk Kim     /* Initialize root tables. */
3882be4e471SJung-uk Kim     if (ACPI_FAILURE(acpi_Startup())) {
3892be4e471SJung-uk Kim 	printf("ACPI: Try disabling either ACPI or apic support.\n");
3905217af30SJohn Baldwin 	return (ENXIO);
3912be4e471SJung-uk Kim     }
39215e32d5dSMike Smith 
3935217af30SJohn Baldwin     if ((paddr = AcpiOsGetRootPointer()) == 0 ||
3945217af30SJohn Baldwin 	(rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
3955217af30SJohn Baldwin 	return (ENXIO);
3965217af30SJohn Baldwin     if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
3975217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
3985217af30SJohn Baldwin     else
3995217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
4005217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
4015217af30SJohn Baldwin 
4025217af30SJohn Baldwin     if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
4035217af30SJohn Baldwin 	return (ENXIO);
4045217af30SJohn Baldwin     sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
4055217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
4065217af30SJohn Baldwin     sbuf_trim(&sb);
4075217af30SJohn Baldwin     sbuf_putc(&sb, ' ');
4085217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
4095217af30SJohn Baldwin     sbuf_trim(&sb);
4105217af30SJohn Baldwin     sbuf_finish(&sb);
4115217af30SJohn Baldwin     sbuf_delete(&sb);
4125217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
4135217af30SJohn Baldwin 
4145217af30SJohn Baldwin     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
4155217af30SJohn Baldwin 
4165217af30SJohn Baldwin     return (0);
41715e32d5dSMike Smith }
41815e32d5dSMike Smith 
41915e32d5dSMike Smith /*
420bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
42115e32d5dSMike Smith  */
42215e32d5dSMike Smith static int
42315e32d5dSMike Smith acpi_probe(device_t dev)
42415e32d5dSMike Smith {
42515e32d5dSMike Smith 
426b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
427f9390180SMitsuru IWASAKI 
4285217af30SJohn Baldwin     device_set_desc(dev, acpi_desc);
429bee4aa4aSNate Lawson 
430feeec74dSNathan Whitehorn     return_VALUE (BUS_PROBE_NOWILDCARD);
43115e32d5dSMike Smith }
43215e32d5dSMike Smith 
43315e32d5dSMike Smith static int
43415e32d5dSMike Smith acpi_attach(device_t dev)
43515e32d5dSMike Smith {
43615e32d5dSMike Smith     struct acpi_softc	*sc;
437cb9b0d80SMike Smith     ACPI_STATUS		status;
438ea27c63eSNate Lawson     int			error, state;
43932d18aa5SMike Smith     UINT32		flags;
440ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
441498d464fSMitsuru IWASAKI     char		*env;
44215e32d5dSMike Smith 
443b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
444bee4aa4aSNate Lawson 
44515e32d5dSMike Smith     sc = device_get_softc(dev);
44615e32d5dSMike Smith     sc->acpi_dev = dev;
447fd90e2edSJung-uk Kim     callout_init(&sc->susp_force_to, 1);
44815e32d5dSMike Smith 
4492be4e471SJung-uk Kim     error = ENXIO;
4502be4e471SJung-uk Kim 
45191233413SNate Lawson     /* Initialize resource manager. */
45291233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
45391233413SNate Lawson     acpi_rman_io.rm_start = 0;
45491233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
4550bba6acfSJohn Baldwin     acpi_rman_io.rm_descr = "ACPI I/O ports";
45691233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
45791233413SNate Lawson 	panic("acpi rman_init IO ports failed");
45891233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
4590bba6acfSJohn Baldwin     acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
46091233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
46191233413SNate Lawson 	panic("acpi rman_init memory failed");
46291233413SNate Lawson 
4632be4e471SJung-uk Kim     /* Initialise the ACPI mutex */
4642be4e471SJung-uk Kim     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
4652be4e471SJung-uk Kim 
4662be4e471SJung-uk Kim     /*
4672be4e471SJung-uk Kim      * Set the globals from our tunables.  This is needed because ACPI-CA
4682be4e471SJung-uk Kim      * uses UINT8 for some values and we have no tunable_byte.
4692be4e471SJung-uk Kim      */
4702a18c71dSJung-uk Kim     AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
4712a18c71dSJung-uk Kim     AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
472313a0c13SJung-uk Kim     AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE;
4732a18c71dSJung-uk Kim 
4742a18c71dSJung-uk Kim #ifndef ACPI_DEBUG
4752a18c71dSJung-uk Kim     /*
4762a18c71dSJung-uk Kim      * Disable all debugging layers and levels.
4772a18c71dSJung-uk Kim      */
4782a18c71dSJung-uk Kim     AcpiDbgLayer = 0;
4792a18c71dSJung-uk Kim     AcpiDbgLevel = 0;
4802a18c71dSJung-uk Kim #endif
4812be4e471SJung-uk Kim 
4822be4e471SJung-uk Kim     /* Start up the ACPI CA subsystem. */
4832be4e471SJung-uk Kim     status = AcpiInitializeSubsystem();
4842be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4852be4e471SJung-uk Kim 	device_printf(dev, "Could not initialize Subsystem: %s\n",
4862be4e471SJung-uk Kim 		      AcpiFormatException(status));
4872be4e471SJung-uk Kim 	goto out;
4882be4e471SJung-uk Kim     }
4892be4e471SJung-uk Kim 
490ae19af49SJung-uk Kim     /* Override OS interfaces if the user requested. */
491ae19af49SJung-uk Kim     acpi_reset_interfaces(dev);
492ae19af49SJung-uk Kim 
4932be4e471SJung-uk Kim     /* Load ACPI name space. */
4942be4e471SJung-uk Kim     status = AcpiLoadTables();
4952be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4962be4e471SJung-uk Kim 	device_printf(dev, "Could not load Namespace: %s\n",
4972be4e471SJung-uk Kim 		      AcpiFormatException(status));
4982be4e471SJung-uk Kim 	goto out;
4992be4e471SJung-uk Kim     }
5002be4e471SJung-uk Kim 
501d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
502d320e05cSJohn Baldwin     /* Handle MCFG table if present. */
503d320e05cSJohn Baldwin     acpi_enable_pcie();
504d320e05cSJohn Baldwin #endif
505d320e05cSJohn Baldwin 
50615e32d5dSMike Smith     /*
507be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
508be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
509be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
510be2b1797SNate Lawson      * object init pass.
51115e32d5dSMike Smith      *
51232d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
51332d18aa5SMike Smith      *
514be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
515be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
51615e32d5dSMike Smith      */
51732d18aa5SMike Smith     flags = 0;
518d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
51932d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
520bee4aa4aSNate Lawson 
521bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
522b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
523be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
524be2b1797SNate Lawson 		      AcpiFormatException(status));
525cb9b0d80SMike Smith 	goto out;
52615e32d5dSMike Smith     }
52715e32d5dSMike Smith 
528f8335e3aSNate Lawson     /*
529f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
530f8335e3aSNate Lawson      * the namespace has been evaluated.
531da72d149SNate Lawson      *
532da72d149SNate Lawson      * XXX This happens before the sysresource devices have been probed and
533da72d149SNate Lawson      * attached so its resources come from nexus0.  In practice, this isn't
534da72d149SNate Lawson      * a problem but should be addressed eventually.
535f8335e3aSNate Lawson      */
536f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
537f8335e3aSNate Lawson 
538bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
539b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
540be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
541be2b1797SNate Lawson 		      AcpiFormatException(status));
542b69ed3f4SMitsuru IWASAKI 	goto out;
543b69ed3f4SMitsuru IWASAKI     }
544b69ed3f4SMitsuru IWASAKI 
54515e32d5dSMike Smith     /*
5461d073b1dSJohn Baldwin      * Setup our sysctl tree.
5471d073b1dSJohn Baldwin      *
5481d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
5491d073b1dSJohn Baldwin      */
5501d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
5511d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
5521d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
5531d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
5541d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
555d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
556ad73b301SAdrian Chadd 	0, 0, acpi_supported_sleep_state_sysctl, "A",
557ad73b301SAdrian Chadd 	"List supported ACPI sleep states.");
558d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5591d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
560ad73b301SAdrian Chadd 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A",
561ad73b301SAdrian Chadd 	"Power button ACPI sleep state.");
5621d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5631d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
564ad73b301SAdrian Chadd 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A",
565ad73b301SAdrian Chadd 	"Sleep button ACPI sleep state.");
5661d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5671d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
568ad73b301SAdrian Chadd 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A",
569ad73b301SAdrian Chadd 	"Lid ACPI sleep state. Set to S3 if you want to suspend your laptop when close the Lid.");
570f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
571f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
572f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
573f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
574f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
575f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5762d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
577197b4dccSNate Lawson 	OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
57831f301d0SChristian Brueffer 	"sleep delay in seconds");
579ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
580197b4dccSNate Lawson 	OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
5811611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
582197b4dccSNate Lawson 	OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
583d85e6785SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
584d85e6785SNate Lawson 	OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
585d85e6785SNate Lawson 	&sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
586d1b16e18SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
587d1b16e18SNate Lawson 	OID_AUTO, "handle_reboot", CTLFLAG_RW,
588d1b16e18SNate Lawson 	&sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
589bf0c18ecSNate Lawson 
590bf0c18ecSNate Lawson     /*
5912b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
592bf0c18ecSNate Lawson      * stabilize.
593bf0c18ecSNate Lawson      */
5942b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
5952d644607SMitsuru IWASAKI     if (bootverbose)
5962d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
5972be111bfSDavide Italiano     if ((env = kern_getenv("hw.acpi.verbose")) != NULL) {
598965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
599498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
600498d464fSMitsuru IWASAKI 	freeenv(env);
601498d464fSMitsuru IWASAKI     }
6021d073b1dSJohn Baldwin 
603ac731af5SJung-uk Kim     /* Only enable reboot by default if the FADT says it is available. */
604ac731af5SJung-uk Kim     if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER)
605ac731af5SJung-uk Kim 	sc->acpi_handle_reboot = 1;
606ac731af5SJung-uk Kim 
607fe5d5d7dSAndrew Turner #if !ACPI_REDUCED_HARDWARE
6082d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
609d8c37c3aSAndrew Turner     if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
6102d610c46SNate Lawson 	sc->acpi_s4bios = 1;
611fe5d5d7dSAndrew Turner #endif
6122d610c46SNate Lawson 
613a0e73a12SJung-uk Kim     /* Probe all supported sleep states. */
614a0e73a12SJung-uk Kim     acpi_sleep_states[ACPI_STATE_S0] = TRUE;
615a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
616efcc2a30SJung-uk Kim 	if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT,
617efcc2a30SJung-uk Kim 	    __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) &&
618efcc2a30SJung-uk Kim 	    ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
619a0e73a12SJung-uk Kim 	    acpi_sleep_states[state] = TRUE;
620a0e73a12SJung-uk Kim 
6211d073b1dSJohn Baldwin     /*
622ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
623a0e73a12SJung-uk Kim      * to UNKNOWN by default to avoid surprising users.
62415e32d5dSMike Smith      */
625a0e73a12SJung-uk Kim     sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
626a0e73a12SJung-uk Kim 	ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
627a0e73a12SJung-uk Kim     sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
628a0e73a12SJung-uk Kim     sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
629a0e73a12SJung-uk Kim 	ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
630a0e73a12SJung-uk Kim     sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
631a0e73a12SJung-uk Kim 	ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
63215e32d5dSMike Smith 
633ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
634a0e73a12SJung-uk Kim     sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
63500a30448SNate Lawson     for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
636a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state]) {
637ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
638ea27c63eSNate Lawson 	    break;
639ea27c63eSNate Lawson 	}
640ea27c63eSNate Lawson 
64113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
64215e32d5dSMike Smith 
64315e32d5dSMike Smith     /*
64415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
64515e32d5dSMike Smith      */
64615e32d5dSMike Smith 
64759e472e9SNate Lawson     /* Register our shutdown handler. */
648be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
649be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
65015e32d5dSMike Smith 
65115e32d5dSMike Smith     /*
65215e32d5dSMike Smith      * Register our acpi event handlers.
65315e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
65415e32d5dSMike Smith      */
655be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
656be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
657be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
658be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
65915e32d5dSMike Smith 
660be2b1797SNate Lawson     /* Flag our initial states. */
661a0e73a12SJung-uk Kim     sc->acpi_enabled = TRUE;
66215e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
663a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
66415e32d5dSMike Smith 
665be2b1797SNate Lawson     /* Create the control device */
666a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
6672a4689e8SRobert Watson 			      "acpi");
66815e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
66915e32d5dSMike Smith 
670be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
671f86214b6SMitsuru IWASAKI 	goto out;
672f86214b6SMitsuru IWASAKI 
673f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
674f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
675f9390180SMitsuru IWASAKI 
67664de8019SJohn Baldwin     if (!acpi_disabled("bus")) {
67764de8019SJohn Baldwin 	EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000);
678eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
67964de8019SJohn Baldwin     }
680eeb6dba2SJohn Baldwin 
6815a77b11bSJung-uk Kim     /* Update all GPEs and enable runtime GPEs. */
6825a77b11bSJung-uk Kim     status = AcpiUpdateAllGpes();
6835a77b11bSJung-uk Kim     if (ACPI_FAILURE(status))
6845a77b11bSJung-uk Kim 	device_printf(dev, "Could not update all GPEs: %s\n",
6855a77b11bSJung-uk Kim 	    AcpiFormatException(status));
6865a77b11bSJung-uk Kim 
687a0e73a12SJung-uk Kim     /* Allow sleep request after a while. */
688fadf3fb9SJohn Baldwin     callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0);
689fadf3fb9SJohn Baldwin     callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME,
690fadf3fb9SJohn Baldwin 	acpi_sleep_enable, sc);
691a0e73a12SJung-uk Kim 
692cb9b0d80SMike Smith     error = 0;
693cb9b0d80SMike Smith 
694cb9b0d80SMike Smith  out:
695cb9b0d80SMike Smith     return_VALUE (error);
69615e32d5dSMike Smith }
69715e32d5dSMike Smith 
698a7a3177fSJung-uk Kim static void
699a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state)
700a7a3177fSJung-uk Kim {
70144aba0f6SJung-uk Kim 	device_t child;
702a7a3177fSJung-uk Kim 	device_t *devlist;
703a7a3177fSJung-uk Kim 	int dstate, i, numdevs;
704a7a3177fSJung-uk Kim 
705a7a3177fSJung-uk Kim 	if (device_get_children(dev, &devlist, &numdevs) != 0)
706a7a3177fSJung-uk Kim 		return;
707a7a3177fSJung-uk Kim 
708a7a3177fSJung-uk Kim 	/*
709a7a3177fSJung-uk Kim 	 * Retrieve and set D-state for the sleep state if _SxD is present.
710a7a3177fSJung-uk Kim 	 * Skip children who aren't attached since they are handled separately.
711a7a3177fSJung-uk Kim 	 */
712a7a3177fSJung-uk Kim 	for (i = 0; i < numdevs; i++) {
713a7a3177fSJung-uk Kim 		child = devlist[i];
714a7a3177fSJung-uk Kim 		dstate = state;
715a7a3177fSJung-uk Kim 		if (device_is_attached(child) &&
71644aba0f6SJung-uk Kim 		    acpi_device_pwr_for_sleep(dev, child, &dstate) == 0)
717a7a3177fSJung-uk Kim 			acpi_set_powerstate(child, dstate);
718a7a3177fSJung-uk Kim 	}
719a7a3177fSJung-uk Kim 	free(devlist, M_TEMP);
720a7a3177fSJung-uk Kim }
721a7a3177fSJung-uk Kim 
722169b539aSNate Lawson static int
72310ce62b9SNate Lawson acpi_suspend(device_t dev)
72410ce62b9SNate Lawson {
725a7a3177fSJung-uk Kim     int error;
72610ce62b9SNate Lawson 
727a56fe095SJohn Baldwin     GIANT_REQUIRED;
728a56fe095SJohn Baldwin 
72910ce62b9SNate Lawson     error = bus_generic_suspend(dev);
730a7a3177fSJung-uk Kim     if (error == 0)
731a7a3177fSJung-uk Kim 	acpi_set_power_children(dev, ACPI_STATE_D3);
73210ce62b9SNate Lawson 
73310ce62b9SNate Lawson     return (error);
73410ce62b9SNate Lawson }
73510ce62b9SNate Lawson 
73610ce62b9SNate Lawson static int
73710ce62b9SNate Lawson acpi_resume(device_t dev)
73810ce62b9SNate Lawson {
73910ce62b9SNate Lawson 
740a56fe095SJohn Baldwin     GIANT_REQUIRED;
741a56fe095SJohn Baldwin 
742a7a3177fSJung-uk Kim     acpi_set_power_children(dev, ACPI_STATE_D0);
74310ce62b9SNate Lawson 
74410ce62b9SNate Lawson     return (bus_generic_resume(dev));
74510ce62b9SNate Lawson }
74610ce62b9SNate Lawson 
74710ce62b9SNate Lawson static int
748169b539aSNate Lawson acpi_shutdown(device_t dev)
749169b539aSNate Lawson {
750169b539aSNate Lawson 
751a56fe095SJohn Baldwin     GIANT_REQUIRED;
752a56fe095SJohn Baldwin 
7530e414868SNate Lawson     /* Allow children to shutdown first. */
7540e414868SNate Lawson     bus_generic_shutdown(dev);
7550e414868SNate Lawson 
756bee4aa4aSNate Lawson     /*
757bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
758bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
759bee4aa4aSNate Lawson      */
760d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
761d4b9ff91SNate Lawson 
762169b539aSNate Lawson     return (0);
763169b539aSNate Lawson }
764169b539aSNate Lawson 
76515e32d5dSMike Smith /*
76615e32d5dSMike Smith  * Handle a new device being added
76715e32d5dSMike Smith  */
76815e32d5dSMike Smith static device_t
7693d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit)
77015e32d5dSMike Smith {
77115e32d5dSMike Smith     struct acpi_device	*ad;
77215e32d5dSMike Smith     device_t		child;
77315e32d5dSMike Smith 
774be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
77515e32d5dSMike Smith 	return (NULL);
77615e32d5dSMike Smith 
77715e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
77815e32d5dSMike Smith 
77915e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
78015e32d5dSMike Smith     if (child != NULL)
78115e32d5dSMike Smith 	device_set_ivars(child, ad);
78243ce1c77SNate Lawson     else
78343ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
78415e32d5dSMike Smith     return (child);
78515e32d5dSMike Smith }
78615e32d5dSMike Smith 
78715e32d5dSMike Smith static int
78815e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
78915e32d5dSMike Smith {
79015e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
79115e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
79215e32d5dSMike Smith     int retval = 0;
79315e32d5dSMike Smith 
79415e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
795da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#jx");
796da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
797da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%jd");
798da1b038aSJustin Hibbits     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%jd");
799a91c5fa8SNate Lawson     if (device_get_flags(child))
800a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
801ffcf962dSAdrian Chadd     retval += bus_print_child_domain(bus, child);
8022c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
80315e32d5dSMike Smith 
80415e32d5dSMike Smith     return (retval);
80515e32d5dSMike Smith }
80615e32d5dSMike Smith 
80710ce62b9SNate Lawson /*
80810ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
80910ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
81010ce62b9SNate Lawson  *
81110ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
81210ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
81310ce62b9SNate Lawson  * them to be powered up.
81410ce62b9SNate Lawson  */
81510ce62b9SNate Lawson static void
81610ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
81710ce62b9SNate Lawson {
81896cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
819a7a3177fSJung-uk Kim     acpi_set_powerstate(child, ACPI_STATE_D3);
82096cf0b6dSWarner Losh #endif
82110ce62b9SNate Lawson }
82210ce62b9SNate Lawson 
82310ce62b9SNate Lawson /*
82410ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
82510ce62b9SNate Lawson  *
82610ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
82710ce62b9SNate Lawson  */
82810ce62b9SNate Lawson static void
82910ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
83010ce62b9SNate Lawson {
83110ce62b9SNate Lawson     device_t child, *devlist;
83210ce62b9SNate Lawson     int i, numdevs;
83310ce62b9SNate Lawson 
83410ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
835099ea4b5SWarner Losh     if (device_get_children(dev, &devlist, &numdevs))
836099ea4b5SWarner Losh 	    return;
83710ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
83810ce62b9SNate Lawson 	child = devlist[i];
83910ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
84096cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
841a7a3177fSJung-uk Kim 	    acpi_set_powerstate(child, ACPI_STATE_D0);
84210ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
843a7a3177fSJung-uk Kim 		acpi_set_powerstate(child, ACPI_STATE_D3);
84496cf0b6dSWarner Losh #else
84596cf0b6dSWarner Losh 	    device_probe_and_attach(child);
84696cf0b6dSWarner Losh #endif
84710ce62b9SNate Lawson 	}
84810ce62b9SNate Lawson     }
84910ce62b9SNate Lawson     free(devlist, M_TEMP);
85010ce62b9SNate Lawson }
85110ce62b9SNate Lawson 
85263600cc3SNate Lawson /* Location hint for devctl(8) */
85363600cc3SNate Lawson static int
854247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
855247648afSTakanori Watanabe     size_t buflen)
856247648afSTakanori Watanabe {
857247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
8589cf5a6aaSAdrian Chadd     char buf2[32];
8599cf5a6aaSAdrian Chadd     int pxm;
860247648afSTakanori Watanabe 
8619cf5a6aaSAdrian Chadd     if (dinfo->ad_handle) {
862d40c0f53SNate Lawson         snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
8639cf5a6aaSAdrian Chadd         if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) {
8649cf5a6aaSAdrian Chadd                 snprintf(buf2, 32, " _PXM=%d", pxm);
8659cf5a6aaSAdrian Chadd                 strlcat(buf, buf2, buflen);
8669cf5a6aaSAdrian Chadd         }
8679cf5a6aaSAdrian Chadd     } else {
868d40c0f53SNate Lawson         snprintf(buf, buflen, "unknown");
8699cf5a6aaSAdrian Chadd     }
870247648afSTakanori Watanabe     return (0);
871247648afSTakanori Watanabe }
872247648afSTakanori Watanabe 
87363600cc3SNate Lawson /* PnP information for devctl(8) */
87463600cc3SNate Lawson static int
875247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
876247648afSTakanori Watanabe     size_t buflen)
877247648afSTakanori Watanabe {
87863600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
87992488a57SJung-uk Kim     ACPI_DEVICE_INFO *adinfo;
880247648afSTakanori Watanabe 
88192488a57SJung-uk Kim     if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) {
882d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
88392488a57SJung-uk Kim 	return (0);
88492488a57SJung-uk Kim     }
88592488a57SJung-uk Kim 
886247648afSTakanori Watanabe     snprintf(buf, buflen, "_HID=%s _UID=%lu",
887247648afSTakanori Watanabe 	(adinfo->Valid & ACPI_VALID_HID) ?
88892488a57SJung-uk Kim 	adinfo->HardwareId.String : "none",
88963600cc3SNate Lawson 	(adinfo->Valid & ACPI_VALID_UID) ?
89092488a57SJung-uk Kim 	strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL);
891247648afSTakanori Watanabe     AcpiOsFree(adinfo);
892247648afSTakanori Watanabe 
893247648afSTakanori Watanabe     return (0);
894247648afSTakanori Watanabe }
895247648afSTakanori Watanabe 
896247648afSTakanori Watanabe /*
89715e32d5dSMike Smith  * Handle per-device ivars
89815e32d5dSMike Smith  */
89915e32d5dSMike Smith static int
90015e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
90115e32d5dSMike Smith {
90215e32d5dSMike Smith     struct acpi_device	*ad;
90315e32d5dSMike Smith 
90415e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9052d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
90615e32d5dSMike Smith 	return (ENOENT);
90715e32d5dSMike Smith     }
90815e32d5dSMike Smith 
909be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
91015e32d5dSMike Smith     switch(index) {
91115e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
91215e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
91315e32d5dSMike Smith 	break;
91415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
91515e32d5dSMike Smith 	*(void **)result = ad->ad_private;
91615e32d5dSMike Smith 	break;
917d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
918d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
919d4b9ff91SNate Lawson 	break;
92032d18aa5SMike Smith     case ISA_IVAR_VENDORID:
92132d18aa5SMike Smith     case ISA_IVAR_SERIAL:
92232d18aa5SMike Smith     case ISA_IVAR_COMPATID:
92332d18aa5SMike Smith 	*(int *)result = -1;
92432d18aa5SMike Smith 	break;
92532d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
92632d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
92732d18aa5SMike Smith 	break;
928*0e68afe5SAndrew Turner     case PCI_IVAR_CLASS:
929*0e68afe5SAndrew Turner 	*(uint8_t*)result = (ad->ad_cls_class >> 16) & 0xff;
930*0e68afe5SAndrew Turner 	break;
931*0e68afe5SAndrew Turner     case PCI_IVAR_SUBCLASS:
932*0e68afe5SAndrew Turner 	*(uint8_t*)result = (ad->ad_cls_class >> 8) & 0xff;
933*0e68afe5SAndrew Turner 	break;
934*0e68afe5SAndrew Turner     case PCI_IVAR_PROGIF:
935*0e68afe5SAndrew Turner 	*(uint8_t*)result = (ad->ad_cls_class >> 0) & 0xff;
936*0e68afe5SAndrew Turner 	break;
93715e32d5dSMike Smith     default:
93815e32d5dSMike Smith 	return (ENOENT);
93915e32d5dSMike Smith     }
940be2b1797SNate Lawson 
94115e32d5dSMike Smith     return (0);
94215e32d5dSMike Smith }
94315e32d5dSMike Smith 
94415e32d5dSMike Smith static int
94515e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
94615e32d5dSMike Smith {
94715e32d5dSMike Smith     struct acpi_device	*ad;
94815e32d5dSMike Smith 
94915e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9502d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
95115e32d5dSMike Smith 	return (ENOENT);
95215e32d5dSMike Smith     }
95315e32d5dSMike Smith 
95415e32d5dSMike Smith     switch(index) {
95515e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
95615e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
95715e32d5dSMike Smith 	break;
95815e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
95915e32d5dSMike Smith 	ad->ad_private = (void *)value;
96015e32d5dSMike Smith 	break;
961d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
962d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
963d4b9ff91SNate Lawson 	break;
96415e32d5dSMike Smith     default:
9652ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
96615e32d5dSMike Smith 	return (ENOENT);
96715e32d5dSMike Smith     }
968be2b1797SNate Lawson 
96915e32d5dSMike Smith     return (0);
97015e32d5dSMike Smith }
97115e32d5dSMike Smith 
97215e32d5dSMike Smith /*
97315e32d5dSMike Smith  * Handle child resource allocation/removal
97415e32d5dSMike Smith  */
97591233413SNate Lawson static struct resource_list *
97691233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
97715e32d5dSMike Smith {
97891233413SNate Lawson     struct acpi_device		*ad;
97915e32d5dSMike Smith 
98091233413SNate Lawson     ad = device_get_ivars(child);
98191233413SNate Lawson     return (&ad->ad_rl);
98215e32d5dSMike Smith }
98315e32d5dSMike Smith 
9840d484d24SJohn Baldwin static int
9850d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value)
9860d484d24SJohn Baldwin {
9870d484d24SJohn Baldwin     struct acpi_device *ad = device_get_ivars(dev);
9880d484d24SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
9890d484d24SJohn Baldwin     struct resource_list_entry *rle;
9900d484d24SJohn Baldwin 
9910d484d24SJohn Baldwin     STAILQ_FOREACH(rle, rl, link) {
9920d484d24SJohn Baldwin 	if (rle->type != type)
9930d484d24SJohn Baldwin 	    continue;
9940d484d24SJohn Baldwin 	if (rle->start <= value && rle->end >= value)
9950d484d24SJohn Baldwin 	    return (1);
9960d484d24SJohn Baldwin     }
9970d484d24SJohn Baldwin     return (0);
9980d484d24SJohn Baldwin }
9990d484d24SJohn Baldwin 
10000d484d24SJohn Baldwin /*
10010d484d24SJohn Baldwin  * Wire device unit numbers based on resource matches in hints.
10020d484d24SJohn Baldwin  */
10030d484d24SJohn Baldwin static void
10040d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
10050d484d24SJohn Baldwin     int *unitp)
10060d484d24SJohn Baldwin {
10070d484d24SJohn Baldwin     const char *s;
10080d484d24SJohn Baldwin     long value;
10090d484d24SJohn Baldwin     int line, matches, unit;
10100d484d24SJohn Baldwin 
10110d484d24SJohn Baldwin     /*
10120d484d24SJohn Baldwin      * Iterate over all the hints for the devices with the specified
10130d484d24SJohn Baldwin      * name to see if one's resources are a subset of this device.
10140d484d24SJohn Baldwin      */
10150d484d24SJohn Baldwin     line = 0;
10160d484d24SJohn Baldwin     for (;;) {
10170d484d24SJohn Baldwin 	if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
10180d484d24SJohn Baldwin 	    break;
10190d484d24SJohn Baldwin 
10200d484d24SJohn Baldwin 	/* Must have an "at" for acpi or isa. */
10210d484d24SJohn Baldwin 	resource_string_value(name, unit, "at", &s);
10220d484d24SJohn Baldwin 	if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
10230d484d24SJohn Baldwin 	    strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0))
10240d484d24SJohn Baldwin 	    continue;
10250d484d24SJohn Baldwin 
10260d484d24SJohn Baldwin 	/*
10272fcd493cSJohn Baldwin 	 * Check for matching resources.  We must have at least one match.
10282fcd493cSJohn Baldwin 	 * Since I/O and memory resources cannot be shared, if we get a
10292fcd493cSJohn Baldwin 	 * match on either of those, ignore any mismatches in IRQs or DRQs.
10300d484d24SJohn Baldwin 	 *
10310d484d24SJohn Baldwin 	 * XXX: We may want to revisit this to be more lenient and wire
10320d484d24SJohn Baldwin 	 * as long as it gets one match.
10330d484d24SJohn Baldwin 	 */
10340d484d24SJohn Baldwin 	matches = 0;
10350d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "port", &value) == 0) {
10362fcd493cSJohn Baldwin 	    /*
10372fcd493cSJohn Baldwin 	     * Floppy drive controllers are notorious for having a
10382fcd493cSJohn Baldwin 	     * wide variety of resources not all of which include the
10392fcd493cSJohn Baldwin 	     * first port that is specified by the hint (typically
10402fcd493cSJohn Baldwin 	     * 0x3f0) (see the comment above fdc_isa_alloc_resources()
10412fcd493cSJohn Baldwin 	     * in fdc_isa.c).  However, they do all seem to include
10422fcd493cSJohn Baldwin 	     * port + 2 (e.g. 0x3f2) so for a floppy device, look for
10432fcd493cSJohn Baldwin 	     * 'value + 2' in the port resources instead of the hint
10442fcd493cSJohn Baldwin 	     * value.
10452fcd493cSJohn Baldwin 	     */
10462fcd493cSJohn Baldwin 	    if (strcmp(name, "fdc") == 0)
10472fcd493cSJohn Baldwin 		value += 2;
10480d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
10490d484d24SJohn Baldwin 		matches++;
10500d484d24SJohn Baldwin 	    else
10510d484d24SJohn Baldwin 		continue;
10520d484d24SJohn Baldwin 	}
10530d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "maddr", &value) == 0) {
10540d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
10550d484d24SJohn Baldwin 		matches++;
10560d484d24SJohn Baldwin 	    else
10570d484d24SJohn Baldwin 		continue;
10580d484d24SJohn Baldwin 	}
10592fcd493cSJohn Baldwin 	if (matches > 0)
10602fcd493cSJohn Baldwin 	    goto matched;
10610d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "irq", &value) == 0) {
10620d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
10630d484d24SJohn Baldwin 		matches++;
10640d484d24SJohn Baldwin 	    else
10650d484d24SJohn Baldwin 		continue;
10660d484d24SJohn Baldwin 	}
10670d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "drq", &value) == 0) {
10680d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
10690d484d24SJohn Baldwin 		matches++;
10700d484d24SJohn Baldwin 	    else
10710d484d24SJohn Baldwin 		continue;
10720d484d24SJohn Baldwin 	}
10730d484d24SJohn Baldwin 
10742fcd493cSJohn Baldwin     matched:
10750d484d24SJohn Baldwin 	if (matches > 0) {
10760d484d24SJohn Baldwin 	    /* We have a winner! */
10770d484d24SJohn Baldwin 	    *unitp = unit;
10780d484d24SJohn Baldwin 	    break;
10790d484d24SJohn Baldwin 	}
10800d484d24SJohn Baldwin     }
10810d484d24SJohn Baldwin }
10820d484d24SJohn Baldwin 
1083adad4744SNate Lawson /*
10848d791e5aSJohn Baldwin  * Fetch the NUMA domain for a device by mapping the value returned by
10858d791e5aSJohn Baldwin  * _PXM to a NUMA domain.  If the device does not have a _PXM method,
10868d791e5aSJohn Baldwin  * -2 is returned.  If any other error occurs, -1 is returned.
10875d18c60aSAdrian Chadd  */
10888d791e5aSJohn Baldwin static int
10898d791e5aSJohn Baldwin acpi_parse_pxm(device_t dev)
10905d18c60aSAdrian Chadd {
109162d70a81SJohn Baldwin #ifdef DEVICE_NUMA
10928d791e5aSJohn Baldwin 	ACPI_HANDLE handle;
10938d791e5aSJohn Baldwin 	ACPI_STATUS status;
10948d791e5aSJohn Baldwin 	int pxm;
10955d18c60aSAdrian Chadd 
10968d791e5aSJohn Baldwin 	handle = acpi_get_handle(dev);
10978d791e5aSJohn Baldwin 	if (handle == NULL)
10988d791e5aSJohn Baldwin 		return (-2);
10998d791e5aSJohn Baldwin 	status = acpi_GetInteger(handle, "_PXM", &pxm);
11008d791e5aSJohn Baldwin 	if (ACPI_SUCCESS(status))
11018d791e5aSJohn Baldwin 		return (acpi_map_pxm_to_vm_domainid(pxm));
11028d791e5aSJohn Baldwin 	if (status == AE_NOT_FOUND)
11038d791e5aSJohn Baldwin 		return (-2);
11048a08b7d3SJohn Baldwin #endif
11058d791e5aSJohn Baldwin 	return (-1);
11068d791e5aSJohn Baldwin }
11078a08b7d3SJohn Baldwin 
11088d791e5aSJohn Baldwin int
11098d791e5aSJohn Baldwin acpi_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
11108d791e5aSJohn Baldwin     cpuset_t *cpuset)
11118d791e5aSJohn Baldwin {
11128d791e5aSJohn Baldwin 	int d, error;
11138d791e5aSJohn Baldwin 
11148d791e5aSJohn Baldwin 	d = acpi_parse_pxm(child);
11158d791e5aSJohn Baldwin 	if (d < 0)
11168d791e5aSJohn Baldwin 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
11178d791e5aSJohn Baldwin 
11188d791e5aSJohn Baldwin 	switch (op) {
11198d791e5aSJohn Baldwin 	case LOCAL_CPUS:
11208d791e5aSJohn Baldwin 		if (setsize != sizeof(cpuset_t))
11218d791e5aSJohn Baldwin 			return (EINVAL);
11228d791e5aSJohn Baldwin 		*cpuset = cpuset_domain[d];
11238a08b7d3SJohn Baldwin 		return (0);
11248d791e5aSJohn Baldwin 	case INTR_CPUS:
11258d791e5aSJohn Baldwin 		error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
11268d791e5aSJohn Baldwin 		if (error != 0)
11278d791e5aSJohn Baldwin 			return (error);
11288d791e5aSJohn Baldwin 		if (setsize != sizeof(cpuset_t))
11298d791e5aSJohn Baldwin 			return (EINVAL);
11308d791e5aSJohn Baldwin 		CPU_AND(cpuset, &cpuset_domain[d]);
11318d791e5aSJohn Baldwin 		return (0);
11328d791e5aSJohn Baldwin 	default:
11338d791e5aSJohn Baldwin 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
11348d791e5aSJohn Baldwin 	}
11355d18c60aSAdrian Chadd }
11365d18c60aSAdrian Chadd 
11375d18c60aSAdrian Chadd /*
11388d791e5aSJohn Baldwin  * Fetch the NUMA domain for the given device 'dev'.
1139ffcf962dSAdrian Chadd  *
1140ffcf962dSAdrian Chadd  * If a device has a _PXM method, map that to a NUMA domain.
11418d791e5aSJohn Baldwin  * Otherwise, pass the request up to the parent.
11428d791e5aSJohn Baldwin  * If there's no matching domain or the domain cannot be
11438d791e5aSJohn Baldwin  * determined, return ENOENT.
1144ffcf962dSAdrian Chadd  */
1145ffcf962dSAdrian Chadd int
1146ffcf962dSAdrian Chadd acpi_get_domain(device_t dev, device_t child, int *domain)
1147ffcf962dSAdrian Chadd {
11488d791e5aSJohn Baldwin 	int d;
1149ffcf962dSAdrian Chadd 
11508d791e5aSJohn Baldwin 	d = acpi_parse_pxm(child);
11518d791e5aSJohn Baldwin 	if (d >= 0) {
11528d791e5aSJohn Baldwin 		*domain = d;
11538a08b7d3SJohn Baldwin 		return (0);
11548d791e5aSJohn Baldwin 	}
11558d791e5aSJohn Baldwin 	if (d == -1)
11568d791e5aSJohn Baldwin 		return (ENOENT);
11575d18c60aSAdrian Chadd 
1158ffcf962dSAdrian Chadd 	/* No _PXM node; go up a level */
1159ffcf962dSAdrian Chadd 	return (bus_generic_get_domain(dev, child, domain));
1160ffcf962dSAdrian Chadd }
1161ffcf962dSAdrian Chadd 
1162ffcf962dSAdrian Chadd /*
1163adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
1164adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
1165adad4744SNate Lawson  */
1166adad4744SNate Lawson static int
1167adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
1168adad4744SNate Lawson {
1169adad4744SNate Lawson     struct resource *res;
1170adad4744SNate Lawson     struct resource_list *rl;
1171adad4744SNate Lawson     struct resource_list_entry *rle;
1172adad4744SNate Lawson     struct rman *rm;
1173da72d149SNate Lawson     char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1174da72d149SNate Lawson     device_t *children;
1175da72d149SNate Lawson     int child_count, i;
1176da72d149SNate Lawson 
1177da72d149SNate Lawson     /*
1178da72d149SNate Lawson      * Probe/attach any sysresource devices.  This would be unnecessary if we
1179da72d149SNate Lawson      * had multi-pass probe/attach.
1180da72d149SNate Lawson      */
1181da72d149SNate Lawson     if (device_get_children(dev, &children, &child_count) != 0)
1182da72d149SNate Lawson 	return (ENXIO);
1183da72d149SNate Lawson     for (i = 0; i < child_count; i++) {
1184da72d149SNate Lawson 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1185da72d149SNate Lawson 	    device_probe_and_attach(children[i]);
1186da72d149SNate Lawson     }
1187da72d149SNate Lawson     free(children, M_TEMP);
1188adad4744SNate Lawson 
1189adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
1190be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
1191adad4744SNate Lawson 	if (rle->res != NULL) {
1192da1b038aSJustin Hibbits 	    device_printf(dev, "duplicate resource for %jx\n", rle->start);
1193adad4744SNate Lawson 	    continue;
1194adad4744SNate Lawson 	}
1195adad4744SNate Lawson 
1196adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
1197adad4744SNate Lawson 	switch (rle->type) {
1198adad4744SNate Lawson 	case SYS_RES_IOPORT:
1199adad4744SNate Lawson 	    rm = &acpi_rman_io;
1200adad4744SNate Lawson 	    break;
1201adad4744SNate Lawson 	case SYS_RES_MEMORY:
1202adad4744SNate Lawson 	    rm = &acpi_rman_mem;
1203adad4744SNate Lawson 	    break;
1204adad4744SNate Lawson 	default:
1205adad4744SNate Lawson 	    continue;
1206adad4744SNate Lawson 	}
1207adad4744SNate Lawson 
1208adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
1209adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
1210adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
1211adad4744SNate Lawson 	if (res != NULL) {
1212adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1213adad4744SNate Lawson 	    rle->res = res;
121444947d3aSJohn Baldwin 	} else if (bootverbose)
1215da1b038aSJustin Hibbits 	    device_printf(dev, "reservation of %jx, %jx (%d) failed\n",
1216adad4744SNate Lawson 		rle->start, rle->count, rle->type);
1217adad4744SNate Lawson     }
1218adad4744SNate Lawson     return (0);
1219adad4744SNate Lawson }
1220adad4744SNate Lawson 
1221ea233199SJohn Baldwin static char *pcilink_ids[] = { "PNP0C0F", NULL };
1222ea233199SJohn Baldwin static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1223ea233199SJohn Baldwin 
1224ea233199SJohn Baldwin /*
1225ea233199SJohn Baldwin  * Reserve declared resources for devices found during attach once system
1226ea233199SJohn Baldwin  * resources have been allocated.
1227ea233199SJohn Baldwin  */
1228ea233199SJohn Baldwin static void
1229ea233199SJohn Baldwin acpi_reserve_resources(device_t dev)
1230ea233199SJohn Baldwin {
1231ea233199SJohn Baldwin     struct resource_list_entry *rle;
1232ea233199SJohn Baldwin     struct resource_list *rl;
1233ea233199SJohn Baldwin     struct acpi_device *ad;
1234ea233199SJohn Baldwin     struct acpi_softc *sc;
1235ea233199SJohn Baldwin     device_t *children;
1236ea233199SJohn Baldwin     int child_count, i;
1237ea233199SJohn Baldwin 
1238ea233199SJohn Baldwin     sc = device_get_softc(dev);
1239ea233199SJohn Baldwin     if (device_get_children(dev, &children, &child_count) != 0)
1240ea233199SJohn Baldwin 	return;
1241ea233199SJohn Baldwin     for (i = 0; i < child_count; i++) {
1242ea233199SJohn Baldwin 	ad = device_get_ivars(children[i]);
1243ea233199SJohn Baldwin 	rl = &ad->ad_rl;
1244ea233199SJohn Baldwin 
1245ea233199SJohn Baldwin 	/* Don't reserve system resources. */
1246ea233199SJohn Baldwin 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1247ea233199SJohn Baldwin 	    continue;
1248ea233199SJohn Baldwin 
1249ea233199SJohn Baldwin 	STAILQ_FOREACH(rle, rl, link) {
1250ea233199SJohn Baldwin 	    /*
1251ea233199SJohn Baldwin 	     * Don't reserve IRQ resources.  There are many sticky things
1252ea233199SJohn Baldwin 	     * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET
1253ea233199SJohn Baldwin 	     * when using legacy routing).
1254ea233199SJohn Baldwin 	     */
1255ea233199SJohn Baldwin 	    if (rle->type == SYS_RES_IRQ)
1256ea233199SJohn Baldwin 		continue;
1257ea233199SJohn Baldwin 
1258ea233199SJohn Baldwin 	    /*
12590d81cf12SJohn Baldwin 	     * Don't reserve the resource if it is already allocated.
12600d81cf12SJohn Baldwin 	     * The acpi_ec(4) driver can allocate its resources early
12610d81cf12SJohn Baldwin 	     * if ECDT is present.
12620d81cf12SJohn Baldwin 	     */
12630d81cf12SJohn Baldwin 	    if (rle->res != NULL)
12640d81cf12SJohn Baldwin 		continue;
12650d81cf12SJohn Baldwin 
12660d81cf12SJohn Baldwin 	    /*
1267ea233199SJohn Baldwin 	     * Try to reserve the resource from our parent.  If this
1268ea233199SJohn Baldwin 	     * fails because the resource is a system resource, just
1269ea233199SJohn Baldwin 	     * let it be.  The resource range is already reserved so
1270ea233199SJohn Baldwin 	     * that other devices will not use it.  If the driver
1271ea233199SJohn Baldwin 	     * needs to allocate the resource, then
1272ea233199SJohn Baldwin 	     * acpi_alloc_resource() will sub-alloc from the system
1273ea233199SJohn Baldwin 	     * resource.
1274ea233199SJohn Baldwin 	     */
1275ea233199SJohn Baldwin 	    resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid,
1276ea233199SJohn Baldwin 		rle->start, rle->end, rle->count, 0);
1277ea233199SJohn Baldwin 	}
1278ea233199SJohn Baldwin     }
1279ea233199SJohn Baldwin     free(children, M_TEMP);
1280ea233199SJohn Baldwin     sc->acpi_resources_reserved = 1;
1281ea233199SJohn Baldwin }
1282ea233199SJohn Baldwin 
1283ea233199SJohn Baldwin static int
1284ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid,
12852dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t count)
1286ea233199SJohn Baldwin {
1287ea233199SJohn Baldwin     struct acpi_softc *sc = device_get_softc(dev);
1288ea233199SJohn Baldwin     struct acpi_device *ad = device_get_ivars(child);
1289ea233199SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
1290f9a22a06SAndrew Turner #if defined(__i386__) || defined(__amd64__)
12917e3343bfSJohn Baldwin     ACPI_DEVICE_INFO *devinfo;
1292f9a22a06SAndrew Turner #endif
12932dd1bdf1SJustin Hibbits     rman_res_t end;
1294ea233199SJohn Baldwin 
1295ea233199SJohn Baldwin     /* Ignore IRQ resources for PCI link devices. */
1296ea233199SJohn Baldwin     if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL)
1297ea233199SJohn Baldwin 	return (0);
1298ea233199SJohn Baldwin 
12997e3343bfSJohn Baldwin     /*
1300d2dc06caSJohn Baldwin      * Ignore most resources for PCI root bridges.  Some BIOSes
13017e3343bfSJohn Baldwin      * incorrectly enumerate the memory ranges they decode as plain
1302d2dc06caSJohn Baldwin      * memory resources instead of as ResourceProducer ranges.  Other
1303d2dc06caSJohn Baldwin      * BIOSes incorrectly list system resource entries for I/O ranges
1304d2dc06caSJohn Baldwin      * under the PCI bridge.  Do allow the one known-correct case on
1305d2dc06caSJohn Baldwin      * x86 of a PCI bridge claiming the I/O ports used for PCI config
1306d2dc06caSJohn Baldwin      * access.
13077e3343bfSJohn Baldwin      */
1308f9a22a06SAndrew Turner #if defined(__i386__) || defined(__amd64__)
1309d2dc06caSJohn Baldwin     if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
13107e3343bfSJohn Baldwin 	if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) {
13117e3343bfSJohn Baldwin 	    if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) {
1312f9a22a06SAndrew Turner 		if (!(type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT)) {
13137e3343bfSJohn Baldwin 		    AcpiOsFree(devinfo);
13147e3343bfSJohn Baldwin 		    return (0);
13157e3343bfSJohn Baldwin 		}
1316d2dc06caSJohn Baldwin 	    }
13177e3343bfSJohn Baldwin 	    AcpiOsFree(devinfo);
13187e3343bfSJohn Baldwin 	}
13197e3343bfSJohn Baldwin     }
1320f9a22a06SAndrew Turner #endif
13217e3343bfSJohn Baldwin 
1322ea233199SJohn Baldwin     /* If the resource is already allocated, fail. */
1323ea233199SJohn Baldwin     if (resource_list_busy(rl, type, rid))
1324ea233199SJohn Baldwin 	return (EBUSY);
1325ea233199SJohn Baldwin 
1326ea233199SJohn Baldwin     /* If the resource is already reserved, release it. */
1327ea233199SJohn Baldwin     if (resource_list_reserved(rl, type, rid))
1328ea233199SJohn Baldwin 	resource_list_unreserve(rl, dev, child, type, rid);
1329ea233199SJohn Baldwin 
1330ea233199SJohn Baldwin     /* Add the resource. */
1331ea233199SJohn Baldwin     end = (start + count - 1);
1332ea233199SJohn Baldwin     resource_list_add(rl, type, rid, start, end, count);
1333ea233199SJohn Baldwin 
1334ea233199SJohn Baldwin     /* Don't reserve resources until the system resources are allocated. */
1335ea233199SJohn Baldwin     if (!sc->acpi_resources_reserved)
1336ea233199SJohn Baldwin 	return (0);
1337ea233199SJohn Baldwin 
1338ea233199SJohn Baldwin     /* Don't reserve system resources. */
1339ea233199SJohn Baldwin     if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL)
1340ea233199SJohn Baldwin 	return (0);
1341ea233199SJohn Baldwin 
1342ea233199SJohn Baldwin     /*
1343ea233199SJohn Baldwin      * Don't reserve IRQ resources.  There are many sticky things to
1344ea233199SJohn Baldwin      * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when
1345ea233199SJohn Baldwin      * using legacy routing).
1346ea233199SJohn Baldwin      */
1347ea233199SJohn Baldwin     if (type == SYS_RES_IRQ)
1348ea233199SJohn Baldwin 	return (0);
1349ea233199SJohn Baldwin 
1350ea233199SJohn Baldwin     /*
1351ea233199SJohn Baldwin      * Reserve the resource.
1352ea233199SJohn Baldwin      *
1353ea233199SJohn Baldwin      * XXX: Ignores failure for now.  Failure here is probably a
1354ea233199SJohn Baldwin      * BIOS/firmware bug?
1355ea233199SJohn Baldwin      */
1356ea233199SJohn Baldwin     resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0);
1357ea233199SJohn Baldwin     return (0);
1358ea233199SJohn Baldwin }
1359ea233199SJohn Baldwin 
136015e32d5dSMike Smith static struct resource *
136115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
13622dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
136315e32d5dSMike Smith {
136495957f62SJohn Baldwin     ACPI_RESOURCE ares;
1365ea233199SJohn Baldwin     struct acpi_device *ad;
136691233413SNate Lawson     struct resource_list_entry *rle;
1367ea233199SJohn Baldwin     struct resource_list *rl;
136891233413SNate Lawson     struct resource *res;
13697915adb5SJustin Hibbits     int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
137015e2f34fSNate Lawson 
137191233413SNate Lawson     /*
1372ea233199SJohn Baldwin      * First attempt at allocating the resource.  For direct children,
1373ea233199SJohn Baldwin      * use resource_list_alloc() to handle reserved resources.  For
1374f6133e71SJohn Baldwin      * other devices, pass the request up to our parent.
137591233413SNate Lawson      */
1376ea233199SJohn Baldwin     if (bus == device_get_parent(child)) {
1377ea233199SJohn Baldwin 	ad = device_get_ivars(child);
1378ea233199SJohn Baldwin 	rl = &ad->ad_rl;
137991233413SNate Lawson 
1380397c30a8SJohn Baldwin 	/*
1381ea233199SJohn Baldwin 	 * Simulate the behavior of the ISA bus for direct children
1382ea233199SJohn Baldwin 	 * devices.  That is, if a non-default range is specified for
1383ea233199SJohn Baldwin 	 * a resource that doesn't exist, use bus_set_resource() to
1384ea233199SJohn Baldwin 	 * add the resource before allocating it.  Note that these
1385ea233199SJohn Baldwin 	 * resources will not be reserved.
1386397c30a8SJohn Baldwin 	 */
1387ea233199SJohn Baldwin 	if (!isdefault && resource_list_find(rl, type, *rid) == NULL)
1388ea233199SJohn Baldwin 		resource_list_add(rl, type, *rid, start, end, count);
1389ea233199SJohn Baldwin 	res = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
1390ea233199SJohn Baldwin 	    flags);
1391ea233199SJohn Baldwin 	if (res != NULL && type == SYS_RES_IRQ) {
139295957f62SJohn Baldwin 	    /*
139395957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
139495957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
139595957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
139695957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
139795957f62SJohn Baldwin 	     *
139895957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
139995957f62SJohn Baldwin 	     */
140095957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
140195957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
140295957f62SJohn Baldwin 	}
140315e2f34fSNate Lawson 
1404ea233199SJohn Baldwin 	/*
1405ea233199SJohn Baldwin 	 * If this is an allocation of the "default" range for a given
1406ea233199SJohn Baldwin 	 * RID, fetch the exact bounds for this resource from the
1407ea233199SJohn Baldwin 	 * resource list entry to try to allocate the range from the
1408ea233199SJohn Baldwin 	 * system resource regions.
1409ea233199SJohn Baldwin 	 */
1410ea233199SJohn Baldwin 	if (res == NULL && isdefault) {
1411ea233199SJohn Baldwin 	    rle = resource_list_find(rl, type, *rid);
1412ea233199SJohn Baldwin 	    if (rle != NULL) {
1413ea233199SJohn Baldwin 		start = rle->start;
1414ea233199SJohn Baldwin 		end = rle->end;
1415ea233199SJohn Baldwin 		count = rle->count;
1416ea233199SJohn Baldwin 	    }
1417ea233199SJohn Baldwin 	}
1418ea233199SJohn Baldwin     } else
1419ea233199SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
1420ea233199SJohn Baldwin 	    start, end, count, flags);
1421ea233199SJohn Baldwin 
1422ea233199SJohn Baldwin     /*
1423ea233199SJohn Baldwin      * If the first attempt failed and this is an allocation of a
1424ea233199SJohn Baldwin      * specific range, try to satisfy the request via a suballocation
14255d0d779bSJohn Baldwin      * from our system resource regions.
1426ea233199SJohn Baldwin      */
14275d0d779bSJohn Baldwin     if (res == NULL && start + count - 1 == end)
14285d0d779bSJohn Baldwin 	res = acpi_alloc_sysres(child, type, rid, start, end, count, flags);
14295d0d779bSJohn Baldwin     return (res);
14305d0d779bSJohn Baldwin }
14315d0d779bSJohn Baldwin 
14325d0d779bSJohn Baldwin /*
14335d0d779bSJohn Baldwin  * Attempt to allocate a specific resource range from the system
14345d0d779bSJohn Baldwin  * resource ranges.  Note that we only handle memory and I/O port
14355d0d779bSJohn Baldwin  * system resources.
14365d0d779bSJohn Baldwin  */
14375d0d779bSJohn Baldwin struct resource *
14382dd1bdf1SJustin Hibbits acpi_alloc_sysres(device_t child, int type, int *rid, rman_res_t start,
14392dd1bdf1SJustin Hibbits     rman_res_t end, rman_res_t count, u_int flags)
14405d0d779bSJohn Baldwin {
14415d0d779bSJohn Baldwin     struct rman *rm;
14425d0d779bSJohn Baldwin     struct resource *res;
14435d0d779bSJohn Baldwin 
1444ea233199SJohn Baldwin     switch (type) {
1445ea233199SJohn Baldwin     case SYS_RES_IOPORT:
1446ea233199SJohn Baldwin 	rm = &acpi_rman_io;
1447ea233199SJohn Baldwin 	break;
1448ea233199SJohn Baldwin     case SYS_RES_MEMORY:
1449ea233199SJohn Baldwin 	rm = &acpi_rman_mem;
1450ea233199SJohn Baldwin 	break;
1451ea233199SJohn Baldwin     default:
1452ea233199SJohn Baldwin 	return (NULL);
1453ea233199SJohn Baldwin     }
1454ea233199SJohn Baldwin 
14555d0d779bSJohn Baldwin     KASSERT(start + count - 1 == end, ("wildcard resource range"));
1456ea233199SJohn Baldwin     res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1457ea233199SJohn Baldwin 	child);
1458ea233199SJohn Baldwin     if (res == NULL)
1459ea233199SJohn Baldwin 	return (NULL);
1460ea233199SJohn Baldwin 
1461ea233199SJohn Baldwin     rman_set_rid(res, *rid);
1462ea233199SJohn Baldwin 
1463ea233199SJohn Baldwin     /* If requested, activate the resource using the parent's method. */
1464ea233199SJohn Baldwin     if (flags & RF_ACTIVE)
1465ea233199SJohn Baldwin 	if (bus_activate_resource(child, type, *rid, res) != 0) {
1466ea233199SJohn Baldwin 	    rman_release_resource(res);
1467ea233199SJohn Baldwin 	    return (NULL);
1468ea233199SJohn Baldwin 	}
1469ea233199SJohn Baldwin 
147091233413SNate Lawson     return (res);
147115e32d5dSMike Smith }
147215e32d5dSMike Smith 
147315e32d5dSMike Smith static int
1474049dc0d1SJohn Baldwin acpi_is_resource_managed(int type, struct resource *r)
147515e32d5dSMike Smith {
147615e32d5dSMike Smith 
1477397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1478397c30a8SJohn Baldwin     switch (type) {
1479397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1480049dc0d1SJohn Baldwin 	return (rman_is_region_manager(r, &acpi_rman_io));
1481397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1482049dc0d1SJohn Baldwin 	return (rman_is_region_manager(r, &acpi_rman_mem));
1483397c30a8SJohn Baldwin     }
1484049dc0d1SJohn Baldwin     return (0);
1485049dc0d1SJohn Baldwin }
1486049dc0d1SJohn Baldwin 
1487049dc0d1SJohn Baldwin static int
1488049dc0d1SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
14892dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end)
1490049dc0d1SJohn Baldwin {
1491049dc0d1SJohn Baldwin 
1492049dc0d1SJohn Baldwin     if (acpi_is_resource_managed(type, r))
1493049dc0d1SJohn Baldwin 	return (rman_adjust_resource(r, start, end));
1494049dc0d1SJohn Baldwin     return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1495049dc0d1SJohn Baldwin }
1496049dc0d1SJohn Baldwin 
1497049dc0d1SJohn Baldwin static int
1498049dc0d1SJohn Baldwin acpi_release_resource(device_t bus, device_t child, int type, int rid,
1499049dc0d1SJohn Baldwin     struct resource *r)
1500049dc0d1SJohn Baldwin {
1501049dc0d1SJohn Baldwin     int ret;
1502397c30a8SJohn Baldwin 
150391233413SNate Lawson     /*
1504397c30a8SJohn Baldwin      * If this resource belongs to one of our internal managers,
1505ea233199SJohn Baldwin      * deactivate it and release it to the local pool.
150691233413SNate Lawson      */
1507049dc0d1SJohn Baldwin     if (acpi_is_resource_managed(type, r)) {
150891233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
150991233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
151091233413SNate Lawson 	    if (ret != 0)
151191233413SNate Lawson 		return (ret);
151215e32d5dSMike Smith 	}
1513ea233199SJohn Baldwin 	return (rman_release_resource(r));
1514ea233199SJohn Baldwin     }
1515ea233199SJohn Baldwin 
1516ea233199SJohn Baldwin     return (bus_generic_rl_release_resource(bus, child, type, rid, r));
1517ea233199SJohn Baldwin }
151815e32d5dSMike Smith 
15198b28b622SNate Lawson static void
15208b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
15218b28b622SNate Lawson {
15228b28b622SNate Lawson     struct resource_list *rl;
15238b28b622SNate Lawson 
15248b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
1525ea233199SJohn Baldwin     if (resource_list_busy(rl, type, rid)) {
1526ea233199SJohn Baldwin 	device_printf(bus, "delete_resource: Resource still owned by child"
1527ea233199SJohn Baldwin 	    " (type=%d, rid=%d)\n", type, rid);
1528ea233199SJohn Baldwin 	return;
1529ea233199SJohn Baldwin     }
1530ea233199SJohn Baldwin     resource_list_unreserve(rl, bus, child, type, rid);
15318b28b622SNate Lawson     resource_list_delete(rl, type, rid);
15328b28b622SNate Lawson }
15338b28b622SNate Lawson 
1534dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1535e1c4bf3fSNate Lawson int
1536e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1537907b6777SNate Lawson     struct resource **res, u_int flags)
1538dc750869SNate Lawson {
1539e1c4bf3fSNate Lawson     int error, res_type;
1540dc750869SNate Lawson 
1541e1c4bf3fSNate Lawson     error = ENOMEM;
15428f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1543e1c4bf3fSNate Lawson 	return (EINVAL);
1544dc750869SNate Lawson 
15458f118e25SNate Lawson     /* We only support memory and IO spaces. */
15462be4e471SJung-uk Kim     switch (gas->SpaceId) {
1547dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1548e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1549dc750869SNate Lawson 	break;
1550dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1551e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1552dc750869SNate Lawson 	break;
1553dc750869SNate Lawson     default:
1554e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1555dc750869SNate Lawson     }
1556dc750869SNate Lawson 
15572fe912dfSNate Lawson     /*
1558f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1559f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
15602fe912dfSNate Lawson      */
15612be4e471SJung-uk Kim     if (gas->BitWidth && gas->BitWidth < 8)
15622be4e471SJung-uk Kim 	gas->BitWidth = 8;
15632fe912dfSNate Lawson 
15648f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
15652be4e471SJung-uk Kim     if (gas->Address == 0 || gas->BitWidth == 0)
15668f118e25SNate Lawson 	return (EINVAL);
15678f118e25SNate Lawson 
1568e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
15692be4e471SJung-uk Kim 	gas->BitWidth / 8);
1570907b6777SNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1571e1c4bf3fSNate Lawson     if (*res != NULL) {
1572e1c4bf3fSNate Lawson 	*type = res_type;
1573e1c4bf3fSNate Lawson 	error = 0;
1574ca2c69c8SNate Lawson     } else
1575ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1576ca2c69c8SNate Lawson 
1577e1c4bf3fSNate Lawson     return (error);
1578dc750869SNate Lawson }
1579dc750869SNate Lawson 
1580c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
15811e4925e8SNate Lawson static uint32_t
158232d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1583bc0f2195SMike Smith {
15841e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1585bc0f2195SMike Smith     ACPI_HANDLE		h;
158692488a57SJung-uk Kim     uint32_t		pnpid;
158732d18aa5SMike Smith 
1588b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
158932d18aa5SMike Smith 
15906fca9360SNate Lawson     /* Fetch and validate the HID. */
159192488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
159292488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
159392488a57SJung-uk Kim 	return_VALUE (0);
159432d18aa5SMike Smith 
159592488a57SJung-uk Kim     pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
159692488a57SJung-uk Kim 	devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
159792488a57SJung-uk Kim 	PNP_EISAID(devinfo->HardwareId.String) : 0;
159892488a57SJung-uk Kim     AcpiOsFree(devinfo);
1599be2b1797SNate Lawson 
160032d18aa5SMike Smith     return_VALUE (pnpid);
160132d18aa5SMike Smith }
160232d18aa5SMike Smith 
16031e4925e8SNate Lawson static int
16041e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1605b2566207STakanori Watanabe {
16061e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
16078ef1a331SJung-uk Kim     ACPI_PNP_DEVICE_ID	*ids;
1608b2566207STakanori Watanabe     ACPI_HANDLE		h;
16091e4925e8SNate Lawson     uint32_t		*pnpid;
161092488a57SJung-uk Kim     int			i, valid;
1611b2566207STakanori Watanabe 
1612b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1613b2566207STakanori Watanabe 
16141e4925e8SNate Lawson     pnpid = cids;
1615bd189fe7SNate Lawson 
16161e4925e8SNate Lawson     /* Fetch and validate the CID */
161792488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
161892488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
161992488a57SJung-uk Kim 	return_VALUE (0);
1620b2566207STakanori Watanabe 
162192488a57SJung-uk Kim     if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
162292488a57SJung-uk Kim 	AcpiOsFree(devinfo);
162392488a57SJung-uk Kim 	return_VALUE (0);
1624b2566207STakanori Watanabe     }
1625b2566207STakanori Watanabe 
162692488a57SJung-uk Kim     if (devinfo->CompatibleIdList.Count < count)
162792488a57SJung-uk Kim 	count = devinfo->CompatibleIdList.Count;
162892488a57SJung-uk Kim     ids = devinfo->CompatibleIdList.Ids;
162992488a57SJung-uk Kim     for (i = 0, valid = 0; i < count; i++)
163092488a57SJung-uk Kim 	if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
163192488a57SJung-uk Kim 	    strncmp(ids[i].String, "PNP", 3) == 0) {
163292488a57SJung-uk Kim 	    *pnpid++ = PNP_EISAID(ids[i].String);
163392488a57SJung-uk Kim 	    valid++;
163492488a57SJung-uk Kim 	}
163592488a57SJung-uk Kim     AcpiOsFree(devinfo);
163692488a57SJung-uk Kim 
16371e4925e8SNate Lawson     return_VALUE (valid);
16381e4925e8SNate Lawson }
1639b2566207STakanori Watanabe 
1640ce619b2eSNate Lawson static char *
1641ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1642ce619b2eSNate Lawson {
1643ce619b2eSNate Lawson     ACPI_HANDLE h;
164492488a57SJung-uk Kim     ACPI_OBJECT_TYPE t;
1645ce619b2eSNate Lawson     int i;
1646ce619b2eSNate Lawson 
1647ce619b2eSNate Lawson     h = acpi_get_handle(dev);
164892488a57SJung-uk Kim     if (ids == NULL || h == NULL)
164992488a57SJung-uk Kim 	return (NULL);
165092488a57SJung-uk Kim     t = acpi_get_type(dev);
165192488a57SJung-uk Kim     if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
1652ce619b2eSNate Lawson 	return (NULL);
1653ce619b2eSNate Lawson 
1654ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1655ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1656ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1657ce619b2eSNate Lawson 	    return (ids[i]);
1658ce619b2eSNate Lawson     }
1659ce619b2eSNate Lawson     return (NULL);
1660ce619b2eSNate Lawson }
1661ce619b2eSNate Lawson 
1662ce619b2eSNate Lawson static ACPI_STATUS
1663ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1664ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1665ce619b2eSNate Lawson {
1666ce619b2eSNate Lawson     ACPI_HANDLE h;
1667ce619b2eSNate Lawson 
1668df8d2a32SNate Lawson     if (dev == NULL)
1669df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1670df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1671ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1672ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1673ce619b2eSNate Lawson }
1674ce619b2eSNate Lawson 
167562508c53SJohn Baldwin int
167610ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
167710ce62b9SNate Lawson {
167810ce62b9SNate Lawson     struct acpi_softc *sc;
167910ce62b9SNate Lawson     ACPI_HANDLE handle;
168010ce62b9SNate Lawson     ACPI_STATUS status;
168110ce62b9SNate Lawson     char sxd[8];
168210ce62b9SNate Lawson 
168310ce62b9SNate Lawson     handle = acpi_get_handle(dev);
168410ce62b9SNate Lawson 
168510ce62b9SNate Lawson     /*
168610ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
168710ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
168810ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
168910ce62b9SNate Lawson      * need special handling in their drivers.
169010ce62b9SNate Lawson      */
1691a7a3177fSJung-uk Kim     if (dstate == NULL || handle == NULL ||
169210ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
169310ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
169410ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
169510ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
169610ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
169710ce62b9SNate Lawson 	return (ENXIO);
169810ce62b9SNate Lawson 
169910ce62b9SNate Lawson     /*
1700a7a3177fSJung-uk Kim      * Override next state with the value from _SxD, if present.
1701a7a3177fSJung-uk Kim      * Note illegal _S0D is evaluated because some systems expect this.
170210ce62b9SNate Lawson      */
1703a7a3177fSJung-uk Kim     sc = device_get_softc(bus);
170410ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
170510ce62b9SNate Lawson     status = acpi_GetInteger(handle, sxd, dstate);
1706a7a3177fSJung-uk Kim     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
1707a7a3177fSJung-uk Kim 	    device_printf(dev, "failed to get %s on %s: %s\n", sxd,
1708a7a3177fSJung-uk Kim 		acpi_name(handle), AcpiFormatException(status));
1709a7a3177fSJung-uk Kim 	    return (ENXIO);
171010ce62b9SNate Lawson     }
171110ce62b9SNate Lawson 
1712a7a3177fSJung-uk Kim     return (0);
171310ce62b9SNate Lawson }
171410ce62b9SNate Lawson 
1715df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1716df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1717df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1718df8d2a32SNate Lawson     void		*arg;
1719df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1720df8d2a32SNate Lawson };
1721df8d2a32SNate Lawson 
1722ce619b2eSNate Lawson static ACPI_STATUS
1723df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1724df8d2a32SNate Lawson {
1725df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1726df8d2a32SNate Lawson     device_t dev, old_dev;
1727df8d2a32SNate Lawson     ACPI_STATUS status;
1728df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1729df8d2a32SNate Lawson 
1730df8d2a32SNate Lawson     /*
1731df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1732df8d2a32SNate Lawson      * the parent where the scan began.
1733df8d2a32SNate Lawson      */
1734df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1735df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1736df8d2a32SNate Lawson 	return (AE_OK);
1737df8d2a32SNate Lawson 
1738df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1739df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1740df8d2a32SNate Lawson 	return (AE_OK);
1741df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1742df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1743df8d2a32SNate Lawson 	return (AE_OK);
1744df8d2a32SNate Lawson 
1745df8d2a32SNate Lawson     /*
1746df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1747df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1748df8d2a32SNate Lawson      */
1749df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1750df8d2a32SNate Lawson     dev = old_dev;
1751df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1752df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1753df8d2a32SNate Lawson 	return (status);
1754df8d2a32SNate Lawson 
1755df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1756df8d2a32SNate Lawson     if (old_dev != NULL) {
1757df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1758df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1759df8d2a32SNate Lawson     }
1760df8d2a32SNate Lawson 
1761df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1762df8d2a32SNate Lawson     if (dev != NULL)
1763df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1764df8d2a32SNate Lawson 
1765df8d2a32SNate Lawson     return (AE_OK);
1766df8d2a32SNate Lawson }
1767df8d2a32SNate Lawson 
1768df8d2a32SNate Lawson static ACPI_STATUS
1769df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1770df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1771ce619b2eSNate Lawson {
1772ce619b2eSNate Lawson     ACPI_HANDLE h;
1773df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1774ce619b2eSNate Lawson 
1775df8d2a32SNate Lawson     if (acpi_disabled("children"))
1776df8d2a32SNate Lawson 	return (AE_OK);
1777df8d2a32SNate Lawson 
1778df8d2a32SNate Lawson     if (dev == NULL)
1779df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1780df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1781ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1782df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1783df8d2a32SNate Lawson     ctx.arg = arg;
1784df8d2a32SNate Lawson     ctx.parent = h;
1785df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
17862272d050SJung-uk Kim 	acpi_device_scan_cb, NULL, &ctx, NULL));
1787ce619b2eSNate Lawson }
1788ce619b2eSNate Lawson 
178910ce62b9SNate Lawson /*
179010ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
179110ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
179210ce62b9SNate Lawson  */
179310ce62b9SNate Lawson static int
1794a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state)
179510ce62b9SNate Lawson {
179610ce62b9SNate Lawson     ACPI_HANDLE h;
179710ce62b9SNate Lawson     ACPI_STATUS status;
179810ce62b9SNate Lawson 
179910ce62b9SNate Lawson     h = acpi_get_handle(child);
1800a0e73a12SJung-uk Kim     if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
180110ce62b9SNate Lawson 	return (EINVAL);
180210ce62b9SNate Lawson     if (h == NULL)
180310ce62b9SNate Lawson 	return (0);
180410ce62b9SNate Lawson 
180510ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
180610ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
1807a7a3177fSJung-uk Kim     if (ACPI_SUCCESS(status)) {
1808a7a3177fSJung-uk Kim 	if (bootverbose)
1809a7a3177fSJung-uk Kim 	    device_printf(child, "set ACPI power state D%d on %s\n",
1810a7a3177fSJung-uk Kim 		state, acpi_name(h));
1811a7a3177fSJung-uk Kim     } else if (status != AE_NOT_FOUND)
1812a7a3177fSJung-uk Kim 	device_printf(child,
1813a7a3177fSJung-uk Kim 	    "failed to set ACPI power state D%d on %s: %s\n", state,
1814a7a3177fSJung-uk Kim 	    acpi_name(h), AcpiFormatException(status));
181510ce62b9SNate Lawson 
1816a7a3177fSJung-uk Kim     return (0);
181710ce62b9SNate Lawson }
181810ce62b9SNate Lawson 
181932d18aa5SMike Smith static int
182032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
182132d18aa5SMike Smith {
18221e4925e8SNate Lawson     int			result, cid_count, i;
18231e4925e8SNate Lawson     uint32_t		lid, cids[8];
1824bc0f2195SMike Smith 
1825b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1826bc0f2195SMike Smith 
1827bc0f2195SMike Smith     /*
1828bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1829bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1830bc0f2195SMike Smith      * that to happen, so don't ever return it.
1831bc0f2195SMike Smith      */
1832bc0f2195SMike Smith     result = ENXIO;
1833bc0f2195SMike Smith 
1834be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1835b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
18361e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1837bc0f2195SMike Smith     while (ids && ids->ip_id) {
18381e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1839bc0f2195SMike Smith 	    result = 0;
1840bc0f2195SMike Smith 	    goto out;
1841bc0f2195SMike Smith 	}
18421e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
18431e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
18441e4925e8SNate Lawson 		result = 0;
18451e4925e8SNate Lawson 		goto out;
18461e4925e8SNate Lawson 	    }
18471e4925e8SNate Lawson 	}
1848bc0f2195SMike Smith 	ids++;
1849bc0f2195SMike Smith     }
1850be2b1797SNate Lawson 
1851bc0f2195SMike Smith  out:
18529e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
18539e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
18549e0dd54fSNate Lawson 
1855bc0f2195SMike Smith     return_VALUE (result);
1856bc0f2195SMike Smith }
1857bc0f2195SMike Smith 
1858d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
1859d320e05cSJohn Baldwin /*
1860d320e05cSJohn Baldwin  * Look for a MCFG table.  If it is present, use the settings for
1861d320e05cSJohn Baldwin  * domain (segment) 0 to setup PCI config space access via the memory
1862d320e05cSJohn Baldwin  * map.
1863d320e05cSJohn Baldwin  */
1864d320e05cSJohn Baldwin static void
1865d320e05cSJohn Baldwin acpi_enable_pcie(void)
1866d320e05cSJohn Baldwin {
1867d320e05cSJohn Baldwin 	ACPI_TABLE_HEADER *hdr;
1868d320e05cSJohn Baldwin 	ACPI_MCFG_ALLOCATION *alloc, *end;
1869d320e05cSJohn Baldwin 	ACPI_STATUS status;
1870d320e05cSJohn Baldwin 
1871d320e05cSJohn Baldwin 	status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1872d320e05cSJohn Baldwin 	if (ACPI_FAILURE(status))
1873d320e05cSJohn Baldwin 		return;
1874d320e05cSJohn Baldwin 
1875d320e05cSJohn Baldwin 	end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1876d320e05cSJohn Baldwin 	alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1877d320e05cSJohn Baldwin 	while (alloc < end) {
1878d320e05cSJohn Baldwin 		if (alloc->PciSegment == 0) {
1879d320e05cSJohn Baldwin 			pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1880d320e05cSJohn Baldwin 			    alloc->EndBusNumber);
1881d320e05cSJohn Baldwin 			return;
1882d320e05cSJohn Baldwin 		}
1883d320e05cSJohn Baldwin 		alloc++;
1884d320e05cSJohn Baldwin 	}
1885d320e05cSJohn Baldwin }
1886d320e05cSJohn Baldwin #endif
1887d320e05cSJohn Baldwin 
1888bc0f2195SMike Smith /*
188933332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
189015e32d5dSMike Smith  *
189133332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
189233332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
189333332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
189433332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
189533332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
189615e32d5dSMike Smith  */
189715e32d5dSMike Smith static void
189815e32d5dSMike Smith acpi_probe_children(device_t bus)
189915e32d5dSMike Smith {
190015e32d5dSMike Smith 
1901b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
19020ae55423SMike Smith 
190315e32d5dSMike Smith     /*
190415e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1905edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
190615e32d5dSMike Smith      *
190715e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1908be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1909be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1910be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
191115e32d5dSMike Smith      */
19124c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
191333332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
19142272d050SJung-uk Kim 	NULL, bus, NULL);
191515e32d5dSMike Smith 
1916adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1917adad4744SNate Lawson     acpi_sysres_alloc(bus);
1918adad4744SNate Lawson 
1919ea233199SJohn Baldwin     /* Reserve resources already allocated to children. */
1920ea233199SJohn Baldwin     acpi_reserve_resources(bus);
1921ea233199SJohn Baldwin 
1922edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1923edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1924edc13633SNate Lawson     bus_generic_probe(bus);
1925edc13633SNate Lawson 
19265e66b8e2SJohn Baldwin     /* Probe/attach all children, created statically and from the namespace. */
19270430ba9eSAndriy Gapon     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n"));
192815e32d5dSMike Smith     bus_generic_attach(bus);
19290ae55423SMike Smith 
193088a79fc0SNate Lawson     /* Attach wake sysctls. */
19315c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
193288a79fc0SNate Lawson 
1933bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
19340ae55423SMike Smith     return_VOID;
193515e32d5dSMike Smith }
193615e32d5dSMike Smith 
1937b82ca9a9SNate Lawson /*
1938d227204dSJohn Baldwin  * Determine the probe order for a given device.
1939b82ca9a9SNate Lawson  */
1940d227204dSJohn Baldwin static void
1941b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
194291233413SNate Lawson {
1943463e0f91SJohn Baldwin 	ACPI_OBJECT_TYPE type;
194491233413SNate Lawson 
1945b82ca9a9SNate Lawson 	/*
1946404b0d10SJung-uk Kim 	 * 0. CPUs
1947404b0d10SJung-uk Kim 	 * 1. I/O port and memory system resource holders
1948404b0d10SJung-uk Kim 	 * 2. Clocks and timers (to handle early accesses)
1949b1f9b996SAndriy Gapon 	 * 3. Embedded controllers (to handle early accesses)
1950b1f9b996SAndriy Gapon 	 * 4. PCI Link Devices
1951b82ca9a9SNate Lawson 	 */
1952463e0f91SJohn Baldwin 	AcpiGetType(handle, &type);
1953aa835160SAndriy Gapon 	if (type == ACPI_TYPE_PROCESSOR)
1954404b0d10SJung-uk Kim 		*order = 0;
1955404b0d10SJung-uk Kim 	else if (acpi_MatchHid(handle, "PNP0C01") ||
1956404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0C02"))
195791233413SNate Lawson 		*order = 1;
1958404b0d10SJung-uk Kim 	else if (acpi_MatchHid(handle, "PNP0100") ||
1959404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0103") ||
1960404b0d10SJung-uk Kim 	    acpi_MatchHid(handle, "PNP0B00"))
196191233413SNate Lawson 		*order = 2;
1962aa835160SAndriy Gapon 	else if (acpi_MatchHid(handle, "PNP0C09"))
1963b460c6f8SJohn Baldwin 		*order = 3;
1964aa835160SAndriy Gapon 	else if (acpi_MatchHid(handle, "PNP0C0F"))
1965aa835160SAndriy Gapon 		*order = 4;
196691233413SNate Lawson }
196791233413SNate Lawson 
196815e32d5dSMike Smith /*
196915e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
197015e32d5dSMike Smith  * it.
197115e32d5dSMike Smith  */
197215e32d5dSMike Smith static ACPI_STATUS
197315e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
197415e32d5dSMike Smith {
1975*0e68afe5SAndrew Turner     ACPI_DEVICE_INFO *devinfo;
1976*0e68afe5SAndrew Turner     struct acpi_device	*ad;
19775a77b11bSJung-uk Kim     struct acpi_prw_data prw;
197815e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
1979858a52f4SMitsuru IWASAKI     ACPI_HANDLE h;
198033332dc2SNate Lawson     device_t bus, child;
1981495a4144SJung-uk Kim     char *handle_str;
1982e4cd9dcfSJohn Baldwin     int order;
198315e32d5dSMike Smith 
1984b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
19850ae55423SMike Smith 
1986495a4144SJung-uk Kim     if (acpi_disabled("children"))
1987495a4144SJung-uk Kim 	return_ACPI_STATUS (AE_OK);
1988495a4144SJung-uk Kim 
1989be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
19900ae55423SMike Smith     if (acpi_avoid(handle))
19910ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
19920ae55423SMike Smith 
199391233413SNate Lawson     bus = (device_t)context;
1994b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
1995495a4144SJung-uk Kim 	handle_str = acpi_name(handle);
199615e32d5dSMike Smith 	switch (type) {
199715e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
1998495a4144SJung-uk Kim 	    /*
1999495a4144SJung-uk Kim 	     * Since we scan from \, be sure to skip system scope objects.
2000495a4144SJung-uk Kim 	     * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around
2001affa1826SJung-uk Kim 	     * BIOS bugs.  For example, \_SB_ is to allow \_SB_._INI to be run
2002453130d9SPedro F. Giffuni 	     * during the initialization and \_TZ_ is to support Notify() on it.
2003495a4144SJung-uk Kim 	     */
2004495a4144SJung-uk Kim 	    if (strcmp(handle_str, "\\_SB_") == 0 ||
2005495a4144SJung-uk Kim 		strcmp(handle_str, "\\_TZ_") == 0)
2006495a4144SJung-uk Kim 		break;
20075a77b11bSJung-uk Kim 	    if (acpi_parse_prw(handle, &prw) == 0)
20085a77b11bSJung-uk Kim 		AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit);
2009183c8af3SJohn Baldwin 
2010183c8af3SJohn Baldwin 	    /*
2011183c8af3SJohn Baldwin 	     * Ignore devices that do not have a _HID or _CID.  They should
2012183c8af3SJohn Baldwin 	     * be discovered by other buses (e.g. the PCI bus driver).
2013183c8af3SJohn Baldwin 	     */
2014183c8af3SJohn Baldwin 	    if (!acpi_has_hid(handle))
2015183c8af3SJohn Baldwin 		break;
2016495a4144SJung-uk Kim 	    /* FALLTHROUGH */
201715e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
201815e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
201915e32d5dSMike Smith 	case ACPI_TYPE_POWER:
202033332dc2SNate Lawson 	    /*
2021463e0f91SJohn Baldwin 	     * Create a placeholder device for this node.  Sort the
2022463e0f91SJohn Baldwin 	     * placeholder so that the probe/attach passes will run
2023463e0f91SJohn Baldwin 	     * breadth-first.  Orders less than ACPI_DEV_BASE_ORDER
2024463e0f91SJohn Baldwin 	     * are reserved for special objects (i.e., system
2025b1f9b996SAndriy Gapon 	     * resources).
202615e32d5dSMike Smith 	     */
202733332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
2028404b0d10SJung-uk Kim 	    order = level * 10 + ACPI_DEV_BASE_ORDER;
2029da72d149SNate Lawson 	    acpi_probe_order(handle, &order);
2030e4cd9dcfSJohn Baldwin 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
2031bc0f2195SMike Smith 	    if (child == NULL)
2032bc0f2195SMike Smith 		break;
203359a890e6SNate Lawson 
203459a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
203515e32d5dSMike Smith 	    acpi_set_handle(child, handle);
203659a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
2037bc0f2195SMike Smith 
2038bc0f2195SMike Smith 	    /*
2039b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
2040b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
2041b519f9d6SMike Smith 	     * the handle, but we don't probe it).
2042893f750aSNate Lawson 	     *
2043893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
2044893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
2045893f750aSNate Lawson 	     * anyway since we may enable them later.
2046b519f9d6SMike Smith 	     */
2047858a52f4SMitsuru IWASAKI 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
2048858a52f4SMitsuru IWASAKI 		/* Never disable PCI link devices. */
2049858a52f4SMitsuru IWASAKI 		if (acpi_MatchHid(handle, "PNP0C0F"))
2050858a52f4SMitsuru IWASAKI 		    break;
2051858a52f4SMitsuru IWASAKI 		/*
2052858a52f4SMitsuru IWASAKI 		 * Docking stations should remain enabled since the system
2053858a52f4SMitsuru IWASAKI 		 * may be undocked at boot.
2054858a52f4SMitsuru IWASAKI 		 */
2055858a52f4SMitsuru IWASAKI 		if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
2056858a52f4SMitsuru IWASAKI 		    break;
2057858a52f4SMitsuru IWASAKI 
2058b519f9d6SMike Smith 		device_disable(child);
2059b519f9d6SMike Smith 		break;
2060b519f9d6SMike Smith 	    }
2061b519f9d6SMike Smith 
2062b519f9d6SMike Smith 	    /*
2063bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
2064bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
2065bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
2066bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
2067bc0f2195SMike Smith 	     * device not to have any resources.
2068bc0f2195SMike Smith 	     */
206972ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
2070*0e68afe5SAndrew Turner 
2071*0e68afe5SAndrew Turner 	    ad = device_get_ivars(child);
2072*0e68afe5SAndrew Turner 	    ad->ad_cls_class = 0xffffff;
2073*0e68afe5SAndrew Turner 	    if (ACPI_SUCCESS(AcpiGetObjectInfo(handle, &devinfo))) {
2074*0e68afe5SAndrew Turner 		if ((devinfo->Valid & ACPI_VALID_CLS) != 0 &&
2075*0e68afe5SAndrew Turner 		    devinfo->ClassCode.Length >= ACPI_PCICLS_STRING_SIZE) {
2076*0e68afe5SAndrew Turner 		    ad->ad_cls_class = strtoul(devinfo->ClassCode.String,
2077*0e68afe5SAndrew Turner 			NULL, 16);
2078*0e68afe5SAndrew Turner 		}
2079*0e68afe5SAndrew Turner 		AcpiOsFree(devinfo);
2080*0e68afe5SAndrew Turner 	    }
20810ae55423SMike Smith 	    break;
208215e32d5dSMike Smith 	}
208315e32d5dSMike Smith     }
2084be2b1797SNate Lawson 
20850ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
208615e32d5dSMike Smith }
208715e32d5dSMike Smith 
208859a890e6SNate Lawson /*
208959a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
209059a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
209159a890e6SNate Lawson  */
209259a890e6SNate Lawson void
209392488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data)
209459a890e6SNate Lawson {
209559a890e6SNate Lawson }
209659a890e6SNate Lawson 
209715e32d5dSMike Smith static void
209815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
209915e32d5dSMike Smith {
21002d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
210171804adcSJung-uk Kim     register_t intr;
2102d33f4987STakanori Watanabe     ACPI_STATUS status;
2103eeb3a05fSNate Lawson 
2104eeb3a05fSNate Lawson     /*
210580f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
210680f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
210780f0e4c2SNate Lawson      * an AP.
2108eeb3a05fSNate Lawson      */
2109eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
2110904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
2111904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
21122d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2113904bf0c2SNate Lawson 		AcpiFormatException(status));
2114904bf0c2SNate Lawson 	    return;
2115904bf0c2SNate Lawson 	}
21162d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Powering system off\n");
211771804adcSJung-uk Kim 	intr = intr_disable();
21181df130f1SJung-uk Kim 	status = AcpiEnterSleepState(ACPI_STATE_S5);
211971804adcSJung-uk Kim 	if (ACPI_FAILURE(status)) {
212071804adcSJung-uk Kim 	    intr_restore(intr);
21212d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - %s\n",
21222d0c82e8SJung-uk Kim 		AcpiFormatException(status));
212371804adcSJung-uk Kim 	} else {
212415e32d5dSMike Smith 	    DELAY(1000000);
212571804adcSJung-uk Kim 	    intr_restore(intr);
21262d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - timeout\n");
212715e32d5dSMike Smith 	}
2128ac731af5SJung-uk Kim     } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) {
2129d85e6785SNate Lawson 	/* Reboot using the reset register. */
2130ac731af5SJung-uk Kim 	status = AcpiReset();
2131ac731af5SJung-uk Kim 	if (ACPI_SUCCESS(status)) {
213287a500cdSNate Lawson 	    DELAY(1000000);
21332d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - timeout\n");
2134ac731af5SJung-uk Kim 	} else if (status != AE_NOT_EXIST)
2135ac731af5SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - %s\n",
2136ac731af5SJung-uk Kim 		AcpiFormatException(status));
2137d85e6785SNate Lawson     } else if (sc->acpi_do_disable && panicstr == NULL) {
2138d85e6785SNate Lawson 	/*
2139d85e6785SNate Lawson 	 * Only disable ACPI if the user requested.  On some systems, writing
2140d85e6785SNate Lawson 	 * the disable value to SMI_CMD hangs the system.
2141d85e6785SNate Lawson 	 */
21422d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Shutting down\n");
214380f0e4c2SNate Lawson 	AcpiTerminate();
214480f0e4c2SNate Lawson     }
214515e32d5dSMike Smith }
214615e32d5dSMike Smith 
214713d4f7baSMitsuru IWASAKI static void
214813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
214913d4f7baSMitsuru IWASAKI {
215013d4f7baSMitsuru IWASAKI     static int	first_time = 1;
215113d4f7baSMitsuru IWASAKI 
215213d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
21532be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
215459ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
215513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
215633febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
2157be2b1797SNate Lawson 	if (first_time)
21586c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
215913d4f7baSMitsuru IWASAKI     }
21602be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
216159ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
216213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
216333febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
2164be2b1797SNate Lawson 	if (first_time)
21656c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
216613d4f7baSMitsuru IWASAKI     }
216713d4f7baSMitsuru IWASAKI 
216813d4f7baSMitsuru IWASAKI     first_time = 0;
216913d4f7baSMitsuru IWASAKI }
217013d4f7baSMitsuru IWASAKI 
217115e32d5dSMike Smith /*
2172c5ba0be4SMike Smith  * Returns true if the device is actually present and should
2173c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
2174c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
2175c5ba0be4SMike Smith  */
2176c5ba0be4SMike Smith BOOLEAN
2177c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
2178c5ba0be4SMike Smith {
21791e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
2180c5ba0be4SMike Smith     ACPI_HANDLE		h;
218192488a57SJung-uk Kim     BOOLEAN		present;
2182c5ba0be4SMike Smith 
218392488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
218492488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2185c5ba0be4SMike Smith 	return (FALSE);
2186be2b1797SNate Lawson 
2187be2b1797SNate Lawson     /* If no _STA method, must be present */
218892488a57SJung-uk Kim     present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
218992488a57SJung-uk Kim 	ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
2190be2b1797SNate Lawson 
219192488a57SJung-uk Kim     AcpiOsFree(devinfo);
219292488a57SJung-uk Kim     return (present);
2193c5ba0be4SMike Smith }
2194c5ba0be4SMike Smith 
2195c5ba0be4SMike Smith /*
2196b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
2197b53f2771SMike Smith  */
2198b53f2771SMike Smith BOOLEAN
2199b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
2200b53f2771SMike Smith {
22011e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
2202b53f2771SMike Smith     ACPI_HANDLE		h;
220392488a57SJung-uk Kim     BOOLEAN		present;
2204b53f2771SMike Smith 
220592488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
220692488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2207b53f2771SMike Smith 	return (FALSE);
2208be2b1797SNate Lawson 
2209be2b1797SNate Lawson     /* If no _STA method, must be present */
221092488a57SJung-uk Kim     present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
221192488a57SJung-uk Kim 	ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
2212be2b1797SNate Lawson 
221392488a57SJung-uk Kim     AcpiOsFree(devinfo);
221492488a57SJung-uk Kim     return (present);
2215b53f2771SMike Smith }
2216b53f2771SMike Smith 
2217b53f2771SMike Smith /*
2218183c8af3SJohn Baldwin  * Returns true if a device has at least one valid device ID.
2219183c8af3SJohn Baldwin  */
2220183c8af3SJohn Baldwin static BOOLEAN
2221183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h)
2222183c8af3SJohn Baldwin {
2223183c8af3SJohn Baldwin     ACPI_DEVICE_INFO	*devinfo;
2224183c8af3SJohn Baldwin     BOOLEAN		ret;
2225183c8af3SJohn Baldwin 
2226183c8af3SJohn Baldwin     if (h == NULL ||
2227183c8af3SJohn Baldwin 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
2228183c8af3SJohn Baldwin 	return (FALSE);
2229183c8af3SJohn Baldwin 
2230183c8af3SJohn Baldwin     ret = FALSE;
2231183c8af3SJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
2232183c8af3SJohn Baldwin 	ret = TRUE;
2233183c8af3SJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
2234183c8af3SJohn Baldwin 	if (devinfo->CompatibleIdList.Count > 0)
2235183c8af3SJohn Baldwin 	    ret = TRUE;
2236183c8af3SJohn Baldwin 
2237183c8af3SJohn Baldwin     AcpiOsFree(devinfo);
2238183c8af3SJohn Baldwin     return (ret);
2239183c8af3SJohn Baldwin }
2240183c8af3SJohn Baldwin 
2241183c8af3SJohn Baldwin /*
224291233413SNate Lawson  * Match a HID string against a handle
224315e32d5dSMike Smith  */
22443c4c08dcSAlexander Motin BOOLEAN
2245ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
224615e32d5dSMike Smith {
22471e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
224892488a57SJung-uk Kim     BOOLEAN		ret;
224992488a57SJung-uk Kim     int			i;
225092488a57SJung-uk Kim 
225192488a57SJung-uk Kim     if (hid == NULL || h == NULL ||
225292488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
225392488a57SJung-uk Kim 	return (FALSE);
225415e32d5dSMike Smith 
22551e4925e8SNate Lawson     ret = FALSE;
2256c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
225792488a57SJung-uk Kim 	strcmp(hid, devinfo->HardwareId.String) == 0)
22581e4925e8SNate Lawson 	    ret = TRUE;
225992488a57SJung-uk Kim     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
226092488a57SJung-uk Kim 	for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
226192488a57SJung-uk Kim 	    if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
22621e4925e8SNate Lawson 		ret = TRUE;
22631e4925e8SNate Lawson 		break;
22641e4925e8SNate Lawson 	    }
22651e4925e8SNate Lawson 	}
2266be2b1797SNate Lawson 
226792488a57SJung-uk Kim     AcpiOsFree(devinfo);
22681e4925e8SNate Lawson     return (ret);
226915e32d5dSMike Smith }
227015e32d5dSMike Smith 
227115e32d5dSMike Smith /*
2272c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
2273c5ba0be4SMike Smith  * or one if its parents.
2274c5ba0be4SMike Smith  */
2275c5ba0be4SMike Smith ACPI_STATUS
2276c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
2277c5ba0be4SMike Smith {
2278c5ba0be4SMike Smith     ACPI_HANDLE		r;
2279c5ba0be4SMike Smith     ACPI_STATUS		status;
2280c5ba0be4SMike Smith 
2281be2b1797SNate Lawson     /* Walk back up the tree to the root */
2282c5ba0be4SMike Smith     for (;;) {
2283be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
2284be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
2285c5ba0be4SMike Smith 	    *result = r;
2286c5ba0be4SMike Smith 	    return (AE_OK);
2287c5ba0be4SMike Smith 	}
2288bee4aa4aSNate Lawson 	/* XXX Return error here? */
2289c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
2290c5ba0be4SMike Smith 	    return (AE_OK);
2291b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
2292c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
2293c5ba0be4SMike Smith 	parent = r;
2294c5ba0be4SMike Smith     }
2295c5ba0be4SMike Smith }
2296c5ba0be4SMike Smith 
2297c5ba0be4SMike Smith /*
2298c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
2299c5ba0be4SMike Smith  */
2300c5ba0be4SMike Smith ACPI_BUFFER *
2301c5ba0be4SMike Smith acpi_AllocBuffer(int size)
2302c5ba0be4SMike Smith {
2303c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
2304c5ba0be4SMike Smith 
2305c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2306c5ba0be4SMike Smith 	return (NULL);
2307c5ba0be4SMike Smith     buf->Length = size;
2308c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
2309c5ba0be4SMike Smith     return (buf);
2310c5ba0be4SMike Smith }
2311c5ba0be4SMike Smith 
2312c310653eSNate Lawson ACPI_STATUS
2313cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2314c310653eSNate Lawson {
2315c310653eSNate Lawson     ACPI_OBJECT arg1;
2316c310653eSNate Lawson     ACPI_OBJECT_LIST args;
2317c310653eSNate Lawson 
2318c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
2319c310653eSNate Lawson     arg1.Integer.Value = number;
2320c310653eSNate Lawson     args.Count = 1;
2321c310653eSNate Lawson     args.Pointer = &arg1;
2322c310653eSNate Lawson 
2323c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
2324c310653eSNate Lawson }
2325c310653eSNate Lawson 
2326c5ba0be4SMike Smith /*
2327c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
2328c5ba0be4SMike Smith  */
2329c5ba0be4SMike Smith ACPI_STATUS
2330cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2331c5ba0be4SMike Smith {
2332be2b1797SNate Lawson     ACPI_STATUS	status;
2333c5ba0be4SMike Smith     ACPI_BUFFER	buf;
2334c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
2335c5ba0be4SMike Smith 
2336c5ba0be4SMike Smith     if (handle == NULL)
2337c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
23380c7da7acSJohn Baldwin 
23390c7da7acSJohn Baldwin     /*
23400c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
23410c7da7acSJohn Baldwin      * a method that will return an Integer.
23420c7da7acSJohn Baldwin      */
2343c5ba0be4SMike Smith     buf.Pointer = &param;
2344c5ba0be4SMike Smith     buf.Length = sizeof(param);
2345be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
2346be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
2347be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
2348c5ba0be4SMike Smith 	    *number = param.Integer.Value;
2349be2b1797SNate Lawson 	else
2350be2b1797SNate Lawson 	    status = AE_TYPE;
2351c5ba0be4SMike Smith     }
23520c7da7acSJohn Baldwin 
23530c7da7acSJohn Baldwin     /*
23540c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
23550c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
23560c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
23570c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
23580c7da7acSJohn Baldwin      *
23590c7da7acSJohn Baldwin      * This is a hack.
23600c7da7acSJohn Baldwin      */
2361be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
2362b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2363be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
23640c7da7acSJohn Baldwin 	} else {
2365be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
2366be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
2367be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
23680c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
23690c7da7acSJohn Baldwin 	}
2370f97739daSNate Lawson     }
2371be2b1797SNate Lawson     return (status);
2372c5ba0be4SMike Smith }
2373c5ba0be4SMike Smith 
2374c573e654SMitsuru IWASAKI ACPI_STATUS
2375cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2376c573e654SMitsuru IWASAKI {
2377c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
2378dba55fa2SNate Lawson     UINT8	*val;
2379c573e654SMitsuru IWASAKI     int		i;
2380c573e654SMitsuru IWASAKI 
2381c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
2382c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
2383c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
2384c573e654SMitsuru IWASAKI 	return (AE_OK);
2385c573e654SMitsuru IWASAKI     }
2386c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
2387c573e654SMitsuru IWASAKI 	return (AE_TYPE);
2388c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
2389c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
2390be2b1797SNate Lawson 
2391c573e654SMitsuru IWASAKI     *number = 0;
2392dba55fa2SNate Lawson     val = p->Buffer.Pointer;
2393c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
2394dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
2395c573e654SMitsuru IWASAKI     return (AE_OK);
2396c573e654SMitsuru IWASAKI }
2397c573e654SMitsuru IWASAKI 
2398c5ba0be4SMike Smith /*
2399c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
2400c5ba0be4SMike Smith  * function for each element.
2401c5ba0be4SMike Smith  *
2402c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
2403c5ba0be4SMike Smith  */
2404c5ba0be4SMike Smith ACPI_STATUS
2405be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2406be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2407c5ba0be4SMike Smith {
2408c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
2409c5ba0be4SMike Smith     int		i;
2410c5ba0be4SMike Smith 
2411be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2412c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
2413c5ba0be4SMike Smith 
2414be2b1797SNate Lawson     /* Iterate over components */
2415be2b1797SNate Lawson     i = 0;
2416be2b1797SNate Lawson     comp = pkg->Package.Elements;
2417be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
2418c5ba0be4SMike Smith 	func(comp, arg);
2419c5ba0be4SMike Smith 
2420c5ba0be4SMike Smith     return (AE_OK);
2421c5ba0be4SMike Smith }
2422c5ba0be4SMike Smith 
24236f69255bSMike Smith /*
24246f69255bSMike Smith  * Find the (index)th resource object in a set.
24256f69255bSMike Smith  */
24266f69255bSMike Smith ACPI_STATUS
24276d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
24286f69255bSMike Smith {
24296d63101aSMike Smith     ACPI_RESOURCE	*rp;
24306f69255bSMike Smith     int			i;
24316f69255bSMike Smith 
24326d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
24336f69255bSMike Smith     i = index;
24346d63101aSMike Smith     while (i-- > 0) {
2435be2b1797SNate Lawson 	/* Range check */
24366d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
24376f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
2438be2b1797SNate Lawson 
2439be2b1797SNate Lawson 	/* Check for terminator */
2440e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
24416d63101aSMike Smith 	    return (AE_NOT_FOUND);
2442abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
24436f69255bSMike Smith     }
24446f69255bSMike Smith     if (resp != NULL)
24456d63101aSMike Smith 	*resp = rp;
2446be2b1797SNate Lawson 
24476d63101aSMike Smith     return (AE_OK);
24486d63101aSMike Smith }
24496d63101aSMike Smith 
24506d63101aSMike Smith /*
24516d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
24526d63101aSMike Smith  *
24536d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
24546d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
24556d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
24566d63101aSMike Smith  * resources.
24576d63101aSMike Smith  */
24586d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
24596d63101aSMike Smith 
24606d63101aSMike Smith ACPI_STATUS
24616d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
24626d63101aSMike Smith {
24636d63101aSMike Smith     ACPI_RESOURCE	*rp;
24646d63101aSMike Smith     void		*newp;
24656d63101aSMike Smith 
2466be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
24676d63101aSMike Smith     if (buf->Pointer == NULL) {
24686d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
24696d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
24706d63101aSMike Smith 	    return (AE_NO_MEMORY);
24716d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2472e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
247360d306f0SJohn Baldwin 	rp->Length = ACPI_RS_SIZE_MIN;
24746d63101aSMike Smith     }
24756d63101aSMike Smith     if (res == NULL)
24766d63101aSMike Smith 	return (AE_OK);
24776d63101aSMike Smith 
24786d63101aSMike Smith     /*
24796d63101aSMike Smith      * Scan the current buffer looking for the terminator.
24806d63101aSMike Smith      * This will either find the terminator or hit the end
24816d63101aSMike Smith      * of the buffer and return an error.
24826d63101aSMike Smith      */
24836d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
24846d63101aSMike Smith     for (;;) {
2485be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
24866d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
24876d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2488e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
24896d63101aSMike Smith 	    break;
2490abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
24916d63101aSMike Smith     }
24926d63101aSMike Smith 
24936d63101aSMike Smith     /*
24946d63101aSMike Smith      * Check the size of the buffer and expand if required.
24956d63101aSMike Smith      *
24966d63101aSMike Smith      * Required size is:
24976d63101aSMike Smith      *	size of existing resources before terminator +
24986d63101aSMike Smith      *	size of new resource and header +
24996d63101aSMike Smith      * 	size of terminator.
25006d63101aSMike Smith      *
25016d63101aSMike Smith      * Note that this loop should really only run once, unless
25026d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
25036d63101aSMike Smith      */
25046d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2505e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2506e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
25076d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
25086d63101aSMike Smith 	    return (AE_NO_MEMORY);
25096d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2510a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2511a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
25126d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
25136d63101aSMike Smith 	buf->Pointer = newp;
25146d63101aSMike Smith 	buf->Length += buf->Length;
25156d63101aSMike Smith     }
25166d63101aSMike Smith 
2517be2b1797SNate Lawson     /* Insert the new resource. */
2518e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
25196d63101aSMike Smith 
2520be2b1797SNate Lawson     /* And add the terminator. */
2521abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2522e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
252360d306f0SJohn Baldwin     rp->Length = ACPI_RS_SIZE_MIN;
25246d63101aSMike Smith 
25256f69255bSMike Smith     return (AE_OK);
25266f69255bSMike Smith }
2527c5ba0be4SMike Smith 
25285f3dd91aSJohn Baldwin ACPI_STATUS
25295f3dd91aSJohn Baldwin acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count,
25304c26ac69SJohn Baldwin     uint32_t *caps_in, uint32_t *caps_out, bool query)
25315f3dd91aSJohn Baldwin {
25324c26ac69SJohn Baldwin 	ACPI_OBJECT arg[4], *ret;
25335f3dd91aSJohn Baldwin 	ACPI_OBJECT_LIST arglist;
25344c26ac69SJohn Baldwin 	ACPI_BUFFER buf;
25354c26ac69SJohn Baldwin 	ACPI_STATUS status;
25365f3dd91aSJohn Baldwin 
25375f3dd91aSJohn Baldwin 	arglist.Pointer = arg;
25385f3dd91aSJohn Baldwin 	arglist.Count = 4;
25395f3dd91aSJohn Baldwin 	arg[0].Type = ACPI_TYPE_BUFFER;
25405f3dd91aSJohn Baldwin 	arg[0].Buffer.Length = ACPI_UUID_LENGTH;
25415f3dd91aSJohn Baldwin 	arg[0].Buffer.Pointer = uuid;
25425f3dd91aSJohn Baldwin 	arg[1].Type = ACPI_TYPE_INTEGER;
25435f3dd91aSJohn Baldwin 	arg[1].Integer.Value = revision;
25445f3dd91aSJohn Baldwin 	arg[2].Type = ACPI_TYPE_INTEGER;
25455f3dd91aSJohn Baldwin 	arg[2].Integer.Value = count;
25465f3dd91aSJohn Baldwin 	arg[3].Type = ACPI_TYPE_BUFFER;
25474c26ac69SJohn Baldwin 	arg[3].Buffer.Length = count * sizeof(*caps_in);
25484c26ac69SJohn Baldwin 	arg[3].Buffer.Pointer = (uint8_t *)caps_in;
25494c26ac69SJohn Baldwin 	caps_in[0] = query ? 1 : 0;
25504c26ac69SJohn Baldwin 	buf.Pointer = NULL;
25514c26ac69SJohn Baldwin 	buf.Length = ACPI_ALLOCATE_BUFFER;
25524c26ac69SJohn Baldwin 	status = AcpiEvaluateObjectTyped(handle, "_OSC", &arglist, &buf,
25534c26ac69SJohn Baldwin 	    ACPI_TYPE_BUFFER);
25544c26ac69SJohn Baldwin 	if (ACPI_FAILURE(status))
25554c26ac69SJohn Baldwin 		return (status);
25564c26ac69SJohn Baldwin 	if (caps_out != NULL) {
25574c26ac69SJohn Baldwin 		ret = buf.Pointer;
25584c26ac69SJohn Baldwin 		if (ret->Buffer.Length != count * sizeof(*caps_out)) {
25594c26ac69SJohn Baldwin 			AcpiOsFree(buf.Pointer);
25604c26ac69SJohn Baldwin 			return (AE_BUFFER_OVERFLOW);
25614c26ac69SJohn Baldwin 		}
25624c26ac69SJohn Baldwin 		bcopy(ret->Buffer.Pointer, caps_out, ret->Buffer.Length);
25634c26ac69SJohn Baldwin 	}
25644c26ac69SJohn Baldwin 	AcpiOsFree(buf.Pointer);
25654c26ac69SJohn Baldwin 	return (status);
25665f3dd91aSJohn Baldwin }
25675f3dd91aSJohn Baldwin 
2568da14ac9fSJohn Baldwin /*
2569da14ac9fSJohn Baldwin  * Set interrupt model.
2570da14ac9fSJohn Baldwin  */
2571da14ac9fSJohn Baldwin ACPI_STATUS
2572da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2573da14ac9fSJohn Baldwin {
2574da14ac9fSJohn Baldwin 
2575c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2576da14ac9fSJohn Baldwin }
2577da14ac9fSJohn Baldwin 
257800a30448SNate Lawson /*
2579d95e7f5aSJohn Baldwin  * Walk subtables of a table and call a callback routine for each
2580d95e7f5aSJohn Baldwin  * subtable.  The caller should provide the first subtable and a
2581d95e7f5aSJohn Baldwin  * pointer to the end of the table.  This can be used to walk tables
2582d95e7f5aSJohn Baldwin  * such as MADT and SRAT that use subtable entries.
2583d95e7f5aSJohn Baldwin  */
2584d95e7f5aSJohn Baldwin void
2585d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2586d95e7f5aSJohn Baldwin     void *arg)
2587d95e7f5aSJohn Baldwin {
2588d95e7f5aSJohn Baldwin     ACPI_SUBTABLE_HEADER *entry;
2589d95e7f5aSJohn Baldwin 
2590d95e7f5aSJohn Baldwin     for (entry = first; (void *)entry < end; ) {
2591d95e7f5aSJohn Baldwin 	/* Avoid an infinite loop if we hit a bogus entry. */
2592d95e7f5aSJohn Baldwin 	if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2593d95e7f5aSJohn Baldwin 	    return;
2594d95e7f5aSJohn Baldwin 
2595d95e7f5aSJohn Baldwin 	handler(entry, arg);
2596d95e7f5aSJohn Baldwin 	entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2597d95e7f5aSJohn Baldwin     }
2598d95e7f5aSJohn Baldwin }
2599d95e7f5aSJohn Baldwin 
2600d95e7f5aSJohn Baldwin /*
260100a30448SNate Lawson  * DEPRECATED.  This interface has serious deficiencies and will be
260200a30448SNate Lawson  * removed.
260300a30448SNate Lawson  *
260400a30448SNate Lawson  * Immediately enter the sleep state.  In the old model, acpiconf(8) ran
260500a30448SNate Lawson  * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
260600a30448SNate Lawson  */
260700a30448SNate Lawson ACPI_STATUS
260800a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state)
260900a30448SNate Lawson {
261000a30448SNate Lawson     static int once;
261100a30448SNate Lawson 
261200a30448SNate Lawson     if (!once) {
26132d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
261400a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n");
261500a30448SNate Lawson 	once = 1;
261600a30448SNate Lawson     }
261700a30448SNate Lawson     return (acpi_EnterSleepState(sc, state));
261800a30448SNate Lawson }
261900a30448SNate Lawson 
2620c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
262100a30448SNate Lawson static void
2622ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context)
2623ffe3f1f8SMitsuru IWASAKI {
2624ffe3f1f8SMitsuru IWASAKI     struct acpi_softc *sc = (struct acpi_softc *)context;
2625ffe3f1f8SMitsuru IWASAKI 
2626ffe3f1f8SMitsuru IWASAKI     if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
2627ffe3f1f8SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
2628ffe3f1f8SMitsuru IWASAKI 	    sc->acpi_next_sstate);
2629ffe3f1f8SMitsuru IWASAKI }
2630ffe3f1f8SMitsuru IWASAKI 
2631ffe3f1f8SMitsuru IWASAKI static void
263200a30448SNate Lawson acpi_sleep_force(void *arg)
263300a30448SNate Lawson {
26342d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
263500a30448SNate Lawson 
26362d0c82e8SJung-uk Kim     device_printf(sc->acpi_dev,
26372d0c82e8SJung-uk Kim 	"suspend request timed out, forcing sleep now\n");
2638ffe3f1f8SMitsuru IWASAKI     /*
2639fadf3fb9SJohn Baldwin      * XXX Suspending from callout causes freezes in DEVICE_SUSPEND().
2640ffe3f1f8SMitsuru IWASAKI      * Suspend from acpi_task thread instead.
2641ffe3f1f8SMitsuru IWASAKI      */
2642ffe3f1f8SMitsuru IWASAKI     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
2643ffe3f1f8SMitsuru IWASAKI 	acpi_sleep_force_task, sc)))
2644ffe3f1f8SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n");
264500a30448SNate Lawson }
2646c66d2b38SJung-uk Kim #endif
264700a30448SNate Lawson 
264800a30448SNate Lawson /*
264900a30448SNate Lawson  * Request that the system enter the given suspend state.  All /dev/apm
265000a30448SNate Lawson  * devices and devd(8) will be notified.  Userland then has a chance to
265100a30448SNate Lawson  * save state and acknowledge the request.  The system sleeps once all
265200a30448SNate Lawson  * acks are in.
265300a30448SNate Lawson  */
265400a30448SNate Lawson int
265500a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state)
265600a30448SNate Lawson {
265771f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
265800a30448SNate Lawson     struct apm_clone_data *clone;
2659337744d9SJung-uk Kim     ACPI_STATUS status;
266000a30448SNate Lawson 
2661a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
266200a30448SNate Lawson 	return (EINVAL);
2663a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state])
2664a0e73a12SJung-uk Kim 	return (EOPNOTSUPP);
266500a30448SNate Lawson 
26662eb0015aSColin Percival     /*
26672eb0015aSColin Percival      * If a reboot/shutdown/suspend request is already in progress or
26682eb0015aSColin Percival      * suspend is blocked due to an upcoming shutdown, just return.
26692eb0015aSColin Percival      */
26702eb0015aSColin Percival     if (rebooting || sc->acpi_next_sstate != 0 || suspend_blocked) {
2671b12bc99fSMitsuru IWASAKI 	return (0);
2672b12bc99fSMitsuru IWASAKI     }
2673b12bc99fSMitsuru IWASAKI 
2674f22377a8SMitsuru IWASAKI     /* Wait until sleep is enabled. */
2675f22377a8SMitsuru IWASAKI     while (sc->acpi_sleep_disabled) {
2676f22377a8SMitsuru IWASAKI 	AcpiOsSleep(1000);
2677f22377a8SMitsuru IWASAKI     }
2678f22377a8SMitsuru IWASAKI 
2679337744d9SJung-uk Kim     ACPI_LOCK(acpi);
268000a30448SNate Lawson 
2681f22377a8SMitsuru IWASAKI     sc->acpi_next_sstate = state;
268200a30448SNate Lawson 
2683337744d9SJung-uk Kim     /* S5 (soft-off) should be entered directly with no waiting. */
2684337744d9SJung-uk Kim     if (state == ACPI_STATE_S5) {
2685337744d9SJung-uk Kim     	ACPI_UNLOCK(acpi);
2686337744d9SJung-uk Kim 	status = acpi_EnterSleepState(sc, state);
2687337744d9SJung-uk Kim 	return (ACPI_SUCCESS(status) ? 0 : ENXIO);
2688337744d9SJung-uk Kim     }
2689337744d9SJung-uk Kim 
269000a30448SNate Lawson     /* Record the pending state and notify all apm devices. */
269100a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
269200a30448SNate Lawson 	clone->notify_status = APM_EV_NONE;
269300a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_DEVD) == 0) {
269400a30448SNate Lawson 	    selwakeuppri(&clone->sel_read, PZERO);
2695374d9c4dSJohn Baldwin 	    KNOTE_LOCKED(&clone->sel_read.si_note, 0);
269600a30448SNate Lawson 	}
269700a30448SNate Lawson     }
269800a30448SNate Lawson 
26990c26519eSMitsuru IWASAKI     /* If devd(8) is not running, immediately enter the sleep state. */
2700d42f0ad8SJung-uk Kim     if (!devctl_process_running()) {
27010c26519eSMitsuru IWASAKI 	ACPI_UNLOCK(acpi);
2702337744d9SJung-uk Kim 	status = acpi_EnterSleepState(sc, state);
2703337744d9SJung-uk Kim 	return (ACPI_SUCCESS(status) ? 0 : ENXIO);
27040c26519eSMitsuru IWASAKI     }
27050c26519eSMitsuru IWASAKI 
270600a30448SNate Lawson     /*
270700a30448SNate Lawson      * Set a timeout to fire if userland doesn't ack the suspend request
270800a30448SNate Lawson      * in time.  This way we still eventually go to sleep if we were
270900a30448SNate Lawson      * overheating or running low on battery, even if userland is hung.
271000a30448SNate Lawson      * We cancel this timeout once all userland acks are in or the
271100a30448SNate Lawson      * suspend request is aborted.
271200a30448SNate Lawson      */
271300a30448SNate Lawson     callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
271400a30448SNate Lawson     ACPI_UNLOCK(acpi);
2715d42f0ad8SJung-uk Kim 
2716d42f0ad8SJung-uk Kim     /* Now notify devd(8) also. */
2717d42f0ad8SJung-uk Kim     acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2718d42f0ad8SJung-uk Kim 
271900a30448SNate Lawson     return (0);
2720c66d2b38SJung-uk Kim #else
2721c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2722c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2723c66d2b38SJung-uk Kim #endif
272400a30448SNate Lawson }
272500a30448SNate Lawson 
272600a30448SNate Lawson /*
272700a30448SNate Lawson  * Acknowledge (or reject) a pending sleep state.  The caller has
272800a30448SNate Lawson  * prepared for suspend and is now ready for it to proceed.  If the
272900a30448SNate Lawson  * error argument is non-zero, it indicates suspend should be cancelled
273000a30448SNate Lawson  * and gives an errno value describing why.  Once all votes are in,
273100a30448SNate Lawson  * we suspend the system.
273200a30448SNate Lawson  */
273300a30448SNate Lawson int
273400a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error)
273500a30448SNate Lawson {
2736c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
273700a30448SNate Lawson     struct acpi_softc *sc;
273800a30448SNate Lawson     int ret, sleeping;
273900a30448SNate Lawson 
274000a30448SNate Lawson     /* If no pending sleep state, return an error. */
274100a30448SNate Lawson     ACPI_LOCK(acpi);
274200a30448SNate Lawson     sc = clone->acpi_sc;
274300a30448SNate Lawson     if (sc->acpi_next_sstate == 0) {
274400a30448SNate Lawson     	ACPI_UNLOCK(acpi);
274500a30448SNate Lawson 	return (ENXIO);
274600a30448SNate Lawson     }
274700a30448SNate Lawson 
274800a30448SNate Lawson     /* Caller wants to abort suspend process. */
274900a30448SNate Lawson     if (error) {
275000a30448SNate Lawson 	sc->acpi_next_sstate = 0;
275100a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
27522d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
27532d0c82e8SJung-uk Kim 	    "listener on %s cancelled the pending suspend\n",
275400a30448SNate Lawson 	    devtoname(clone->cdev));
275500a30448SNate Lawson     	ACPI_UNLOCK(acpi);
275600a30448SNate Lawson 	return (0);
275700a30448SNate Lawson     }
275800a30448SNate Lawson 
275900a30448SNate Lawson     /*
276000a30448SNate Lawson      * Mark this device as acking the suspend request.  Then, walk through
276100a30448SNate Lawson      * all devices, seeing if they agree yet.  We only count devices that
276200a30448SNate Lawson      * are writable since read-only devices couldn't ack the request.
276300a30448SNate Lawson      */
276400a30448SNate Lawson     sleeping = TRUE;
2765c66d2b38SJung-uk Kim     clone->notify_status = APM_EV_ACKED;
276600a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
276700a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
276800a30448SNate Lawson 	    clone->notify_status != APM_EV_ACKED) {
276900a30448SNate Lawson 	    sleeping = FALSE;
277000a30448SNate Lawson 	    break;
277100a30448SNate Lawson 	}
277200a30448SNate Lawson     }
277300a30448SNate Lawson 
277400a30448SNate Lawson     /* If all devices have voted "yes", we will suspend now. */
277500a30448SNate Lawson     if (sleeping)
277600a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
277700a30448SNate Lawson     ACPI_UNLOCK(acpi);
277800a30448SNate Lawson     ret = 0;
277900a30448SNate Lawson     if (sleeping) {
278000a30448SNate Lawson 	if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
278100a30448SNate Lawson 		ret = ENODEV;
278200a30448SNate Lawson     }
278300a30448SNate Lawson     return (ret);
2784c66d2b38SJung-uk Kim #else
2785c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2786c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2787c66d2b38SJung-uk Kim #endif
278800a30448SNate Lawson }
278900a30448SNate Lawson 
2790ece50487SMitsuru IWASAKI static void
2791ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2792ece50487SMitsuru IWASAKI {
2793d42f0ad8SJung-uk Kim     struct acpi_softc	*sc = (struct acpi_softc *)arg;
2794bee4aa4aSNate Lawson 
2795fadf3fb9SJohn Baldwin     ACPI_LOCK_ASSERT(acpi);
2796fadf3fb9SJohn Baldwin 
2797a0e73a12SJung-uk Kim     /* Reschedule if the system is not fully up and running. */
2798a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning) {
2799fadf3fb9SJohn Baldwin 	callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
2800a0e73a12SJung-uk Kim 	return;
2801a0e73a12SJung-uk Kim     }
2802a0e73a12SJung-uk Kim 
2803a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = FALSE;
2804d42f0ad8SJung-uk Kim }
2805d42f0ad8SJung-uk Kim 
2806d42f0ad8SJung-uk Kim static ACPI_STATUS
2807d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc)
2808d42f0ad8SJung-uk Kim {
2809d42f0ad8SJung-uk Kim     ACPI_STATUS		status;
2810d42f0ad8SJung-uk Kim 
2811a0e73a12SJung-uk Kim     /* Fail if the system is not fully up and running. */
2812a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning)
2813a0e73a12SJung-uk Kim 	return (AE_ERROR);
2814a0e73a12SJung-uk Kim 
2815d42f0ad8SJung-uk Kim     ACPI_LOCK(acpi);
2816d42f0ad8SJung-uk Kim     status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
2817a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
2818d42f0ad8SJung-uk Kim     ACPI_UNLOCK(acpi);
2819d42f0ad8SJung-uk Kim 
2820d42f0ad8SJung-uk Kim     return (status);
2821ece50487SMitsuru IWASAKI }
2822c30382dfSMitsuru IWASAKI 
282315e2f34fSNate Lawson enum acpi_sleep_state {
282415e2f34fSNate Lawson     ACPI_SS_NONE,
282515e2f34fSNate Lawson     ACPI_SS_GPE_SET,
282615e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
282715e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
282815e2f34fSNate Lawson     ACPI_SS_SLEPT,
282915e2f34fSNate Lawson };
283015e2f34fSNate Lawson 
283115e32d5dSMike Smith /*
283200a30448SNate Lawson  * Enter the desired system sleep state.
283315e32d5dSMike Smith  *
2834be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
283515e32d5dSMike Smith  */
283600a30448SNate Lawson static ACPI_STATUS
283700a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state)
283815e32d5dSMike Smith {
283971804adcSJung-uk Kim     register_t intr;
284015e2f34fSNate Lawson     ACPI_STATUS status;
2841b913a7d5SAndriy Gapon     ACPI_EVENT_STATUS power_button_status;
284215e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
2843f0a101b7SMitsuru IWASAKI     int sleep_result;
284415e32d5dSMike Smith 
2845b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
28460ae55423SMike Smith 
2847a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
284833c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_BAD_PARAMETER);
2849a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state]) {
2850a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
2851a0e73a12SJung-uk Kim 	    state);
2852a0e73a12SJung-uk Kim 	return (AE_SUPPORT);
2853a0e73a12SJung-uk Kim     }
2854a0e73a12SJung-uk Kim 
2855a0e73a12SJung-uk Kim     /* Re-entry once we're suspending is not allowed. */
2856a0e73a12SJung-uk Kim     status = acpi_sleep_disable(sc);
2857a0e73a12SJung-uk Kim     if (ACPI_FAILURE(status)) {
2858a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev,
2859a0e73a12SJung-uk Kim 	    "suspend request ignored (not ready yet)\n");
2860a0e73a12SJung-uk Kim 	return (status);
2861a0e73a12SJung-uk Kim     }
286233c70d41SAndriy Gapon 
286333c70d41SAndriy Gapon     if (state == ACPI_STATE_S5) {
286433c70d41SAndriy Gapon 	/*
286533c70d41SAndriy Gapon 	 * Shut down cleanly and power off.  This will call us back through the
286633c70d41SAndriy Gapon 	 * shutdown handlers.
286733c70d41SAndriy Gapon 	 */
286833c70d41SAndriy Gapon 	shutdown_nice(RB_POWEROFF);
286933c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_OK);
287033c70d41SAndriy Gapon     }
287133c70d41SAndriy Gapon 
287285f95fffSAndriy Gapon     EVENTHANDLER_INVOKE(power_suspend_early);
287385f95fffSAndriy Gapon     stop_all_proc();
28744a8fa6feSJung-uk Kim     EVENTHANDLER_INVOKE(power_suspend);
28754a8fa6feSJung-uk Kim 
2876fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
2877fdce57a0SJohn Baldwin     MPASS(mp_ncpus == 1 || smp_started);
2878fdce57a0SJohn Baldwin     thread_lock(curthread);
2879fdce57a0SJohn Baldwin     sched_bind(curthread, 0);
2880fdce57a0SJohn Baldwin     thread_unlock(curthread);
2881fdce57a0SJohn Baldwin #else
288236a483bbSJung-uk Kim     if (smp_started) {
2883c66d2b38SJung-uk Kim 	thread_lock(curthread);
2884c66d2b38SJung-uk Kim 	sched_bind(curthread, 0);
2885c66d2b38SJung-uk Kim 	thread_unlock(curthread);
288636a483bbSJung-uk Kim     }
2887fdce57a0SJohn Baldwin #endif
2888c66d2b38SJung-uk Kim 
2889a56fe095SJohn Baldwin     /*
2890a56fe095SJohn Baldwin      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
2891a56fe095SJohn Baldwin      * drivers need this.
2892a56fe095SJohn Baldwin      */
2893a56fe095SJohn Baldwin     mtx_lock(&Giant);
2894c66d2b38SJung-uk Kim 
289515e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
289656d8cb57SMitsuru IWASAKI 
2897a1fccb47SMitsuru IWASAKI     sc->acpi_sstate = state;
2898a1fccb47SMitsuru IWASAKI 
2899d4b9ff91SNate Lawson     /* Enable any GPEs as appropriate and requested by the user. */
2900d4b9ff91SNate Lawson     acpi_wake_prep_walk(state);
290115e2f34fSNate Lawson     slp_state = ACPI_SS_GPE_SET;
2902e8b4d56eSNate Lawson 
290315e32d5dSMike Smith     /*
2904c7f88fc3SNate Lawson      * Inform all devices that we are going to sleep.  If at least one
2905c7f88fc3SNate Lawson      * device fails, DEVICE_SUSPEND() automatically resumes the tree.
290615e32d5dSMike Smith      *
2907c7f88fc3SNate Lawson      * XXX Note that a better two-pass approach with a 'veto' pass
2908c7f88fc3SNate Lawson      * followed by a "real thing" pass would be better, but the current
2909c7f88fc3SNate Lawson      * bus interface does not provide for this.
291015e32d5dSMike Smith      */
291115e2f34fSNate Lawson     if (DEVICE_SUSPEND(root_bus) != 0) {
291215e2f34fSNate Lawson 	device_printf(sc->acpi_dev, "device_suspend failed\n");
291333c70d41SAndriy Gapon 	goto backout;
291415e2f34fSNate Lawson     }
291515e2f34fSNate Lawson     slp_state = ACPI_SS_DEV_SUSPEND;
2916b9c780d6SMitsuru IWASAKI 
291793bfd059SNate Lawson     /* If testing device suspend only, back out of everything here. */
291893bfd059SNate Lawson     if (acpi_susp_bounce)
291933c70d41SAndriy Gapon 	goto backout;
292093bfd059SNate Lawson 
2921be2b1797SNate Lawson     status = AcpiEnterSleepStatePrep(state);
2922be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
2923b9c780d6SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2924b9c780d6SMitsuru IWASAKI 		      AcpiFormatException(status));
292533c70d41SAndriy Gapon 	goto backout;
2926b9c780d6SMitsuru IWASAKI     }
292715e2f34fSNate Lawson     slp_state = ACPI_SS_SLP_PREP;
2928b9c780d6SMitsuru IWASAKI 
2929be2b1797SNate Lawson     if (sc->acpi_sleep_delay > 0)
2930ff01efb5SMitsuru IWASAKI 	DELAY(sc->acpi_sleep_delay * 1000000);
2931ff01efb5SMitsuru IWASAKI 
2932f0a101b7SMitsuru IWASAKI     intr = intr_disable();
29334d46ef51SJung-uk Kim     if (state != ACPI_STATE_S1) {
2934f0a101b7SMitsuru IWASAKI 	sleep_result = acpi_sleep_machdep(sc, state);
2935f0a101b7SMitsuru IWASAKI 	acpi_wakeup_machdep(sc, state, sleep_result, 0);
2936b1a5c017SAndriy Gapon 
2937b1a5c017SAndriy Gapon 	/*
2938b1a5c017SAndriy Gapon 	 * XXX According to ACPI specification SCI_EN bit should be restored
2939b1a5c017SAndriy Gapon 	 * by ACPI platform (BIOS, firmware) to its pre-sleep state.
2940b1a5c017SAndriy Gapon 	 * Unfortunately some BIOSes fail to do that and that leads to
2941b1a5c017SAndriy Gapon 	 * unexpected and serious consequences during wake up like a system
2942b1a5c017SAndriy Gapon 	 * getting stuck in SMI handlers.
2943b1a5c017SAndriy Gapon 	 * This hack is picked up from Linux, which claims that it follows
2944b1a5c017SAndriy Gapon 	 * Windows behavior.
2945b1a5c017SAndriy Gapon 	 */
2946b1a5c017SAndriy Gapon 	if (sleep_result == 1 && state != ACPI_STATE_S4)
2947b1a5c017SAndriy Gapon 	    AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT);
2948b1a5c017SAndriy Gapon 
29491df130f1SJung-uk Kim 	AcpiLeaveSleepStatePrep(state);
2950b913a7d5SAndriy Gapon 
2951b913a7d5SAndriy Gapon 	if (sleep_result == 1 && state == ACPI_STATE_S3) {
2952b913a7d5SAndriy Gapon 	    /*
2953b913a7d5SAndriy Gapon 	     * Prevent mis-interpretation of the wakeup by power button
2954b913a7d5SAndriy Gapon 	     * as a request for power off.
2955b913a7d5SAndriy Gapon 	     * Ideally we should post an appropriate wakeup event,
2956b913a7d5SAndriy Gapon 	     * perhaps using acpi_event_power_button_wake or alike.
2957b913a7d5SAndriy Gapon 	     *
2958b913a7d5SAndriy Gapon 	     * Clearing of power button status after wakeup is mandated
2959b913a7d5SAndriy Gapon 	     * by ACPI specification in section "Fixed Power Button".
2960b913a7d5SAndriy Gapon 	     *
2961b913a7d5SAndriy Gapon 	     * XXX As of ACPICA 20121114 AcpiGetEventStatus provides
2962b913a7d5SAndriy Gapon 	     * status as 0/1 corressponding to inactive/active despite
2963b913a7d5SAndriy Gapon 	     * its type being ACPI_EVENT_STATUS.  In other words,
2964b913a7d5SAndriy Gapon 	     * we should not test for ACPI_EVENT_FLAG_SET for time being.
2965b913a7d5SAndriy Gapon 	     */
2966b913a7d5SAndriy Gapon 	    if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON,
2967b913a7d5SAndriy Gapon 		&power_button_status)) && power_button_status != 0) {
2968b913a7d5SAndriy Gapon 		AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
2969b913a7d5SAndriy Gapon 		device_printf(sc->acpi_dev,
2970b913a7d5SAndriy Gapon 		    "cleared fixed power button status\n");
2971b913a7d5SAndriy Gapon 	    }
2972b913a7d5SAndriy Gapon 	}
2973b913a7d5SAndriy Gapon 
2974f0a101b7SMitsuru IWASAKI 	intr_restore(intr);
2975f0a101b7SMitsuru IWASAKI 
2976f0a101b7SMitsuru IWASAKI 	/* call acpi_wakeup_machdep() again with interrupt enabled */
2977f0a101b7SMitsuru IWASAKI 	acpi_wakeup_machdep(sc, state, sleep_result, 1);
2978f0a101b7SMitsuru IWASAKI 
2979f0a101b7SMitsuru IWASAKI 	if (sleep_result == -1)
2980a159c266SJung-uk Kim 		goto backout;
29816161544cSTakanori Watanabe 
29826161544cSTakanori Watanabe 	/* Re-enable ACPI hardware on wakeup from sleep state 4. */
2983be2b1797SNate Lawson 	if (state == ACPI_STATE_S4)
2984964679ceSMitsuru IWASAKI 	    AcpiEnable();
29856161544cSTakanori Watanabe     } else {
29861df130f1SJung-uk Kim 	status = AcpiEnterSleepState(state);
29871df130f1SJung-uk Kim 	AcpiLeaveSleepStatePrep(state);
298871804adcSJung-uk Kim 	intr_restore(intr);
2989be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2990be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2991be2b1797SNate Lawson 			  AcpiFormatException(status));
299233c70d41SAndriy Gapon 	    goto backout;
299315e32d5dSMike Smith 	}
29946161544cSTakanori Watanabe     }
299515e2f34fSNate Lawson     slp_state = ACPI_SS_SLEPT;
2996ece50487SMitsuru IWASAKI 
299715e2f34fSNate Lawson     /*
299815e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
299915e2f34fSNate Lawson      * process.  This handles both the error and success cases.
300015e2f34fSNate Lawson      */
300133c70d41SAndriy Gapon backout:
300215e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
300315e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
300415e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
300515e2f34fSNate Lawson     }
3006e3b56b66SMitsuru IWASAKI     if (slp_state >= ACPI_SS_DEV_SUSPEND)
3007e3b56b66SMitsuru IWASAKI 	DEVICE_RESUME(root_bus);
3008f0a101b7SMitsuru IWASAKI     if (slp_state >= ACPI_SS_SLP_PREP)
300915e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
3010a0a15716SJung-uk Kim     if (slp_state >= ACPI_SS_SLEPT) {
3011a0a15716SJung-uk Kim 	acpi_resync_clock(sc);
301215e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
3013a0a15716SJung-uk Kim     }
3014337744d9SJung-uk Kim     sc->acpi_next_sstate = 0;
301515e2f34fSNate Lawson 
3016a56fe095SJohn Baldwin     mtx_unlock(&Giant);
3017c66d2b38SJung-uk Kim 
3018fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
3019fdce57a0SJohn Baldwin     thread_lock(curthread);
3020fdce57a0SJohn Baldwin     sched_unbind(curthread);
3021fdce57a0SJohn Baldwin     thread_unlock(curthread);
3022fdce57a0SJohn Baldwin #else
302336a483bbSJung-uk Kim     if (smp_started) {
3024c66d2b38SJung-uk Kim 	thread_lock(curthread);
3025c66d2b38SJung-uk Kim 	sched_unbind(curthread);
3026c66d2b38SJung-uk Kim 	thread_unlock(curthread);
302736a483bbSJung-uk Kim     }
3028fdce57a0SJohn Baldwin #endif
3029c66d2b38SJung-uk Kim 
303085f95fffSAndriy Gapon     resume_all_proc();
303185f95fffSAndriy Gapon 
30324a8fa6feSJung-uk Kim     EVENTHANDLER_INVOKE(power_resume);
30334a8fa6feSJung-uk Kim 
3034d42f0ad8SJung-uk Kim     /* Allow another sleep request after a while. */
3035fadf3fb9SJohn Baldwin     callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME);
3036d42f0ad8SJung-uk Kim 
3037d42f0ad8SJung-uk Kim     /* Run /etc/rc.resume after we are back. */
3038d42f0ad8SJung-uk Kim     if (devctl_process_running())
3039d42f0ad8SJung-uk Kim 	acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
3040d42f0ad8SJung-uk Kim 
30410ae55423SMike Smith     return_ACPI_STATUS (status);
304215e32d5dSMike Smith }
304315e32d5dSMike Smith 
3044a0a15716SJung-uk Kim static void
30450755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc)
30460755473bSJung-uk Kim {
30470755473bSJung-uk Kim 
30480755473bSJung-uk Kim     /*
30490755473bSJung-uk Kim      * Warm up timecounter again and reset system clock.
30500755473bSJung-uk Kim      */
30510755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
30520755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
30530755473bSJung-uk Kim     inittodr(time_second + sc->acpi_sleep_delay);
30540755473bSJung-uk Kim }
30550755473bSJung-uk Kim 
3056e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
3057e8b4d56eSNate Lawson int
3058e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
3059e8b4d56eSNate Lawson {
3060e8b4d56eSNate Lawson     struct acpi_prw_data prw;
3061e8b4d56eSNate Lawson     ACPI_STATUS status;
3062e8b4d56eSNate Lawson     int flags;
3063e8b4d56eSNate Lawson 
3064d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
30652be4e471SJung-uk Kim     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
3066e8b4d56eSNate Lawson 	return (ENXIO);
3067e8b4d56eSNate Lawson 
3068d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
3069e8b4d56eSNate Lawson     if (enable) {
30705a77b11bSJung-uk Kim 	status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
30715a77b11bSJung-uk Kim 	    ACPI_GPE_ENABLE);
3072e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
3073e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
3074e8b4d56eSNate Lawson 	    return (ENXIO);
3075e8b4d56eSNate Lawson 	}
3076d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
3077e8b4d56eSNate Lawson     } else {
30785a77b11bSJung-uk Kim 	status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit,
30795a77b11bSJung-uk Kim 	    ACPI_GPE_DISABLE);
3080e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
3081e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
3082e8b4d56eSNate Lawson 	    return (ENXIO);
3083e8b4d56eSNate Lawson 	}
3084d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
3085e8b4d56eSNate Lawson     }
3086e8b4d56eSNate Lawson 
3087e8b4d56eSNate Lawson     return (0);
3088e8b4d56eSNate Lawson }
3089e8b4d56eSNate Lawson 
3090d4b9ff91SNate Lawson static int
3091d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
3092e8b4d56eSNate Lawson {
3093e8b4d56eSNate Lawson     struct acpi_prw_data prw;
3094d4b9ff91SNate Lawson     device_t dev;
3095e8b4d56eSNate Lawson 
3096d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
3097e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
3098e8b4d56eSNate Lawson 	return (ENXIO);
3099d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
3100e8b4d56eSNate Lawson 
3101e8b4d56eSNate Lawson     /*
3102d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
3103d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
3104d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
3105d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
3106d4b9ff91SNate Lawson      * and set _PSW.
3107e8b4d56eSNate Lawson      */
3108d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
31095a77b11bSJung-uk Kim 	AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE);
3110e8b4d56eSNate Lawson 	if (bootverbose)
3111d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
3112d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
3113d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
3114d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
3115d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
3116d4b9ff91SNate Lawson 	if (bootverbose)
3117d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
3118d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
3119d4b9ff91SNate Lawson     }
3120cc85c78cSNate Lawson 
3121cc85c78cSNate Lawson     return (0);
3122e8b4d56eSNate Lawson }
3123e8b4d56eSNate Lawson 
3124d4b9ff91SNate Lawson static int
3125d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
3126cc85c78cSNate Lawson {
3127cc85c78cSNate Lawson     struct acpi_prw_data prw;
3128d4b9ff91SNate Lawson     device_t dev;
3129cc85c78cSNate Lawson 
3130cc85c78cSNate Lawson     /*
3131d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
3132d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
3133cc85c78cSNate Lawson      */
3134d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
3135d4b9ff91SNate Lawson 	return (ENXIO);
3136d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
3137d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
3138d4b9ff91SNate Lawson 	return (0);
3139cc85c78cSNate Lawson 
3140d4b9ff91SNate Lawson     /*
3141d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
3142d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
3143d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
3144d4b9ff91SNate Lawson      */
3145d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
31465a77b11bSJung-uk Kim 	AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE);
3147d4b9ff91SNate Lawson 	if (bootverbose)
3148d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
3149d4b9ff91SNate Lawson     } else {
3150d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
3151d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
3152d4b9ff91SNate Lawson 	if (bootverbose)
3153d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
3154d4b9ff91SNate Lawson 		acpi_name(handle));
3155d4b9ff91SNate Lawson     }
3156d4b9ff91SNate Lawson 
3157e8b4d56eSNate Lawson     return (0);
3158e8b4d56eSNate Lawson }
3159e8b4d56eSNate Lawson 
3160e8b4d56eSNate Lawson static ACPI_STATUS
3161d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
3162e8b4d56eSNate Lawson {
3163d4b9ff91SNate Lawson     int sstate;
3164e8b4d56eSNate Lawson 
3165d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
3166d4b9ff91SNate Lawson     sstate = *(int *)context;
3167d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
3168d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
3169d4b9ff91SNate Lawson     else
3170d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
3171e8b4d56eSNate Lawson     return (AE_OK);
3172e8b4d56eSNate Lawson }
3173e8b4d56eSNate Lawson 
3174d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
3175e8b4d56eSNate Lawson static int
3176d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
3177e8b4d56eSNate Lawson {
3178e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
3179e8b4d56eSNate Lawson 
3180e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
3181d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
31822272d050SJung-uk Kim 	    acpi_wake_prep, NULL, &sstate, NULL);
3183e8b4d56eSNate Lawson     return (0);
3184e8b4d56eSNate Lawson }
3185e8b4d56eSNate Lawson 
318688a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
318788a79fc0SNate Lawson static int
318888a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
318988a79fc0SNate Lawson {
319088a79fc0SNate Lawson     int error, i, numdevs;
319188a79fc0SNate Lawson     device_t *devlist;
319288a79fc0SNate Lawson     device_t child;
3193d4b9ff91SNate Lawson     ACPI_STATUS status;
319488a79fc0SNate Lawson 
319588a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
31961c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
31971c9ec538SNate Lawson 	if (numdevs == 0)
31981c9ec538SNate Lawson 	    free(devlist, M_TEMP);
319988a79fc0SNate Lawson 	return (error);
32001c9ec538SNate Lawson     }
320188a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
320288a79fc0SNate Lawson 	child = devlist[i];
3203d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
320488a79fc0SNate Lawson 	if (!device_is_attached(child))
320588a79fc0SNate Lawson 	    continue;
3206d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
3207d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
320888a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
320988a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
321088a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
321188a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
321288a79fc0SNate Lawson 	}
321388a79fc0SNate Lawson     }
321488a79fc0SNate Lawson     free(devlist, M_TEMP);
321588a79fc0SNate Lawson 
321688a79fc0SNate Lawson     return (0);
321788a79fc0SNate Lawson }
321888a79fc0SNate Lawson 
321988a79fc0SNate Lawson /* Enable or disable wake from userland. */
322088a79fc0SNate Lawson static int
322188a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
322288a79fc0SNate Lawson {
322388a79fc0SNate Lawson     int enable, error;
322488a79fc0SNate Lawson     device_t dev;
322588a79fc0SNate Lawson 
322688a79fc0SNate Lawson     dev = (device_t)arg1;
3227d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
322888a79fc0SNate Lawson 
322988a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
323088a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
323188a79fc0SNate Lawson 	return (error);
323288a79fc0SNate Lawson     if (enable != 0 && enable != 1)
323388a79fc0SNate Lawson 	return (EINVAL);
323488a79fc0SNate Lawson 
323588a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
323688a79fc0SNate Lawson }
323788a79fc0SNate Lawson 
3238e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
3239d4b9ff91SNate Lawson int
3240e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
3241e8b4d56eSNate Lawson {
3242e8b4d56eSNate Lawson     ACPI_STATUS			status;
3243e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
3244e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
3245d4b9ff91SNate Lawson     int				error, i, power_count;
3246e8b4d56eSNate Lawson 
3247e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
3248e8b4d56eSNate Lawson 	return (EINVAL);
3249e8b4d56eSNate Lawson 
3250e8b4d56eSNate Lawson     /*
3251e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
3252e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
3253e8b4d56eSNate Lawson      */
3254e8b4d56eSNate Lawson     error = EINVAL;
3255e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
3256e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
3257e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
3258e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
3259e8b4d56eSNate Lawson 	return (ENOENT);
3260e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
3261e8b4d56eSNate Lawson     if (res == NULL)
3262e8b4d56eSNate Lawson 	return (ENOENT);
3263e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
3264e8b4d56eSNate Lawson 	goto out;
3265e8b4d56eSNate Lawson 
3266e8b4d56eSNate Lawson     /*
3267e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
3268e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
3269e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
3270e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
3271e8b4d56eSNate Lawson      */
3272e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
3273e8b4d56eSNate Lawson 	goto out;
3274e8b4d56eSNate Lawson 
3275e8b4d56eSNate Lawson     /*
3276e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
3277e8b4d56eSNate Lawson      */
3278e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
3279e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
3280e8b4d56eSNate Lawson 	/*
3281e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
3282e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
3283e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
3284e8b4d56eSNate Lawson 	 * enabled for the wake event.
3285e8b4d56eSNate Lawson 	 */
3286e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
3287e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
3288e8b4d56eSNate Lawson 	error = 0;
3289e8b4d56eSNate Lawson 	break;
3290e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
3291e8b4d56eSNate Lawson 	/*
3292e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
3293e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
3294e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
3295e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
3296e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
3297e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
3298e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
3299e8b4d56eSNate Lawson 	 * the wake event.
3300e8b4d56eSNate Lawson 	 *
3301e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
3302e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
3303e8b4d56eSNate Lawson 	 */
3304e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
3305e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
3306e8b4d56eSNate Lawson 	    goto out;
3307e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
3308e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
3309e8b4d56eSNate Lawson 	    goto out;
3310e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
3311e8b4d56eSNate Lawson 	    goto out;
3312e8b4d56eSNate Lawson 	error = 0;
3313e8b4d56eSNate Lawson 	break;
3314e8b4d56eSNate Lawson     default:
3315e8b4d56eSNate Lawson 	goto out;
3316e8b4d56eSNate Lawson     }
3317e8b4d56eSNate Lawson 
3318d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
3319d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
3320d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
3321d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
3322d4b9ff91SNate Lawson 	power_count = 0;
3323d4b9ff91SNate Lawson     }
3324d4b9ff91SNate Lawson     prw->power_res_count = power_count;
3325d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
3326d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
3327e8b4d56eSNate Lawson 
3328e8b4d56eSNate Lawson out:
3329e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
3330e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
3331e8b4d56eSNate Lawson     return (error);
3332e8b4d56eSNate Lawson }
3333e8b4d56eSNate Lawson 
333415e32d5dSMike Smith /*
333515e32d5dSMike Smith  * ACPI Event Handlers
333615e32d5dSMike Smith  */
333715e32d5dSMike Smith 
333815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
333915e32d5dSMike Smith 
334015e32d5dSMike Smith static void
334115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
334215e32d5dSMike Smith {
33432d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
334400a30448SNate Lawson     int ret;
3345bee4aa4aSNate Lawson 
3346b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
334715e32d5dSMike Smith 
3348a0e73a12SJung-uk Kim     /* Check if button action is disabled or unknown. */
3349a0e73a12SJung-uk Kim     if (state == ACPI_STATE_UNKNOWN)
3350813d6dcaSNate Lawson 	return;
3351813d6dcaSNate Lawson 
335200a30448SNate Lawson     /* Request that the system prepare to enter the given suspend state. */
33532d0c82e8SJung-uk Kim     ret = acpi_ReqSleepState(sc, state);
335400a30448SNate Lawson     if (ret != 0)
33552d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
33562d0c82e8SJung-uk Kim 	    "request to enter state S%d failed (err %d)\n", state, ret);
3357bee4aa4aSNate Lawson 
33580ae55423SMike Smith     return_VOID;
335915e32d5dSMike Smith }
336015e32d5dSMike Smith 
336115e32d5dSMike Smith static void
336215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
336315e32d5dSMike Smith {
3364bee4aa4aSNate Lawson 
3365b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
33660ae55423SMike Smith 
3367bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
3368cb9b0d80SMike Smith 
33690ae55423SMike Smith     return_VOID;
337015e32d5dSMike Smith }
337115e32d5dSMike Smith 
337215e32d5dSMike Smith /*
337315e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
337415e32d5dSMike Smith  */
3375b5854f5fSJung-uk Kim static void
3376b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context)
3377b5854f5fSJung-uk Kim {
3378b5854f5fSJung-uk Kim 
3379b5854f5fSJung-uk Kim     EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context);
3380b5854f5fSJung-uk Kim }
3381b5854f5fSJung-uk Kim 
3382b5854f5fSJung-uk Kim static void
3383b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context)
3384b5854f5fSJung-uk Kim {
3385b5854f5fSJung-uk Kim 
3386b5854f5fSJung-uk Kim     EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context);
3387b5854f5fSJung-uk Kim }
3388b5854f5fSJung-uk Kim 
338915e32d5dSMike Smith UINT32
339033febf93SNate Lawson acpi_event_power_button_sleep(void *context)
339115e32d5dSMike Smith {
339215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
339315e32d5dSMike Smith 
3394b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
33950ae55423SMike Smith 
3396b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3397b5854f5fSJung-uk Kim 	acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx)))
3398b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3399b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
340015e32d5dSMike Smith }
340115e32d5dSMike Smith 
340215e32d5dSMike Smith UINT32
340333febf93SNate Lawson acpi_event_power_button_wake(void *context)
340415e32d5dSMike Smith {
340515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
340615e32d5dSMike Smith 
3407b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
34080ae55423SMike Smith 
3409b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3410b5854f5fSJung-uk Kim 	acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx)))
3411b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3412b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
341315e32d5dSMike Smith }
341415e32d5dSMike Smith 
341515e32d5dSMike Smith UINT32
341633febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
341715e32d5dSMike Smith {
341815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
341915e32d5dSMike Smith 
3420b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
34210ae55423SMike Smith 
3422b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3423b5854f5fSJung-uk Kim 	acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx)))
3424b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3425b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
342615e32d5dSMike Smith }
342715e32d5dSMike Smith 
342815e32d5dSMike Smith UINT32
342933febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
343015e32d5dSMike Smith {
343115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
343215e32d5dSMike Smith 
3433b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
34340ae55423SMike Smith 
3435b5854f5fSJung-uk Kim     if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER,
3436b5854f5fSJung-uk Kim 	acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx)))
3437b5854f5fSJung-uk Kim 	return_VALUE (ACPI_INTERRUPT_NOT_HANDLED);
3438b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
343915e32d5dSMike Smith }
344015e32d5dSMike Smith 
344115e32d5dSMike Smith /*
3442bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
3443bee4aa4aSNate Lawson  * use this for single-threaded callers.
344415e32d5dSMike Smith  */
344515e32d5dSMike Smith char *
344615e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
344715e32d5dSMike Smith {
3448bee4aa4aSNate Lawson     ACPI_BUFFER buf;
3449bee4aa4aSNate Lawson     static char data[256];
345015e32d5dSMike Smith 
3451bee4aa4aSNate Lawson     buf.Length = sizeof(data);
3452bee4aa4aSNate Lawson     buf.Pointer = data;
3453cb9b0d80SMike Smith 
34540a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3455bee4aa4aSNate Lawson 	return (data);
3456bee4aa4aSNate Lawson     return ("(unknown)");
345715e32d5dSMike Smith }
345815e32d5dSMike Smith 
345915e32d5dSMike Smith /*
346015e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
346115e32d5dSMike Smith  * parts of the namespace.
346215e32d5dSMike Smith  */
346315e32d5dSMike Smith int
346415e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
346515e32d5dSMike Smith {
34663c600cfbSJohn Baldwin     char	*cp, *env, *np;
346715e32d5dSMike Smith     int		len;
346815e32d5dSMike Smith 
346915e32d5dSMike Smith     np = acpi_name(handle);
347015e32d5dSMike Smith     if (*np == '\\')
347115e32d5dSMike Smith 	np++;
34722be111bfSDavide Italiano     if ((env = kern_getenv("debug.acpi.avoid")) == NULL)
347315e32d5dSMike Smith 	return (0);
347415e32d5dSMike Smith 
3475be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
34763c600cfbSJohn Baldwin     cp = env;
347715e32d5dSMike Smith     for (;;) {
3478bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
347915e32d5dSMike Smith 	    cp++;
348015e32d5dSMike Smith 	if (*cp == 0)
348115e32d5dSMike Smith 	    break;
348215e32d5dSMike Smith 	len = 0;
3483bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
348415e32d5dSMike Smith 	    len++;
3485d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
34863c600cfbSJohn Baldwin 	    freeenv(env);
34870ae55423SMike Smith 	    return(1);
3488d786139cSMaxime Henrion 	}
34890ae55423SMike Smith 	cp += len;
34900ae55423SMike Smith     }
34913c600cfbSJohn Baldwin     freeenv(env);
3492be2b1797SNate Lawson 
34930ae55423SMike Smith     return (0);
34940ae55423SMike Smith }
34950ae55423SMike Smith 
34960ae55423SMike Smith /*
34970ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
34980ae55423SMike Smith  */
34990ae55423SMike Smith int
35000ae55423SMike Smith acpi_disabled(char *subsys)
35010ae55423SMike Smith {
350278689b15SMaxime Henrion     char	*cp, *env;
35030ae55423SMike Smith     int		len;
35040ae55423SMike Smith 
35052be111bfSDavide Italiano     if ((env = kern_getenv("debug.acpi.disabled")) == NULL)
35060ae55423SMike Smith 	return (0);
35073184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
350878689b15SMaxime Henrion 	freeenv(env);
35090ae55423SMike Smith 	return (1);
3510d786139cSMaxime Henrion     }
35110ae55423SMike Smith 
35123184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
351378689b15SMaxime Henrion     cp = env;
35140ae55423SMike Smith     for (;;) {
35153184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
35160ae55423SMike Smith 	    cp++;
35173184cf5aSNate Lawson 	if (*cp == '\0')
35180ae55423SMike Smith 	    break;
35190ae55423SMike Smith 	len = 0;
35203184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
35210ae55423SMike Smith 	    len++;
35223184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
352378689b15SMaxime Henrion 	    freeenv(env);
352415e32d5dSMike Smith 	    return (1);
3525d786139cSMaxime Henrion 	}
352615e32d5dSMike Smith 	cp += len;
352715e32d5dSMike Smith     }
352878689b15SMaxime Henrion     freeenv(env);
3529be2b1797SNate Lawson 
353015e32d5dSMike Smith     return (0);
353115e32d5dSMike Smith }
353215e32d5dSMike Smith 
353364de8019SJohn Baldwin static void
353464de8019SJohn Baldwin acpi_lookup(void *arg, const char *name, device_t *dev)
353564de8019SJohn Baldwin {
353664de8019SJohn Baldwin     ACPI_HANDLE handle;
353764de8019SJohn Baldwin 
353864de8019SJohn Baldwin     if (*dev != NULL)
353964de8019SJohn Baldwin 	return;
354064de8019SJohn Baldwin 
354164de8019SJohn Baldwin     /*
354264de8019SJohn Baldwin      * Allow any handle name that is specified as an absolute path and
354364de8019SJohn Baldwin      * starts with '\'.  We could restrict this to \_SB and friends,
354464de8019SJohn Baldwin      * but see acpi_probe_children() for notes on why we scan the entire
354564de8019SJohn Baldwin      * namespace for devices.
354664de8019SJohn Baldwin      *
354764de8019SJohn Baldwin      * XXX: The pathname argument to AcpiGetHandle() should be fixed to
354864de8019SJohn Baldwin      * be const.
354964de8019SJohn Baldwin      */
355064de8019SJohn Baldwin     if (name[0] != '\\')
355164de8019SJohn Baldwin 	return;
355264de8019SJohn Baldwin     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name),
355364de8019SJohn Baldwin 	&handle)))
355464de8019SJohn Baldwin 	return;
355564de8019SJohn Baldwin     *dev = acpi_get_device(handle);
355664de8019SJohn Baldwin }
355764de8019SJohn Baldwin 
355815e32d5dSMike Smith /*
355915e32d5dSMike Smith  * Control interface.
356015e32d5dSMike Smith  *
35610ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
3562be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
3563be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
356415e32d5dSMike Smith  */
35650ae55423SMike Smith struct acpi_ioctl_hook
35660ae55423SMike Smith {
35670ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
35680ae55423SMike Smith     u_long			 cmd;
3569be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
35700ae55423SMike Smith     void			 *arg;
35710ae55423SMike Smith };
35720ae55423SMike Smith 
35730ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
35740ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
35750ae55423SMike Smith 
35760ae55423SMike Smith int
3577be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
35780ae55423SMike Smith {
35790ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
35800ae55423SMike Smith 
35810ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
35820ae55423SMike Smith 	return (ENOMEM);
35830ae55423SMike Smith     hp->cmd = cmd;
35840ae55423SMike Smith     hp->fn = fn;
35850ae55423SMike Smith     hp->arg = arg;
358615e2f34fSNate Lawson 
358715e2f34fSNate Lawson     ACPI_LOCK(acpi);
35880ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
35890ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
35900ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
35910ae55423SMike Smith     }
35920ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
359315e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
359415e2f34fSNate Lawson 
35950ae55423SMike Smith     return (0);
35960ae55423SMike Smith }
35970ae55423SMike Smith 
35980ae55423SMike Smith void
3599be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
36000ae55423SMike Smith {
36010ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
36020ae55423SMike Smith 
360315e2f34fSNate Lawson     ACPI_LOCK(acpi);
36040ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
360515e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
36060ae55423SMike Smith 	    break;
36070ae55423SMike Smith 
36080ae55423SMike Smith     if (hp != NULL) {
36090ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
36100ae55423SMike Smith 	free(hp, M_ACPIDEV);
36110ae55423SMike Smith     }
361215e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
36130ae55423SMike Smith }
36140ae55423SMike Smith 
361515e32d5dSMike Smith static int
361600b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
361715e32d5dSMike Smith {
361815e32d5dSMike Smith     return (0);
361915e32d5dSMike Smith }
362015e32d5dSMike Smith 
362115e32d5dSMike Smith static int
362200b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
362315e32d5dSMike Smith {
362415e32d5dSMike Smith     return (0);
362515e32d5dSMike Smith }
362615e32d5dSMike Smith 
362715e32d5dSMike Smith static int
362800b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
362915e32d5dSMike Smith {
363015e32d5dSMike Smith     struct acpi_softc		*sc;
36310ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
3632bee4aa4aSNate Lawson     int				error, state;
363315e32d5dSMike Smith 
3634a2e35898SDavid E. O'Brien     error = 0;
3635a2e35898SDavid E. O'Brien     hp = NULL;
363615e32d5dSMike Smith     sc = dev->si_drv1;
363715e32d5dSMike Smith 
36380ae55423SMike Smith     /*
36390ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
36400ae55423SMike Smith      */
364115e2f34fSNate Lawson     ACPI_LOCK(acpi);
3642bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
36430ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3644bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
3645bee4aa4aSNate Lawson 		break;
36460ae55423SMike Smith 	}
364715e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
3648bee4aa4aSNate Lawson     if (hp)
3649bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
36500ae55423SMike Smith 
36510ae55423SMike Smith     /*
3652a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
3653a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
3654a89fcf28STakanori Watanabe      * Not changing system behavior.
3655a89fcf28STakanori Watanabe      */
3656be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
3657be2b1797SNate Lawson 	return (EPERM);
3658a89fcf28STakanori Watanabe 
3659be2b1797SNate Lawson     /* Core system ioctls. */
366015e32d5dSMike Smith     switch (cmd) {
366100a30448SNate Lawson     case ACPIIO_REQSLPSTATE:
366200a30448SNate Lawson 	state = *(int *)addr;
366300a30448SNate Lawson 	if (state != ACPI_STATE_S5)
3664a0e73a12SJung-uk Kim 	    return (acpi_ReqSleepState(sc, state));
3665a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
3666a0e73a12SJung-uk Kim 	error = EOPNOTSUPP;
366700a30448SNate Lawson 	break;
366800a30448SNate Lawson     case ACPIIO_ACKSLPSTATE:
366900a30448SNate Lawson 	error = *(int *)addr;
367000a30448SNate Lawson 	error = acpi_AckSleepState(sc->acpi_clone, error);
367100a30448SNate Lawson 	break;
367200a30448SNate Lawson     case ACPIIO_SETSLPSTATE:	/* DEPRECATED */
367315e32d5dSMike Smith 	state = *(int *)addr;
3674a0e73a12SJung-uk Kim 	if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
3675a0e73a12SJung-uk Kim 	    return (EINVAL);
3676a0e73a12SJung-uk Kim 	if (!acpi_sleep_states[state])
3677a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3678a0e73a12SJung-uk Kim 	if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
3679a0e73a12SJung-uk Kim 	    error = ENXIO;
368015e32d5dSMike Smith 	break;
368115e32d5dSMike Smith     default:
3682bee4aa4aSNate Lawson 	error = ENXIO;
368315e32d5dSMike Smith 	break;
368415e32d5dSMike Smith     }
3685917d44c8SMitsuru IWASAKI 
368615e32d5dSMike Smith     return (error);
368715e32d5dSMike Smith }
368815e32d5dSMike Smith 
36891d073b1dSJohn Baldwin static int
3690a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname)
3691a0e73a12SJung-uk Kim {
3692a0e73a12SJung-uk Kim     int sstate;
3693a0e73a12SJung-uk Kim 
3694a0e73a12SJung-uk Kim     if (toupper(sname[0]) == 'S') {
3695a0e73a12SJung-uk Kim 	sstate = sname[1] - '0';
3696a0e73a12SJung-uk Kim 	if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
3697a0e73a12SJung-uk Kim 	    sname[2] == '\0')
3698a0e73a12SJung-uk Kim 	    return (sstate);
3699a0e73a12SJung-uk Kim     } else if (strcasecmp(sname, "NONE") == 0)
3700a0e73a12SJung-uk Kim 	return (ACPI_STATE_UNKNOWN);
3701a0e73a12SJung-uk Kim     return (-1);
3702a0e73a12SJung-uk Kim }
3703a0e73a12SJung-uk Kim 
3704a0e73a12SJung-uk Kim static const char *
3705a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate)
3706a0e73a12SJung-uk Kim {
3707a0e73a12SJung-uk Kim     static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
3708a0e73a12SJung-uk Kim 
3709a0e73a12SJung-uk Kim     if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
3710a0e73a12SJung-uk Kim 	return (snames[sstate]);
3711a0e73a12SJung-uk Kim     else if (sstate == ACPI_STATE_UNKNOWN)
3712a0e73a12SJung-uk Kim 	return ("NONE");
3713a0e73a12SJung-uk Kim     return (NULL);
3714a0e73a12SJung-uk Kim }
3715a0e73a12SJung-uk Kim 
3716a0e73a12SJung-uk Kim static int
3717d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3718d75de536SMitsuru IWASAKI {
3719d75de536SMitsuru IWASAKI     int error;
3720bee4aa4aSNate Lawson     struct sbuf sb;
3721a0e73a12SJung-uk Kim     UINT8 state;
3722d75de536SMitsuru IWASAKI 
3723bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3724a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
3725a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state])
3726a0e73a12SJung-uk Kim 	    sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
3727bee4aa4aSNate Lawson     sbuf_trim(&sb);
3728bee4aa4aSNate Lawson     sbuf_finish(&sb);
3729bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3730bee4aa4aSNate Lawson     sbuf_delete(&sb);
3731d75de536SMitsuru IWASAKI     return (error);
3732d75de536SMitsuru IWASAKI }
3733d75de536SMitsuru IWASAKI 
3734d75de536SMitsuru IWASAKI static int
37351d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
37361d073b1dSJohn Baldwin {
37371d073b1dSJohn Baldwin     char sleep_state[10];
3738a0e73a12SJung-uk Kim     int error, new_state, old_state;
37391d073b1dSJohn Baldwin 
3740a0e73a12SJung-uk Kim     old_state = *(int *)oidp->oid_arg1;
3741a0e73a12SJung-uk Kim     strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
37421d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
37431d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
3744a0e73a12SJung-uk Kim 	new_state = acpi_sname2sstate(sleep_state);
3745a0e73a12SJung-uk Kim 	if (new_state < ACPI_STATE_S1)
3746a0e73a12SJung-uk Kim 	    return (EINVAL);
374754b43614SJung-uk Kim 	if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
3748a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3749be2b1797SNate Lawson 	if (new_state != old_state)
3750a0e73a12SJung-uk Kim 	    *(int *)oidp->oid_arg1 = new_state;
37511d073b1dSJohn Baldwin     }
37521d073b1dSJohn Baldwin     return (error);
37531d073b1dSJohn Baldwin }
37541d073b1dSJohn Baldwin 
37559b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
37569b937d48SNate Lawson void
37579b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
37589b937d48SNate Lawson {
37599b937d48SNate Lawson     char		notify_buf[16];
37609b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
37619b937d48SNate Lawson     ACPI_STATUS		status;
37629b937d48SNate Lawson 
37639b937d48SNate Lawson     if (subsystem == NULL)
37649b937d48SNate Lawson 	return;
37659b937d48SNate Lawson 
37669b937d48SNate Lawson     handle_buf.Pointer = NULL;
37679b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
37680594dadeSJung-uk Kim     status = AcpiNsHandleToPathname(h, &handle_buf, FALSE);
37699b937d48SNate Lawson     if (ACPI_FAILURE(status))
37709b937d48SNate Lawson 	return;
37719b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
37729b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
37739b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
37749b937d48SNate Lawson }
37759b937d48SNate Lawson 
377615e32d5dSMike Smith #ifdef ACPI_DEBUG
37770ae55423SMike Smith /*
37780ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
37790ae55423SMike Smith  *
37800ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
37810ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
37820ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
37830ae55423SMike Smith  * prefixing the bit name with !.
37840ae55423SMike Smith  */
378515e32d5dSMike Smith struct debugtag
378615e32d5dSMike Smith {
378715e32d5dSMike Smith     char	*name;
378815e32d5dSMike Smith     UINT32	value;
378915e32d5dSMike Smith };
379015e32d5dSMike Smith 
379115e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
3792c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
3793c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
3794c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
3795c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
3796c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
3797c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
3798c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
3799c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
3800c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
3801d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
38024c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
3803d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
3804297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
38054c1cdee6SMike Smith 
3806c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
3807c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
38083184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
3809c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
38103184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
38113184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
38123184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
38134c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
3814cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
38153184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
3816b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
381715e32d5dSMike Smith     {NULL, 0}
381815e32d5dSMike Smith };
381915e32d5dSMike Smith 
382015e32d5dSMike Smith static struct debugtag dbg_level[] = {
38219501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
38224c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
38239501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
3824cc6455afSJung-uk Kim     {"ACPI_LV_REPAIR",		ACPI_LV_REPAIR},
38254c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
3826d62ab2f4SMitsuru IWASAKI 
3827d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
3828297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
38294c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
38304c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
3831d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
38324c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
38334c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
38344c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
38354c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
38364c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
38374c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
38384c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
38394c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
38404c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
38414c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
3842d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
3843d62ab2f4SMitsuru IWASAKI 
3844d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
3845d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
3846d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
3847d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
3848d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
38494c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
3850d62ab2f4SMitsuru IWASAKI 
3851d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3852d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
3853d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
3854d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
3855d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
3856d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
3857d62ab2f4SMitsuru IWASAKI 
3858d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
385998479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
386098479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
386198479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
386298479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
386398479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
386415e32d5dSMike Smith     {NULL, 0}
386515e32d5dSMike Smith };
386615e32d5dSMike Smith 
386715e32d5dSMike Smith static void
386815e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
386915e32d5dSMike Smith {
387015e32d5dSMike Smith     char	*ep;
387115e32d5dSMike Smith     int		i, l;
38720ae55423SMike Smith     int		set;
387315e32d5dSMike Smith 
387415e32d5dSMike Smith     while (*cp) {
387515e32d5dSMike Smith 	if (isspace(*cp)) {
387615e32d5dSMike Smith 	    cp++;
387715e32d5dSMike Smith 	    continue;
387815e32d5dSMike Smith 	}
387915e32d5dSMike Smith 	ep = cp;
388015e32d5dSMike Smith 	while (*ep && !isspace(*ep))
388115e32d5dSMike Smith 	    ep++;
38820ae55423SMike Smith 	if (*cp == '!') {
38830ae55423SMike Smith 	    set = 0;
38840ae55423SMike Smith 	    cp++;
38850ae55423SMike Smith 	    if (cp == ep)
38860ae55423SMike Smith 		continue;
38870ae55423SMike Smith 	} else {
38880ae55423SMike Smith 	    set = 1;
38890ae55423SMike Smith 	}
389015e32d5dSMike Smith 	l = ep - cp;
389115e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
389215e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
3893be2b1797SNate Lawson 		if (set)
389415e32d5dSMike Smith 		    *flag |= tag[i].value;
3895be2b1797SNate Lawson 		else
38960ae55423SMike Smith 		    *flag &= ~tag[i].value;
389715e32d5dSMike Smith 	    }
389815e32d5dSMike Smith 	}
389915e32d5dSMike Smith 	cp = ep;
390015e32d5dSMike Smith     }
390115e32d5dSMike Smith }
390215e32d5dSMike Smith 
390315e32d5dSMike Smith static void
39042a4ac806SMike Smith acpi_set_debugging(void *junk)
390515e32d5dSMike Smith {
39067e639165SNate Lawson     char	*layer, *level;
390715e32d5dSMike Smith 
39081d7b121cSNate Lawson     if (cold) {
39090ae55423SMike Smith 	AcpiDbgLayer = 0;
39100ae55423SMike Smith 	AcpiDbgLevel = 0;
39111d7b121cSNate Lawson     }
39121d7b121cSNate Lawson 
39132be111bfSDavide Italiano     layer = kern_getenv("debug.acpi.layer");
39142be111bfSDavide Italiano     level = kern_getenv("debug.acpi.level");
39157e639165SNate Lawson     if (layer == NULL && level == NULL)
39167e639165SNate Lawson 	return;
39170ae55423SMike Smith 
39187e639165SNate Lawson     printf("ACPI set debug");
39197e639165SNate Lawson     if (layer != NULL) {
39207e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
39217e639165SNate Lawson 	    printf(" layer '%s'", layer);
39227e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
39237e639165SNate Lawson 	freeenv(layer);
39241d7b121cSNate Lawson     }
39257e639165SNate Lawson     if (level != NULL) {
39267e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
39277e639165SNate Lawson 	    printf(" level '%s'", level);
39287e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
39297e639165SNate Lawson 	freeenv(level);
39307e639165SNate Lawson     }
39317e639165SNate Lawson     printf("\n");
393215e32d5dSMike Smith }
3933bee4aa4aSNate Lawson 
3934be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3935be2b1797SNate Lawson 	NULL);
39361d7b121cSNate Lawson 
39371d7b121cSNate Lawson static int
39381d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
39391d7b121cSNate Lawson {
394054f6bca0SNate Lawson     int		 error, *dbg;
39411d7b121cSNate Lawson     struct	 debugtag *tag;
394254f6bca0SNate Lawson     struct	 sbuf sb;
39430e1152fcSHans Petter Selasky     char	 temp[128];
39441d7b121cSNate Lawson 
394554f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
394654f6bca0SNate Lawson 	return (ENOMEM);
39471d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
39481d7b121cSNate Lawson 	tag = &dbg_layer[0];
39491d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
39501d7b121cSNate Lawson     } else {
39511d7b121cSNate Lawson 	tag = &dbg_level[0];
39521d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
39531d7b121cSNate Lawson     }
39541d7b121cSNate Lawson 
39551d7b121cSNate Lawson     /* Get old values if this is a get request. */
395615e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
39571d7b121cSNate Lawson     if (*dbg == 0) {
395854f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
39591d7b121cSNate Lawson     } else if (req->newptr == NULL) {
39601d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
396154f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
396254f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
39631d7b121cSNate Lawson 	}
39641d7b121cSNate Lawson     }
396554f6bca0SNate Lawson     sbuf_trim(&sb);
396654f6bca0SNate Lawson     sbuf_finish(&sb);
39670e1152fcSHans Petter Selasky     strlcpy(temp, sbuf_data(&sb), sizeof(temp));
396854f6bca0SNate Lawson     sbuf_delete(&sb);
39691d7b121cSNate Lawson 
39700e1152fcSHans Petter Selasky     error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
39710e1152fcSHans Petter Selasky 
39720e1152fcSHans Petter Selasky     /* Check for error or no change */
39731d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
39741d7b121cSNate Lawson 	*dbg = 0;
39750e1152fcSHans Petter Selasky 	kern_setenv((char *)oidp->oid_arg1, temp);
39761d7b121cSNate Lawson 	acpi_set_debugging(NULL);
39771d7b121cSNate Lawson     }
397815e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
39791d7b121cSNate Lawson 
39801d7b121cSNate Lawson     return (error);
39811d7b121cSNate Lawson }
3982bee4aa4aSNate Lawson 
39831d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
39841d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
39851d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
39861d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3987bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3988f9390180SMitsuru IWASAKI 
3989f9390180SMitsuru IWASAKI static int
39902a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
39912a18c71dSJung-uk Kim {
39922a18c71dSJung-uk Kim 	int	error;
39932a18c71dSJung-uk Kim 	int	old;
39942a18c71dSJung-uk Kim 
39952a18c71dSJung-uk Kim 	old = acpi_debug_objects;
39962a18c71dSJung-uk Kim 	error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
39972a18c71dSJung-uk Kim 	if (error != 0 || req->newptr == NULL)
39982a18c71dSJung-uk Kim 		return (error);
39992a18c71dSJung-uk Kim 	if (old == acpi_debug_objects || (old && acpi_debug_objects))
40002a18c71dSJung-uk Kim 		return (0);
40012a18c71dSJung-uk Kim 
40022a18c71dSJung-uk Kim 	ACPI_SERIAL_BEGIN(acpi);
40032a18c71dSJung-uk Kim 	AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
40042a18c71dSJung-uk Kim 	ACPI_SERIAL_END(acpi);
40052a18c71dSJung-uk Kim 
40062a18c71dSJung-uk Kim 	return (0);
40072a18c71dSJung-uk Kim }
40082a18c71dSJung-uk Kim 
40092a18c71dSJung-uk Kim static int
4010ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface)
4011ae19af49SJung-uk Kim {
4012ae19af49SJung-uk Kim 	char *p;
4013ae19af49SJung-uk Kim 	size_t len;
4014ae19af49SJung-uk Kim 	int i, j;
4015ae19af49SJung-uk Kim 
4016ae19af49SJung-uk Kim 	p = str;
4017ae19af49SJung-uk Kim 	while (isspace(*p) || *p == ',')
4018ae19af49SJung-uk Kim 		p++;
4019ae19af49SJung-uk Kim 	len = strlen(p);
4020ae19af49SJung-uk Kim 	if (len == 0)
4021ae19af49SJung-uk Kim 		return (0);
4022ae19af49SJung-uk Kim 	p = strdup(p, M_TEMP);
4023ae19af49SJung-uk Kim 	for (i = 0; i < len; i++)
4024ae19af49SJung-uk Kim 		if (p[i] == ',')
4025ae19af49SJung-uk Kim 			p[i] = '\0';
4026ae19af49SJung-uk Kim 	i = j = 0;
4027ae19af49SJung-uk Kim 	while (i < len)
4028ae19af49SJung-uk Kim 		if (isspace(p[i]) || p[i] == '\0')
4029ae19af49SJung-uk Kim 			i++;
4030ae19af49SJung-uk Kim 		else {
4031ae19af49SJung-uk Kim 			i += strlen(p + i) + 1;
4032ae19af49SJung-uk Kim 			j++;
4033ae19af49SJung-uk Kim 		}
4034ae19af49SJung-uk Kim 	if (j == 0) {
4035ae19af49SJung-uk Kim 		free(p, M_TEMP);
4036ae19af49SJung-uk Kim 		return (0);
4037ae19af49SJung-uk Kim 	}
4038ae19af49SJung-uk Kim 	iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK);
4039ae19af49SJung-uk Kim 	iface->num = j;
4040ae19af49SJung-uk Kim 	i = j = 0;
4041ae19af49SJung-uk Kim 	while (i < len)
4042ae19af49SJung-uk Kim 		if (isspace(p[i]) || p[i] == '\0')
4043ae19af49SJung-uk Kim 			i++;
4044ae19af49SJung-uk Kim 		else {
4045ae19af49SJung-uk Kim 			iface->data[j] = p + i;
4046ae19af49SJung-uk Kim 			i += strlen(p + i) + 1;
4047ae19af49SJung-uk Kim 			j++;
4048ae19af49SJung-uk Kim 		}
4049ae19af49SJung-uk Kim 
4050ae19af49SJung-uk Kim 	return (j);
4051ae19af49SJung-uk Kim }
4052ae19af49SJung-uk Kim 
4053ae19af49SJung-uk Kim static void
4054ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface)
4055ae19af49SJung-uk Kim {
4056ae19af49SJung-uk Kim 
4057ae19af49SJung-uk Kim 	free(iface->data[0], M_TEMP);
4058ae19af49SJung-uk Kim 	free(iface->data, M_TEMP);
4059ae19af49SJung-uk Kim }
4060ae19af49SJung-uk Kim 
4061ae19af49SJung-uk Kim static void
4062ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev)
4063ae19af49SJung-uk Kim {
4064ae19af49SJung-uk Kim 	struct acpi_interface list;
4065ae19af49SJung-uk Kim 	ACPI_STATUS status;
4066ae19af49SJung-uk Kim 	int i;
4067ae19af49SJung-uk Kim 
4068ae19af49SJung-uk Kim 	if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) {
4069ae19af49SJung-uk Kim 		for (i = 0; i < list.num; i++) {
4070ae19af49SJung-uk Kim 			status = AcpiInstallInterface(list.data[i]);
4071ae19af49SJung-uk Kim 			if (ACPI_FAILURE(status))
4072ae19af49SJung-uk Kim 				device_printf(dev,
4073ae19af49SJung-uk Kim 				    "failed to install _OSI(\"%s\"): %s\n",
4074ae19af49SJung-uk Kim 				    list.data[i], AcpiFormatException(status));
4075ae19af49SJung-uk Kim 			else if (bootverbose)
4076ae19af49SJung-uk Kim 				device_printf(dev, "installed _OSI(\"%s\")\n",
4077ae19af49SJung-uk Kim 				    list.data[i]);
4078ae19af49SJung-uk Kim 		}
4079ae19af49SJung-uk Kim 		acpi_free_interfaces(&list);
4080ae19af49SJung-uk Kim 	}
4081ae19af49SJung-uk Kim 	if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) {
4082ae19af49SJung-uk Kim 		for (i = 0; i < list.num; i++) {
4083ae19af49SJung-uk Kim 			status = AcpiRemoveInterface(list.data[i]);
4084ae19af49SJung-uk Kim 			if (ACPI_FAILURE(status))
4085ae19af49SJung-uk Kim 				device_printf(dev,
4086ae19af49SJung-uk Kim 				    "failed to remove _OSI(\"%s\"): %s\n",
4087ae19af49SJung-uk Kim 				    list.data[i], AcpiFormatException(status));
4088ae19af49SJung-uk Kim 			else if (bootverbose)
4089ae19af49SJung-uk Kim 				device_printf(dev, "removed _OSI(\"%s\")\n",
4090ae19af49SJung-uk Kim 				    list.data[i]);
4091ae19af49SJung-uk Kim 		}
4092ae19af49SJung-uk Kim 		acpi_free_interfaces(&list);
4093ae19af49SJung-uk Kim 	}
4094ae19af49SJung-uk Kim }
4095ae19af49SJung-uk Kim 
4096ae19af49SJung-uk Kim static int
4097f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
4098f9390180SMitsuru IWASAKI {
4099f9390180SMitsuru IWASAKI 	int	state, acpi_state;
4100f9390180SMitsuru IWASAKI 	int	error;
4101f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
4102f9390180SMitsuru IWASAKI 	va_list	ap;
4103f9390180SMitsuru IWASAKI 
4104f9390180SMitsuru IWASAKI 	error = 0;
4105f9390180SMitsuru IWASAKI 	switch (cmd) {
4106f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
4107f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
4108f9390180SMitsuru IWASAKI 		if (sc == NULL) {
4109f9390180SMitsuru IWASAKI 			error = EINVAL;
4110f9390180SMitsuru IWASAKI 			goto out;
4111f9390180SMitsuru IWASAKI 		}
4112f9390180SMitsuru IWASAKI 
4113f9390180SMitsuru IWASAKI 		va_start(ap, arg);
4114f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
4115f9390180SMitsuru IWASAKI 		va_end(ap);
4116f9390180SMitsuru IWASAKI 
4117f9390180SMitsuru IWASAKI 		switch (state) {
4118f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
4119f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
4120f9390180SMitsuru IWASAKI 			break;
4121f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
4122f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
4123f9390180SMitsuru IWASAKI 			break;
4124f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
4125f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
4126f9390180SMitsuru IWASAKI 			break;
4127f9390180SMitsuru IWASAKI 		default:
4128f9390180SMitsuru IWASAKI 			error = EINVAL;
4129f9390180SMitsuru IWASAKI 			goto out;
4130f9390180SMitsuru IWASAKI 		}
4131f9390180SMitsuru IWASAKI 
413200a30448SNate Lawson 		if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
413300a30448SNate Lawson 			error = ENXIO;
4134f9390180SMitsuru IWASAKI 		break;
4135f9390180SMitsuru IWASAKI 	default:
4136f9390180SMitsuru IWASAKI 		error = EINVAL;
4137f9390180SMitsuru IWASAKI 		goto out;
4138f9390180SMitsuru IWASAKI 	}
4139f9390180SMitsuru IWASAKI 
4140f9390180SMitsuru IWASAKI out:
4141f9390180SMitsuru IWASAKI 	return (error);
4142f9390180SMitsuru IWASAKI }
4143f9390180SMitsuru IWASAKI 
4144f9390180SMitsuru IWASAKI static void
4145f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
4146f9390180SMitsuru IWASAKI {
4147be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
41486c407052SMitsuru IWASAKI 	return;
4149f9390180SMitsuru IWASAKI 
4150f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
4151f9390180SMitsuru IWASAKI }
4152f9390180SMitsuru IWASAKI 
4153f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
4154