xref: /freebsd/sys/dev/acpica/acpi.c (revision c7f88fc386a233b758fd6f7bc52ef4463b51c383)
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  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
38fe12f24bSPoul-Henning Kamp #include <sys/module.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
451611ea87SMitsuru IWASAKI #include <sys/linker.h>
46f9390180SMitsuru IWASAKI #include <sys/power.h>
4754f6bca0SNate Lawson #include <sys/sbuf.h>
48eeb3a05fSNate Lawson #include <sys/smp.h>
4915e32d5dSMike Smith 
5015e32d5dSMike Smith #include <machine/clock.h>
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 
5715e32d5dSMike Smith #include "acpi.h"
5815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5915e32d5dSMike Smith #include <dev/acpica/acpiio.h>
609b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
6115e32d5dSMike Smith 
6215e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6315e32d5dSMike Smith 
64be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
65cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
66b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
670ae55423SMike Smith 
6815e32d5dSMike Smith static d_open_t		acpiopen;
6915e32d5dSMike Smith static d_close_t	acpiclose;
7015e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7115e32d5dSMike Smith 
7215e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
73dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
74dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
757ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
767ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
777ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
787ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
7915e32d5dSMike Smith };
8015e32d5dSMike Smith 
81346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
82cb9b0d80SMike Smith struct mtx	acpi_mutex;
83cb9b0d80SMike Smith 
8489fb5af8SNate Lawson /* Bitmap of device quirks. */
8589fb5af8SNate Lawson int		acpi_quirks;
8689fb5af8SNate Lawson 
8791233413SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
8891233413SNate Lawson struct rman	acpi_rman_io, acpi_rman_mem;
8991233413SNate Lawson 
906d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9115e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9215e32d5dSMike Smith static int	acpi_probe(device_t dev);
9315e32d5dSMike Smith static int	acpi_attach(device_t dev);
94169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
95be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
96be2b1797SNate Lawson 			int unit);
9715e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
98be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
99be2b1797SNate Lawson 			uintptr_t *result);
100be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
101be2b1797SNate Lawson 			uintptr_t value);
10291233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
103be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
104be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
105be2b1797SNate Lawson 			u_long count, u_int flags);
106be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
107be2b1797SNate Lawson 			int rid, struct resource *r);
1081e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1091e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
110ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
111ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
112ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
113ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
114df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
115df8d2a32SNate Lawson 		    void *context, void **retval);
116df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
117df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
118be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
119be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
12015e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
121b82ca9a9SNate Lawson static int	acpi_probe_order(ACPI_HANDLE handle, int *order);
122be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
123be2b1797SNate Lawson 		    void *context, void **status);
124ce619b2eSNate Lawson static BOOLEAN	acpi_MatchHid(ACPI_HANDLE h, const char *hid);
12515e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
12613d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
127d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
128d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
129d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
13088a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
13188a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
13215e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13315e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
134d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1351d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
136f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
137879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
138879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
139879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
140879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
141879d6c23STakanori Watanabe 
14215e32d5dSMike Smith static device_method_t acpi_methods[] = {
14315e32d5dSMike Smith     /* Device interface */
14415e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
14515e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
14615e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
147169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
14856a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
14915e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
15015e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
15115e32d5dSMike Smith 
15215e32d5dSMike Smith     /* Bus interface */
15315e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
15415e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
15515e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
15615e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
15791233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
15891233413SNate Lawson     DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
15991233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
16015e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
16115e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
162879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
163879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
16415e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
16515e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
16615e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
16715e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
16815e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
16915e32d5dSMike Smith 
170ce619b2eSNate Lawson     /* ACPI bus */
171ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
172ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
173df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
174ce619b2eSNate Lawson 
175bc0f2195SMike Smith     /* ISA emulation */
176bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
177bc0f2195SMike Smith 
17815e32d5dSMike Smith     {0, 0}
17915e32d5dSMike Smith };
18015e32d5dSMike Smith 
18115e32d5dSMike Smith static driver_t acpi_driver = {
18215e32d5dSMike Smith     "acpi",
18315e32d5dSMike Smith     acpi_methods,
18415e32d5dSMike Smith     sizeof(struct acpi_softc),
18515e32d5dSMike Smith };
18615e32d5dSMike Smith 
1873273b005SMike Smith static devclass_t acpi_devclass;
1886d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
189a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
19015e32d5dSMike Smith 
191bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
192bee4aa4aSNate Lawson 
193865b8d0bSNate Lawson static const char* sleep_state_names[] = {
194865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
195865b8d0bSNate Lawson 
1961d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1971d7b121cSNate Lawson static char acpi_ca_version[12];
1981d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1991d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
20015e32d5dSMike Smith 
20115e32d5dSMike Smith /*
202413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
203c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
204c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
205c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
206c9b8d77dSNate Lawson  * some IBMs use such code.
207413081d7SNate Lawson  */
208c9b8d77dSNate Lawson static int acpi_serialize_methods;
209413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
210413081d7SNate Lawson 
211c9b8d77dSNate Lawson /*
2126d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2136d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
214be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
215be2b1797SNate Lawson  * off it.
2166d63101aSMike Smith  */
2176d63101aSMike Smith static int
2186d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2196d63101aSMike Smith {
2206d63101aSMike Smith     switch (event) {
2216d63101aSMike Smith     case MOD_LOAD:
22287b45ed5SMitsuru IWASAKI 	if (!cold) {
22387b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2246d63101aSMike Smith 	    return (EPERM);
22587b45ed5SMitsuru IWASAKI 	}
2266d63101aSMike Smith 	break;
2276d63101aSMike Smith     case MOD_UNLOAD:
228f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2296d63101aSMike Smith 	    return (EBUSY);
230f9390180SMitsuru IWASAKI 	break;
2316d63101aSMike Smith     default:
2326d63101aSMike Smith 	break;
2336d63101aSMike Smith     }
2346d63101aSMike Smith     return (0);
2356d63101aSMike Smith }
2366d63101aSMike Smith 
2376d63101aSMike Smith /*
238bbc2815cSJohn Baldwin  * Perform early initialization.
23915e32d5dSMike Smith  */
240bbc2815cSJohn Baldwin ACPI_STATUS
241bbc2815cSJohn Baldwin acpi_Startup(void)
24215e32d5dSMike Smith {
24389fb5af8SNate Lawson     static int started = 0;
24489fb5af8SNate Lawson     int error, val;
24515e32d5dSMike Smith 
2461b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2471b8c233dSPeter Pentchev 
248bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
249bbc2815cSJohn Baldwin     if (started)
25089fb5af8SNate Lawson 	return_VALUE (0);
251bbc2815cSJohn Baldwin     started = 1;
25215e32d5dSMike Smith 
253be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2546008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
255cb9b0d80SMike Smith 
256413081d7SNate Lawson     /*
257413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
258f3fc4f8bSNate Lawson      * uses UINT8 for some values and we have no tunable_byte.
259413081d7SNate Lawson      */
260f3fc4f8bSNate Lawson     AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods;
261413081d7SNate Lawson 
262be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
263b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
264bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
265bbc2815cSJohn Baldwin 	return_VALUE (error);
26615e32d5dSMike Smith     }
2671611ea87SMitsuru IWASAKI 
268b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
269bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
27089fb5af8SNate Lawson 	AcpiTerminate();
271bbc2815cSJohn Baldwin 	return_VALUE (error);
27215e32d5dSMike Smith     }
2733184cf5aSNate Lawson 
27489fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
27589fb5af8SNate Lawson     acpi_table_quirks(&acpi_quirks);
27689fb5af8SNate Lawson 
27789fb5af8SNate Lawson     /* If the user manually set the disabled hint to 0, override any quirk. */
27889fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
27989fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
28089fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
28189fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
28289fb5af8SNate Lawson 	AcpiTerminate();
2834e376d58SNate Lawson 	return_VALUE (AE_ERROR);
28489fb5af8SNate Lawson     }
2853184cf5aSNate Lawson 
286bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
287bbc2815cSJohn Baldwin }
288bbc2815cSJohn Baldwin 
289bbc2815cSJohn Baldwin /*
290bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
291bbc2815cSJohn Baldwin  */
292bbc2815cSJohn Baldwin static void
293bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
294bbc2815cSJohn Baldwin {
295bbc2815cSJohn Baldwin     device_t	child;
296bbc2815cSJohn Baldwin 
297bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
298bbc2815cSJohn Baldwin 
299bbc2815cSJohn Baldwin     if (!cold)
300bbc2815cSJohn Baldwin 	return_VOID;
301bbc2815cSJohn Baldwin 
302bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
303bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
304bbc2815cSJohn Baldwin 	return_VOID;
305bbc2815cSJohn Baldwin 
306bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
307bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
308bbc2815cSJohn Baldwin 	return_VOID;
309bbc2815cSJohn Baldwin 
310bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
311bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
312bbc2815cSJohn Baldwin 	return_VOID;
31315e32d5dSMike Smith 
3144e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3154e376d58SNate Lawson 
316be2b1797SNate Lawson     /* Attach the actual ACPI device. */
31715e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
318bee4aa4aSNate Lawson 	device_printf(parent, "device_identify failed\n");
3190ae55423SMike Smith 	return_VOID;
32015e32d5dSMike Smith     }
32115e32d5dSMike Smith }
32215e32d5dSMike Smith 
32315e32d5dSMike Smith /*
324bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
32515e32d5dSMike Smith  */
32615e32d5dSMike Smith static int
32715e32d5dSMike Smith acpi_probe(device_t dev)
32815e32d5dSMike Smith {
32915e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
33015e32d5dSMike Smith     char		buf[20];
33115e32d5dSMike Smith     int			error;
3322ccd1cacSNate Lawson     struct sbuf		sb;
3332ccd1cacSNate Lawson     ACPI_STATUS		status;
33415e32d5dSMike Smith 
335b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
336f9390180SMitsuru IWASAKI 
337f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
338f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
339bee4aa4aSNate Lawson 	device_printf(dev, "probe failed, other PM system enabled.\n");
340f9390180SMitsuru IWASAKI 	return_VALUE (ENXIO);
341f9390180SMitsuru IWASAKI     }
342f9390180SMitsuru IWASAKI 
343b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
344be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
345be2b1797SNate Lawson 		      AcpiFormatException(status));
346cb9b0d80SMike Smith 	error = ENXIO;
347cb9b0d80SMike Smith     } else {
3482ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3492ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3502ccd1cacSNate Lawson 	sbuf_trim(&sb);
3512ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3522ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3532ccd1cacSNate Lawson 	sbuf_trim(&sb);
3542ccd1cacSNate Lawson 	sbuf_finish(&sb);
3552ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3562ccd1cacSNate Lawson 	sbuf_delete(&sb);
357cb9b0d80SMike Smith 	error = 0;
358cb9b0d80SMike Smith     }
359bee4aa4aSNate Lawson 
360cb9b0d80SMike Smith     return_VALUE (error);
36115e32d5dSMike Smith }
36215e32d5dSMike Smith 
36315e32d5dSMike Smith static int
36415e32d5dSMike Smith acpi_attach(device_t dev)
36515e32d5dSMike Smith {
36615e32d5dSMike Smith     struct acpi_softc	*sc;
367cb9b0d80SMike Smith     ACPI_STATUS		status;
368ea27c63eSNate Lawson     int			error, state;
36932d18aa5SMike Smith     UINT32		flags;
370ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
371498d464fSMitsuru IWASAKI     char		*env;
37215e32d5dSMike Smith 
373b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
374bee4aa4aSNate Lawson 
37515e32d5dSMike Smith     sc = device_get_softc(dev);
37615e32d5dSMike Smith     sc->acpi_dev = dev;
37715e32d5dSMike Smith 
37891233413SNate Lawson     /* Initialize resource manager. */
37991233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
38091233413SNate Lawson     acpi_rman_io.rm_start = 0;
38191233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
38291233413SNate Lawson     acpi_rman_io.rm_descr = "I/O ports";
38391233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
38491233413SNate Lawson 	panic("acpi rman_init IO ports failed");
38591233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
38691233413SNate Lawson     acpi_rman_mem.rm_start = 0;
38791233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
38891233413SNate Lawson     acpi_rman_mem.rm_descr = "I/O memory addresses";
38991233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
39091233413SNate Lawson 	panic("acpi rman_init memory failed");
39191233413SNate Lawson 
392be2b1797SNate Lawson     /* Install the default address space handlers. */
393cb9b0d80SMike Smith     error = ENXIO;
394be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
395be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
396be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
397be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
398be2b1797SNate Lawson 		      AcpiFormatException(status));
399cb9b0d80SMike Smith 	goto out;
40015e32d5dSMike Smith     }
401be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
402be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
403be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
404be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
405be2b1797SNate Lawson 		      AcpiFormatException(status));
406cb9b0d80SMike Smith 	goto out;
40715e32d5dSMike Smith     }
408be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
409be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
410be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
411be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
412be2b1797SNate Lawson 		      AcpiFormatException(status));
413cb9b0d80SMike Smith 	goto out;
41415e32d5dSMike Smith     }
41515e32d5dSMike Smith 
41615e32d5dSMike Smith     /*
417be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
418be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
419be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
420be2b1797SNate Lawson      * object init pass.
42115e32d5dSMike Smith      *
42232d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
42332d18aa5SMike Smith      *
424be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
425be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
42615e32d5dSMike Smith      */
42732d18aa5SMike Smith     flags = 0;
428d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
42932d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
430bee4aa4aSNate Lawson 
431bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
432b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
433be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
434be2b1797SNate Lawson 		      AcpiFormatException(status));
435cb9b0d80SMike Smith 	goto out;
43615e32d5dSMike Smith     }
43715e32d5dSMike Smith 
438f8335e3aSNate Lawson     /*
439f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
440f8335e3aSNate Lawson      * the namespace has been evaluated.
441f8335e3aSNate Lawson      */
442f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
443f8335e3aSNate Lawson 
444bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
445b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
446be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
447be2b1797SNate Lawson 		      AcpiFormatException(status));
448b69ed3f4SMitsuru IWASAKI 	goto out;
449b69ed3f4SMitsuru IWASAKI     }
450b69ed3f4SMitsuru IWASAKI 
45115e32d5dSMike Smith     /*
4521d073b1dSJohn Baldwin      * Setup our sysctl tree.
4531d073b1dSJohn Baldwin      *
4541d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4551d073b1dSJohn Baldwin      */
4561d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4571d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4581d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4591d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4601d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
461d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
462d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
463d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4641d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4651d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4661d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4671d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4681d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4691d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4701d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4711d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
472f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
473f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
474f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
475f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
476f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
477f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4782d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
479ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
480ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
481ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4821611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4831611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4841611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4852d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4862d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
487bf0c18ecSNate Lawson 
488bf0c18ecSNate Lawson     /*
4892b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
490bf0c18ecSNate Lawson      * stabilize.
491bf0c18ecSNate Lawson      */
4922b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
4932d644607SMitsuru IWASAKI     if (bootverbose)
4942d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
495498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
496498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
497498d464fSMitsuru IWASAKI 	freeenv(env);
498498d464fSMitsuru IWASAKI     }
4991d073b1dSJohn Baldwin 
5002d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5012d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5022d610c46SNate Lawson 	sc->acpi_s4bios = 1;
5032d610c46SNate Lawson 
5041d073b1dSJohn Baldwin     /*
505ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
506ea27c63eSNate Lawson      * to NONE by default to avoid surprising users.
50715e32d5dSMike Smith      */
508ea27c63eSNate Lawson     sc->acpi_power_button_sx = ACPI_STATE_S5;
509ea27c63eSNate Lawson     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
510f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
511f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
51215e32d5dSMike Smith 
513ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
514ea27c63eSNate Lawson     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
515ea27c63eSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
516ea27c63eSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
517ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
518ea27c63eSNate Lawson 	    break;
519ea27c63eSNate Lawson 	}
520ea27c63eSNate Lawson 
52113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
52215e32d5dSMike Smith 
52315e32d5dSMike Smith     /*
52415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
52515e32d5dSMike Smith      */
52615e32d5dSMike Smith 
52759e472e9SNate Lawson     /* Register our shutdown handler. */
528be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
529be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
53015e32d5dSMike Smith 
53115e32d5dSMike Smith     /*
53215e32d5dSMike Smith      * Register our acpi event handlers.
53315e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
53415e32d5dSMike Smith      */
535be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
536be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
537be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
538be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
53915e32d5dSMike Smith 
540be2b1797SNate Lawson     /* Flag our initial states. */
54115e32d5dSMike Smith     sc->acpi_enabled = 1;
54215e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
543ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
54415e32d5dSMike Smith 
545be2b1797SNate Lawson     /* Create the control device */
546a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5472a4689e8SRobert Watson 			      "acpi");
54815e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
54915e32d5dSMike Smith 
55091da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
551be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
552c573e654SMitsuru IWASAKI 	goto out;
553c573e654SMitsuru IWASAKI #endif
554c573e654SMitsuru IWASAKI 
555be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
556f86214b6SMitsuru IWASAKI 	goto out;
557f86214b6SMitsuru IWASAKI 
558f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
559f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
560f9390180SMitsuru IWASAKI 
561eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
562eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
563eeb6dba2SJohn Baldwin 
564cb9b0d80SMike Smith     error = 0;
565cb9b0d80SMike Smith 
566cb9b0d80SMike Smith  out:
567cb9b0d80SMike Smith     return_VALUE (error);
56815e32d5dSMike Smith }
56915e32d5dSMike Smith 
570169b539aSNate Lawson static int
571169b539aSNate Lawson acpi_shutdown(device_t dev)
572169b539aSNate Lawson {
573169b539aSNate Lawson 
5740e414868SNate Lawson     /* Allow children to shutdown first. */
5750e414868SNate Lawson     bus_generic_shutdown(dev);
5760e414868SNate Lawson 
577bee4aa4aSNate Lawson     /*
578bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
579bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
580bee4aa4aSNate Lawson      */
581d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
582d4b9ff91SNate Lawson 
583169b539aSNate Lawson     return (0);
584169b539aSNate Lawson }
585169b539aSNate Lawson 
58615e32d5dSMike Smith /*
58715e32d5dSMike Smith  * Handle a new device being added
58815e32d5dSMike Smith  */
58915e32d5dSMike Smith static device_t
59015e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
59115e32d5dSMike Smith {
59215e32d5dSMike Smith     struct acpi_device	*ad;
59315e32d5dSMike Smith     device_t		child;
59415e32d5dSMike Smith 
595be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
59615e32d5dSMike Smith 	return (NULL);
59715e32d5dSMike Smith 
59815e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
59915e32d5dSMike Smith 
60015e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
60115e32d5dSMike Smith     if (child != NULL)
60215e32d5dSMike Smith 	device_set_ivars(child, ad);
60315e32d5dSMike Smith     return (child);
60415e32d5dSMike Smith }
60515e32d5dSMike Smith 
60615e32d5dSMike Smith static int
60715e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
60815e32d5dSMike Smith {
60915e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
61015e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
61115e32d5dSMike Smith     int retval = 0;
61215e32d5dSMike Smith 
61315e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
6149ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
6159ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
6169ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
6179ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
61815e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
61915e32d5dSMike Smith 
62015e32d5dSMike Smith     return (retval);
62115e32d5dSMike Smith }
62215e32d5dSMike Smith 
62363600cc3SNate Lawson /* Location hint for devctl(8) */
62463600cc3SNate Lawson static int
625247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
626247648afSTakanori Watanabe     size_t buflen)
627247648afSTakanori Watanabe {
628247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
629247648afSTakanori Watanabe 
630247648afSTakanori Watanabe     if (dinfo->ad_handle)
631d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
632247648afSTakanori Watanabe     else
633d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
634247648afSTakanori Watanabe     return (0);
635247648afSTakanori Watanabe }
636247648afSTakanori Watanabe 
63763600cc3SNate Lawson /* PnP information for devctl(8) */
63863600cc3SNate Lawson static int
639247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
640247648afSTakanori Watanabe     size_t buflen)
641247648afSTakanori Watanabe {
642247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
64363600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
64463600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
645247648afSTakanori Watanabe     char *end;
646247648afSTakanori Watanabe     int error;
647247648afSTakanori Watanabe 
648247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
649247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
650247648afSTakanori Watanabe     if (error)
651d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
652247648afSTakanori Watanabe     else
653247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
654247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
655d40c0f53SNate Lawson 		 adinfo->HardwareId.Value : "none",
65663600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
65763600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
658247648afSTakanori Watanabe     if (adinfo)
659247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
660247648afSTakanori Watanabe 
661247648afSTakanori Watanabe     return (0);
662247648afSTakanori Watanabe }
663247648afSTakanori Watanabe 
664247648afSTakanori Watanabe /*
66515e32d5dSMike Smith  * Handle per-device ivars
66615e32d5dSMike Smith  */
66715e32d5dSMike Smith static int
66815e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
66915e32d5dSMike Smith {
67015e32d5dSMike Smith     struct acpi_device	*ad;
67115e32d5dSMike Smith 
67215e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
67315e32d5dSMike Smith 	printf("device has no ivars\n");
67415e32d5dSMike Smith 	return (ENOENT);
67515e32d5dSMike Smith     }
67615e32d5dSMike Smith 
677be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
67815e32d5dSMike Smith     switch(index) {
67915e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
68015e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
68115e32d5dSMike Smith 	break;
68215e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
68315e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
68415e32d5dSMike Smith 	break;
68515e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
68615e32d5dSMike Smith 	*(void **)result = ad->ad_private;
68715e32d5dSMike Smith 	break;
688d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
689d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
690d4b9ff91SNate Lawson 	break;
69132d18aa5SMike Smith     case ISA_IVAR_VENDORID:
69232d18aa5SMike Smith     case ISA_IVAR_SERIAL:
69332d18aa5SMike Smith     case ISA_IVAR_COMPATID:
69432d18aa5SMike Smith 	*(int *)result = -1;
69532d18aa5SMike Smith 	break;
69632d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
69732d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
69832d18aa5SMike Smith 	break;
69915e32d5dSMike Smith     default:
70015e32d5dSMike Smith 	return (ENOENT);
70115e32d5dSMike Smith     }
702be2b1797SNate Lawson 
70315e32d5dSMike Smith     return (0);
70415e32d5dSMike Smith }
70515e32d5dSMike Smith 
70615e32d5dSMike Smith static int
70715e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
70815e32d5dSMike Smith {
70915e32d5dSMike Smith     struct acpi_device	*ad;
71015e32d5dSMike Smith 
71115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
71215e32d5dSMike Smith 	printf("device has no ivars\n");
71315e32d5dSMike Smith 	return (ENOENT);
71415e32d5dSMike Smith     }
71515e32d5dSMike Smith 
71615e32d5dSMike Smith     switch(index) {
71715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
71815e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
71915e32d5dSMike Smith 	break;
72015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
72115e32d5dSMike Smith 	ad->ad_magic = (int)value;
72215e32d5dSMike Smith 	break;
72315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
72415e32d5dSMike Smith 	ad->ad_private = (void *)value;
72515e32d5dSMike Smith 	break;
726d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
727d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
728d4b9ff91SNate Lawson 	break;
72915e32d5dSMike Smith     default:
7302ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
73115e32d5dSMike Smith 	return (ENOENT);
73215e32d5dSMike Smith     }
733be2b1797SNate Lawson 
73415e32d5dSMike Smith     return (0);
73515e32d5dSMike Smith }
73615e32d5dSMike Smith 
73715e32d5dSMike Smith /*
73815e32d5dSMike Smith  * Handle child resource allocation/removal
73915e32d5dSMike Smith  */
74091233413SNate Lawson static struct resource_list *
74191233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
74215e32d5dSMike Smith {
74391233413SNate Lawson     struct acpi_device		*ad;
74415e32d5dSMike Smith 
74591233413SNate Lawson     ad = device_get_ivars(child);
74691233413SNate Lawson     return (&ad->ad_rl);
74715e32d5dSMike Smith }
74815e32d5dSMike Smith 
74915e32d5dSMike Smith static struct resource *
75015e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
75115e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
75215e32d5dSMike Smith {
75395957f62SJohn Baldwin     ACPI_RESOURCE ares;
75415e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
75515e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
75691233413SNate Lawson     struct resource_list_entry *rle;
75791233413SNate Lawson     struct resource *res;
75891233413SNate Lawson     struct rman *rm;
75915e32d5dSMike Smith 
76091233413SNate Lawson     /*
76191233413SNate Lawson      * If this is an allocation of the "default" range for a given RID, and
76291233413SNate Lawson      * we know what the resources for this device are (i.e., they're on the
76391233413SNate Lawson      * child's resource list), use those start/end values.
76491233413SNate Lawson      */
76591233413SNate Lawson     if (start == 0UL && end == ~0UL) {
76691233413SNate Lawson 	rle = resource_list_find(rl, type, *rid);
76791233413SNate Lawson 	if (rle == NULL)
76891233413SNate Lawson 	    return (NULL);
76991233413SNate Lawson 	start = rle->start;
77091233413SNate Lawson 	end = rle->end;
77191233413SNate Lawson 	count = rle->count;
77291233413SNate Lawson     }
77391233413SNate Lawson 
77491233413SNate Lawson     /* If we don't manage this address, pass the request up to the parent. */
77591233413SNate Lawson     rle = acpi_sysres_find(type, start);
77691233413SNate Lawson     if (rle == NULL) {
77795957f62SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
77895957f62SJohn Baldwin 	    start, end, count, flags);
77995957f62SJohn Baldwin     } else {
78091233413SNate Lawson 
78191233413SNate Lawson 	/* We only handle memory and IO resources through rman. */
78291233413SNate Lawson 	switch (type) {
78391233413SNate Lawson 	case SYS_RES_IOPORT:
78491233413SNate Lawson 	    rm = &acpi_rman_io;
78591233413SNate Lawson 	    break;
78691233413SNate Lawson 	case SYS_RES_MEMORY:
78791233413SNate Lawson 	    rm = &acpi_rman_mem;
78891233413SNate Lawson 	    break;
78991233413SNate Lawson 	default:
79091233413SNate Lawson 	    panic("acpi_alloc_resource: invalid res type %d", type);
79191233413SNate Lawson 	}
79291233413SNate Lawson 
79391233413SNate Lawson 	/* If we do know it, allocate it from the local pool. */
79495957f62SJohn Baldwin 	res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
79595957f62SJohn Baldwin 	    child);
79691233413SNate Lawson 	if (res == NULL)
79791233413SNate Lawson 	    return (NULL);
79891233413SNate Lawson 
799a3236570SWarner Losh 	/* Copy the bus tag and handle from the pre-allocated resource. */
80091233413SNate Lawson 	rman_set_bustag(res, rman_get_bustag(rle->res));
801ed67d9deSWarner Losh 	rman_set_bushandle(res, rman_get_start(res));
80291233413SNate Lawson 
80391233413SNate Lawson 	/* If requested, activate the resource using the parent's method. */
80495957f62SJohn Baldwin 	if (flags & RF_ACTIVE)
80591233413SNate Lawson 	    if (bus_activate_resource(child, type, *rid, res) != 0) {
80691233413SNate Lawson 		rman_release_resource(res);
80791233413SNate Lawson 		return (NULL);
80891233413SNate Lawson 	    }
80995957f62SJohn Baldwin     }
81091233413SNate Lawson 
81195957f62SJohn Baldwin     if (res != NULL && device_get_parent(child) == bus)
81295957f62SJohn Baldwin 	switch (type) {
81395957f62SJohn Baldwin 	case SYS_RES_IRQ:
81495957f62SJohn Baldwin 	    /*
81595957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
81695957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
81795957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
81895957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
81995957f62SJohn Baldwin 	     *
82095957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
82195957f62SJohn Baldwin 	     */
82295957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
82395957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
82495957f62SJohn Baldwin 	    break;
82595957f62SJohn Baldwin 	}
82691233413SNate Lawson     return (res);
82715e32d5dSMike Smith }
82815e32d5dSMike Smith 
82915e32d5dSMike Smith static int
83091233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid,
83191233413SNate Lawson     struct resource *r)
83215e32d5dSMike Smith {
83391233413SNate Lawson     int ret;
83415e32d5dSMike Smith 
83591233413SNate Lawson     /*
83691233413SNate Lawson      * If we know about this address, deactivate it and release it to the
83791233413SNate Lawson      * local pool.  If we don't, pass this request up to the parent.
83891233413SNate Lawson      */
83991233413SNate Lawson     if (acpi_sysres_find(type, rman_get_start(r)) == NULL) {
84091233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
84191233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
84291233413SNate Lawson 	    if (ret != 0)
84391233413SNate Lawson 		return (ret);
84491233413SNate Lawson 	}
84591233413SNate Lawson 	ret = rman_release_resource(r);
84691233413SNate Lawson     } else
84791233413SNate Lawson 	ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
84891233413SNate Lawson 
84991233413SNate Lawson     return (ret);
85015e32d5dSMike Smith }
85115e32d5dSMike Smith 
852dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
853dc750869SNate Lawson struct resource *
854dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
855dc750869SNate Lawson {
856dc750869SNate Lawson     int type;
857dc750869SNate Lawson 
858dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
859dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
860dc750869SNate Lawson 	return (NULL);
861dc750869SNate Lawson 
862dc750869SNate Lawson     switch (gas->AddressSpaceId) {
863dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
864dc750869SNate Lawson 	type = SYS_RES_MEMORY;
865dc750869SNate Lawson 	break;
866dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
867dc750869SNate Lawson 	type = SYS_RES_IOPORT;
868dc750869SNate Lawson 	break;
869dc750869SNate Lawson     default:
870dc750869SNate Lawson 	return (NULL);
871dc750869SNate Lawson     }
872dc750869SNate Lawson 
873dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
8745f96beb9SNate Lawson     return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
875dc750869SNate Lawson }
876dc750869SNate Lawson 
877c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
8781e4925e8SNate Lawson static uint32_t
87932d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
880bc0f2195SMike Smith {
8811e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
8821e4925e8SNate Lawson     ACPI_BUFFER		buf;
883bc0f2195SMike Smith     ACPI_HANDLE		h;
884bc0f2195SMike Smith     ACPI_STATUS		error;
885bc0f2195SMike Smith     u_int32_t		pnpid;
88632d18aa5SMike Smith 
887b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
88832d18aa5SMike Smith 
88932d18aa5SMike Smith     pnpid = 0;
890bd189fe7SNate Lawson     buf.Pointer = NULL;
891bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
892bd189fe7SNate Lawson 
8936fca9360SNate Lawson     /* Fetch and validate the HID. */
89432d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
895bd189fe7SNate Lawson 	goto out;
8966fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
8976fca9360SNate Lawson     if (ACPI_FAILURE(error))
898bd189fe7SNate Lawson 	goto out;
8991e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
90032d18aa5SMike Smith 
9011e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
9021e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
903be2b1797SNate Lawson 
904bd189fe7SNate Lawson out:
905bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9061e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
90732d18aa5SMike Smith     return_VALUE (pnpid);
90832d18aa5SMike Smith }
90932d18aa5SMike Smith 
9101e4925e8SNate Lawson static int
9111e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
912b2566207STakanori Watanabe {
9131e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9141e4925e8SNate Lawson     ACPI_BUFFER		buf;
915b2566207STakanori Watanabe     ACPI_HANDLE		h;
916b2566207STakanori Watanabe     ACPI_STATUS		error;
9171e4925e8SNate Lawson     uint32_t		*pnpid;
9181e4925e8SNate Lawson     int			valid, i;
919b2566207STakanori Watanabe 
920b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
921b2566207STakanori Watanabe 
9221e4925e8SNate Lawson     pnpid = cids;
9231e4925e8SNate Lawson     valid = 0;
924bd189fe7SNate Lawson     buf.Pointer = NULL;
925bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
926bd189fe7SNate Lawson 
9271e4925e8SNate Lawson     /* Fetch and validate the CID */
928b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
929bd189fe7SNate Lawson 	goto out;
9301e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9311e4925e8SNate Lawson     if (ACPI_FAILURE(error))
932bd189fe7SNate Lawson 	goto out;
9331e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
9341e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
935b2566207STakanori Watanabe 	goto out;
936b2566207STakanori Watanabe 
9371e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
9381e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
9391e4925e8SNate Lawson     for (i = 0; i < count; i++) {
9401e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9411e4925e8SNate Lawson 	    continue;
9421e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9431e4925e8SNate Lawson 	valid++;
944b2566207STakanori Watanabe     }
945b2566207STakanori Watanabe 
9461e4925e8SNate Lawson out:
947bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9481e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
9491e4925e8SNate Lawson     return_VALUE (valid);
9501e4925e8SNate Lawson }
951b2566207STakanori Watanabe 
952ce619b2eSNate Lawson static char *
953ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
954ce619b2eSNate Lawson {
955ce619b2eSNate Lawson     ACPI_HANDLE h;
956ce619b2eSNate Lawson     int i;
957ce619b2eSNate Lawson 
958ce619b2eSNate Lawson     h = acpi_get_handle(dev);
959ce619b2eSNate Lawson     if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
960ce619b2eSNate Lawson 	return (NULL);
961ce619b2eSNate Lawson 
962ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
963ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
964ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
965ce619b2eSNate Lawson 	    return (ids[i]);
966ce619b2eSNate Lawson     }
967ce619b2eSNate Lawson     return (NULL);
968ce619b2eSNate Lawson }
969ce619b2eSNate Lawson 
970ce619b2eSNate Lawson static ACPI_STATUS
971ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
972ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
973ce619b2eSNate Lawson {
974ce619b2eSNate Lawson     ACPI_HANDLE h;
975ce619b2eSNate Lawson 
976df8d2a32SNate Lawson     if (dev == NULL)
977df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
978df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
979ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
980ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
981ce619b2eSNate Lawson }
982ce619b2eSNate Lawson 
983df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
984df8d2a32SNate Lawson struct acpi_device_scan_ctx {
985df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
986df8d2a32SNate Lawson     void		*arg;
987df8d2a32SNate Lawson     ACPI_HANDLE		parent;
988df8d2a32SNate Lawson };
989df8d2a32SNate Lawson 
990ce619b2eSNate Lawson static ACPI_STATUS
991df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
992df8d2a32SNate Lawson {
993df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
994df8d2a32SNate Lawson     device_t dev, old_dev;
995df8d2a32SNate Lawson     ACPI_STATUS status;
996df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
997df8d2a32SNate Lawson 
998df8d2a32SNate Lawson     /*
999df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1000df8d2a32SNate Lawson      * the parent where the scan began.
1001df8d2a32SNate Lawson      */
1002df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1003df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1004df8d2a32SNate Lawson 	return (AE_OK);
1005df8d2a32SNate Lawson 
1006df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1007df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1008df8d2a32SNate Lawson 	return (AE_OK);
1009df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1010df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1011df8d2a32SNate Lawson 	return (AE_OK);
1012df8d2a32SNate Lawson 
1013df8d2a32SNate Lawson     /*
1014df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1015df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1016df8d2a32SNate Lawson      */
1017df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1018df8d2a32SNate Lawson     dev = old_dev;
1019df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1020df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1021df8d2a32SNate Lawson 	return (status);
1022df8d2a32SNate Lawson 
1023df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1024df8d2a32SNate Lawson     if (old_dev != NULL) {
1025df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1026df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1027df8d2a32SNate Lawson     }
1028df8d2a32SNate Lawson 
1029df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1030df8d2a32SNate Lawson     if (dev != NULL)
1031df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1032df8d2a32SNate Lawson 
1033df8d2a32SNate Lawson     return (AE_OK);
1034df8d2a32SNate Lawson }
1035df8d2a32SNate Lawson 
1036df8d2a32SNate Lawson static ACPI_STATUS
1037df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1038df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1039ce619b2eSNate Lawson {
1040ce619b2eSNate Lawson     ACPI_HANDLE h;
1041df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1042ce619b2eSNate Lawson 
1043df8d2a32SNate Lawson     if (acpi_disabled("children"))
1044df8d2a32SNate Lawson 	return (AE_OK);
1045df8d2a32SNate Lawson 
1046df8d2a32SNate Lawson     if (dev == NULL)
1047df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1048df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1049ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1050df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1051df8d2a32SNate Lawson     ctx.arg = arg;
1052df8d2a32SNate Lawson     ctx.parent = h;
1053df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1054df8d2a32SNate Lawson 	acpi_device_scan_cb, &ctx, NULL));
1055ce619b2eSNate Lawson }
1056ce619b2eSNate Lawson 
105732d18aa5SMike Smith static int
105832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
105932d18aa5SMike Smith {
10601e4925e8SNate Lawson     int			result, cid_count, i;
10611e4925e8SNate Lawson     uint32_t		lid, cids[8];
1062bc0f2195SMike Smith 
1063b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1064bc0f2195SMike Smith 
1065bc0f2195SMike Smith     /*
1066bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1067bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1068bc0f2195SMike Smith      * that to happen, so don't ever return it.
1069bc0f2195SMike Smith      */
1070bc0f2195SMike Smith     result = ENXIO;
1071bc0f2195SMike Smith 
1072be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1073b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
10741e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1075bc0f2195SMike Smith     while (ids && ids->ip_id) {
10761e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1077bc0f2195SMike Smith 	    result = 0;
1078bc0f2195SMike Smith 	    goto out;
1079bc0f2195SMike Smith 	}
10801e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
10811e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
10821e4925e8SNate Lawson 		result = 0;
10831e4925e8SNate Lawson 		goto out;
10841e4925e8SNate Lawson 	    }
10851e4925e8SNate Lawson 	}
1086bc0f2195SMike Smith 	ids++;
1087bc0f2195SMike Smith     }
1088be2b1797SNate Lawson 
1089bc0f2195SMike Smith  out:
1090bc0f2195SMike Smith     return_VALUE (result);
1091bc0f2195SMike Smith }
1092bc0f2195SMike Smith 
1093bc0f2195SMike Smith /*
109415e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
109515e32d5dSMike Smith  *
1096be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1097be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
109815e32d5dSMike Smith  */
109915e32d5dSMike Smith static void
110015e32d5dSMike Smith acpi_probe_children(device_t bus)
110115e32d5dSMike Smith {
110215e32d5dSMike Smith     ACPI_HANDLE	parent;
1103be2b1797SNate Lawson     ACPI_STATUS	status;
110415e32d5dSMike Smith     int		i;
110591233413SNate Lawson     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
110615e32d5dSMike Smith 
1107b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
11080ae55423SMike Smith 
110915e32d5dSMike Smith     /*
111015e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1111edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
111215e32d5dSMike Smith      *
111315e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1114be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1115be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1116be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
111715e32d5dSMike Smith      */
11184c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1119be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
1120be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1121be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1122be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1123be2b1797SNate Lawson 			      bus, NULL);
1124be2b1797SNate Lawson 	}
1125be2b1797SNate Lawson     }
112615e32d5dSMike Smith 
1127edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1128edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1129edc13633SNate Lawson     bus_generic_probe(bus);
1130edc13633SNate Lawson 
1131edc13633SNate Lawson     /* Probe/attach all children, created staticly and from the namespace. */
11324c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
113315e32d5dSMike Smith     bus_generic_attach(bus);
113415e32d5dSMike Smith 
113515e32d5dSMike Smith     /*
113615e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
113715e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
113815e32d5dSMike Smith      */
11394c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
114015e32d5dSMike Smith     bus_generic_attach(bus);
11410ae55423SMike Smith 
114288a79fc0SNate Lawson     /* Attach wake sysctls. */
11435c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
114488a79fc0SNate Lawson 
1145bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
11460ae55423SMike Smith     return_VOID;
114715e32d5dSMike Smith }
114815e32d5dSMike Smith 
1149b82ca9a9SNate Lawson /*
1150b82ca9a9SNate Lawson  * Determine the probe order for a given device and return non-zero if it
1151b82ca9a9SNate Lawson  * should be attached immediately.
1152b82ca9a9SNate Lawson  */
115391233413SNate Lawson static int
1154b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
115591233413SNate Lawson {
115691233413SNate Lawson     int ret;
115791233413SNate Lawson 
1158b82ca9a9SNate Lawson     /*
1159b82ca9a9SNate Lawson      * 1. I/O port and memory system resource holders
1160b82ca9a9SNate Lawson      * 2. Embedded controllers (to handle early accesses)
1161b82ca9a9SNate Lawson      */
116291233413SNate Lawson     ret = 0;
116391233413SNate Lawson     if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) {
116491233413SNate Lawson 	*order = 1;
116591233413SNate Lawson 	ret = 1;
1166b82ca9a9SNate Lawson     } else if (acpi_MatchHid(handle, "PNP0C09")) {
116791233413SNate Lawson 	*order = 2;
116891233413SNate Lawson 	ret = 1;
116991233413SNate Lawson     }
117091233413SNate Lawson 
117191233413SNate Lawson     return (ret);
117291233413SNate Lawson }
117391233413SNate Lawson 
117415e32d5dSMike Smith /*
117515e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
117615e32d5dSMike Smith  * it.
117715e32d5dSMike Smith  */
117815e32d5dSMike Smith static ACPI_STATUS
117915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
118015e32d5dSMike Smith {
118115e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
118291233413SNate Lawson     device_t		child, bus;
118391233413SNate Lawson     int			order, probe_now;
118415e32d5dSMike Smith 
1185b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
11860ae55423SMike Smith 
1187be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
11880ae55423SMike Smith     if (acpi_avoid(handle))
11890ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
11900ae55423SMike Smith 
119191233413SNate Lawson     bus = (device_t)context;
1192b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
119315e32d5dSMike Smith 	switch (type) {
119415e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
119515e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
119615e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
119715e32d5dSMike Smith 	case ACPI_TYPE_POWER:
11980ae55423SMike Smith 	    if (acpi_disabled("children"))
11990ae55423SMike Smith 		break;
1200be2b1797SNate Lawson 
120115e32d5dSMike Smith 	    /*
120215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
1203b82ca9a9SNate Lawson 	     * so that the probe/attach passes will run breadth-first.  Orders
1204b82ca9a9SNate Lawson 	     * less than 10 are reserved for special objects (i.e., system
1205b82ca9a9SNate Lawson 	     * resources).  Larger values are used for all other devices.
120615e32d5dSMike Smith 	     */
1207be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1208be2b1797SNate Lawson 			     acpi_name(handle)));
1209b82ca9a9SNate Lawson 	    order = (level + 1) * 10;
1210b82ca9a9SNate Lawson 	    probe_now = acpi_probe_order(handle, &order);
121191233413SNate Lawson 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1212bc0f2195SMike Smith 	    if (child == NULL)
1213bc0f2195SMike Smith 		break;
121459a890e6SNate Lawson 
121559a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
121615e32d5dSMike Smith 	    acpi_set_handle(child, handle);
121759a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1218bc0f2195SMike Smith 
1219bc0f2195SMike Smith 	    /*
1220b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1221b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1222b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1223b519f9d6SMike Smith 	     */
1224be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1225b519f9d6SMike Smith 		device_disable(child);
1226b519f9d6SMike Smith 		break;
1227b519f9d6SMike Smith 	    }
1228b519f9d6SMike Smith 
1229b519f9d6SMike Smith 	    /*
1230bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1231bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1232bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1233bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1234bc0f2195SMike Smith 	     * device not to have any resources.
1235bc0f2195SMike Smith 	     */
123672ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1237bc0f2195SMike Smith 
1238f504b607SNate Lawson 	    /* If order was overridden, probe/attach now rather than later. */
123991233413SNate Lawson 	    if (probe_now)
124091233413SNate Lawson 		device_probe_and_attach(child);
12410ae55423SMike Smith 	    break;
124215e32d5dSMike Smith 	}
124315e32d5dSMike Smith     }
1244be2b1797SNate Lawson 
12450ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
124615e32d5dSMike Smith }
124715e32d5dSMike Smith 
124859a890e6SNate Lawson /*
124959a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
125059a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
125159a890e6SNate Lawson  */
125259a890e6SNate Lawson void
125359a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
125459a890e6SNate Lawson {
125559a890e6SNate Lawson }
125659a890e6SNate Lawson 
125715e32d5dSMike Smith static void
125815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
125915e32d5dSMike Smith {
1260d33f4987STakanori Watanabe     ACPI_STATUS	status;
1261eeb3a05fSNate Lawson 
1262eeb3a05fSNate Lawson     /*
126380f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
126480f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
126580f0e4c2SNate Lawson      * an AP.
1266eeb3a05fSNate Lawson      */
1267eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1268904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1269904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1270904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1271904bf0c2SNate Lawson 		   AcpiFormatException(status));
1272904bf0c2SNate Lawson 	    return;
1273904bf0c2SNate Lawson 	}
1274eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
127555398047SNate Lawson 	ACPI_DISABLE_IRQS();
1276865b8d0bSNate Lawson 	status = AcpiEnterSleepState(ACPI_STATE_S5);
1277be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1278bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
127915e32d5dSMike Smith 	} else {
128015e32d5dSMike Smith 	    DELAY(1000000);
128115e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
128215e32d5dSMike Smith 	}
128380f0e4c2SNate Lawson     } else {
128480f0e4c2SNate Lawson 	printf("Shutting down ACPI\n");
128580f0e4c2SNate Lawson 	AcpiTerminate();
128680f0e4c2SNate Lawson     }
128715e32d5dSMike Smith }
128815e32d5dSMike Smith 
128913d4f7baSMitsuru IWASAKI static void
129013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
129113d4f7baSMitsuru IWASAKI {
129213d4f7baSMitsuru IWASAKI     static int	first_time = 1;
129313d4f7baSMitsuru IWASAKI 
129413d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1295be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
129659ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
129713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
129833febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1299be2b1797SNate Lawson 	if (first_time)
13006c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
130113d4f7baSMitsuru IWASAKI     }
1302be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
130359ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
130413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
130533febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1306be2b1797SNate Lawson 	if (first_time)
13076c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
130813d4f7baSMitsuru IWASAKI     }
130913d4f7baSMitsuru IWASAKI 
131013d4f7baSMitsuru IWASAKI     first_time = 0;
131113d4f7baSMitsuru IWASAKI }
131213d4f7baSMitsuru IWASAKI 
131315e32d5dSMike Smith /*
1314c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1315c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1316c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1317c5ba0be4SMike Smith  */
1318c5ba0be4SMike Smith BOOLEAN
1319c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1320c5ba0be4SMike Smith {
13211e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1322c5ba0be4SMike Smith     ACPI_HANDLE		h;
13231e4925e8SNate Lawson     ACPI_BUFFER		buf;
1324c5ba0be4SMike Smith     ACPI_STATUS		error;
13251e4925e8SNate Lawson     int			ret;
1326c5ba0be4SMike Smith 
13271e4925e8SNate Lawson     ret = FALSE;
1328c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1329c5ba0be4SMike Smith 	return (FALSE);
13301e4925e8SNate Lawson     buf.Pointer = NULL;
13311e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13326fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13336fca9360SNate Lawson     if (ACPI_FAILURE(error))
1334c5ba0be4SMike Smith 	return (FALSE);
13351e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1336be2b1797SNate Lawson 
1337be2b1797SNate Lawson     /* If no _STA method, must be present */
13381e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
13391e4925e8SNate Lawson 	ret = TRUE;
1340be2b1797SNate Lawson 
1341be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
134217dbe0f7SNate Lawson     if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
13431e4925e8SNate Lawson 	ret = TRUE;
1344be2b1797SNate Lawson 
13451e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13461e4925e8SNate Lawson     return (ret);
1347c5ba0be4SMike Smith }
1348c5ba0be4SMike Smith 
1349c5ba0be4SMike Smith /*
1350b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1351b53f2771SMike Smith  */
1352b53f2771SMike Smith BOOLEAN
1353b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1354b53f2771SMike Smith {
13551e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1356b53f2771SMike Smith     ACPI_HANDLE		h;
13571e4925e8SNate Lawson     ACPI_BUFFER		buf;
1358b53f2771SMike Smith     ACPI_STATUS		error;
13591e4925e8SNate Lawson     int			ret;
1360b53f2771SMike Smith 
13611e4925e8SNate Lawson     ret = FALSE;
1362b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1363b53f2771SMike Smith 	return (FALSE);
13641e4925e8SNate Lawson     buf.Pointer = NULL;
13651e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13666fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13676fca9360SNate Lawson     if (ACPI_FAILURE(error))
1368b53f2771SMike Smith 	return (FALSE);
13691e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1370be2b1797SNate Lawson 
1371be2b1797SNate Lawson     /* If no _STA method, must be present */
13721e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
13731e4925e8SNate Lawson 	ret = TRUE;
1374be2b1797SNate Lawson 
137517dbe0f7SNate Lawson     /* Return true for 'present', 'battery present', and 'functioning' */
137617dbe0f7SNate Lawson     if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
13771e4925e8SNate Lawson 	ret = TRUE;
1378be2b1797SNate Lawson 
13791e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13801e4925e8SNate Lawson     return (ret);
1381b53f2771SMike Smith }
1382b53f2771SMike Smith 
1383b53f2771SMike Smith /*
138491233413SNate Lawson  * Match a HID string against a handle
138515e32d5dSMike Smith  */
1386ce619b2eSNate Lawson static BOOLEAN
1387ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
138815e32d5dSMike Smith {
13891e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
13901e4925e8SNate Lawson     ACPI_BUFFER		buf;
139115e32d5dSMike Smith     ACPI_STATUS		error;
13921e4925e8SNate Lawson     int			ret, i;
139315e32d5dSMike Smith 
13941e4925e8SNate Lawson     ret = FALSE;
139591233413SNate Lawson     if (hid == NULL || h == NULL)
139691233413SNate Lawson 	return (ret);
13971e4925e8SNate Lawson     buf.Pointer = NULL;
13981e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13996fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
14006fca9360SNate Lawson     if (ACPI_FAILURE(error))
140191233413SNate Lawson 	return (ret);
14021e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1403be2b1797SNate Lawson 
1404c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1405c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
14061e4925e8SNate Lawson 	    ret = TRUE;
1407c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
14081e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
14091e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
14101e4925e8SNate Lawson 		ret = TRUE;
14111e4925e8SNate Lawson 		break;
14121e4925e8SNate Lawson 	    }
14131e4925e8SNate Lawson 	}
14141e4925e8SNate Lawson     }
1415be2b1797SNate Lawson 
14161e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
14171e4925e8SNate Lawson     return (ret);
141815e32d5dSMike Smith }
141915e32d5dSMike Smith 
142015e32d5dSMike Smith /*
1421c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1422c5ba0be4SMike Smith  * or one if its parents.
1423c5ba0be4SMike Smith  */
1424c5ba0be4SMike Smith ACPI_STATUS
1425c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1426c5ba0be4SMike Smith {
1427c5ba0be4SMike Smith     ACPI_HANDLE		r;
1428c5ba0be4SMike Smith     ACPI_STATUS		status;
1429c5ba0be4SMike Smith 
1430be2b1797SNate Lawson     /* Walk back up the tree to the root */
1431c5ba0be4SMike Smith     for (;;) {
1432be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1433be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1434c5ba0be4SMike Smith 	    *result = r;
1435c5ba0be4SMike Smith 	    return (AE_OK);
1436c5ba0be4SMike Smith 	}
1437bee4aa4aSNate Lawson 	/* XXX Return error here? */
1438c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1439c5ba0be4SMike Smith 	    return (AE_OK);
1440b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1441c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1442c5ba0be4SMike Smith 	parent = r;
1443c5ba0be4SMike Smith     }
1444c5ba0be4SMike Smith }
1445c5ba0be4SMike Smith 
1446eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1447eea17c34SNate Lawson uint32_t
1448eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1449eea17c34SNate Lawson {
1450eea17c34SNate Lawson     uint32_t delta;
1451eea17c34SNate Lawson 
1452eea17c34SNate Lawson     if (end >= start)
1453eea17c34SNate Lawson 	delta = end - start;
1454eea17c34SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
14558d01ceefSNate Lawson 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1456eea17c34SNate Lawson     else
1457eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
1458eea17c34SNate Lawson     return (delta);
1459eea17c34SNate Lawson }
1460eea17c34SNate Lawson 
1461c5ba0be4SMike Smith /*
1462c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1463c5ba0be4SMike Smith  */
1464c5ba0be4SMike Smith ACPI_BUFFER *
1465c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1466c5ba0be4SMike Smith {
1467c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1468c5ba0be4SMike Smith 
1469c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1470c5ba0be4SMike Smith 	return (NULL);
1471c5ba0be4SMike Smith     buf->Length = size;
1472c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1473c5ba0be4SMike Smith     return (buf);
1474c5ba0be4SMike Smith }
1475c5ba0be4SMike Smith 
1476c310653eSNate Lawson ACPI_STATUS
1477cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1478c310653eSNate Lawson {
1479c310653eSNate Lawson     ACPI_OBJECT arg1;
1480c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1481c310653eSNate Lawson 
1482c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1483c310653eSNate Lawson     arg1.Integer.Value = number;
1484c310653eSNate Lawson     args.Count = 1;
1485c310653eSNate Lawson     args.Pointer = &arg1;
1486c310653eSNate Lawson 
1487c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1488c310653eSNate Lawson }
1489c310653eSNate Lawson 
1490c5ba0be4SMike Smith /*
1491c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1492c5ba0be4SMike Smith  */
1493c5ba0be4SMike Smith ACPI_STATUS
1494cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1495c5ba0be4SMike Smith {
1496be2b1797SNate Lawson     ACPI_STATUS	status;
1497c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1498c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1499c5ba0be4SMike Smith 
1500c5ba0be4SMike Smith     if (handle == NULL)
1501c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
15020c7da7acSJohn Baldwin 
15030c7da7acSJohn Baldwin     /*
15040c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
15050c7da7acSJohn Baldwin      * a method that will return an Integer.
15060c7da7acSJohn Baldwin      */
1507c5ba0be4SMike Smith     buf.Pointer = &param;
1508c5ba0be4SMike Smith     buf.Length = sizeof(param);
1509be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1510be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1511be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1512c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1513be2b1797SNate Lawson 	else
1514be2b1797SNate Lawson 	    status = AE_TYPE;
1515c5ba0be4SMike Smith     }
15160c7da7acSJohn Baldwin 
15170c7da7acSJohn Baldwin     /*
15180c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
15190c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
15200c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
15210c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
15220c7da7acSJohn Baldwin      *
15230c7da7acSJohn Baldwin      * This is a hack.
15240c7da7acSJohn Baldwin      */
1525be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1526b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1527be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
15280c7da7acSJohn Baldwin 	} else {
1529be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1530be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1531be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
15320c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
15330c7da7acSJohn Baldwin 	}
1534f97739daSNate Lawson     }
1535be2b1797SNate Lawson     return (status);
1536c5ba0be4SMike Smith }
1537c5ba0be4SMike Smith 
1538c573e654SMitsuru IWASAKI ACPI_STATUS
1539cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1540c573e654SMitsuru IWASAKI {
1541c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1542dba55fa2SNate Lawson     UINT8	*val;
1543c573e654SMitsuru IWASAKI     int		i;
1544c573e654SMitsuru IWASAKI 
1545c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1546c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1547c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1548c573e654SMitsuru IWASAKI 	return (AE_OK);
1549c573e654SMitsuru IWASAKI     }
1550c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1551c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1552c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1553c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1554be2b1797SNate Lawson 
1555c573e654SMitsuru IWASAKI     *number = 0;
1556dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1557c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1558dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1559c573e654SMitsuru IWASAKI     return (AE_OK);
1560c573e654SMitsuru IWASAKI }
1561c573e654SMitsuru IWASAKI 
1562c5ba0be4SMike Smith /*
1563c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1564c5ba0be4SMike Smith  * function for each element.
1565c5ba0be4SMike Smith  *
1566c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1567c5ba0be4SMike Smith  */
1568c5ba0be4SMike Smith ACPI_STATUS
1569be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1570be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1571c5ba0be4SMike Smith {
1572c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1573c5ba0be4SMike Smith     int		i;
1574c5ba0be4SMike Smith 
1575be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1576c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1577c5ba0be4SMike Smith 
1578be2b1797SNate Lawson     /* Iterate over components */
1579be2b1797SNate Lawson     i = 0;
1580be2b1797SNate Lawson     comp = pkg->Package.Elements;
1581be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1582c5ba0be4SMike Smith 	func(comp, arg);
1583c5ba0be4SMike Smith 
1584c5ba0be4SMike Smith     return (AE_OK);
1585c5ba0be4SMike Smith }
1586c5ba0be4SMike Smith 
15876f69255bSMike Smith /*
15886f69255bSMike Smith  * Find the (index)th resource object in a set.
15896f69255bSMike Smith  */
15906f69255bSMike Smith ACPI_STATUS
15916d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
15926f69255bSMike Smith {
15936d63101aSMike Smith     ACPI_RESOURCE	*rp;
15946f69255bSMike Smith     int			i;
15956f69255bSMike Smith 
15966d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15976f69255bSMike Smith     i = index;
15986d63101aSMike Smith     while (i-- > 0) {
1599be2b1797SNate Lawson 	/* Range check */
16006d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
16016f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1602be2b1797SNate Lawson 
1603be2b1797SNate Lawson 	/* Check for terminator */
1604be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
16056d63101aSMike Smith 	    return (AE_NOT_FOUND);
1606abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
16076f69255bSMike Smith     }
16086f69255bSMike Smith     if (resp != NULL)
16096d63101aSMike Smith 	*resp = rp;
1610be2b1797SNate Lawson 
16116d63101aSMike Smith     return (AE_OK);
16126d63101aSMike Smith }
16136d63101aSMike Smith 
16146d63101aSMike Smith /*
16156d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
16166d63101aSMike Smith  *
16176d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
16186d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
16196d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
16206d63101aSMike Smith  * resources.
16216d63101aSMike Smith  */
16226d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
16236d63101aSMike Smith 
16246d63101aSMike Smith ACPI_STATUS
16256d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
16266d63101aSMike Smith {
16276d63101aSMike Smith     ACPI_RESOURCE	*rp;
16286d63101aSMike Smith     void		*newp;
16296d63101aSMike Smith 
1630be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
16316d63101aSMike Smith     if (buf->Pointer == NULL) {
16326d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
16336d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
16346d63101aSMike Smith 	    return (AE_NO_MEMORY);
16356d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
16366d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
16376d63101aSMike Smith 	rp->Length = 0;
16386d63101aSMike Smith     }
16396d63101aSMike Smith     if (res == NULL)
16406d63101aSMike Smith 	return (AE_OK);
16416d63101aSMike Smith 
16426d63101aSMike Smith     /*
16436d63101aSMike Smith      * Scan the current buffer looking for the terminator.
16446d63101aSMike Smith      * This will either find the terminator or hit the end
16456d63101aSMike Smith      * of the buffer and return an error.
16466d63101aSMike Smith      */
16476d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
16486d63101aSMike Smith     for (;;) {
1649be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
16506d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
16516d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1652be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
16536d63101aSMike Smith 	    break;
1654abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
16556d63101aSMike Smith     }
16566d63101aSMike Smith 
16576d63101aSMike Smith     /*
16586d63101aSMike Smith      * Check the size of the buffer and expand if required.
16596d63101aSMike Smith      *
16606d63101aSMike Smith      * Required size is:
16616d63101aSMike Smith      *	size of existing resources before terminator +
16626d63101aSMike Smith      *	size of new resource and header +
16636d63101aSMike Smith      * 	size of terminator.
16646d63101aSMike Smith      *
16656d63101aSMike Smith      * Note that this loop should really only run once, unless
16666d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
16676d63101aSMike Smith      */
16686d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
16696d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
16706d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
16716d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
16726d63101aSMike Smith 	    return (AE_NO_MEMORY);
16736d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1674a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1675a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
16766d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
16776d63101aSMike Smith 	buf->Pointer = newp;
16786d63101aSMike Smith 	buf->Length += buf->Length;
16796d63101aSMike Smith     }
16806d63101aSMike Smith 
1681be2b1797SNate Lawson     /* Insert the new resource. */
16826d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
16836d63101aSMike Smith 
1684be2b1797SNate Lawson     /* And add the terminator. */
1685abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
16866d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
16876d63101aSMike Smith     rp->Length = 0;
16886d63101aSMike Smith 
16896f69255bSMike Smith     return (AE_OK);
16906f69255bSMike Smith }
1691c5ba0be4SMike Smith 
1692da14ac9fSJohn Baldwin /*
1693da14ac9fSJohn Baldwin  * Set interrupt model.
1694da14ac9fSJohn Baldwin  */
1695da14ac9fSJohn Baldwin ACPI_STATUS
1696da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1697da14ac9fSJohn Baldwin {
1698da14ac9fSJohn Baldwin 
1699c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1700da14ac9fSJohn Baldwin }
1701da14ac9fSJohn Baldwin 
1702ece50487SMitsuru IWASAKI static void
1703ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1704ece50487SMitsuru IWASAKI {
1705bee4aa4aSNate Lawson 
1706ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1707ece50487SMitsuru IWASAKI }
1708c30382dfSMitsuru IWASAKI 
170915e32d5dSMike Smith /*
171015e32d5dSMike Smith  * Set the system sleep state
171115e32d5dSMike Smith  *
1712be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
171315e32d5dSMike Smith  */
171415e32d5dSMike Smith ACPI_STATUS
171515e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
171615e32d5dSMike Smith {
171715e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
171856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
171956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
172015e32d5dSMike Smith 
1721b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
17220ae55423SMike Smith 
172398175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
17246397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
172598175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
17266397624dSMitsuru IWASAKI 
172798175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1728ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1729ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1730ece50487SMitsuru IWASAKI 
173115e32d5dSMike Smith     switch (state) {
173215e32d5dSMike Smith     case ACPI_STATE_S1:
17336161544cSTakanori Watanabe     case ACPI_STATE_S2:
17346161544cSTakanori Watanabe     case ACPI_STATE_S3:
17356161544cSTakanori Watanabe     case ACPI_STATE_S4:
1736be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
173798175614SNate Lawson 	if (status == AE_NOT_FOUND) {
173898175614SNate Lawson 	    device_printf(sc->acpi_dev,
173998175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
174098175614SNate Lawson 	    break;
174198175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1742be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1743be2b1797SNate Lawson 			  AcpiFormatException(status));
174456d8cb57SMitsuru IWASAKI 	    break;
174556d8cb57SMitsuru IWASAKI 	}
174656d8cb57SMitsuru IWASAKI 
1747a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1748a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1749a1fccb47SMitsuru IWASAKI 
1750d4b9ff91SNate Lawson 	/* Enable any GPEs as appropriate and requested by the user. */
1751d4b9ff91SNate Lawson 	acpi_wake_prep_walk(state);
1752e8b4d56eSNate Lawson 
175315e32d5dSMike Smith 	/*
1754c7f88fc3SNate Lawson 	 * Inform all devices that we are going to sleep.  If at least one
1755c7f88fc3SNate Lawson 	 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
175615e32d5dSMike Smith 	 *
1757c7f88fc3SNate Lawson 	 * XXX Note that a better two-pass approach with a 'veto' pass
1758c7f88fc3SNate Lawson 	 * followed by a "real thing" pass would be better, but the current
1759c7f88fc3SNate Lawson 	 * bus interface does not provide for this.
176015e32d5dSMike Smith 	 */
1761c7f88fc3SNate Lawson 	if (DEVICE_SUSPEND(root_bus) != 0)
17620ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
1763b9c780d6SMitsuru IWASAKI 
1764be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1765be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1766b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1767b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1768b9c780d6SMitsuru IWASAKI 	    break;
1769b9c780d6SMitsuru IWASAKI 	}
1770b9c780d6SMitsuru IWASAKI 
1771be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1772ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1773ff01efb5SMitsuru IWASAKI 
17746161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
17756161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
17766161544cSTakanori Watanabe 
17776161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1778be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1779964679ceSMitsuru IWASAKI 		AcpiEnable();
17806161544cSTakanori Watanabe 	} else {
1781c7f88fc3SNate Lawson 	    ACPI_DISABLE_IRQS();
1782be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1783be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1784be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1785be2b1797SNate Lawson 			      AcpiFormatException(status));
1786c30382dfSMitsuru IWASAKI 		break;
178715e32d5dSMike Smith 	    }
17886161544cSTakanori Watanabe 	}
1789d4b9ff91SNate Lawson 
1790d4b9ff91SNate Lawson 	/* Resume devices, re-enable GPEs and fixed events. */
1791d4b9ff91SNate Lawson 	acpi_wake_prep_walk(state);
1792ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
179315e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
179415e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
179513d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
179615e32d5dSMike Smith 	break;
179715e32d5dSMike Smith     case ACPI_STATE_S5:
179815e32d5dSMike Smith 	/*
179915e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
180015e32d5dSMike Smith 	 * shutdown handlers.
180115e32d5dSMike Smith 	 */
180215e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
180315e32d5dSMike Smith 	break;
180498175614SNate Lawson     case ACPI_STATE_S0:
180515e32d5dSMike Smith     default:
180615e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
180715e32d5dSMike Smith 	break;
180815e32d5dSMike Smith     }
1809ece50487SMitsuru IWASAKI 
181098175614SNate Lawson     /* Disable a second sleep request for a short period */
1811ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1812ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1813ece50487SMitsuru IWASAKI 
18140ae55423SMike Smith     return_ACPI_STATUS (status);
181515e32d5dSMike Smith }
181615e32d5dSMike Smith 
1817e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
1818e8b4d56eSNate Lawson int
1819e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
1820e8b4d56eSNate Lawson {
1821e8b4d56eSNate Lawson     struct acpi_prw_data prw;
1822e8b4d56eSNate Lawson 
1823e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
1824e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
1825e8b4d56eSNate Lawson 	return (ENXIO);
1826e8b4d56eSNate Lawson 
1827e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
1828e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
1829e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
1830e8b4d56eSNate Lawson 	return (ENXIO);
1831e8b4d56eSNate Lawson     }
1832e8b4d56eSNate Lawson 
1833e8b4d56eSNate Lawson     return (0);
1834e8b4d56eSNate Lawson }
1835e8b4d56eSNate Lawson 
1836e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
1837e8b4d56eSNate Lawson int
1838e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
1839e8b4d56eSNate Lawson {
1840e8b4d56eSNate Lawson     struct acpi_prw_data prw;
1841e8b4d56eSNate Lawson     ACPI_HANDLE handle;
1842e8b4d56eSNate Lawson     ACPI_STATUS status;
1843e8b4d56eSNate Lawson     int flags;
1844e8b4d56eSNate Lawson 
1845d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
1846e8b4d56eSNate Lawson     handle = acpi_get_handle(dev);
1847e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
1848e8b4d56eSNate Lawson 	return (ENXIO);
1849e8b4d56eSNate Lawson 
1850d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
1851e8b4d56eSNate Lawson     if (enable) {
1852e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1853e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
1854e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
1855e8b4d56eSNate Lawson 	    return (ENXIO);
1856e8b4d56eSNate Lawson 	}
1857d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
1858e8b4d56eSNate Lawson     } else {
1859e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1860e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
1861e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
1862e8b4d56eSNate Lawson 	    return (ENXIO);
1863e8b4d56eSNate Lawson 	}
1864d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
1865e8b4d56eSNate Lawson     }
1866e8b4d56eSNate Lawson 
1867e8b4d56eSNate Lawson     return (0);
1868e8b4d56eSNate Lawson }
1869e8b4d56eSNate Lawson 
1870d4b9ff91SNate Lawson static int
1871d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
1872e8b4d56eSNate Lawson {
1873e8b4d56eSNate Lawson     struct acpi_prw_data prw;
1874d4b9ff91SNate Lawson     device_t dev;
1875e8b4d56eSNate Lawson 
1876d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
1877e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
1878e8b4d56eSNate Lawson 	return (ENXIO);
1879d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
1880e8b4d56eSNate Lawson 
1881e8b4d56eSNate Lawson     /*
1882d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
1883d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
1884d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
1885d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
1886d4b9ff91SNate Lawson      * and set _PSW.
1887e8b4d56eSNate Lawson      */
1888d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
1889cc85c78cSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1890e8b4d56eSNate Lawson 	if (bootverbose)
1891d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
1892d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
1893d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
1894d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
1895d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
1896d4b9ff91SNate Lawson 	if (bootverbose)
1897d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
1898d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
1899d4b9ff91SNate Lawson     }
1900cc85c78cSNate Lawson 
1901cc85c78cSNate Lawson     return (0);
1902e8b4d56eSNate Lawson }
1903e8b4d56eSNate Lawson 
1904d4b9ff91SNate Lawson static int
1905d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
1906cc85c78cSNate Lawson {
1907cc85c78cSNate Lawson     struct acpi_prw_data prw;
1908d4b9ff91SNate Lawson     device_t dev;
1909cc85c78cSNate Lawson 
1910cc85c78cSNate Lawson     /*
1911d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
1912d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
1913cc85c78cSNate Lawson      */
1914d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
1915d4b9ff91SNate Lawson 	return (ENXIO);
1916d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
1917d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
1918d4b9ff91SNate Lawson 	return (0);
1919cc85c78cSNate Lawson 
1920d4b9ff91SNate Lawson     /*
1921d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
1922d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
1923d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
1924d4b9ff91SNate Lawson      */
1925d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
1926cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1927d4b9ff91SNate Lawson 	if (bootverbose)
1928d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
1929d4b9ff91SNate Lawson     } else {
1930d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
1931d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
1932d4b9ff91SNate Lawson 	if (bootverbose)
1933d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
1934d4b9ff91SNate Lawson 		acpi_name(handle));
1935d4b9ff91SNate Lawson     }
1936d4b9ff91SNate Lawson 
1937e8b4d56eSNate Lawson     return (0);
1938e8b4d56eSNate Lawson }
1939e8b4d56eSNate Lawson 
1940e8b4d56eSNate Lawson static ACPI_STATUS
1941d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
1942e8b4d56eSNate Lawson {
1943d4b9ff91SNate Lawson     int sstate;
1944e8b4d56eSNate Lawson 
1945d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
1946d4b9ff91SNate Lawson     sstate = *(int *)context;
1947d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
1948d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
1949d4b9ff91SNate Lawson     else
1950d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
1951e8b4d56eSNate Lawson     return (AE_OK);
1952e8b4d56eSNate Lawson }
1953e8b4d56eSNate Lawson 
1954d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
1955e8b4d56eSNate Lawson static int
1956d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
1957e8b4d56eSNate Lawson {
1958e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
1959e8b4d56eSNate Lawson 
1960e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
1961d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
1962d4b9ff91SNate Lawson 	    acpi_wake_prep, &sstate, NULL);
1963e8b4d56eSNate Lawson     return (0);
1964e8b4d56eSNate Lawson }
1965e8b4d56eSNate Lawson 
196688a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
196788a79fc0SNate Lawson static int
196888a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
196988a79fc0SNate Lawson {
197088a79fc0SNate Lawson     int error, i, numdevs;
197188a79fc0SNate Lawson     device_t *devlist;
197288a79fc0SNate Lawson     device_t child;
1973d4b9ff91SNate Lawson     ACPI_STATUS status;
197488a79fc0SNate Lawson 
197588a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
197688a79fc0SNate Lawson     if (error != 0 || numdevs == 0)
197788a79fc0SNate Lawson 	return (error);
197888a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
197988a79fc0SNate Lawson 	child = devlist[i];
1980d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
198188a79fc0SNate Lawson 	if (!device_is_attached(child))
198288a79fc0SNate Lawson 	    continue;
1983d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
1984d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
198588a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
198688a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
198788a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
198888a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
198988a79fc0SNate Lawson 	}
199088a79fc0SNate Lawson     }
199188a79fc0SNate Lawson     free(devlist, M_TEMP);
199288a79fc0SNate Lawson 
199388a79fc0SNate Lawson     return (0);
199488a79fc0SNate Lawson }
199588a79fc0SNate Lawson 
199688a79fc0SNate Lawson /* Enable or disable wake from userland. */
199788a79fc0SNate Lawson static int
199888a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
199988a79fc0SNate Lawson {
200088a79fc0SNate Lawson     int enable, error;
200188a79fc0SNate Lawson     device_t dev;
200288a79fc0SNate Lawson 
200388a79fc0SNate Lawson     dev = (device_t)arg1;
2004d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
200588a79fc0SNate Lawson 
200688a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
200788a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
200888a79fc0SNate Lawson 	return (error);
200988a79fc0SNate Lawson     if (enable != 0 && enable != 1)
201088a79fc0SNate Lawson 	return (EINVAL);
201188a79fc0SNate Lawson 
201288a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
201388a79fc0SNate Lawson }
201488a79fc0SNate Lawson 
2015e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
2016d4b9ff91SNate Lawson int
2017e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2018e8b4d56eSNate Lawson {
2019e8b4d56eSNate Lawson     ACPI_STATUS			status;
2020e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2021e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2022d4b9ff91SNate Lawson     int				error, i, power_count;
2023e8b4d56eSNate Lawson 
2024e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2025e8b4d56eSNate Lawson 	return (EINVAL);
2026e8b4d56eSNate Lawson 
2027e8b4d56eSNate Lawson     /*
2028e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2029e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2030e8b4d56eSNate Lawson      */
2031e8b4d56eSNate Lawson     error = EINVAL;
2032e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2033e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2034e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2035e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2036e8b4d56eSNate Lawson 	return (ENOENT);
2037e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2038e8b4d56eSNate Lawson     if (res == NULL)
2039e8b4d56eSNate Lawson 	return (ENOENT);
2040e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2041e8b4d56eSNate Lawson 	goto out;
2042e8b4d56eSNate Lawson 
2043e8b4d56eSNate Lawson     /*
2044e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2045e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2046e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2047e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2048e8b4d56eSNate Lawson      */
2049e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2050e8b4d56eSNate Lawson 	goto out;
2051e8b4d56eSNate Lawson 
2052e8b4d56eSNate Lawson     /*
2053e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2054e8b4d56eSNate Lawson      */
2055e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2056e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2057e8b4d56eSNate Lawson 	/*
2058e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2059e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2060e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2061e8b4d56eSNate Lawson 	 * enabled for the wake event.
2062e8b4d56eSNate Lawson 	 */
2063e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2064e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2065e8b4d56eSNate Lawson 	error = 0;
2066e8b4d56eSNate Lawson 	break;
2067e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2068e8b4d56eSNate Lawson 	/*
2069e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2070e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2071e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2072e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2073e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2074e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2075e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2076e8b4d56eSNate Lawson 	 * the wake event.
2077e8b4d56eSNate Lawson 	 *
2078e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2079e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2080e8b4d56eSNate Lawson 	 */
2081e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2082e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2083e8b4d56eSNate Lawson 	    goto out;
2084e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2085e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2086e8b4d56eSNate Lawson 	    goto out;
2087e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2088e8b4d56eSNate Lawson 	    goto out;
2089e8b4d56eSNate Lawson 	error = 0;
2090e8b4d56eSNate Lawson 	break;
2091e8b4d56eSNate Lawson     default:
2092e8b4d56eSNate Lawson 	goto out;
2093e8b4d56eSNate Lawson     }
2094e8b4d56eSNate Lawson 
2095d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
2096d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
2097d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
2098d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
2099d4b9ff91SNate Lawson 	power_count = 0;
2100d4b9ff91SNate Lawson     }
2101d4b9ff91SNate Lawson     prw->power_res_count = power_count;
2102d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
2103d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
2104e8b4d56eSNate Lawson 
2105e8b4d56eSNate Lawson out:
2106e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2107e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2108e8b4d56eSNate Lawson     return (error);
2109e8b4d56eSNate Lawson }
2110e8b4d56eSNate Lawson 
211115e32d5dSMike Smith /*
211215e32d5dSMike Smith  * Enable/Disable ACPI
211315e32d5dSMike Smith  */
211415e32d5dSMike Smith ACPI_STATUS
211515e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
211615e32d5dSMike Smith {
211715e32d5dSMike Smith     ACPI_STATUS	status;
211815e32d5dSMike Smith     u_int32_t	flags;
211915e32d5dSMike Smith 
2120b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21210ae55423SMike Smith 
2122bee4aa4aSNate Lawson     status = AE_ERROR;
212315e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
212415e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
2125bee4aa4aSNate Lawson 
2126be2b1797SNate Lawson     if (!sc->acpi_enabled)
212715e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
2128bee4aa4aSNate Lawson     if (ACPI_SUCCESS(status))
212915e32d5dSMike Smith 	sc->acpi_enabled = 1;
2130be2b1797SNate Lawson 
21310ae55423SMike Smith     return_ACPI_STATUS (status);
213215e32d5dSMike Smith }
213315e32d5dSMike Smith 
213415e32d5dSMike Smith ACPI_STATUS
213515e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
213615e32d5dSMike Smith {
213715e32d5dSMike Smith     ACPI_STATUS	status;
213815e32d5dSMike Smith 
2139b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21400ae55423SMike Smith 
2141bee4aa4aSNate Lawson     status = AE_ERROR;
2142be2b1797SNate Lawson     if (sc->acpi_enabled)
214315e32d5dSMike Smith 	status = AcpiDisable();
2144bee4aa4aSNate Lawson     if (ACPI_SUCCESS(status))
214515e32d5dSMike Smith 	sc->acpi_enabled = 0;
2146be2b1797SNate Lawson 
21470ae55423SMike Smith     return_ACPI_STATUS (status);
214815e32d5dSMike Smith }
214915e32d5dSMike Smith 
215015e32d5dSMike Smith /*
215115e32d5dSMike Smith  * ACPI Event Handlers
215215e32d5dSMike Smith  */
215315e32d5dSMike Smith 
215415e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
215515e32d5dSMike Smith 
215615e32d5dSMike Smith static void
215715e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
215815e32d5dSMike Smith {
2159bee4aa4aSNate Lawson 
2160b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
216115e32d5dSMike Smith 
2162a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
216315e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
2164bee4aa4aSNate Lawson 
21650ae55423SMike Smith     return_VOID;
216615e32d5dSMike Smith }
216715e32d5dSMike Smith 
216815e32d5dSMike Smith static void
216915e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
217015e32d5dSMike Smith {
2171bee4aa4aSNate Lawson 
2172b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
21730ae55423SMike Smith 
2174bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
2175cb9b0d80SMike Smith 
21760ae55423SMike Smith     return_VOID;
217715e32d5dSMike Smith }
217815e32d5dSMike Smith 
217915e32d5dSMike Smith /*
218015e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
218115e32d5dSMike Smith  */
218215e32d5dSMike Smith UINT32
218333febf93SNate Lawson acpi_event_power_button_sleep(void *context)
218415e32d5dSMike Smith {
218515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
218615e32d5dSMike Smith 
2187b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21880ae55423SMike Smith 
218915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
21900ae55423SMike Smith 
2191b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
219215e32d5dSMike Smith }
219315e32d5dSMike Smith 
219415e32d5dSMike Smith UINT32
219533febf93SNate Lawson acpi_event_power_button_wake(void *context)
219615e32d5dSMike Smith {
219715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
219815e32d5dSMike Smith 
2199b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
22000ae55423SMike Smith 
220115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
22020ae55423SMike Smith 
2203b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
220415e32d5dSMike Smith }
220515e32d5dSMike Smith 
220615e32d5dSMike Smith UINT32
220733febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
220815e32d5dSMike Smith {
220915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
221015e32d5dSMike Smith 
2211b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
22120ae55423SMike Smith 
221315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
22140ae55423SMike Smith 
2215b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
221615e32d5dSMike Smith }
221715e32d5dSMike Smith 
221815e32d5dSMike Smith UINT32
221933febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
222015e32d5dSMike Smith {
222115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
222215e32d5dSMike Smith 
2223b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
22240ae55423SMike Smith 
222515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
22260ae55423SMike Smith 
2227b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
222815e32d5dSMike Smith }
222915e32d5dSMike Smith 
223015e32d5dSMike Smith /*
2231bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
2232bee4aa4aSNate Lawson  * use this for single-threaded callers.
223315e32d5dSMike Smith  */
223415e32d5dSMike Smith char *
223515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
223615e32d5dSMike Smith {
2237bee4aa4aSNate Lawson     ACPI_BUFFER buf;
2238bee4aa4aSNate Lawson     static char data[256];
223915e32d5dSMike Smith 
2240bee4aa4aSNate Lawson     buf.Length = sizeof(data);
2241bee4aa4aSNate Lawson     buf.Pointer = data;
2242cb9b0d80SMike Smith 
2243bee4aa4aSNate Lawson     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
2244bee4aa4aSNate Lawson 	return (data);
2245bee4aa4aSNate Lawson     return ("(unknown)");
224615e32d5dSMike Smith }
224715e32d5dSMike Smith 
224815e32d5dSMike Smith /*
224915e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
225015e32d5dSMike Smith  * parts of the namespace.
225115e32d5dSMike Smith  */
225215e32d5dSMike Smith int
225315e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
225415e32d5dSMike Smith {
22553c600cfbSJohn Baldwin     char	*cp, *env, *np;
225615e32d5dSMike Smith     int		len;
225715e32d5dSMike Smith 
225815e32d5dSMike Smith     np = acpi_name(handle);
225915e32d5dSMike Smith     if (*np == '\\')
226015e32d5dSMike Smith 	np++;
22613c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
226215e32d5dSMike Smith 	return (0);
226315e32d5dSMike Smith 
2264be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
22653c600cfbSJohn Baldwin     cp = env;
226615e32d5dSMike Smith     for (;;) {
2267bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
226815e32d5dSMike Smith 	    cp++;
226915e32d5dSMike Smith 	if (*cp == 0)
227015e32d5dSMike Smith 	    break;
227115e32d5dSMike Smith 	len = 0;
2272bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
227315e32d5dSMike Smith 	    len++;
2274d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
22753c600cfbSJohn Baldwin 	    freeenv(env);
22760ae55423SMike Smith 	    return(1);
2277d786139cSMaxime Henrion 	}
22780ae55423SMike Smith 	cp += len;
22790ae55423SMike Smith     }
22803c600cfbSJohn Baldwin     freeenv(env);
2281be2b1797SNate Lawson 
22820ae55423SMike Smith     return (0);
22830ae55423SMike Smith }
22840ae55423SMike Smith 
22850ae55423SMike Smith /*
22860ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
22870ae55423SMike Smith  */
22880ae55423SMike Smith int
22890ae55423SMike Smith acpi_disabled(char *subsys)
22900ae55423SMike Smith {
229178689b15SMaxime Henrion     char	*cp, *env;
22920ae55423SMike Smith     int		len;
22930ae55423SMike Smith 
22943184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
22950ae55423SMike Smith 	return (0);
22963184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
229778689b15SMaxime Henrion 	freeenv(env);
22980ae55423SMike Smith 	return (1);
2299d786139cSMaxime Henrion     }
23000ae55423SMike Smith 
23013184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
230278689b15SMaxime Henrion     cp = env;
23030ae55423SMike Smith     for (;;) {
23043184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
23050ae55423SMike Smith 	    cp++;
23063184cf5aSNate Lawson 	if (*cp == '\0')
23070ae55423SMike Smith 	    break;
23080ae55423SMike Smith 	len = 0;
23093184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
23100ae55423SMike Smith 	    len++;
23113184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
231278689b15SMaxime Henrion 	    freeenv(env);
231315e32d5dSMike Smith 	    return (1);
2314d786139cSMaxime Henrion 	}
231515e32d5dSMike Smith 	cp += len;
231615e32d5dSMike Smith     }
231778689b15SMaxime Henrion     freeenv(env);
2318be2b1797SNate Lawson 
231915e32d5dSMike Smith     return (0);
232015e32d5dSMike Smith }
232115e32d5dSMike Smith 
232215e32d5dSMike Smith /*
232315e32d5dSMike Smith  * Control interface.
232415e32d5dSMike Smith  *
23250ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2326be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2327be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
232815e32d5dSMike Smith  */
23290ae55423SMike Smith struct acpi_ioctl_hook
23300ae55423SMike Smith {
23310ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
23320ae55423SMike Smith     u_long			 cmd;
2333be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
23340ae55423SMike Smith     void			 *arg;
23350ae55423SMike Smith };
23360ae55423SMike Smith 
23370ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
23380ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
23390ae55423SMike Smith 
23400ae55423SMike Smith int
2341be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
23420ae55423SMike Smith {
23430ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
23440ae55423SMike Smith 
23450ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
23460ae55423SMike Smith 	return (ENOMEM);
23470ae55423SMike Smith     hp->cmd = cmd;
23480ae55423SMike Smith     hp->fn = fn;
23490ae55423SMike Smith     hp->arg = arg;
23500ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
23510ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
23520ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
23530ae55423SMike Smith     }
23540ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
23550ae55423SMike Smith     return (0);
23560ae55423SMike Smith }
23570ae55423SMike Smith 
23580ae55423SMike Smith /*
23590ae55423SMike Smith  * Deregister an ioctl handler.
23600ae55423SMike Smith  */
23610ae55423SMike Smith void
2362be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
23630ae55423SMike Smith {
23640ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
23650ae55423SMike Smith 
23660ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
23670ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
23680ae55423SMike Smith 	    break;
23690ae55423SMike Smith 
23700ae55423SMike Smith     if (hp != NULL) {
23710ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
23720ae55423SMike Smith 	free(hp, M_ACPIDEV);
23730ae55423SMike Smith     }
23740ae55423SMike Smith }
23750ae55423SMike Smith 
237615e32d5dSMike Smith static int
237789c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
237815e32d5dSMike Smith {
237915e32d5dSMike Smith     return (0);
238015e32d5dSMike Smith }
238115e32d5dSMike Smith 
238215e32d5dSMike Smith static int
238389c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
238415e32d5dSMike Smith {
238515e32d5dSMike Smith     return (0);
238615e32d5dSMike Smith }
238715e32d5dSMike Smith 
238815e32d5dSMike Smith static int
238989c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
239015e32d5dSMike Smith {
239115e32d5dSMike Smith     struct acpi_softc		*sc;
23920ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
2393bee4aa4aSNate Lawson     int				error, state;
239415e32d5dSMike Smith 
2395a2e35898SDavid E. O'Brien     error = 0;
2396a2e35898SDavid E. O'Brien     hp = NULL;
239715e32d5dSMike Smith     sc = dev->si_drv1;
239815e32d5dSMike Smith 
23990ae55423SMike Smith     /*
24000ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
24010ae55423SMike Smith      */
2402bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
24030ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2404bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
2405bee4aa4aSNate Lawson 		break;
24060ae55423SMike Smith 	}
2407bee4aa4aSNate Lawson     if (hp)
2408bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
24090ae55423SMike Smith 
24100ae55423SMike Smith     /*
2411a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2412a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2413a89fcf28STakanori Watanabe      * Not changing system behavior.
2414a89fcf28STakanori Watanabe      */
2415be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
2416be2b1797SNate Lawson 	return (EPERM);
2417a89fcf28STakanori Watanabe 
2418be2b1797SNate Lawson     /* Core system ioctls. */
241915e32d5dSMike Smith     switch (cmd) {
242015e32d5dSMike Smith     case ACPIIO_ENABLE:
24210ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
242215e32d5dSMike Smith 	    error = ENXIO;
242315e32d5dSMike Smith 	break;
242415e32d5dSMike Smith     case ACPIIO_DISABLE:
24250ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
242615e32d5dSMike Smith 	    error = ENXIO;
242715e32d5dSMike Smith 	break;
242815e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
242915e32d5dSMike Smith 	if (!sc->acpi_enabled) {
243015e32d5dSMike Smith 	    error = ENXIO;
243115e32d5dSMike Smith 	    break;
243215e32d5dSMike Smith 	}
2433bee4aa4aSNate Lawson 	error = EINVAL;
243415e32d5dSMike Smith 	state = *(int *)addr;
2435bee4aa4aSNate Lawson 	if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2436bee4aa4aSNate Lawson 	    if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
2437bee4aa4aSNate Lawson 		error = 0;
243815e32d5dSMike Smith 	break;
243915e32d5dSMike Smith     default:
2440bee4aa4aSNate Lawson 	error = ENXIO;
244115e32d5dSMike Smith 	break;
244215e32d5dSMike Smith     }
2443917d44c8SMitsuru IWASAKI 
244415e32d5dSMike Smith     return (error);
244515e32d5dSMike Smith }
244615e32d5dSMike Smith 
24471d073b1dSJohn Baldwin static int
2448d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2449d75de536SMitsuru IWASAKI {
2450d75de536SMitsuru IWASAKI     int error;
2451bee4aa4aSNate Lawson     struct sbuf sb;
2452d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2453d75de536SMitsuru IWASAKI 
2454bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
2455bee4aa4aSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
2456bee4aa4aSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
2457bee4aa4aSNate Lawson 	    sbuf_printf(&sb, "S%d ", state);
2458bee4aa4aSNate Lawson     sbuf_trim(&sb);
2459bee4aa4aSNate Lawson     sbuf_finish(&sb);
2460bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2461bee4aa4aSNate Lawson     sbuf_delete(&sb);
2462d75de536SMitsuru IWASAKI     return (error);
2463d75de536SMitsuru IWASAKI }
2464d75de536SMitsuru IWASAKI 
2465d75de536SMitsuru IWASAKI static int
24661d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
24671d073b1dSJohn Baldwin {
24681d073b1dSJohn Baldwin     char sleep_state[10];
24691d073b1dSJohn Baldwin     int error;
24701d073b1dSJohn Baldwin     u_int new_state, old_state;
24711d073b1dSJohn Baldwin 
24721d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2473bee4aa4aSNate Lawson     if (old_state > ACPI_S_STATES_MAX + 1)
2474bee4aa4aSNate Lawson 	strlcpy(sleep_state, "unknown", sizeof(sleep_state));
2475bee4aa4aSNate Lawson     else
2476bee4aa4aSNate Lawson 	strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
24771d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
24781d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2479be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2480bee4aa4aSNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
2481bee4aa4aSNate Lawson 	    if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
24821d073b1dSJohn Baldwin 		break;
2483931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2484be2b1797SNate Lawson 	    if (new_state != old_state)
24851d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2486bee4aa4aSNate Lawson 	} else
24871d073b1dSJohn Baldwin 	    error = EINVAL;
24881d073b1dSJohn Baldwin     }
2489be2b1797SNate Lawson 
24901d073b1dSJohn Baldwin     return (error);
24911d073b1dSJohn Baldwin }
24921d073b1dSJohn Baldwin 
24939b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
24949b937d48SNate Lawson void
24959b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
24969b937d48SNate Lawson {
24979b937d48SNate Lawson     char		notify_buf[16];
24989b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
24999b937d48SNate Lawson     ACPI_STATUS		status;
25009b937d48SNate Lawson 
25019b937d48SNate Lawson     if (subsystem == NULL)
25029b937d48SNate Lawson 	return;
25039b937d48SNate Lawson 
25049b937d48SNate Lawson     handle_buf.Pointer = NULL;
25059b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
25069b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
25079b937d48SNate Lawson     if (ACPI_FAILURE(status))
25089b937d48SNate Lawson 	return;
25099b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
25109b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
25119b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
25129b937d48SNate Lawson }
25139b937d48SNate Lawson 
251415e32d5dSMike Smith #ifdef ACPI_DEBUG
25150ae55423SMike Smith /*
25160ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
25170ae55423SMike Smith  *
25180ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
25190ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
25200ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
25210ae55423SMike Smith  * prefixing the bit name with !.
25220ae55423SMike Smith  */
252315e32d5dSMike Smith struct debugtag
252415e32d5dSMike Smith {
252515e32d5dSMike Smith     char	*name;
252615e32d5dSMike Smith     UINT32	value;
252715e32d5dSMike Smith };
252815e32d5dSMike Smith 
252915e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2530c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2531c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2532c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2533c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2534c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2535c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2536c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2537c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2538c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2539d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
25404c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2541d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2542297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
25434c1cdee6SMike Smith 
2544c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2545c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
25463184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2547c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
25483184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
25493184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
25503184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
25514c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2552cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
25533184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2554b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
255515e32d5dSMike Smith     {NULL, 0}
255615e32d5dSMike Smith };
255715e32d5dSMike Smith 
255815e32d5dSMike Smith static struct debugtag dbg_level[] = {
25594c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
25609501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
25619501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
25624c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
25639501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
25644c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2565d62ab2f4SMitsuru IWASAKI 
2566d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2567297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
25684c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
25694c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2570d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
25714c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
25724c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
25734c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
25744c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
25754c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
25764c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
25774c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
25784c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
25794c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
25804c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2581d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2582d62ab2f4SMitsuru IWASAKI 
2583d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2584d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2585d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2586d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2587d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
25884c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2589d62ab2f4SMitsuru IWASAKI 
2590d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2591d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2592d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2593d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2594d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2595d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2596d62ab2f4SMitsuru IWASAKI 
2597d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
259898479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
259998479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
260098479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
260198479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
260298479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
260315e32d5dSMike Smith     {NULL, 0}
260415e32d5dSMike Smith };
260515e32d5dSMike Smith 
260615e32d5dSMike Smith static void
260715e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
260815e32d5dSMike Smith {
260915e32d5dSMike Smith     char	*ep;
261015e32d5dSMike Smith     int		i, l;
26110ae55423SMike Smith     int		set;
261215e32d5dSMike Smith 
261315e32d5dSMike Smith     while (*cp) {
261415e32d5dSMike Smith 	if (isspace(*cp)) {
261515e32d5dSMike Smith 	    cp++;
261615e32d5dSMike Smith 	    continue;
261715e32d5dSMike Smith 	}
261815e32d5dSMike Smith 	ep = cp;
261915e32d5dSMike Smith 	while (*ep && !isspace(*ep))
262015e32d5dSMike Smith 	    ep++;
26210ae55423SMike Smith 	if (*cp == '!') {
26220ae55423SMike Smith 	    set = 0;
26230ae55423SMike Smith 	    cp++;
26240ae55423SMike Smith 	    if (cp == ep)
26250ae55423SMike Smith 		continue;
26260ae55423SMike Smith 	} else {
26270ae55423SMike Smith 	    set = 1;
26280ae55423SMike Smith 	}
262915e32d5dSMike Smith 	l = ep - cp;
263015e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
263115e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2632be2b1797SNate Lawson 		if (set)
263315e32d5dSMike Smith 		    *flag |= tag[i].value;
2634be2b1797SNate Lawson 		else
26350ae55423SMike Smith 		    *flag &= ~tag[i].value;
263615e32d5dSMike Smith 	    }
263715e32d5dSMike Smith 	}
263815e32d5dSMike Smith 	cp = ep;
263915e32d5dSMike Smith     }
264015e32d5dSMike Smith }
264115e32d5dSMike Smith 
264215e32d5dSMike Smith static void
26432a4ac806SMike Smith acpi_set_debugging(void *junk)
264415e32d5dSMike Smith {
26457e639165SNate Lawson     char	*layer, *level;
264615e32d5dSMike Smith 
26471d7b121cSNate Lawson     if (cold) {
26480ae55423SMike Smith 	AcpiDbgLayer = 0;
26490ae55423SMike Smith 	AcpiDbgLevel = 0;
26501d7b121cSNate Lawson     }
26511d7b121cSNate Lawson 
26527e639165SNate Lawson     layer = getenv("debug.acpi.layer");
26537e639165SNate Lawson     level = getenv("debug.acpi.level");
26547e639165SNate Lawson     if (layer == NULL && level == NULL)
26557e639165SNate Lawson 	return;
26560ae55423SMike Smith 
26577e639165SNate Lawson     printf("ACPI set debug");
26587e639165SNate Lawson     if (layer != NULL) {
26597e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
26607e639165SNate Lawson 	    printf(" layer '%s'", layer);
26617e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
26627e639165SNate Lawson 	freeenv(layer);
26631d7b121cSNate Lawson     }
26647e639165SNate Lawson     if (level != NULL) {
26657e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
26667e639165SNate Lawson 	    printf(" level '%s'", level);
26677e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
26687e639165SNate Lawson 	freeenv(level);
26697e639165SNate Lawson     }
26707e639165SNate Lawson     printf("\n");
267115e32d5dSMike Smith }
2672bee4aa4aSNate Lawson 
2673be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2674be2b1797SNate Lawson 	NULL);
26751d7b121cSNate Lawson 
26761d7b121cSNate Lawson static int
26771d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
26781d7b121cSNate Lawson {
267954f6bca0SNate Lawson     int		 error, *dbg;
26801d7b121cSNate Lawson     struct	 debugtag *tag;
268154f6bca0SNate Lawson     struct	 sbuf sb;
26821d7b121cSNate Lawson 
268354f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
268454f6bca0SNate Lawson 	return (ENOMEM);
26851d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
26861d7b121cSNate Lawson 	tag = &dbg_layer[0];
26871d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
26881d7b121cSNate Lawson     } else {
26891d7b121cSNate Lawson 	tag = &dbg_level[0];
26901d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
26911d7b121cSNate Lawson     }
26921d7b121cSNate Lawson 
26931d7b121cSNate Lawson     /* Get old values if this is a get request. */
26941d7b121cSNate Lawson     if (*dbg == 0) {
269554f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
26961d7b121cSNate Lawson     } else if (req->newptr == NULL) {
26971d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
269854f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
269954f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
27001d7b121cSNate Lawson 	}
27011d7b121cSNate Lawson     }
270254f6bca0SNate Lawson     sbuf_trim(&sb);
270354f6bca0SNate Lawson     sbuf_finish(&sb);
27041d7b121cSNate Lawson 
27057e639165SNate Lawson     /* Copy out the old values to the user. */
27067e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
270754f6bca0SNate Lawson     sbuf_delete(&sb);
27081d7b121cSNate Lawson 
27091d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
27101d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
27111d7b121cSNate Lawson 	*dbg = 0;
27121d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
27131d7b121cSNate Lawson 	acpi_set_debugging(NULL);
27141d7b121cSNate Lawson     }
27151d7b121cSNate Lawson 
27161d7b121cSNate Lawson     return (error);
27171d7b121cSNate Lawson }
2718bee4aa4aSNate Lawson 
27191d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
27201d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
27211d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
27221d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
2723bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
2724f9390180SMitsuru IWASAKI 
2725f9390180SMitsuru IWASAKI static int
2726f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2727f9390180SMitsuru IWASAKI {
2728f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2729f9390180SMitsuru IWASAKI 	int	error;
2730f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2731f9390180SMitsuru IWASAKI 	va_list	ap;
2732f9390180SMitsuru IWASAKI 
2733f9390180SMitsuru IWASAKI 	error = 0;
2734f9390180SMitsuru IWASAKI 	switch (cmd) {
2735f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2736f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2737f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2738f9390180SMitsuru IWASAKI 			error = EINVAL;
2739f9390180SMitsuru IWASAKI 			goto out;
2740f9390180SMitsuru IWASAKI 		}
2741f9390180SMitsuru IWASAKI 
2742f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2743f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2744f9390180SMitsuru IWASAKI 		va_end(ap);
2745f9390180SMitsuru IWASAKI 
2746f9390180SMitsuru IWASAKI 		switch (state) {
2747f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2748f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2749f9390180SMitsuru IWASAKI 			break;
2750f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2751f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2752f9390180SMitsuru IWASAKI 			break;
2753f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2754f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2755f9390180SMitsuru IWASAKI 			break;
2756f9390180SMitsuru IWASAKI 		default:
2757f9390180SMitsuru IWASAKI 			error = EINVAL;
2758f9390180SMitsuru IWASAKI 			goto out;
2759f9390180SMitsuru IWASAKI 		}
2760f9390180SMitsuru IWASAKI 
2761f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2762f9390180SMitsuru IWASAKI 		break;
2763f9390180SMitsuru IWASAKI 	default:
2764f9390180SMitsuru IWASAKI 		error = EINVAL;
2765f9390180SMitsuru IWASAKI 		goto out;
2766f9390180SMitsuru IWASAKI 	}
2767f9390180SMitsuru IWASAKI 
2768f9390180SMitsuru IWASAKI out:
2769f9390180SMitsuru IWASAKI 	return (error);
2770f9390180SMitsuru IWASAKI }
2771f9390180SMitsuru IWASAKI 
2772f9390180SMitsuru IWASAKI static void
2773f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2774f9390180SMitsuru IWASAKI {
2775be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
27766c407052SMitsuru IWASAKI 	return;
2777f9390180SMitsuru IWASAKI 
2778f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2779f9390180SMitsuru IWASAKI }
2780f9390180SMitsuru IWASAKI 
2781f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2782