xref: /freebsd/sys/dev/acpica/acpi.c (revision 9b937d4836af13ea4e86868b9bd4e1cc09dee732)
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>
49bc0f2195SMike Smith #include <isa/isavar.h>
50bc0f2195SMike Smith 
5115e32d5dSMike Smith #include "acpi.h"
5215e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5315e32d5dSMike Smith #include <dev/acpica/acpiio.h>
549b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
5515e32d5dSMike Smith 
5615e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
5715e32d5dSMike Smith 
58be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
59cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
60b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
610ae55423SMike Smith 
6215e32d5dSMike Smith static d_open_t		acpiopen;
6315e32d5dSMike Smith static d_close_t	acpiclose;
6415e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6515e32d5dSMike Smith 
6615e32d5dSMike Smith #define CDEV_MAJOR 152
6715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
687ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
697ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
707ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
717ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
727ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
7315e32d5dSMike Smith };
7415e32d5dSMike Smith 
751d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
76b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
771d073b1dSJohn Baldwin 
782a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
792a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
802a4ac806SMike Smith 
81fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
82cb9b0d80SMike Smith struct mtx	acpi_mutex;
83fc0ea94aSJohn Baldwin #endif
84cb9b0d80SMike Smith 
856d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
8615e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
8715e32d5dSMike Smith static int	acpi_probe(device_t dev);
8815e32d5dSMike Smith static int	acpi_attach(device_t dev);
89be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
90be2b1797SNate Lawson 			int unit);
9115e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
92be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
93be2b1797SNate Lawson 			uintptr_t *result);
94be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
95be2b1797SNate Lawson 			uintptr_t value);
96be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
97be2b1797SNate Lawson 			int rid, u_long start, u_long count);
98be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
99be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
100be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
101be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
102be2b1797SNate Lawson 			u_long count, u_int flags);
103be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
104be2b1797SNate Lawson 			int rid, struct resource *r);
10532d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
106b2566207STakanori Watanabe static u_int32_t acpi_isa_get_compatid(device_t dev);
107be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
108be2b1797SNate Lawson 			struct isa_pnp_id *ids);
10915e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
110be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
111be2b1797SNate Lawson 			void *context, void **status);
11215e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11315e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11413d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
11515e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
11615e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
117d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1181d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
119f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
120f9390180SMitsuru IWASAKI 
12115e32d5dSMike Smith static device_method_t acpi_methods[] = {
12215e32d5dSMike Smith     /* Device interface */
12315e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
12415e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
12515e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
12615e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
12715e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
12815e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
12915e32d5dSMike Smith 
13015e32d5dSMike Smith     /* Bus interface */
13115e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13215e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
13315e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
13415e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
13515e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
13615e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
13715e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
13815e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
13915e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14015e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14115e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14215e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14315e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
14415e32d5dSMike Smith 
145bc0f2195SMike Smith     /* ISA emulation */
146bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
147bc0f2195SMike Smith 
14815e32d5dSMike Smith     {0, 0}
14915e32d5dSMike Smith };
15015e32d5dSMike Smith 
15115e32d5dSMike Smith static driver_t acpi_driver = {
15215e32d5dSMike Smith     "acpi",
15315e32d5dSMike Smith     acpi_methods,
15415e32d5dSMike Smith     sizeof(struct acpi_softc),
15515e32d5dSMike Smith };
15615e32d5dSMike Smith 
1573273b005SMike Smith static devclass_t acpi_devclass;
1586d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
159dde24897SMike Smith MODULE_VERSION(acpi, 100);
16015e32d5dSMike Smith 
1611d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1621d7b121cSNate Lawson static char acpi_ca_version[12];
1631d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1641d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
16515e32d5dSMike Smith 
16615e32d5dSMike Smith /*
1676d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1686d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
169be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
170be2b1797SNate Lawson  * off it.
1716d63101aSMike Smith  */
1726d63101aSMike Smith static int
1736d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1746d63101aSMike Smith {
1756d63101aSMike Smith     switch(event) {
1766d63101aSMike Smith     case MOD_LOAD:
17787b45ed5SMitsuru IWASAKI 	if (!cold) {
17887b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
1796d63101aSMike Smith 	    return (EPERM);
18087b45ed5SMitsuru IWASAKI 	}
1816d63101aSMike Smith 	break;
1826d63101aSMike Smith     case MOD_UNLOAD:
183f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1846d63101aSMike Smith 	    return (EBUSY);
185f9390180SMitsuru IWASAKI 	break;
1866d63101aSMike Smith     default:
1876d63101aSMike Smith 	break;
1886d63101aSMike Smith     }
1896d63101aSMike Smith     return (0);
1906d63101aSMike Smith }
1916d63101aSMike Smith 
1926d63101aSMike Smith /*
19315e32d5dSMike Smith  * Detect ACPI, perform early initialisation
19415e32d5dSMike Smith  */
19515e32d5dSMike Smith static void
19615e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
19715e32d5dSMike Smith {
19815e32d5dSMike Smith     device_t	child;
19915e32d5dSMike Smith     int		error;
200d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
201d786139cSMaxime Henrion     char	*debugpoint;
20215e32d5dSMike Smith #endif
20315e32d5dSMike Smith 
204b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2050ae55423SMike Smith 
20687b45ed5SMitsuru IWASAKI     if (!cold)
20787b45ed5SMitsuru IWASAKI 	return_VOID;
208840f9b53STakanori Watanabe 
209be2b1797SNate Lawson     /* Check that we haven't been disabled with a hint. */
2108a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21132d18aa5SMike Smith 	return_VOID;
21232d18aa5SMike Smith 
2131d7b121cSNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "0x%x",
2141d7b121cSNate Lawson 	     ACPI_CA_VERSION);
2151d7b121cSNate Lawson 
216be2b1797SNate Lawson     /* Make sure we're not being doubly invoked. */
21715e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2180ae55423SMike Smith 	return_VOID;
21915e32d5dSMike Smith 
220fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
221be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2226008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
223fc0ea94aSJohn Baldwin #endif
224cb9b0d80SMike Smith 
225be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
226d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
227d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
228d786139cSMaxime Henrion     if (debugpoint) {
229d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
23015e32d5dSMike Smith 	    acpi_EnterDebugger();
231d786139cSMaxime Henrion 	freeenv(debugpoint);
232d786139cSMaxime Henrion     }
23315e32d5dSMike Smith #endif
234b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
235bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2360ae55423SMike Smith 	return_VOID;
23715e32d5dSMike Smith     }
238d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
239d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
240d786139cSMaxime Henrion     if (debugpoint) {
241d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
24215e32d5dSMike Smith 	    acpi_EnterDebugger();
243d786139cSMaxime Henrion 	freeenv(debugpoint);
244d786139cSMaxime Henrion     }
24515e32d5dSMike Smith #endif
2461611ea87SMitsuru IWASAKI 
247b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
248bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2490ae55423SMike Smith 	return_VOID;
25015e32d5dSMike Smith     }
25115e32d5dSMike Smith 
252be2b1797SNate Lawson     /* Attach the actual ACPI device. */
25315e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
25415e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
2550ae55423SMike Smith 	return_VOID;
25615e32d5dSMike Smith     }
25715e32d5dSMike Smith }
25815e32d5dSMike Smith 
25915e32d5dSMike Smith /*
26015e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
26115e32d5dSMike Smith  */
26215e32d5dSMike Smith static int
26315e32d5dSMike Smith acpi_probe(device_t dev)
26415e32d5dSMike Smith {
26515e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
26615e32d5dSMike Smith     char		buf[20];
267cb9b0d80SMike Smith     ACPI_STATUS		status;
26815e32d5dSMike Smith     int			error;
269fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
27015e32d5dSMike Smith 
271b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
272f9390180SMitsuru IWASAKI 
273f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
274f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
275be2b1797SNate Lawson 
276f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
277f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
278f9390180SMitsuru IWASAKI     }
279f9390180SMitsuru IWASAKI 
280cb9b0d80SMike Smith     ACPI_LOCK;
2810ae55423SMike Smith 
282b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
283be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
284be2b1797SNate Lawson 		      AcpiFormatException(status));
285cb9b0d80SMike Smith 	error = ENXIO;
286cb9b0d80SMike Smith     } else {
28715e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
28815e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
289cb9b0d80SMike Smith 	error = 0;
290cb9b0d80SMike Smith     }
291cb9b0d80SMike Smith     ACPI_UNLOCK;
292cb9b0d80SMike Smith     return_VALUE(error);
29315e32d5dSMike Smith }
29415e32d5dSMike Smith 
29515e32d5dSMike Smith static int
29615e32d5dSMike Smith acpi_attach(device_t dev)
29715e32d5dSMike Smith {
29815e32d5dSMike Smith     struct acpi_softc	*sc;
299cb9b0d80SMike Smith     ACPI_STATUS		status;
30015e32d5dSMike Smith     int			error;
30132d18aa5SMike Smith     UINT32		flags;
302498d464fSMitsuru IWASAKI     char		*env;
303d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
304d786139cSMaxime Henrion     char		*debugpoint;
30515e32d5dSMike Smith #endif
306fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
30715e32d5dSMike Smith 
308b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
309cb9b0d80SMike Smith     ACPI_LOCK;
31015e32d5dSMike Smith     sc = device_get_softc(dev);
31115e32d5dSMike Smith     bzero(sc, sizeof(*sc));
31215e32d5dSMike Smith     sc->acpi_dev = dev;
31315e32d5dSMike Smith 
314d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
315d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
316d786139cSMaxime Henrion     if (debugpoint) {
317d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
31815e32d5dSMike Smith 	    acpi_EnterDebugger();
319d786139cSMaxime Henrion 	freeenv(debugpoint);
320d786139cSMaxime Henrion     }
32115e32d5dSMike Smith #endif
32215e32d5dSMike Smith 
323be2b1797SNate Lawson     /* Install the default address space handlers. */
324cb9b0d80SMike Smith     error = ENXIO;
325be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
326be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
327be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
328be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
329be2b1797SNate Lawson 		      AcpiFormatException(status));
330cb9b0d80SMike Smith 	goto out;
33115e32d5dSMike Smith     }
332be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
333be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
334be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
335be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
336be2b1797SNate Lawson 		      AcpiFormatException(status));
337cb9b0d80SMike Smith 	goto out;
33815e32d5dSMike Smith     }
339be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
340be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
341be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
342be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
343be2b1797SNate Lawson 		      AcpiFormatException(status));
344cb9b0d80SMike Smith 	goto out;
34515e32d5dSMike Smith     }
34615e32d5dSMike Smith 
34715e32d5dSMike Smith     /*
34815e32d5dSMike Smith      * Bring ACPI fully online.
34915e32d5dSMike Smith      *
350be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
351be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
352be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
353be2b1797SNate Lawson      * object init pass.
35415e32d5dSMike Smith      *
35532d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
35632d18aa5SMike Smith      *
357be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
358be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
35915e32d5dSMike Smith      */
360d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
361d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
362d786139cSMaxime Henrion     if (debugpoint) {
363d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
36415e32d5dSMike Smith 	    acpi_EnterDebugger();
365d786139cSMaxime Henrion 	freeenv(debugpoint);
366d786139cSMaxime Henrion     }
36715e32d5dSMike Smith #endif
36832d18aa5SMike Smith     flags = 0;
369d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
37032d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
371b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
372be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
373be2b1797SNate Lawson 		      AcpiFormatException(status));
374cb9b0d80SMike Smith 	goto out;
37515e32d5dSMike Smith     }
37615e32d5dSMike Smith 
377f8335e3aSNate Lawson     /*
378f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
379f8335e3aSNate Lawson      * the namespace has been evaluated.
380f8335e3aSNate Lawson      */
381f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
382f8335e3aSNate Lawson 
383b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
384be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
385be2b1797SNate Lawson 		      AcpiFormatException(status));
386b69ed3f4SMitsuru IWASAKI 	goto out;
387b69ed3f4SMitsuru IWASAKI     }
388b69ed3f4SMitsuru IWASAKI 
38915e32d5dSMike Smith     /*
3901d073b1dSJohn Baldwin      * Setup our sysctl tree.
3911d073b1dSJohn Baldwin      *
3921d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3931d073b1dSJohn Baldwin      */
3941d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3951d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3961d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3971d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3981d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
399d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
400d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
401d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4021d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4031d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4041d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4051d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4061d073b1dSJohn Baldwin 	&sc->acpi_sleep_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, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4091d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
410f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
411f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
412f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_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, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
415f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4162d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
417ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
418ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
419ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4201611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4211611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4221611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4232d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4242d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
425c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
426c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
427c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
428bf0c18ecSNate Lawson 
429bf0c18ecSNate Lawson     /*
430bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
431bf0c18ecSNate Lawson      * stabilize.
432bf0c18ecSNate Lawson      */
433bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
434c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
4352d644607SMitsuru IWASAKI     if (bootverbose)
4362d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
437498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
438498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
439498d464fSMitsuru IWASAKI 	freeenv(env);
440498d464fSMitsuru IWASAKI     }
4411d073b1dSJohn Baldwin 
4422d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
4432d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
4442d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
4452d610c46SNate Lawson 
4461d073b1dSJohn Baldwin     /*
44715e32d5dSMike Smith      * Dispatch the default sleep state to devices.
44815e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
44915e32d5dSMike Smith      */
45015e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
45115e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
45215e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
453f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
454f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
45515e32d5dSMike Smith 
45613d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
45715e32d5dSMike Smith 
45815e32d5dSMike Smith     /*
45915e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
46015e32d5dSMike Smith      */
461d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
462d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
463d786139cSMaxime Henrion     if (debugpoint) {
464d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
46515e32d5dSMike Smith 	    acpi_EnterDebugger();
466d786139cSMaxime Henrion 	freeenv(debugpoint);
467d786139cSMaxime Henrion     }
46815e32d5dSMike Smith #endif
46915e32d5dSMike Smith 
470be2b1797SNate Lawson     /* Register our shutdown handlers */
471be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
472be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
473be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
474be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
47515e32d5dSMike Smith 
47615e32d5dSMike Smith     /*
47715e32d5dSMike Smith      * Register our acpi event handlers.
47815e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
47915e32d5dSMike Smith      */
480be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
481be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
482be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
483be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
48415e32d5dSMike Smith 
485be2b1797SNate Lawson     /* Flag our initial states. */
48615e32d5dSMike Smith     sc->acpi_enabled = 1;
48715e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
488ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
48915e32d5dSMike Smith 
490be2b1797SNate Lawson     /* Create the control device */
491a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4922a4689e8SRobert Watson 			      "acpi");
49315e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
49415e32d5dSMike Smith 
495d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
496d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
497d786139cSMaxime Henrion     if (debugpoint) {
498be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
49915e32d5dSMike Smith 	    acpi_EnterDebugger();
500d786139cSMaxime Henrion 	freeenv(debugpoint);
501d786139cSMaxime Henrion     }
50215e32d5dSMike Smith #endif
503f86214b6SMitsuru IWASAKI 
50491da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
505be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
506c573e654SMitsuru IWASAKI 	goto out;
507c573e654SMitsuru IWASAKI #endif
508c573e654SMitsuru IWASAKI 
509be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
510f86214b6SMitsuru IWASAKI 	goto out;
511f86214b6SMitsuru IWASAKI 
512f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
513f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
514f9390180SMitsuru IWASAKI 
515eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
516eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
517eeb6dba2SJohn Baldwin 
518cb9b0d80SMike Smith     error = 0;
519cb9b0d80SMike Smith 
520cb9b0d80SMike Smith  out:
521cb9b0d80SMike Smith     ACPI_UNLOCK;
522cb9b0d80SMike Smith     return_VALUE (error);
52315e32d5dSMike Smith }
52415e32d5dSMike Smith 
52515e32d5dSMike Smith /*
52615e32d5dSMike Smith  * Handle a new device being added
52715e32d5dSMike Smith  */
52815e32d5dSMike Smith static device_t
52915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
53015e32d5dSMike Smith {
53115e32d5dSMike Smith     struct acpi_device	*ad;
53215e32d5dSMike Smith     device_t		child;
53315e32d5dSMike Smith 
534be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
53515e32d5dSMike Smith 	return (NULL);
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 
576be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
57715e32d5dSMike Smith     switch(index) {
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;
58732d18aa5SMike Smith     case ISA_IVAR_VENDORID:
58832d18aa5SMike Smith     case ISA_IVAR_SERIAL:
58932d18aa5SMike Smith     case ISA_IVAR_COMPATID:
59032d18aa5SMike Smith 	*(int *)result = -1;
59132d18aa5SMike Smith 	break;
59232d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
59332d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59432d18aa5SMike Smith 	break;
59515e32d5dSMike Smith     default:
59615e32d5dSMike Smith 	return (ENOENT);
59715e32d5dSMike Smith     }
598be2b1797SNate Lawson 
59915e32d5dSMike Smith     return (0);
60015e32d5dSMike Smith }
60115e32d5dSMike Smith 
60215e32d5dSMike Smith static int
60315e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
60415e32d5dSMike Smith {
60515e32d5dSMike Smith     struct acpi_device	*ad;
60615e32d5dSMike Smith 
60715e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
60815e32d5dSMike Smith 	printf("device has no ivars\n");
60915e32d5dSMike Smith 	return (ENOENT);
61015e32d5dSMike Smith     }
61115e32d5dSMike Smith 
61215e32d5dSMike Smith     switch(index) {
61315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61415e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
61515e32d5dSMike Smith 	break;
61615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
61715e32d5dSMike Smith 	ad->ad_magic = (int)value;
61815e32d5dSMike Smith 	break;
61915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62015e32d5dSMike Smith 	ad->ad_private = (void *)value;
62115e32d5dSMike Smith 	break;
62215e32d5dSMike Smith     default:
6232ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
62415e32d5dSMike Smith 	return (ENOENT);
62515e32d5dSMike Smith     }
626be2b1797SNate Lawson 
62715e32d5dSMike Smith     return (0);
62815e32d5dSMike Smith }
62915e32d5dSMike Smith 
630be2b1797SNate Lawson ACPI_HANDLE
631be2b1797SNate Lawson acpi_get_handle(device_t dev)
632be2b1797SNate Lawson {
633be2b1797SNate Lawson     uintptr_t up;
634be2b1797SNate Lawson     ACPI_HANDLE	h;
635be2b1797SNate Lawson 
636be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
637be2b1797SNate Lawson 	return(NULL);
638be2b1797SNate Lawson     h = (ACPI_HANDLE)up;
639be2b1797SNate Lawson     return (h);
640be2b1797SNate Lawson }
641be2b1797SNate Lawson 
642be2b1797SNate Lawson int
643be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h)
644be2b1797SNate Lawson {
645be2b1797SNate Lawson     uintptr_t up;
646be2b1797SNate Lawson 
647be2b1797SNate Lawson     up = (uintptr_t)h;
648be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
649be2b1797SNate Lawson }
650be2b1797SNate Lawson 
651be2b1797SNate Lawson int
652be2b1797SNate Lawson acpi_get_magic(device_t dev)
653be2b1797SNate Lawson {
654be2b1797SNate Lawson     uintptr_t up;
655be2b1797SNate Lawson     int	m;
656be2b1797SNate Lawson 
657be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
658be2b1797SNate Lawson 	return(0);
659be2b1797SNate Lawson     m = (int)up;
660be2b1797SNate Lawson     return (m);
661be2b1797SNate Lawson }
662be2b1797SNate Lawson 
663be2b1797SNate Lawson int
664be2b1797SNate Lawson acpi_set_magic(device_t dev, int m)
665be2b1797SNate Lawson {
666be2b1797SNate Lawson     uintptr_t up;
667be2b1797SNate Lawson 
668be2b1797SNate Lawson     up = (uintptr_t)m;
669be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
670be2b1797SNate Lawson }
671be2b1797SNate Lawson 
672be2b1797SNate Lawson void *
673be2b1797SNate Lawson acpi_get_private(device_t dev)
674be2b1797SNate Lawson {
675be2b1797SNate Lawson     uintptr_t up;
676be2b1797SNate Lawson     void *p;
677be2b1797SNate Lawson 
678be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
679be2b1797SNate Lawson 	return (NULL);
680be2b1797SNate Lawson     p = (void *)up;
681be2b1797SNate Lawson     return (p);
682be2b1797SNate Lawson }
683be2b1797SNate Lawson 
684be2b1797SNate Lawson int
685be2b1797SNate Lawson acpi_set_private(device_t dev, void *p)
686be2b1797SNate Lawson {
687be2b1797SNate Lawson     uintptr_t up;
688be2b1797SNate Lawson 
689be2b1797SNate Lawson     up = (uintptr_t)p;
690be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
691be2b1797SNate Lawson }
692be2b1797SNate Lawson 
693be2b1797SNate Lawson ACPI_OBJECT_TYPE
694be2b1797SNate Lawson acpi_get_type(device_t dev)
695be2b1797SNate Lawson {
696be2b1797SNate Lawson     ACPI_HANDLE		h;
697be2b1797SNate Lawson     ACPI_OBJECT_TYPE	t;
698be2b1797SNate Lawson 
699be2b1797SNate Lawson     if ((h = acpi_get_handle(dev)) == NULL)
700be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
701be2b1797SNate Lawson     if (AcpiGetType(h, &t) != AE_OK)
702be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
703be2b1797SNate Lawson     return (t);
704be2b1797SNate Lawson }
705be2b1797SNate Lawson 
70615e32d5dSMike Smith /*
70715e32d5dSMike Smith  * Handle child resource allocation/removal
70815e32d5dSMike Smith  */
70915e32d5dSMike Smith static int
710be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
711be2b1797SNate Lawson 		  u_long start, u_long count)
71215e32d5dSMike Smith {
71315e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
71415e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
71515e32d5dSMike Smith 
71615e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
71715e32d5dSMike Smith 
71815e32d5dSMike Smith     return(0);
71915e32d5dSMike Smith }
72015e32d5dSMike Smith 
72115e32d5dSMike Smith static int
722be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
723be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
72415e32d5dSMike Smith {
72515e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
72615e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
72715e32d5dSMike Smith     struct resource_list_entry	*rle;
72815e32d5dSMike Smith 
72915e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
73015e32d5dSMike Smith     if (!rle)
73115e32d5dSMike Smith 	return(ENOENT);
73215e32d5dSMike Smith 
73315e32d5dSMike Smith     if (startp)
73415e32d5dSMike Smith 	*startp = rle->start;
73515e32d5dSMike Smith     if (countp)
73615e32d5dSMike Smith 	*countp = rle->count;
73715e32d5dSMike Smith 
73815e32d5dSMike Smith     return (0);
73915e32d5dSMike Smith }
74015e32d5dSMike Smith 
74115e32d5dSMike Smith static struct resource *
74215e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
74315e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
74415e32d5dSMike Smith {
74515e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
74615e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
74715e32d5dSMike Smith 
748be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
749be2b1797SNate Lawson 	    flags));
75015e32d5dSMike Smith }
75115e32d5dSMike Smith 
75215e32d5dSMike Smith static int
75315e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
75415e32d5dSMike Smith {
75515e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
75615e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
75715e32d5dSMike Smith 
75815e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
75915e32d5dSMike Smith }
76015e32d5dSMike Smith 
76115e32d5dSMike Smith /*
762bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
763bc0f2195SMike Smith  */
764bc0f2195SMike Smith #define PNP_EISAID(s)				\
765bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
766bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
767bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
768bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
769bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
770bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
771bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
772bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
773bc0f2195SMike Smith 
77432d18aa5SMike Smith static u_int32_t
77532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
776bc0f2195SMike Smith {
777bc0f2195SMike Smith     ACPI_HANDLE		h;
778bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
7796fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
780bc0f2195SMike Smith     ACPI_STATUS		error;
781bc0f2195SMike Smith     u_int32_t		pnpid;
782fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
78332d18aa5SMike Smith 
784b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
78532d18aa5SMike Smith 
78632d18aa5SMike Smith     pnpid = 0;
78732d18aa5SMike Smith     ACPI_LOCK;
78832d18aa5SMike Smith 
7896fca9360SNate Lawson     /* Fetch and validate the HID. */
79032d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
79132d18aa5SMike Smith 	goto out;
7926fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
7936fca9360SNate Lawson     if (ACPI_FAILURE(error))
79432d18aa5SMike Smith 	goto out;
7956fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) == 0)
79632d18aa5SMike Smith 	goto out;
79732d18aa5SMike Smith 
7986fca9360SNate Lawson     pnpid = PNP_EISAID(devinfo.HardwareId.Value);
799be2b1797SNate Lawson 
80032d18aa5SMike Smith out:
80132d18aa5SMike Smith     ACPI_UNLOCK;
80232d18aa5SMike Smith     return_VALUE (pnpid);
80332d18aa5SMike Smith }
80432d18aa5SMike Smith 
805b2566207STakanori Watanabe static u_int32_t
806b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev)
807b2566207STakanori Watanabe {
808b2566207STakanori Watanabe     ACPI_HANDLE		h;
809b2566207STakanori Watanabe     ACPI_STATUS		error;
810b2566207STakanori Watanabe     u_int32_t		pnpid;
811b2566207STakanori Watanabe     ACPI_LOCK_DECL;
812b2566207STakanori Watanabe 
813b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
814b2566207STakanori Watanabe 
815b2566207STakanori Watanabe     pnpid = 0;
816b2566207STakanori Watanabe     ACPI_LOCK;
817b2566207STakanori Watanabe 
818be2b1797SNate Lawson     /* Fetch and validate the HID */
819b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
820b2566207STakanori Watanabe 	goto out;
821b2566207STakanori Watanabe     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
822b2566207STakanori Watanabe 	goto out;
823b2566207STakanori Watanabe 
824b2566207STakanori Watanabe out:
825b2566207STakanori Watanabe     ACPI_UNLOCK;
826b2566207STakanori Watanabe     return_VALUE (pnpid);
827b2566207STakanori Watanabe }
828b2566207STakanori Watanabe 
829b2566207STakanori Watanabe 
83032d18aa5SMike Smith static int
83132d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
83232d18aa5SMike Smith {
833bc0f2195SMike Smith     int			result;
834b2566207STakanori Watanabe     u_int32_t		lid, cid;
835bc0f2195SMike Smith 
836b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
837bc0f2195SMike Smith 
838bc0f2195SMike Smith     /*
839bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
840bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
841bc0f2195SMike Smith      * that to happen, so don't ever return it.
842bc0f2195SMike Smith      */
843bc0f2195SMike Smith     result = ENXIO;
844bc0f2195SMike Smith 
845be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
846b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
847b2566207STakanori Watanabe     cid = acpi_isa_get_compatid(child);
848bc0f2195SMike Smith     while (ids && ids->ip_id) {
849b2566207STakanori Watanabe 	if (lid == ids->ip_id || cid == ids->ip_id) {
850bc0f2195SMike Smith 	    result = 0;
851bc0f2195SMike Smith 	    goto out;
852bc0f2195SMike Smith 	}
853bc0f2195SMike Smith 	ids++;
854bc0f2195SMike Smith     }
855be2b1797SNate Lawson 
856bc0f2195SMike Smith  out:
857bc0f2195SMike Smith     return_VALUE(result);
858bc0f2195SMike Smith }
859bc0f2195SMike Smith 
860bc0f2195SMike Smith /*
86115e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
86215e32d5dSMike Smith  *
863be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
864be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
86515e32d5dSMike Smith  */
86615e32d5dSMike Smith static void
86715e32d5dSMike Smith acpi_probe_children(device_t bus)
86815e32d5dSMike Smith {
86915e32d5dSMike Smith     ACPI_HANDLE	parent;
870be2b1797SNate Lawson     ACPI_STATUS	status;
8717d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
87215e32d5dSMike Smith     int		i;
87315e32d5dSMike Smith 
874b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
875cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
8760ae55423SMike Smith 
877be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
8784c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
87915e32d5dSMike Smith     bus_generic_probe(bus);
88015e32d5dSMike Smith 
88115e32d5dSMike Smith     /*
88215e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
88315e32d5dSMike Smith      * we find.
88415e32d5dSMike Smith      *
88515e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
886be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
887be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
888be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
88915e32d5dSMike Smith      */
8904c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
891be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
892be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
893be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
894be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
895be2b1797SNate Lawson 			      bus, NULL);
896be2b1797SNate Lawson 	}
897be2b1797SNate Lawson     }
89815e32d5dSMike Smith 
89915e32d5dSMike Smith     /*
90015e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
90115e32d5dSMike Smith      */
9024c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
90315e32d5dSMike Smith     bus_generic_attach(bus);
90415e32d5dSMike Smith 
90515e32d5dSMike Smith     /*
90615e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
90715e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
90815e32d5dSMike Smith      */
9094c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
91015e32d5dSMike Smith     bus_generic_attach(bus);
9110ae55423SMike Smith 
912bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
9130ae55423SMike Smith     return_VOID;
91415e32d5dSMike Smith }
91515e32d5dSMike Smith 
91615e32d5dSMike Smith /*
91715e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
91815e32d5dSMike Smith  * it.
91915e32d5dSMike Smith  */
92015e32d5dSMike Smith static ACPI_STATUS
92115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
92215e32d5dSMike Smith {
92315e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
92415e32d5dSMike Smith     device_t		child, bus = (device_t)context;
92515e32d5dSMike Smith 
926b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
9270ae55423SMike Smith 
928be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
9290ae55423SMike Smith     if (acpi_avoid(handle))
9300ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
9310ae55423SMike Smith 
932b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
93315e32d5dSMike Smith 	switch(type) {
93415e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
93515e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
93615e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
93715e32d5dSMike Smith 	case ACPI_TYPE_POWER:
9380ae55423SMike Smith 	    if (acpi_disabled("children"))
9390ae55423SMike Smith 		break;
940be2b1797SNate Lawson 
94115e32d5dSMike Smith 	    /*
94215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
94315e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
94415e32d5dSMike Smith 	     */
945be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
946be2b1797SNate Lawson 			     acpi_name(handle)));
94715e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
948bc0f2195SMike Smith 	    if (child == NULL)
949bc0f2195SMike Smith 		break;
95015e32d5dSMike Smith 	    acpi_set_handle(child, handle);
951bc0f2195SMike Smith 
952bc0f2195SMike Smith 	    /*
953b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
954b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
955b519f9d6SMike Smith 	     * the handle, but we don't probe it).
956b519f9d6SMike Smith 	     */
957be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
958b519f9d6SMike Smith 		device_disable(child);
959b519f9d6SMike Smith 		break;
960b519f9d6SMike Smith 	    }
961b519f9d6SMike Smith 
962b519f9d6SMike Smith 	    /*
963bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
964bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
965bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
966bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
967bc0f2195SMike Smith 	     * device not to have any resources.
968bc0f2195SMike Smith 	     */
969bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
970bc0f2195SMike Smith 
971be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
972b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
9730ae55423SMike Smith 	    break;
97415e32d5dSMike Smith 	}
97515e32d5dSMike Smith     }
976be2b1797SNate Lawson 
9770ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
97815e32d5dSMike Smith }
97915e32d5dSMike Smith 
98015e32d5dSMike Smith static void
98115e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
98215e32d5dSMike Smith {
983c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
984c6a78e98STakanori Watanabe 
985cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
986cb9b0d80SMike Smith 
98715e32d5dSMike Smith     /*
9880ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
98915e32d5dSMike Smith      * will be turned on again on some laptops.
99015e32d5dSMike Smith      *
99115e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
99215e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
99315e32d5dSMike Smith      *     shutdown process.
99415e32d5dSMike Smith      */
995c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
996c6a78e98STakanori Watanabe 	acpi_Disable(sc);
99715e32d5dSMike Smith }
99815e32d5dSMike Smith 
99915e32d5dSMike Smith static void
100015e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
100115e32d5dSMike Smith {
100215e32d5dSMike Smith     ACPI_STATUS	status;
100315e32d5dSMike Smith 
1004cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1005cb9b0d80SMike Smith 
1006be2b1797SNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1007e1a90ae1SNate Lawson 	printf("Powering system off using ACPI\n");
1008be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(acpi_off_state);
1009be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1010b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1011b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
1012b9c780d6SMitsuru IWASAKI 	    return;
1013b9c780d6SMitsuru IWASAKI 	}
101455398047SNate Lawson 	ACPI_DISABLE_IRQS();
1015be2b1797SNate Lawson 	status = AcpiEnterSleepState(acpi_off_state);
1016be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1017bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
101815e32d5dSMike Smith 	} else {
101915e32d5dSMike Smith 	    DELAY(1000000);
102015e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
102115e32d5dSMike Smith 	}
1022494ae560SMitsuru IWASAKI     } else {
1023e1a90ae1SNate Lawson 	printf("Shutting down ACPI\n");
1024494ae560SMitsuru IWASAKI 	AcpiTerminate();
102515e32d5dSMike Smith     }
102615e32d5dSMike Smith }
102715e32d5dSMike Smith 
102813d4f7baSMitsuru IWASAKI static void
102913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
103013d4f7baSMitsuru IWASAKI {
103113d4f7baSMitsuru IWASAKI     static int	first_time = 1;
103213d4f7baSMitsuru IWASAKI 
1033cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1034cb9b0d80SMike Smith 
103513d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1036be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
103759ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0);
103859ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
103913d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1040be2b1797SNate Lawson 				     acpi_eventhandler_power_button_for_sleep,
1041be2b1797SNate Lawson 				     sc);
1042be2b1797SNate Lawson 	if (first_time)
10436c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
104413d4f7baSMitsuru IWASAKI     }
1045be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
104659ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0);
104759ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
104813d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1049be2b1797SNate Lawson 				     acpi_eventhandler_sleep_button_for_sleep,
1050be2b1797SNate Lawson 				     sc);
1051be2b1797SNate Lawson 	if (first_time)
10526c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
105313d4f7baSMitsuru IWASAKI     }
105413d4f7baSMitsuru IWASAKI 
105513d4f7baSMitsuru IWASAKI     first_time = 0;
105613d4f7baSMitsuru IWASAKI }
105713d4f7baSMitsuru IWASAKI 
105815e32d5dSMike Smith /*
1059c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1060c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1061c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1062c5ba0be4SMike Smith  */
1063c5ba0be4SMike Smith BOOLEAN
1064c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1065c5ba0be4SMike Smith {
1066c5ba0be4SMike Smith     ACPI_HANDLE		h;
1067c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
10686fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1069c5ba0be4SMike Smith     ACPI_STATUS		error;
1070c5ba0be4SMike Smith 
1071cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1072cb9b0d80SMike Smith 
1073c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1074c5ba0be4SMike Smith 	return (FALSE);
10756fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10766fca9360SNate Lawson     if (ACPI_FAILURE(error))
1077c5ba0be4SMike Smith 	return (FALSE);
1078be2b1797SNate Lawson 
1079be2b1797SNate Lawson     /* If no _STA method, must be present */
10806fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
108143896e91SMike Smith 	return (TRUE);
1082be2b1797SNate Lawson 
1083be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
108443896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
1085c5ba0be4SMike Smith 	return (TRUE);
1086be2b1797SNate Lawson 
1087c5ba0be4SMike Smith     return (FALSE);
1088c5ba0be4SMike Smith }
1089c5ba0be4SMike Smith 
1090c5ba0be4SMike Smith /*
1091b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1092b53f2771SMike Smith  */
1093b53f2771SMike Smith BOOLEAN
1094b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1095b53f2771SMike Smith {
1096b53f2771SMike Smith     ACPI_HANDLE		h;
1097b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
10986fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1099b53f2771SMike Smith     ACPI_STATUS		error;
1100b53f2771SMike Smith 
1101b53f2771SMike Smith     ACPI_ASSERTLOCK;
1102b53f2771SMike Smith 
1103b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1104b53f2771SMike Smith 	return (FALSE);
11056fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11066fca9360SNate Lawson     if (ACPI_FAILURE(error))
1107b53f2771SMike Smith 	return (FALSE);
1108be2b1797SNate Lawson 
1109be2b1797SNate Lawson     /* If no _STA method, must be present */
11106fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
1111b53f2771SMike Smith 	return (TRUE);
1112be2b1797SNate Lawson 
1113be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
1114b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1115b53f2771SMike Smith 	return (TRUE);
1116be2b1797SNate Lawson 
1117b53f2771SMike Smith     return (FALSE);
1118b53f2771SMike Smith }
1119b53f2771SMike Smith 
1120b53f2771SMike Smith /*
112115e32d5dSMike Smith  * Match a HID string against a device
112215e32d5dSMike Smith  */
112315e32d5dSMike Smith BOOLEAN
112415e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
112515e32d5dSMike Smith {
112615e32d5dSMike Smith     ACPI_HANDLE		h;
112715e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
11286fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
112915e32d5dSMike Smith     ACPI_STATUS		error;
1130ed136da6SDoug Rabson     int			cid;
113115e32d5dSMike Smith 
1132cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1133cb9b0d80SMike Smith 
11344d332989SMike Smith     if (hid == NULL)
113515e32d5dSMike Smith 	return (FALSE);
113615e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
113715e32d5dSMike Smith 	return (FALSE);
11386fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11396fca9360SNate Lawson     if (ACPI_FAILURE(error))
114015e32d5dSMike Smith 	return (FALSE);
11416fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) != 0 &&
11426fca9360SNate Lawson 	strcmp(hid, devinfo.HardwareId.Value) == 0)
114315e32d5dSMike Smith 	return (TRUE);
1144be2b1797SNate Lawson 
1145b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1146ed136da6SDoug Rabson 	return (FALSE);
1147ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1148ed136da6SDoug Rabson 	return (TRUE);
1149be2b1797SNate Lawson 
115015e32d5dSMike Smith     return (FALSE);
115115e32d5dSMike Smith }
115215e32d5dSMike Smith 
115315e32d5dSMike Smith /*
1154c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1155c5ba0be4SMike Smith  * or one if its parents.
1156c5ba0be4SMike Smith  */
1157c5ba0be4SMike Smith ACPI_STATUS
1158c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1159c5ba0be4SMike Smith {
1160c5ba0be4SMike Smith     ACPI_HANDLE		r;
1161c5ba0be4SMike Smith     ACPI_STATUS		status;
1162c5ba0be4SMike Smith 
1163cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1164cb9b0d80SMike Smith 
1165be2b1797SNate Lawson     /* Walk back up the tree to the root */
1166c5ba0be4SMike Smith     for (;;) {
1167be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1168be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1169c5ba0be4SMike Smith 	    *result = r;
1170c5ba0be4SMike Smith 	    return (AE_OK);
1171c5ba0be4SMike Smith 	}
1172c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1173c5ba0be4SMike Smith 	    return (AE_OK);
1174b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1175c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1176c5ba0be4SMike Smith 	parent = r;
1177c5ba0be4SMike Smith     }
1178c5ba0be4SMike Smith }
1179c5ba0be4SMike Smith 
1180c5ba0be4SMike Smith /*
1181c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1182c5ba0be4SMike Smith  */
1183c5ba0be4SMike Smith ACPI_BUFFER *
1184c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1185c5ba0be4SMike Smith {
1186c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1187c5ba0be4SMike Smith 
1188c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1189c5ba0be4SMike Smith 	return (NULL);
1190c5ba0be4SMike Smith     buf->Length = size;
1191c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1192c5ba0be4SMike Smith     return (buf);
1193c5ba0be4SMike Smith }
1194c5ba0be4SMike Smith 
1195c5ba0be4SMike Smith /*
1196c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1197c5ba0be4SMike Smith  */
1198c5ba0be4SMike Smith ACPI_STATUS
1199c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1200c5ba0be4SMike Smith {
1201be2b1797SNate Lawson     ACPI_STATUS	status;
1202c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1203c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1204c5ba0be4SMike Smith 
1205cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1206cb9b0d80SMike Smith 
1207c5ba0be4SMike Smith     if (handle == NULL)
1208c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
12090c7da7acSJohn Baldwin 
12100c7da7acSJohn Baldwin     /*
12110c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
12120c7da7acSJohn Baldwin      * a method that will return an Integer.
12130c7da7acSJohn Baldwin      */
1214c5ba0be4SMike Smith     buf.Pointer = &param;
1215c5ba0be4SMike Smith     buf.Length = sizeof(param);
1216be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1217be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1218be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1219c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1220be2b1797SNate Lawson 	else
1221be2b1797SNate Lawson 	    status = AE_TYPE;
1222c5ba0be4SMike Smith     }
12230c7da7acSJohn Baldwin 
12240c7da7acSJohn Baldwin     /*
12250c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
12260c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
12270c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
12280c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
12290c7da7acSJohn Baldwin      *
12300c7da7acSJohn Baldwin      * This is a hack.
12310c7da7acSJohn Baldwin      */
1232be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1233b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1234be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
12350c7da7acSJohn Baldwin 	} else {
1236be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1237be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1238be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
12390c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
12400c7da7acSJohn Baldwin 	}
1241f97739daSNate Lawson     }
1242be2b1797SNate Lawson     return (status);
1243c5ba0be4SMike Smith }
1244c5ba0be4SMike Smith 
1245c573e654SMitsuru IWASAKI ACPI_STATUS
1246c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1247c573e654SMitsuru IWASAKI {
1248c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1249c573e654SMitsuru IWASAKI     int		i;
1250c573e654SMitsuru IWASAKI 
1251c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1252c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1253c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1254c573e654SMitsuru IWASAKI 	return (AE_OK);
1255c573e654SMitsuru IWASAKI     }
1256c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1257c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1258c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1259c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1260be2b1797SNate Lawson 
1261c573e654SMitsuru IWASAKI     *number = 0;
1262c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1263c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1264c573e654SMitsuru IWASAKI     return (AE_OK);
1265c573e654SMitsuru IWASAKI }
1266c573e654SMitsuru IWASAKI 
1267c5ba0be4SMike Smith /*
1268c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1269c5ba0be4SMike Smith  * function for each element.
1270c5ba0be4SMike Smith  *
1271c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1272c5ba0be4SMike Smith  */
1273c5ba0be4SMike Smith ACPI_STATUS
1274be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1275be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1276c5ba0be4SMike Smith {
1277c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1278c5ba0be4SMike Smith     int		i;
1279c5ba0be4SMike Smith 
1280be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1281c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1282c5ba0be4SMike Smith 
1283be2b1797SNate Lawson     /* Iterate over components */
1284be2b1797SNate Lawson     i = 0;
1285be2b1797SNate Lawson     comp = pkg->Package.Elements;
1286be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1287c5ba0be4SMike Smith 	func(comp, arg);
1288c5ba0be4SMike Smith 
1289c5ba0be4SMike Smith     return (AE_OK);
1290c5ba0be4SMike Smith }
1291c5ba0be4SMike Smith 
12926f69255bSMike Smith /*
12936f69255bSMike Smith  * Find the (index)th resource object in a set.
12946f69255bSMike Smith  */
12956f69255bSMike Smith ACPI_STATUS
12966d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
12976f69255bSMike Smith {
12986d63101aSMike Smith     ACPI_RESOURCE	*rp;
12996f69255bSMike Smith     int			i;
13006f69255bSMike Smith 
13016d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
13026f69255bSMike Smith     i = index;
13036d63101aSMike Smith     while (i-- > 0) {
1304be2b1797SNate Lawson 	/* Range check */
13056d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
13066f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1307be2b1797SNate Lawson 
1308be2b1797SNate Lawson 	/* Check for terminator */
1309be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
13106d63101aSMike Smith 	    return (AE_NOT_FOUND);
13116d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
13126f69255bSMike Smith     }
13136f69255bSMike Smith     if (resp != NULL)
13146d63101aSMike Smith 	*resp = rp;
1315be2b1797SNate Lawson 
13166d63101aSMike Smith     return (AE_OK);
13176d63101aSMike Smith }
13186d63101aSMike Smith 
13196d63101aSMike Smith /*
13206d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
13216d63101aSMike Smith  *
13226d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
13236d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
13246d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
13256d63101aSMike Smith  * resources.
13266d63101aSMike Smith  */
13276d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
13286d63101aSMike Smith 
13296d63101aSMike Smith ACPI_STATUS
13306d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
13316d63101aSMike Smith {
13326d63101aSMike Smith     ACPI_RESOURCE	*rp;
13336d63101aSMike Smith     void		*newp;
13346d63101aSMike Smith 
1335be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
13366d63101aSMike Smith     if (buf->Pointer == NULL) {
13376d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
13386d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
13396d63101aSMike Smith 	    return (AE_NO_MEMORY);
13406d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
13416d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
13426d63101aSMike Smith 	rp->Length = 0;
13436d63101aSMike Smith     }
13446d63101aSMike Smith     if (res == NULL)
13456d63101aSMike Smith 	return (AE_OK);
13466d63101aSMike Smith 
13476d63101aSMike Smith     /*
13486d63101aSMike Smith      * Scan the current buffer looking for the terminator.
13496d63101aSMike Smith      * This will either find the terminator or hit the end
13506d63101aSMike Smith      * of the buffer and return an error.
13516d63101aSMike Smith      */
13526d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
13536d63101aSMike Smith     for (;;) {
1354be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
13556d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
13566d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1357be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
13586d63101aSMike Smith 	    break;
13596d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
13606d63101aSMike Smith     }
13616d63101aSMike Smith 
13626d63101aSMike Smith     /*
13636d63101aSMike Smith      * Check the size of the buffer and expand if required.
13646d63101aSMike Smith      *
13656d63101aSMike Smith      * Required size is:
13666d63101aSMike Smith      *	size of existing resources before terminator +
13676d63101aSMike Smith      *	size of new resource and header +
13686d63101aSMike Smith      * 	size of terminator.
13696d63101aSMike Smith      *
13706d63101aSMike Smith      * Note that this loop should really only run once, unless
13716d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
13726d63101aSMike Smith      */
13736d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
13746d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
13756d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
13766d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
13776d63101aSMike Smith 	    return (AE_NO_MEMORY);
13786d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1379a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1380a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
13816d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
13826d63101aSMike Smith 	buf->Pointer = newp;
13836d63101aSMike Smith 	buf->Length += buf->Length;
13846d63101aSMike Smith     }
13856d63101aSMike Smith 
1386be2b1797SNate Lawson     /* Insert the new resource. */
13876d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
13886d63101aSMike Smith 
1389be2b1797SNate Lawson     /* And add the terminator. */
13906d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
13916d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
13926d63101aSMike Smith     rp->Length = 0;
13936d63101aSMike Smith 
13946f69255bSMike Smith     return (AE_OK);
13956f69255bSMike Smith }
1396c5ba0be4SMike Smith 
1397da14ac9fSJohn Baldwin /*
1398da14ac9fSJohn Baldwin  * Set interrupt model.
1399da14ac9fSJohn Baldwin  */
1400da14ac9fSJohn Baldwin ACPI_STATUS
1401da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1402da14ac9fSJohn Baldwin {
1403da14ac9fSJohn Baldwin     ACPI_OBJECT_LIST ArgList;
1404da14ac9fSJohn Baldwin     ACPI_OBJECT Arg;
1405da14ac9fSJohn Baldwin 
1406da14ac9fSJohn Baldwin     Arg.Type = ACPI_TYPE_INTEGER;
1407da14ac9fSJohn Baldwin     Arg.Integer.Value = model;
1408da14ac9fSJohn Baldwin     ArgList.Count = 1;
1409da14ac9fSJohn Baldwin     ArgList.Pointer = &Arg;
1410da14ac9fSJohn Baldwin     return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1411da14ac9fSJohn Baldwin }
1412da14ac9fSJohn Baldwin 
1413ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1414ece50487SMitsuru IWASAKI 
1415ece50487SMitsuru IWASAKI static void
1416ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1417ece50487SMitsuru IWASAKI {
1418ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1419ece50487SMitsuru IWASAKI }
1420c30382dfSMitsuru IWASAKI 
142115e32d5dSMike Smith /*
142215e32d5dSMike Smith  * Set the system sleep state
142315e32d5dSMike Smith  *
1424be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
142515e32d5dSMike Smith  */
142615e32d5dSMike Smith ACPI_STATUS
142715e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
142815e32d5dSMike Smith {
142915e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
143056d8cb57SMitsuru IWASAKI     UINT8	TypeA;
143156d8cb57SMitsuru IWASAKI     UINT8	TypeB;
143215e32d5dSMike Smith 
1433b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1434cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14350ae55423SMike Smith 
143698175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
14376397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
143898175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
14396397624dSMitsuru IWASAKI 
144098175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1441ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1442ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1443ece50487SMitsuru IWASAKI 
144415e32d5dSMike Smith     switch (state) {
144515e32d5dSMike Smith     case ACPI_STATE_S1:
14466161544cSTakanori Watanabe     case ACPI_STATE_S2:
14476161544cSTakanori Watanabe     case ACPI_STATE_S3:
14486161544cSTakanori Watanabe     case ACPI_STATE_S4:
1449be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
145098175614SNate Lawson 	if (status == AE_NOT_FOUND) {
145198175614SNate Lawson 	    device_printf(sc->acpi_dev,
145298175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
145398175614SNate Lawson 	    break;
145498175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1455be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1456be2b1797SNate Lawson 			  AcpiFormatException(status));
145756d8cb57SMitsuru IWASAKI 	    break;
145856d8cb57SMitsuru IWASAKI 	}
145956d8cb57SMitsuru IWASAKI 
1460a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1461a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1462a1fccb47SMitsuru IWASAKI 
1463be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
146415e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
146515e32d5dSMike Smith 	    /*
146615e32d5dSMike Smith 	     * Re-wake the system.
146715e32d5dSMike Smith 	     *
146815e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
146915e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
147015e32d5dSMike Smith 	     *     current bus interface does not provide for this.
147115e32d5dSMike Smith 	     */
147215e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
14730ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
147415e32d5dSMike Smith 	}
1475b9c780d6SMitsuru IWASAKI 
1476be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1477be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1478b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1479b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1480b9c780d6SMitsuru IWASAKI 	    break;
1481b9c780d6SMitsuru IWASAKI 	}
1482b9c780d6SMitsuru IWASAKI 
1483be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1484ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1485ff01efb5SMitsuru IWASAKI 
14866161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
14876161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
14886161544cSTakanori Watanabe 
1489be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
149059ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
149159ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
149259ddeb18SNate Lawson 
14936161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1494a1fccb47SMitsuru IWASAKI 	    }
14956161544cSTakanori Watanabe 
14966161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1497be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1498964679ceSMitsuru IWASAKI 		AcpiEnable();
14996161544cSTakanori Watanabe 	} else {
1500be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1501be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1502be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1503be2b1797SNate Lawson 			      AcpiFormatException(status));
1504c30382dfSMitsuru IWASAKI 		break;
150515e32d5dSMike Smith 	    }
15066161544cSTakanori Watanabe 	}
1507ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
150815e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
150915e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
151013d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
151115e32d5dSMike Smith 	break;
151215e32d5dSMike Smith     case ACPI_STATE_S5:
151315e32d5dSMike Smith 	/*
151415e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
151515e32d5dSMike Smith 	 * shutdown handlers.
151615e32d5dSMike Smith 	 */
151715e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
151815e32d5dSMike Smith 	break;
151998175614SNate Lawson     case ACPI_STATE_S0:
152015e32d5dSMike Smith     default:
152115e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
152215e32d5dSMike Smith 	break;
152315e32d5dSMike Smith     }
1524ece50487SMitsuru IWASAKI 
152598175614SNate Lawson     /* Disable a second sleep request for a short period */
1526ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1527ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1528ece50487SMitsuru IWASAKI 
15290ae55423SMike Smith     return_ACPI_STATUS (status);
153015e32d5dSMike Smith }
153115e32d5dSMike Smith 
153215e32d5dSMike Smith /*
153315e32d5dSMike Smith  * Enable/Disable ACPI
153415e32d5dSMike Smith  */
153515e32d5dSMike Smith ACPI_STATUS
153615e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
153715e32d5dSMike Smith {
153815e32d5dSMike Smith     ACPI_STATUS	status;
153915e32d5dSMike Smith     u_int32_t	flags;
154015e32d5dSMike Smith 
1541b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1542cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
15430ae55423SMike Smith 
154415e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
154515e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1546be2b1797SNate Lawson     if (!sc->acpi_enabled)
154715e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1548be2b1797SNate Lawson     else
154915e32d5dSMike Smith 	status = AE_OK;
1550be2b1797SNate Lawson 
155115e32d5dSMike Smith     if (status == AE_OK)
155215e32d5dSMike Smith 	sc->acpi_enabled = 1;
1553be2b1797SNate Lawson 
15540ae55423SMike Smith     return_ACPI_STATUS (status);
155515e32d5dSMike Smith }
155615e32d5dSMike Smith 
155715e32d5dSMike Smith ACPI_STATUS
155815e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
155915e32d5dSMike Smith {
156015e32d5dSMike Smith     ACPI_STATUS	status;
156115e32d5dSMike Smith 
1562b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1563cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
15640ae55423SMike Smith 
1565be2b1797SNate Lawson     if (sc->acpi_enabled)
156615e32d5dSMike Smith 	status = AcpiDisable();
1567be2b1797SNate Lawson     else
156815e32d5dSMike Smith 	status = AE_OK;
1569be2b1797SNate Lawson 
157015e32d5dSMike Smith     if (status == AE_OK)
157115e32d5dSMike Smith 	sc->acpi_enabled = 0;
1572be2b1797SNate Lawson 
15730ae55423SMike Smith     return_ACPI_STATUS (status);
157415e32d5dSMike Smith }
157515e32d5dSMike Smith 
157615e32d5dSMike Smith /*
157715e32d5dSMike Smith  * ACPI Event Handlers
157815e32d5dSMike Smith  */
157915e32d5dSMike Smith 
158015e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
158115e32d5dSMike Smith 
158215e32d5dSMike Smith static void
158315e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
158415e32d5dSMike Smith {
1585fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1586b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
158715e32d5dSMike Smith 
1588cb9b0d80SMike Smith     ACPI_LOCK;
1589a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
159015e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1591cb9b0d80SMike Smith     ACPI_UNLOCK;
15920ae55423SMike Smith     return_VOID;
159315e32d5dSMike Smith }
159415e32d5dSMike Smith 
159515e32d5dSMike Smith static void
159615e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
159715e32d5dSMike Smith {
1598fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1599b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
16000ae55423SMike Smith 
160115e32d5dSMike Smith     /* Well, what to do? :-) */
16020ae55423SMike Smith 
1603cb9b0d80SMike Smith     ACPI_LOCK;
1604cb9b0d80SMike Smith     ACPI_UNLOCK;
1605cb9b0d80SMike Smith 
16060ae55423SMike Smith     return_VOID;
160715e32d5dSMike Smith }
160815e32d5dSMike Smith 
160915e32d5dSMike Smith /*
161015e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
161115e32d5dSMike Smith  */
161215e32d5dSMike Smith UINT32
161315e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
161415e32d5dSMike Smith {
161515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
161615e32d5dSMike Smith 
1617b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16180ae55423SMike Smith 
161915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
16200ae55423SMike Smith 
1621b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
162215e32d5dSMike Smith }
162315e32d5dSMike Smith 
162415e32d5dSMike Smith UINT32
162515e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
162615e32d5dSMike Smith {
162715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
162815e32d5dSMike Smith 
1629b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16300ae55423SMike Smith 
163115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
16320ae55423SMike Smith 
1633b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
163415e32d5dSMike Smith }
163515e32d5dSMike Smith 
163615e32d5dSMike Smith UINT32
163715e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
163815e32d5dSMike Smith {
163915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
164015e32d5dSMike Smith 
1641b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16420ae55423SMike Smith 
164315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
16440ae55423SMike Smith 
1645b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
164615e32d5dSMike Smith }
164715e32d5dSMike Smith 
164815e32d5dSMike Smith UINT32
164915e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
165015e32d5dSMike Smith {
165115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
165215e32d5dSMike Smith 
1653b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16540ae55423SMike Smith 
165515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
16560ae55423SMike Smith 
1657b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
165815e32d5dSMike Smith }
165915e32d5dSMike Smith 
166015e32d5dSMike Smith /*
166115e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
166215e32d5dSMike Smith  */
166315e32d5dSMike Smith struct acpi_staticbuf {
166415e32d5dSMike Smith     ACPI_BUFFER	buffer;
166515e32d5dSMike Smith     char	data[512];
166615e32d5dSMike Smith };
166715e32d5dSMike Smith 
166815e32d5dSMike Smith char *
166915e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
167015e32d5dSMike Smith {
167115e32d5dSMike Smith     static struct acpi_staticbuf	buf;
167215e32d5dSMike Smith 
1673cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1674cb9b0d80SMike Smith 
167515e32d5dSMike Smith     buf.buffer.Length = 512;
167615e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
167715e32d5dSMike Smith 
1678b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
167915e32d5dSMike Smith 	return (buf.buffer.Pointer);
1680be2b1797SNate Lawson 
168115e32d5dSMike Smith     return ("(unknown path)");
168215e32d5dSMike Smith }
168315e32d5dSMike Smith 
168415e32d5dSMike Smith /*
168515e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
168615e32d5dSMike Smith  * parts of the namespace.
168715e32d5dSMike Smith  */
168815e32d5dSMike Smith int
168915e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
169015e32d5dSMike Smith {
16913c600cfbSJohn Baldwin     char	*cp, *env, *np;
169215e32d5dSMike Smith     int		len;
169315e32d5dSMike Smith 
169415e32d5dSMike Smith     np = acpi_name(handle);
169515e32d5dSMike Smith     if (*np == '\\')
169615e32d5dSMike Smith 	np++;
16973c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
169815e32d5dSMike Smith 	return (0);
169915e32d5dSMike Smith 
1700be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
17013c600cfbSJohn Baldwin     cp = env;
170215e32d5dSMike Smith     for (;;) {
170315e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
170415e32d5dSMike Smith 	    cp++;
170515e32d5dSMike Smith 	if (*cp == 0)
170615e32d5dSMike Smith 	    break;
170715e32d5dSMike Smith 	len = 0;
170815e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
170915e32d5dSMike Smith 	    len++;
1710d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
17113c600cfbSJohn Baldwin 	    freeenv(env);
17120ae55423SMike Smith 	    return(1);
1713d786139cSMaxime Henrion 	}
17140ae55423SMike Smith 	cp += len;
17150ae55423SMike Smith     }
17163c600cfbSJohn Baldwin     freeenv(env);
1717be2b1797SNate Lawson 
17180ae55423SMike Smith     return (0);
17190ae55423SMike Smith }
17200ae55423SMike Smith 
17210ae55423SMike Smith /*
17220ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
17230ae55423SMike Smith  */
17240ae55423SMike Smith int
17250ae55423SMike Smith acpi_disabled(char *subsys)
17260ae55423SMike Smith {
172778689b15SMaxime Henrion     char	*cp, *env;
17280ae55423SMike Smith     int		len;
17290ae55423SMike Smith 
173078689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
17310ae55423SMike Smith 	return (0);
173278689b15SMaxime Henrion     if (!strcmp(env, "all")) {
173378689b15SMaxime Henrion 	freeenv(env);
17340ae55423SMike Smith 	return (1);
1735d786139cSMaxime Henrion     }
17360ae55423SMike Smith 
17370ae55423SMike Smith     /* scan the disable list checking for a match */
173878689b15SMaxime Henrion     cp = env;
17390ae55423SMike Smith     for (;;) {
17400ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
17410ae55423SMike Smith 	    cp++;
17420ae55423SMike Smith 	if (*cp == 0)
17430ae55423SMike Smith 	    break;
17440ae55423SMike Smith 	len = 0;
17450ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
17460ae55423SMike Smith 	    len++;
1747d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
174878689b15SMaxime Henrion 	    freeenv(env);
174915e32d5dSMike Smith 	    return (1);
1750d786139cSMaxime Henrion 	}
175115e32d5dSMike Smith 	cp += len;
175215e32d5dSMike Smith     }
175378689b15SMaxime Henrion     freeenv(env);
1754be2b1797SNate Lawson 
175515e32d5dSMike Smith     return (0);
175615e32d5dSMike Smith }
175715e32d5dSMike Smith 
175815e32d5dSMike Smith /*
1759a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1760a1fccb47SMitsuru IWASAKI  */
1761a1fccb47SMitsuru IWASAKI void
1762a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1763a1fccb47SMitsuru IWASAKI {
1764a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1765a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1766a1fccb47SMitsuru IWASAKI 
1767a1fccb47SMitsuru IWASAKI     /*
1768a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1769a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1770a1fccb47SMitsuru IWASAKI      */
1771a1fccb47SMitsuru IWASAKI 
1772a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1773a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1774a1fccb47SMitsuru IWASAKI 
1775a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1776a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1777a1fccb47SMitsuru IWASAKI 
1778a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1779a1fccb47SMitsuru IWASAKI }
1780a1fccb47SMitsuru IWASAKI 
1781a1fccb47SMitsuru IWASAKI void
1782a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1783a1fccb47SMitsuru IWASAKI {
1784a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1785a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1786a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1787a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1788a1fccb47SMitsuru IWASAKI 
1789a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1790a1fccb47SMitsuru IWASAKI 
1791be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
1792be2b1797SNate Lawson     if (sc == NULL)
1793a1fccb47SMitsuru IWASAKI 	return;
1794a1fccb47SMitsuru IWASAKI 
1795a1fccb47SMitsuru IWASAKI     /*
1796a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1797a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1798a1fccb47SMitsuru IWASAKI      */
1799a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1800a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1801be2b1797SNate Lawson     if (ACPI_FAILURE(status))
1802a1fccb47SMitsuru IWASAKI 	return;
1803a1fccb47SMitsuru IWASAKI 
1804a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1805be2b1797SNate Lawson     if (res == NULL)
1806a1fccb47SMitsuru IWASAKI 	return;
1807a1fccb47SMitsuru IWASAKI 
1808a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1809a1fccb47SMitsuru IWASAKI 	goto out;
1810a1fccb47SMitsuru IWASAKI     }
1811a1fccb47SMitsuru IWASAKI 
1812a1fccb47SMitsuru IWASAKI     /*
1813a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1814a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1815a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1816a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1817a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1818a1fccb47SMitsuru IWASAKI      */
1819be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1820a1fccb47SMitsuru IWASAKI 	goto out;
1821a1fccb47SMitsuru IWASAKI 
1822be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
1823a1fccb47SMitsuru IWASAKI 	goto out;
1824a1fccb47SMitsuru IWASAKI 
1825a1fccb47SMitsuru IWASAKI     /*
1826a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1827a1fccb47SMitsuru IWASAKI      */
1828a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1829a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1830a1fccb47SMitsuru IWASAKI 	/*
1831a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1832a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1833a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1834a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1835a1fccb47SMitsuru IWASAKI 	 */
1836a1fccb47SMitsuru IWASAKI 
18376fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
18386fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1839a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1840a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
1841a1fccb47SMitsuru IWASAKI 	break;
1842a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1843a1fccb47SMitsuru IWASAKI 	/*
1844be2b1797SNate Lawson 	 * XXX TBD
1845be2b1797SNate Lawson 	 *
1846a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1847a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1848a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1849a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1850a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1851a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1852a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1853a1fccb47SMitsuru IWASAKI 	 * the wake event.
1854a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1855a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1856a1fccb47SMitsuru IWASAKI 	 */
1857a1fccb47SMitsuru IWASAKI 	break;
1858a1fccb47SMitsuru IWASAKI     default:
1859a1fccb47SMitsuru IWASAKI 	break;
1860a1fccb47SMitsuru IWASAKI     }
1861a1fccb47SMitsuru IWASAKI 
1862a1fccb47SMitsuru IWASAKI out:
1863a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1864a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1865a1fccb47SMitsuru IWASAKI }
1866a1fccb47SMitsuru IWASAKI 
1867a1fccb47SMitsuru IWASAKI /*
186815e32d5dSMike Smith  * Control interface.
186915e32d5dSMike Smith  *
18700ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
1871be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
1872be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
187315e32d5dSMike Smith  */
18740ae55423SMike Smith struct acpi_ioctl_hook
18750ae55423SMike Smith {
18760ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
18770ae55423SMike Smith     u_long			 cmd;
1878be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
18790ae55423SMike Smith     void			 *arg;
18800ae55423SMike Smith };
18810ae55423SMike Smith 
18820ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
18830ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
18840ae55423SMike Smith 
18850ae55423SMike Smith /*
18860ae55423SMike Smith  * Register an ioctl handler.
18870ae55423SMike Smith  */
18880ae55423SMike Smith int
1889be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
18900ae55423SMike Smith {
18910ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18920ae55423SMike Smith 
18930ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
18940ae55423SMike Smith 	return (ENOMEM);
18950ae55423SMike Smith     hp->cmd = cmd;
18960ae55423SMike Smith     hp->fn = fn;
18970ae55423SMike Smith     hp->arg = arg;
18980ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
18990ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
19000ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
19010ae55423SMike Smith     }
19020ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
19030ae55423SMike Smith     return (0);
19040ae55423SMike Smith }
19050ae55423SMike Smith 
19060ae55423SMike Smith /*
19070ae55423SMike Smith  * Deregister an ioctl handler.
19080ae55423SMike Smith  */
19090ae55423SMike Smith void
1910be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
19110ae55423SMike Smith {
19120ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
19130ae55423SMike Smith 
19140ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
19150ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
19160ae55423SMike Smith 	    break;
19170ae55423SMike Smith 
19180ae55423SMike Smith     if (hp != NULL) {
19190ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
19200ae55423SMike Smith 	free(hp, M_ACPIDEV);
19210ae55423SMike Smith     }
19220ae55423SMike Smith }
19230ae55423SMike Smith 
192415e32d5dSMike Smith static int
19256b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
192615e32d5dSMike Smith {
192715e32d5dSMike Smith     return (0);
192815e32d5dSMike Smith }
192915e32d5dSMike Smith 
193015e32d5dSMike Smith static int
19316b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
193215e32d5dSMike Smith {
193315e32d5dSMike Smith     return (0);
193415e32d5dSMike Smith }
193515e32d5dSMike Smith 
193615e32d5dSMike Smith static int
19376b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
193815e32d5dSMike Smith {
193915e32d5dSMike Smith     struct acpi_softc		*sc;
19400ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
19410ae55423SMike Smith     int				error, xerror, state;
1942fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
194315e32d5dSMike Smith 
1944cb9b0d80SMike Smith     ACPI_LOCK;
1945cb9b0d80SMike Smith 
194615e32d5dSMike Smith     error = state = 0;
194715e32d5dSMike Smith     sc = dev->si_drv1;
194815e32d5dSMike Smith 
19490ae55423SMike Smith     /*
19500ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
19510ae55423SMike Smith      */
19520ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
19530ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
19540ae55423SMike Smith 	    if (hp->cmd == cmd) {
19550ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
19560ae55423SMike Smith 		if (xerror != 0)
19570ae55423SMike Smith 		    error = xerror;
1958917d44c8SMitsuru IWASAKI 		goto out;
19590ae55423SMike Smith 	    }
19600ae55423SMike Smith 	}
19610ae55423SMike Smith     }
19620ae55423SMike Smith 
19630ae55423SMike Smith     /*
1964a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
1965a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
1966a89fcf28STakanori Watanabe      * Not changing system behavior.
1967a89fcf28STakanori Watanabe      */
1968be2b1797SNate Lawson     if((flag & FWRITE) == 0)
1969be2b1797SNate Lawson 	return (EPERM);
1970a89fcf28STakanori Watanabe 
1971be2b1797SNate Lawson     /* Core system ioctls. */
197215e32d5dSMike Smith     switch (cmd) {
197315e32d5dSMike Smith     case ACPIIO_ENABLE:
19740ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
197515e32d5dSMike Smith 	    error = ENXIO;
197615e32d5dSMike Smith 	break;
197715e32d5dSMike Smith     case ACPIIO_DISABLE:
19780ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
197915e32d5dSMike Smith 	    error = ENXIO;
198015e32d5dSMike Smith 	break;
198115e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
198215e32d5dSMike Smith 	if (!sc->acpi_enabled) {
198315e32d5dSMike Smith 	    error = ENXIO;
198415e32d5dSMike Smith 	    break;
198515e32d5dSMike Smith 	}
198615e32d5dSMike Smith 	state = *(int *)addr;
19872d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
19882d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
198915e32d5dSMike Smith 		error = EINVAL;
19902d610c46SNate Lawson 	} else {
19912d610c46SNate Lawson 	    error = EINVAL;
19922d610c46SNate Lawson 	}
199315e32d5dSMike Smith 	break;
199415e32d5dSMike Smith     default:
19950ae55423SMike Smith 	if (error == 0)
199615e32d5dSMike Smith 	    error = EINVAL;
199715e32d5dSMike Smith 	break;
199815e32d5dSMike Smith     }
1999917d44c8SMitsuru IWASAKI 
2000917d44c8SMitsuru IWASAKI out:
2001cb9b0d80SMike Smith     ACPI_UNLOCK;
200215e32d5dSMike Smith     return (error);
200315e32d5dSMike Smith }
200415e32d5dSMike Smith 
20051d073b1dSJohn Baldwin static int
2006d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2007d75de536SMitsuru IWASAKI {
2008d75de536SMitsuru IWASAKI     char sleep_state[4];
2009d75de536SMitsuru IWASAKI     char buf[16];
2010d75de536SMitsuru IWASAKI     int error;
2011d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2012d75de536SMitsuru IWASAKI 
2013d75de536SMitsuru IWASAKI     buf[0] = '\0';
2014d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2015d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2016d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2017d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2018d75de536SMitsuru IWASAKI 	}
2019d75de536SMitsuru IWASAKI     }
2020d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2021d75de536SMitsuru IWASAKI     return (error);
2022d75de536SMitsuru IWASAKI }
2023d75de536SMitsuru IWASAKI 
2024d75de536SMitsuru IWASAKI static int
20251d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
20261d073b1dSJohn Baldwin {
20271d073b1dSJohn Baldwin     char sleep_state[10];
20281d073b1dSJohn Baldwin     int error;
20291d073b1dSJohn Baldwin     u_int new_state, old_state;
20301d073b1dSJohn Baldwin 
20311d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2032b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
20331d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2034cb9b0d80SMike Smith     } else {
2035b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
20361d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
20371d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2038cb9b0d80SMike Smith     }
20391d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
20401d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2041be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2042be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
20431d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
20441d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
20451d073b1dSJohn Baldwin 		break;
2046cb9b0d80SMike Smith 	}
2047931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2048be2b1797SNate Lawson 	    if (new_state != old_state)
20491d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2050cb9b0d80SMike Smith 	} else {
20511d073b1dSJohn Baldwin 	    error = EINVAL;
20521d073b1dSJohn Baldwin 	}
2053cb9b0d80SMike Smith     }
2054be2b1797SNate Lawson 
20551d073b1dSJohn Baldwin     return (error);
20561d073b1dSJohn Baldwin }
20571d073b1dSJohn Baldwin 
20589b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
20599b937d48SNate Lawson void
20609b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
20619b937d48SNate Lawson {
20629b937d48SNate Lawson     char		notify_buf[16];
20639b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
20649b937d48SNate Lawson     ACPI_STATUS		status;
20659b937d48SNate Lawson 
20669b937d48SNate Lawson     if (subsystem == NULL)
20679b937d48SNate Lawson 	return;
20689b937d48SNate Lawson 
20699b937d48SNate Lawson     handle_buf.Pointer = NULL;
20709b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
20719b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
20729b937d48SNate Lawson     if (ACPI_FAILURE(status))
20739b937d48SNate Lawson 	return;
20749b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
20759b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
20769b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
20779b937d48SNate Lawson }
20789b937d48SNate Lawson 
207915e32d5dSMike Smith #ifdef ACPI_DEBUG
20800ae55423SMike Smith /*
20810ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
20820ae55423SMike Smith  *
20830ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
20840ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
20850ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
20860ae55423SMike Smith  * prefixing the bit name with !.
20870ae55423SMike Smith  */
208815e32d5dSMike Smith struct debugtag
208915e32d5dSMike Smith {
209015e32d5dSMike Smith     char	*name;
209115e32d5dSMike Smith     UINT32	value;
209215e32d5dSMike Smith };
209315e32d5dSMike Smith 
209415e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2095c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2096c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2097c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2098c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2099c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2100c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2101c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2102c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2103c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2104d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
21054c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2106d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2107297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
21084c1cdee6SMike Smith 
2109cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
21104c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2111cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2112cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2113c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2114c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2115c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
21164c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2117cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
21184c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
2119b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
212015e32d5dSMike Smith     {NULL, 0}
212115e32d5dSMike Smith };
212215e32d5dSMike Smith 
212315e32d5dSMike Smith static struct debugtag dbg_level[] = {
21244c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
21259501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
21269501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
21274c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
21289501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
21294c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2130d62ab2f4SMitsuru IWASAKI 
2131d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2132297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
21334c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
21344c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2135d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
21364c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
21374c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
21384c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
21394c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
21404c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
21414c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
21424c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
21434c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
21444c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
21454c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2146d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2147d62ab2f4SMitsuru IWASAKI 
2148d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2149d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2150d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2151d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2152d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
21534c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2154d62ab2f4SMitsuru IWASAKI 
2155d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2156d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2157d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2158d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2159d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2160d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2161d62ab2f4SMitsuru IWASAKI 
2162d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
216398479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
216498479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
216598479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
216698479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
216798479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
216815e32d5dSMike Smith     {NULL, 0}
216915e32d5dSMike Smith };
217015e32d5dSMike Smith 
217115e32d5dSMike Smith static void
217215e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
217315e32d5dSMike Smith {
217415e32d5dSMike Smith     char	*ep;
217515e32d5dSMike Smith     int		i, l;
21760ae55423SMike Smith     int		set;
217715e32d5dSMike Smith 
217815e32d5dSMike Smith     while (*cp) {
217915e32d5dSMike Smith 	if (isspace(*cp)) {
218015e32d5dSMike Smith 	    cp++;
218115e32d5dSMike Smith 	    continue;
218215e32d5dSMike Smith 	}
218315e32d5dSMike Smith 	ep = cp;
218415e32d5dSMike Smith 	while (*ep && !isspace(*ep))
218515e32d5dSMike Smith 	    ep++;
21860ae55423SMike Smith 	if (*cp == '!') {
21870ae55423SMike Smith 	    set = 0;
21880ae55423SMike Smith 	    cp++;
21890ae55423SMike Smith 	    if (cp == ep)
21900ae55423SMike Smith 		continue;
21910ae55423SMike Smith 	} else {
21920ae55423SMike Smith 	    set = 1;
21930ae55423SMike Smith 	}
219415e32d5dSMike Smith 	l = ep - cp;
219515e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
219615e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2197be2b1797SNate Lawson 		if (set)
219815e32d5dSMike Smith 		    *flag |= tag[i].value;
2199be2b1797SNate Lawson 		else
22000ae55423SMike Smith 		    *flag &= ~tag[i].value;
220115e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
220215e32d5dSMike Smith 	    }
220315e32d5dSMike Smith 	}
220415e32d5dSMike Smith 	cp = ep;
220515e32d5dSMike Smith     }
220615e32d5dSMike Smith }
220715e32d5dSMike Smith 
220815e32d5dSMike Smith static void
22092a4ac806SMike Smith acpi_set_debugging(void *junk)
221015e32d5dSMike Smith {
221194aae282SMike Barcroft     char	*cp;
221215e32d5dSMike Smith 
22131d7b121cSNate Lawson     if (cold) {
22140ae55423SMike Smith 	AcpiDbgLayer = 0;
22150ae55423SMike Smith 	AcpiDbgLevel = 0;
22161d7b121cSNate Lawson     }
22171d7b121cSNate Lawson 
221894aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
221915e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
222094aae282SMike Barcroft 	freeenv(cp);
222194aae282SMike Barcroft     }
222294aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
222315e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
222494aae282SMike Barcroft 	freeenv(cp);
222594aae282SMike Barcroft     }
22260ae55423SMike Smith 
22271d7b121cSNate Lawson     if (cold) {
22281d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
22291d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
22301d7b121cSNate Lawson     }
223115e32d5dSMike Smith }
2232be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2233be2b1797SNate Lawson 	NULL);
22341d7b121cSNate Lawson 
22351d7b121cSNate Lawson static int
22361d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
22371d7b121cSNate Lawson {
22381d7b121cSNate Lawson     char	*options;
22391d7b121cSNate Lawson     int		 error, len, *dbg;
22401d7b121cSNate Lawson     struct	 debugtag *tag;
22411d7b121cSNate Lawson 
22421d7b121cSNate Lawson     len = 512;
22431d7b121cSNate Lawson     MALLOC(options, char *, len, M_TEMP, M_WAITOK);
22441d7b121cSNate Lawson     options[0] = '\0';
22451d7b121cSNate Lawson 
22461d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
22471d7b121cSNate Lawson 	tag = &dbg_layer[0];
22481d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
22491d7b121cSNate Lawson     } else {
22501d7b121cSNate Lawson 	tag = &dbg_level[0];
22511d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
22521d7b121cSNate Lawson     }
22531d7b121cSNate Lawson 
22541d7b121cSNate Lawson     /* Get old values if this is a get request. */
22551d7b121cSNate Lawson     if (*dbg == 0) {
22561d7b121cSNate Lawson 	strlcpy(options, "NONE", sizeof(options));
22571d7b121cSNate Lawson     } else if (req->newptr == NULL) {
22581d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
22591d7b121cSNate Lawson 	    if ((*dbg & tag->value) == tag->value) {
22601d7b121cSNate Lawson 		strlcat(options, tag->name, len);
22611d7b121cSNate Lawson 		strlcat(options, " ", len); /* XXX */
22621d7b121cSNate Lawson 	    }
22631d7b121cSNate Lawson 	}
22641d7b121cSNate Lawson     }
22651d7b121cSNate Lawson 
22661d7b121cSNate Lawson     error = sysctl_handle_string(oidp, options, len, req);
22671d7b121cSNate Lawson 
22681d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
22691d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
22701d7b121cSNate Lawson 	*dbg = 0;
22711d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
22721d7b121cSNate Lawson 	acpi_set_debugging(NULL);
22731d7b121cSNate Lawson     }
22741d7b121cSNate Lawson     FREE(options, M_TEMP);
22751d7b121cSNate Lawson 
22761d7b121cSNate Lawson     return (error);
22771d7b121cSNate Lawson }
22781d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
22791d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
22801d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
22811d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
228215e32d5dSMike Smith #endif
2283f9390180SMitsuru IWASAKI 
2284f9390180SMitsuru IWASAKI static int
2285f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2286f9390180SMitsuru IWASAKI {
2287f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2288f9390180SMitsuru IWASAKI 	int	error;
2289f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2290f9390180SMitsuru IWASAKI 	va_list	ap;
2291f9390180SMitsuru IWASAKI 
2292f9390180SMitsuru IWASAKI 	error = 0;
2293f9390180SMitsuru IWASAKI 	switch (cmd) {
2294f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2295f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2296f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2297f9390180SMitsuru IWASAKI 			error = EINVAL;
2298f9390180SMitsuru IWASAKI 			goto out;
2299f9390180SMitsuru IWASAKI 		}
2300f9390180SMitsuru IWASAKI 
2301f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2302f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2303f9390180SMitsuru IWASAKI 		va_end(ap);
2304f9390180SMitsuru IWASAKI 
2305f9390180SMitsuru IWASAKI 		switch (state) {
2306f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2307f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2308f9390180SMitsuru IWASAKI 			break;
2309f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2310f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2311f9390180SMitsuru IWASAKI 			break;
2312f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2313f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2314f9390180SMitsuru IWASAKI 			break;
2315f9390180SMitsuru IWASAKI 		default:
2316f9390180SMitsuru IWASAKI 			error = EINVAL;
2317f9390180SMitsuru IWASAKI 			goto out;
2318f9390180SMitsuru IWASAKI 		}
2319f9390180SMitsuru IWASAKI 
2320f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2321f9390180SMitsuru IWASAKI 		break;
2322f9390180SMitsuru IWASAKI 	default:
2323f9390180SMitsuru IWASAKI 		error = EINVAL;
2324f9390180SMitsuru IWASAKI 		goto out;
2325f9390180SMitsuru IWASAKI 	}
2326f9390180SMitsuru IWASAKI 
2327f9390180SMitsuru IWASAKI out:
2328f9390180SMitsuru IWASAKI 	return (error);
2329f9390180SMitsuru IWASAKI }
2330f9390180SMitsuru IWASAKI 
2331f9390180SMitsuru IWASAKI static void
2332f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2333f9390180SMitsuru IWASAKI {
2334be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
23356c407052SMitsuru IWASAKI 	return;
2336f9390180SMitsuru IWASAKI 
2337f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2338f9390180SMitsuru IWASAKI }
2339f9390180SMitsuru IWASAKI 
2340f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2341