xref: /freebsd/sys/dev/acpica/acpi.c (revision d75de53699afbb898d6dff2e2994eabd1988e357)
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>
4615e32d5dSMike Smith 
4715e32d5dSMike Smith #include <machine/clock.h>
4815e32d5dSMike Smith #include <machine/resource.h>
4915e32d5dSMike Smith 
50bc0f2195SMike Smith #include <isa/isavar.h>
51bc0f2195SMike Smith 
5215e32d5dSMike Smith #include "acpi.h"
5315e32d5dSMike Smith 
541611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h>
551611ea87SMitsuru IWASAKI 
5615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5715e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5815e32d5dSMike Smith 
5915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6015e32d5dSMike Smith 
6115e32d5dSMike Smith /*
620ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
630ae55423SMike Smith  */
64cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
65b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
660ae55423SMike Smith 
670ae55423SMike Smith /*
6815e32d5dSMike Smith  * Character device
6915e32d5dSMike Smith  */
7015e32d5dSMike Smith 
7115e32d5dSMike Smith static d_open_t		acpiopen;
7215e32d5dSMike Smith static d_close_t	acpiclose;
7315e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7415e32d5dSMike Smith 
7515e32d5dSMike Smith #define CDEV_MAJOR 152
7615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
777ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
787ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
797ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
807ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
817ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
8215e32d5dSMike Smith };
8315e32d5dSMike Smith 
841d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
85b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
861d073b1dSJohn Baldwin 
872a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
882a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
892a4ac806SMike Smith 
90fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
91cb9b0d80SMike Smith struct mtx	acpi_mutex;
92fc0ea94aSJohn Baldwin #endif
93cb9b0d80SMike Smith 
946d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9515e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9615e32d5dSMike Smith static int	acpi_probe(device_t dev);
9715e32d5dSMike Smith static int	acpi_attach(device_t dev);
9815e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
9915e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10015e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
10115e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
10215e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
10315e32d5dSMike Smith 				  u_long count);
10415e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
10515e32d5dSMike Smith 				  u_long *countp);
10615e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
10715e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
10815e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
10932d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
110b2566207STakanori Watanabe static u_int32_t acpi_isa_get_compatid(device_t dev);
111bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
11215e32d5dSMike Smith 
11315e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
11415e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
11515e32d5dSMike Smith 
11615e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11715e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11815e32d5dSMike Smith 
11913d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12013d4f7baSMitsuru IWASAKI 
12115e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
12215e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
123d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1241d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
12515e32d5dSMike Smith 
126f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
127f9390180SMitsuru IWASAKI 
12815e32d5dSMike Smith static device_method_t acpi_methods[] = {
12915e32d5dSMike Smith     /* Device interface */
13015e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13115e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13215e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
13315e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13415e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
13515e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
13615e32d5dSMike Smith 
13715e32d5dSMike Smith     /* Bus interface */
13815e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13915e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14015e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14115e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14215e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14315e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14415e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
14515e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
14615e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14715e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14815e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14915e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15015e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15115e32d5dSMike Smith 
152bc0f2195SMike Smith     /* ISA emulation */
153bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
154bc0f2195SMike Smith 
15515e32d5dSMike Smith     {0, 0}
15615e32d5dSMike Smith };
15715e32d5dSMike Smith 
15815e32d5dSMike Smith static driver_t acpi_driver = {
15915e32d5dSMike Smith     "acpi",
16015e32d5dSMike Smith     acpi_methods,
16115e32d5dSMike Smith     sizeof(struct acpi_softc),
16215e32d5dSMike Smith };
16315e32d5dSMike Smith 
1643273b005SMike Smith static devclass_t acpi_devclass;
1656d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
166dde24897SMike Smith MODULE_VERSION(acpi, 100);
16715e32d5dSMike Smith 
16815e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
16915e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
170dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
171dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17215e32d5dSMike Smith 
17315e32d5dSMike Smith /*
1746d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1756d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1766d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1776d63101aSMike Smith  */
1786d63101aSMike Smith static int
1796d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1806d63101aSMike Smith {
1816d63101aSMike Smith     switch(event) {
1826d63101aSMike Smith     case MOD_LOAD:
18387b45ed5SMitsuru IWASAKI 	if (!cold) {
18487b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
1856d63101aSMike Smith 	    return(EPERM);
18687b45ed5SMitsuru IWASAKI 	}
1876d63101aSMike Smith 	break;
1886d63101aSMike Smith     case MOD_UNLOAD:
189f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1906d63101aSMike Smith 	    return(EBUSY);
191f9390180SMitsuru IWASAKI 	break;
1926d63101aSMike Smith     default:
1936d63101aSMike Smith 	break;
1946d63101aSMike Smith     }
1956d63101aSMike Smith     return(0);
1966d63101aSMike Smith }
1976d63101aSMike Smith 
1986d63101aSMike Smith /*
19915e32d5dSMike Smith  * Detect ACPI, perform early initialisation
20015e32d5dSMike Smith  */
20115e32d5dSMike Smith static void
20215e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20315e32d5dSMike Smith {
20415e32d5dSMike Smith     device_t			child;
20515e32d5dSMike Smith     int				error;
206d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
207d786139cSMaxime Henrion     char			*debugpoint;
20815e32d5dSMike Smith #endif
20915e32d5dSMike Smith 
210b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2110ae55423SMike Smith 
21287b45ed5SMitsuru IWASAKI     if (!cold)
21387b45ed5SMitsuru IWASAKI 	return_VOID;
214840f9b53STakanori Watanabe 
21515e32d5dSMike Smith     /*
21632d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
21732d18aa5SMike Smith      */
21832d18aa5SMike Smith     if (!resource_int_value("acpi", 0, "disabled", &error) &&
21932d18aa5SMike Smith 	(error != 0))
22032d18aa5SMike Smith 	return_VOID;
22132d18aa5SMike Smith 
22232d18aa5SMike Smith     /*
22315e32d5dSMike Smith      * Make sure we're not being doubly invoked.
22415e32d5dSMike Smith      */
22515e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2260ae55423SMike Smith 	return_VOID;
22715e32d5dSMike Smith 
228fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
229cb9b0d80SMike Smith     /* initialise the ACPI mutex */
2306008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
231fc0ea94aSJohn Baldwin #endif
232cb9b0d80SMike Smith 
23315e32d5dSMike Smith     /*
2342a4ac806SMike Smith      * Start up the ACPI CA subsystem.
23515e32d5dSMike Smith      */
236d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
237d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
238d786139cSMaxime Henrion     if (debugpoint) {
239d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
24015e32d5dSMike Smith 	    acpi_EnterDebugger();
241d786139cSMaxime Henrion         freeenv(debugpoint);
242d786139cSMaxime Henrion     }
24315e32d5dSMike Smith #endif
244b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
245bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2460ae55423SMike Smith 	return_VOID;
24715e32d5dSMike Smith     }
248d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
249d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
250d786139cSMaxime Henrion     if (debugpoint) {
251d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
25215e32d5dSMike Smith 	    acpi_EnterDebugger();
253d786139cSMaxime Henrion         freeenv(debugpoint);
254d786139cSMaxime Henrion     }
25515e32d5dSMike Smith #endif
2561611ea87SMitsuru IWASAKI 
257b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
258bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2590ae55423SMike Smith 	return_VOID;
26015e32d5dSMike Smith     }
26115e32d5dSMike Smith 
26215e32d5dSMike Smith     /*
26315e32d5dSMike Smith      * Attach the actual ACPI device.
26415e32d5dSMike Smith      */
26515e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
26615e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2670ae55423SMike Smith 	    return_VOID;
26815e32d5dSMike Smith     }
26915e32d5dSMike Smith }
27015e32d5dSMike Smith 
27115e32d5dSMike Smith /*
27215e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
27315e32d5dSMike Smith  */
27415e32d5dSMike Smith static int
27515e32d5dSMike Smith acpi_probe(device_t dev)
27615e32d5dSMike Smith {
27715e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
27815e32d5dSMike Smith     char		buf[20];
279cb9b0d80SMike Smith     ACPI_STATUS		status;
28015e32d5dSMike Smith     int			error;
281fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
28215e32d5dSMike Smith 
283b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
284f9390180SMitsuru IWASAKI 
285f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
286f9390180SMitsuru IWASAKI         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
287f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
288f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
289f9390180SMitsuru IWASAKI     }
290f9390180SMitsuru IWASAKI 
291cb9b0d80SMike Smith     ACPI_LOCK;
2920ae55423SMike Smith 
293b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
294bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
295cb9b0d80SMike Smith 	error = ENXIO;
296cb9b0d80SMike Smith     } else {
29715e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
29815e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
299cb9b0d80SMike Smith 	error = 0;
300cb9b0d80SMike Smith     }
301cb9b0d80SMike Smith     ACPI_UNLOCK;
302cb9b0d80SMike Smith     return_VALUE(error);
30315e32d5dSMike Smith }
30415e32d5dSMike Smith 
30515e32d5dSMike Smith static int
30615e32d5dSMike Smith acpi_attach(device_t dev)
30715e32d5dSMike Smith {
30815e32d5dSMike Smith     struct acpi_softc	*sc;
309cb9b0d80SMike Smith     ACPI_STATUS		status;
31015e32d5dSMike Smith     int			error;
31132d18aa5SMike Smith     UINT32		flags;
312498d464fSMitsuru IWASAKI     char		*env;
313d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
314d786139cSMaxime Henrion     char		*debugpoint;
31515e32d5dSMike Smith #endif
316fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
31715e32d5dSMike Smith 
318b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
319cb9b0d80SMike Smith     ACPI_LOCK;
32015e32d5dSMike Smith     sc = device_get_softc(dev);
32115e32d5dSMike Smith     bzero(sc, sizeof(*sc));
32215e32d5dSMike Smith     sc->acpi_dev = dev;
32315e32d5dSMike Smith 
324d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
325d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
326d786139cSMaxime Henrion     if (debugpoint) {
327d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
32815e32d5dSMike Smith 	    acpi_EnterDebugger();
329d786139cSMaxime Henrion         freeenv(debugpoint);
330d786139cSMaxime Henrion     }
33115e32d5dSMike Smith #endif
33215e32d5dSMike Smith 
33315e32d5dSMike Smith     /*
33415e32d5dSMike Smith      * Install the default address space handlers.
33515e32d5dSMike Smith      */
336cb9b0d80SMike Smith     error = ENXIO;
337b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3382a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
33915e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
340b53f2771SMike Smith 						NULL, NULL))) {
341bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
342cb9b0d80SMike Smith 	goto out;
34315e32d5dSMike Smith     }
344b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3452a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
34615e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
347b53f2771SMike Smith 						NULL, NULL))) {
348bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
349cb9b0d80SMike Smith 	goto out;
35015e32d5dSMike Smith     }
351b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3522a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
35315e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
354b53f2771SMike Smith 						NULL, NULL))) {
355bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
356cb9b0d80SMike Smith 	goto out;
35715e32d5dSMike Smith     }
35815e32d5dSMike Smith 
35915e32d5dSMike Smith     /*
36015e32d5dSMike Smith      * Bring ACPI fully online.
36115e32d5dSMike Smith      *
36232d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
36332d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
36432d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
36515e32d5dSMike Smith      *
36632d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
36732d18aa5SMike Smith      *
36832d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
36932d18aa5SMike Smith      *     child devices, but on many systems it works here.
37015e32d5dSMike Smith      */
371d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
372d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
373d786139cSMaxime Henrion     if (debugpoint) {
374d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
37515e32d5dSMike Smith 	    acpi_EnterDebugger();
376d786139cSMaxime Henrion         freeenv(debugpoint);
377d786139cSMaxime Henrion     }
37815e32d5dSMike Smith #endif
37932d18aa5SMike Smith     flags = 0;
380d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
38132d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
382b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
383bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
384cb9b0d80SMike Smith 	goto out;
38515e32d5dSMike Smith     }
38615e32d5dSMike Smith 
387b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
388b69ed3f4SMitsuru IWASAKI 	device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status));
389b69ed3f4SMitsuru IWASAKI 	goto out;
390b69ed3f4SMitsuru IWASAKI     }
391b69ed3f4SMitsuru IWASAKI 
39215e32d5dSMike Smith     /*
3931d073b1dSJohn Baldwin      * Setup our sysctl tree.
3941d073b1dSJohn Baldwin      *
3951d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3961d073b1dSJohn Baldwin      */
3971d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3981d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3991d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4001d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4011d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
402d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
403d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
404d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4051d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4061d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4071d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4081d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4091d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4101d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4111d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4121d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
413f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
414f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
415f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
416f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
417f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
418f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4192d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
420ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
421ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
422ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4231611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4241611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4251611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4262d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4272d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
428c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
429c6a78e98STakanori Watanabe 		   OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
430c6a78e98STakanori Watanabe 		   &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
431c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
432f3a6cbb6SMitsuru IWASAKI     sc->acpi_sleep_delay = 0;
433931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 1;
4342d644607SMitsuru IWASAKI     if (bootverbose)
4352d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
436498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
437498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
438498d464fSMitsuru IWASAKI 	freeenv(env);
439498d464fSMitsuru IWASAKI     }
4401d073b1dSJohn Baldwin 
4411d073b1dSJohn Baldwin     /*
44215e32d5dSMike Smith      * Dispatch the default sleep state to devices.
44315e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
44415e32d5dSMike Smith      */
44515e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
44615e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
44715e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
448f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
449f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
45015e32d5dSMike Smith 
45113d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
45215e32d5dSMike Smith 
45315e32d5dSMike Smith     /*
45415e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
45515e32d5dSMike Smith      */
456d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
457d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
458d786139cSMaxime Henrion     if (debugpoint) {
459d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
46015e32d5dSMike Smith 	    acpi_EnterDebugger();
461d786139cSMaxime Henrion 	freeenv(debugpoint);
462d786139cSMaxime Henrion     }
46315e32d5dSMike Smith #endif
46415e32d5dSMike Smith 
46515e32d5dSMike Smith     /*
46615e32d5dSMike Smith      * Register our shutdown handlers
46715e32d5dSMike Smith      */
46815e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
46915e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
47015e32d5dSMike Smith 
47115e32d5dSMike Smith     /*
47215e32d5dSMike Smith      * Register our acpi event handlers.
47315e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
47415e32d5dSMike Smith      */
47515e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
47615e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
47715e32d5dSMike Smith 
47815e32d5dSMike Smith     /*
47915e32d5dSMike Smith      * Flag our initial states.
48015e32d5dSMike Smith      */
48115e32d5dSMike Smith     sc->acpi_enabled = 1;
48215e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
483ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
48415e32d5dSMike Smith 
48515e32d5dSMike Smith     /*
48615e32d5dSMike Smith      * Create the control device
48715e32d5dSMike Smith      */
488a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4892a4689e8SRobert Watson 	"acpi");
49015e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
49115e32d5dSMike Smith 
492d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
493d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
494d786139cSMaxime Henrion     if (debugpoint) {
495d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "running"))
49615e32d5dSMike Smith 	    acpi_EnterDebugger();
497d786139cSMaxime Henrion 	freeenv(debugpoint);
498d786139cSMaxime Henrion     }
49915e32d5dSMike Smith #endif
500f86214b6SMitsuru IWASAKI 
50191da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
502c573e654SMitsuru IWASAKI     if ((error = acpi_task_thread_init())) {
503c573e654SMitsuru IWASAKI 	goto out;
504c573e654SMitsuru IWASAKI     }
505c573e654SMitsuru IWASAKI #endif
506c573e654SMitsuru IWASAKI 
507f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
508f86214b6SMitsuru IWASAKI 	goto out;
509f86214b6SMitsuru IWASAKI     }
510f86214b6SMitsuru IWASAKI 
511f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
512f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
513f9390180SMitsuru IWASAKI 
514eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
515eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
516eeb6dba2SJohn Baldwin 
517cb9b0d80SMike Smith     error = 0;
518cb9b0d80SMike Smith 
519cb9b0d80SMike Smith  out:
520cb9b0d80SMike Smith     ACPI_UNLOCK;
521cb9b0d80SMike Smith     return_VALUE(error);
52215e32d5dSMike Smith }
52315e32d5dSMike Smith 
52415e32d5dSMike Smith /*
52515e32d5dSMike Smith  * Handle a new device being added
52615e32d5dSMike Smith  */
52715e32d5dSMike Smith static device_t
52815e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
52915e32d5dSMike Smith {
53015e32d5dSMike Smith     struct acpi_device	*ad;
53115e32d5dSMike Smith     device_t		child;
53215e32d5dSMike Smith 
53315e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
53415e32d5dSMike Smith 	return(NULL);
53515e32d5dSMike Smith     bzero(ad, sizeof(*ad));
53615e32d5dSMike Smith 
53715e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
53815e32d5dSMike Smith 
53915e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
54015e32d5dSMike Smith     if (child != NULL)
54115e32d5dSMike Smith 	device_set_ivars(child, ad);
54215e32d5dSMike Smith     return(child);
54315e32d5dSMike Smith }
54415e32d5dSMike Smith 
54515e32d5dSMike Smith static int
54615e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
54715e32d5dSMike Smith {
54815e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
54915e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
55015e32d5dSMike Smith     int retval = 0;
55115e32d5dSMike Smith 
55215e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5539ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5549ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5559ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5569ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
55715e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
55815e32d5dSMike Smith 
55915e32d5dSMike Smith     return(retval);
56015e32d5dSMike Smith }
56115e32d5dSMike Smith 
56215e32d5dSMike Smith 
56315e32d5dSMike Smith /*
56415e32d5dSMike Smith  * Handle per-device ivars
56515e32d5dSMike Smith  */
56615e32d5dSMike Smith static int
56715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
56815e32d5dSMike Smith {
56915e32d5dSMike Smith     struct acpi_device	*ad;
57015e32d5dSMike Smith 
57115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
57215e32d5dSMike Smith 	printf("device has no ivars\n");
57315e32d5dSMike Smith 	return(ENOENT);
57415e32d5dSMike Smith     }
57515e32d5dSMike Smith 
57615e32d5dSMike Smith     switch(index) {
57715e32d5dSMike Smith 	/* ACPI ivars */
57815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
57915e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
58015e32d5dSMike Smith 	break;
58115e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
58215e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
58315e32d5dSMike Smith 	break;
58415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58515e32d5dSMike Smith 	*(void **)result = ad->ad_private;
58615e32d5dSMike Smith 	break;
58715e32d5dSMike Smith 
58832d18aa5SMike Smith 	/* ISA compatibility */
58932d18aa5SMike Smith     case ISA_IVAR_VENDORID:
59032d18aa5SMike Smith     case ISA_IVAR_SERIAL:
59132d18aa5SMike Smith     case ISA_IVAR_COMPATID:
59232d18aa5SMike Smith 	*(int *)result = -1;
59332d18aa5SMike Smith 	break;
59432d18aa5SMike Smith 
59532d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
59632d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59732d18aa5SMike Smith 	break;
59832d18aa5SMike Smith 
59915e32d5dSMike Smith     default:
60015e32d5dSMike Smith 	return(ENOENT);
60115e32d5dSMike Smith     }
60215e32d5dSMike Smith     return(0);
60315e32d5dSMike Smith }
60415e32d5dSMike Smith 
60515e32d5dSMike Smith static int
60615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
60715e32d5dSMike Smith {
60815e32d5dSMike Smith     struct acpi_device	*ad;
60915e32d5dSMike Smith 
61015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
61115e32d5dSMike Smith 	printf("device has no ivars\n");
61215e32d5dSMike Smith 	return(ENOENT);
61315e32d5dSMike Smith     }
61415e32d5dSMike Smith 
61515e32d5dSMike Smith     switch(index) {
61615e32d5dSMike Smith 	/* ACPI ivars */
61715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61815e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
61915e32d5dSMike Smith 	break;
62015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
62115e32d5dSMike Smith 	ad->ad_magic = (int )value;
62215e32d5dSMike Smith 	break;
62315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62415e32d5dSMike Smith 	ad->ad_private = (void *)value;
62515e32d5dSMike Smith 	break;
62615e32d5dSMike Smith 
62715e32d5dSMike Smith     default:
6282ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
62915e32d5dSMike Smith 	return(ENOENT);
63015e32d5dSMike Smith     }
63115e32d5dSMike Smith     return(0);
63215e32d5dSMike Smith }
63315e32d5dSMike Smith 
63415e32d5dSMike Smith /*
63515e32d5dSMike Smith  * Handle child resource allocation/removal
63615e32d5dSMike Smith  */
63715e32d5dSMike Smith static int
63815e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
63915e32d5dSMike Smith {
64015e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
64115e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
64215e32d5dSMike Smith 
64315e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
64415e32d5dSMike Smith 
64515e32d5dSMike Smith     return(0);
64615e32d5dSMike Smith }
64715e32d5dSMike Smith 
64815e32d5dSMike Smith static int
64915e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
65015e32d5dSMike Smith {
65115e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
65215e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
65315e32d5dSMike Smith     struct resource_list_entry	*rle;
65415e32d5dSMike Smith 
65515e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
65615e32d5dSMike Smith     if (!rle)
65715e32d5dSMike Smith 	return(ENOENT);
65815e32d5dSMike Smith 
65915e32d5dSMike Smith     if (startp)
66015e32d5dSMike Smith 	*startp = rle->start;
66115e32d5dSMike Smith     if (countp)
66215e32d5dSMike Smith 	*countp = rle->count;
66315e32d5dSMike Smith 
66415e32d5dSMike Smith     return(0);
66515e32d5dSMike Smith }
66615e32d5dSMike Smith 
66715e32d5dSMike Smith static struct resource *
66815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
66915e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
67015e32d5dSMike Smith {
67115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
67215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
67315e32d5dSMike Smith 
67415e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
67515e32d5dSMike Smith }
67615e32d5dSMike Smith 
67715e32d5dSMike Smith static int
67815e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
67915e32d5dSMike Smith {
68015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
68115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
68215e32d5dSMike Smith 
68315e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
68415e32d5dSMike Smith }
68515e32d5dSMike Smith 
68615e32d5dSMike Smith /*
687bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
688bc0f2195SMike Smith  */
689bc0f2195SMike Smith #define PNP_EISAID(s)				\
690bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
691bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
692bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
693bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
694bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
695bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
696bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
697bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
698bc0f2195SMike Smith 
69932d18aa5SMike Smith static u_int32_t
70032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
701bc0f2195SMike Smith {
702bc0f2195SMike Smith     ACPI_HANDLE		h;
703bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
704bc0f2195SMike Smith     ACPI_STATUS		error;
705bc0f2195SMike Smith     u_int32_t		pnpid;
706fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
70732d18aa5SMike Smith 
708b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
70932d18aa5SMike Smith 
71032d18aa5SMike Smith     pnpid = 0;
71132d18aa5SMike Smith     ACPI_LOCK;
71232d18aa5SMike Smith 
71332d18aa5SMike Smith     /* fetch and validate the HID */
71432d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
71532d18aa5SMike Smith 	goto out;
716b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
71732d18aa5SMike Smith 	goto out;
71832d18aa5SMike Smith     if (!(devinfo.Valid & ACPI_VALID_HID))
71932d18aa5SMike Smith 	goto out;
72032d18aa5SMike Smith 
72132d18aa5SMike Smith     pnpid = PNP_EISAID(devinfo.HardwareId);
72232d18aa5SMike Smith out:
72332d18aa5SMike Smith     ACPI_UNLOCK;
72432d18aa5SMike Smith     return_VALUE(pnpid);
72532d18aa5SMike Smith }
72632d18aa5SMike Smith 
727b2566207STakanori Watanabe static u_int32_t
728b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev)
729b2566207STakanori Watanabe {
730b2566207STakanori Watanabe     ACPI_HANDLE		h;
731b2566207STakanori Watanabe     ACPI_DEVICE_INFO	devinfo;
732b2566207STakanori Watanabe     ACPI_STATUS		error;
733b2566207STakanori Watanabe     u_int32_t		pnpid;
734b2566207STakanori Watanabe     ACPI_LOCK_DECL;
735b2566207STakanori Watanabe 
736b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
737b2566207STakanori Watanabe 
738b2566207STakanori Watanabe     pnpid = 0;
739b2566207STakanori Watanabe     ACPI_LOCK;
740b2566207STakanori Watanabe 
741b2566207STakanori Watanabe     /* fetch and validate the HID */
742b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
743b2566207STakanori Watanabe 	goto out;
744b2566207STakanori Watanabe     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
745b2566207STakanori Watanabe 	goto out;
746b2566207STakanori Watanabe     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
747b2566207STakanori Watanabe 	goto out;
748b2566207STakanori Watanabe 
749b2566207STakanori Watanabe out:
750b2566207STakanori Watanabe     ACPI_UNLOCK;
751b2566207STakanori Watanabe     return_VALUE(pnpid);
752b2566207STakanori Watanabe }
753b2566207STakanori Watanabe 
754b2566207STakanori Watanabe 
75532d18aa5SMike Smith static int
75632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
75732d18aa5SMike Smith {
758bc0f2195SMike Smith     int			result;
759b2566207STakanori Watanabe     u_int32_t		lid, cid;
760bc0f2195SMike Smith 
761b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
762bc0f2195SMike Smith 
763bc0f2195SMike Smith     /*
764bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
765bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
766bc0f2195SMike Smith      * that to happen, so don't ever return it.
767bc0f2195SMike Smith      */
768bc0f2195SMike Smith     result = ENXIO;
769bc0f2195SMike Smith 
770bc0f2195SMike Smith     /* scan the supplied IDs for a match */
771b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
772b2566207STakanori Watanabe     cid = acpi_isa_get_compatid(child);
773bc0f2195SMike Smith     while (ids && ids->ip_id) {
774b2566207STakanori Watanabe 	if (lid == ids->ip_id || cid == ids->ip_id) {
775bc0f2195SMike Smith 	    result = 0;
776bc0f2195SMike Smith 	    goto out;
777bc0f2195SMike Smith 	}
778bc0f2195SMike Smith 	ids++;
779bc0f2195SMike Smith     }
780bc0f2195SMike Smith  out:
781bc0f2195SMike Smith     return_VALUE(result);
782bc0f2195SMike Smith }
783bc0f2195SMike Smith 
784bc0f2195SMike Smith /*
78515e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
78615e32d5dSMike Smith  *
7877d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7887d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
78915e32d5dSMike Smith  */
79015e32d5dSMike Smith static void
79115e32d5dSMike Smith acpi_probe_children(device_t bus)
79215e32d5dSMike Smith {
79315e32d5dSMike Smith     ACPI_HANDLE		parent;
7947d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
79515e32d5dSMike Smith     int			i;
79615e32d5dSMike Smith 
797b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
798cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7990ae55423SMike Smith 
80015e32d5dSMike Smith     /*
80115e32d5dSMike Smith      * Create any static children by calling device identify methods.
80215e32d5dSMike Smith      */
8034c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
80415e32d5dSMike Smith     bus_generic_probe(bus);
80515e32d5dSMike Smith 
80615e32d5dSMike Smith     /*
80715e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
80815e32d5dSMike Smith      * we find.
80915e32d5dSMike Smith      *
81015e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
81115e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
81215e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
81315e32d5dSMike Smith      * appear, which might be smarter.)
81415e32d5dSMike Smith      */
8154c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
81615e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
817b53f2771SMike Smith 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
81815e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
81915e32d5dSMike Smith 
82015e32d5dSMike Smith     /*
82115e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
82215e32d5dSMike Smith      */
8234c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
82415e32d5dSMike Smith     bus_generic_attach(bus);
82515e32d5dSMike Smith 
82615e32d5dSMike Smith     /*
82715e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
82815e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
82915e32d5dSMike Smith      */
8304c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
83115e32d5dSMike Smith     bus_generic_attach(bus);
8320ae55423SMike Smith 
833bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8340ae55423SMike Smith     return_VOID;
83515e32d5dSMike Smith }
83615e32d5dSMike Smith 
83715e32d5dSMike Smith /*
83815e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
83915e32d5dSMike Smith  * it.
84015e32d5dSMike Smith  */
84115e32d5dSMike Smith static ACPI_STATUS
84215e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
84315e32d5dSMike Smith {
84415e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
84515e32d5dSMike Smith     device_t		child, bus = (device_t)context;
84615e32d5dSMike Smith 
847b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
8480ae55423SMike Smith 
8490ae55423SMike Smith     /*
8500ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8510ae55423SMike Smith      */
8520ae55423SMike Smith     if (acpi_avoid(handle))
8530ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8540ae55423SMike Smith 
855b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
85615e32d5dSMike Smith 	switch(type) {
85715e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
85815e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
85915e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
86015e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8610ae55423SMike Smith 	    if (acpi_disabled("children"))
8620ae55423SMike Smith 		break;
86315e32d5dSMike Smith 	    /*
86415e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
86515e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
86615e32d5dSMike Smith 	     */
8674c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
86815e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
869bc0f2195SMike Smith 	    if (child == NULL)
870bc0f2195SMike Smith 		break;
87115e32d5dSMike Smith 	    acpi_set_handle(child, handle);
872bc0f2195SMike Smith 
873bc0f2195SMike Smith 	    /*
874b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
875b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
876b519f9d6SMike Smith 	     * the handle, but we don't probe it).
877b519f9d6SMike Smith 	     */
87823b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
879b519f9d6SMike Smith 		device_disable(child);
880b519f9d6SMike Smith 		break;
881b519f9d6SMike Smith 	    }
882b519f9d6SMike Smith 
883b519f9d6SMike Smith 	    /*
884bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
885bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
886bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
887bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
888bc0f2195SMike Smith 	     * device not to have any resources.
889bc0f2195SMike Smith 	     */
890bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
891bc0f2195SMike Smith 
892bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
893b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
8940ae55423SMike Smith 	    break;
89515e32d5dSMike Smith 	}
89615e32d5dSMike Smith     }
8970ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
89815e32d5dSMike Smith }
89915e32d5dSMike Smith 
90015e32d5dSMike Smith static void
90115e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
90215e32d5dSMike Smith {
903cb9b0d80SMike Smith 
904c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
905c6a78e98STakanori Watanabe 
906cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
907cb9b0d80SMike Smith 
90815e32d5dSMike Smith     /*
9090ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
91015e32d5dSMike Smith      * will be turned on again on some laptops.
91115e32d5dSMike Smith      *
91215e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
91315e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
91415e32d5dSMike Smith      *     shutdown process.
91515e32d5dSMike Smith      */
916c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
917c6a78e98STakanori Watanabe 	acpi_Disable(sc);
91815e32d5dSMike Smith }
91915e32d5dSMike Smith 
92015e32d5dSMike Smith static void
92115e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
92215e32d5dSMike Smith {
92315e32d5dSMike Smith     ACPI_STATUS	status;
92415e32d5dSMike Smith 
925cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
926cb9b0d80SMike Smith 
9275f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
92815e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
929b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
930b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
931b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
932b9c780d6SMitsuru IWASAKI 	    return;
933b9c780d6SMitsuru IWASAKI 	}
934b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
935bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
93615e32d5dSMike Smith 	} else {
93715e32d5dSMike Smith 	    DELAY(1000000);
93815e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
93915e32d5dSMike Smith 	}
940494ae560SMitsuru IWASAKI     } else {
941494ae560SMitsuru IWASAKI 	printf("Terminate ACPI\n");
942494ae560SMitsuru IWASAKI 	AcpiTerminate();
94315e32d5dSMike Smith     }
94415e32d5dSMike Smith }
94515e32d5dSMike Smith 
94613d4f7baSMitsuru IWASAKI static void
94713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
94813d4f7baSMitsuru IWASAKI {
94913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
95013d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
95113d4f7baSMitsuru IWASAKI 
952cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
953cb9b0d80SMike Smith 
95413d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
955cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
95643896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
95713d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
95813d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
95913d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
96013d4f7baSMitsuru IWASAKI 	if (first_time) {
96113d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
96213d4f7baSMitsuru IWASAKI 	}
96313d4f7baSMitsuru IWASAKI     }
964cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
96543896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
96613d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
96713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
96813d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
96913d4f7baSMitsuru IWASAKI 	if (first_time) {
97013d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
97113d4f7baSMitsuru IWASAKI 	}
97213d4f7baSMitsuru IWASAKI     }
97313d4f7baSMitsuru IWASAKI 
97413d4f7baSMitsuru IWASAKI     first_time = 0;
97513d4f7baSMitsuru IWASAKI }
97613d4f7baSMitsuru IWASAKI 
97715e32d5dSMike Smith /*
978c5ba0be4SMike Smith  * Returns true if the device is actually present and should
979c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
980c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
981c5ba0be4SMike Smith  */
982c5ba0be4SMike Smith BOOLEAN
983c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
984c5ba0be4SMike Smith {
985c5ba0be4SMike Smith     ACPI_HANDLE		h;
986c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
987c5ba0be4SMike Smith     ACPI_STATUS		error;
988c5ba0be4SMike Smith 
989cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
990cb9b0d80SMike Smith 
991c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
992c5ba0be4SMike Smith 	return(FALSE);
993b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
994c5ba0be4SMike Smith 	return(FALSE);
99543896e91SMike Smith     /* if no _STA method, must be present */
99643896e91SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
99743896e91SMike Smith 	return(TRUE);
99843896e91SMike Smith     /* return true for 'present' and 'functioning' */
99943896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
1000c5ba0be4SMike Smith 	return(TRUE);
1001c5ba0be4SMike Smith     return(FALSE);
1002c5ba0be4SMike Smith }
1003c5ba0be4SMike Smith 
1004c5ba0be4SMike Smith /*
1005b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1006b53f2771SMike Smith  */
1007b53f2771SMike Smith BOOLEAN
1008b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1009b53f2771SMike Smith {
1010b53f2771SMike Smith     ACPI_HANDLE		h;
1011b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
1012b53f2771SMike Smith     ACPI_STATUS		error;
1013b53f2771SMike Smith 
1014b53f2771SMike Smith     ACPI_ASSERTLOCK;
1015b53f2771SMike Smith 
1016b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1017b53f2771SMike Smith 	return(FALSE);
1018b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
1019b53f2771SMike Smith 	return(FALSE);
1020b53f2771SMike Smith     /* if no _STA method, must be present */
1021b53f2771SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
1022b53f2771SMike Smith 	return(TRUE);
1023b53f2771SMike Smith     /* return true for 'present' and 'functioning' */
1024b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1025b53f2771SMike Smith 	return(TRUE);
1026b53f2771SMike Smith     return(FALSE);
1027b53f2771SMike Smith }
1028b53f2771SMike Smith 
1029b53f2771SMike Smith /*
103015e32d5dSMike Smith  * Match a HID string against a device
103115e32d5dSMike Smith  */
103215e32d5dSMike Smith BOOLEAN
103315e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
103415e32d5dSMike Smith {
103515e32d5dSMike Smith     ACPI_HANDLE		h;
103615e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
103715e32d5dSMike Smith     ACPI_STATUS		error;
1038ed136da6SDoug Rabson     int			cid;
103915e32d5dSMike Smith 
1040cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1041cb9b0d80SMike Smith 
10424d332989SMike Smith     if (hid == NULL)
104315e32d5dSMike Smith 	return(FALSE);
104415e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
104515e32d5dSMike Smith 	return(FALSE);
1046b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
104715e32d5dSMike Smith 	return(FALSE);
10484d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
104915e32d5dSMike Smith 	return(TRUE);
1050b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1051ed136da6SDoug Rabson 	return(FALSE);
1052ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1053ed136da6SDoug Rabson 	return(TRUE);
105415e32d5dSMike Smith     return(FALSE);
105515e32d5dSMike Smith }
105615e32d5dSMike Smith 
105715e32d5dSMike Smith /*
1058c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1059c5ba0be4SMike Smith  * or one if its parents.
1060c5ba0be4SMike Smith  */
1061c5ba0be4SMike Smith ACPI_STATUS
1062c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1063c5ba0be4SMike Smith {
1064c5ba0be4SMike Smith     ACPI_HANDLE		r;
1065c5ba0be4SMike Smith     ACPI_STATUS		status;
1066c5ba0be4SMike Smith 
1067cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1068cb9b0d80SMike Smith 
1069c5ba0be4SMike Smith     /* walk back up the tree to the root */
1070c5ba0be4SMike Smith     for (;;) {
1071b53f2771SMike Smith 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
1072c5ba0be4SMike Smith 	    *result = r;
1073c5ba0be4SMike Smith 	    return(AE_OK);
1074c5ba0be4SMike Smith 	}
1075c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1076c5ba0be4SMike Smith 	    return(AE_OK);
1077b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1078c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1079c5ba0be4SMike Smith 	parent = r;
1080c5ba0be4SMike Smith     }
1081c5ba0be4SMike Smith }
1082c5ba0be4SMike Smith 
1083c5ba0be4SMike Smith /*
1084c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1085c5ba0be4SMike Smith  */
1086c5ba0be4SMike Smith ACPI_BUFFER *
1087c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1088c5ba0be4SMike Smith {
1089c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1090c5ba0be4SMike Smith 
1091c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1092c5ba0be4SMike Smith 	return(NULL);
1093c5ba0be4SMike Smith     buf->Length = size;
1094c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1095c5ba0be4SMike Smith     return(buf);
1096c5ba0be4SMike Smith }
1097c5ba0be4SMike Smith 
1098c5ba0be4SMike Smith /*
1099c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1100c5ba0be4SMike Smith  */
1101c5ba0be4SMike Smith ACPI_STATUS
1102c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1103c5ba0be4SMike Smith {
1104c5ba0be4SMike Smith     ACPI_STATUS	error;
1105c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1106c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1107c5ba0be4SMike Smith 
1108cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1109cb9b0d80SMike Smith 
1110c5ba0be4SMike Smith     if (handle == NULL)
1111c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
11120c7da7acSJohn Baldwin 
11130c7da7acSJohn Baldwin     /*
11140c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
11150c7da7acSJohn Baldwin      * a method that will return an Integer.
11160c7da7acSJohn Baldwin      */
1117c5ba0be4SMike Smith     buf.Pointer = &param;
1118c5ba0be4SMike Smith     buf.Length = sizeof(param);
1119b53f2771SMike Smith     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1120c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1121c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1122c5ba0be4SMike Smith 	} else {
1123c5ba0be4SMike Smith 	    error = AE_TYPE;
1124c5ba0be4SMike Smith 	}
1125c5ba0be4SMike Smith     }
11260c7da7acSJohn Baldwin 
11270c7da7acSJohn Baldwin     /*
11280c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
11290c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
11300c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
11310c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
11320c7da7acSJohn Baldwin      *
11330c7da7acSJohn Baldwin      * This is a hack.
11340c7da7acSJohn Baldwin      */
11350c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
1136b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
11370c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11380c7da7acSJohn Baldwin 	} else {
1139b53f2771SMike Smith 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
1140c573e654SMitsuru IWASAKI 		error = acpi_ConvertBufferToInteger(&buf, number);
11410c7da7acSJohn Baldwin 	    }
11420c7da7acSJohn Baldwin 	}
11430c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11440c7da7acSJohn Baldwin     }
1145c5ba0be4SMike Smith     return(error);
1146c5ba0be4SMike Smith }
1147c5ba0be4SMike Smith 
1148c573e654SMitsuru IWASAKI ACPI_STATUS
1149c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1150c573e654SMitsuru IWASAKI {
1151c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1152c573e654SMitsuru IWASAKI     int		i;
1153c573e654SMitsuru IWASAKI 
1154c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1155c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1156c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1157c573e654SMitsuru IWASAKI 	return(AE_OK);
1158c573e654SMitsuru IWASAKI     }
1159c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1160c573e654SMitsuru IWASAKI 	return(AE_TYPE);
1161c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1162c573e654SMitsuru IWASAKI 	return(AE_BAD_DATA);
1163c573e654SMitsuru IWASAKI     *number = 0;
1164c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1165c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1166c573e654SMitsuru IWASAKI     return(AE_OK);
1167c573e654SMitsuru IWASAKI }
1168c573e654SMitsuru IWASAKI 
1169c5ba0be4SMike Smith /*
1170c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1171c5ba0be4SMike Smith  * function for each element.
1172c5ba0be4SMike Smith  *
1173c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1174c5ba0be4SMike Smith  */
1175c5ba0be4SMike Smith ACPI_STATUS
1176c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1177c5ba0be4SMike Smith {
1178c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1179c5ba0be4SMike Smith     int		i;
1180c5ba0be4SMike Smith 
1181c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1182c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1183c5ba0be4SMike Smith 
1184c5ba0be4SMike Smith     /* iterate over components */
1185c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1186c5ba0be4SMike Smith 	func(comp, arg);
1187c5ba0be4SMike Smith 
1188c5ba0be4SMike Smith     return(AE_OK);
1189c5ba0be4SMike Smith }
1190c5ba0be4SMike Smith 
11916f69255bSMike Smith /*
11926f69255bSMike Smith  * Find the (index)th resource object in a set.
11936f69255bSMike Smith  */
11946f69255bSMike Smith ACPI_STATUS
11956d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11966f69255bSMike Smith {
11976d63101aSMike Smith     ACPI_RESOURCE	*rp;
11986f69255bSMike Smith     int			i;
11996f69255bSMike Smith 
12006d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12016f69255bSMike Smith     i = index;
12026d63101aSMike Smith     while (i-- > 0) {
12036f69255bSMike Smith 	/* range check */
12046d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12056f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
12066d63101aSMike Smith 	/* check for terminator */
12076d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12086d63101aSMike Smith 	    (rp->Length == 0))
12096d63101aSMike Smith 	    return(AE_NOT_FOUND);
12106d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12116f69255bSMike Smith     }
12126f69255bSMike Smith     if (resp != NULL)
12136d63101aSMike Smith 	*resp = rp;
12146d63101aSMike Smith     return(AE_OK);
12156d63101aSMike Smith }
12166d63101aSMike Smith 
12176d63101aSMike Smith /*
12186d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
12196d63101aSMike Smith  *
12206d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
12216d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
12226d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
12236d63101aSMike Smith  * resources.
12246d63101aSMike Smith  */
12256d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
12266d63101aSMike Smith 
12276d63101aSMike Smith ACPI_STATUS
12286d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
12296d63101aSMike Smith {
12306d63101aSMike Smith     ACPI_RESOURCE	*rp;
12316d63101aSMike Smith     void		*newp;
12326d63101aSMike Smith 
12336d63101aSMike Smith     /*
12346d63101aSMike Smith      * Initialise the buffer if necessary.
12356d63101aSMike Smith      */
12366d63101aSMike Smith     if (buf->Pointer == NULL) {
12376d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12386d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12396d63101aSMike Smith 	    return(AE_NO_MEMORY);
12406d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12416d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12426d63101aSMike Smith 	rp->Length = 0;
12436d63101aSMike Smith     }
12446d63101aSMike Smith     if (res == NULL)
12456d63101aSMike Smith 	return(AE_OK);
12466d63101aSMike Smith 
12476d63101aSMike Smith     /*
12486d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12496d63101aSMike Smith      * This will either find the terminator or hit the end
12506d63101aSMike Smith      * of the buffer and return an error.
12516d63101aSMike Smith      */
12526d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12536d63101aSMike Smith     for (;;) {
12546d63101aSMike Smith 	/* range check, don't go outside the buffer */
12556d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12566d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12576d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12586d63101aSMike Smith 	    (rp->Length == 0)) {
12596d63101aSMike Smith 	    break;
12606d63101aSMike Smith 	}
12616d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12626d63101aSMike Smith     }
12636d63101aSMike Smith 
12646d63101aSMike Smith     /*
12656d63101aSMike Smith      * Check the size of the buffer and expand if required.
12666d63101aSMike Smith      *
12676d63101aSMike Smith      * Required size is:
12686d63101aSMike Smith      *	size of existing resources before terminator +
12696d63101aSMike Smith      *	size of new resource and header +
12706d63101aSMike Smith      * 	size of terminator.
12716d63101aSMike Smith      *
12726d63101aSMike Smith      * Note that this loop should really only run once, unless
12736d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12746d63101aSMike Smith      */
12756d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12766d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12776d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12786d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12796d63101aSMike Smith 	    return(AE_NO_MEMORY);
12806d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1281a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1282a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12836d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12846d63101aSMike Smith 	buf->Pointer = newp;
12856d63101aSMike Smith 	buf->Length += buf->Length;
12866d63101aSMike Smith     }
12876d63101aSMike Smith 
12886d63101aSMike Smith     /*
12896d63101aSMike Smith      * Insert the new resource.
12906d63101aSMike Smith      */
12916d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12926d63101aSMike Smith 
12936d63101aSMike Smith     /*
12946d63101aSMike Smith      * And add the terminator.
12956d63101aSMike Smith      */
12966d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12976d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12986d63101aSMike Smith     rp->Length = 0;
12996d63101aSMike Smith 
13006f69255bSMike Smith     return(AE_OK);
13016f69255bSMike Smith }
1302c5ba0be4SMike Smith 
1303da14ac9fSJohn Baldwin /*
1304da14ac9fSJohn Baldwin  * Set interrupt model.
1305da14ac9fSJohn Baldwin  */
1306da14ac9fSJohn Baldwin ACPI_STATUS
1307da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1308da14ac9fSJohn Baldwin {
1309da14ac9fSJohn Baldwin 	ACPI_OBJECT_LIST ArgList;
1310da14ac9fSJohn Baldwin 	ACPI_OBJECT Arg;
1311da14ac9fSJohn Baldwin 
1312da14ac9fSJohn Baldwin 	Arg.Type = ACPI_TYPE_INTEGER;
1313da14ac9fSJohn Baldwin 	Arg.Integer.Value = model;
1314da14ac9fSJohn Baldwin 	ArgList.Count = 1;
1315da14ac9fSJohn Baldwin 	ArgList.Pointer = &Arg;
1316da14ac9fSJohn Baldwin 	return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1317da14ac9fSJohn Baldwin }
1318da14ac9fSJohn Baldwin 
1319ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1320ece50487SMitsuru IWASAKI 
1321ece50487SMitsuru IWASAKI static void
1322ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1323ece50487SMitsuru IWASAKI {
1324ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1325ece50487SMitsuru IWASAKI }
1326c30382dfSMitsuru IWASAKI 
132715e32d5dSMike Smith /*
132815e32d5dSMike Smith  * Set the system sleep state
132915e32d5dSMike Smith  *
133015e32d5dSMike Smith  * Currently we only support S1 and S5
133115e32d5dSMike Smith  */
133215e32d5dSMike Smith ACPI_STATUS
133315e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
133415e32d5dSMike Smith {
133515e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
133656d8cb57SMitsuru IWASAKI     UINT8	TypeA;
133756d8cb57SMitsuru IWASAKI     UINT8	TypeB;
133815e32d5dSMike Smith 
1339b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1340cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13410ae55423SMike Smith 
13426397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
13436397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13446397624dSMitsuru IWASAKI 
1345ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1346ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1347ece50487SMitsuru IWASAKI 
134815e32d5dSMike Smith     switch (state) {
134915e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1350b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1351bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
135215e32d5dSMike Smith 	}
135315e32d5dSMike Smith 	break;
135415e32d5dSMike Smith 
135515e32d5dSMike Smith     case ACPI_STATE_S1:
13566161544cSTakanori Watanabe     case ACPI_STATE_S2:
13576161544cSTakanori Watanabe     case ACPI_STATE_S3:
13586161544cSTakanori Watanabe     case ACPI_STATE_S4:
135998479b04SMitsuru IWASAKI 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
136098479b04SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
136156d8cb57SMitsuru IWASAKI 	    break;
136256d8cb57SMitsuru IWASAKI 	}
136356d8cb57SMitsuru IWASAKI 
1364a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1365a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1366a1fccb47SMitsuru IWASAKI 
136715e32d5dSMike Smith 	/*
136815e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
136915e32d5dSMike Smith 	 */
137015e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
137115e32d5dSMike Smith 	    /*
137215e32d5dSMike Smith 	     * Re-wake the system.
137315e32d5dSMike Smith 	     *
137415e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
137515e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
137615e32d5dSMike Smith 	     *     current bus interface does not provide for this.
137715e32d5dSMike Smith 	     */
137815e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13790ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
138015e32d5dSMike Smith 	}
1381b9c780d6SMitsuru IWASAKI 
1382b53f2771SMike Smith 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
1383b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1384b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1385b9c780d6SMitsuru IWASAKI 	    break;
1386b9c780d6SMitsuru IWASAKI 	}
1387b9c780d6SMitsuru IWASAKI 
1388ff01efb5SMitsuru IWASAKI 	if (sc->acpi_sleep_delay > 0) {
1389ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1390ff01efb5SMitsuru IWASAKI 	}
1391ff01efb5SMitsuru IWASAKI 
13926161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13936161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13946161544cSTakanori Watanabe 
1395a1fccb47SMitsuru IWASAKI 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
1396a1fccb47SMitsuru IWASAKI 	    if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) {
13976161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1398a1fccb47SMitsuru IWASAKI 	    }
13996161544cSTakanori Watanabe 
14006161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1401964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1402964679ceSMitsuru IWASAKI 		AcpiEnable();
14036161544cSTakanori Watanabe 	    }
14046161544cSTakanori Watanabe 	} else {
1405b53f2771SMike Smith 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
1406bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1407c30382dfSMitsuru IWASAKI 		break;
140815e32d5dSMike Smith 	    }
14096161544cSTakanori Watanabe 	}
1410ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
141115e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
141215e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
141313d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
141415e32d5dSMike Smith 	break;
141515e32d5dSMike Smith 
141615e32d5dSMike Smith     case ACPI_STATE_S5:
141715e32d5dSMike Smith 	/*
141815e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
141915e32d5dSMike Smith 	 * shutdown handlers.
142015e32d5dSMike Smith 	 */
142115e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
142215e32d5dSMike Smith 	break;
142315e32d5dSMike Smith 
142415e32d5dSMike Smith     default:
142515e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
142615e32d5dSMike Smith 	break;
142715e32d5dSMike Smith     }
1428ece50487SMitsuru IWASAKI 
1429ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1430ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1431ece50487SMitsuru IWASAKI 
14320ae55423SMike Smith     return_ACPI_STATUS(status);
143315e32d5dSMike Smith }
143415e32d5dSMike Smith 
143515e32d5dSMike Smith /*
143615e32d5dSMike Smith  * Enable/Disable ACPI
143715e32d5dSMike Smith  */
143815e32d5dSMike Smith ACPI_STATUS
143915e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
144015e32d5dSMike Smith {
144115e32d5dSMike Smith     ACPI_STATUS	status;
144215e32d5dSMike Smith     u_int32_t	flags;
144315e32d5dSMike Smith 
1444b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1445cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14460ae55423SMike Smith 
144715e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
144815e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
144915e32d5dSMike Smith     if (!sc->acpi_enabled) {
145015e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
145115e32d5dSMike Smith     } else {
145215e32d5dSMike Smith 	status = AE_OK;
145315e32d5dSMike Smith     }
145415e32d5dSMike Smith     if (status == AE_OK)
145515e32d5dSMike Smith 	sc->acpi_enabled = 1;
14560ae55423SMike Smith     return_ACPI_STATUS(status);
145715e32d5dSMike Smith }
145815e32d5dSMike Smith 
145915e32d5dSMike Smith ACPI_STATUS
146015e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
146115e32d5dSMike Smith {
146215e32d5dSMike Smith     ACPI_STATUS	status;
146315e32d5dSMike Smith 
1464b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1465cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14660ae55423SMike Smith 
146715e32d5dSMike Smith     if (sc->acpi_enabled) {
146815e32d5dSMike Smith 	status = AcpiDisable();
146915e32d5dSMike Smith     } else {
147015e32d5dSMike Smith 	status = AE_OK;
147115e32d5dSMike Smith     }
147215e32d5dSMike Smith     if (status == AE_OK)
147315e32d5dSMike Smith 	sc->acpi_enabled = 0;
14740ae55423SMike Smith     return_ACPI_STATUS(status);
147515e32d5dSMike Smith }
147615e32d5dSMike Smith 
147715e32d5dSMike Smith /*
147815e32d5dSMike Smith  * ACPI Event Handlers
147915e32d5dSMike Smith  */
148015e32d5dSMike Smith 
148115e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
148215e32d5dSMike Smith 
148315e32d5dSMike Smith static void
148415e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
148515e32d5dSMike Smith {
1486fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1487b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
148815e32d5dSMike Smith 
1489cb9b0d80SMike Smith     ACPI_LOCK;
1490a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
149115e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1492cb9b0d80SMike Smith     ACPI_UNLOCK;
14930ae55423SMike Smith     return_VOID;
149415e32d5dSMike Smith }
149515e32d5dSMike Smith 
149615e32d5dSMike Smith static void
149715e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
149815e32d5dSMike Smith {
1499fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1500b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
15010ae55423SMike Smith 
150215e32d5dSMike Smith     /* Well, what to do? :-) */
15030ae55423SMike Smith 
1504cb9b0d80SMike Smith     ACPI_LOCK;
1505cb9b0d80SMike Smith     ACPI_UNLOCK;
1506cb9b0d80SMike Smith 
15070ae55423SMike Smith     return_VOID;
150815e32d5dSMike Smith }
150915e32d5dSMike Smith 
151015e32d5dSMike Smith /*
151115e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
151215e32d5dSMike Smith  */
151315e32d5dSMike Smith UINT32
151415e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
151515e32d5dSMike Smith {
151615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
151715e32d5dSMike Smith 
1518b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15190ae55423SMike Smith 
152015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
15210ae55423SMike Smith 
1522b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
152315e32d5dSMike Smith }
152415e32d5dSMike Smith 
152515e32d5dSMike Smith UINT32
152615e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
152715e32d5dSMike Smith {
152815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
152915e32d5dSMike Smith 
1530b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15310ae55423SMike Smith 
153215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
15330ae55423SMike Smith 
1534b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
153515e32d5dSMike Smith }
153615e32d5dSMike Smith 
153715e32d5dSMike Smith UINT32
153815e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
153915e32d5dSMike Smith {
154015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
154115e32d5dSMike Smith 
1542b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15430ae55423SMike Smith 
154415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
15450ae55423SMike Smith 
1546b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
154715e32d5dSMike Smith }
154815e32d5dSMike Smith 
154915e32d5dSMike Smith UINT32
155015e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
155115e32d5dSMike Smith {
155215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
155315e32d5dSMike Smith 
1554b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15550ae55423SMike Smith 
155615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15570ae55423SMike Smith 
1558b53f2771SMike Smith     return_VALUE(ACPI_INTERRUPT_HANDLED);
155915e32d5dSMike Smith }
156015e32d5dSMike Smith 
156115e32d5dSMike Smith /*
156215e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
156315e32d5dSMike Smith  */
156415e32d5dSMike Smith struct acpi_staticbuf {
156515e32d5dSMike Smith     ACPI_BUFFER	buffer;
156615e32d5dSMike Smith     char	data[512];
156715e32d5dSMike Smith };
156815e32d5dSMike Smith 
156915e32d5dSMike Smith char *
157015e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
157115e32d5dSMike Smith {
157215e32d5dSMike Smith     static struct acpi_staticbuf	buf;
157315e32d5dSMike Smith 
1574cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1575cb9b0d80SMike Smith 
157615e32d5dSMike Smith     buf.buffer.Length = 512;
157715e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
157815e32d5dSMike Smith 
1579b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
158015e32d5dSMike Smith 	return(buf.buffer.Pointer);
158115e32d5dSMike Smith     return("(unknown path)");
158215e32d5dSMike Smith }
158315e32d5dSMike Smith 
158415e32d5dSMike Smith /*
158515e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
158615e32d5dSMike Smith  * parts of the namespace.
158715e32d5dSMike Smith  */
158815e32d5dSMike Smith int
158915e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
159015e32d5dSMike Smith {
15913c600cfbSJohn Baldwin     char	*cp, *env, *np;
159215e32d5dSMike Smith     int		len;
159315e32d5dSMike Smith 
159415e32d5dSMike Smith     np = acpi_name(handle);
159515e32d5dSMike Smith     if (*np == '\\')
159615e32d5dSMike Smith 	np++;
15973c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
159815e32d5dSMike Smith 	return(0);
159915e32d5dSMike Smith 
160015e32d5dSMike Smith     /* scan the avoid list checking for a match */
16013c600cfbSJohn Baldwin     cp = env;
160215e32d5dSMike Smith     for (;;) {
160315e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
160415e32d5dSMike Smith 	    cp++;
160515e32d5dSMike Smith 	if (*cp == 0)
160615e32d5dSMike Smith 	    break;
160715e32d5dSMike Smith 	len = 0;
160815e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
160915e32d5dSMike Smith 	    len++;
1610d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
16113c600cfbSJohn Baldwin 	    freeenv(env);
16120ae55423SMike Smith 	    return(1);
1613d786139cSMaxime Henrion 	}
16140ae55423SMike Smith 	cp += len;
16150ae55423SMike Smith     }
16163c600cfbSJohn Baldwin     freeenv(env);
16170ae55423SMike Smith     return(0);
16180ae55423SMike Smith }
16190ae55423SMike Smith 
16200ae55423SMike Smith /*
16210ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
16220ae55423SMike Smith  */
16230ae55423SMike Smith int
16240ae55423SMike Smith acpi_disabled(char *subsys)
16250ae55423SMike Smith {
162678689b15SMaxime Henrion     char	*cp, *env;
16270ae55423SMike Smith     int		len;
16280ae55423SMike Smith 
162978689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
16300ae55423SMike Smith 	return(0);
163178689b15SMaxime Henrion     if (!strcmp(env, "all")) {
163278689b15SMaxime Henrion 	freeenv(env);
16330ae55423SMike Smith 	return(1);
1634d786139cSMaxime Henrion     }
16350ae55423SMike Smith 
16360ae55423SMike Smith     /* scan the disable list checking for a match */
163778689b15SMaxime Henrion     cp = env;
16380ae55423SMike Smith     for (;;) {
16390ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
16400ae55423SMike Smith 	    cp++;
16410ae55423SMike Smith 	if (*cp == 0)
16420ae55423SMike Smith 	    break;
16430ae55423SMike Smith 	len = 0;
16440ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
16450ae55423SMike Smith 	    len++;
1646d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
164778689b15SMaxime Henrion 	    freeenv(env);
164815e32d5dSMike Smith 	    return(1);
1649d786139cSMaxime Henrion 	}
165015e32d5dSMike Smith 	cp += len;
165115e32d5dSMike Smith     }
165278689b15SMaxime Henrion     freeenv(env);
165315e32d5dSMike Smith     return(0);
165415e32d5dSMike Smith }
165515e32d5dSMike Smith 
165615e32d5dSMike Smith /*
1657a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1658a1fccb47SMitsuru IWASAKI  */
1659a1fccb47SMitsuru IWASAKI void
1660a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1661a1fccb47SMitsuru IWASAKI {
1662a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1663a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1664a1fccb47SMitsuru IWASAKI 
1665a1fccb47SMitsuru IWASAKI     /*
1666a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1667a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1668a1fccb47SMitsuru IWASAKI      */
1669a1fccb47SMitsuru IWASAKI 
1670a1fccb47SMitsuru IWASAKI     /*
1671a1fccb47SMitsuru IWASAKI      * enable/disable device wake function.
1672a1fccb47SMitsuru IWASAKI      */
1673a1fccb47SMitsuru IWASAKI 
1674a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1675a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1676a1fccb47SMitsuru IWASAKI 
1677a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1678a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1679a1fccb47SMitsuru IWASAKI 
1680a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1681a1fccb47SMitsuru IWASAKI }
1682a1fccb47SMitsuru IWASAKI 
1683a1fccb47SMitsuru IWASAKI void
1684a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1685a1fccb47SMitsuru IWASAKI {
1686a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1687a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1688a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1689a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1690a1fccb47SMitsuru IWASAKI 
1691a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1692a1fccb47SMitsuru IWASAKI 
1693a1fccb47SMitsuru IWASAKI     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
1694a1fccb47SMitsuru IWASAKI 	return;
1695a1fccb47SMitsuru IWASAKI     }
1696a1fccb47SMitsuru IWASAKI 
1697a1fccb47SMitsuru IWASAKI     /*
1698a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1699a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1700a1fccb47SMitsuru IWASAKI      */
1701a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1702a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1703a1fccb47SMitsuru IWASAKI     if (ACPI_FAILURE(status)) {
1704a1fccb47SMitsuru IWASAKI 	return;
1705a1fccb47SMitsuru IWASAKI     }
1706a1fccb47SMitsuru IWASAKI 
1707a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1708a1fccb47SMitsuru IWASAKI     if (res == NULL) {
1709a1fccb47SMitsuru IWASAKI 	return;
1710a1fccb47SMitsuru IWASAKI     }
1711a1fccb47SMitsuru IWASAKI 
1712a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1713a1fccb47SMitsuru IWASAKI 	goto out;
1714a1fccb47SMitsuru IWASAKI     }
1715a1fccb47SMitsuru IWASAKI 
1716a1fccb47SMitsuru IWASAKI     /*
1717a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1718a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1719a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1720a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1721a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1722a1fccb47SMitsuru IWASAKI      */
1723a1fccb47SMitsuru IWASAKI     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
1724a1fccb47SMitsuru IWASAKI 	goto out;
1725a1fccb47SMitsuru IWASAKI     }
1726a1fccb47SMitsuru IWASAKI 
1727a1fccb47SMitsuru IWASAKI     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
1728a1fccb47SMitsuru IWASAKI 	goto out;
1729a1fccb47SMitsuru IWASAKI     }
1730a1fccb47SMitsuru IWASAKI 
1731a1fccb47SMitsuru IWASAKI     /*
1732a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1733a1fccb47SMitsuru IWASAKI      */
1734a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1735a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1736a1fccb47SMitsuru IWASAKI 	/*
1737a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1738a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1739a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1740a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1741a1fccb47SMitsuru IWASAKI 	 */
1742a1fccb47SMitsuru IWASAKI 
1743a1fccb47SMitsuru IWASAKI 	status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value,
1744a1fccb47SMitsuru IWASAKI 				 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE);
1745a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1746a1fccb47SMitsuru IWASAKI             printf("%s: EnableEvent Failed\n", __func__);
1747a1fccb47SMitsuru IWASAKI 	break;
1748a1fccb47SMitsuru IWASAKI 
1749a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1750a1fccb47SMitsuru IWASAKI 	/* XXX TBD */
1751a1fccb47SMitsuru IWASAKI 
1752a1fccb47SMitsuru IWASAKI 	/*
1753a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1754a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1755a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1756a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1757a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1758a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1759a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1760a1fccb47SMitsuru IWASAKI 	 * the wake event.
1761a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1762a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1763a1fccb47SMitsuru IWASAKI 	 */
1764a1fccb47SMitsuru IWASAKI 
1765a1fccb47SMitsuru IWASAKI 	break;
1766a1fccb47SMitsuru IWASAKI 
1767a1fccb47SMitsuru IWASAKI     default:
1768a1fccb47SMitsuru IWASAKI 	break;
1769a1fccb47SMitsuru IWASAKI     }
1770a1fccb47SMitsuru IWASAKI 
1771a1fccb47SMitsuru IWASAKI out:
1772a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1773a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1774a1fccb47SMitsuru IWASAKI     return;
1775a1fccb47SMitsuru IWASAKI }
1776a1fccb47SMitsuru IWASAKI 
1777a1fccb47SMitsuru IWASAKI /*
177815e32d5dSMike Smith  * Control interface.
177915e32d5dSMike Smith  *
17800ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
17810ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
17820ae55423SMike Smith  * interface to make their handlers visible.
178315e32d5dSMike Smith  */
17840ae55423SMike Smith struct acpi_ioctl_hook
17850ae55423SMike Smith {
17860ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
17870ae55423SMike Smith     u_long				cmd;
17880ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
17890ae55423SMike Smith     void				*arg;
17900ae55423SMike Smith };
17910ae55423SMike Smith 
17920ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
17930ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
17940ae55423SMike Smith 
17950ae55423SMike Smith /*
17960ae55423SMike Smith  * Register an ioctl handler.
17970ae55423SMike Smith  */
17980ae55423SMike Smith int
17990ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
18000ae55423SMike Smith {
18010ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18020ae55423SMike Smith 
18030ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
18040ae55423SMike Smith 	return(ENOMEM);
18050ae55423SMike Smith     hp->cmd = cmd;
18060ae55423SMike Smith     hp->fn = fn;
18070ae55423SMike Smith     hp->arg = arg;
18080ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
18090ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
18100ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
18110ae55423SMike Smith     }
18120ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
18130ae55423SMike Smith     return(0);
18140ae55423SMike Smith }
18150ae55423SMike Smith 
18160ae55423SMike Smith /*
18170ae55423SMike Smith  * Deregister an ioctl handler.
18180ae55423SMike Smith  */
18190ae55423SMike Smith void
18200ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
18210ae55423SMike Smith {
18220ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18230ae55423SMike Smith 
18240ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
18250ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
18260ae55423SMike Smith 	    break;
18270ae55423SMike Smith 
18280ae55423SMike Smith     if (hp != NULL) {
18290ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
18300ae55423SMike Smith 	free(hp, M_ACPIDEV);
18310ae55423SMike Smith     }
18320ae55423SMike Smith }
18330ae55423SMike Smith 
183415e32d5dSMike Smith static int
18356b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
183615e32d5dSMike Smith {
183715e32d5dSMike Smith     return(0);
183815e32d5dSMike Smith }
183915e32d5dSMike Smith 
184015e32d5dSMike Smith static int
18416b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
184215e32d5dSMike Smith {
184315e32d5dSMike Smith     return(0);
184415e32d5dSMike Smith }
184515e32d5dSMike Smith 
184615e32d5dSMike Smith static int
18476b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
184815e32d5dSMike Smith {
184915e32d5dSMike Smith     struct acpi_softc		*sc;
18500ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18510ae55423SMike Smith     int				error, xerror, state;
1852fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
185315e32d5dSMike Smith 
1854cb9b0d80SMike Smith     ACPI_LOCK;
1855cb9b0d80SMike Smith 
185615e32d5dSMike Smith     error = state = 0;
185715e32d5dSMike Smith     sc = dev->si_drv1;
185815e32d5dSMike Smith 
18590ae55423SMike Smith     /*
18600ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
18610ae55423SMike Smith      */
18620ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
18630ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
18640ae55423SMike Smith 	    if (hp->cmd == cmd) {
18650ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
18660ae55423SMike Smith 		if (xerror != 0)
18670ae55423SMike Smith 		    error = xerror;
1868917d44c8SMitsuru IWASAKI 		goto out;
18690ae55423SMike Smith 	    }
18700ae55423SMike Smith 	}
18710ae55423SMike Smith     }
18720ae55423SMike Smith 
18730ae55423SMike Smith     /*
1874a89fcf28STakanori Watanabe      * Core ioctls are  not permitted for non-writable user.
1875a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
1876a89fcf28STakanori Watanabe      * Not changing system behavior.
1877a89fcf28STakanori Watanabe      */
1878a89fcf28STakanori Watanabe     if(!(flag & FWRITE)){
1879a89fcf28STakanori Watanabe 	    return EPERM;
1880a89fcf28STakanori Watanabe     }
1881a89fcf28STakanori Watanabe 
1882a89fcf28STakanori Watanabe     /*
18830ae55423SMike Smith      * Core system ioctls.
18840ae55423SMike Smith      */
188515e32d5dSMike Smith     switch (cmd) {
188615e32d5dSMike Smith     case ACPIIO_ENABLE:
18870ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
188815e32d5dSMike Smith 	    error = ENXIO;
188915e32d5dSMike Smith 	break;
189015e32d5dSMike Smith 
189115e32d5dSMike Smith     case ACPIIO_DISABLE:
18920ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
189315e32d5dSMike Smith 	    error = ENXIO;
189415e32d5dSMike Smith 	break;
189515e32d5dSMike Smith 
189615e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
189715e32d5dSMike Smith 	if (!sc->acpi_enabled) {
189815e32d5dSMike Smith 	    error = ENXIO;
189915e32d5dSMike Smith 	    break;
190015e32d5dSMike Smith 	}
190115e32d5dSMike Smith 	state = *(int *)addr;
1902a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
190315e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
190415e32d5dSMike Smith 	} else {
190515e32d5dSMike Smith 	    error = EINVAL;
190615e32d5dSMike Smith 	}
190715e32d5dSMike Smith 	break;
190815e32d5dSMike Smith 
190915e32d5dSMike Smith     default:
19100ae55423SMike Smith 	if (error == 0)
191115e32d5dSMike Smith 	    error = EINVAL;
191215e32d5dSMike Smith 	break;
191315e32d5dSMike Smith     }
1914917d44c8SMitsuru IWASAKI 
1915917d44c8SMitsuru IWASAKI out:
1916cb9b0d80SMike Smith     ACPI_UNLOCK;
191715e32d5dSMike Smith     return(error);
191815e32d5dSMike Smith }
191915e32d5dSMike Smith 
19201d073b1dSJohn Baldwin static int
1921d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
1922d75de536SMitsuru IWASAKI {
1923d75de536SMitsuru IWASAKI     char sleep_state[4];
1924d75de536SMitsuru IWASAKI     char buf[16];
1925d75de536SMitsuru IWASAKI     int error;
1926d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
1927d75de536SMitsuru IWASAKI 
1928d75de536SMitsuru IWASAKI     buf[0] = '\0';
1929d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
1930d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
1931d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
1932d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
1933d75de536SMitsuru IWASAKI 	}
1934d75de536SMitsuru IWASAKI     }
1935d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1936d75de536SMitsuru IWASAKI     return(error);
1937d75de536SMitsuru IWASAKI }
1938d75de536SMitsuru IWASAKI 
1939d75de536SMitsuru IWASAKI static int
19401d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
19411d073b1dSJohn Baldwin {
19421d073b1dSJohn Baldwin     char sleep_state[10];
19431d073b1dSJohn Baldwin     int error;
19441d073b1dSJohn Baldwin     u_int new_state, old_state;
19451d073b1dSJohn Baldwin 
19461d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1947b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
19481d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1949cb9b0d80SMike Smith     } else {
1950b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
19511d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
19521d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1953cb9b0d80SMike Smith     }
19541d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
19551d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1956b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
19571d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
19581d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
19591d073b1dSJohn Baldwin 		break;
1960cb9b0d80SMike Smith 	}
1961931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX+1) {
1962931a10c9SMitsuru IWASAKI 	    if (new_state != old_state) {
19631d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
1964931a10c9SMitsuru IWASAKI 	    }
1965cb9b0d80SMike Smith 	} else {
19661d073b1dSJohn Baldwin 	    error = EINVAL;
19671d073b1dSJohn Baldwin 	}
1968cb9b0d80SMike Smith     }
19691d073b1dSJohn Baldwin     return(error);
19701d073b1dSJohn Baldwin }
19711d073b1dSJohn Baldwin 
197215e32d5dSMike Smith #ifdef ACPI_DEBUG
19730ae55423SMike Smith /*
19740ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
19750ae55423SMike Smith  *
19760ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
19770ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
19780ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
19790ae55423SMike Smith  * prefixing the bit name with !.
19800ae55423SMike Smith  */
198115e32d5dSMike Smith struct debugtag
198215e32d5dSMike Smith {
198315e32d5dSMike Smith     char	*name;
198415e32d5dSMike Smith     UINT32	value;
198515e32d5dSMike Smith };
198615e32d5dSMike Smith 
198715e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1988c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1989c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1990c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1991c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1992c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1993c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1994c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1995c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1996c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
1997d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
19984c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
1999d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
20004c1cdee6SMike Smith 
2001cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
20024c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2003cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2004cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2005c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2006c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2007c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
20084c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2009cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
20104c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
20114c1cdee6SMike Smith 
2012b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
2013c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
201415e32d5dSMike Smith     {NULL, 0}
201515e32d5dSMike Smith };
201615e32d5dSMike Smith 
201715e32d5dSMike Smith static struct debugtag dbg_level[] = {
20184c1cdee6SMike Smith     {"ACPI_LV_OK",		ACPI_LV_OK},
20194c1cdee6SMike Smith     {"ACPI_LV_INFO",		ACPI_LV_INFO},
20204c1cdee6SMike Smith     {"ACPI_LV_WARN",		ACPI_LV_WARN},
20214c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
20224c1cdee6SMike Smith     {"ACPI_LV_FATAL",		ACPI_LV_FATAL},
20234c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
20244c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2025d62ab2f4SMitsuru IWASAKI 
2026d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
20274c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
20284c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2029d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
20304c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
20314c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
20324c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
20334c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
20344c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
20354c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
20364c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
20374c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
20384c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
20394c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
20404c1cdee6SMike Smith     {"ACPI_LV_INIT",		ACPI_LV_INIT},
2041d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2042d62ab2f4SMitsuru IWASAKI 
2043d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2044d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2045d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2046d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2047d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
20484c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2049d62ab2f4SMitsuru IWASAKI 
2050d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2051d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2052d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2053d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2054d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2055d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2056d62ab2f4SMitsuru IWASAKI 
2057d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
205898479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
205998479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
206098479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
206198479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
206298479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
206315e32d5dSMike Smith     {NULL, 0}
206415e32d5dSMike Smith };
206515e32d5dSMike Smith 
206615e32d5dSMike Smith static void
206715e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
206815e32d5dSMike Smith {
206915e32d5dSMike Smith     char	*ep;
207015e32d5dSMike Smith     int		i, l;
20710ae55423SMike Smith     int		set;
207215e32d5dSMike Smith 
207315e32d5dSMike Smith     while (*cp) {
207415e32d5dSMike Smith 	if (isspace(*cp)) {
207515e32d5dSMike Smith 	    cp++;
207615e32d5dSMike Smith 	    continue;
207715e32d5dSMike Smith 	}
207815e32d5dSMike Smith 	ep = cp;
207915e32d5dSMike Smith 	while (*ep && !isspace(*ep))
208015e32d5dSMike Smith 	    ep++;
20810ae55423SMike Smith 	if (*cp == '!') {
20820ae55423SMike Smith 	    set = 0;
20830ae55423SMike Smith 	    cp++;
20840ae55423SMike Smith 	    if (cp == ep)
20850ae55423SMike Smith 		continue;
20860ae55423SMike Smith 	} else {
20870ae55423SMike Smith 	    set = 1;
20880ae55423SMike Smith 	}
208915e32d5dSMike Smith 	l = ep - cp;
209015e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
209115e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
20920ae55423SMike Smith 		if (set) {
209315e32d5dSMike Smith 		    *flag |= tag[i].value;
20940ae55423SMike Smith 		} else {
20950ae55423SMike Smith 		    *flag &= ~tag[i].value;
20960ae55423SMike Smith 		}
209715e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
209815e32d5dSMike Smith 	    }
209915e32d5dSMike Smith 	}
210015e32d5dSMike Smith 	cp = ep;
210115e32d5dSMike Smith     }
210215e32d5dSMike Smith }
210315e32d5dSMike Smith 
210415e32d5dSMike Smith static void
21052a4ac806SMike Smith acpi_set_debugging(void *junk)
210615e32d5dSMike Smith {
210794aae282SMike Barcroft     char	*cp;
210815e32d5dSMike Smith 
210987b45ed5SMitsuru IWASAKI     if (!cold)
211087b45ed5SMitsuru IWASAKI 	return;
211187b45ed5SMitsuru IWASAKI 
21120ae55423SMike Smith     AcpiDbgLayer = 0;
21130ae55423SMike Smith     AcpiDbgLevel = 0;
211494aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
211515e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
211694aae282SMike Barcroft 	freeenv(cp);
211794aae282SMike Barcroft     }
211894aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
211915e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
212094aae282SMike Barcroft 	freeenv(cp);
212194aae282SMike Barcroft     }
21220ae55423SMike Smith 
21230ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
212415e32d5dSMike Smith }
21252a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
212615e32d5dSMike Smith #endif
2127f9390180SMitsuru IWASAKI 
2128f9390180SMitsuru IWASAKI static int
2129f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2130f9390180SMitsuru IWASAKI {
2131f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2132f9390180SMitsuru IWASAKI 	int	error;
2133f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2134f9390180SMitsuru IWASAKI 	va_list	ap;
2135f9390180SMitsuru IWASAKI 
2136f9390180SMitsuru IWASAKI 	error = 0;
2137f9390180SMitsuru IWASAKI 	switch (cmd) {
2138f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2139f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2140f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2141f9390180SMitsuru IWASAKI 			error = EINVAL;
2142f9390180SMitsuru IWASAKI 			goto out;
2143f9390180SMitsuru IWASAKI 		}
2144f9390180SMitsuru IWASAKI 
2145f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2146f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2147f9390180SMitsuru IWASAKI 		va_end(ap);
2148f9390180SMitsuru IWASAKI 
2149f9390180SMitsuru IWASAKI 		switch (state) {
2150f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2151f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2152f9390180SMitsuru IWASAKI 			break;
2153f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2154f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2155f9390180SMitsuru IWASAKI 			break;
2156f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2157f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2158f9390180SMitsuru IWASAKI 			break;
2159f9390180SMitsuru IWASAKI 		default:
2160f9390180SMitsuru IWASAKI 			error = EINVAL;
2161f9390180SMitsuru IWASAKI 			goto out;
2162f9390180SMitsuru IWASAKI 		}
2163f9390180SMitsuru IWASAKI 
2164f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2165f9390180SMitsuru IWASAKI 		break;
2166f9390180SMitsuru IWASAKI 
2167f9390180SMitsuru IWASAKI 	default:
2168f9390180SMitsuru IWASAKI 		error = EINVAL;
2169f9390180SMitsuru IWASAKI 		goto out;
2170f9390180SMitsuru IWASAKI 	}
2171f9390180SMitsuru IWASAKI 
2172f9390180SMitsuru IWASAKI out:
2173f9390180SMitsuru IWASAKI 	return (error);
2174f9390180SMitsuru IWASAKI }
2175f9390180SMitsuru IWASAKI 
2176f9390180SMitsuru IWASAKI static void
2177f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2178f9390180SMitsuru IWASAKI {
21796c407052SMitsuru IWASAKI     int	error;
21806c407052SMitsuru IWASAKI 
218187b45ed5SMitsuru IWASAKI     if (!cold)
218287b45ed5SMitsuru IWASAKI 	return;
218387b45ed5SMitsuru IWASAKI 
21846c407052SMitsuru IWASAKI     if (!resource_int_value("acpi", 0, "disabled", &error) &&
21856c407052SMitsuru IWASAKI        (error != 0))
21866c407052SMitsuru IWASAKI 		return;
2187f9390180SMitsuru IWASAKI 
2188f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2189f9390180SMitsuru IWASAKI }
2190f9390180SMitsuru IWASAKI 
2191f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2192f9390180SMitsuru IWASAKI 
2193