xref: /freebsd/sys/dev/acpica/acpi.c (revision 1b8c233de3523a560244e847a7d5451eb068402a)
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>
3815e32d5dSMike Smith #include <sys/bus.h>
3915e32d5dSMike Smith #include <sys/conf.h>
4015e32d5dSMike Smith #include <sys/ioccom.h>
4115e32d5dSMike Smith #include <sys/reboot.h>
4215e32d5dSMike Smith #include <sys/sysctl.h>
4315e32d5dSMike Smith #include <sys/ctype.h>
441611ea87SMitsuru IWASAKI #include <sys/linker.h>
45f9390180SMitsuru IWASAKI #include <sys/power.h>
4654f6bca0SNate Lawson #include <sys/sbuf.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/clock.h>
4915e32d5dSMike Smith #include <machine/resource.h>
50dc750869SNate Lawson #include <machine/bus.h>
51dc750869SNate Lawson #include <sys/rman.h>
52bc0f2195SMike Smith #include <isa/isavar.h>
53bc0f2195SMike Smith 
5415e32d5dSMike Smith #include "acpi.h"
5515e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5615e32d5dSMike Smith #include <dev/acpica/acpiio.h>
579b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
5815e32d5dSMike Smith 
5915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6015e32d5dSMike Smith 
61be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
62cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
63b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
640ae55423SMike Smith 
6515e32d5dSMike Smith static d_open_t		acpiopen;
6615e32d5dSMike Smith static d_close_t	acpiclose;
6715e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6815e32d5dSMike Smith 
6915e32d5dSMike Smith #define CDEV_MAJOR 152
7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
717ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
727ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
737ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
747ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
757ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
7615e32d5dSMike Smith };
7715e32d5dSMike Smith 
781d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
79b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
801d073b1dSJohn Baldwin 
812a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
822a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
832a4ac806SMike Smith 
84fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
85cb9b0d80SMike Smith struct mtx	acpi_mutex;
86fc0ea94aSJohn Baldwin #endif
87cb9b0d80SMike Smith 
886d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
8915e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9015e32d5dSMike Smith static int	acpi_probe(device_t dev);
9115e32d5dSMike Smith static int	acpi_attach(device_t dev);
92be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
93be2b1797SNate Lawson 			int unit);
9415e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
95be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
96be2b1797SNate Lawson 			uintptr_t *result);
97be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
98be2b1797SNate Lawson 			uintptr_t value);
99be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
100be2b1797SNate Lawson 			int rid, u_long start, u_long count);
101be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
102be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
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);
110be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
111be2b1797SNate Lawson 			struct isa_pnp_id *ids);
11215e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
113be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
114be2b1797SNate Lawson 			void *context, void **status);
11515e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11615e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
11815e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
11915e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
120d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1211d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
122f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
123f9390180SMitsuru IWASAKI 
12415e32d5dSMike Smith static device_method_t acpi_methods[] = {
12515e32d5dSMike Smith     /* Device interface */
12615e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
12715e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
12815e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
12956a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
13015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
13215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
13315e32d5dSMike Smith 
13415e32d5dSMike Smith     /* Bus interface */
13515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
13715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
13815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
13915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
14215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
14315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
14815e32d5dSMike Smith 
149bc0f2195SMike Smith     /* ISA emulation */
150bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
151bc0f2195SMike Smith 
15215e32d5dSMike Smith     {0, 0}
15315e32d5dSMike Smith };
15415e32d5dSMike Smith 
15515e32d5dSMike Smith static driver_t acpi_driver = {
15615e32d5dSMike Smith     "acpi",
15715e32d5dSMike Smith     acpi_methods,
15815e32d5dSMike Smith     sizeof(struct acpi_softc),
15915e32d5dSMike Smith };
16015e32d5dSMike Smith 
1613273b005SMike Smith static devclass_t acpi_devclass;
1626d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
163dde24897SMike Smith MODULE_VERSION(acpi, 100);
16415e32d5dSMike Smith 
1651d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1661d7b121cSNate Lawson static char acpi_ca_version[12];
1671d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1681d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
16915e32d5dSMike Smith 
17015e32d5dSMike Smith /*
1716d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1726d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
173be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
174be2b1797SNate Lawson  * off it.
1756d63101aSMike Smith  */
1766d63101aSMike Smith static int
1776d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1786d63101aSMike Smith {
1796d63101aSMike Smith     switch(event) {
1806d63101aSMike Smith     case MOD_LOAD:
18187b45ed5SMitsuru IWASAKI 	if (!cold) {
18287b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
1836d63101aSMike Smith 	    return (EPERM);
18487b45ed5SMitsuru IWASAKI 	}
1856d63101aSMike Smith 	break;
1866d63101aSMike Smith     case MOD_UNLOAD:
187f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1886d63101aSMike Smith 	    return (EBUSY);
189f9390180SMitsuru IWASAKI 	break;
1906d63101aSMike Smith     default:
1916d63101aSMike Smith 	break;
1926d63101aSMike Smith     }
1936d63101aSMike Smith     return (0);
1946d63101aSMike Smith }
1956d63101aSMike Smith 
1966d63101aSMike Smith /*
197bbc2815cSJohn Baldwin  * Perform early initialization.
19815e32d5dSMike Smith  */
199bbc2815cSJohn Baldwin ACPI_STATUS
200bbc2815cSJohn Baldwin acpi_Startup(void)
20115e32d5dSMike Smith {
202d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
203d786139cSMaxime Henrion     char *debugpoint;
20415e32d5dSMike Smith #endif
205bbc2815cSJohn Baldwin     static int error, started = 0;
20615e32d5dSMike Smith 
2071b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2081b8c233dSPeter Pentchev 
209bbc2815cSJohn Baldwin     if (started)
210bbc2815cSJohn Baldwin 	return_VALUE(error);
211bbc2815cSJohn Baldwin     started = 1;
21215e32d5dSMike Smith 
213fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
214be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2156008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
216fc0ea94aSJohn Baldwin #endif
217cb9b0d80SMike Smith 
218be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
219d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
220d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
221d786139cSMaxime Henrion     if (debugpoint) {
222d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
22315e32d5dSMike Smith 	    acpi_EnterDebugger();
224d786139cSMaxime Henrion 	freeenv(debugpoint);
225d786139cSMaxime Henrion     }
22615e32d5dSMike Smith #endif
227b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
228bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
229bbc2815cSJohn Baldwin 	return_VALUE(error);
23015e32d5dSMike Smith     }
231d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
232d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
233d786139cSMaxime Henrion     if (debugpoint) {
234d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
23515e32d5dSMike Smith 	    acpi_EnterDebugger();
236d786139cSMaxime Henrion 	freeenv(debugpoint);
237d786139cSMaxime Henrion     }
23815e32d5dSMike Smith #endif
2391611ea87SMitsuru IWASAKI 
240b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
241bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
242bbc2815cSJohn Baldwin 	return_VALUE(error);
24315e32d5dSMike Smith     }
244bbc2815cSJohn Baldwin     return_VALUE(AE_OK);
245bbc2815cSJohn Baldwin }
246bbc2815cSJohn Baldwin 
247bbc2815cSJohn Baldwin /*
248bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
249bbc2815cSJohn Baldwin  */
250bbc2815cSJohn Baldwin static void
251bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
252bbc2815cSJohn Baldwin {
253bbc2815cSJohn Baldwin     device_t	child;
254bbc2815cSJohn Baldwin 
255bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
256bbc2815cSJohn Baldwin 
257bbc2815cSJohn Baldwin     if (!cold)
258bbc2815cSJohn Baldwin 	return_VOID;
259bbc2815cSJohn Baldwin 
260bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
261bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
262bbc2815cSJohn Baldwin 	return_VOID;
263bbc2815cSJohn Baldwin 
264bbc2815cSJohn Baldwin     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "0x%x",
265bbc2815cSJohn Baldwin 	     ACPI_CA_VERSION);
266bbc2815cSJohn Baldwin 
267bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
268bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
269bbc2815cSJohn Baldwin 	return_VOID;
270bbc2815cSJohn Baldwin 
271bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
272bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
273bbc2815cSJohn Baldwin 	return_VOID;
27415e32d5dSMike Smith 
275be2b1797SNate Lawson     /* Attach the actual ACPI device. */
27615e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
27715e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
2780ae55423SMike Smith 	return_VOID;
27915e32d5dSMike Smith     }
28015e32d5dSMike Smith }
28115e32d5dSMike Smith 
28215e32d5dSMike Smith /*
28315e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
28415e32d5dSMike Smith  */
28515e32d5dSMike Smith static int
28615e32d5dSMike Smith acpi_probe(device_t dev)
28715e32d5dSMike Smith {
28815e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
28915e32d5dSMike Smith     char		buf[20];
29015e32d5dSMike Smith     int			error;
2912ccd1cacSNate Lawson     struct sbuf		sb;
2922ccd1cacSNate Lawson     ACPI_STATUS		status;
293fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
29415e32d5dSMike Smith 
295b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
296f9390180SMitsuru IWASAKI 
297f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
298f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
299be2b1797SNate Lawson 
300f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
301f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
302f9390180SMitsuru IWASAKI     }
303f9390180SMitsuru IWASAKI 
304cb9b0d80SMike Smith     ACPI_LOCK;
3050ae55423SMike Smith 
306b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
307be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
308be2b1797SNate Lawson 		      AcpiFormatException(status));
309cb9b0d80SMike Smith 	error = ENXIO;
310cb9b0d80SMike Smith     } else {
3112ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3122ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3132ccd1cacSNate Lawson 	sbuf_trim(&sb);
3142ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3152ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3162ccd1cacSNate Lawson 	sbuf_trim(&sb);
3172ccd1cacSNate Lawson 	sbuf_finish(&sb);
3182ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3192ccd1cacSNate Lawson 	sbuf_delete(&sb);
320cb9b0d80SMike Smith 	error = 0;
321cb9b0d80SMike Smith     }
322cb9b0d80SMike Smith     ACPI_UNLOCK;
323cb9b0d80SMike Smith     return_VALUE(error);
32415e32d5dSMike Smith }
32515e32d5dSMike Smith 
32615e32d5dSMike Smith static int
32715e32d5dSMike Smith acpi_attach(device_t dev)
32815e32d5dSMike Smith {
32915e32d5dSMike Smith     struct acpi_softc	*sc;
330cb9b0d80SMike Smith     ACPI_STATUS		status;
33115e32d5dSMike Smith     int			error;
33232d18aa5SMike Smith     UINT32		flags;
333498d464fSMitsuru IWASAKI     char		*env;
334d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
335d786139cSMaxime Henrion     char		*debugpoint;
33615e32d5dSMike Smith #endif
337fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
33815e32d5dSMike Smith 
339b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
340cb9b0d80SMike Smith     ACPI_LOCK;
34115e32d5dSMike Smith     sc = device_get_softc(dev);
34215e32d5dSMike Smith     bzero(sc, sizeof(*sc));
34315e32d5dSMike Smith     sc->acpi_dev = dev;
34415e32d5dSMike Smith 
345d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
346d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
347d786139cSMaxime Henrion     if (debugpoint) {
348d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
34915e32d5dSMike Smith 	    acpi_EnterDebugger();
350d786139cSMaxime Henrion 	freeenv(debugpoint);
351d786139cSMaxime Henrion     }
35215e32d5dSMike Smith #endif
35315e32d5dSMike Smith 
354be2b1797SNate Lawson     /* Install the default address space handlers. */
355cb9b0d80SMike Smith     error = ENXIO;
356be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
357be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
358be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
359be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
360be2b1797SNate Lawson 		      AcpiFormatException(status));
361cb9b0d80SMike Smith 	goto out;
36215e32d5dSMike Smith     }
363be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
364be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
365be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
366be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
367be2b1797SNate Lawson 		      AcpiFormatException(status));
368cb9b0d80SMike Smith 	goto out;
36915e32d5dSMike Smith     }
370be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
371be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
372be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
373be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
374be2b1797SNate Lawson 		      AcpiFormatException(status));
375cb9b0d80SMike Smith 	goto out;
37615e32d5dSMike Smith     }
37715e32d5dSMike Smith 
37815e32d5dSMike Smith     /*
37915e32d5dSMike Smith      * Bring ACPI fully online.
38015e32d5dSMike Smith      *
381be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
382be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
383be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
384be2b1797SNate Lawson      * object init pass.
38515e32d5dSMike Smith      *
38632d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
38732d18aa5SMike Smith      *
388be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
389be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
39015e32d5dSMike Smith      */
391d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
392d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
393d786139cSMaxime Henrion     if (debugpoint) {
394d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
39515e32d5dSMike Smith 	    acpi_EnterDebugger();
396d786139cSMaxime Henrion 	freeenv(debugpoint);
397d786139cSMaxime Henrion     }
39815e32d5dSMike Smith #endif
39932d18aa5SMike Smith     flags = 0;
400d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
40132d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
402b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
403be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
404be2b1797SNate Lawson 		      AcpiFormatException(status));
405cb9b0d80SMike Smith 	goto out;
40615e32d5dSMike Smith     }
40715e32d5dSMike Smith 
408f8335e3aSNate Lawson     /*
409f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
410f8335e3aSNate Lawson      * the namespace has been evaluated.
411f8335e3aSNate Lawson      */
412f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
413f8335e3aSNate Lawson 
414b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
415be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
416be2b1797SNate Lawson 		      AcpiFormatException(status));
417b69ed3f4SMitsuru IWASAKI 	goto out;
418b69ed3f4SMitsuru IWASAKI     }
419b69ed3f4SMitsuru IWASAKI 
42015e32d5dSMike Smith     /*
4211d073b1dSJohn Baldwin      * Setup our sysctl tree.
4221d073b1dSJohn Baldwin      *
4231d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4241d073b1dSJohn Baldwin      */
4251d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4261d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4271d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4281d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4291d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
430d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
431d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
432d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4331d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4341d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4351d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4361d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4371d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4381d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4391d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4401d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
441f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
442f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
443f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
444f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
445f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
446f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4472d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
448ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
449ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
450ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4511611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4521611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4531611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4542d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4552d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
456c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
457c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
458c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
459bf0c18ecSNate Lawson 
460bf0c18ecSNate Lawson     /*
461bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
462bf0c18ecSNate Lawson      * stabilize.
463bf0c18ecSNate Lawson      */
464bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
465c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
4662d644607SMitsuru IWASAKI     if (bootverbose)
4672d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
468498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
469498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
470498d464fSMitsuru IWASAKI 	freeenv(env);
471498d464fSMitsuru IWASAKI     }
4721d073b1dSJohn Baldwin 
4732d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
4742d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
4752d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
4762d610c46SNate Lawson 
4771d073b1dSJohn Baldwin     /*
47815e32d5dSMike Smith      * Dispatch the default sleep state to devices.
47915e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
48015e32d5dSMike Smith      */
48115e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
48215e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
48315e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
484f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
485f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
48615e32d5dSMike Smith 
48713d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
48815e32d5dSMike Smith 
48915e32d5dSMike Smith     /*
49015e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
49115e32d5dSMike Smith      */
492d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
493d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
494d786139cSMaxime Henrion     if (debugpoint) {
495d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
49615e32d5dSMike Smith 	    acpi_EnterDebugger();
497d786139cSMaxime Henrion 	freeenv(debugpoint);
498d786139cSMaxime Henrion     }
49915e32d5dSMike Smith #endif
50015e32d5dSMike Smith 
501be2b1797SNate Lawson     /* Register our shutdown handlers */
502be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
503be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
504be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
505be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
50615e32d5dSMike Smith 
50715e32d5dSMike Smith     /*
50815e32d5dSMike Smith      * Register our acpi event handlers.
50915e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
51015e32d5dSMike Smith      */
511be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
512be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
513be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
514be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
51515e32d5dSMike Smith 
516be2b1797SNate Lawson     /* Flag our initial states. */
51715e32d5dSMike Smith     sc->acpi_enabled = 1;
51815e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
519ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
52015e32d5dSMike Smith 
521be2b1797SNate Lawson     /* Create the control device */
522a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5232a4689e8SRobert Watson 			      "acpi");
52415e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
52515e32d5dSMike Smith 
526d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
527d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
528d786139cSMaxime Henrion     if (debugpoint) {
529be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
53015e32d5dSMike Smith 	    acpi_EnterDebugger();
531d786139cSMaxime Henrion 	freeenv(debugpoint);
532d786139cSMaxime Henrion     }
53315e32d5dSMike Smith #endif
534f86214b6SMitsuru IWASAKI 
53591da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
536be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
537c573e654SMitsuru IWASAKI 	goto out;
538c573e654SMitsuru IWASAKI #endif
539c573e654SMitsuru IWASAKI 
540be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
541f86214b6SMitsuru IWASAKI 	goto out;
542f86214b6SMitsuru IWASAKI 
543f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
544f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
545f9390180SMitsuru IWASAKI 
546eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
547eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
548eeb6dba2SJohn Baldwin 
549cb9b0d80SMike Smith     error = 0;
550cb9b0d80SMike Smith 
551cb9b0d80SMike Smith  out:
552cb9b0d80SMike Smith     ACPI_UNLOCK;
553cb9b0d80SMike Smith     return_VALUE (error);
55415e32d5dSMike Smith }
55515e32d5dSMike Smith 
55615e32d5dSMike Smith /*
55715e32d5dSMike Smith  * Handle a new device being added
55815e32d5dSMike Smith  */
55915e32d5dSMike Smith static device_t
56015e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
56115e32d5dSMike Smith {
56215e32d5dSMike Smith     struct acpi_device	*ad;
56315e32d5dSMike Smith     device_t		child;
56415e32d5dSMike Smith 
565be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
56615e32d5dSMike Smith 	return (NULL);
56715e32d5dSMike Smith 
56815e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
56915e32d5dSMike Smith 
57015e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
57115e32d5dSMike Smith     if (child != NULL)
57215e32d5dSMike Smith 	device_set_ivars(child, ad);
57315e32d5dSMike Smith     return (child);
57415e32d5dSMike Smith }
57515e32d5dSMike Smith 
57615e32d5dSMike Smith static int
57715e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
57815e32d5dSMike Smith {
57915e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
58015e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
58115e32d5dSMike Smith     int retval = 0;
58215e32d5dSMike Smith 
58315e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5849ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5859ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5869ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5879ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
58815e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
58915e32d5dSMike Smith 
59015e32d5dSMike Smith     return (retval);
59115e32d5dSMike Smith }
59215e32d5dSMike Smith 
59315e32d5dSMike Smith 
59415e32d5dSMike Smith /*
59515e32d5dSMike Smith  * Handle per-device ivars
59615e32d5dSMike Smith  */
59715e32d5dSMike Smith static int
59815e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
59915e32d5dSMike Smith {
60015e32d5dSMike Smith     struct acpi_device	*ad;
60115e32d5dSMike Smith 
60215e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
60315e32d5dSMike Smith 	printf("device has no ivars\n");
60415e32d5dSMike Smith 	return (ENOENT);
60515e32d5dSMike Smith     }
60615e32d5dSMike Smith 
607be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
60815e32d5dSMike Smith     switch(index) {
60915e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61015e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
61115e32d5dSMike Smith 	break;
61215e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
61315e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
61415e32d5dSMike Smith 	break;
61515e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
61615e32d5dSMike Smith 	*(void **)result = ad->ad_private;
61715e32d5dSMike Smith 	break;
61832d18aa5SMike Smith     case ISA_IVAR_VENDORID:
61932d18aa5SMike Smith     case ISA_IVAR_SERIAL:
62032d18aa5SMike Smith     case ISA_IVAR_COMPATID:
62132d18aa5SMike Smith 	*(int *)result = -1;
62232d18aa5SMike Smith 	break;
62332d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
62432d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
62532d18aa5SMike Smith 	break;
62615e32d5dSMike Smith     default:
62715e32d5dSMike Smith 	return (ENOENT);
62815e32d5dSMike Smith     }
629be2b1797SNate Lawson 
63015e32d5dSMike Smith     return (0);
63115e32d5dSMike Smith }
63215e32d5dSMike Smith 
63315e32d5dSMike Smith static int
63415e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
63515e32d5dSMike Smith {
63615e32d5dSMike Smith     struct acpi_device	*ad;
63715e32d5dSMike Smith 
63815e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
63915e32d5dSMike Smith 	printf("device has no ivars\n");
64015e32d5dSMike Smith 	return (ENOENT);
64115e32d5dSMike Smith     }
64215e32d5dSMike Smith 
64315e32d5dSMike Smith     switch(index) {
64415e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
64515e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
64615e32d5dSMike Smith 	break;
64715e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
64815e32d5dSMike Smith 	ad->ad_magic = (int)value;
64915e32d5dSMike Smith 	break;
65015e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
65115e32d5dSMike Smith 	ad->ad_private = (void *)value;
65215e32d5dSMike Smith 	break;
65315e32d5dSMike Smith     default:
6542ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
65515e32d5dSMike Smith 	return (ENOENT);
65615e32d5dSMike Smith     }
657be2b1797SNate Lawson 
65815e32d5dSMike Smith     return (0);
65915e32d5dSMike Smith }
66015e32d5dSMike Smith 
661be2b1797SNate Lawson ACPI_HANDLE
662be2b1797SNate Lawson acpi_get_handle(device_t dev)
663be2b1797SNate Lawson {
664be2b1797SNate Lawson     uintptr_t up;
665be2b1797SNate Lawson     ACPI_HANDLE	h;
666be2b1797SNate Lawson 
667be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
668be2b1797SNate Lawson 	return(NULL);
669be2b1797SNate Lawson     h = (ACPI_HANDLE)up;
670be2b1797SNate Lawson     return (h);
671be2b1797SNate Lawson }
672be2b1797SNate Lawson 
673be2b1797SNate Lawson int
674be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h)
675be2b1797SNate Lawson {
676be2b1797SNate Lawson     uintptr_t up;
677be2b1797SNate Lawson 
678be2b1797SNate Lawson     up = (uintptr_t)h;
679be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
680be2b1797SNate Lawson }
681be2b1797SNate Lawson 
682be2b1797SNate Lawson int
683be2b1797SNate Lawson acpi_get_magic(device_t dev)
684be2b1797SNate Lawson {
685be2b1797SNate Lawson     uintptr_t up;
686be2b1797SNate Lawson     int	m;
687be2b1797SNate Lawson 
688be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
689be2b1797SNate Lawson 	return(0);
690be2b1797SNate Lawson     m = (int)up;
691be2b1797SNate Lawson     return (m);
692be2b1797SNate Lawson }
693be2b1797SNate Lawson 
694be2b1797SNate Lawson int
695be2b1797SNate Lawson acpi_set_magic(device_t dev, int m)
696be2b1797SNate Lawson {
697be2b1797SNate Lawson     uintptr_t up;
698be2b1797SNate Lawson 
699be2b1797SNate Lawson     up = (uintptr_t)m;
700be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
701be2b1797SNate Lawson }
702be2b1797SNate Lawson 
703be2b1797SNate Lawson void *
704be2b1797SNate Lawson acpi_get_private(device_t dev)
705be2b1797SNate Lawson {
706be2b1797SNate Lawson     uintptr_t up;
707be2b1797SNate Lawson     void *p;
708be2b1797SNate Lawson 
709be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
710be2b1797SNate Lawson 	return (NULL);
711be2b1797SNate Lawson     p = (void *)up;
712be2b1797SNate Lawson     return (p);
713be2b1797SNate Lawson }
714be2b1797SNate Lawson 
715be2b1797SNate Lawson int
716be2b1797SNate Lawson acpi_set_private(device_t dev, void *p)
717be2b1797SNate Lawson {
718be2b1797SNate Lawson     uintptr_t up;
719be2b1797SNate Lawson 
720be2b1797SNate Lawson     up = (uintptr_t)p;
721be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
722be2b1797SNate Lawson }
723be2b1797SNate Lawson 
724be2b1797SNate Lawson ACPI_OBJECT_TYPE
725be2b1797SNate Lawson acpi_get_type(device_t dev)
726be2b1797SNate Lawson {
727be2b1797SNate Lawson     ACPI_HANDLE		h;
728be2b1797SNate Lawson     ACPI_OBJECT_TYPE	t;
729be2b1797SNate Lawson 
730be2b1797SNate Lawson     if ((h = acpi_get_handle(dev)) == NULL)
731be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
732be2b1797SNate Lawson     if (AcpiGetType(h, &t) != AE_OK)
733be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
734be2b1797SNate Lawson     return (t);
735be2b1797SNate Lawson }
736be2b1797SNate Lawson 
73715e32d5dSMike Smith /*
73815e32d5dSMike Smith  * Handle child resource allocation/removal
73915e32d5dSMike Smith  */
74015e32d5dSMike Smith static int
741be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
742be2b1797SNate Lawson 		  u_long start, u_long count)
74315e32d5dSMike Smith {
74415e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
74515e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
74615e32d5dSMike Smith 
74715e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
74815e32d5dSMike Smith 
74915e32d5dSMike Smith     return(0);
75015e32d5dSMike Smith }
75115e32d5dSMike Smith 
75215e32d5dSMike Smith static int
753be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
754be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
75515e32d5dSMike Smith {
75615e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
75715e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
75815e32d5dSMike Smith     struct resource_list_entry	*rle;
75915e32d5dSMike Smith 
76015e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
76115e32d5dSMike Smith     if (!rle)
76215e32d5dSMike Smith 	return(ENOENT);
76315e32d5dSMike Smith 
76415e32d5dSMike Smith     if (startp)
76515e32d5dSMike Smith 	*startp = rle->start;
76615e32d5dSMike Smith     if (countp)
76715e32d5dSMike Smith 	*countp = rle->count;
76815e32d5dSMike Smith 
76915e32d5dSMike Smith     return (0);
77015e32d5dSMike Smith }
77115e32d5dSMike Smith 
77215e32d5dSMike Smith static struct resource *
77315e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
77415e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
77515e32d5dSMike Smith {
77615e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
77715e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
77815e32d5dSMike Smith 
779be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
780be2b1797SNate Lawson 	    flags));
78115e32d5dSMike Smith }
78215e32d5dSMike Smith 
78315e32d5dSMike Smith static int
78415e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
78515e32d5dSMike Smith {
78615e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
78715e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
78815e32d5dSMike Smith 
78915e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
79015e32d5dSMike Smith }
79115e32d5dSMike Smith 
792dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
793dc750869SNate Lawson struct resource *
794dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
795dc750869SNate Lawson {
796dc750869SNate Lawson     int type;
797dc750869SNate Lawson 
798dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
799dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
800dc750869SNate Lawson 	return (NULL);
801dc750869SNate Lawson 
802dc750869SNate Lawson     switch (gas->AddressSpaceId) {
803dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
804dc750869SNate Lawson 	type = SYS_RES_MEMORY;
805dc750869SNate Lawson 	break;
806dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
807dc750869SNate Lawson 	type = SYS_RES_IOPORT;
808dc750869SNate Lawson 	break;
809dc750869SNate Lawson     default:
810dc750869SNate Lawson 	return (NULL);
811dc750869SNate Lawson     }
812dc750869SNate Lawson 
813dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
814dc750869SNate Lawson     return (bus_alloc_resource(dev, type, rid, 0, ~0, 1, RF_ACTIVE));
815dc750869SNate Lawson }
816dc750869SNate Lawson 
81715e32d5dSMike Smith /*
818bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
819bc0f2195SMike Smith  */
820bc0f2195SMike Smith #define PNP_EISAID(s)				\
821bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
822bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
823bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
824bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
825bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
826bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
827bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
828bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
829bc0f2195SMike Smith 
8301e4925e8SNate Lawson static uint32_t
83132d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
832bc0f2195SMike Smith {
8331e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
8341e4925e8SNate Lawson     ACPI_BUFFER		buf;
835bc0f2195SMike Smith     ACPI_HANDLE		h;
836bc0f2195SMike Smith     ACPI_STATUS		error;
837bc0f2195SMike Smith     u_int32_t		pnpid;
838fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
83932d18aa5SMike Smith 
840b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
84132d18aa5SMike Smith 
84232d18aa5SMike Smith     pnpid = 0;
843bd189fe7SNate Lawson     buf.Pointer = NULL;
844bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
845bd189fe7SNate Lawson 
84632d18aa5SMike Smith     ACPI_LOCK;
84732d18aa5SMike Smith 
8486fca9360SNate Lawson     /* Fetch and validate the HID. */
84932d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
850bd189fe7SNate Lawson 	goto out;
8516fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
8526fca9360SNate Lawson     if (ACPI_FAILURE(error))
853bd189fe7SNate Lawson 	goto out;
8541e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
85532d18aa5SMike Smith 
8561e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
8571e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
858be2b1797SNate Lawson 
859bd189fe7SNate Lawson out:
860bd189fe7SNate Lawson     if (buf.Pointer != NULL)
8611e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
86232d18aa5SMike Smith     ACPI_UNLOCK;
86332d18aa5SMike Smith     return_VALUE (pnpid);
86432d18aa5SMike Smith }
86532d18aa5SMike Smith 
8661e4925e8SNate Lawson static int
8671e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
868b2566207STakanori Watanabe {
8691e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
8701e4925e8SNate Lawson     ACPI_BUFFER		buf;
871b2566207STakanori Watanabe     ACPI_HANDLE		h;
872b2566207STakanori Watanabe     ACPI_STATUS		error;
8731e4925e8SNate Lawson     uint32_t		*pnpid;
8741e4925e8SNate Lawson     int			valid, i;
875b2566207STakanori Watanabe     ACPI_LOCK_DECL;
876b2566207STakanori Watanabe 
877b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
878b2566207STakanori Watanabe 
8791e4925e8SNate Lawson     pnpid = cids;
8801e4925e8SNate Lawson     valid = 0;
881bd189fe7SNate Lawson     buf.Pointer = NULL;
882bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
883bd189fe7SNate Lawson 
884b2566207STakanori Watanabe     ACPI_LOCK;
885b2566207STakanori Watanabe 
8861e4925e8SNate Lawson     /* Fetch and validate the CID */
887b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
888bd189fe7SNate Lawson 	goto out;
8891e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
8901e4925e8SNate Lawson     if (ACPI_FAILURE(error))
891bd189fe7SNate Lawson 	goto out;
8921e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
8931e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
894b2566207STakanori Watanabe 	goto out;
895b2566207STakanori Watanabe 
8961e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
8971e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
8981e4925e8SNate Lawson     for (i = 0; i < count; i++) {
8991e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9001e4925e8SNate Lawson 	    continue;
9011e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9021e4925e8SNate Lawson 	valid++;
903b2566207STakanori Watanabe     }
904b2566207STakanori Watanabe 
9051e4925e8SNate Lawson out:
906bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9071e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
9081e4925e8SNate Lawson     ACPI_UNLOCK;
9091e4925e8SNate Lawson     return_VALUE (valid);
9101e4925e8SNate Lawson }
911b2566207STakanori Watanabe 
91232d18aa5SMike Smith static int
91332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
91432d18aa5SMike Smith {
9151e4925e8SNate Lawson     int			result, cid_count, i;
9161e4925e8SNate Lawson     uint32_t		lid, cids[8];
917bc0f2195SMike Smith 
918b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
919bc0f2195SMike Smith 
920bc0f2195SMike Smith     /*
921bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
922bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
923bc0f2195SMike Smith      * that to happen, so don't ever return it.
924bc0f2195SMike Smith      */
925bc0f2195SMike Smith     result = ENXIO;
926bc0f2195SMike Smith 
927be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
928b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
9291e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
930bc0f2195SMike Smith     while (ids && ids->ip_id) {
9311e4925e8SNate Lawson 	if (lid == ids->ip_id) {
932bc0f2195SMike Smith 	    result = 0;
933bc0f2195SMike Smith 	    goto out;
934bc0f2195SMike Smith 	}
9351e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
9361e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
9371e4925e8SNate Lawson 		result = 0;
9381e4925e8SNate Lawson 		goto out;
9391e4925e8SNate Lawson 	    }
9401e4925e8SNate Lawson 	}
941bc0f2195SMike Smith 	ids++;
942bc0f2195SMike Smith     }
943be2b1797SNate Lawson 
944bc0f2195SMike Smith  out:
945bc0f2195SMike Smith     return_VALUE (result);
946bc0f2195SMike Smith }
947bc0f2195SMike Smith 
948bc0f2195SMike Smith /*
94915e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
95015e32d5dSMike Smith  *
951be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
952be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
95315e32d5dSMike Smith  */
95415e32d5dSMike Smith static void
95515e32d5dSMike Smith acpi_probe_children(device_t bus)
95615e32d5dSMike Smith {
95715e32d5dSMike Smith     ACPI_HANDLE	parent;
958be2b1797SNate Lawson     ACPI_STATUS	status;
9597d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
96015e32d5dSMike Smith     int		i;
96115e32d5dSMike Smith 
962b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
963cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
9640ae55423SMike Smith 
965be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
9664c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
96715e32d5dSMike Smith     bus_generic_probe(bus);
96815e32d5dSMike Smith 
96915e32d5dSMike Smith     /*
97015e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
97115e32d5dSMike Smith      * we find.
97215e32d5dSMike Smith      *
97315e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
974be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
975be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
976be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
97715e32d5dSMike Smith      */
9784c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
979be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
980be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
981be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
982be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
983be2b1797SNate Lawson 			      bus, NULL);
984be2b1797SNate Lawson 	}
985be2b1797SNate Lawson     }
98615e32d5dSMike Smith 
98715e32d5dSMike Smith     /*
98815e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
98915e32d5dSMike Smith      */
9904c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
99115e32d5dSMike Smith     bus_generic_attach(bus);
99215e32d5dSMike Smith 
99315e32d5dSMike Smith     /*
99415e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
99515e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
99615e32d5dSMike Smith      */
9974c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
99815e32d5dSMike Smith     bus_generic_attach(bus);
9990ae55423SMike Smith 
1000bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
10010ae55423SMike Smith     return_VOID;
100215e32d5dSMike Smith }
100315e32d5dSMike Smith 
100415e32d5dSMike Smith /*
100515e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
100615e32d5dSMike Smith  * it.
100715e32d5dSMike Smith  */
100815e32d5dSMike Smith static ACPI_STATUS
100915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
101015e32d5dSMike Smith {
101115e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
101215e32d5dSMike Smith     device_t		child, bus = (device_t)context;
101315e32d5dSMike Smith 
1014b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10150ae55423SMike Smith 
1016be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
10170ae55423SMike Smith     if (acpi_avoid(handle))
10180ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
10190ae55423SMike Smith 
1020b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
102115e32d5dSMike Smith 	switch(type) {
102215e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
102315e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
102415e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
102515e32d5dSMike Smith 	case ACPI_TYPE_POWER:
10260ae55423SMike Smith 	    if (acpi_disabled("children"))
10270ae55423SMike Smith 		break;
1028be2b1797SNate Lawson 
102915e32d5dSMike Smith 	    /*
103015e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
103115e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
103215e32d5dSMike Smith 	     */
1033be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1034be2b1797SNate Lawson 			     acpi_name(handle)));
103515e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1036bc0f2195SMike Smith 	    if (child == NULL)
1037bc0f2195SMike Smith 		break;
103815e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1039bc0f2195SMike Smith 
1040bc0f2195SMike Smith 	    /*
1041b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1042b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1043b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1044b519f9d6SMike Smith 	     */
1045be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1046b519f9d6SMike Smith 		device_disable(child);
1047b519f9d6SMike Smith 		break;
1048b519f9d6SMike Smith 	    }
1049b519f9d6SMike Smith 
1050b519f9d6SMike Smith 	    /*
1051bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1052bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1053bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1054bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1055bc0f2195SMike Smith 	     * device not to have any resources.
1056bc0f2195SMike Smith 	     */
1057bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
1058bc0f2195SMike Smith 
1059be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1060b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
10610ae55423SMike Smith 	    break;
106215e32d5dSMike Smith 	}
106315e32d5dSMike Smith     }
1064be2b1797SNate Lawson 
10650ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
106615e32d5dSMike Smith }
106715e32d5dSMike Smith 
106815e32d5dSMike Smith static void
106915e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
107015e32d5dSMike Smith {
1071c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1072c6a78e98STakanori Watanabe 
1073cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1074cb9b0d80SMike Smith 
107515e32d5dSMike Smith     /*
10760ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
107715e32d5dSMike Smith      * will be turned on again on some laptops.
107815e32d5dSMike Smith      *
107915e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
108015e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
108115e32d5dSMike Smith      *     shutdown process.
108215e32d5dSMike Smith      */
1083c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1084c6a78e98STakanori Watanabe 	acpi_Disable(sc);
108515e32d5dSMike Smith }
108615e32d5dSMike Smith 
108715e32d5dSMike Smith static void
108815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
108915e32d5dSMike Smith {
109015e32d5dSMike Smith     ACPI_STATUS	status;
109115e32d5dSMike Smith 
1092cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1093cb9b0d80SMike Smith 
1094be2b1797SNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1095e1a90ae1SNate Lawson 	printf("Powering system off using ACPI\n");
1096be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(acpi_off_state);
1097be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1098b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1099b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
1100b9c780d6SMitsuru IWASAKI 	    return;
1101b9c780d6SMitsuru IWASAKI 	}
110255398047SNate Lawson 	ACPI_DISABLE_IRQS();
1103be2b1797SNate Lawson 	status = AcpiEnterSleepState(acpi_off_state);
1104be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1105bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
110615e32d5dSMike Smith 	} else {
110715e32d5dSMike Smith 	    DELAY(1000000);
110815e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
110915e32d5dSMike Smith 	}
1110494ae560SMitsuru IWASAKI     } else {
1111e1a90ae1SNate Lawson 	printf("Shutting down ACPI\n");
1112494ae560SMitsuru IWASAKI 	AcpiTerminate();
111315e32d5dSMike Smith     }
111415e32d5dSMike Smith }
111515e32d5dSMike Smith 
111613d4f7baSMitsuru IWASAKI static void
111713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
111813d4f7baSMitsuru IWASAKI {
111913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
112013d4f7baSMitsuru IWASAKI 
1121cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1122cb9b0d80SMike Smith 
112313d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1124be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
112559ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0);
112659ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
112713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1128be2b1797SNate Lawson 				     acpi_eventhandler_power_button_for_sleep,
1129be2b1797SNate Lawson 				     sc);
1130be2b1797SNate Lawson 	if (first_time)
11316c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
113213d4f7baSMitsuru IWASAKI     }
1133be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
113459ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0);
113559ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
113613d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1137be2b1797SNate Lawson 				     acpi_eventhandler_sleep_button_for_sleep,
1138be2b1797SNate Lawson 				     sc);
1139be2b1797SNate Lawson 	if (first_time)
11406c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
114113d4f7baSMitsuru IWASAKI     }
114213d4f7baSMitsuru IWASAKI 
114313d4f7baSMitsuru IWASAKI     first_time = 0;
114413d4f7baSMitsuru IWASAKI }
114513d4f7baSMitsuru IWASAKI 
114615e32d5dSMike Smith /*
1147c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1148c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1149c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1150c5ba0be4SMike Smith  */
1151c5ba0be4SMike Smith BOOLEAN
1152c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1153c5ba0be4SMike Smith {
11541e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1155c5ba0be4SMike Smith     ACPI_HANDLE		h;
11561e4925e8SNate Lawson     ACPI_BUFFER		buf;
1157c5ba0be4SMike Smith     ACPI_STATUS		error;
11581e4925e8SNate Lawson     int			ret;
1159c5ba0be4SMike Smith 
1160cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1161cb9b0d80SMike Smith 
11621e4925e8SNate Lawson     ret = FALSE;
1163c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1164c5ba0be4SMike Smith 	return (FALSE);
11651e4925e8SNate Lawson     buf.Pointer = NULL;
11661e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
11676fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11686fca9360SNate Lawson     if (ACPI_FAILURE(error))
1169c5ba0be4SMike Smith 	return (FALSE);
11701e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1171be2b1797SNate Lawson 
1172be2b1797SNate Lawson     /* If no _STA method, must be present */
11731e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
11741e4925e8SNate Lawson 	ret = TRUE;
1175be2b1797SNate Lawson 
1176be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
11771e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
11781e4925e8SNate Lawson 	ret = TRUE;
1179be2b1797SNate Lawson 
11801e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
11811e4925e8SNate Lawson     return (ret);
1182c5ba0be4SMike Smith }
1183c5ba0be4SMike Smith 
1184c5ba0be4SMike Smith /*
1185b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1186b53f2771SMike Smith  */
1187b53f2771SMike Smith BOOLEAN
1188b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1189b53f2771SMike Smith {
11901e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1191b53f2771SMike Smith     ACPI_HANDLE		h;
11921e4925e8SNate Lawson     ACPI_BUFFER		buf;
1193b53f2771SMike Smith     ACPI_STATUS		error;
11941e4925e8SNate Lawson     int			ret;
1195b53f2771SMike Smith 
1196b53f2771SMike Smith     ACPI_ASSERTLOCK;
1197b53f2771SMike Smith 
11981e4925e8SNate Lawson     ret = FALSE;
1199b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1200b53f2771SMike Smith 	return (FALSE);
12011e4925e8SNate Lawson     buf.Pointer = NULL;
12021e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12036fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12046fca9360SNate Lawson     if (ACPI_FAILURE(error))
1205b53f2771SMike Smith 	return (FALSE);
12061e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1207be2b1797SNate Lawson 
1208be2b1797SNate Lawson     /* If no _STA method, must be present */
12091e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12101e4925e8SNate Lawson 	ret = TRUE;
1211be2b1797SNate Lawson 
1212be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12131e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
12141e4925e8SNate Lawson 	ret = TRUE;
1215be2b1797SNate Lawson 
12161e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12171e4925e8SNate Lawson     return (ret);
1218b53f2771SMike Smith }
1219b53f2771SMike Smith 
1220b53f2771SMike Smith /*
122115e32d5dSMike Smith  * Match a HID string against a device
122215e32d5dSMike Smith  */
122315e32d5dSMike Smith BOOLEAN
122415e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
122515e32d5dSMike Smith {
12261e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
122715e32d5dSMike Smith     ACPI_HANDLE		h;
12281e4925e8SNate Lawson     ACPI_BUFFER		buf;
122915e32d5dSMike Smith     ACPI_STATUS		error;
12301e4925e8SNate Lawson     int			ret, i;
123115e32d5dSMike Smith 
1232cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1233cb9b0d80SMike Smith 
12341e4925e8SNate Lawson     ret = FALSE;
12354d332989SMike Smith     if (hid == NULL)
123615e32d5dSMike Smith 	return (FALSE);
123715e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
123815e32d5dSMike Smith 	return (FALSE);
12391e4925e8SNate Lawson     buf.Pointer = NULL;
12401e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12416fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12426fca9360SNate Lawson     if (ACPI_FAILURE(error))
124315e32d5dSMike Smith 	return (FALSE);
12441e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1245be2b1797SNate Lawson 
1246c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1247c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
12481e4925e8SNate Lawson 	    ret = TRUE;
1249c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
12501e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
12511e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
12521e4925e8SNate Lawson 		ret = TRUE;
12531e4925e8SNate Lawson 		break;
12541e4925e8SNate Lawson 	    }
12551e4925e8SNate Lawson 	}
12561e4925e8SNate Lawson     }
1257be2b1797SNate Lawson 
12581e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12591e4925e8SNate Lawson     return (ret);
126015e32d5dSMike Smith }
126115e32d5dSMike Smith 
126215e32d5dSMike Smith /*
1263c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1264c5ba0be4SMike Smith  * or one if its parents.
1265c5ba0be4SMike Smith  */
1266c5ba0be4SMike Smith ACPI_STATUS
1267c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1268c5ba0be4SMike Smith {
1269c5ba0be4SMike Smith     ACPI_HANDLE		r;
1270c5ba0be4SMike Smith     ACPI_STATUS		status;
1271c5ba0be4SMike Smith 
1272cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1273cb9b0d80SMike Smith 
1274be2b1797SNate Lawson     /* Walk back up the tree to the root */
1275c5ba0be4SMike Smith     for (;;) {
1276be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1277be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1278c5ba0be4SMike Smith 	    *result = r;
1279c5ba0be4SMike Smith 	    return (AE_OK);
1280c5ba0be4SMike Smith 	}
1281c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1282c5ba0be4SMike Smith 	    return (AE_OK);
1283b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1284c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1285c5ba0be4SMike Smith 	parent = r;
1286c5ba0be4SMike Smith     }
1287c5ba0be4SMike Smith }
1288c5ba0be4SMike Smith 
1289c5ba0be4SMike Smith /*
1290c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1291c5ba0be4SMike Smith  */
1292c5ba0be4SMike Smith ACPI_BUFFER *
1293c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1294c5ba0be4SMike Smith {
1295c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1296c5ba0be4SMike Smith 
1297c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1298c5ba0be4SMike Smith 	return (NULL);
1299c5ba0be4SMike Smith     buf->Length = size;
1300c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1301c5ba0be4SMike Smith     return (buf);
1302c5ba0be4SMike Smith }
1303c5ba0be4SMike Smith 
1304c5ba0be4SMike Smith /*
1305c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1306c5ba0be4SMike Smith  */
1307c5ba0be4SMike Smith ACPI_STATUS
1308c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1309c5ba0be4SMike Smith {
1310be2b1797SNate Lawson     ACPI_STATUS	status;
1311c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1312c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1313c5ba0be4SMike Smith 
1314cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1315cb9b0d80SMike Smith 
1316c5ba0be4SMike Smith     if (handle == NULL)
1317c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
13180c7da7acSJohn Baldwin 
13190c7da7acSJohn Baldwin     /*
13200c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
13210c7da7acSJohn Baldwin      * a method that will return an Integer.
13220c7da7acSJohn Baldwin      */
1323c5ba0be4SMike Smith     buf.Pointer = &param;
1324c5ba0be4SMike Smith     buf.Length = sizeof(param);
1325be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1326be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1327be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1328c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1329be2b1797SNate Lawson 	else
1330be2b1797SNate Lawson 	    status = AE_TYPE;
1331c5ba0be4SMike Smith     }
13320c7da7acSJohn Baldwin 
13330c7da7acSJohn Baldwin     /*
13340c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
13350c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
13360c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
13370c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
13380c7da7acSJohn Baldwin      *
13390c7da7acSJohn Baldwin      * This is a hack.
13400c7da7acSJohn Baldwin      */
1341be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1342b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1343be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
13440c7da7acSJohn Baldwin 	} else {
1345be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1346be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1347be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
13480c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
13490c7da7acSJohn Baldwin 	}
1350f97739daSNate Lawson     }
1351be2b1797SNate Lawson     return (status);
1352c5ba0be4SMike Smith }
1353c5ba0be4SMike Smith 
1354c573e654SMitsuru IWASAKI ACPI_STATUS
1355c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1356c573e654SMitsuru IWASAKI {
1357c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1358c573e654SMitsuru IWASAKI     int		i;
1359c573e654SMitsuru IWASAKI 
1360c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1361c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1362c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1363c573e654SMitsuru IWASAKI 	return (AE_OK);
1364c573e654SMitsuru IWASAKI     }
1365c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1366c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1367c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1368c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1369be2b1797SNate Lawson 
1370c573e654SMitsuru IWASAKI     *number = 0;
1371c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1372c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1373c573e654SMitsuru IWASAKI     return (AE_OK);
1374c573e654SMitsuru IWASAKI }
1375c573e654SMitsuru IWASAKI 
1376c5ba0be4SMike Smith /*
1377c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1378c5ba0be4SMike Smith  * function for each element.
1379c5ba0be4SMike Smith  *
1380c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1381c5ba0be4SMike Smith  */
1382c5ba0be4SMike Smith ACPI_STATUS
1383be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1384be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1385c5ba0be4SMike Smith {
1386c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1387c5ba0be4SMike Smith     int		i;
1388c5ba0be4SMike Smith 
1389be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1390c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1391c5ba0be4SMike Smith 
1392be2b1797SNate Lawson     /* Iterate over components */
1393be2b1797SNate Lawson     i = 0;
1394be2b1797SNate Lawson     comp = pkg->Package.Elements;
1395be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1396c5ba0be4SMike Smith 	func(comp, arg);
1397c5ba0be4SMike Smith 
1398c5ba0be4SMike Smith     return (AE_OK);
1399c5ba0be4SMike Smith }
1400c5ba0be4SMike Smith 
14016f69255bSMike Smith /*
14026f69255bSMike Smith  * Find the (index)th resource object in a set.
14036f69255bSMike Smith  */
14046f69255bSMike Smith ACPI_STATUS
14056d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
14066f69255bSMike Smith {
14076d63101aSMike Smith     ACPI_RESOURCE	*rp;
14086f69255bSMike Smith     int			i;
14096f69255bSMike Smith 
14106d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
14116f69255bSMike Smith     i = index;
14126d63101aSMike Smith     while (i-- > 0) {
1413be2b1797SNate Lawson 	/* Range check */
14146d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
14156f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1416be2b1797SNate Lawson 
1417be2b1797SNate Lawson 	/* Check for terminator */
1418be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
14196d63101aSMike Smith 	    return (AE_NOT_FOUND);
14206d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
14216f69255bSMike Smith     }
14226f69255bSMike Smith     if (resp != NULL)
14236d63101aSMike Smith 	*resp = rp;
1424be2b1797SNate Lawson 
14256d63101aSMike Smith     return (AE_OK);
14266d63101aSMike Smith }
14276d63101aSMike Smith 
14286d63101aSMike Smith /*
14296d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
14306d63101aSMike Smith  *
14316d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
14326d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
14336d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
14346d63101aSMike Smith  * resources.
14356d63101aSMike Smith  */
14366d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
14376d63101aSMike Smith 
14386d63101aSMike Smith ACPI_STATUS
14396d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
14406d63101aSMike Smith {
14416d63101aSMike Smith     ACPI_RESOURCE	*rp;
14426d63101aSMike Smith     void		*newp;
14436d63101aSMike Smith 
1444be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
14456d63101aSMike Smith     if (buf->Pointer == NULL) {
14466d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
14476d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
14486d63101aSMike Smith 	    return (AE_NO_MEMORY);
14496d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
14506d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
14516d63101aSMike Smith 	rp->Length = 0;
14526d63101aSMike Smith     }
14536d63101aSMike Smith     if (res == NULL)
14546d63101aSMike Smith 	return (AE_OK);
14556d63101aSMike Smith 
14566d63101aSMike Smith     /*
14576d63101aSMike Smith      * Scan the current buffer looking for the terminator.
14586d63101aSMike Smith      * This will either find the terminator or hit the end
14596d63101aSMike Smith      * of the buffer and return an error.
14606d63101aSMike Smith      */
14616d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
14626d63101aSMike Smith     for (;;) {
1463be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
14646d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
14656d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1466be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
14676d63101aSMike Smith 	    break;
14686d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
14696d63101aSMike Smith     }
14706d63101aSMike Smith 
14716d63101aSMike Smith     /*
14726d63101aSMike Smith      * Check the size of the buffer and expand if required.
14736d63101aSMike Smith      *
14746d63101aSMike Smith      * Required size is:
14756d63101aSMike Smith      *	size of existing resources before terminator +
14766d63101aSMike Smith      *	size of new resource and header +
14776d63101aSMike Smith      * 	size of terminator.
14786d63101aSMike Smith      *
14796d63101aSMike Smith      * Note that this loop should really only run once, unless
14806d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
14816d63101aSMike Smith      */
14826d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
14836d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
14846d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
14856d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
14866d63101aSMike Smith 	    return (AE_NO_MEMORY);
14876d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1488a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1489a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
14906d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
14916d63101aSMike Smith 	buf->Pointer = newp;
14926d63101aSMike Smith 	buf->Length += buf->Length;
14936d63101aSMike Smith     }
14946d63101aSMike Smith 
1495be2b1797SNate Lawson     /* Insert the new resource. */
14966d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
14976d63101aSMike Smith 
1498be2b1797SNate Lawson     /* And add the terminator. */
14996d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
15006d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
15016d63101aSMike Smith     rp->Length = 0;
15026d63101aSMike Smith 
15036f69255bSMike Smith     return (AE_OK);
15046f69255bSMike Smith }
1505c5ba0be4SMike Smith 
1506da14ac9fSJohn Baldwin /*
1507da14ac9fSJohn Baldwin  * Set interrupt model.
1508da14ac9fSJohn Baldwin  */
1509da14ac9fSJohn Baldwin ACPI_STATUS
1510da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1511da14ac9fSJohn Baldwin {
1512da14ac9fSJohn Baldwin     ACPI_OBJECT_LIST ArgList;
1513da14ac9fSJohn Baldwin     ACPI_OBJECT Arg;
1514da14ac9fSJohn Baldwin 
1515da14ac9fSJohn Baldwin     Arg.Type = ACPI_TYPE_INTEGER;
1516da14ac9fSJohn Baldwin     Arg.Integer.Value = model;
1517da14ac9fSJohn Baldwin     ArgList.Count = 1;
1518da14ac9fSJohn Baldwin     ArgList.Pointer = &Arg;
1519da14ac9fSJohn Baldwin     return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1520da14ac9fSJohn Baldwin }
1521da14ac9fSJohn Baldwin 
1522ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1523ece50487SMitsuru IWASAKI 
1524ece50487SMitsuru IWASAKI static void
1525ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1526ece50487SMitsuru IWASAKI {
1527ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1528ece50487SMitsuru IWASAKI }
1529c30382dfSMitsuru IWASAKI 
153015e32d5dSMike Smith /*
153115e32d5dSMike Smith  * Set the system sleep state
153215e32d5dSMike Smith  *
1533be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
153415e32d5dSMike Smith  */
153515e32d5dSMike Smith ACPI_STATUS
153615e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
153715e32d5dSMike Smith {
153815e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
153956d8cb57SMitsuru IWASAKI     UINT8	TypeA;
154056d8cb57SMitsuru IWASAKI     UINT8	TypeB;
154115e32d5dSMike Smith 
1542b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1543cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
15440ae55423SMike Smith 
154598175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
15466397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
154798175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
15486397624dSMitsuru IWASAKI 
154998175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1550ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1551ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1552ece50487SMitsuru IWASAKI 
155315e32d5dSMike Smith     switch (state) {
155415e32d5dSMike Smith     case ACPI_STATE_S1:
15556161544cSTakanori Watanabe     case ACPI_STATE_S2:
15566161544cSTakanori Watanabe     case ACPI_STATE_S3:
15576161544cSTakanori Watanabe     case ACPI_STATE_S4:
1558be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
155998175614SNate Lawson 	if (status == AE_NOT_FOUND) {
156098175614SNate Lawson 	    device_printf(sc->acpi_dev,
156198175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
156298175614SNate Lawson 	    break;
156398175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1564be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1565be2b1797SNate Lawson 			  AcpiFormatException(status));
156656d8cb57SMitsuru IWASAKI 	    break;
156756d8cb57SMitsuru IWASAKI 	}
156856d8cb57SMitsuru IWASAKI 
1569a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1570a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1571a1fccb47SMitsuru IWASAKI 
1572be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
157315e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
157415e32d5dSMike Smith 	    /*
157515e32d5dSMike Smith 	     * Re-wake the system.
157615e32d5dSMike Smith 	     *
157715e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
157815e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
157915e32d5dSMike Smith 	     *     current bus interface does not provide for this.
158015e32d5dSMike Smith 	     */
158115e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
15820ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
158315e32d5dSMike Smith 	}
1584b9c780d6SMitsuru IWASAKI 
1585be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1586be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1587b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1588b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1589b9c780d6SMitsuru IWASAKI 	    break;
1590b9c780d6SMitsuru IWASAKI 	}
1591b9c780d6SMitsuru IWASAKI 
1592be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1593ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1594ff01efb5SMitsuru IWASAKI 
15956161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
15966161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
15976161544cSTakanori Watanabe 
1598be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
159959ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
160059ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
160159ddeb18SNate Lawson 
16026161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1603a1fccb47SMitsuru IWASAKI 	    }
16046161544cSTakanori Watanabe 
16056161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1606be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1607964679ceSMitsuru IWASAKI 		AcpiEnable();
16086161544cSTakanori Watanabe 	} else {
1609be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1610be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1611be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1612be2b1797SNate Lawson 			      AcpiFormatException(status));
1613c30382dfSMitsuru IWASAKI 		break;
161415e32d5dSMike Smith 	    }
16156161544cSTakanori Watanabe 	}
1616ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
161715e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
161815e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
161913d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
162015e32d5dSMike Smith 	break;
162115e32d5dSMike Smith     case ACPI_STATE_S5:
162215e32d5dSMike Smith 	/*
162315e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
162415e32d5dSMike Smith 	 * shutdown handlers.
162515e32d5dSMike Smith 	 */
162615e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
162715e32d5dSMike Smith 	break;
162898175614SNate Lawson     case ACPI_STATE_S0:
162915e32d5dSMike Smith     default:
163015e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
163115e32d5dSMike Smith 	break;
163215e32d5dSMike Smith     }
1633ece50487SMitsuru IWASAKI 
163498175614SNate Lawson     /* Disable a second sleep request for a short period */
1635ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1636ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1637ece50487SMitsuru IWASAKI 
16380ae55423SMike Smith     return_ACPI_STATUS (status);
163915e32d5dSMike Smith }
164015e32d5dSMike Smith 
164115e32d5dSMike Smith /*
164215e32d5dSMike Smith  * Enable/Disable ACPI
164315e32d5dSMike Smith  */
164415e32d5dSMike Smith ACPI_STATUS
164515e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
164615e32d5dSMike Smith {
164715e32d5dSMike Smith     ACPI_STATUS	status;
164815e32d5dSMike Smith     u_int32_t	flags;
164915e32d5dSMike Smith 
1650b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1651cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16520ae55423SMike Smith 
165315e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
165415e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1655be2b1797SNate Lawson     if (!sc->acpi_enabled)
165615e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1657be2b1797SNate Lawson     else
165815e32d5dSMike Smith 	status = AE_OK;
1659be2b1797SNate Lawson 
166015e32d5dSMike Smith     if (status == AE_OK)
166115e32d5dSMike Smith 	sc->acpi_enabled = 1;
1662be2b1797SNate Lawson 
16630ae55423SMike Smith     return_ACPI_STATUS (status);
166415e32d5dSMike Smith }
166515e32d5dSMike Smith 
166615e32d5dSMike Smith ACPI_STATUS
166715e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
166815e32d5dSMike Smith {
166915e32d5dSMike Smith     ACPI_STATUS	status;
167015e32d5dSMike Smith 
1671b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1672cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16730ae55423SMike Smith 
1674be2b1797SNate Lawson     if (sc->acpi_enabled)
167515e32d5dSMike Smith 	status = AcpiDisable();
1676be2b1797SNate Lawson     else
167715e32d5dSMike Smith 	status = AE_OK;
1678be2b1797SNate Lawson 
167915e32d5dSMike Smith     if (status == AE_OK)
168015e32d5dSMike Smith 	sc->acpi_enabled = 0;
1681be2b1797SNate Lawson 
16820ae55423SMike Smith     return_ACPI_STATUS (status);
168315e32d5dSMike Smith }
168415e32d5dSMike Smith 
168515e32d5dSMike Smith /*
168615e32d5dSMike Smith  * ACPI Event Handlers
168715e32d5dSMike Smith  */
168815e32d5dSMike Smith 
168915e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
169015e32d5dSMike Smith 
169115e32d5dSMike Smith static void
169215e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
169315e32d5dSMike Smith {
1694fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1695b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
169615e32d5dSMike Smith 
1697cb9b0d80SMike Smith     ACPI_LOCK;
1698a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
169915e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1700cb9b0d80SMike Smith     ACPI_UNLOCK;
17010ae55423SMike Smith     return_VOID;
170215e32d5dSMike Smith }
170315e32d5dSMike Smith 
170415e32d5dSMike Smith static void
170515e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
170615e32d5dSMike Smith {
1707fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1708b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
17090ae55423SMike Smith 
171015e32d5dSMike Smith     /* Well, what to do? :-) */
17110ae55423SMike Smith 
1712cb9b0d80SMike Smith     ACPI_LOCK;
1713cb9b0d80SMike Smith     ACPI_UNLOCK;
1714cb9b0d80SMike Smith 
17150ae55423SMike Smith     return_VOID;
171615e32d5dSMike Smith }
171715e32d5dSMike Smith 
171815e32d5dSMike Smith /*
171915e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
172015e32d5dSMike Smith  */
172115e32d5dSMike Smith UINT32
172215e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
172315e32d5dSMike Smith {
172415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
172515e32d5dSMike Smith 
1726b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17270ae55423SMike Smith 
172815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
17290ae55423SMike Smith 
1730b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
173115e32d5dSMike Smith }
173215e32d5dSMike Smith 
173315e32d5dSMike Smith UINT32
173415e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
173515e32d5dSMike Smith {
173615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
173715e32d5dSMike Smith 
1738b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17390ae55423SMike Smith 
174015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
17410ae55423SMike Smith 
1742b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
174315e32d5dSMike Smith }
174415e32d5dSMike Smith 
174515e32d5dSMike Smith UINT32
174615e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
174715e32d5dSMike Smith {
174815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
174915e32d5dSMike Smith 
1750b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17510ae55423SMike Smith 
175215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
17530ae55423SMike Smith 
1754b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
175515e32d5dSMike Smith }
175615e32d5dSMike Smith 
175715e32d5dSMike Smith UINT32
175815e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
175915e32d5dSMike Smith {
176015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
176115e32d5dSMike Smith 
1762b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17630ae55423SMike Smith 
176415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
17650ae55423SMike Smith 
1766b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
176715e32d5dSMike Smith }
176815e32d5dSMike Smith 
176915e32d5dSMike Smith /*
177015e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
177115e32d5dSMike Smith  */
177215e32d5dSMike Smith struct acpi_staticbuf {
177315e32d5dSMike Smith     ACPI_BUFFER	buffer;
177415e32d5dSMike Smith     char	data[512];
177515e32d5dSMike Smith };
177615e32d5dSMike Smith 
177715e32d5dSMike Smith char *
177815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
177915e32d5dSMike Smith {
178015e32d5dSMike Smith     static struct acpi_staticbuf	buf;
178115e32d5dSMike Smith 
1782cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1783cb9b0d80SMike Smith 
178415e32d5dSMike Smith     buf.buffer.Length = 512;
178515e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
178615e32d5dSMike Smith 
1787b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
178815e32d5dSMike Smith 	return (buf.buffer.Pointer);
1789be2b1797SNate Lawson 
179015e32d5dSMike Smith     return ("(unknown path)");
179115e32d5dSMike Smith }
179215e32d5dSMike Smith 
179315e32d5dSMike Smith /*
179415e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
179515e32d5dSMike Smith  * parts of the namespace.
179615e32d5dSMike Smith  */
179715e32d5dSMike Smith int
179815e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
179915e32d5dSMike Smith {
18003c600cfbSJohn Baldwin     char	*cp, *env, *np;
180115e32d5dSMike Smith     int		len;
180215e32d5dSMike Smith 
180315e32d5dSMike Smith     np = acpi_name(handle);
180415e32d5dSMike Smith     if (*np == '\\')
180515e32d5dSMike Smith 	np++;
18063c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
180715e32d5dSMike Smith 	return (0);
180815e32d5dSMike Smith 
1809be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
18103c600cfbSJohn Baldwin     cp = env;
181115e32d5dSMike Smith     for (;;) {
181215e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
181315e32d5dSMike Smith 	    cp++;
181415e32d5dSMike Smith 	if (*cp == 0)
181515e32d5dSMike Smith 	    break;
181615e32d5dSMike Smith 	len = 0;
181715e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
181815e32d5dSMike Smith 	    len++;
1819d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
18203c600cfbSJohn Baldwin 	    freeenv(env);
18210ae55423SMike Smith 	    return(1);
1822d786139cSMaxime Henrion 	}
18230ae55423SMike Smith 	cp += len;
18240ae55423SMike Smith     }
18253c600cfbSJohn Baldwin     freeenv(env);
1826be2b1797SNate Lawson 
18270ae55423SMike Smith     return (0);
18280ae55423SMike Smith }
18290ae55423SMike Smith 
18300ae55423SMike Smith /*
18310ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
18320ae55423SMike Smith  */
18330ae55423SMike Smith int
18340ae55423SMike Smith acpi_disabled(char *subsys)
18350ae55423SMike Smith {
183678689b15SMaxime Henrion     char	*cp, *env;
18370ae55423SMike Smith     int		len;
18380ae55423SMike Smith 
183978689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
18400ae55423SMike Smith 	return (0);
184178689b15SMaxime Henrion     if (!strcmp(env, "all")) {
184278689b15SMaxime Henrion 	freeenv(env);
18430ae55423SMike Smith 	return (1);
1844d786139cSMaxime Henrion     }
18450ae55423SMike Smith 
18460ae55423SMike Smith     /* scan the disable list checking for a match */
184778689b15SMaxime Henrion     cp = env;
18480ae55423SMike Smith     for (;;) {
18490ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
18500ae55423SMike Smith 	    cp++;
18510ae55423SMike Smith 	if (*cp == 0)
18520ae55423SMike Smith 	    break;
18530ae55423SMike Smith 	len = 0;
18540ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
18550ae55423SMike Smith 	    len++;
1856d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
185778689b15SMaxime Henrion 	    freeenv(env);
185815e32d5dSMike Smith 	    return (1);
1859d786139cSMaxime Henrion 	}
186015e32d5dSMike Smith 	cp += len;
186115e32d5dSMike Smith     }
186278689b15SMaxime Henrion     freeenv(env);
1863be2b1797SNate Lawson 
186415e32d5dSMike Smith     return (0);
186515e32d5dSMike Smith }
186615e32d5dSMike Smith 
186715e32d5dSMike Smith /*
1868a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1869a1fccb47SMitsuru IWASAKI  */
1870a1fccb47SMitsuru IWASAKI void
1871a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1872a1fccb47SMitsuru IWASAKI {
1873a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1874a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1875a1fccb47SMitsuru IWASAKI 
1876a1fccb47SMitsuru IWASAKI     /*
1877a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1878a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1879a1fccb47SMitsuru IWASAKI      */
1880a1fccb47SMitsuru IWASAKI 
1881a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1882a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1883a1fccb47SMitsuru IWASAKI 
1884a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1885a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1886a1fccb47SMitsuru IWASAKI 
1887a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1888a1fccb47SMitsuru IWASAKI }
1889a1fccb47SMitsuru IWASAKI 
1890a1fccb47SMitsuru IWASAKI void
1891a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1892a1fccb47SMitsuru IWASAKI {
1893a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1894a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1895a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1896a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1897a1fccb47SMitsuru IWASAKI 
1898a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1899a1fccb47SMitsuru IWASAKI 
1900be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
1901be2b1797SNate Lawson     if (sc == NULL)
1902a1fccb47SMitsuru IWASAKI 	return;
1903a1fccb47SMitsuru IWASAKI 
1904a1fccb47SMitsuru IWASAKI     /*
1905a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1906a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1907a1fccb47SMitsuru IWASAKI      */
1908a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1909a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1910be2b1797SNate Lawson     if (ACPI_FAILURE(status))
1911a1fccb47SMitsuru IWASAKI 	return;
1912a1fccb47SMitsuru IWASAKI 
1913a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1914be2b1797SNate Lawson     if (res == NULL)
1915a1fccb47SMitsuru IWASAKI 	return;
1916a1fccb47SMitsuru IWASAKI 
1917a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1918a1fccb47SMitsuru IWASAKI 	goto out;
1919a1fccb47SMitsuru IWASAKI     }
1920a1fccb47SMitsuru IWASAKI 
1921a1fccb47SMitsuru IWASAKI     /*
1922a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1923a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1924a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1925a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1926a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1927a1fccb47SMitsuru IWASAKI      */
1928be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1929a1fccb47SMitsuru IWASAKI 	goto out;
1930a1fccb47SMitsuru IWASAKI 
1931be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
1932a1fccb47SMitsuru IWASAKI 	goto out;
1933a1fccb47SMitsuru IWASAKI 
1934a1fccb47SMitsuru IWASAKI     /*
1935a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1936a1fccb47SMitsuru IWASAKI      */
1937a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1938a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1939a1fccb47SMitsuru IWASAKI 	/*
1940a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1941a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1942a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1943a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1944a1fccb47SMitsuru IWASAKI 	 */
1945a1fccb47SMitsuru IWASAKI 
19466fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
19476fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1948a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1949a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
1950a1fccb47SMitsuru IWASAKI 	break;
1951a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1952a1fccb47SMitsuru IWASAKI 	/*
1953be2b1797SNate Lawson 	 * XXX TBD
1954be2b1797SNate Lawson 	 *
1955a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1956a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1957a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1958a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1959a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1960a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1961a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1962a1fccb47SMitsuru IWASAKI 	 * the wake event.
1963a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1964a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1965a1fccb47SMitsuru IWASAKI 	 */
1966a1fccb47SMitsuru IWASAKI 	break;
1967a1fccb47SMitsuru IWASAKI     default:
1968a1fccb47SMitsuru IWASAKI 	break;
1969a1fccb47SMitsuru IWASAKI     }
1970a1fccb47SMitsuru IWASAKI 
1971a1fccb47SMitsuru IWASAKI out:
1972a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1973a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1974a1fccb47SMitsuru IWASAKI }
1975a1fccb47SMitsuru IWASAKI 
1976a1fccb47SMitsuru IWASAKI /*
197715e32d5dSMike Smith  * Control interface.
197815e32d5dSMike Smith  *
19790ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
1980be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
1981be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
198215e32d5dSMike Smith  */
19830ae55423SMike Smith struct acpi_ioctl_hook
19840ae55423SMike Smith {
19850ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
19860ae55423SMike Smith     u_long			 cmd;
1987be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
19880ae55423SMike Smith     void			 *arg;
19890ae55423SMike Smith };
19900ae55423SMike Smith 
19910ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
19920ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
19930ae55423SMike Smith 
19940ae55423SMike Smith /*
19950ae55423SMike Smith  * Register an ioctl handler.
19960ae55423SMike Smith  */
19970ae55423SMike Smith int
1998be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
19990ae55423SMike Smith {
20000ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20010ae55423SMike Smith 
20020ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
20030ae55423SMike Smith 	return (ENOMEM);
20040ae55423SMike Smith     hp->cmd = cmd;
20050ae55423SMike Smith     hp->fn = fn;
20060ae55423SMike Smith     hp->arg = arg;
20070ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
20080ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
20090ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
20100ae55423SMike Smith     }
20110ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
20120ae55423SMike Smith     return (0);
20130ae55423SMike Smith }
20140ae55423SMike Smith 
20150ae55423SMike Smith /*
20160ae55423SMike Smith  * Deregister an ioctl handler.
20170ae55423SMike Smith  */
20180ae55423SMike Smith void
2019be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
20200ae55423SMike Smith {
20210ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20220ae55423SMike Smith 
20230ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
20240ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
20250ae55423SMike Smith 	    break;
20260ae55423SMike Smith 
20270ae55423SMike Smith     if (hp != NULL) {
20280ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
20290ae55423SMike Smith 	free(hp, M_ACPIDEV);
20300ae55423SMike Smith     }
20310ae55423SMike Smith }
20320ae55423SMike Smith 
203315e32d5dSMike Smith static int
20346b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
203515e32d5dSMike Smith {
203615e32d5dSMike Smith     return (0);
203715e32d5dSMike Smith }
203815e32d5dSMike Smith 
203915e32d5dSMike Smith static int
20406b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
204115e32d5dSMike Smith {
204215e32d5dSMike Smith     return (0);
204315e32d5dSMike Smith }
204415e32d5dSMike Smith 
204515e32d5dSMike Smith static int
20466b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
204715e32d5dSMike Smith {
204815e32d5dSMike Smith     struct acpi_softc		*sc;
20490ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20500ae55423SMike Smith     int				error, xerror, state;
2051fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
205215e32d5dSMike Smith 
2053cb9b0d80SMike Smith     ACPI_LOCK;
2054cb9b0d80SMike Smith 
205515e32d5dSMike Smith     error = state = 0;
205615e32d5dSMike Smith     sc = dev->si_drv1;
205715e32d5dSMike Smith 
20580ae55423SMike Smith     /*
20590ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
20600ae55423SMike Smith      */
20610ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
20620ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
20630ae55423SMike Smith 	    if (hp->cmd == cmd) {
20640ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
20650ae55423SMike Smith 		if (xerror != 0)
20660ae55423SMike Smith 		    error = xerror;
2067917d44c8SMitsuru IWASAKI 		goto out;
20680ae55423SMike Smith 	    }
20690ae55423SMike Smith 	}
20700ae55423SMike Smith     }
20710ae55423SMike Smith 
20720ae55423SMike Smith     /*
2073a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2074a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2075a89fcf28STakanori Watanabe      * Not changing system behavior.
2076a89fcf28STakanori Watanabe      */
2077be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2078be2b1797SNate Lawson 	return (EPERM);
2079a89fcf28STakanori Watanabe 
2080be2b1797SNate Lawson     /* Core system ioctls. */
208115e32d5dSMike Smith     switch (cmd) {
208215e32d5dSMike Smith     case ACPIIO_ENABLE:
20830ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
208415e32d5dSMike Smith 	    error = ENXIO;
208515e32d5dSMike Smith 	break;
208615e32d5dSMike Smith     case ACPIIO_DISABLE:
20870ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
208815e32d5dSMike Smith 	    error = ENXIO;
208915e32d5dSMike Smith 	break;
209015e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
209115e32d5dSMike Smith 	if (!sc->acpi_enabled) {
209215e32d5dSMike Smith 	    error = ENXIO;
209315e32d5dSMike Smith 	    break;
209415e32d5dSMike Smith 	}
209515e32d5dSMike Smith 	state = *(int *)addr;
20962d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
20972d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
209815e32d5dSMike Smith 		error = EINVAL;
20992d610c46SNate Lawson 	} else {
21002d610c46SNate Lawson 	    error = EINVAL;
21012d610c46SNate Lawson 	}
210215e32d5dSMike Smith 	break;
210315e32d5dSMike Smith     default:
21040ae55423SMike Smith 	if (error == 0)
210515e32d5dSMike Smith 	    error = EINVAL;
210615e32d5dSMike Smith 	break;
210715e32d5dSMike Smith     }
2108917d44c8SMitsuru IWASAKI 
2109917d44c8SMitsuru IWASAKI out:
2110cb9b0d80SMike Smith     ACPI_UNLOCK;
211115e32d5dSMike Smith     return (error);
211215e32d5dSMike Smith }
211315e32d5dSMike Smith 
21141d073b1dSJohn Baldwin static int
2115d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2116d75de536SMitsuru IWASAKI {
2117d75de536SMitsuru IWASAKI     char sleep_state[4];
2118d75de536SMitsuru IWASAKI     char buf[16];
2119d75de536SMitsuru IWASAKI     int error;
2120d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2121d75de536SMitsuru IWASAKI 
2122d75de536SMitsuru IWASAKI     buf[0] = '\0';
2123d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2124d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2125d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2126d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2127d75de536SMitsuru IWASAKI 	}
2128d75de536SMitsuru IWASAKI     }
2129d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2130d75de536SMitsuru IWASAKI     return (error);
2131d75de536SMitsuru IWASAKI }
2132d75de536SMitsuru IWASAKI 
2133d75de536SMitsuru IWASAKI static int
21341d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
21351d073b1dSJohn Baldwin {
21361d073b1dSJohn Baldwin     char sleep_state[10];
21371d073b1dSJohn Baldwin     int error;
21381d073b1dSJohn Baldwin     u_int new_state, old_state;
21391d073b1dSJohn Baldwin 
21401d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2141b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
21421d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2143cb9b0d80SMike Smith     } else {
2144b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
21451d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
21461d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2147cb9b0d80SMike Smith     }
21481d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
21491d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2150be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2151be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
21521d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
21531d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
21541d073b1dSJohn Baldwin 		break;
2155cb9b0d80SMike Smith 	}
2156931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2157be2b1797SNate Lawson 	    if (new_state != old_state)
21581d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2159cb9b0d80SMike Smith 	} else {
21601d073b1dSJohn Baldwin 	    error = EINVAL;
21611d073b1dSJohn Baldwin 	}
2162cb9b0d80SMike Smith     }
2163be2b1797SNate Lawson 
21641d073b1dSJohn Baldwin     return (error);
21651d073b1dSJohn Baldwin }
21661d073b1dSJohn Baldwin 
21679b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
21689b937d48SNate Lawson void
21699b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
21709b937d48SNate Lawson {
21719b937d48SNate Lawson     char		notify_buf[16];
21729b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
21739b937d48SNate Lawson     ACPI_STATUS		status;
21749b937d48SNate Lawson 
21759b937d48SNate Lawson     if (subsystem == NULL)
21769b937d48SNate Lawson 	return;
21779b937d48SNate Lawson 
21789b937d48SNate Lawson     handle_buf.Pointer = NULL;
21799b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
21809b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
21819b937d48SNate Lawson     if (ACPI_FAILURE(status))
21829b937d48SNate Lawson 	return;
21839b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
21849b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
21859b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
21869b937d48SNate Lawson }
21879b937d48SNate Lawson 
218815e32d5dSMike Smith #ifdef ACPI_DEBUG
21890ae55423SMike Smith /*
21900ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
21910ae55423SMike Smith  *
21920ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
21930ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
21940ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
21950ae55423SMike Smith  * prefixing the bit name with !.
21960ae55423SMike Smith  */
219715e32d5dSMike Smith struct debugtag
219815e32d5dSMike Smith {
219915e32d5dSMike Smith     char	*name;
220015e32d5dSMike Smith     UINT32	value;
220115e32d5dSMike Smith };
220215e32d5dSMike Smith 
220315e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2204c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2205c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2206c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2207c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2208c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2209c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2210c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2211c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2212c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2213d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
22144c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2215d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2216297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
22174c1cdee6SMike Smith 
2218cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
22194c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2220cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2221cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2222c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2223c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2224c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
22254c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2226cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
22274c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
2228b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
222915e32d5dSMike Smith     {NULL, 0}
223015e32d5dSMike Smith };
223115e32d5dSMike Smith 
223215e32d5dSMike Smith static struct debugtag dbg_level[] = {
22334c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
22349501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
22359501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
22364c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
22379501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
22384c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2239d62ab2f4SMitsuru IWASAKI 
2240d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2241297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
22424c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
22434c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2244d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
22454c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
22464c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
22474c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
22484c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
22494c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
22504c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
22514c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
22524c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
22534c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
22544c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2255d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2256d62ab2f4SMitsuru IWASAKI 
2257d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2258d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2259d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2260d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2261d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
22624c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2263d62ab2f4SMitsuru IWASAKI 
2264d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2265d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2266d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2267d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2268d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2269d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2270d62ab2f4SMitsuru IWASAKI 
2271d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
227298479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
227398479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
227498479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
227598479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
227698479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
227715e32d5dSMike Smith     {NULL, 0}
227815e32d5dSMike Smith };
227915e32d5dSMike Smith 
228015e32d5dSMike Smith static void
228115e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
228215e32d5dSMike Smith {
228315e32d5dSMike Smith     char	*ep;
228415e32d5dSMike Smith     int		i, l;
22850ae55423SMike Smith     int		set;
228615e32d5dSMike Smith 
228715e32d5dSMike Smith     while (*cp) {
228815e32d5dSMike Smith 	if (isspace(*cp)) {
228915e32d5dSMike Smith 	    cp++;
229015e32d5dSMike Smith 	    continue;
229115e32d5dSMike Smith 	}
229215e32d5dSMike Smith 	ep = cp;
229315e32d5dSMike Smith 	while (*ep && !isspace(*ep))
229415e32d5dSMike Smith 	    ep++;
22950ae55423SMike Smith 	if (*cp == '!') {
22960ae55423SMike Smith 	    set = 0;
22970ae55423SMike Smith 	    cp++;
22980ae55423SMike Smith 	    if (cp == ep)
22990ae55423SMike Smith 		continue;
23000ae55423SMike Smith 	} else {
23010ae55423SMike Smith 	    set = 1;
23020ae55423SMike Smith 	}
230315e32d5dSMike Smith 	l = ep - cp;
230415e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
230515e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2306be2b1797SNate Lawson 		if (set)
230715e32d5dSMike Smith 		    *flag |= tag[i].value;
2308be2b1797SNate Lawson 		else
23090ae55423SMike Smith 		    *flag &= ~tag[i].value;
231015e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
231115e32d5dSMike Smith 	    }
231215e32d5dSMike Smith 	}
231315e32d5dSMike Smith 	cp = ep;
231415e32d5dSMike Smith     }
231515e32d5dSMike Smith }
231615e32d5dSMike Smith 
231715e32d5dSMike Smith static void
23182a4ac806SMike Smith acpi_set_debugging(void *junk)
231915e32d5dSMike Smith {
232094aae282SMike Barcroft     char	*cp;
232115e32d5dSMike Smith 
23221d7b121cSNate Lawson     if (cold) {
23230ae55423SMike Smith 	AcpiDbgLayer = 0;
23240ae55423SMike Smith 	AcpiDbgLevel = 0;
23251d7b121cSNate Lawson     }
23261d7b121cSNate Lawson 
232794aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
232815e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
232994aae282SMike Barcroft 	freeenv(cp);
233094aae282SMike Barcroft     }
233194aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
233215e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
233394aae282SMike Barcroft 	freeenv(cp);
233494aae282SMike Barcroft     }
23350ae55423SMike Smith 
23361d7b121cSNate Lawson     if (cold) {
23371d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
23381d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
23391d7b121cSNate Lawson     }
234015e32d5dSMike Smith }
2341be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2342be2b1797SNate Lawson 	NULL);
23431d7b121cSNate Lawson 
23441d7b121cSNate Lawson static int
23451d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
23461d7b121cSNate Lawson {
234754f6bca0SNate Lawson     int		 error, *dbg;
23481d7b121cSNate Lawson     struct	 debugtag *tag;
234954f6bca0SNate Lawson     struct	 sbuf sb;
23501d7b121cSNate Lawson 
235154f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
235254f6bca0SNate Lawson 	return (ENOMEM);
23531d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
23541d7b121cSNate Lawson 	tag = &dbg_layer[0];
23551d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
23561d7b121cSNate Lawson     } else {
23571d7b121cSNate Lawson 	tag = &dbg_level[0];
23581d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
23591d7b121cSNate Lawson     }
23601d7b121cSNate Lawson 
23611d7b121cSNate Lawson     /* Get old values if this is a get request. */
23621d7b121cSNate Lawson     if (*dbg == 0) {
236354f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
23641d7b121cSNate Lawson     } else if (req->newptr == NULL) {
23651d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
236654f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
236754f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
23681d7b121cSNate Lawson 	}
23691d7b121cSNate Lawson     }
237054f6bca0SNate Lawson     sbuf_trim(&sb);
237154f6bca0SNate Lawson     sbuf_finish(&sb);
23721d7b121cSNate Lawson 
237354f6bca0SNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
237454f6bca0SNate Lawson     sbuf_delete(&sb);
23751d7b121cSNate Lawson 
23761d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
23771d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
23781d7b121cSNate Lawson 	*dbg = 0;
23791d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
23801d7b121cSNate Lawson 	acpi_set_debugging(NULL);
23811d7b121cSNate Lawson     }
23821d7b121cSNate Lawson 
23831d7b121cSNate Lawson     return (error);
23841d7b121cSNate Lawson }
23851d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
23861d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
23871d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
23881d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
238915e32d5dSMike Smith #endif
2390f9390180SMitsuru IWASAKI 
2391f9390180SMitsuru IWASAKI static int
2392f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2393f9390180SMitsuru IWASAKI {
2394f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2395f9390180SMitsuru IWASAKI 	int	error;
2396f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2397f9390180SMitsuru IWASAKI 	va_list	ap;
2398f9390180SMitsuru IWASAKI 
2399f9390180SMitsuru IWASAKI 	error = 0;
2400f9390180SMitsuru IWASAKI 	switch (cmd) {
2401f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2402f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2403f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2404f9390180SMitsuru IWASAKI 			error = EINVAL;
2405f9390180SMitsuru IWASAKI 			goto out;
2406f9390180SMitsuru IWASAKI 		}
2407f9390180SMitsuru IWASAKI 
2408f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2409f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2410f9390180SMitsuru IWASAKI 		va_end(ap);
2411f9390180SMitsuru IWASAKI 
2412f9390180SMitsuru IWASAKI 		switch (state) {
2413f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2414f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2415f9390180SMitsuru IWASAKI 			break;
2416f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2417f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2418f9390180SMitsuru IWASAKI 			break;
2419f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2420f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2421f9390180SMitsuru IWASAKI 			break;
2422f9390180SMitsuru IWASAKI 		default:
2423f9390180SMitsuru IWASAKI 			error = EINVAL;
2424f9390180SMitsuru IWASAKI 			goto out;
2425f9390180SMitsuru IWASAKI 		}
2426f9390180SMitsuru IWASAKI 
2427f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2428f9390180SMitsuru IWASAKI 		break;
2429f9390180SMitsuru IWASAKI 	default:
2430f9390180SMitsuru IWASAKI 		error = EINVAL;
2431f9390180SMitsuru IWASAKI 		goto out;
2432f9390180SMitsuru IWASAKI 	}
2433f9390180SMitsuru IWASAKI 
2434f9390180SMitsuru IWASAKI out:
2435f9390180SMitsuru IWASAKI 	return (error);
2436f9390180SMitsuru IWASAKI }
2437f9390180SMitsuru IWASAKI 
2438f9390180SMitsuru IWASAKI static void
2439f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2440f9390180SMitsuru IWASAKI {
2441be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
24426c407052SMitsuru IWASAKI 	return;
2443f9390180SMitsuru IWASAKI 
2444f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2445f9390180SMitsuru IWASAKI }
2446f9390180SMitsuru IWASAKI 
2447f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2448