xref: /freebsd/sys/dev/acpica/acpi.c (revision 0bba6acf85b9ca94d7bb97d4da6add28bc4692c3)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  */
2915e32d5dSMike Smith 
30dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$");
32dad97feeSDavid E. O'Brien 
3315e32d5dSMike Smith #include "opt_acpi.h"
3415e32d5dSMike Smith #include <sys/param.h>
3515e32d5dSMike Smith #include <sys/kernel.h>
36fb919e4dSMark Murray #include <sys/proc.h>
37a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3815e32d5dSMike Smith #include <sys/malloc.h>
39fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4015e32d5dSMike Smith #include <sys/bus.h>
4115e32d5dSMike Smith #include <sys/conf.h>
4215e32d5dSMike Smith #include <sys/ioccom.h>
4315e32d5dSMike Smith #include <sys/reboot.h>
4415e32d5dSMike Smith #include <sys/sysctl.h>
4515e32d5dSMike Smith #include <sys/ctype.h>
461611ea87SMitsuru IWASAKI #include <sys/linker.h>
47f9390180SMitsuru IWASAKI #include <sys/power.h>
4854f6bca0SNate Lawson #include <sys/sbuf.h>
49eeb3a05fSNate Lawson #include <sys/smp.h>
5015e32d5dSMike Smith 
5115e32d5dSMike Smith #include <machine/resource.h>
52dc750869SNate Lawson #include <machine/bus.h>
53dc750869SNate Lawson #include <sys/rman.h>
54bc0f2195SMike Smith #include <isa/isavar.h>
55c796e27bSNate Lawson #include <isa/pnpvar.h>
56bc0f2195SMike Smith 
572a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h>
5815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5915e32d5dSMike Smith #include <dev/acpica/acpiio.h>
6087a500cdSNate Lawson #include <contrib/dev/acpica/achware.h>
619b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
6215e32d5dSMike Smith 
6310ce62b9SNate Lawson #include "pci_if.h"
6410ce62b9SNate Lawson #include <dev/pci/pcivar.h>
6510ce62b9SNate Lawson #include <dev/pci/pci_private.h>
6610ce62b9SNate Lawson 
6715e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6815e32d5dSMike Smith 
69be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
70cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
71b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
720ae55423SMike Smith 
7315e32d5dSMike Smith static d_open_t		acpiopen;
7415e32d5dSMike Smith static d_close_t	acpiclose;
7515e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7615e32d5dSMike Smith 
7715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
78dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
797ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
807ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
817ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
827ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
8315e32d5dSMike Smith };
8415e32d5dSMike Smith 
85346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
86cb9b0d80SMike Smith struct mtx	acpi_mutex;
87cb9b0d80SMike Smith 
8889fb5af8SNate Lawson /* Bitmap of device quirks. */
8989fb5af8SNate Lawson int		acpi_quirks;
9089fb5af8SNate Lawson 
916d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9215e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9315e32d5dSMike Smith static int	acpi_probe(device_t dev);
9415e32d5dSMike Smith static int	acpi_attach(device_t dev);
9510ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
9610ce62b9SNate Lawson static int	acpi_resume(device_t dev);
97169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
98be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
99be2b1797SNate Lawson 			int unit);
10015e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10110ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
10210ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
103be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
104be2b1797SNate Lawson 			uintptr_t *result);
105be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
106be2b1797SNate Lawson 			uintptr_t value);
10791233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
108adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
109adad4744SNate Lawson static struct resource_list_entry *acpi_sysres_find(device_t dev, int type,
110adad4744SNate Lawson 		    u_long addr);
111be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
112be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
113be2b1797SNate Lawson 			u_long count, u_int flags);
114be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
115be2b1797SNate Lawson 			int rid, struct resource *r);
1168b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1178b28b622SNate Lawson 		    int rid);
1181e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1191e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
120ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
121ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
122ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
123ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
12410ce62b9SNate Lawson static int	acpi_device_pwr_for_sleep(device_t bus, device_t dev,
12510ce62b9SNate Lawson 		    int *dstate);
126df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
127df8d2a32SNate Lawson 		    void *context, void **retval);
128df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
129df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
13010ce62b9SNate Lawson static int	acpi_set_powerstate_method(device_t bus, device_t child,
13110ce62b9SNate Lawson 		    int state);
132be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
133be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
13415e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
135b82ca9a9SNate Lawson static int	acpi_probe_order(ACPI_HANDLE handle, int *order);
136be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
137be2b1797SNate Lawson 		    void *context, void **status);
138ce619b2eSNate Lawson static BOOLEAN	acpi_MatchHid(ACPI_HANDLE h, const char *hid);
13915e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
14013d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
141d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
142d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
143d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
14488a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
14588a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
14615e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
14715e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
148d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1491d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
150f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
151879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
152879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
153879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
154879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
155879d6c23STakanori Watanabe 
15615e32d5dSMike Smith static device_method_t acpi_methods[] = {
15715e32d5dSMike Smith     /* Device interface */
15815e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
15915e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
16015e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
161169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
16256a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
16310ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
16410ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
16515e32d5dSMike Smith 
16615e32d5dSMike Smith     /* Bus interface */
16715e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
16815e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
16910ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
17010ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
17115e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
17215e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
17391233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
17491233413SNate Lawson     DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
17591233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
17615e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
17715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
1788b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
179879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
180879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
18115e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
18215e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
18315e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
18415e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
18515e32d5dSMike Smith 
186ce619b2eSNate Lawson     /* ACPI bus */
187ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
188ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
18910ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
190df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
191ce619b2eSNate Lawson 
19210ce62b9SNate Lawson     /* PCI emulation */
19310ce62b9SNate Lawson     DEVMETHOD(pci_set_powerstate,	acpi_set_powerstate_method),
19410ce62b9SNate Lawson 
195bc0f2195SMike Smith     /* ISA emulation */
196bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
197bc0f2195SMike Smith 
19815e32d5dSMike Smith     {0, 0}
19915e32d5dSMike Smith };
20015e32d5dSMike Smith 
20115e32d5dSMike Smith static driver_t acpi_driver = {
20215e32d5dSMike Smith     "acpi",
20315e32d5dSMike Smith     acpi_methods,
20415e32d5dSMike Smith     sizeof(struct acpi_softc),
20515e32d5dSMike Smith };
20615e32d5dSMike Smith 
2073273b005SMike Smith static devclass_t acpi_devclass;
2086d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
209a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
21015e32d5dSMike Smith 
21115e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
21215e2f34fSNate Lawson 
213adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
214adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
215adad4744SNate Lawson 
216bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
217bee4aa4aSNate Lawson 
218865b8d0bSNate Lawson static const char* sleep_state_names[] = {
219865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
220865b8d0bSNate Lawson 
221197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
2221d7b121cSNate Lawson static char acpi_ca_version[12];
2231d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2241d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
22515e32d5dSMike Smith 
22615e32d5dSMike Smith /*
227413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
228c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
229c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
230c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
231c9b8d77dSNate Lawson  * some IBMs use such code.
232413081d7SNate Lawson  */
233c9b8d77dSNate Lawson static int acpi_serialize_methods;
234413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
235413081d7SNate Lawson 
23610ce62b9SNate Lawson /* Power devices off and on in suspend and resume.  XXX Remove once tested. */
23710ce62b9SNate Lawson static int acpi_do_powerstate = 1;
23810ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
23910ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
24010ce62b9SNate Lawson     &acpi_do_powerstate, 1, "Turn off devices when suspending.");
24110ce62b9SNate Lawson 
24239da3eb3SNate Lawson /* Allow users to override quirks. */
24339da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
24439da3eb3SNate Lawson 
245c9b8d77dSNate Lawson /*
2466d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2476d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
248be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
249be2b1797SNate Lawson  * off it.
2506d63101aSMike Smith  */
2516d63101aSMike Smith static int
2526d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2536d63101aSMike Smith {
2546d63101aSMike Smith     switch (event) {
2556d63101aSMike Smith     case MOD_LOAD:
25687b45ed5SMitsuru IWASAKI 	if (!cold) {
25787b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2586d63101aSMike Smith 	    return (EPERM);
25987b45ed5SMitsuru IWASAKI 	}
2606d63101aSMike Smith 	break;
2616d63101aSMike Smith     case MOD_UNLOAD:
262f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2636d63101aSMike Smith 	    return (EBUSY);
264f9390180SMitsuru IWASAKI 	break;
2656d63101aSMike Smith     default:
2666d63101aSMike Smith 	break;
2676d63101aSMike Smith     }
2686d63101aSMike Smith     return (0);
2696d63101aSMike Smith }
2706d63101aSMike Smith 
2716d63101aSMike Smith /*
272bbc2815cSJohn Baldwin  * Perform early initialization.
27315e32d5dSMike Smith  */
274bbc2815cSJohn Baldwin ACPI_STATUS
275bbc2815cSJohn Baldwin acpi_Startup(void)
27615e32d5dSMike Smith {
27789fb5af8SNate Lawson     static int started = 0;
27889fb5af8SNate Lawson     int error, val;
27915e32d5dSMike Smith 
2801b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2811b8c233dSPeter Pentchev 
282bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
283bbc2815cSJohn Baldwin     if (started)
28489fb5af8SNate Lawson 	return_VALUE (0);
285bbc2815cSJohn Baldwin     started = 1;
28615e32d5dSMike Smith 
287be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2886008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
289cb9b0d80SMike Smith 
290413081d7SNate Lawson     /*
291413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
292f3fc4f8bSNate Lawson      * uses UINT8 for some values and we have no tunable_byte.
293413081d7SNate Lawson      */
294834a79deSNate Lawson     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
295834a79deSNate Lawson     AcpiGbl_EnableInterpreterSlack = TRUE;
296413081d7SNate Lawson 
297be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
298b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
299bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
300bbc2815cSJohn Baldwin 	return_VALUE (error);
30115e32d5dSMike Smith     }
3021611ea87SMitsuru IWASAKI 
303b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
304bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
30589fb5af8SNate Lawson 	AcpiTerminate();
306bbc2815cSJohn Baldwin 	return_VALUE (error);
30715e32d5dSMike Smith     }
3083184cf5aSNate Lawson 
30989fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
31039da3eb3SNate Lawson     if (acpi_quirks == 0)
31189fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
31289fb5af8SNate Lawson 
31339da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
31489fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
31589fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
31689fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
31789fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
31889fb5af8SNate Lawson 	AcpiTerminate();
3194e376d58SNate Lawson 	return_VALUE (AE_ERROR);
32089fb5af8SNate Lawson     }
3213184cf5aSNate Lawson 
322bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
323bbc2815cSJohn Baldwin }
324bbc2815cSJohn Baldwin 
325bbc2815cSJohn Baldwin /*
326bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
327bbc2815cSJohn Baldwin  */
328bbc2815cSJohn Baldwin static void
329bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
330bbc2815cSJohn Baldwin {
331bbc2815cSJohn Baldwin     device_t	child;
332bbc2815cSJohn Baldwin 
333bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
334bbc2815cSJohn Baldwin 
335bbc2815cSJohn Baldwin     if (!cold)
336bbc2815cSJohn Baldwin 	return_VOID;
337bbc2815cSJohn Baldwin 
338bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
339bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
340bbc2815cSJohn Baldwin 	return_VOID;
341bbc2815cSJohn Baldwin 
342bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
343bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
344bbc2815cSJohn Baldwin 	return_VOID;
345bbc2815cSJohn Baldwin 
346bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
347bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
348bbc2815cSJohn Baldwin 	return_VOID;
34915e32d5dSMike Smith 
3504e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3514e376d58SNate Lawson 
352be2b1797SNate Lawson     /* Attach the actual ACPI device. */
35315e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
354bee4aa4aSNate Lawson 	device_printf(parent, "device_identify failed\n");
3550ae55423SMike Smith 	return_VOID;
35615e32d5dSMike Smith     }
35715e32d5dSMike Smith }
35815e32d5dSMike Smith 
35915e32d5dSMike Smith /*
360bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
36115e32d5dSMike Smith  */
36215e32d5dSMike Smith static int
36315e32d5dSMike Smith acpi_probe(device_t dev)
36415e32d5dSMike Smith {
36515e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
36615e32d5dSMike Smith     char		buf[20];
36715e32d5dSMike Smith     int			error;
3682ccd1cacSNate Lawson     struct sbuf		sb;
3692ccd1cacSNate Lawson     ACPI_STATUS		status;
37015e32d5dSMike Smith 
371b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
372f9390180SMitsuru IWASAKI 
373f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
374f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
375bee4aa4aSNate Lawson 	device_printf(dev, "probe failed, other PM system enabled.\n");
376f9390180SMitsuru IWASAKI 	return_VALUE (ENXIO);
377f9390180SMitsuru IWASAKI     }
378f9390180SMitsuru IWASAKI 
379b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
380be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
381be2b1797SNate Lawson 		      AcpiFormatException(status));
382cb9b0d80SMike Smith 	error = ENXIO;
383cb9b0d80SMike Smith     } else {
3842ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3852ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3862ccd1cacSNate Lawson 	sbuf_trim(&sb);
3872ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3882ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3892ccd1cacSNate Lawson 	sbuf_trim(&sb);
3902ccd1cacSNate Lawson 	sbuf_finish(&sb);
3912ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3922ccd1cacSNate Lawson 	sbuf_delete(&sb);
393cb9b0d80SMike Smith 	error = 0;
394cb9b0d80SMike Smith     }
395bee4aa4aSNate Lawson 
396cb9b0d80SMike Smith     return_VALUE (error);
39715e32d5dSMike Smith }
39815e32d5dSMike Smith 
39915e32d5dSMike Smith static int
40015e32d5dSMike Smith acpi_attach(device_t dev)
40115e32d5dSMike Smith {
40215e32d5dSMike Smith     struct acpi_softc	*sc;
403cb9b0d80SMike Smith     ACPI_STATUS		status;
404ea27c63eSNate Lawson     int			error, state;
40532d18aa5SMike Smith     UINT32		flags;
406ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
407498d464fSMitsuru IWASAKI     char		*env;
40815e32d5dSMike Smith 
409b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
410bee4aa4aSNate Lawson 
41115e32d5dSMike Smith     sc = device_get_softc(dev);
41215e32d5dSMike Smith     sc->acpi_dev = dev;
41315e32d5dSMike Smith 
41491233413SNate Lawson     /* Initialize resource manager. */
41591233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
41691233413SNate Lawson     acpi_rman_io.rm_start = 0;
41791233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
4180bba6acfSJohn Baldwin     acpi_rman_io.rm_descr = "ACPI I/O ports";
41991233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
42091233413SNate Lawson 	panic("acpi rman_init IO ports failed");
42191233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
42291233413SNate Lawson     acpi_rman_mem.rm_start = 0;
42391233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
4240bba6acfSJohn Baldwin     acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
42591233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
42691233413SNate Lawson 	panic("acpi rman_init memory failed");
42791233413SNate Lawson 
428be2b1797SNate Lawson     /* Install the default address space handlers. */
429cb9b0d80SMike Smith     error = ENXIO;
430be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
431be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
432be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
433be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
434be2b1797SNate Lawson 		      AcpiFormatException(status));
435cb9b0d80SMike Smith 	goto out;
43615e32d5dSMike Smith     }
437be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
438be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
439be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
440be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
441be2b1797SNate Lawson 		      AcpiFormatException(status));
442cb9b0d80SMike Smith 	goto out;
44315e32d5dSMike Smith     }
444be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
445be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
446be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
447be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
448be2b1797SNate Lawson 		      AcpiFormatException(status));
449cb9b0d80SMike Smith 	goto out;
45015e32d5dSMike Smith     }
45115e32d5dSMike Smith 
45215e32d5dSMike Smith     /*
453be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
454be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
455be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
456be2b1797SNate Lawson      * object init pass.
45715e32d5dSMike Smith      *
45832d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
45932d18aa5SMike Smith      *
460be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
461be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
46215e32d5dSMike Smith      */
46332d18aa5SMike Smith     flags = 0;
464d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
46532d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
466bee4aa4aSNate Lawson 
467bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
468b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
469be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
470be2b1797SNate Lawson 		      AcpiFormatException(status));
471cb9b0d80SMike Smith 	goto out;
47215e32d5dSMike Smith     }
47315e32d5dSMike Smith 
474f8335e3aSNate Lawson     /*
475f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
476f8335e3aSNate Lawson      * the namespace has been evaluated.
477da72d149SNate Lawson      *
478da72d149SNate Lawson      * XXX This happens before the sysresource devices have been probed and
479da72d149SNate Lawson      * attached so its resources come from nexus0.  In practice, this isn't
480da72d149SNate Lawson      * a problem but should be addressed eventually.
481f8335e3aSNate Lawson      */
482f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
483f8335e3aSNate Lawson 
484bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
485b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
486be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
487be2b1797SNate Lawson 		      AcpiFormatException(status));
488b69ed3f4SMitsuru IWASAKI 	goto out;
489b69ed3f4SMitsuru IWASAKI     }
490b69ed3f4SMitsuru IWASAKI 
49115e32d5dSMike Smith     /*
4921d073b1dSJohn Baldwin      * Setup our sysctl tree.
4931d073b1dSJohn Baldwin      *
4941d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4951d073b1dSJohn Baldwin      */
4961d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4971d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4981d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4991d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
5001d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
501d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
502d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
503d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5041d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5051d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5061d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5071d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5081d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5091d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5101d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
5111d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
512f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
513f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
514f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
515f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
516f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
517f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5182d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
519197b4dccSNate Lawson 	OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
520197b4dccSNate Lawson 	"sleep delay");
521ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
522197b4dccSNate Lawson 	OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
5231611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
524197b4dccSNate Lawson 	OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
525d85e6785SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
526d85e6785SNate Lawson 	OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
527d85e6785SNate Lawson 	&sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
528d1b16e18SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
529d1b16e18SNate Lawson 	OID_AUTO, "handle_reboot", CTLFLAG_RW,
530d1b16e18SNate Lawson 	&sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
531bf0c18ecSNate Lawson 
532bf0c18ecSNate Lawson     /*
5332b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
534bf0c18ecSNate Lawson      * stabilize.
535bf0c18ecSNate Lawson      */
5362b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
5372d644607SMitsuru IWASAKI     if (bootverbose)
5382d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
539965a34fbSNate Lawson     if ((env = getenv("hw.acpi.verbose")) != NULL) {
540965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
541498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
542498d464fSMitsuru IWASAKI 	freeenv(env);
543498d464fSMitsuru IWASAKI     }
5441d073b1dSJohn Baldwin 
5452d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5462d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5472d610c46SNate Lawson 	sc->acpi_s4bios = 1;
5482d610c46SNate Lawson 
5491d073b1dSJohn Baldwin     /*
550ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
551ea27c63eSNate Lawson      * to NONE by default to avoid surprising users.
55215e32d5dSMike Smith      */
553ea27c63eSNate Lawson     sc->acpi_power_button_sx = ACPI_STATE_S5;
554ea27c63eSNate Lawson     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
555f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
556f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
55715e32d5dSMike Smith 
558ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
559ea27c63eSNate Lawson     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
560ea27c63eSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
561ea27c63eSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
562ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
563ea27c63eSNate Lawson 	    break;
564ea27c63eSNate Lawson 	}
565ea27c63eSNate Lawson 
56613d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
56715e32d5dSMike Smith 
56815e32d5dSMike Smith     /*
56915e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
57015e32d5dSMike Smith      */
57115e32d5dSMike Smith 
57259e472e9SNate Lawson     /* Register our shutdown handler. */
573be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
574be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
57515e32d5dSMike Smith 
57615e32d5dSMike Smith     /*
57715e32d5dSMike Smith      * Register our acpi event handlers.
57815e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
57915e32d5dSMike Smith      */
580be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
581be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
582be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
583be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
58415e32d5dSMike Smith 
585be2b1797SNate Lawson     /* Flag our initial states. */
58615e32d5dSMike Smith     sc->acpi_enabled = 1;
58715e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
588ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
58915e32d5dSMike Smith 
590be2b1797SNate Lawson     /* Create the control device */
591a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5922a4689e8SRobert Watson 			      "acpi");
59315e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
59415e32d5dSMike Smith 
595be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
596f86214b6SMitsuru IWASAKI 	goto out;
597f86214b6SMitsuru IWASAKI 
598f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
599f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
600f9390180SMitsuru IWASAKI 
601eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
602eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
603eeb6dba2SJohn Baldwin 
604cb9b0d80SMike Smith     error = 0;
605cb9b0d80SMike Smith 
606cb9b0d80SMike Smith  out:
607cb9b0d80SMike Smith     return_VALUE (error);
60815e32d5dSMike Smith }
60915e32d5dSMike Smith 
610169b539aSNate Lawson static int
61110ce62b9SNate Lawson acpi_suspend(device_t dev)
61210ce62b9SNate Lawson {
61310ce62b9SNate Lawson     device_t child, *devlist;
61410ce62b9SNate Lawson     int error, i, numdevs, pstate;
61510ce62b9SNate Lawson 
6165d3d03f1SNate Lawson     GIANT_REQUIRED;
6175d3d03f1SNate Lawson 
61810ce62b9SNate Lawson     /* First give child devices a chance to suspend. */
61910ce62b9SNate Lawson     error = bus_generic_suspend(dev);
62010ce62b9SNate Lawson     if (error)
62110ce62b9SNate Lawson 	return (error);
62210ce62b9SNate Lawson 
62310ce62b9SNate Lawson     /*
62410ce62b9SNate Lawson      * Now, set them into the appropriate power state, usually D3.  If the
62510ce62b9SNate Lawson      * device has an _SxD method for the next sleep state, use that power
62610ce62b9SNate Lawson      * state instead.
62710ce62b9SNate Lawson      */
62810ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
62910ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
63010ce62b9SNate Lawson 	/* If the device is not attached, we've powered it down elsewhere. */
63110ce62b9SNate Lawson 	child = devlist[i];
63210ce62b9SNate Lawson 	if (!device_is_attached(child))
63310ce62b9SNate Lawson 	    continue;
63410ce62b9SNate Lawson 
63510ce62b9SNate Lawson 	/*
63610ce62b9SNate Lawson 	 * Default to D3 for all sleep states.  The _SxD method is optional
63710ce62b9SNate Lawson 	 * so set the powerstate even if it's absent.
63810ce62b9SNate Lawson 	 */
63910ce62b9SNate Lawson 	pstate = PCI_POWERSTATE_D3;
64010ce62b9SNate Lawson 	error = acpi_device_pwr_for_sleep(device_get_parent(child),
64110ce62b9SNate Lawson 	    child, &pstate);
64210ce62b9SNate Lawson 	if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
64310ce62b9SNate Lawson 	    pci_set_powerstate(child, pstate);
64410ce62b9SNate Lawson     }
64510ce62b9SNate Lawson     free(devlist, M_TEMP);
64610ce62b9SNate Lawson     error = 0;
64710ce62b9SNate Lawson 
64810ce62b9SNate Lawson     return (error);
64910ce62b9SNate Lawson }
65010ce62b9SNate Lawson 
65110ce62b9SNate Lawson static int
65210ce62b9SNate Lawson acpi_resume(device_t dev)
65310ce62b9SNate Lawson {
65410ce62b9SNate Lawson     ACPI_HANDLE handle;
65510ce62b9SNate Lawson     int i, numdevs;
65610ce62b9SNate Lawson     device_t child, *devlist;
65710ce62b9SNate Lawson 
6585d3d03f1SNate Lawson     GIANT_REQUIRED;
6595d3d03f1SNate Lawson 
66010ce62b9SNate Lawson     /*
66110ce62b9SNate Lawson      * Put all devices in D0 before resuming them.  Call _S0D on each one
66210ce62b9SNate Lawson      * since some systems expect this.
66310ce62b9SNate Lawson      */
66410ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
66510ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
66610ce62b9SNate Lawson 	child = devlist[i];
66710ce62b9SNate Lawson 	handle = acpi_get_handle(child);
66810ce62b9SNate Lawson 	if (handle)
66910ce62b9SNate Lawson 	    AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
67010ce62b9SNate Lawson 	if (device_is_attached(child) && acpi_do_powerstate)
67110ce62b9SNate Lawson 	    pci_set_powerstate(child, PCI_POWERSTATE_D0);
67210ce62b9SNate Lawson     }
67310ce62b9SNate Lawson     free(devlist, M_TEMP);
67410ce62b9SNate Lawson 
67510ce62b9SNate Lawson     return (bus_generic_resume(dev));
67610ce62b9SNate Lawson }
67710ce62b9SNate Lawson 
67810ce62b9SNate Lawson static int
679169b539aSNate Lawson acpi_shutdown(device_t dev)
680169b539aSNate Lawson {
681169b539aSNate Lawson 
6825d3d03f1SNate Lawson     GIANT_REQUIRED;
6835d3d03f1SNate Lawson 
6840e414868SNate Lawson     /* Allow children to shutdown first. */
6850e414868SNate Lawson     bus_generic_shutdown(dev);
6860e414868SNate Lawson 
687bee4aa4aSNate Lawson     /*
688bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
689bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
690bee4aa4aSNate Lawson      */
691d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
692d4b9ff91SNate Lawson 
693169b539aSNate Lawson     return (0);
694169b539aSNate Lawson }
695169b539aSNate Lawson 
69615e32d5dSMike Smith /*
69715e32d5dSMike Smith  * Handle a new device being added
69815e32d5dSMike Smith  */
69915e32d5dSMike Smith static device_t
70015e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
70115e32d5dSMike Smith {
70215e32d5dSMike Smith     struct acpi_device	*ad;
70315e32d5dSMike Smith     device_t		child;
70415e32d5dSMike Smith 
705be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
70615e32d5dSMike Smith 	return (NULL);
70715e32d5dSMike Smith 
70815e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
70915e32d5dSMike Smith 
71015e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
71115e32d5dSMike Smith     if (child != NULL)
71215e32d5dSMike Smith 	device_set_ivars(child, ad);
71343ce1c77SNate Lawson     else
71443ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
71515e32d5dSMike Smith     return (child);
71615e32d5dSMike Smith }
71715e32d5dSMike Smith 
71815e32d5dSMike Smith static int
71915e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
72015e32d5dSMike Smith {
72115e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
72215e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
72315e32d5dSMike Smith     int retval = 0;
72415e32d5dSMike Smith 
72515e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
7269ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
7279ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
7289ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
7299ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
730a91c5fa8SNate Lawson     if (device_get_flags(child))
731a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
7322c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
73315e32d5dSMike Smith 
73415e32d5dSMike Smith     return (retval);
73515e32d5dSMike Smith }
73615e32d5dSMike Smith 
73710ce62b9SNate Lawson /*
73810ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
73910ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
74010ce62b9SNate Lawson  *
74110ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
74210ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
74310ce62b9SNate Lawson  * them to be powered up.
74410ce62b9SNate Lawson  */
74510ce62b9SNate Lawson static void
74610ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
74710ce62b9SNate Lawson {
74810ce62b9SNate Lawson 
74910ce62b9SNate Lawson     /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
75010ce62b9SNate Lawson }
75110ce62b9SNate Lawson 
75210ce62b9SNate Lawson /*
75310ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
75410ce62b9SNate Lawson  *
75510ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
75610ce62b9SNate Lawson  */
75710ce62b9SNate Lawson static void
75810ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
75910ce62b9SNate Lawson {
76010ce62b9SNate Lawson     device_t child, *devlist;
76110ce62b9SNate Lawson     int i, numdevs;
76210ce62b9SNate Lawson 
76310ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
76410ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
76510ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
76610ce62b9SNate Lawson 	child = devlist[i];
76710ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
76810ce62b9SNate Lawson 	    /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
76910ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
77010ce62b9SNate Lawson 		; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
77110ce62b9SNate Lawson 	}
77210ce62b9SNate Lawson     }
77310ce62b9SNate Lawson     free(devlist, M_TEMP);
77410ce62b9SNate Lawson }
77510ce62b9SNate Lawson 
77663600cc3SNate Lawson /* Location hint for devctl(8) */
77763600cc3SNate Lawson static int
778247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
779247648afSTakanori Watanabe     size_t buflen)
780247648afSTakanori Watanabe {
781247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
782247648afSTakanori Watanabe 
783247648afSTakanori Watanabe     if (dinfo->ad_handle)
784d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
785247648afSTakanori Watanabe     else
786d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
787247648afSTakanori Watanabe     return (0);
788247648afSTakanori Watanabe }
789247648afSTakanori Watanabe 
79063600cc3SNate Lawson /* PnP information for devctl(8) */
79163600cc3SNate Lawson static int
792247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
793247648afSTakanori Watanabe     size_t buflen)
794247648afSTakanori Watanabe {
795247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
79663600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
79763600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
798247648afSTakanori Watanabe     char *end;
799247648afSTakanori Watanabe     int error;
800247648afSTakanori Watanabe 
801247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
802247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
803247648afSTakanori Watanabe     if (error)
804d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
805247648afSTakanori Watanabe     else
806247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
807247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
808d40c0f53SNate Lawson 		 adinfo->HardwareId.Value : "none",
80963600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
81063600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
811247648afSTakanori Watanabe     if (adinfo)
812247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
813247648afSTakanori Watanabe 
814247648afSTakanori Watanabe     return (0);
815247648afSTakanori Watanabe }
816247648afSTakanori Watanabe 
817247648afSTakanori Watanabe /*
81815e32d5dSMike Smith  * Handle per-device ivars
81915e32d5dSMike Smith  */
82015e32d5dSMike Smith static int
82115e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
82215e32d5dSMike Smith {
82315e32d5dSMike Smith     struct acpi_device	*ad;
82415e32d5dSMike Smith 
82515e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
82615e32d5dSMike Smith 	printf("device has no ivars\n");
82715e32d5dSMike Smith 	return (ENOENT);
82815e32d5dSMike Smith     }
82915e32d5dSMike Smith 
830be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
83115e32d5dSMike Smith     switch(index) {
83215e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
83315e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
83415e32d5dSMike Smith 	break;
83515e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
83615e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
83715e32d5dSMike Smith 	break;
83815e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
83915e32d5dSMike Smith 	*(void **)result = ad->ad_private;
84015e32d5dSMike Smith 	break;
841d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
842d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
843d4b9ff91SNate Lawson 	break;
84432d18aa5SMike Smith     case ISA_IVAR_VENDORID:
84532d18aa5SMike Smith     case ISA_IVAR_SERIAL:
84632d18aa5SMike Smith     case ISA_IVAR_COMPATID:
84732d18aa5SMike Smith 	*(int *)result = -1;
84832d18aa5SMike Smith 	break;
84932d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
85032d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
85132d18aa5SMike Smith 	break;
85215e32d5dSMike Smith     default:
85315e32d5dSMike Smith 	return (ENOENT);
85415e32d5dSMike Smith     }
855be2b1797SNate Lawson 
85615e32d5dSMike Smith     return (0);
85715e32d5dSMike Smith }
85815e32d5dSMike Smith 
85915e32d5dSMike Smith static int
86015e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
86115e32d5dSMike Smith {
86215e32d5dSMike Smith     struct acpi_device	*ad;
86315e32d5dSMike Smith 
86415e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
86515e32d5dSMike Smith 	printf("device has no ivars\n");
86615e32d5dSMike Smith 	return (ENOENT);
86715e32d5dSMike Smith     }
86815e32d5dSMike Smith 
86915e32d5dSMike Smith     switch(index) {
87015e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
87115e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
87215e32d5dSMike Smith 	break;
87315e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
87415e32d5dSMike Smith 	ad->ad_magic = (int)value;
87515e32d5dSMike Smith 	break;
87615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
87715e32d5dSMike Smith 	ad->ad_private = (void *)value;
87815e32d5dSMike Smith 	break;
879d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
880d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
881d4b9ff91SNate Lawson 	break;
88215e32d5dSMike Smith     default:
8832ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
88415e32d5dSMike Smith 	return (ENOENT);
88515e32d5dSMike Smith     }
886be2b1797SNate Lawson 
88715e32d5dSMike Smith     return (0);
88815e32d5dSMike Smith }
88915e32d5dSMike Smith 
89015e32d5dSMike Smith /*
89115e32d5dSMike Smith  * Handle child resource allocation/removal
89215e32d5dSMike Smith  */
89391233413SNate Lawson static struct resource_list *
89491233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
89515e32d5dSMike Smith {
89691233413SNate Lawson     struct acpi_device		*ad;
89715e32d5dSMike Smith 
89891233413SNate Lawson     ad = device_get_ivars(child);
89991233413SNate Lawson     return (&ad->ad_rl);
90015e32d5dSMike Smith }
90115e32d5dSMike Smith 
902adad4744SNate Lawson /*
903adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
904adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
905adad4744SNate Lawson  */
906adad4744SNate Lawson static int
907adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
908adad4744SNate Lawson {
909adad4744SNate Lawson     struct resource *res;
910adad4744SNate Lawson     struct resource_list *rl;
911adad4744SNate Lawson     struct resource_list_entry *rle;
912adad4744SNate Lawson     struct rman *rm;
913da72d149SNate Lawson     char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
914da72d149SNate Lawson     device_t *children;
915da72d149SNate Lawson     int child_count, i;
916da72d149SNate Lawson 
917da72d149SNate Lawson     /*
918da72d149SNate Lawson      * Probe/attach any sysresource devices.  This would be unnecessary if we
919da72d149SNate Lawson      * had multi-pass probe/attach.
920da72d149SNate Lawson      */
921da72d149SNate Lawson     if (device_get_children(dev, &children, &child_count) != 0)
922da72d149SNate Lawson 	return (ENXIO);
923da72d149SNate Lawson     for (i = 0; i < child_count; i++) {
924da72d149SNate Lawson 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
925da72d149SNate Lawson 	    device_probe_and_attach(children[i]);
926da72d149SNate Lawson     }
927da72d149SNate Lawson     free(children, M_TEMP);
928adad4744SNate Lawson 
929adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
930be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
931adad4744SNate Lawson 	if (rle->res != NULL) {
932adad4744SNate Lawson 	    device_printf(dev, "duplicate resource for %lx\n", rle->start);
933adad4744SNate Lawson 	    continue;
934adad4744SNate Lawson 	}
935adad4744SNate Lawson 
936adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
937adad4744SNate Lawson 	switch (rle->type) {
938adad4744SNate Lawson 	case SYS_RES_IOPORT:
939adad4744SNate Lawson 	    rm = &acpi_rman_io;
940adad4744SNate Lawson 	    break;
941adad4744SNate Lawson 	case SYS_RES_MEMORY:
942adad4744SNate Lawson 	    rm = &acpi_rman_mem;
943adad4744SNate Lawson 	    break;
944adad4744SNate Lawson 	default:
945adad4744SNate Lawson 	    continue;
946adad4744SNate Lawson 	}
947adad4744SNate Lawson 
948adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
949adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
950adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
951adad4744SNate Lawson 	if (res != NULL) {
952adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
953adad4744SNate Lawson 	    rle->res = res;
954adad4744SNate Lawson 	} else
955adad4744SNate Lawson 	    device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
956adad4744SNate Lawson 		rle->start, rle->count, rle->type);
957adad4744SNate Lawson     }
958adad4744SNate Lawson     return (0);
959adad4744SNate Lawson }
960adad4744SNate Lawson 
961adad4744SNate Lawson /* Find if we manage a given resource. */
962adad4744SNate Lawson static struct resource_list_entry *
963adad4744SNate Lawson acpi_sysres_find(device_t dev, int type, u_long addr)
964adad4744SNate Lawson {
965adad4744SNate Lawson     struct resource_list *rl;
966adad4744SNate Lawson     struct resource_list_entry *rle;
967adad4744SNate Lawson 
968adad4744SNate Lawson     ACPI_SERIAL_ASSERT(acpi);
969adad4744SNate Lawson 
970adad4744SNate Lawson     /* We only consider IO and memory resources for our pool. */
971adad4744SNate Lawson     rle = NULL;
972adad4744SNate Lawson     if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY)
973adad4744SNate Lawson 	goto out;
974adad4744SNate Lawson 
975adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
976be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
977adad4744SNate Lawson 	if (type == rle->type && addr >= rle->start &&
978adad4744SNate Lawson 	    addr < rle->start + rle->count)
979adad4744SNate Lawson 	    break;
980adad4744SNate Lawson     }
981adad4744SNate Lawson 
982adad4744SNate Lawson out:
983adad4744SNate Lawson     return (rle);
984adad4744SNate Lawson }
985adad4744SNate Lawson 
98615e32d5dSMike Smith static struct resource *
98715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
98815e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
98915e32d5dSMike Smith {
99095957f62SJohn Baldwin     ACPI_RESOURCE ares;
99115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
99215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
99391233413SNate Lawson     struct resource_list_entry *rle;
99491233413SNate Lawson     struct resource *res;
99591233413SNate Lawson     struct rman *rm;
99615e32d5dSMike Smith 
99715e2f34fSNate Lawson     res = NULL;
99815e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
99915e2f34fSNate Lawson 
100091233413SNate Lawson     /*
100191233413SNate Lawson      * If this is an allocation of the "default" range for a given RID, and
100291233413SNate Lawson      * we know what the resources for this device are (i.e., they're on the
100391233413SNate Lawson      * child's resource list), use those start/end values.
100491233413SNate Lawson      */
1005f09aa88cSWarner Losh     if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) {
100691233413SNate Lawson 	rle = resource_list_find(rl, type, *rid);
100791233413SNate Lawson 	if (rle == NULL)
100815e2f34fSNate Lawson 	    goto out;
100991233413SNate Lawson 	start = rle->start;
101091233413SNate Lawson 	end = rle->end;
101191233413SNate Lawson 	count = rle->count;
101291233413SNate Lawson     }
101391233413SNate Lawson 
101491233413SNate Lawson     /* If we don't manage this address, pass the request up to the parent. */
1015adad4744SNate Lawson     rle = acpi_sysres_find(bus, type, start);
101691233413SNate Lawson     if (rle == NULL) {
101795957f62SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
101895957f62SJohn Baldwin 	    start, end, count, flags);
101995957f62SJohn Baldwin     } else {
102091233413SNate Lawson 
102191233413SNate Lawson 	/* We only handle memory and IO resources through rman. */
102291233413SNate Lawson 	switch (type) {
102391233413SNate Lawson 	case SYS_RES_IOPORT:
102491233413SNate Lawson 	    rm = &acpi_rman_io;
102591233413SNate Lawson 	    break;
102691233413SNate Lawson 	case SYS_RES_MEMORY:
102791233413SNate Lawson 	    rm = &acpi_rman_mem;
102891233413SNate Lawson 	    break;
102991233413SNate Lawson 	default:
103091233413SNate Lawson 	    panic("acpi_alloc_resource: invalid res type %d", type);
103191233413SNate Lawson 	}
103291233413SNate Lawson 
103391233413SNate Lawson 	/* If we do know it, allocate it from the local pool. */
103495957f62SJohn Baldwin 	res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
103595957f62SJohn Baldwin 	    child);
103691233413SNate Lawson 	if (res == NULL)
103715e2f34fSNate Lawson 	    goto out;
103891233413SNate Lawson 
1039a3236570SWarner Losh 	/* Copy the bus tag and handle from the pre-allocated resource. */
1040c3f861f4SWarner Losh 	rman_set_rid(res, *rid);
104191233413SNate Lawson 	rman_set_bustag(res, rman_get_bustag(rle->res));
1042ed67d9deSWarner Losh 	rman_set_bushandle(res, rman_get_start(res));
104391233413SNate Lawson 
104491233413SNate Lawson 	/* If requested, activate the resource using the parent's method. */
104595957f62SJohn Baldwin 	if (flags & RF_ACTIVE)
104691233413SNate Lawson 	    if (bus_activate_resource(child, type, *rid, res) != 0) {
104791233413SNate Lawson 		rman_release_resource(res);
104815e2f34fSNate Lawson 		res = NULL;
104915e2f34fSNate Lawson 		goto out;
105091233413SNate Lawson 	    }
105195957f62SJohn Baldwin     }
105291233413SNate Lawson 
105395957f62SJohn Baldwin     if (res != NULL && device_get_parent(child) == bus)
105495957f62SJohn Baldwin 	switch (type) {
105595957f62SJohn Baldwin 	case SYS_RES_IRQ:
105695957f62SJohn Baldwin 	    /*
105795957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
105895957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
105995957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
106095957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
106195957f62SJohn Baldwin 	     *
106295957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
106395957f62SJohn Baldwin 	     */
106495957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
106595957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
106695957f62SJohn Baldwin 	    break;
106795957f62SJohn Baldwin 	}
106815e2f34fSNate Lawson 
106915e2f34fSNate Lawson out:
107015e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
107191233413SNate Lawson     return (res);
107215e32d5dSMike Smith }
107315e32d5dSMike Smith 
107415e32d5dSMike Smith static int
107591233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid,
107691233413SNate Lawson     struct resource *r)
107715e32d5dSMike Smith {
107891233413SNate Lawson     int ret;
107915e32d5dSMike Smith 
108015e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
108115e2f34fSNate Lawson 
108291233413SNate Lawson     /*
108391233413SNate Lawson      * If we know about this address, deactivate it and release it to the
108491233413SNate Lawson      * local pool.  If we don't, pass this request up to the parent.
108591233413SNate Lawson      */
1086adad4744SNate Lawson     if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) {
108791233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
108891233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
108991233413SNate Lawson 	    if (ret != 0)
109015e2f34fSNate Lawson 		goto out;
109191233413SNate Lawson 	}
109291233413SNate Lawson 	ret = rman_release_resource(r);
109391233413SNate Lawson     } else
109491233413SNate Lawson 	ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
109591233413SNate Lawson 
109615e2f34fSNate Lawson out:
109715e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
109891233413SNate Lawson     return (ret);
109915e32d5dSMike Smith }
110015e32d5dSMike Smith 
11018b28b622SNate Lawson static void
11028b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
11038b28b622SNate Lawson {
11048b28b622SNate Lawson     struct resource_list *rl;
11058b28b622SNate Lawson 
11068b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
11078b28b622SNate Lawson     resource_list_delete(rl, type, rid);
11088b28b622SNate Lawson }
11098b28b622SNate Lawson 
1110dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1111e1c4bf3fSNate Lawson int
1112e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1113e1c4bf3fSNate Lawson     struct resource **res)
1114dc750869SNate Lawson {
1115e1c4bf3fSNate Lawson     int error, res_type;
1116dc750869SNate Lawson 
1117e1c4bf3fSNate Lawson     error = ENOMEM;
11188f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1119e1c4bf3fSNate Lawson 	return (EINVAL);
1120dc750869SNate Lawson 
11218f118e25SNate Lawson     /* We only support memory and IO spaces. */
1122dc750869SNate Lawson     switch (gas->AddressSpaceId) {
1123dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1124e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1125dc750869SNate Lawson 	break;
1126dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1127e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1128dc750869SNate Lawson 	break;
1129dc750869SNate Lawson     default:
1130e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1131dc750869SNate Lawson     }
1132dc750869SNate Lawson 
11332fe912dfSNate Lawson     /*
1134f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1135f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
11362fe912dfSNate Lawson      */
1137f45fc848SNate Lawson     if (gas->RegisterBitWidth && gas->RegisterBitWidth < 8)
1138f45fc848SNate Lawson 	gas->RegisterBitWidth = 8;
11392fe912dfSNate Lawson 
11408f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
1141f45fc848SNate Lawson     if (!ACPI_VALID_ADDRESS(gas->Address) || gas->RegisterBitWidth == 0)
11428f118e25SNate Lawson 	return (EINVAL);
11438f118e25SNate Lawson 
1144e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
1145e1c4bf3fSNate Lawson 	gas->RegisterBitWidth / 8);
1146e1c4bf3fSNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE);
1147e1c4bf3fSNate Lawson     if (*res != NULL) {
1148e1c4bf3fSNate Lawson 	*type = res_type;
1149e1c4bf3fSNate Lawson 	error = 0;
1150ca2c69c8SNate Lawson     } else
1151ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1152ca2c69c8SNate Lawson 
1153e1c4bf3fSNate Lawson     return (error);
1154dc750869SNate Lawson }
1155dc750869SNate Lawson 
1156c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
11571e4925e8SNate Lawson static uint32_t
115832d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1159bc0f2195SMike Smith {
11601e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11611e4925e8SNate Lawson     ACPI_BUFFER		buf;
1162bc0f2195SMike Smith     ACPI_HANDLE		h;
1163bc0f2195SMike Smith     ACPI_STATUS		error;
1164bc0f2195SMike Smith     u_int32_t		pnpid;
116532d18aa5SMike Smith 
1166b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
116732d18aa5SMike Smith 
116832d18aa5SMike Smith     pnpid = 0;
1169bd189fe7SNate Lawson     buf.Pointer = NULL;
1170bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1171bd189fe7SNate Lawson 
11726fca9360SNate Lawson     /* Fetch and validate the HID. */
117332d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1174bd189fe7SNate Lawson 	goto out;
11756fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11766fca9360SNate Lawson     if (ACPI_FAILURE(error))
1177bd189fe7SNate Lawson 	goto out;
11781e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
117932d18aa5SMike Smith 
11801e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
11811e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1182be2b1797SNate Lawson 
1183bd189fe7SNate Lawson out:
1184bd189fe7SNate Lawson     if (buf.Pointer != NULL)
11851e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
118632d18aa5SMike Smith     return_VALUE (pnpid);
118732d18aa5SMike Smith }
118832d18aa5SMike Smith 
11891e4925e8SNate Lawson static int
11901e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1191b2566207STakanori Watanabe {
11921e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11931e4925e8SNate Lawson     ACPI_BUFFER		buf;
1194b2566207STakanori Watanabe     ACPI_HANDLE		h;
1195b2566207STakanori Watanabe     ACPI_STATUS		error;
11961e4925e8SNate Lawson     uint32_t		*pnpid;
11971e4925e8SNate Lawson     int			valid, i;
1198b2566207STakanori Watanabe 
1199b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1200b2566207STakanori Watanabe 
12011e4925e8SNate Lawson     pnpid = cids;
12021e4925e8SNate Lawson     valid = 0;
1203bd189fe7SNate Lawson     buf.Pointer = NULL;
1204bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1205bd189fe7SNate Lawson 
12061e4925e8SNate Lawson     /* Fetch and validate the CID */
1207b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
1208bd189fe7SNate Lawson 	goto out;
12091e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12101e4925e8SNate Lawson     if (ACPI_FAILURE(error))
1211bd189fe7SNate Lawson 	goto out;
12121e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
12131e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1214b2566207STakanori Watanabe 	goto out;
1215b2566207STakanori Watanabe 
12161e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
12171e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
12181e4925e8SNate Lawson     for (i = 0; i < count; i++) {
12191e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
12201e4925e8SNate Lawson 	    continue;
12211e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
12221e4925e8SNate Lawson 	valid++;
1223b2566207STakanori Watanabe     }
1224b2566207STakanori Watanabe 
12251e4925e8SNate Lawson out:
1226bd189fe7SNate Lawson     if (buf.Pointer != NULL)
12271e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
12281e4925e8SNate Lawson     return_VALUE (valid);
12291e4925e8SNate Lawson }
1230b2566207STakanori Watanabe 
1231ce619b2eSNate Lawson static char *
1232ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1233ce619b2eSNate Lawson {
1234ce619b2eSNate Lawson     ACPI_HANDLE h;
1235ce619b2eSNate Lawson     int i;
1236ce619b2eSNate Lawson 
1237ce619b2eSNate Lawson     h = acpi_get_handle(dev);
1238ce619b2eSNate Lawson     if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
1239ce619b2eSNate Lawson 	return (NULL);
1240ce619b2eSNate Lawson 
1241ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1242ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1243ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1244ce619b2eSNate Lawson 	    return (ids[i]);
1245ce619b2eSNate Lawson     }
1246ce619b2eSNate Lawson     return (NULL);
1247ce619b2eSNate Lawson }
1248ce619b2eSNate Lawson 
1249ce619b2eSNate Lawson static ACPI_STATUS
1250ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1251ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1252ce619b2eSNate Lawson {
1253ce619b2eSNate Lawson     ACPI_HANDLE h;
1254ce619b2eSNate Lawson 
1255df8d2a32SNate Lawson     if (dev == NULL)
1256df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1257df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1258ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1259ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1260ce619b2eSNate Lawson }
1261ce619b2eSNate Lawson 
126210ce62b9SNate Lawson static int
126310ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
126410ce62b9SNate Lawson {
126510ce62b9SNate Lawson     struct acpi_softc *sc;
126610ce62b9SNate Lawson     ACPI_HANDLE handle;
126710ce62b9SNate Lawson     ACPI_STATUS status;
126810ce62b9SNate Lawson     char sxd[8];
126910ce62b9SNate Lawson     int error;
127010ce62b9SNate Lawson 
127110ce62b9SNate Lawson     sc = device_get_softc(bus);
127210ce62b9SNate Lawson     handle = acpi_get_handle(dev);
127310ce62b9SNate Lawson 
127410ce62b9SNate Lawson     /*
127510ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
127610ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
127710ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
127810ce62b9SNate Lawson      * need special handling in their drivers.
127910ce62b9SNate Lawson      */
128010ce62b9SNate Lawson     if (handle == NULL ||
128110ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
128210ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
128310ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
128410ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
128510ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
128610ce62b9SNate Lawson 	return (ENXIO);
128710ce62b9SNate Lawson 
128810ce62b9SNate Lawson     /*
128910ce62b9SNate Lawson      * Override next state with the value from _SxD, if present.  If no
129010ce62b9SNate Lawson      * dstate argument was provided, don't fetch the return value.
129110ce62b9SNate Lawson      */
129210ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
129310ce62b9SNate Lawson     if (dstate)
129410ce62b9SNate Lawson 	status = acpi_GetInteger(handle, sxd, dstate);
129510ce62b9SNate Lawson     else
129610ce62b9SNate Lawson 	status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
129710ce62b9SNate Lawson 
129810ce62b9SNate Lawson     switch (status) {
129910ce62b9SNate Lawson     case AE_OK:
130010ce62b9SNate Lawson 	error = 0;
130110ce62b9SNate Lawson 	break;
130210ce62b9SNate Lawson     case AE_NOT_FOUND:
130310ce62b9SNate Lawson 	error = ESRCH;
130410ce62b9SNate Lawson 	break;
130510ce62b9SNate Lawson     default:
130610ce62b9SNate Lawson 	error = ENXIO;
130710ce62b9SNate Lawson 	break;
130810ce62b9SNate Lawson     }
130910ce62b9SNate Lawson 
131010ce62b9SNate Lawson     return (error);
131110ce62b9SNate Lawson }
131210ce62b9SNate Lawson 
1313df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1314df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1315df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1316df8d2a32SNate Lawson     void		*arg;
1317df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1318df8d2a32SNate Lawson };
1319df8d2a32SNate Lawson 
1320ce619b2eSNate Lawson static ACPI_STATUS
1321df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1322df8d2a32SNate Lawson {
1323df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1324df8d2a32SNate Lawson     device_t dev, old_dev;
1325df8d2a32SNate Lawson     ACPI_STATUS status;
1326df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1327df8d2a32SNate Lawson 
1328df8d2a32SNate Lawson     /*
1329df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1330df8d2a32SNate Lawson      * the parent where the scan began.
1331df8d2a32SNate Lawson      */
1332df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1333df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1334df8d2a32SNate Lawson 	return (AE_OK);
1335df8d2a32SNate Lawson 
1336df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1337df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1338df8d2a32SNate Lawson 	return (AE_OK);
1339df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1340df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1341df8d2a32SNate Lawson 	return (AE_OK);
1342df8d2a32SNate Lawson 
1343df8d2a32SNate Lawson     /*
1344df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1345df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1346df8d2a32SNate Lawson      */
1347df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1348df8d2a32SNate Lawson     dev = old_dev;
1349df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1350df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1351df8d2a32SNate Lawson 	return (status);
1352df8d2a32SNate Lawson 
1353df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1354df8d2a32SNate Lawson     if (old_dev != NULL) {
1355df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1356df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1357df8d2a32SNate Lawson     }
1358df8d2a32SNate Lawson 
1359df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1360df8d2a32SNate Lawson     if (dev != NULL)
1361df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1362df8d2a32SNate Lawson 
1363df8d2a32SNate Lawson     return (AE_OK);
1364df8d2a32SNate Lawson }
1365df8d2a32SNate Lawson 
1366df8d2a32SNate Lawson static ACPI_STATUS
1367df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1368df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1369ce619b2eSNate Lawson {
1370ce619b2eSNate Lawson     ACPI_HANDLE h;
1371df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1372ce619b2eSNate Lawson 
1373df8d2a32SNate Lawson     if (acpi_disabled("children"))
1374df8d2a32SNate Lawson 	return (AE_OK);
1375df8d2a32SNate Lawson 
1376df8d2a32SNate Lawson     if (dev == NULL)
1377df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1378df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1379ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1380df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1381df8d2a32SNate Lawson     ctx.arg = arg;
1382df8d2a32SNate Lawson     ctx.parent = h;
1383df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1384df8d2a32SNate Lawson 	acpi_device_scan_cb, &ctx, NULL));
1385ce619b2eSNate Lawson }
1386ce619b2eSNate Lawson 
138710ce62b9SNate Lawson /*
138810ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
138910ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
139010ce62b9SNate Lawson  */
139110ce62b9SNate Lawson static int
139210ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state)
139310ce62b9SNate Lawson {
139410ce62b9SNate Lawson     ACPI_HANDLE h;
139510ce62b9SNate Lawson     ACPI_STATUS status;
139610ce62b9SNate Lawson     int error;
139710ce62b9SNate Lawson 
139810ce62b9SNate Lawson     error = 0;
139910ce62b9SNate Lawson     h = acpi_get_handle(child);
140010ce62b9SNate Lawson     if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
140110ce62b9SNate Lawson 	return (EINVAL);
140210ce62b9SNate Lawson     if (h == NULL)
140310ce62b9SNate Lawson 	return (0);
140410ce62b9SNate Lawson 
140510ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
140610ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
140710ce62b9SNate Lawson     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
140810ce62b9SNate Lawson 	&& status != AE_BAD_PARAMETER)
140910ce62b9SNate Lawson 	device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
141010ce62b9SNate Lawson 	    state, acpi_name(h), AcpiFormatException(status));
141110ce62b9SNate Lawson 
141210ce62b9SNate Lawson     return (error);
141310ce62b9SNate Lawson }
141410ce62b9SNate Lawson 
141532d18aa5SMike Smith static int
141632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
141732d18aa5SMike Smith {
14181e4925e8SNate Lawson     int			result, cid_count, i;
14191e4925e8SNate Lawson     uint32_t		lid, cids[8];
1420bc0f2195SMike Smith 
1421b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1422bc0f2195SMike Smith 
1423bc0f2195SMike Smith     /*
1424bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1425bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1426bc0f2195SMike Smith      * that to happen, so don't ever return it.
1427bc0f2195SMike Smith      */
1428bc0f2195SMike Smith     result = ENXIO;
1429bc0f2195SMike Smith 
1430be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1431b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
14321e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1433bc0f2195SMike Smith     while (ids && ids->ip_id) {
14341e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1435bc0f2195SMike Smith 	    result = 0;
1436bc0f2195SMike Smith 	    goto out;
1437bc0f2195SMike Smith 	}
14381e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
14391e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
14401e4925e8SNate Lawson 		result = 0;
14411e4925e8SNate Lawson 		goto out;
14421e4925e8SNate Lawson 	    }
14431e4925e8SNate Lawson 	}
1444bc0f2195SMike Smith 	ids++;
1445bc0f2195SMike Smith     }
1446be2b1797SNate Lawson 
1447bc0f2195SMike Smith  out:
14489e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
14499e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
14509e0dd54fSNate Lawson 
1451bc0f2195SMike Smith     return_VALUE (result);
1452bc0f2195SMike Smith }
1453bc0f2195SMike Smith 
1454bc0f2195SMike Smith /*
145533332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
145615e32d5dSMike Smith  *
145733332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
145833332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
145933332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
146033332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
146133332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
146215e32d5dSMike Smith  */
146315e32d5dSMike Smith static void
146415e32d5dSMike Smith acpi_probe_children(device_t bus)
146515e32d5dSMike Smith {
146615e32d5dSMike Smith 
1467b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14680ae55423SMike Smith 
146915e32d5dSMike Smith     /*
147015e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1471edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
147215e32d5dSMike Smith      *
147315e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1474be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1475be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1476be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
147715e32d5dSMike Smith      */
14784c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
147933332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1480be2b1797SNate Lawson 	bus, NULL);
148115e32d5dSMike Smith 
1482adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1483adad4744SNate Lawson     acpi_sysres_alloc(bus);
1484adad4744SNate Lawson 
1485edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1486edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1487edc13633SNate Lawson     bus_generic_probe(bus);
1488edc13633SNate Lawson 
1489edc13633SNate Lawson     /* Probe/attach all children, created staticly and from the namespace. */
14904c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
149115e32d5dSMike Smith     bus_generic_attach(bus);
149215e32d5dSMike Smith 
149315e32d5dSMike Smith     /*
149415e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
149515e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
149615e32d5dSMike Smith      */
14974c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
149815e32d5dSMike Smith     bus_generic_attach(bus);
14990ae55423SMike Smith 
150088a79fc0SNate Lawson     /* Attach wake sysctls. */
15015c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
150288a79fc0SNate Lawson 
1503bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
15040ae55423SMike Smith     return_VOID;
150515e32d5dSMike Smith }
150615e32d5dSMike Smith 
1507b82ca9a9SNate Lawson /*
1508b82ca9a9SNate Lawson  * Determine the probe order for a given device and return non-zero if it
1509b82ca9a9SNate Lawson  * should be attached immediately.
1510b82ca9a9SNate Lawson  */
151191233413SNate Lawson static int
1512b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
151391233413SNate Lawson {
151491233413SNate Lawson 
1515b82ca9a9SNate Lawson     /*
1516b82ca9a9SNate Lawson      * 1. I/O port and memory system resource holders
1517b82ca9a9SNate Lawson      * 2. Embedded controllers (to handle early accesses)
1518b460c6f8SJohn Baldwin      * 3. PCI Link Devices
1519b82ca9a9SNate Lawson      */
1520da72d149SNate Lawson     if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02"))
152191233413SNate Lawson 	*order = 1;
1522da72d149SNate Lawson     else if (acpi_MatchHid(handle, "PNP0C09"))
152391233413SNate Lawson 	*order = 2;
1524da72d149SNate Lawson     else if (acpi_MatchHid(handle, "PNP0C0F"))
1525b460c6f8SJohn Baldwin 	*order = 3;
1526da72d149SNate Lawson     return (0);
152791233413SNate Lawson }
152891233413SNate Lawson 
152915e32d5dSMike Smith /*
153015e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
153115e32d5dSMike Smith  * it.
153215e32d5dSMike Smith  */
153315e32d5dSMike Smith static ACPI_STATUS
153415e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
153515e32d5dSMike Smith {
153615e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
1537858a52f4SMitsuru IWASAKI     ACPI_HANDLE h;
153833332dc2SNate Lawson     device_t bus, child;
1539da72d149SNate Lawson     int order;
154033332dc2SNate Lawson     char *handle_str, **search;
154133332dc2SNate Lawson     static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
154215e32d5dSMike Smith 
1543b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15440ae55423SMike Smith 
1545be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
15460ae55423SMike Smith     if (acpi_avoid(handle))
15470ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
15480ae55423SMike Smith 
154991233413SNate Lawson     bus = (device_t)context;
1550b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
155115e32d5dSMike Smith 	switch (type) {
155215e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
155315e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
155415e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
155515e32d5dSMike Smith 	case ACPI_TYPE_POWER:
15560ae55423SMike Smith 	    if (acpi_disabled("children"))
15570ae55423SMike Smith 		break;
1558be2b1797SNate Lawson 
155915e32d5dSMike Smith 	    /*
156033332dc2SNate Lawson 	     * Since we scan from \, be sure to skip system scope objects.
156133332dc2SNate Lawson 	     * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
156233332dc2SNate Lawson 	     */
156333332dc2SNate Lawson 	    handle_str = acpi_name(handle);
156433332dc2SNate Lawson 	    for (search = scopes; *search != NULL; search++) {
156533332dc2SNate Lawson 		if (strcmp(handle_str, *search) == 0)
156633332dc2SNate Lawson 		    break;
156733332dc2SNate Lawson 	    }
156833332dc2SNate Lawson 	    if (*search != NULL)
156933332dc2SNate Lawson 		break;
157033332dc2SNate Lawson 
157133332dc2SNate Lawson 	    /*
157215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
1573b82ca9a9SNate Lawson 	     * so that the probe/attach passes will run breadth-first.  Orders
1574b82ca9a9SNate Lawson 	     * less than 10 are reserved for special objects (i.e., system
1575b82ca9a9SNate Lawson 	     * resources).  Larger values are used for all other devices.
157615e32d5dSMike Smith 	     */
157733332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1578b82ca9a9SNate Lawson 	    order = (level + 1) * 10;
1579da72d149SNate Lawson 	    acpi_probe_order(handle, &order);
158091233413SNate Lawson 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1581bc0f2195SMike Smith 	    if (child == NULL)
1582bc0f2195SMike Smith 		break;
158359a890e6SNate Lawson 
158459a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
158515e32d5dSMike Smith 	    acpi_set_handle(child, handle);
158659a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1587bc0f2195SMike Smith 
1588bc0f2195SMike Smith 	    /*
1589b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1590b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1591b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1592893f750aSNate Lawson 	     *
1593893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
1594893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
1595893f750aSNate Lawson 	     * anyway since we may enable them later.
1596b519f9d6SMike Smith 	     */
1597858a52f4SMitsuru IWASAKI 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1598858a52f4SMitsuru IWASAKI 		/* Never disable PCI link devices. */
1599858a52f4SMitsuru IWASAKI 		if (acpi_MatchHid(handle, "PNP0C0F"))
1600858a52f4SMitsuru IWASAKI 		    break;
1601858a52f4SMitsuru IWASAKI 		/*
1602858a52f4SMitsuru IWASAKI 		 * Docking stations should remain enabled since the system
1603858a52f4SMitsuru IWASAKI 		 * may be undocked at boot.
1604858a52f4SMitsuru IWASAKI 		 */
1605858a52f4SMitsuru IWASAKI 		if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
1606858a52f4SMitsuru IWASAKI 		    break;
1607858a52f4SMitsuru IWASAKI 
1608b519f9d6SMike Smith 		device_disable(child);
1609b519f9d6SMike Smith 		break;
1610b519f9d6SMike Smith 	    }
1611b519f9d6SMike Smith 
1612b519f9d6SMike Smith 	    /*
1613bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1614bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1615bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1616bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1617bc0f2195SMike Smith 	     * device not to have any resources.
1618bc0f2195SMike Smith 	     */
161972ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
16200ae55423SMike Smith 	    break;
162115e32d5dSMike Smith 	}
162215e32d5dSMike Smith     }
1623be2b1797SNate Lawson 
16240ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
162515e32d5dSMike Smith }
162615e32d5dSMike Smith 
162759a890e6SNate Lawson /*
162859a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
162959a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
163059a890e6SNate Lawson  */
163159a890e6SNate Lawson void
163259a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
163359a890e6SNate Lawson {
163459a890e6SNate Lawson }
163559a890e6SNate Lawson 
163615e32d5dSMike Smith static void
163715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
163815e32d5dSMike Smith {
1639d85e6785SNate Lawson     struct acpi_softc *sc;
1640d33f4987STakanori Watanabe     ACPI_STATUS status;
1641eeb3a05fSNate Lawson 
1642eeb3a05fSNate Lawson     /*
164380f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
164480f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
164580f0e4c2SNate Lawson      * an AP.
1646eeb3a05fSNate Lawson      */
1647d85e6785SNate Lawson     sc = arg;
1648eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1649904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1650904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1651904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1652904bf0c2SNate Lawson 		   AcpiFormatException(status));
1653904bf0c2SNate Lawson 	    return;
1654904bf0c2SNate Lawson 	}
1655eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
165655398047SNate Lawson 	ACPI_DISABLE_IRQS();
1657865b8d0bSNate Lawson 	status = AcpiEnterSleepState(ACPI_STATE_S5);
1658be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1659bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
166015e32d5dSMike Smith 	} else {
166115e32d5dSMike Smith 	    DELAY(1000000);
166215e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
166315e32d5dSMike Smith 	}
1664d1b16e18SNate Lawson     } else if ((howto & RB_HALT) == 0 && AcpiGbl_FADT->ResetRegSup &&
1665d1b16e18SNate Lawson 	sc->acpi_handle_reboot) {
1666d85e6785SNate Lawson 	/* Reboot using the reset register. */
166787a500cdSNate Lawson 	status = AcpiHwLowLevelWrite(
166887a500cdSNate Lawson 	    AcpiGbl_FADT->ResetRegister.RegisterBitWidth,
166987a500cdSNate Lawson 	    AcpiGbl_FADT->ResetValue, &AcpiGbl_FADT->ResetRegister);
167087a500cdSNate Lawson 	if (ACPI_FAILURE(status)) {
167187a500cdSNate Lawson 	    printf("ACPI reset failed - %s\n", AcpiFormatException(status));
167287a500cdSNate Lawson 	} else {
167387a500cdSNate Lawson 	    DELAY(1000000);
167487a500cdSNate Lawson 	    printf("ACPI reset failed - timeout\n");
167587a500cdSNate Lawson 	}
1676d85e6785SNate Lawson     } else if (sc->acpi_do_disable && panicstr == NULL) {
1677d85e6785SNate Lawson 	/*
1678d85e6785SNate Lawson 	 * Only disable ACPI if the user requested.  On some systems, writing
1679d85e6785SNate Lawson 	 * the disable value to SMI_CMD hangs the system.
1680d85e6785SNate Lawson 	 */
168180f0e4c2SNate Lawson 	printf("Shutting down ACPI\n");
168280f0e4c2SNate Lawson 	AcpiTerminate();
168380f0e4c2SNate Lawson     }
168415e32d5dSMike Smith }
168515e32d5dSMike Smith 
168613d4f7baSMitsuru IWASAKI static void
168713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
168813d4f7baSMitsuru IWASAKI {
168913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
169013d4f7baSMitsuru IWASAKI 
169113d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1692be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
169359ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
169413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
169533febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1696be2b1797SNate Lawson 	if (first_time)
16976c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
169813d4f7baSMitsuru IWASAKI     }
1699be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
170059ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
170113d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
170233febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1703be2b1797SNate Lawson 	if (first_time)
17046c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
170513d4f7baSMitsuru IWASAKI     }
170613d4f7baSMitsuru IWASAKI 
170713d4f7baSMitsuru IWASAKI     first_time = 0;
170813d4f7baSMitsuru IWASAKI }
170913d4f7baSMitsuru IWASAKI 
171015e32d5dSMike Smith /*
1711c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1712c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1713c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1714c5ba0be4SMike Smith  */
1715c5ba0be4SMike Smith BOOLEAN
1716c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1717c5ba0be4SMike Smith {
17181e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1719c5ba0be4SMike Smith     ACPI_HANDLE		h;
17201e4925e8SNate Lawson     ACPI_BUFFER		buf;
1721c5ba0be4SMike Smith     ACPI_STATUS		error;
17221e4925e8SNate Lawson     int			ret;
1723c5ba0be4SMike Smith 
17241e4925e8SNate Lawson     ret = FALSE;
1725c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1726c5ba0be4SMike Smith 	return (FALSE);
17271e4925e8SNate Lawson     buf.Pointer = NULL;
17281e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17296fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17306fca9360SNate Lawson     if (ACPI_FAILURE(error))
1731c5ba0be4SMike Smith 	return (FALSE);
17321e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1733be2b1797SNate Lawson 
1734be2b1797SNate Lawson     /* If no _STA method, must be present */
17351e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
17361e4925e8SNate Lawson 	ret = TRUE;
1737be2b1797SNate Lawson 
1738be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
173917dbe0f7SNate Lawson     if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
17401e4925e8SNate Lawson 	ret = TRUE;
1741be2b1797SNate Lawson 
17421e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17431e4925e8SNate Lawson     return (ret);
1744c5ba0be4SMike Smith }
1745c5ba0be4SMike Smith 
1746c5ba0be4SMike Smith /*
1747b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1748b53f2771SMike Smith  */
1749b53f2771SMike Smith BOOLEAN
1750b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1751b53f2771SMike Smith {
17521e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1753b53f2771SMike Smith     ACPI_HANDLE		h;
17541e4925e8SNate Lawson     ACPI_BUFFER		buf;
1755b53f2771SMike Smith     ACPI_STATUS		error;
17561e4925e8SNate Lawson     int			ret;
1757b53f2771SMike Smith 
17581e4925e8SNate Lawson     ret = FALSE;
1759b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1760b53f2771SMike Smith 	return (FALSE);
17611e4925e8SNate Lawson     buf.Pointer = NULL;
17621e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17636fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17646fca9360SNate Lawson     if (ACPI_FAILURE(error))
1765b53f2771SMike Smith 	return (FALSE);
17661e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1767be2b1797SNate Lawson 
1768be2b1797SNate Lawson     /* If no _STA method, must be present */
17691e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
17701e4925e8SNate Lawson 	ret = TRUE;
1771be2b1797SNate Lawson 
177217dbe0f7SNate Lawson     /* Return true for 'present', 'battery present', and 'functioning' */
177317dbe0f7SNate Lawson     if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
17741e4925e8SNate Lawson 	ret = TRUE;
1775be2b1797SNate Lawson 
17761e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17771e4925e8SNate Lawson     return (ret);
1778b53f2771SMike Smith }
1779b53f2771SMike Smith 
1780b53f2771SMike Smith /*
178191233413SNate Lawson  * Match a HID string against a handle
178215e32d5dSMike Smith  */
1783ce619b2eSNate Lawson static BOOLEAN
1784ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
178515e32d5dSMike Smith {
17861e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
17871e4925e8SNate Lawson     ACPI_BUFFER		buf;
178815e32d5dSMike Smith     ACPI_STATUS		error;
17891e4925e8SNate Lawson     int			ret, i;
179015e32d5dSMike Smith 
17911e4925e8SNate Lawson     ret = FALSE;
179291233413SNate Lawson     if (hid == NULL || h == NULL)
179391233413SNate Lawson 	return (ret);
17941e4925e8SNate Lawson     buf.Pointer = NULL;
17951e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17966fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17976fca9360SNate Lawson     if (ACPI_FAILURE(error))
179891233413SNate Lawson 	return (ret);
17991e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1800be2b1797SNate Lawson 
1801c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1802c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
18031e4925e8SNate Lawson 	    ret = TRUE;
1804c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
18051e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
18061e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
18071e4925e8SNate Lawson 		ret = TRUE;
18081e4925e8SNate Lawson 		break;
18091e4925e8SNate Lawson 	    }
18101e4925e8SNate Lawson 	}
18111e4925e8SNate Lawson     }
1812be2b1797SNate Lawson 
18131e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
18141e4925e8SNate Lawson     return (ret);
181515e32d5dSMike Smith }
181615e32d5dSMike Smith 
181715e32d5dSMike Smith /*
1818c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1819c5ba0be4SMike Smith  * or one if its parents.
1820c5ba0be4SMike Smith  */
1821c5ba0be4SMike Smith ACPI_STATUS
1822c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1823c5ba0be4SMike Smith {
1824c5ba0be4SMike Smith     ACPI_HANDLE		r;
1825c5ba0be4SMike Smith     ACPI_STATUS		status;
1826c5ba0be4SMike Smith 
1827be2b1797SNate Lawson     /* Walk back up the tree to the root */
1828c5ba0be4SMike Smith     for (;;) {
1829be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1830be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1831c5ba0be4SMike Smith 	    *result = r;
1832c5ba0be4SMike Smith 	    return (AE_OK);
1833c5ba0be4SMike Smith 	}
1834bee4aa4aSNate Lawson 	/* XXX Return error here? */
1835c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1836c5ba0be4SMike Smith 	    return (AE_OK);
1837b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1838c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1839c5ba0be4SMike Smith 	parent = r;
1840c5ba0be4SMike Smith     }
1841c5ba0be4SMike Smith }
1842c5ba0be4SMike Smith 
1843eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1844eea17c34SNate Lawson uint32_t
1845eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1846eea17c34SNate Lawson {
1847eea17c34SNate Lawson     uint32_t delta;
1848eea17c34SNate Lawson 
1849eea17c34SNate Lawson     if (end >= start)
1850eea17c34SNate Lawson 	delta = end - start;
1851eea17c34SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
18528d01ceefSNate Lawson 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1853eea17c34SNate Lawson     else
1854eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
1855eea17c34SNate Lawson     return (delta);
1856eea17c34SNate Lawson }
1857eea17c34SNate Lawson 
1858c5ba0be4SMike Smith /*
1859c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1860c5ba0be4SMike Smith  */
1861c5ba0be4SMike Smith ACPI_BUFFER *
1862c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1863c5ba0be4SMike Smith {
1864c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1865c5ba0be4SMike Smith 
1866c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1867c5ba0be4SMike Smith 	return (NULL);
1868c5ba0be4SMike Smith     buf->Length = size;
1869c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1870c5ba0be4SMike Smith     return (buf);
1871c5ba0be4SMike Smith }
1872c5ba0be4SMike Smith 
1873c310653eSNate Lawson ACPI_STATUS
1874cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1875c310653eSNate Lawson {
1876c310653eSNate Lawson     ACPI_OBJECT arg1;
1877c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1878c310653eSNate Lawson 
1879c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1880c310653eSNate Lawson     arg1.Integer.Value = number;
1881c310653eSNate Lawson     args.Count = 1;
1882c310653eSNate Lawson     args.Pointer = &arg1;
1883c310653eSNate Lawson 
1884c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1885c310653eSNate Lawson }
1886c310653eSNate Lawson 
1887c5ba0be4SMike Smith /*
1888c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1889c5ba0be4SMike Smith  */
1890c5ba0be4SMike Smith ACPI_STATUS
1891cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1892c5ba0be4SMike Smith {
1893be2b1797SNate Lawson     ACPI_STATUS	status;
1894c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1895c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1896c5ba0be4SMike Smith 
1897c5ba0be4SMike Smith     if (handle == NULL)
1898c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
18990c7da7acSJohn Baldwin 
19000c7da7acSJohn Baldwin     /*
19010c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
19020c7da7acSJohn Baldwin      * a method that will return an Integer.
19030c7da7acSJohn Baldwin      */
1904c5ba0be4SMike Smith     buf.Pointer = &param;
1905c5ba0be4SMike Smith     buf.Length = sizeof(param);
1906be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1907be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1908be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1909c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1910be2b1797SNate Lawson 	else
1911be2b1797SNate Lawson 	    status = AE_TYPE;
1912c5ba0be4SMike Smith     }
19130c7da7acSJohn Baldwin 
19140c7da7acSJohn Baldwin     /*
19150c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
19160c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
19170c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
19180c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
19190c7da7acSJohn Baldwin      *
19200c7da7acSJohn Baldwin      * This is a hack.
19210c7da7acSJohn Baldwin      */
1922be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1923b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1924be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
19250c7da7acSJohn Baldwin 	} else {
1926be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1927be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1928be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
19290c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
19300c7da7acSJohn Baldwin 	}
1931f97739daSNate Lawson     }
1932be2b1797SNate Lawson     return (status);
1933c5ba0be4SMike Smith }
1934c5ba0be4SMike Smith 
1935c573e654SMitsuru IWASAKI ACPI_STATUS
1936cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1937c573e654SMitsuru IWASAKI {
1938c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1939dba55fa2SNate Lawson     UINT8	*val;
1940c573e654SMitsuru IWASAKI     int		i;
1941c573e654SMitsuru IWASAKI 
1942c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1943c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1944c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1945c573e654SMitsuru IWASAKI 	return (AE_OK);
1946c573e654SMitsuru IWASAKI     }
1947c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1948c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1949c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1950c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1951be2b1797SNate Lawson 
1952c573e654SMitsuru IWASAKI     *number = 0;
1953dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1954c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1955dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1956c573e654SMitsuru IWASAKI     return (AE_OK);
1957c573e654SMitsuru IWASAKI }
1958c573e654SMitsuru IWASAKI 
1959c5ba0be4SMike Smith /*
1960c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1961c5ba0be4SMike Smith  * function for each element.
1962c5ba0be4SMike Smith  *
1963c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1964c5ba0be4SMike Smith  */
1965c5ba0be4SMike Smith ACPI_STATUS
1966be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1967be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1968c5ba0be4SMike Smith {
1969c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1970c5ba0be4SMike Smith     int		i;
1971c5ba0be4SMike Smith 
1972be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1973c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1974c5ba0be4SMike Smith 
1975be2b1797SNate Lawson     /* Iterate over components */
1976be2b1797SNate Lawson     i = 0;
1977be2b1797SNate Lawson     comp = pkg->Package.Elements;
1978be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1979c5ba0be4SMike Smith 	func(comp, arg);
1980c5ba0be4SMike Smith 
1981c5ba0be4SMike Smith     return (AE_OK);
1982c5ba0be4SMike Smith }
1983c5ba0be4SMike Smith 
19846f69255bSMike Smith /*
19856f69255bSMike Smith  * Find the (index)th resource object in a set.
19866f69255bSMike Smith  */
19876f69255bSMike Smith ACPI_STATUS
19886d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
19896f69255bSMike Smith {
19906d63101aSMike Smith     ACPI_RESOURCE	*rp;
19916f69255bSMike Smith     int			i;
19926f69255bSMike Smith 
19936d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
19946f69255bSMike Smith     i = index;
19956d63101aSMike Smith     while (i-- > 0) {
1996be2b1797SNate Lawson 	/* Range check */
19976d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
19986f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1999be2b1797SNate Lawson 
2000be2b1797SNate Lawson 	/* Check for terminator */
2001e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
20026d63101aSMike Smith 	    return (AE_NOT_FOUND);
2003abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
20046f69255bSMike Smith     }
20056f69255bSMike Smith     if (resp != NULL)
20066d63101aSMike Smith 	*resp = rp;
2007be2b1797SNate Lawson 
20086d63101aSMike Smith     return (AE_OK);
20096d63101aSMike Smith }
20106d63101aSMike Smith 
20116d63101aSMike Smith /*
20126d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
20136d63101aSMike Smith  *
20146d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
20156d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
20166d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
20176d63101aSMike Smith  * resources.
20186d63101aSMike Smith  */
20196d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
20206d63101aSMike Smith 
20216d63101aSMike Smith ACPI_STATUS
20226d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
20236d63101aSMike Smith {
20246d63101aSMike Smith     ACPI_RESOURCE	*rp;
20256d63101aSMike Smith     void		*newp;
20266d63101aSMike Smith 
2027be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
20286d63101aSMike Smith     if (buf->Pointer == NULL) {
20296d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
20306d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
20316d63101aSMike Smith 	    return (AE_NO_MEMORY);
20326d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2033e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
20346d63101aSMike Smith 	rp->Length = 0;
20356d63101aSMike Smith     }
20366d63101aSMike Smith     if (res == NULL)
20376d63101aSMike Smith 	return (AE_OK);
20386d63101aSMike Smith 
20396d63101aSMike Smith     /*
20406d63101aSMike Smith      * Scan the current buffer looking for the terminator.
20416d63101aSMike Smith      * This will either find the terminator or hit the end
20426d63101aSMike Smith      * of the buffer and return an error.
20436d63101aSMike Smith      */
20446d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
20456d63101aSMike Smith     for (;;) {
2046be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
20476d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
20486d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2049e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
20506d63101aSMike Smith 	    break;
2051abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
20526d63101aSMike Smith     }
20536d63101aSMike Smith 
20546d63101aSMike Smith     /*
20556d63101aSMike Smith      * Check the size of the buffer and expand if required.
20566d63101aSMike Smith      *
20576d63101aSMike Smith      * Required size is:
20586d63101aSMike Smith      *	size of existing resources before terminator +
20596d63101aSMike Smith      *	size of new resource and header +
20606d63101aSMike Smith      * 	size of terminator.
20616d63101aSMike Smith      *
20626d63101aSMike Smith      * Note that this loop should really only run once, unless
20636d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
20646d63101aSMike Smith      */
20656d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2066e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2067e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
20686d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
20696d63101aSMike Smith 	    return (AE_NO_MEMORY);
20706d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2071a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2072a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
20736d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
20746d63101aSMike Smith 	buf->Pointer = newp;
20756d63101aSMike Smith 	buf->Length += buf->Length;
20766d63101aSMike Smith     }
20776d63101aSMike Smith 
2078be2b1797SNate Lawson     /* Insert the new resource. */
2079e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
20806d63101aSMike Smith 
2081be2b1797SNate Lawson     /* And add the terminator. */
2082abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2083e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
20846d63101aSMike Smith     rp->Length = 0;
20856d63101aSMike Smith 
20866f69255bSMike Smith     return (AE_OK);
20876f69255bSMike Smith }
2088c5ba0be4SMike Smith 
2089da14ac9fSJohn Baldwin /*
2090da14ac9fSJohn Baldwin  * Set interrupt model.
2091da14ac9fSJohn Baldwin  */
2092da14ac9fSJohn Baldwin ACPI_STATUS
2093da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2094da14ac9fSJohn Baldwin {
2095da14ac9fSJohn Baldwin 
2096c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2097da14ac9fSJohn Baldwin }
2098da14ac9fSJohn Baldwin 
2099ece50487SMitsuru IWASAKI static void
2100ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2101ece50487SMitsuru IWASAKI {
2102bee4aa4aSNate Lawson 
2103ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
2104ece50487SMitsuru IWASAKI }
2105c30382dfSMitsuru IWASAKI 
210615e2f34fSNate Lawson enum acpi_sleep_state {
210715e2f34fSNate Lawson     ACPI_SS_NONE,
210815e2f34fSNate Lawson     ACPI_SS_GPE_SET,
210915e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
211015e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
211115e2f34fSNate Lawson     ACPI_SS_SLEPT,
211215e2f34fSNate Lawson };
211315e2f34fSNate Lawson 
211415e32d5dSMike Smith /*
211515e32d5dSMike Smith  * Set the system sleep state
211615e32d5dSMike Smith  *
2117be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
211815e32d5dSMike Smith  */
211915e32d5dSMike Smith ACPI_STATUS
212015e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
212115e32d5dSMike Smith {
212215e2f34fSNate Lawson     ACPI_STATUS	status;
212356d8cb57SMitsuru IWASAKI     UINT8	TypeA;
212456d8cb57SMitsuru IWASAKI     UINT8	TypeB;
212515e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
212615e32d5dSMike Smith 
2127b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
21280ae55423SMike Smith 
212915e2f34fSNate Lawson     status = AE_OK;
213015e2f34fSNate Lawson     ACPI_LOCK(acpi);
213115e2f34fSNate Lawson     if (sc->acpi_sleep_disabled) {
21326397624dSMitsuru IWASAKI 	if (sc->acpi_sstate != ACPI_STATE_S0)
213315e2f34fSNate Lawson 	    status = AE_ERROR;
213415e2f34fSNate Lawson 	ACPI_UNLOCK(acpi);
213515e2f34fSNate Lawson 	printf("acpi: suspend request ignored (not ready yet)\n");
213615e2f34fSNate Lawson 	return (status);
213715e2f34fSNate Lawson     }
213815e2f34fSNate Lawson     sc->acpi_sleep_disabled = 1;
213915e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
21406397624dSMitsuru IWASAKI 
21415d3d03f1SNate Lawson     /*
21425d3d03f1SNate Lawson      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
21435d3d03f1SNate Lawson      * drivers need this.
21445d3d03f1SNate Lawson      */
21455d3d03f1SNate Lawson     mtx_lock(&Giant);
214615e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
214715e32d5dSMike Smith     switch (state) {
214815e32d5dSMike Smith     case ACPI_STATE_S1:
21496161544cSTakanori Watanabe     case ACPI_STATE_S2:
21506161544cSTakanori Watanabe     case ACPI_STATE_S3:
21516161544cSTakanori Watanabe     case ACPI_STATE_S4:
2152adad4744SNate Lawson 	status = AcpiGetSleepTypeData(state, &TypeA, &TypeB);
215398175614SNate Lawson 	if (status == AE_NOT_FOUND) {
215498175614SNate Lawson 	    device_printf(sc->acpi_dev,
215598175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
215698175614SNate Lawson 	    break;
215798175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
2158be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
2159be2b1797SNate Lawson 			  AcpiFormatException(status));
216056d8cb57SMitsuru IWASAKI 	    break;
216156d8cb57SMitsuru IWASAKI 	}
216256d8cb57SMitsuru IWASAKI 
2163a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
2164a1fccb47SMitsuru IWASAKI 
2165d4b9ff91SNate Lawson 	/* Enable any GPEs as appropriate and requested by the user. */
2166d4b9ff91SNate Lawson 	acpi_wake_prep_walk(state);
216715e2f34fSNate Lawson 	slp_state = ACPI_SS_GPE_SET;
2168e8b4d56eSNate Lawson 
216915e32d5dSMike Smith 	/*
2170c7f88fc3SNate Lawson 	 * Inform all devices that we are going to sleep.  If at least one
2171c7f88fc3SNate Lawson 	 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
217215e32d5dSMike Smith 	 *
2173c7f88fc3SNate Lawson 	 * XXX Note that a better two-pass approach with a 'veto' pass
2174c7f88fc3SNate Lawson 	 * followed by a "real thing" pass would be better, but the current
2175c7f88fc3SNate Lawson 	 * bus interface does not provide for this.
217615e32d5dSMike Smith 	 */
217715e2f34fSNate Lawson 	if (DEVICE_SUSPEND(root_bus) != 0) {
217815e2f34fSNate Lawson 	    device_printf(sc->acpi_dev, "device_suspend failed\n");
217915e2f34fSNate Lawson 	    break;
218015e2f34fSNate Lawson 	}
218115e2f34fSNate Lawson 	slp_state = ACPI_SS_DEV_SUSPEND;
2182b9c780d6SMitsuru IWASAKI 
2183be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
2184be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2185b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2186b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
2187b9c780d6SMitsuru IWASAKI 	    break;
2188b9c780d6SMitsuru IWASAKI 	}
218915e2f34fSNate Lawson 	slp_state = ACPI_SS_SLP_PREP;
2190b9c780d6SMitsuru IWASAKI 
2191be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
2192ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
2193ff01efb5SMitsuru IWASAKI 
21946161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
21956161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
21966161544cSTakanori Watanabe 
21976161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
2198be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
2199964679ceSMitsuru IWASAKI 		AcpiEnable();
22006161544cSTakanori Watanabe 	} else {
2201c7f88fc3SNate Lawson 	    ACPI_DISABLE_IRQS();
2202adad4744SNate Lawson 	    status = AcpiEnterSleepState(state);
2203be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
2204be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2205be2b1797SNate Lawson 			      AcpiFormatException(status));
2206c30382dfSMitsuru IWASAKI 		break;
220715e32d5dSMike Smith 	    }
22086161544cSTakanori Watanabe 	}
220915e2f34fSNate Lawson 	slp_state = ACPI_SS_SLEPT;
221015e32d5dSMike Smith 	break;
221115e32d5dSMike Smith     case ACPI_STATE_S5:
221215e32d5dSMike Smith 	/*
221315e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
221415e32d5dSMike Smith 	 * shutdown handlers.
221515e32d5dSMike Smith 	 */
221615e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
221715e32d5dSMike Smith 	break;
221898175614SNate Lawson     case ACPI_STATE_S0:
221915e32d5dSMike Smith     default:
222015e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
222115e32d5dSMike Smith 	break;
222215e32d5dSMike Smith     }
2223ece50487SMitsuru IWASAKI 
222415e2f34fSNate Lawson     /*
222515e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
222615e2f34fSNate Lawson      * process.  This handles both the error and success cases.
222715e2f34fSNate Lawson      */
222815e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
222915e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
223015e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
223115e2f34fSNate Lawson     }
223215e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLP_PREP)
223315e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
2234071339e2SNate Lawson     if (slp_state >= ACPI_SS_DEV_SUSPEND)
2235071339e2SNate Lawson 	DEVICE_RESUME(root_bus);
223615e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLEPT)
223715e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
223815e2f34fSNate Lawson 
223915e2f34fSNate Lawson     /* Allow another sleep request after a while. */
224015e2f34fSNate Lawson     if (state != ACPI_STATE_S5)
2241ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
2242ece50487SMitsuru IWASAKI 
22435d3d03f1SNate Lawson     mtx_unlock(&Giant);
22440ae55423SMike Smith     return_ACPI_STATUS (status);
224515e32d5dSMike Smith }
224615e32d5dSMike Smith 
2247e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
2248e8b4d56eSNate Lawson int
2249e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
2250e8b4d56eSNate Lawson {
2251e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2252e8b4d56eSNate Lawson 
2253e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
2254e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2255e8b4d56eSNate Lawson 	return (ENXIO);
2256e8b4d56eSNate Lawson 
2257e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
2258e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
2259e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
2260e8b4d56eSNate Lawson 	return (ENXIO);
2261e8b4d56eSNate Lawson     }
2262e8b4d56eSNate Lawson 
2263e8b4d56eSNate Lawson     return (0);
2264e8b4d56eSNate Lawson }
2265e8b4d56eSNate Lawson 
2266e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
2267e8b4d56eSNate Lawson int
2268e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
2269e8b4d56eSNate Lawson {
2270e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2271e8b4d56eSNate Lawson     ACPI_HANDLE handle;
2272e8b4d56eSNate Lawson     ACPI_STATUS status;
2273e8b4d56eSNate Lawson     int flags;
2274e8b4d56eSNate Lawson 
2275d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
2276e8b4d56eSNate Lawson     handle = acpi_get_handle(dev);
2277e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2278e8b4d56eSNate Lawson 	return (ENXIO);
2279e8b4d56eSNate Lawson 
2280d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
2281e8b4d56eSNate Lawson     if (enable) {
2282e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2283e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2284e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
2285e8b4d56eSNate Lawson 	    return (ENXIO);
2286e8b4d56eSNate Lawson 	}
2287d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2288e8b4d56eSNate Lawson     } else {
2289e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2290e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2291e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
2292e8b4d56eSNate Lawson 	    return (ENXIO);
2293e8b4d56eSNate Lawson 	}
2294d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2295e8b4d56eSNate Lawson     }
2296e8b4d56eSNate Lawson 
2297e8b4d56eSNate Lawson     return (0);
2298e8b4d56eSNate Lawson }
2299e8b4d56eSNate Lawson 
2300d4b9ff91SNate Lawson static int
2301d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2302e8b4d56eSNate Lawson {
2303e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2304d4b9ff91SNate Lawson     device_t dev;
2305e8b4d56eSNate Lawson 
2306d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
2307e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2308e8b4d56eSNate Lawson 	return (ENXIO);
2309d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2310e8b4d56eSNate Lawson 
2311e8b4d56eSNate Lawson     /*
2312d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
2313d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
2314d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
2315d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
2316d4b9ff91SNate Lawson      * and set _PSW.
2317e8b4d56eSNate Lawson      */
2318d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2319cc85c78cSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2320e8b4d56eSNate Lawson 	if (bootverbose)
2321d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2322d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2323d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2324d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
2325d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
2326d4b9ff91SNate Lawson 	if (bootverbose)
2327d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2328d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2329d4b9ff91SNate Lawson     }
2330cc85c78cSNate Lawson 
2331cc85c78cSNate Lawson     return (0);
2332e8b4d56eSNate Lawson }
2333e8b4d56eSNate Lawson 
2334d4b9ff91SNate Lawson static int
2335d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2336cc85c78cSNate Lawson {
2337cc85c78cSNate Lawson     struct acpi_prw_data prw;
2338d4b9ff91SNate Lawson     device_t dev;
2339cc85c78cSNate Lawson 
2340cc85c78cSNate Lawson     /*
2341d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
2342d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
2343cc85c78cSNate Lawson      */
2344d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2345d4b9ff91SNate Lawson 	return (ENXIO);
2346d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2347d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2348d4b9ff91SNate Lawson 	return (0);
2349cc85c78cSNate Lawson 
2350d4b9ff91SNate Lawson     /*
2351d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
2352d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
2353d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
2354d4b9ff91SNate Lawson      */
2355d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2356cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2357d4b9ff91SNate Lawson 	if (bootverbose)
2358d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2359d4b9ff91SNate Lawson     } else {
2360d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
2361d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
2362d4b9ff91SNate Lawson 	if (bootverbose)
2363d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
2364d4b9ff91SNate Lawson 		acpi_name(handle));
2365d4b9ff91SNate Lawson     }
2366d4b9ff91SNate Lawson 
2367e8b4d56eSNate Lawson     return (0);
2368e8b4d56eSNate Lawson }
2369e8b4d56eSNate Lawson 
2370e8b4d56eSNate Lawson static ACPI_STATUS
2371d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2372e8b4d56eSNate Lawson {
2373d4b9ff91SNate Lawson     int sstate;
2374e8b4d56eSNate Lawson 
2375d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
2376d4b9ff91SNate Lawson     sstate = *(int *)context;
2377d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
2378d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
2379d4b9ff91SNate Lawson     else
2380d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
2381e8b4d56eSNate Lawson     return (AE_OK);
2382e8b4d56eSNate Lawson }
2383e8b4d56eSNate Lawson 
2384d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2385e8b4d56eSNate Lawson static int
2386d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
2387e8b4d56eSNate Lawson {
2388e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
2389e8b4d56eSNate Lawson 
2390e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2391d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
2392d4b9ff91SNate Lawson 	    acpi_wake_prep, &sstate, NULL);
2393e8b4d56eSNate Lawson     return (0);
2394e8b4d56eSNate Lawson }
2395e8b4d56eSNate Lawson 
239688a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
239788a79fc0SNate Lawson static int
239888a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
239988a79fc0SNate Lawson {
240088a79fc0SNate Lawson     int error, i, numdevs;
240188a79fc0SNate Lawson     device_t *devlist;
240288a79fc0SNate Lawson     device_t child;
2403d4b9ff91SNate Lawson     ACPI_STATUS status;
240488a79fc0SNate Lawson 
240588a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
24061c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
24071c9ec538SNate Lawson 	if (numdevs == 0)
24081c9ec538SNate Lawson 	    free(devlist, M_TEMP);
240988a79fc0SNate Lawson 	return (error);
24101c9ec538SNate Lawson     }
241188a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
241288a79fc0SNate Lawson 	child = devlist[i];
2413d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
241488a79fc0SNate Lawson 	if (!device_is_attached(child))
241588a79fc0SNate Lawson 	    continue;
2416d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2417d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
241888a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
241988a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
242088a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
242188a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
242288a79fc0SNate Lawson 	}
242388a79fc0SNate Lawson     }
242488a79fc0SNate Lawson     free(devlist, M_TEMP);
242588a79fc0SNate Lawson 
242688a79fc0SNate Lawson     return (0);
242788a79fc0SNate Lawson }
242888a79fc0SNate Lawson 
242988a79fc0SNate Lawson /* Enable or disable wake from userland. */
243088a79fc0SNate Lawson static int
243188a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
243288a79fc0SNate Lawson {
243388a79fc0SNate Lawson     int enable, error;
243488a79fc0SNate Lawson     device_t dev;
243588a79fc0SNate Lawson 
243688a79fc0SNate Lawson     dev = (device_t)arg1;
2437d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
243888a79fc0SNate Lawson 
243988a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
244088a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
244188a79fc0SNate Lawson 	return (error);
244288a79fc0SNate Lawson     if (enable != 0 && enable != 1)
244388a79fc0SNate Lawson 	return (EINVAL);
244488a79fc0SNate Lawson 
244588a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
244688a79fc0SNate Lawson }
244788a79fc0SNate Lawson 
2448e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
2449d4b9ff91SNate Lawson int
2450e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2451e8b4d56eSNate Lawson {
2452e8b4d56eSNate Lawson     ACPI_STATUS			status;
2453e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2454e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2455d4b9ff91SNate Lawson     int				error, i, power_count;
2456e8b4d56eSNate Lawson 
2457e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2458e8b4d56eSNate Lawson 	return (EINVAL);
2459e8b4d56eSNate Lawson 
2460e8b4d56eSNate Lawson     /*
2461e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2462e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2463e8b4d56eSNate Lawson      */
2464e8b4d56eSNate Lawson     error = EINVAL;
2465e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2466e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2467e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2468e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2469e8b4d56eSNate Lawson 	return (ENOENT);
2470e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2471e8b4d56eSNate Lawson     if (res == NULL)
2472e8b4d56eSNate Lawson 	return (ENOENT);
2473e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2474e8b4d56eSNate Lawson 	goto out;
2475e8b4d56eSNate Lawson 
2476e8b4d56eSNate Lawson     /*
2477e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2478e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2479e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2480e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2481e8b4d56eSNate Lawson      */
2482e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2483e8b4d56eSNate Lawson 	goto out;
2484e8b4d56eSNate Lawson 
2485e8b4d56eSNate Lawson     /*
2486e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2487e8b4d56eSNate Lawson      */
2488e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2489e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2490e8b4d56eSNate Lawson 	/*
2491e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2492e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2493e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2494e8b4d56eSNate Lawson 	 * enabled for the wake event.
2495e8b4d56eSNate Lawson 	 */
2496e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2497e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2498e8b4d56eSNate Lawson 	error = 0;
2499e8b4d56eSNate Lawson 	break;
2500e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2501e8b4d56eSNate Lawson 	/*
2502e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2503e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2504e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2505e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2506e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2507e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2508e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2509e8b4d56eSNate Lawson 	 * the wake event.
2510e8b4d56eSNate Lawson 	 *
2511e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2512e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2513e8b4d56eSNate Lawson 	 */
2514e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2515e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2516e8b4d56eSNate Lawson 	    goto out;
2517e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2518e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2519e8b4d56eSNate Lawson 	    goto out;
2520e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2521e8b4d56eSNate Lawson 	    goto out;
2522e8b4d56eSNate Lawson 	error = 0;
2523e8b4d56eSNate Lawson 	break;
2524e8b4d56eSNate Lawson     default:
2525e8b4d56eSNate Lawson 	goto out;
2526e8b4d56eSNate Lawson     }
2527e8b4d56eSNate Lawson 
2528d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
2529d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
2530d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
2531d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
2532d4b9ff91SNate Lawson 	power_count = 0;
2533d4b9ff91SNate Lawson     }
2534d4b9ff91SNate Lawson     prw->power_res_count = power_count;
2535d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
2536d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
2537e8b4d56eSNate Lawson 
2538e8b4d56eSNate Lawson out:
2539e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2540e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2541e8b4d56eSNate Lawson     return (error);
2542e8b4d56eSNate Lawson }
2543e8b4d56eSNate Lawson 
254415e32d5dSMike Smith /*
254515e32d5dSMike Smith  * ACPI Event Handlers
254615e32d5dSMike Smith  */
254715e32d5dSMike Smith 
254815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
254915e32d5dSMike Smith 
255015e32d5dSMike Smith static void
255115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
255215e32d5dSMike Smith {
2553bee4aa4aSNate Lawson 
2554b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
255515e32d5dSMike Smith 
2556a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
255715e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
2558bee4aa4aSNate Lawson 
25590ae55423SMike Smith     return_VOID;
256015e32d5dSMike Smith }
256115e32d5dSMike Smith 
256215e32d5dSMike Smith static void
256315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
256415e32d5dSMike Smith {
2565bee4aa4aSNate Lawson 
2566b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
25670ae55423SMike Smith 
2568bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
2569cb9b0d80SMike Smith 
25700ae55423SMike Smith     return_VOID;
257115e32d5dSMike Smith }
257215e32d5dSMike Smith 
257315e32d5dSMike Smith /*
257415e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
257515e32d5dSMike Smith  */
257615e32d5dSMike Smith UINT32
257733febf93SNate Lawson acpi_event_power_button_sleep(void *context)
257815e32d5dSMike Smith {
257915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
258015e32d5dSMike Smith 
2581b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25820ae55423SMike Smith 
258315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
25840ae55423SMike Smith 
2585b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
258615e32d5dSMike Smith }
258715e32d5dSMike Smith 
258815e32d5dSMike Smith UINT32
258933febf93SNate Lawson acpi_event_power_button_wake(void *context)
259015e32d5dSMike Smith {
259115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
259215e32d5dSMike Smith 
2593b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25940ae55423SMike Smith 
259515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
25960ae55423SMike Smith 
2597b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
259815e32d5dSMike Smith }
259915e32d5dSMike Smith 
260015e32d5dSMike Smith UINT32
260133febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
260215e32d5dSMike Smith {
260315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
260415e32d5dSMike Smith 
2605b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
26060ae55423SMike Smith 
260715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
26080ae55423SMike Smith 
2609b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
261015e32d5dSMike Smith }
261115e32d5dSMike Smith 
261215e32d5dSMike Smith UINT32
261333febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
261415e32d5dSMike Smith {
261515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
261615e32d5dSMike Smith 
2617b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
26180ae55423SMike Smith 
261915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
26200ae55423SMike Smith 
2621b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
262215e32d5dSMike Smith }
262315e32d5dSMike Smith 
262415e32d5dSMike Smith /*
2625bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
2626bee4aa4aSNate Lawson  * use this for single-threaded callers.
262715e32d5dSMike Smith  */
262815e32d5dSMike Smith char *
262915e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
263015e32d5dSMike Smith {
2631bee4aa4aSNate Lawson     ACPI_BUFFER buf;
2632bee4aa4aSNate Lawson     static char data[256];
263315e32d5dSMike Smith 
2634bee4aa4aSNate Lawson     buf.Length = sizeof(data);
2635bee4aa4aSNate Lawson     buf.Pointer = data;
2636cb9b0d80SMike Smith 
26370a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
2638bee4aa4aSNate Lawson 	return (data);
2639bee4aa4aSNate Lawson     return ("(unknown)");
264015e32d5dSMike Smith }
264115e32d5dSMike Smith 
264215e32d5dSMike Smith /*
264315e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
264415e32d5dSMike Smith  * parts of the namespace.
264515e32d5dSMike Smith  */
264615e32d5dSMike Smith int
264715e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
264815e32d5dSMike Smith {
26493c600cfbSJohn Baldwin     char	*cp, *env, *np;
265015e32d5dSMike Smith     int		len;
265115e32d5dSMike Smith 
265215e32d5dSMike Smith     np = acpi_name(handle);
265315e32d5dSMike Smith     if (*np == '\\')
265415e32d5dSMike Smith 	np++;
26553c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
265615e32d5dSMike Smith 	return (0);
265715e32d5dSMike Smith 
2658be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
26593c600cfbSJohn Baldwin     cp = env;
266015e32d5dSMike Smith     for (;;) {
2661bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
266215e32d5dSMike Smith 	    cp++;
266315e32d5dSMike Smith 	if (*cp == 0)
266415e32d5dSMike Smith 	    break;
266515e32d5dSMike Smith 	len = 0;
2666bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
266715e32d5dSMike Smith 	    len++;
2668d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
26693c600cfbSJohn Baldwin 	    freeenv(env);
26700ae55423SMike Smith 	    return(1);
2671d786139cSMaxime Henrion 	}
26720ae55423SMike Smith 	cp += len;
26730ae55423SMike Smith     }
26743c600cfbSJohn Baldwin     freeenv(env);
2675be2b1797SNate Lawson 
26760ae55423SMike Smith     return (0);
26770ae55423SMike Smith }
26780ae55423SMike Smith 
26790ae55423SMike Smith /*
26800ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
26810ae55423SMike Smith  */
26820ae55423SMike Smith int
26830ae55423SMike Smith acpi_disabled(char *subsys)
26840ae55423SMike Smith {
268578689b15SMaxime Henrion     char	*cp, *env;
26860ae55423SMike Smith     int		len;
26870ae55423SMike Smith 
26883184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
26890ae55423SMike Smith 	return (0);
26903184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
269178689b15SMaxime Henrion 	freeenv(env);
26920ae55423SMike Smith 	return (1);
2693d786139cSMaxime Henrion     }
26940ae55423SMike Smith 
26953184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
269678689b15SMaxime Henrion     cp = env;
26970ae55423SMike Smith     for (;;) {
26983184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
26990ae55423SMike Smith 	    cp++;
27003184cf5aSNate Lawson 	if (*cp == '\0')
27010ae55423SMike Smith 	    break;
27020ae55423SMike Smith 	len = 0;
27033184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
27040ae55423SMike Smith 	    len++;
27053184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
270678689b15SMaxime Henrion 	    freeenv(env);
270715e32d5dSMike Smith 	    return (1);
2708d786139cSMaxime Henrion 	}
270915e32d5dSMike Smith 	cp += len;
271015e32d5dSMike Smith     }
271178689b15SMaxime Henrion     freeenv(env);
2712be2b1797SNate Lawson 
271315e32d5dSMike Smith     return (0);
271415e32d5dSMike Smith }
271515e32d5dSMike Smith 
271615e32d5dSMike Smith /*
271715e32d5dSMike Smith  * Control interface.
271815e32d5dSMike Smith  *
27190ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2720be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2721be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
272215e32d5dSMike Smith  */
27230ae55423SMike Smith struct acpi_ioctl_hook
27240ae55423SMike Smith {
27250ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
27260ae55423SMike Smith     u_long			 cmd;
2727be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
27280ae55423SMike Smith     void			 *arg;
27290ae55423SMike Smith };
27300ae55423SMike Smith 
27310ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
27320ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
27330ae55423SMike Smith 
27340ae55423SMike Smith int
2735be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
27360ae55423SMike Smith {
27370ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
27380ae55423SMike Smith 
27390ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
27400ae55423SMike Smith 	return (ENOMEM);
27410ae55423SMike Smith     hp->cmd = cmd;
27420ae55423SMike Smith     hp->fn = fn;
27430ae55423SMike Smith     hp->arg = arg;
274415e2f34fSNate Lawson 
274515e2f34fSNate Lawson     ACPI_LOCK(acpi);
27460ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
27470ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
27480ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
27490ae55423SMike Smith     }
27500ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
275115e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
275215e2f34fSNate Lawson 
27530ae55423SMike Smith     return (0);
27540ae55423SMike Smith }
27550ae55423SMike Smith 
27560ae55423SMike Smith void
2757be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
27580ae55423SMike Smith {
27590ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
27600ae55423SMike Smith 
276115e2f34fSNate Lawson     ACPI_LOCK(acpi);
27620ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
276315e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
27640ae55423SMike Smith 	    break;
27650ae55423SMike Smith 
27660ae55423SMike Smith     if (hp != NULL) {
27670ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
27680ae55423SMike Smith 	free(hp, M_ACPIDEV);
27690ae55423SMike Smith     }
277015e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
27710ae55423SMike Smith }
27720ae55423SMike Smith 
277315e32d5dSMike Smith static int
277489c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
277515e32d5dSMike Smith {
277615e32d5dSMike Smith     return (0);
277715e32d5dSMike Smith }
277815e32d5dSMike Smith 
277915e32d5dSMike Smith static int
278089c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
278115e32d5dSMike Smith {
278215e32d5dSMike Smith     return (0);
278315e32d5dSMike Smith }
278415e32d5dSMike Smith 
278515e32d5dSMike Smith static int
278689c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
278715e32d5dSMike Smith {
278815e32d5dSMike Smith     struct acpi_softc		*sc;
27890ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
2790bee4aa4aSNate Lawson     int				error, state;
279115e32d5dSMike Smith 
2792a2e35898SDavid E. O'Brien     error = 0;
2793a2e35898SDavid E. O'Brien     hp = NULL;
279415e32d5dSMike Smith     sc = dev->si_drv1;
279515e32d5dSMike Smith 
27960ae55423SMike Smith     /*
27970ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
27980ae55423SMike Smith      */
279915e2f34fSNate Lawson     ACPI_LOCK(acpi);
2800bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
28010ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2802bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
2803bee4aa4aSNate Lawson 		break;
28040ae55423SMike Smith 	}
280515e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
2806bee4aa4aSNate Lawson     if (hp)
2807bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
28080ae55423SMike Smith 
28090ae55423SMike Smith     /*
2810a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2811a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2812a89fcf28STakanori Watanabe      * Not changing system behavior.
2813a89fcf28STakanori Watanabe      */
2814be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
2815be2b1797SNate Lawson 	return (EPERM);
2816a89fcf28STakanori Watanabe 
2817be2b1797SNate Lawson     /* Core system ioctls. */
281815e32d5dSMike Smith     switch (cmd) {
281915e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
2820bee4aa4aSNate Lawson 	error = EINVAL;
282115e32d5dSMike Smith 	state = *(int *)addr;
2822bee4aa4aSNate Lawson 	if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2823bee4aa4aSNate Lawson 	    if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
2824bee4aa4aSNate Lawson 		error = 0;
282515e32d5dSMike Smith 	break;
282615e32d5dSMike Smith     default:
2827bee4aa4aSNate Lawson 	error = ENXIO;
282815e32d5dSMike Smith 	break;
282915e32d5dSMike Smith     }
2830917d44c8SMitsuru IWASAKI 
283115e32d5dSMike Smith     return (error);
283215e32d5dSMike Smith }
283315e32d5dSMike Smith 
28341d073b1dSJohn Baldwin static int
2835d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2836d75de536SMitsuru IWASAKI {
2837d75de536SMitsuru IWASAKI     int error;
2838bee4aa4aSNate Lawson     struct sbuf sb;
2839d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2840d75de536SMitsuru IWASAKI 
2841bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
2842bee4aa4aSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
2843bee4aa4aSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
2844bee4aa4aSNate Lawson 	    sbuf_printf(&sb, "S%d ", state);
2845bee4aa4aSNate Lawson     sbuf_trim(&sb);
2846bee4aa4aSNate Lawson     sbuf_finish(&sb);
2847bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2848bee4aa4aSNate Lawson     sbuf_delete(&sb);
2849d75de536SMitsuru IWASAKI     return (error);
2850d75de536SMitsuru IWASAKI }
2851d75de536SMitsuru IWASAKI 
2852d75de536SMitsuru IWASAKI static int
28531d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
28541d073b1dSJohn Baldwin {
28551d073b1dSJohn Baldwin     char sleep_state[10];
28561d073b1dSJohn Baldwin     int error;
28571d073b1dSJohn Baldwin     u_int new_state, old_state;
28581d073b1dSJohn Baldwin 
28591d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2860bee4aa4aSNate Lawson     if (old_state > ACPI_S_STATES_MAX + 1)
2861bee4aa4aSNate Lawson 	strlcpy(sleep_state, "unknown", sizeof(sleep_state));
2862bee4aa4aSNate Lawson     else
2863bee4aa4aSNate Lawson 	strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
28641d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
28651d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2866be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2867bee4aa4aSNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
2868bee4aa4aSNate Lawson 	    if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
28691d073b1dSJohn Baldwin 		break;
2870931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2871be2b1797SNate Lawson 	    if (new_state != old_state)
28721d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2873bee4aa4aSNate Lawson 	} else
28741d073b1dSJohn Baldwin 	    error = EINVAL;
28751d073b1dSJohn Baldwin     }
2876be2b1797SNate Lawson 
28771d073b1dSJohn Baldwin     return (error);
28781d073b1dSJohn Baldwin }
28791d073b1dSJohn Baldwin 
28809b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
28819b937d48SNate Lawson void
28829b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
28839b937d48SNate Lawson {
28849b937d48SNate Lawson     char		notify_buf[16];
28859b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
28869b937d48SNate Lawson     ACPI_STATUS		status;
28879b937d48SNate Lawson 
28889b937d48SNate Lawson     if (subsystem == NULL)
28899b937d48SNate Lawson 	return;
28909b937d48SNate Lawson 
28919b937d48SNate Lawson     handle_buf.Pointer = NULL;
28929b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
28939b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
28949b937d48SNate Lawson     if (ACPI_FAILURE(status))
28959b937d48SNate Lawson 	return;
28969b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
28979b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
28989b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
28999b937d48SNate Lawson }
29009b937d48SNate Lawson 
290115e32d5dSMike Smith #ifdef ACPI_DEBUG
29020ae55423SMike Smith /*
29030ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
29040ae55423SMike Smith  *
29050ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
29060ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
29070ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
29080ae55423SMike Smith  * prefixing the bit name with !.
29090ae55423SMike Smith  */
291015e32d5dSMike Smith struct debugtag
291115e32d5dSMike Smith {
291215e32d5dSMike Smith     char	*name;
291315e32d5dSMike Smith     UINT32	value;
291415e32d5dSMike Smith };
291515e32d5dSMike Smith 
291615e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2917c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2918c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2919c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2920c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2921c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2922c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2923c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2924c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2925c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2926d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
29274c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2928d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2929297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
29304c1cdee6SMike Smith 
2931c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2932c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
29333184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2934c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
29353184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
29363184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
29373184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
29384c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2939cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
29403184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2941b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
294215e32d5dSMike Smith     {NULL, 0}
294315e32d5dSMike Smith };
294415e32d5dSMike Smith 
294515e32d5dSMike Smith static struct debugtag dbg_level[] = {
29464c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
29479501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
29489501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
29494c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
29509501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
29514c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2952d62ab2f4SMitsuru IWASAKI 
2953d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2954297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
29554c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
29564c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2957d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
29584c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
29594c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
29604c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
29614c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
29624c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
29634c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
29644c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
29654c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
29664c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
29674c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2968d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2969d62ab2f4SMitsuru IWASAKI 
2970d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2971d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2972d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2973d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2974d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
29754c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2976d62ab2f4SMitsuru IWASAKI 
2977d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2978d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2979d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2980d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2981d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2982d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2983d62ab2f4SMitsuru IWASAKI 
2984d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
298598479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
298698479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
298798479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
298898479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
298998479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
299015e32d5dSMike Smith     {NULL, 0}
299115e32d5dSMike Smith };
299215e32d5dSMike Smith 
299315e32d5dSMike Smith static void
299415e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
299515e32d5dSMike Smith {
299615e32d5dSMike Smith     char	*ep;
299715e32d5dSMike Smith     int		i, l;
29980ae55423SMike Smith     int		set;
299915e32d5dSMike Smith 
300015e32d5dSMike Smith     while (*cp) {
300115e32d5dSMike Smith 	if (isspace(*cp)) {
300215e32d5dSMike Smith 	    cp++;
300315e32d5dSMike Smith 	    continue;
300415e32d5dSMike Smith 	}
300515e32d5dSMike Smith 	ep = cp;
300615e32d5dSMike Smith 	while (*ep && !isspace(*ep))
300715e32d5dSMike Smith 	    ep++;
30080ae55423SMike Smith 	if (*cp == '!') {
30090ae55423SMike Smith 	    set = 0;
30100ae55423SMike Smith 	    cp++;
30110ae55423SMike Smith 	    if (cp == ep)
30120ae55423SMike Smith 		continue;
30130ae55423SMike Smith 	} else {
30140ae55423SMike Smith 	    set = 1;
30150ae55423SMike Smith 	}
301615e32d5dSMike Smith 	l = ep - cp;
301715e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
301815e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
3019be2b1797SNate Lawson 		if (set)
302015e32d5dSMike Smith 		    *flag |= tag[i].value;
3021be2b1797SNate Lawson 		else
30220ae55423SMike Smith 		    *flag &= ~tag[i].value;
302315e32d5dSMike Smith 	    }
302415e32d5dSMike Smith 	}
302515e32d5dSMike Smith 	cp = ep;
302615e32d5dSMike Smith     }
302715e32d5dSMike Smith }
302815e32d5dSMike Smith 
302915e32d5dSMike Smith static void
30302a4ac806SMike Smith acpi_set_debugging(void *junk)
303115e32d5dSMike Smith {
30327e639165SNate Lawson     char	*layer, *level;
303315e32d5dSMike Smith 
30341d7b121cSNate Lawson     if (cold) {
30350ae55423SMike Smith 	AcpiDbgLayer = 0;
30360ae55423SMike Smith 	AcpiDbgLevel = 0;
30371d7b121cSNate Lawson     }
30381d7b121cSNate Lawson 
30397e639165SNate Lawson     layer = getenv("debug.acpi.layer");
30407e639165SNate Lawson     level = getenv("debug.acpi.level");
30417e639165SNate Lawson     if (layer == NULL && level == NULL)
30427e639165SNate Lawson 	return;
30430ae55423SMike Smith 
30447e639165SNate Lawson     printf("ACPI set debug");
30457e639165SNate Lawson     if (layer != NULL) {
30467e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
30477e639165SNate Lawson 	    printf(" layer '%s'", layer);
30487e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
30497e639165SNate Lawson 	freeenv(layer);
30501d7b121cSNate Lawson     }
30517e639165SNate Lawson     if (level != NULL) {
30527e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
30537e639165SNate Lawson 	    printf(" level '%s'", level);
30547e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
30557e639165SNate Lawson 	freeenv(level);
30567e639165SNate Lawson     }
30577e639165SNate Lawson     printf("\n");
305815e32d5dSMike Smith }
3059bee4aa4aSNate Lawson 
3060be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3061be2b1797SNate Lawson 	NULL);
30621d7b121cSNate Lawson 
30631d7b121cSNate Lawson static int
30641d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
30651d7b121cSNate Lawson {
306654f6bca0SNate Lawson     int		 error, *dbg;
30671d7b121cSNate Lawson     struct	 debugtag *tag;
306854f6bca0SNate Lawson     struct	 sbuf sb;
30691d7b121cSNate Lawson 
307054f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
307154f6bca0SNate Lawson 	return (ENOMEM);
30721d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
30731d7b121cSNate Lawson 	tag = &dbg_layer[0];
30741d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
30751d7b121cSNate Lawson     } else {
30761d7b121cSNate Lawson 	tag = &dbg_level[0];
30771d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
30781d7b121cSNate Lawson     }
30791d7b121cSNate Lawson 
30801d7b121cSNate Lawson     /* Get old values if this is a get request. */
308115e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
30821d7b121cSNate Lawson     if (*dbg == 0) {
308354f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
30841d7b121cSNate Lawson     } else if (req->newptr == NULL) {
30851d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
308654f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
308754f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
30881d7b121cSNate Lawson 	}
30891d7b121cSNate Lawson     }
309054f6bca0SNate Lawson     sbuf_trim(&sb);
309154f6bca0SNate Lawson     sbuf_finish(&sb);
30921d7b121cSNate Lawson 
30937e639165SNate Lawson     /* Copy out the old values to the user. */
30947e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
309554f6bca0SNate Lawson     sbuf_delete(&sb);
30961d7b121cSNate Lawson 
30971d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
30981d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
30991d7b121cSNate Lawson 	*dbg = 0;
31001d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
31011d7b121cSNate Lawson 	acpi_set_debugging(NULL);
31021d7b121cSNate Lawson     }
310315e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
31041d7b121cSNate Lawson 
31051d7b121cSNate Lawson     return (error);
31061d7b121cSNate Lawson }
3107bee4aa4aSNate Lawson 
31081d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
31091d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
31101d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
31111d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3112bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3113f9390180SMitsuru IWASAKI 
3114f9390180SMitsuru IWASAKI static int
3115f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
3116f9390180SMitsuru IWASAKI {
3117f9390180SMitsuru IWASAKI 	int	state, acpi_state;
3118f9390180SMitsuru IWASAKI 	int	error;
3119f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
3120f9390180SMitsuru IWASAKI 	va_list	ap;
3121f9390180SMitsuru IWASAKI 
3122f9390180SMitsuru IWASAKI 	error = 0;
3123f9390180SMitsuru IWASAKI 	switch (cmd) {
3124f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
3125f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
3126f9390180SMitsuru IWASAKI 		if (sc == NULL) {
3127f9390180SMitsuru IWASAKI 			error = EINVAL;
3128f9390180SMitsuru IWASAKI 			goto out;
3129f9390180SMitsuru IWASAKI 		}
3130f9390180SMitsuru IWASAKI 
3131f9390180SMitsuru IWASAKI 		va_start(ap, arg);
3132f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
3133f9390180SMitsuru IWASAKI 		va_end(ap);
3134f9390180SMitsuru IWASAKI 
3135f9390180SMitsuru IWASAKI 		switch (state) {
3136f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
3137f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
3138f9390180SMitsuru IWASAKI 			break;
3139f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
3140f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
3141f9390180SMitsuru IWASAKI 			break;
3142f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
3143f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
3144f9390180SMitsuru IWASAKI 			break;
3145f9390180SMitsuru IWASAKI 		default:
3146f9390180SMitsuru IWASAKI 			error = EINVAL;
3147f9390180SMitsuru IWASAKI 			goto out;
3148f9390180SMitsuru IWASAKI 		}
3149f9390180SMitsuru IWASAKI 
3150f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
3151f9390180SMitsuru IWASAKI 		break;
3152f9390180SMitsuru IWASAKI 	default:
3153f9390180SMitsuru IWASAKI 		error = EINVAL;
3154f9390180SMitsuru IWASAKI 		goto out;
3155f9390180SMitsuru IWASAKI 	}
3156f9390180SMitsuru IWASAKI 
3157f9390180SMitsuru IWASAKI out:
3158f9390180SMitsuru IWASAKI 	return (error);
3159f9390180SMitsuru IWASAKI }
3160f9390180SMitsuru IWASAKI 
3161f9390180SMitsuru IWASAKI static void
3162f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
3163f9390180SMitsuru IWASAKI {
3164be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
31656c407052SMitsuru IWASAKI 	return;
3166f9390180SMitsuru IWASAKI 
3167f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3168f9390180SMitsuru IWASAKI }
3169f9390180SMitsuru IWASAKI 
3170f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
3171