xref: /freebsd/sys/dev/acpica/acpi.c (revision f97739da4d8a980bb3d25c6768e158790b93dcb4)
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>
5415e32d5dSMike Smith 
5515e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
5615e32d5dSMike Smith 
57be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
58cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
59b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
600ae55423SMike Smith 
6115e32d5dSMike Smith static d_open_t		acpiopen;
6215e32d5dSMike Smith static d_close_t	acpiclose;
6315e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6415e32d5dSMike Smith 
6515e32d5dSMike Smith #define CDEV_MAJOR 152
6615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
677ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
687ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
697ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
707ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
717ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
7215e32d5dSMike Smith };
7315e32d5dSMike Smith 
741d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
75b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
761d073b1dSJohn Baldwin 
772a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
782a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
792a4ac806SMike Smith 
80fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
81cb9b0d80SMike Smith struct mtx	acpi_mutex;
82fc0ea94aSJohn Baldwin #endif
83cb9b0d80SMike Smith 
846d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
8515e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
8615e32d5dSMike Smith static int	acpi_probe(device_t dev);
8715e32d5dSMike Smith static int	acpi_attach(device_t dev);
88be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
89be2b1797SNate Lawson 			int unit);
9015e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
91be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
92be2b1797SNate Lawson 			uintptr_t *result);
93be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
94be2b1797SNate Lawson 			uintptr_t value);
95be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
96be2b1797SNate Lawson 			int rid, u_long start, u_long count);
97be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
98be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
99be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
100be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
101be2b1797SNate Lawson 			u_long count, u_int flags);
102be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
103be2b1797SNate Lawson 			int rid, struct resource *r);
10432d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
105b2566207STakanori Watanabe static u_int32_t acpi_isa_get_compatid(device_t dev);
106be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
107be2b1797SNate Lawson 			struct isa_pnp_id *ids);
10815e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
109be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
110be2b1797SNate Lawson 			void *context, void **status);
11115e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11215e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11313d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
11415e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
11515e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
116d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1171d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
118f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
119f9390180SMitsuru IWASAKI 
12015e32d5dSMike Smith static device_method_t acpi_methods[] = {
12115e32d5dSMike Smith     /* Device interface */
12215e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
12315e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
12415e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
12515e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
12615e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
12715e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
12815e32d5dSMike Smith 
12915e32d5dSMike Smith     /* Bus interface */
13015e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13115e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
13215e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
13315e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
13415e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
13515e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
13615e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
13715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
13815e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
13915e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14015e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14115e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14215e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
14315e32d5dSMike Smith 
144bc0f2195SMike Smith     /* ISA emulation */
145bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
146bc0f2195SMike Smith 
14715e32d5dSMike Smith     {0, 0}
14815e32d5dSMike Smith };
14915e32d5dSMike Smith 
15015e32d5dSMike Smith static driver_t acpi_driver = {
15115e32d5dSMike Smith     "acpi",
15215e32d5dSMike Smith     acpi_methods,
15315e32d5dSMike Smith     sizeof(struct acpi_softc),
15415e32d5dSMike Smith };
15515e32d5dSMike Smith 
1563273b005SMike Smith static devclass_t acpi_devclass;
1576d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
158dde24897SMike Smith MODULE_VERSION(acpi, 100);
15915e32d5dSMike Smith 
160be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0,
161be2b1797SNate Lawson 	   "");
162be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0,
163be2b1797SNate Lawson 	   "");
164dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
165be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0,
166be2b1797SNate Lawson 	   "");
16715e32d5dSMike Smith 
16815e32d5dSMike Smith /*
1696d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1706d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
171be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
172be2b1797SNate Lawson  * off it.
1736d63101aSMike Smith  */
1746d63101aSMike Smith static int
1756d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1766d63101aSMike Smith {
1776d63101aSMike Smith     switch(event) {
1786d63101aSMike Smith     case MOD_LOAD:
17987b45ed5SMitsuru IWASAKI 	if (!cold) {
18087b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
1816d63101aSMike Smith 	    return (EPERM);
18287b45ed5SMitsuru IWASAKI 	}
1836d63101aSMike Smith 	break;
1846d63101aSMike Smith     case MOD_UNLOAD:
185f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1866d63101aSMike Smith 	    return (EBUSY);
187f9390180SMitsuru IWASAKI 	break;
1886d63101aSMike Smith     default:
1896d63101aSMike Smith 	break;
1906d63101aSMike Smith     }
1916d63101aSMike Smith     return (0);
1926d63101aSMike Smith }
1936d63101aSMike Smith 
1946d63101aSMike Smith /*
19515e32d5dSMike Smith  * Detect ACPI, perform early initialisation
19615e32d5dSMike Smith  */
19715e32d5dSMike Smith static void
19815e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
19915e32d5dSMike Smith {
20015e32d5dSMike Smith     device_t	child;
20115e32d5dSMike Smith     int		error;
202d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
203d786139cSMaxime Henrion     char	*debugpoint;
20415e32d5dSMike Smith #endif
20515e32d5dSMike Smith 
206b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2070ae55423SMike Smith 
20887b45ed5SMitsuru IWASAKI     if (!cold)
20987b45ed5SMitsuru IWASAKI 	return_VOID;
210840f9b53STakanori Watanabe 
211be2b1797SNate Lawson     /* Check that we haven't been disabled with a hint. */
2128a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21332d18aa5SMike Smith 	return_VOID;
21432d18aa5SMike Smith 
215be2b1797SNate Lawson     /* Make sure we're not being doubly invoked. */
21615e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2170ae55423SMike Smith 	return_VOID;
21815e32d5dSMike Smith 
219fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
220be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2216008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
222fc0ea94aSJohn Baldwin #endif
223cb9b0d80SMike Smith 
224be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
225d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
226d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
227d786139cSMaxime Henrion     if (debugpoint) {
228d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
22915e32d5dSMike Smith 	    acpi_EnterDebugger();
230d786139cSMaxime Henrion 	freeenv(debugpoint);
231d786139cSMaxime Henrion     }
23215e32d5dSMike Smith #endif
233b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
234bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2350ae55423SMike Smith 	return_VOID;
23615e32d5dSMike Smith     }
237d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
238d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
239d786139cSMaxime Henrion     if (debugpoint) {
240d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
24115e32d5dSMike Smith 	    acpi_EnterDebugger();
242d786139cSMaxime Henrion 	freeenv(debugpoint);
243d786139cSMaxime Henrion     }
24415e32d5dSMike Smith #endif
2451611ea87SMitsuru IWASAKI 
246b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
247bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2480ae55423SMike Smith 	return_VOID;
24915e32d5dSMike Smith     }
25015e32d5dSMike Smith 
251be2b1797SNate Lawson     /* Attach the actual ACPI device. */
25215e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
25315e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
2540ae55423SMike Smith 	return_VOID;
25515e32d5dSMike Smith     }
25615e32d5dSMike Smith }
25715e32d5dSMike Smith 
25815e32d5dSMike Smith /*
25915e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
26015e32d5dSMike Smith  */
26115e32d5dSMike Smith static int
26215e32d5dSMike Smith acpi_probe(device_t dev)
26315e32d5dSMike Smith {
26415e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
26515e32d5dSMike Smith     char		buf[20];
266cb9b0d80SMike Smith     ACPI_STATUS		status;
26715e32d5dSMike Smith     int			error;
268fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
26915e32d5dSMike Smith 
270b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
271f9390180SMitsuru IWASAKI 
272f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
273f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
274be2b1797SNate Lawson 
275f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
276f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
277f9390180SMitsuru IWASAKI     }
278f9390180SMitsuru IWASAKI 
279cb9b0d80SMike Smith     ACPI_LOCK;
2800ae55423SMike Smith 
281b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
282be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
283be2b1797SNate Lawson 		      AcpiFormatException(status));
284cb9b0d80SMike Smith 	error = ENXIO;
285cb9b0d80SMike Smith     } else {
28615e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
28715e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
288cb9b0d80SMike Smith 	error = 0;
289cb9b0d80SMike Smith     }
290cb9b0d80SMike Smith     ACPI_UNLOCK;
291cb9b0d80SMike Smith     return_VALUE(error);
29215e32d5dSMike Smith }
29315e32d5dSMike Smith 
29415e32d5dSMike Smith static int
29515e32d5dSMike Smith acpi_attach(device_t dev)
29615e32d5dSMike Smith {
29715e32d5dSMike Smith     struct acpi_softc	*sc;
298cb9b0d80SMike Smith     ACPI_STATUS		status;
29915e32d5dSMike Smith     int			error;
30032d18aa5SMike Smith     UINT32		flags;
301498d464fSMitsuru IWASAKI     char		*env;
302d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
303d786139cSMaxime Henrion     char		*debugpoint;
30415e32d5dSMike Smith #endif
305fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
30615e32d5dSMike Smith 
307b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
308cb9b0d80SMike Smith     ACPI_LOCK;
30915e32d5dSMike Smith     sc = device_get_softc(dev);
31015e32d5dSMike Smith     bzero(sc, sizeof(*sc));
31115e32d5dSMike Smith     sc->acpi_dev = dev;
31215e32d5dSMike Smith 
313d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
314d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
315d786139cSMaxime Henrion     if (debugpoint) {
316d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
31715e32d5dSMike Smith 	    acpi_EnterDebugger();
318d786139cSMaxime Henrion 	freeenv(debugpoint);
319d786139cSMaxime Henrion     }
32015e32d5dSMike Smith #endif
32115e32d5dSMike Smith 
322be2b1797SNate Lawson     /* Install the default address space handlers. */
323cb9b0d80SMike Smith     error = ENXIO;
324be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
325be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
326be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
327be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
328be2b1797SNate Lawson 		      AcpiFormatException(status));
329cb9b0d80SMike Smith 	goto out;
33015e32d5dSMike Smith     }
331be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
332be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
333be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
334be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
335be2b1797SNate Lawson 		      AcpiFormatException(status));
336cb9b0d80SMike Smith 	goto out;
33715e32d5dSMike Smith     }
338be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
339be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
340be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
341be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
342be2b1797SNate Lawson 		      AcpiFormatException(status));
343cb9b0d80SMike Smith 	goto out;
34415e32d5dSMike Smith     }
34515e32d5dSMike Smith 
34615e32d5dSMike Smith     /*
34715e32d5dSMike Smith      * Bring ACPI fully online.
34815e32d5dSMike Smith      *
349be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
350be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
351be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
352be2b1797SNate Lawson      * object init pass.
35315e32d5dSMike Smith      *
35432d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
35532d18aa5SMike Smith      *
356be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
357be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
35815e32d5dSMike Smith      */
359d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
360d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
361d786139cSMaxime Henrion     if (debugpoint) {
362d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
36315e32d5dSMike Smith 	    acpi_EnterDebugger();
364d786139cSMaxime Henrion 	freeenv(debugpoint);
365d786139cSMaxime Henrion     }
36615e32d5dSMike Smith #endif
36732d18aa5SMike Smith     flags = 0;
368d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
36932d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
370b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
371be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
372be2b1797SNate Lawson 		      AcpiFormatException(status));
373cb9b0d80SMike Smith 	goto out;
37415e32d5dSMike Smith     }
37515e32d5dSMike Smith 
376f8335e3aSNate Lawson     /*
377f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
378f8335e3aSNate Lawson      * the namespace has been evaluated.
379f8335e3aSNate Lawson      */
380f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
381f8335e3aSNate Lawson 
382b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
383be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
384be2b1797SNate Lawson 		      AcpiFormatException(status));
385b69ed3f4SMitsuru IWASAKI 	goto out;
386b69ed3f4SMitsuru IWASAKI     }
387b69ed3f4SMitsuru IWASAKI 
38815e32d5dSMike Smith     /*
3891d073b1dSJohn Baldwin      * Setup our sysctl tree.
3901d073b1dSJohn Baldwin      *
3911d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3921d073b1dSJohn Baldwin      */
3931d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3941d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3951d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3961d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3971d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
398d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
399d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
400d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4011d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4021d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4031d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4041d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4051d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4061d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4071d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4081d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
409f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
410f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
411f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
412f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
413f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
414f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4152d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
416ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
417ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
418ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4191611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4201611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4211611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4222d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4232d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
424c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
425c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
426c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
427bf0c18ecSNate Lawson 
428bf0c18ecSNate Lawson     /*
429bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
430bf0c18ecSNate Lawson      * stabilize.
431bf0c18ecSNate Lawson      */
432bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
433c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
434931a10c9SMitsuru IWASAKI     sc->acpi_s4bios = 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 
4421d073b1dSJohn Baldwin     /*
44315e32d5dSMike Smith      * Dispatch the default sleep state to devices.
44415e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
44515e32d5dSMike Smith      */
44615e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
44715e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
44815e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
449f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
450f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
45115e32d5dSMike Smith 
45213d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
45315e32d5dSMike Smith 
45415e32d5dSMike Smith     /*
45515e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
45615e32d5dSMike Smith      */
457d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
458d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
459d786139cSMaxime Henrion     if (debugpoint) {
460d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
46115e32d5dSMike Smith 	    acpi_EnterDebugger();
462d786139cSMaxime Henrion 	freeenv(debugpoint);
463d786139cSMaxime Henrion     }
46415e32d5dSMike Smith #endif
46515e32d5dSMike Smith 
466be2b1797SNate Lawson     /* Register our shutdown handlers */
467be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
468be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
469be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
470be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
47115e32d5dSMike Smith 
47215e32d5dSMike Smith     /*
47315e32d5dSMike Smith      * Register our acpi event handlers.
47415e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
47515e32d5dSMike Smith      */
476be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
477be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
478be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
479be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
48015e32d5dSMike Smith 
481be2b1797SNate Lawson     /* Flag our initial states. */
48215e32d5dSMike Smith     sc->acpi_enabled = 1;
48315e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
484ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
48515e32d5dSMike Smith 
486be2b1797SNate Lawson     /* Create the control device */
487a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4882a4689e8SRobert Watson 			      "acpi");
48915e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
49015e32d5dSMike Smith 
491d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
492d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
493d786139cSMaxime Henrion     if (debugpoint) {
494be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
49515e32d5dSMike Smith 	    acpi_EnterDebugger();
496d786139cSMaxime Henrion 	freeenv(debugpoint);
497d786139cSMaxime Henrion     }
49815e32d5dSMike Smith #endif
499f86214b6SMitsuru IWASAKI 
50091da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
501be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
502c573e654SMitsuru IWASAKI 	goto out;
503c573e654SMitsuru IWASAKI #endif
504c573e654SMitsuru IWASAKI 
505be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
506f86214b6SMitsuru IWASAKI 	goto out;
507f86214b6SMitsuru IWASAKI 
508f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
509f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
510f9390180SMitsuru IWASAKI 
511eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
512eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
513eeb6dba2SJohn Baldwin 
514cb9b0d80SMike Smith     error = 0;
515cb9b0d80SMike Smith 
516cb9b0d80SMike Smith  out:
517cb9b0d80SMike Smith     ACPI_UNLOCK;
518cb9b0d80SMike Smith     return_VALUE (error);
51915e32d5dSMike Smith }
52015e32d5dSMike Smith 
52115e32d5dSMike Smith /*
52215e32d5dSMike Smith  * Handle a new device being added
52315e32d5dSMike Smith  */
52415e32d5dSMike Smith static device_t
52515e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
52615e32d5dSMike Smith {
52715e32d5dSMike Smith     struct acpi_device	*ad;
52815e32d5dSMike Smith     device_t		child;
52915e32d5dSMike Smith 
530be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
53115e32d5dSMike Smith 	return (NULL);
53215e32d5dSMike Smith 
53315e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
53415e32d5dSMike Smith 
53515e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
53615e32d5dSMike Smith     if (child != NULL)
53715e32d5dSMike Smith 	device_set_ivars(child, ad);
53815e32d5dSMike Smith     return (child);
53915e32d5dSMike Smith }
54015e32d5dSMike Smith 
54115e32d5dSMike Smith static int
54215e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
54315e32d5dSMike Smith {
54415e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
54515e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
54615e32d5dSMike Smith     int retval = 0;
54715e32d5dSMike Smith 
54815e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5499ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5509ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5519ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5529ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
55315e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
55415e32d5dSMike Smith 
55515e32d5dSMike Smith     return (retval);
55615e32d5dSMike Smith }
55715e32d5dSMike Smith 
55815e32d5dSMike Smith 
55915e32d5dSMike Smith /*
56015e32d5dSMike Smith  * Handle per-device ivars
56115e32d5dSMike Smith  */
56215e32d5dSMike Smith static int
56315e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
56415e32d5dSMike Smith {
56515e32d5dSMike Smith     struct acpi_device	*ad;
56615e32d5dSMike Smith 
56715e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
56815e32d5dSMike Smith 	printf("device has no ivars\n");
56915e32d5dSMike Smith 	return (ENOENT);
57015e32d5dSMike Smith     }
57115e32d5dSMike Smith 
572be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
57315e32d5dSMike Smith     switch(index) {
57415e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
57515e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
57615e32d5dSMike Smith 	break;
57715e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
57815e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
57915e32d5dSMike Smith 	break;
58015e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58115e32d5dSMike Smith 	*(void **)result = ad->ad_private;
58215e32d5dSMike Smith 	break;
58332d18aa5SMike Smith     case ISA_IVAR_VENDORID:
58432d18aa5SMike Smith     case ISA_IVAR_SERIAL:
58532d18aa5SMike Smith     case ISA_IVAR_COMPATID:
58632d18aa5SMike Smith 	*(int *)result = -1;
58732d18aa5SMike Smith 	break;
58832d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
58932d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59032d18aa5SMike Smith 	break;
59115e32d5dSMike Smith     default:
59215e32d5dSMike Smith 	return (ENOENT);
59315e32d5dSMike Smith     }
594be2b1797SNate Lawson 
59515e32d5dSMike Smith     return (0);
59615e32d5dSMike Smith }
59715e32d5dSMike Smith 
59815e32d5dSMike Smith static int
59915e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
60015e32d5dSMike Smith {
60115e32d5dSMike Smith     struct acpi_device	*ad;
60215e32d5dSMike Smith 
60315e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
60415e32d5dSMike Smith 	printf("device has no ivars\n");
60515e32d5dSMike Smith 	return (ENOENT);
60615e32d5dSMike Smith     }
60715e32d5dSMike Smith 
60815e32d5dSMike Smith     switch(index) {
60915e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61015e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
61115e32d5dSMike Smith 	break;
61215e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
61315e32d5dSMike Smith 	ad->ad_magic = (int)value;
61415e32d5dSMike Smith 	break;
61515e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
61615e32d5dSMike Smith 	ad->ad_private = (void *)value;
61715e32d5dSMike Smith 	break;
61815e32d5dSMike Smith     default:
6192ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
62015e32d5dSMike Smith 	return (ENOENT);
62115e32d5dSMike Smith     }
622be2b1797SNate Lawson 
62315e32d5dSMike Smith     return (0);
62415e32d5dSMike Smith }
62515e32d5dSMike Smith 
626be2b1797SNate Lawson ACPI_HANDLE
627be2b1797SNate Lawson acpi_get_handle(device_t dev)
628be2b1797SNate Lawson {
629be2b1797SNate Lawson     uintptr_t up;
630be2b1797SNate Lawson     ACPI_HANDLE	h;
631be2b1797SNate Lawson 
632be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
633be2b1797SNate Lawson 	return(NULL);
634be2b1797SNate Lawson     h = (ACPI_HANDLE)up;
635be2b1797SNate Lawson     return (h);
636be2b1797SNate Lawson }
637be2b1797SNate Lawson 
638be2b1797SNate Lawson int
639be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h)
640be2b1797SNate Lawson {
641be2b1797SNate Lawson     uintptr_t up;
642be2b1797SNate Lawson 
643be2b1797SNate Lawson     up = (uintptr_t)h;
644be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
645be2b1797SNate Lawson }
646be2b1797SNate Lawson 
647be2b1797SNate Lawson int
648be2b1797SNate Lawson acpi_get_magic(device_t dev)
649be2b1797SNate Lawson {
650be2b1797SNate Lawson     uintptr_t up;
651be2b1797SNate Lawson     int	m;
652be2b1797SNate Lawson 
653be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
654be2b1797SNate Lawson 	return(0);
655be2b1797SNate Lawson     m = (int)up;
656be2b1797SNate Lawson     return (m);
657be2b1797SNate Lawson }
658be2b1797SNate Lawson 
659be2b1797SNate Lawson int
660be2b1797SNate Lawson acpi_set_magic(device_t dev, int m)
661be2b1797SNate Lawson {
662be2b1797SNate Lawson     uintptr_t up;
663be2b1797SNate Lawson 
664be2b1797SNate Lawson     up = (uintptr_t)m;
665be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
666be2b1797SNate Lawson }
667be2b1797SNate Lawson 
668be2b1797SNate Lawson void *
669be2b1797SNate Lawson acpi_get_private(device_t dev)
670be2b1797SNate Lawson {
671be2b1797SNate Lawson     uintptr_t up;
672be2b1797SNate Lawson     void *p;
673be2b1797SNate Lawson 
674be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
675be2b1797SNate Lawson 	return (NULL);
676be2b1797SNate Lawson     p = (void *)up;
677be2b1797SNate Lawson     return (p);
678be2b1797SNate Lawson }
679be2b1797SNate Lawson 
680be2b1797SNate Lawson int
681be2b1797SNate Lawson acpi_set_private(device_t dev, void *p)
682be2b1797SNate Lawson {
683be2b1797SNate Lawson     uintptr_t up;
684be2b1797SNate Lawson 
685be2b1797SNate Lawson     up = (uintptr_t)p;
686be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
687be2b1797SNate Lawson }
688be2b1797SNate Lawson 
689be2b1797SNate Lawson ACPI_OBJECT_TYPE
690be2b1797SNate Lawson acpi_get_type(device_t dev)
691be2b1797SNate Lawson {
692be2b1797SNate Lawson     ACPI_HANDLE		h;
693be2b1797SNate Lawson     ACPI_OBJECT_TYPE	t;
694be2b1797SNate Lawson 
695be2b1797SNate Lawson     if ((h = acpi_get_handle(dev)) == NULL)
696be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
697be2b1797SNate Lawson     if (AcpiGetType(h, &t) != AE_OK)
698be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
699be2b1797SNate Lawson     return (t);
700be2b1797SNate Lawson }
701be2b1797SNate Lawson 
70215e32d5dSMike Smith /*
70315e32d5dSMike Smith  * Handle child resource allocation/removal
70415e32d5dSMike Smith  */
70515e32d5dSMike Smith static int
706be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
707be2b1797SNate Lawson 		  u_long start, u_long count)
70815e32d5dSMike Smith {
70915e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
71015e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
71115e32d5dSMike Smith 
71215e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
71315e32d5dSMike Smith 
71415e32d5dSMike Smith     return(0);
71515e32d5dSMike Smith }
71615e32d5dSMike Smith 
71715e32d5dSMike Smith static int
718be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
719be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
72015e32d5dSMike Smith {
72115e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
72215e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
72315e32d5dSMike Smith     struct resource_list_entry	*rle;
72415e32d5dSMike Smith 
72515e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
72615e32d5dSMike Smith     if (!rle)
72715e32d5dSMike Smith 	return(ENOENT);
72815e32d5dSMike Smith 
72915e32d5dSMike Smith     if (startp)
73015e32d5dSMike Smith 	*startp = rle->start;
73115e32d5dSMike Smith     if (countp)
73215e32d5dSMike Smith 	*countp = rle->count;
73315e32d5dSMike Smith 
73415e32d5dSMike Smith     return (0);
73515e32d5dSMike Smith }
73615e32d5dSMike Smith 
73715e32d5dSMike Smith static struct resource *
73815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
73915e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
74015e32d5dSMike Smith {
74115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
74215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
74315e32d5dSMike Smith 
744be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
745be2b1797SNate Lawson 	    flags));
74615e32d5dSMike Smith }
74715e32d5dSMike Smith 
74815e32d5dSMike Smith static int
74915e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
75015e32d5dSMike Smith {
75115e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
75215e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
75315e32d5dSMike Smith 
75415e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
75515e32d5dSMike Smith }
75615e32d5dSMike Smith 
75715e32d5dSMike Smith /*
758bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
759bc0f2195SMike Smith  */
760bc0f2195SMike Smith #define PNP_EISAID(s)				\
761bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
762bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
763bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
764bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
765bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
766bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
767bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
768bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
769bc0f2195SMike Smith 
77032d18aa5SMike Smith static u_int32_t
77132d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
772bc0f2195SMike Smith {
773bc0f2195SMike Smith     ACPI_HANDLE		h;
774bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
7756fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
776bc0f2195SMike Smith     ACPI_STATUS		error;
777bc0f2195SMike Smith     u_int32_t		pnpid;
778fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
77932d18aa5SMike Smith 
780b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
78132d18aa5SMike Smith 
78232d18aa5SMike Smith     pnpid = 0;
78332d18aa5SMike Smith     ACPI_LOCK;
78432d18aa5SMike Smith 
7856fca9360SNate Lawson     /* Fetch and validate the HID. */
78632d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
78732d18aa5SMike Smith 	goto out;
7886fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
7896fca9360SNate Lawson     if (ACPI_FAILURE(error))
79032d18aa5SMike Smith 	goto out;
7916fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) == 0)
79232d18aa5SMike Smith 	goto out;
79332d18aa5SMike Smith 
7946fca9360SNate Lawson     pnpid = PNP_EISAID(devinfo.HardwareId.Value);
795be2b1797SNate Lawson 
79632d18aa5SMike Smith out:
79732d18aa5SMike Smith     ACPI_UNLOCK;
79832d18aa5SMike Smith     return_VALUE (pnpid);
79932d18aa5SMike Smith }
80032d18aa5SMike Smith 
801b2566207STakanori Watanabe static u_int32_t
802b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev)
803b2566207STakanori Watanabe {
804b2566207STakanori Watanabe     ACPI_HANDLE		h;
805b2566207STakanori Watanabe     ACPI_STATUS		error;
806b2566207STakanori Watanabe     u_int32_t		pnpid;
807b2566207STakanori Watanabe     ACPI_LOCK_DECL;
808b2566207STakanori Watanabe 
809b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
810b2566207STakanori Watanabe 
811b2566207STakanori Watanabe     pnpid = 0;
812b2566207STakanori Watanabe     ACPI_LOCK;
813b2566207STakanori Watanabe 
814be2b1797SNate Lawson     /* Fetch and validate the HID */
815b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
816b2566207STakanori Watanabe 	goto out;
817b2566207STakanori Watanabe     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
818b2566207STakanori Watanabe 	goto out;
819b2566207STakanori Watanabe 
820b2566207STakanori Watanabe out:
821b2566207STakanori Watanabe     ACPI_UNLOCK;
822b2566207STakanori Watanabe     return_VALUE (pnpid);
823b2566207STakanori Watanabe }
824b2566207STakanori Watanabe 
825b2566207STakanori Watanabe 
82632d18aa5SMike Smith static int
82732d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
82832d18aa5SMike Smith {
829bc0f2195SMike Smith     int			result;
830b2566207STakanori Watanabe     u_int32_t		lid, cid;
831bc0f2195SMike Smith 
832b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
833bc0f2195SMike Smith 
834bc0f2195SMike Smith     /*
835bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
836bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
837bc0f2195SMike Smith      * that to happen, so don't ever return it.
838bc0f2195SMike Smith      */
839bc0f2195SMike Smith     result = ENXIO;
840bc0f2195SMike Smith 
841be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
842b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
843b2566207STakanori Watanabe     cid = acpi_isa_get_compatid(child);
844bc0f2195SMike Smith     while (ids && ids->ip_id) {
845b2566207STakanori Watanabe 	if (lid == ids->ip_id || cid == ids->ip_id) {
846bc0f2195SMike Smith 	    result = 0;
847bc0f2195SMike Smith 	    goto out;
848bc0f2195SMike Smith 	}
849bc0f2195SMike Smith 	ids++;
850bc0f2195SMike Smith     }
851be2b1797SNate Lawson 
852bc0f2195SMike Smith  out:
853bc0f2195SMike Smith     return_VALUE(result);
854bc0f2195SMike Smith }
855bc0f2195SMike Smith 
856bc0f2195SMike Smith /*
85715e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
85815e32d5dSMike Smith  *
859be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
860be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
86115e32d5dSMike Smith  */
86215e32d5dSMike Smith static void
86315e32d5dSMike Smith acpi_probe_children(device_t bus)
86415e32d5dSMike Smith {
86515e32d5dSMike Smith     ACPI_HANDLE	parent;
866be2b1797SNate Lawson     ACPI_STATUS	status;
8677d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
86815e32d5dSMike Smith     int		i;
86915e32d5dSMike Smith 
870b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
871cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
8720ae55423SMike Smith 
873be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
8744c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
87515e32d5dSMike Smith     bus_generic_probe(bus);
87615e32d5dSMike Smith 
87715e32d5dSMike Smith     /*
87815e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
87915e32d5dSMike Smith      * we find.
88015e32d5dSMike Smith      *
88115e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
882be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
883be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
884be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
88515e32d5dSMike Smith      */
8864c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
887be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
888be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
889be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
890be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
891be2b1797SNate Lawson 			      bus, NULL);
892be2b1797SNate Lawson 	}
893be2b1797SNate Lawson     }
89415e32d5dSMike Smith 
89515e32d5dSMike Smith     /*
89615e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
89715e32d5dSMike Smith      */
8984c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
89915e32d5dSMike Smith     bus_generic_attach(bus);
90015e32d5dSMike Smith 
90115e32d5dSMike Smith     /*
90215e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
90315e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
90415e32d5dSMike Smith      */
9054c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
90615e32d5dSMike Smith     bus_generic_attach(bus);
9070ae55423SMike Smith 
908bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
9090ae55423SMike Smith     return_VOID;
91015e32d5dSMike Smith }
91115e32d5dSMike Smith 
91215e32d5dSMike Smith /*
91315e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
91415e32d5dSMike Smith  * it.
91515e32d5dSMike Smith  */
91615e32d5dSMike Smith static ACPI_STATUS
91715e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
91815e32d5dSMike Smith {
91915e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
92015e32d5dSMike Smith     device_t		child, bus = (device_t)context;
92115e32d5dSMike Smith 
922b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
9230ae55423SMike Smith 
924be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
9250ae55423SMike Smith     if (acpi_avoid(handle))
9260ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
9270ae55423SMike Smith 
928b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
92915e32d5dSMike Smith 	switch(type) {
93015e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
93115e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
93215e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
93315e32d5dSMike Smith 	case ACPI_TYPE_POWER:
9340ae55423SMike Smith 	    if (acpi_disabled("children"))
9350ae55423SMike Smith 		break;
936be2b1797SNate Lawson 
93715e32d5dSMike Smith 	    /*
93815e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
93915e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
94015e32d5dSMike Smith 	     */
941be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
942be2b1797SNate Lawson 			     acpi_name(handle)));
94315e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
944bc0f2195SMike Smith 	    if (child == NULL)
945bc0f2195SMike Smith 		break;
94615e32d5dSMike Smith 	    acpi_set_handle(child, handle);
947bc0f2195SMike Smith 
948bc0f2195SMike Smith 	    /*
949b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
950b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
951b519f9d6SMike Smith 	     * the handle, but we don't probe it).
952b519f9d6SMike Smith 	     */
953be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
954b519f9d6SMike Smith 		device_disable(child);
955b519f9d6SMike Smith 		break;
956b519f9d6SMike Smith 	    }
957b519f9d6SMike Smith 
958b519f9d6SMike Smith 	    /*
959bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
960bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
961bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
962bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
963bc0f2195SMike Smith 	     * device not to have any resources.
964bc0f2195SMike Smith 	     */
965bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
966bc0f2195SMike Smith 
967be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
968b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
9690ae55423SMike Smith 	    break;
97015e32d5dSMike Smith 	}
97115e32d5dSMike Smith     }
972be2b1797SNate Lawson 
9730ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
97415e32d5dSMike Smith }
97515e32d5dSMike Smith 
97615e32d5dSMike Smith static void
97715e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
97815e32d5dSMike Smith {
979c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
980c6a78e98STakanori Watanabe 
981cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
982cb9b0d80SMike Smith 
98315e32d5dSMike Smith     /*
9840ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
98515e32d5dSMike Smith      * will be turned on again on some laptops.
98615e32d5dSMike Smith      *
98715e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
98815e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
98915e32d5dSMike Smith      *     shutdown process.
99015e32d5dSMike Smith      */
991c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
992c6a78e98STakanori Watanabe 	acpi_Disable(sc);
99315e32d5dSMike Smith }
99415e32d5dSMike Smith 
99515e32d5dSMike Smith static void
99615e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
99715e32d5dSMike Smith {
99815e32d5dSMike Smith     ACPI_STATUS	status;
99915e32d5dSMike Smith 
1000cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1001cb9b0d80SMike Smith 
1002be2b1797SNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1003e1a90ae1SNate Lawson 	printf("Powering system off using ACPI\n");
1004be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(acpi_off_state);
1005be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1006b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1007b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
1008b9c780d6SMitsuru IWASAKI 	    return;
1009b9c780d6SMitsuru IWASAKI 	}
1010be2b1797SNate Lawson 	status = AcpiEnterSleepState(acpi_off_state);
1011be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1012bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
101315e32d5dSMike Smith 	} else {
101415e32d5dSMike Smith 	    DELAY(1000000);
101515e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
101615e32d5dSMike Smith 	}
1017494ae560SMitsuru IWASAKI     } else {
1018e1a90ae1SNate Lawson 	printf("Shutting down ACPI\n");
1019494ae560SMitsuru IWASAKI 	AcpiTerminate();
102015e32d5dSMike Smith     }
102115e32d5dSMike Smith }
102215e32d5dSMike Smith 
102313d4f7baSMitsuru IWASAKI static void
102413d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
102513d4f7baSMitsuru IWASAKI {
102613d4f7baSMitsuru IWASAKI     static int	first_time = 1;
102713d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
102813d4f7baSMitsuru IWASAKI 
1029cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1030cb9b0d80SMike Smith 
103113d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1032be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
103359ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0);
103459ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
103513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1036be2b1797SNate Lawson 				     acpi_eventhandler_power_button_for_sleep,
1037be2b1797SNate Lawson 				     sc);
1038be2b1797SNate Lawson 	if (first_time)
103913d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
104013d4f7baSMitsuru IWASAKI     }
1041be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
104259ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0);
104359ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
104413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1045be2b1797SNate Lawson 				     acpi_eventhandler_sleep_button_for_sleep,
1046be2b1797SNate Lawson 				     sc);
1047be2b1797SNate Lawson 	if (first_time)
104813d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
104913d4f7baSMitsuru IWASAKI     }
105013d4f7baSMitsuru IWASAKI 
105113d4f7baSMitsuru IWASAKI     first_time = 0;
105213d4f7baSMitsuru IWASAKI }
105313d4f7baSMitsuru IWASAKI 
105415e32d5dSMike Smith /*
1055c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1056c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1057c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1058c5ba0be4SMike Smith  */
1059c5ba0be4SMike Smith BOOLEAN
1060c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1061c5ba0be4SMike Smith {
1062c5ba0be4SMike Smith     ACPI_HANDLE		h;
1063c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
10646fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1065c5ba0be4SMike Smith     ACPI_STATUS		error;
1066c5ba0be4SMike Smith 
1067cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1068cb9b0d80SMike Smith 
1069c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1070c5ba0be4SMike Smith 	return (FALSE);
10716fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
10726fca9360SNate Lawson     if (ACPI_FAILURE(error))
1073c5ba0be4SMike Smith 	return (FALSE);
1074be2b1797SNate Lawson 
1075be2b1797SNate Lawson     /* If no _STA method, must be present */
10766fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
107743896e91SMike Smith 	return (TRUE);
1078be2b1797SNate Lawson 
1079be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
108043896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
1081c5ba0be4SMike Smith 	return (TRUE);
1082be2b1797SNate Lawson 
1083c5ba0be4SMike Smith     return (FALSE);
1084c5ba0be4SMike Smith }
1085c5ba0be4SMike Smith 
1086c5ba0be4SMike Smith /*
1087b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1088b53f2771SMike Smith  */
1089b53f2771SMike Smith BOOLEAN
1090b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1091b53f2771SMike Smith {
1092b53f2771SMike Smith     ACPI_HANDLE		h;
1093b53f2771SMike Smith     ACPI_DEVICE_INFO	devinfo;
10946fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
1095b53f2771SMike Smith     ACPI_STATUS		error;
1096b53f2771SMike Smith 
1097b53f2771SMike Smith     ACPI_ASSERTLOCK;
1098b53f2771SMike Smith 
1099b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1100b53f2771SMike Smith 	return (FALSE);
11016fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11026fca9360SNate Lawson     if (ACPI_FAILURE(error))
1103b53f2771SMike Smith 	return (FALSE);
1104be2b1797SNate Lawson 
1105be2b1797SNate Lawson     /* If no _STA method, must be present */
11066fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_STA) == 0)
1107b53f2771SMike Smith 	return (TRUE);
1108be2b1797SNate Lawson 
1109be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
1110b53f2771SMike Smith     if ((devinfo.CurrentStatus & 0x19) == 0x19)
1111b53f2771SMike Smith 	return (TRUE);
1112be2b1797SNate Lawson 
1113b53f2771SMike Smith     return (FALSE);
1114b53f2771SMike Smith }
1115b53f2771SMike Smith 
1116b53f2771SMike Smith /*
111715e32d5dSMike Smith  * Match a HID string against a device
111815e32d5dSMike Smith  */
111915e32d5dSMike Smith BOOLEAN
112015e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
112115e32d5dSMike Smith {
112215e32d5dSMike Smith     ACPI_HANDLE		h;
112315e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
11246fca9360SNate Lawson     ACPI_BUFFER		buf = {sizeof(devinfo), &devinfo};
112515e32d5dSMike Smith     ACPI_STATUS		error;
1126ed136da6SDoug Rabson     int			cid;
112715e32d5dSMike Smith 
1128cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1129cb9b0d80SMike Smith 
11304d332989SMike Smith     if (hid == NULL)
113115e32d5dSMike Smith 	return (FALSE);
113215e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
113315e32d5dSMike Smith 	return (FALSE);
11346fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11356fca9360SNate Lawson     if (ACPI_FAILURE(error))
113615e32d5dSMike Smith 	return (FALSE);
11376fca9360SNate Lawson     if ((devinfo.Valid & ACPI_VALID_HID) != 0 &&
11386fca9360SNate Lawson 	strcmp(hid, devinfo.HardwareId.Value) == 0)
113915e32d5dSMike Smith 	return (TRUE);
1140be2b1797SNate Lawson 
1141b53f2771SMike Smith     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
1142ed136da6SDoug Rabson 	return (FALSE);
1143ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
1144ed136da6SDoug Rabson 	return (TRUE);
1145be2b1797SNate Lawson 
114615e32d5dSMike Smith     return (FALSE);
114715e32d5dSMike Smith }
114815e32d5dSMike Smith 
114915e32d5dSMike Smith /*
1150c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1151c5ba0be4SMike Smith  * or one if its parents.
1152c5ba0be4SMike Smith  */
1153c5ba0be4SMike Smith ACPI_STATUS
1154c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1155c5ba0be4SMike Smith {
1156c5ba0be4SMike Smith     ACPI_HANDLE		r;
1157c5ba0be4SMike Smith     ACPI_STATUS		status;
1158c5ba0be4SMike Smith 
1159cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1160cb9b0d80SMike Smith 
1161be2b1797SNate Lawson     /* Walk back up the tree to the root */
1162c5ba0be4SMike Smith     for (;;) {
1163be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1164be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1165c5ba0be4SMike Smith 	    *result = r;
1166c5ba0be4SMike Smith 	    return (AE_OK);
1167c5ba0be4SMike Smith 	}
1168c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1169c5ba0be4SMike Smith 	    return (AE_OK);
1170b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1171c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1172c5ba0be4SMike Smith 	parent = r;
1173c5ba0be4SMike Smith     }
1174c5ba0be4SMike Smith }
1175c5ba0be4SMike Smith 
1176c5ba0be4SMike Smith /*
1177c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1178c5ba0be4SMike Smith  */
1179c5ba0be4SMike Smith ACPI_BUFFER *
1180c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1181c5ba0be4SMike Smith {
1182c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1183c5ba0be4SMike Smith 
1184c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1185c5ba0be4SMike Smith 	return (NULL);
1186c5ba0be4SMike Smith     buf->Length = size;
1187c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1188c5ba0be4SMike Smith     return (buf);
1189c5ba0be4SMike Smith }
1190c5ba0be4SMike Smith 
1191c5ba0be4SMike Smith /*
1192c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1193c5ba0be4SMike Smith  */
1194c5ba0be4SMike Smith ACPI_STATUS
1195c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1196c5ba0be4SMike Smith {
1197be2b1797SNate Lawson     ACPI_STATUS	status;
1198c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1199c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1200c5ba0be4SMike Smith 
1201cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1202cb9b0d80SMike Smith 
1203c5ba0be4SMike Smith     if (handle == NULL)
1204c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
12050c7da7acSJohn Baldwin 
12060c7da7acSJohn Baldwin     /*
12070c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
12080c7da7acSJohn Baldwin      * a method that will return an Integer.
12090c7da7acSJohn Baldwin      */
1210c5ba0be4SMike Smith     buf.Pointer = &param;
1211c5ba0be4SMike Smith     buf.Length = sizeof(param);
1212be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1213be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1214be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1215c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1216be2b1797SNate Lawson 	else
1217be2b1797SNate Lawson 	    status = AE_TYPE;
1218c5ba0be4SMike Smith     }
12190c7da7acSJohn Baldwin 
12200c7da7acSJohn Baldwin     /*
12210c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
12220c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
12230c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
12240c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
12250c7da7acSJohn Baldwin      *
12260c7da7acSJohn Baldwin      * This is a hack.
12270c7da7acSJohn Baldwin      */
1228be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1229b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1230be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
12310c7da7acSJohn Baldwin 	} else {
1232be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1233be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1234be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
12350c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
12360c7da7acSJohn Baldwin 	}
1237f97739daSNate Lawson     }
1238be2b1797SNate Lawson     return (status);
1239c5ba0be4SMike Smith }
1240c5ba0be4SMike Smith 
1241c573e654SMitsuru IWASAKI ACPI_STATUS
1242c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1243c573e654SMitsuru IWASAKI {
1244c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1245c573e654SMitsuru IWASAKI     int		i;
1246c573e654SMitsuru IWASAKI 
1247c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1248c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1249c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1250c573e654SMitsuru IWASAKI 	return (AE_OK);
1251c573e654SMitsuru IWASAKI     }
1252c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1253c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1254c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1255c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1256be2b1797SNate Lawson 
1257c573e654SMitsuru IWASAKI     *number = 0;
1258c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1259c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1260c573e654SMitsuru IWASAKI     return (AE_OK);
1261c573e654SMitsuru IWASAKI }
1262c573e654SMitsuru IWASAKI 
1263c5ba0be4SMike Smith /*
1264c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1265c5ba0be4SMike Smith  * function for each element.
1266c5ba0be4SMike Smith  *
1267c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1268c5ba0be4SMike Smith  */
1269c5ba0be4SMike Smith ACPI_STATUS
1270be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1271be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1272c5ba0be4SMike Smith {
1273c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1274c5ba0be4SMike Smith     int		i;
1275c5ba0be4SMike Smith 
1276be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1277c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1278c5ba0be4SMike Smith 
1279be2b1797SNate Lawson     /* Iterate over components */
1280be2b1797SNate Lawson     i = 0;
1281be2b1797SNate Lawson     comp = pkg->Package.Elements;
1282be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1283c5ba0be4SMike Smith 	func(comp, arg);
1284c5ba0be4SMike Smith 
1285c5ba0be4SMike Smith     return (AE_OK);
1286c5ba0be4SMike Smith }
1287c5ba0be4SMike Smith 
12886f69255bSMike Smith /*
12896f69255bSMike Smith  * Find the (index)th resource object in a set.
12906f69255bSMike Smith  */
12916f69255bSMike Smith ACPI_STATUS
12926d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
12936f69255bSMike Smith {
12946d63101aSMike Smith     ACPI_RESOURCE	*rp;
12956f69255bSMike Smith     int			i;
12966f69255bSMike Smith 
12976d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12986f69255bSMike Smith     i = index;
12996d63101aSMike Smith     while (i-- > 0) {
1300be2b1797SNate Lawson 	/* Range check */
13016d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
13026f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1303be2b1797SNate Lawson 
1304be2b1797SNate Lawson 	/* Check for terminator */
1305be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
13066d63101aSMike Smith 	    return (AE_NOT_FOUND);
13076d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
13086f69255bSMike Smith     }
13096f69255bSMike Smith     if (resp != NULL)
13106d63101aSMike Smith 	*resp = rp;
1311be2b1797SNate Lawson 
13126d63101aSMike Smith     return (AE_OK);
13136d63101aSMike Smith }
13146d63101aSMike Smith 
13156d63101aSMike Smith /*
13166d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
13176d63101aSMike Smith  *
13186d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
13196d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
13206d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
13216d63101aSMike Smith  * resources.
13226d63101aSMike Smith  */
13236d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
13246d63101aSMike Smith 
13256d63101aSMike Smith ACPI_STATUS
13266d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
13276d63101aSMike Smith {
13286d63101aSMike Smith     ACPI_RESOURCE	*rp;
13296d63101aSMike Smith     void		*newp;
13306d63101aSMike Smith 
1331be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
13326d63101aSMike Smith     if (buf->Pointer == NULL) {
13336d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
13346d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
13356d63101aSMike Smith 	    return (AE_NO_MEMORY);
13366d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
13376d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
13386d63101aSMike Smith 	rp->Length = 0;
13396d63101aSMike Smith     }
13406d63101aSMike Smith     if (res == NULL)
13416d63101aSMike Smith 	return (AE_OK);
13426d63101aSMike Smith 
13436d63101aSMike Smith     /*
13446d63101aSMike Smith      * Scan the current buffer looking for the terminator.
13456d63101aSMike Smith      * This will either find the terminator or hit the end
13466d63101aSMike Smith      * of the buffer and return an error.
13476d63101aSMike Smith      */
13486d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
13496d63101aSMike Smith     for (;;) {
1350be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
13516d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
13526d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1353be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
13546d63101aSMike Smith 	    break;
13556d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
13566d63101aSMike Smith     }
13576d63101aSMike Smith 
13586d63101aSMike Smith     /*
13596d63101aSMike Smith      * Check the size of the buffer and expand if required.
13606d63101aSMike Smith      *
13616d63101aSMike Smith      * Required size is:
13626d63101aSMike Smith      *	size of existing resources before terminator +
13636d63101aSMike Smith      *	size of new resource and header +
13646d63101aSMike Smith      * 	size of terminator.
13656d63101aSMike Smith      *
13666d63101aSMike Smith      * Note that this loop should really only run once, unless
13676d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
13686d63101aSMike Smith      */
13696d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
13706d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
13716d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
13726d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
13736d63101aSMike Smith 	    return (AE_NO_MEMORY);
13746d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1375a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1376a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
13776d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
13786d63101aSMike Smith 	buf->Pointer = newp;
13796d63101aSMike Smith 	buf->Length += buf->Length;
13806d63101aSMike Smith     }
13816d63101aSMike Smith 
1382be2b1797SNate Lawson     /* Insert the new resource. */
13836d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
13846d63101aSMike Smith 
1385be2b1797SNate Lawson     /* And add the terminator. */
13866d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
13876d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
13886d63101aSMike Smith     rp->Length = 0;
13896d63101aSMike Smith 
13906f69255bSMike Smith     return (AE_OK);
13916f69255bSMike Smith }
1392c5ba0be4SMike Smith 
1393da14ac9fSJohn Baldwin /*
1394da14ac9fSJohn Baldwin  * Set interrupt model.
1395da14ac9fSJohn Baldwin  */
1396da14ac9fSJohn Baldwin ACPI_STATUS
1397da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1398da14ac9fSJohn Baldwin {
1399da14ac9fSJohn Baldwin     ACPI_OBJECT_LIST ArgList;
1400da14ac9fSJohn Baldwin     ACPI_OBJECT Arg;
1401da14ac9fSJohn Baldwin 
1402da14ac9fSJohn Baldwin     Arg.Type = ACPI_TYPE_INTEGER;
1403da14ac9fSJohn Baldwin     Arg.Integer.Value = model;
1404da14ac9fSJohn Baldwin     ArgList.Count = 1;
1405da14ac9fSJohn Baldwin     ArgList.Pointer = &Arg;
1406da14ac9fSJohn Baldwin     return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1407da14ac9fSJohn Baldwin }
1408da14ac9fSJohn Baldwin 
1409ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1410ece50487SMitsuru IWASAKI 
1411ece50487SMitsuru IWASAKI static void
1412ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1413ece50487SMitsuru IWASAKI {
1414ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1415ece50487SMitsuru IWASAKI }
1416c30382dfSMitsuru IWASAKI 
141715e32d5dSMike Smith /*
141815e32d5dSMike Smith  * Set the system sleep state
141915e32d5dSMike Smith  *
1420be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
142115e32d5dSMike Smith  */
142215e32d5dSMike Smith ACPI_STATUS
142315e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
142415e32d5dSMike Smith {
142515e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
142656d8cb57SMitsuru IWASAKI     UINT8	TypeA;
142756d8cb57SMitsuru IWASAKI     UINT8	TypeB;
142815e32d5dSMike Smith 
1429b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1430cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14310ae55423SMike Smith 
14326397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
14336397624dSMitsuru IWASAKI 	return_ACPI_STATUS (AE_BAD_PARAMETER);	/* avoid reentry */
14346397624dSMitsuru IWASAKI 
1435ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1436ece50487SMitsuru IWASAKI 	return_ACPI_STATUS(AE_OK);
1437ece50487SMitsuru IWASAKI 
143815e32d5dSMike Smith     switch (state) {
143915e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
1440be2b1797SNate Lawson 	status = AcpiEnterSleepState((UINT8)state);
1441be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1442be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1443be2b1797SNate Lawson 			  AcpiFormatException(status));
144415e32d5dSMike Smith 	}
144515e32d5dSMike Smith 	break;
144615e32d5dSMike Smith     case ACPI_STATE_S1:
14476161544cSTakanori Watanabe     case ACPI_STATE_S2:
14486161544cSTakanori Watanabe     case ACPI_STATE_S3:
14496161544cSTakanori Watanabe     case ACPI_STATE_S4:
1450be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
1451be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1452be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1453be2b1797SNate Lawson 			  AcpiFormatException(status));
145456d8cb57SMitsuru IWASAKI 	    break;
145556d8cb57SMitsuru IWASAKI 	}
145656d8cb57SMitsuru IWASAKI 
1457a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1458a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1459a1fccb47SMitsuru IWASAKI 
1460be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
146115e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
146215e32d5dSMike Smith 	    /*
146315e32d5dSMike Smith 	     * Re-wake the system.
146415e32d5dSMike Smith 	     *
146515e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
146615e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
146715e32d5dSMike Smith 	     *     current bus interface does not provide for this.
146815e32d5dSMike Smith 	     */
146915e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
14700ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
147115e32d5dSMike Smith 	}
1472b9c780d6SMitsuru IWASAKI 
1473be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1474be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1475b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1476b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1477b9c780d6SMitsuru IWASAKI 	    break;
1478b9c780d6SMitsuru IWASAKI 	}
1479b9c780d6SMitsuru IWASAKI 
1480be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1481ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1482ff01efb5SMitsuru IWASAKI 
14836161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
14846161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
14856161544cSTakanori Watanabe 
1486be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
148759ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
148859ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
148959ddeb18SNate Lawson 
14906161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1491a1fccb47SMitsuru IWASAKI 	    }
14926161544cSTakanori Watanabe 
14936161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1494be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1495964679ceSMitsuru IWASAKI 		AcpiEnable();
14966161544cSTakanori Watanabe 	} else {
1497be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1498be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1499be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1500be2b1797SNate Lawson 			      AcpiFormatException(status));
1501c30382dfSMitsuru IWASAKI 		break;
150215e32d5dSMike Smith 	    }
15036161544cSTakanori Watanabe 	}
1504ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
150515e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
150615e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
150713d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
150815e32d5dSMike Smith 	break;
150915e32d5dSMike Smith     case ACPI_STATE_S5:
151015e32d5dSMike Smith 	/*
151115e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
151215e32d5dSMike Smith 	 * shutdown handlers.
151315e32d5dSMike Smith 	 */
151415e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
151515e32d5dSMike Smith 	break;
151615e32d5dSMike Smith     default:
151715e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
151815e32d5dSMike Smith 	break;
151915e32d5dSMike Smith     }
1520ece50487SMitsuru IWASAKI 
1521ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1522ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1523ece50487SMitsuru IWASAKI 
15240ae55423SMike Smith     return_ACPI_STATUS (status);
152515e32d5dSMike Smith }
152615e32d5dSMike Smith 
152715e32d5dSMike Smith /*
152815e32d5dSMike Smith  * Enable/Disable ACPI
152915e32d5dSMike Smith  */
153015e32d5dSMike Smith ACPI_STATUS
153115e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
153215e32d5dSMike Smith {
153315e32d5dSMike Smith     ACPI_STATUS	status;
153415e32d5dSMike Smith     u_int32_t	flags;
153515e32d5dSMike Smith 
1536b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1537cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
15380ae55423SMike Smith 
153915e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
154015e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1541be2b1797SNate Lawson     if (!sc->acpi_enabled)
154215e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1543be2b1797SNate Lawson     else
154415e32d5dSMike Smith 	status = AE_OK;
1545be2b1797SNate Lawson 
154615e32d5dSMike Smith     if (status == AE_OK)
154715e32d5dSMike Smith 	sc->acpi_enabled = 1;
1548be2b1797SNate Lawson 
15490ae55423SMike Smith     return_ACPI_STATUS (status);
155015e32d5dSMike Smith }
155115e32d5dSMike Smith 
155215e32d5dSMike Smith ACPI_STATUS
155315e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
155415e32d5dSMike Smith {
155515e32d5dSMike Smith     ACPI_STATUS	status;
155615e32d5dSMike Smith 
1557b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1558cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
15590ae55423SMike Smith 
1560be2b1797SNate Lawson     if (sc->acpi_enabled)
156115e32d5dSMike Smith 	status = AcpiDisable();
1562be2b1797SNate Lawson     else
156315e32d5dSMike Smith 	status = AE_OK;
1564be2b1797SNate Lawson 
156515e32d5dSMike Smith     if (status == AE_OK)
156615e32d5dSMike Smith 	sc->acpi_enabled = 0;
1567be2b1797SNate Lawson 
15680ae55423SMike Smith     return_ACPI_STATUS (status);
156915e32d5dSMike Smith }
157015e32d5dSMike Smith 
157115e32d5dSMike Smith /*
157215e32d5dSMike Smith  * ACPI Event Handlers
157315e32d5dSMike Smith  */
157415e32d5dSMike Smith 
157515e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
157615e32d5dSMike Smith 
157715e32d5dSMike Smith static void
157815e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
157915e32d5dSMike Smith {
1580fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1581b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
158215e32d5dSMike Smith 
1583cb9b0d80SMike Smith     ACPI_LOCK;
1584a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
158515e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1586cb9b0d80SMike Smith     ACPI_UNLOCK;
15870ae55423SMike Smith     return_VOID;
158815e32d5dSMike Smith }
158915e32d5dSMike Smith 
159015e32d5dSMike Smith static void
159115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
159215e32d5dSMike Smith {
1593fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1594b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
15950ae55423SMike Smith 
159615e32d5dSMike Smith     /* Well, what to do? :-) */
15970ae55423SMike Smith 
1598cb9b0d80SMike Smith     ACPI_LOCK;
1599cb9b0d80SMike Smith     ACPI_UNLOCK;
1600cb9b0d80SMike Smith 
16010ae55423SMike Smith     return_VOID;
160215e32d5dSMike Smith }
160315e32d5dSMike Smith 
160415e32d5dSMike Smith /*
160515e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
160615e32d5dSMike Smith  */
160715e32d5dSMike Smith UINT32
160815e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
160915e32d5dSMike Smith {
161015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
161115e32d5dSMike Smith 
1612b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16130ae55423SMike Smith 
161415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
16150ae55423SMike Smith 
1616b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
161715e32d5dSMike Smith }
161815e32d5dSMike Smith 
161915e32d5dSMike Smith UINT32
162015e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
162115e32d5dSMike Smith {
162215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
162315e32d5dSMike Smith 
1624b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16250ae55423SMike Smith 
162615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
16270ae55423SMike Smith 
1628b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
162915e32d5dSMike Smith }
163015e32d5dSMike Smith 
163115e32d5dSMike Smith UINT32
163215e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
163315e32d5dSMike Smith {
163415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
163515e32d5dSMike Smith 
1636b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16370ae55423SMike Smith 
163815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
16390ae55423SMike Smith 
1640b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
164115e32d5dSMike Smith }
164215e32d5dSMike Smith 
164315e32d5dSMike Smith UINT32
164415e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
164515e32d5dSMike Smith {
164615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
164715e32d5dSMike Smith 
1648b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16490ae55423SMike Smith 
165015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
16510ae55423SMike Smith 
1652b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
165315e32d5dSMike Smith }
165415e32d5dSMike Smith 
165515e32d5dSMike Smith /*
165615e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
165715e32d5dSMike Smith  */
165815e32d5dSMike Smith struct acpi_staticbuf {
165915e32d5dSMike Smith     ACPI_BUFFER	buffer;
166015e32d5dSMike Smith     char	data[512];
166115e32d5dSMike Smith };
166215e32d5dSMike Smith 
166315e32d5dSMike Smith char *
166415e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
166515e32d5dSMike Smith {
166615e32d5dSMike Smith     static struct acpi_staticbuf	buf;
166715e32d5dSMike Smith 
1668cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1669cb9b0d80SMike Smith 
167015e32d5dSMike Smith     buf.buffer.Length = 512;
167115e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
167215e32d5dSMike Smith 
1673b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
167415e32d5dSMike Smith 	return (buf.buffer.Pointer);
1675be2b1797SNate Lawson 
167615e32d5dSMike Smith     return ("(unknown path)");
167715e32d5dSMike Smith }
167815e32d5dSMike Smith 
167915e32d5dSMike Smith /*
168015e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
168115e32d5dSMike Smith  * parts of the namespace.
168215e32d5dSMike Smith  */
168315e32d5dSMike Smith int
168415e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
168515e32d5dSMike Smith {
16863c600cfbSJohn Baldwin     char	*cp, *env, *np;
168715e32d5dSMike Smith     int		len;
168815e32d5dSMike Smith 
168915e32d5dSMike Smith     np = acpi_name(handle);
169015e32d5dSMike Smith     if (*np == '\\')
169115e32d5dSMike Smith 	np++;
16923c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
169315e32d5dSMike Smith 	return (0);
169415e32d5dSMike Smith 
1695be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
16963c600cfbSJohn Baldwin     cp = env;
169715e32d5dSMike Smith     for (;;) {
169815e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
169915e32d5dSMike Smith 	    cp++;
170015e32d5dSMike Smith 	if (*cp == 0)
170115e32d5dSMike Smith 	    break;
170215e32d5dSMike Smith 	len = 0;
170315e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
170415e32d5dSMike Smith 	    len++;
1705d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
17063c600cfbSJohn Baldwin 	    freeenv(env);
17070ae55423SMike Smith 	    return(1);
1708d786139cSMaxime Henrion 	}
17090ae55423SMike Smith 	cp += len;
17100ae55423SMike Smith     }
17113c600cfbSJohn Baldwin     freeenv(env);
1712be2b1797SNate Lawson 
17130ae55423SMike Smith     return (0);
17140ae55423SMike Smith }
17150ae55423SMike Smith 
17160ae55423SMike Smith /*
17170ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
17180ae55423SMike Smith  */
17190ae55423SMike Smith int
17200ae55423SMike Smith acpi_disabled(char *subsys)
17210ae55423SMike Smith {
172278689b15SMaxime Henrion     char	*cp, *env;
17230ae55423SMike Smith     int		len;
17240ae55423SMike Smith 
172578689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
17260ae55423SMike Smith 	return (0);
172778689b15SMaxime Henrion     if (!strcmp(env, "all")) {
172878689b15SMaxime Henrion 	freeenv(env);
17290ae55423SMike Smith 	return (1);
1730d786139cSMaxime Henrion     }
17310ae55423SMike Smith 
17320ae55423SMike Smith     /* scan the disable list checking for a match */
173378689b15SMaxime Henrion     cp = env;
17340ae55423SMike Smith     for (;;) {
17350ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
17360ae55423SMike Smith 	    cp++;
17370ae55423SMike Smith 	if (*cp == 0)
17380ae55423SMike Smith 	    break;
17390ae55423SMike Smith 	len = 0;
17400ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
17410ae55423SMike Smith 	    len++;
1742d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
174378689b15SMaxime Henrion 	    freeenv(env);
174415e32d5dSMike Smith 	    return (1);
1745d786139cSMaxime Henrion 	}
174615e32d5dSMike Smith 	cp += len;
174715e32d5dSMike Smith     }
174878689b15SMaxime Henrion     freeenv(env);
1749be2b1797SNate Lawson 
175015e32d5dSMike Smith     return (0);
175115e32d5dSMike Smith }
175215e32d5dSMike Smith 
175315e32d5dSMike Smith /*
1754a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1755a1fccb47SMitsuru IWASAKI  */
1756a1fccb47SMitsuru IWASAKI void
1757a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1758a1fccb47SMitsuru IWASAKI {
1759a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1760a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1761a1fccb47SMitsuru IWASAKI 
1762a1fccb47SMitsuru IWASAKI     /*
1763a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1764a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1765a1fccb47SMitsuru IWASAKI      */
1766a1fccb47SMitsuru IWASAKI 
1767a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1768a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1769a1fccb47SMitsuru IWASAKI 
1770a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1771a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1772a1fccb47SMitsuru IWASAKI 
1773a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1774a1fccb47SMitsuru IWASAKI }
1775a1fccb47SMitsuru IWASAKI 
1776a1fccb47SMitsuru IWASAKI void
1777a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1778a1fccb47SMitsuru IWASAKI {
1779a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1780a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1781a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1782a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1783a1fccb47SMitsuru IWASAKI 
1784a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1785a1fccb47SMitsuru IWASAKI 
1786be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
1787be2b1797SNate Lawson     if (sc == NULL)
1788a1fccb47SMitsuru IWASAKI 	return;
1789a1fccb47SMitsuru IWASAKI 
1790a1fccb47SMitsuru IWASAKI     /*
1791a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1792a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1793a1fccb47SMitsuru IWASAKI      */
1794a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1795a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1796be2b1797SNate Lawson     if (ACPI_FAILURE(status))
1797a1fccb47SMitsuru IWASAKI 	return;
1798a1fccb47SMitsuru IWASAKI 
1799a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1800be2b1797SNate Lawson     if (res == NULL)
1801a1fccb47SMitsuru IWASAKI 	return;
1802a1fccb47SMitsuru IWASAKI 
1803a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1804a1fccb47SMitsuru IWASAKI 	goto out;
1805a1fccb47SMitsuru IWASAKI     }
1806a1fccb47SMitsuru IWASAKI 
1807a1fccb47SMitsuru IWASAKI     /*
1808a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1809a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1810a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1811a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1812a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1813a1fccb47SMitsuru IWASAKI      */
1814be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1815a1fccb47SMitsuru IWASAKI 	goto out;
1816a1fccb47SMitsuru IWASAKI 
1817be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
1818a1fccb47SMitsuru IWASAKI 	goto out;
1819a1fccb47SMitsuru IWASAKI 
1820a1fccb47SMitsuru IWASAKI     /*
1821a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1822a1fccb47SMitsuru IWASAKI      */
1823a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1824a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1825a1fccb47SMitsuru IWASAKI 	/*
1826a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1827a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1828a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1829a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1830a1fccb47SMitsuru IWASAKI 	 */
1831a1fccb47SMitsuru IWASAKI 
18326fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
18336fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1834a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1835a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
1836a1fccb47SMitsuru IWASAKI 	break;
1837a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1838a1fccb47SMitsuru IWASAKI 	/*
1839be2b1797SNate Lawson 	 * XXX TBD
1840be2b1797SNate Lawson 	 *
1841a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1842a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1843a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1844a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1845a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1846a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1847a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1848a1fccb47SMitsuru IWASAKI 	 * the wake event.
1849a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1850a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1851a1fccb47SMitsuru IWASAKI 	 */
1852a1fccb47SMitsuru IWASAKI 	break;
1853a1fccb47SMitsuru IWASAKI     default:
1854a1fccb47SMitsuru IWASAKI 	break;
1855a1fccb47SMitsuru IWASAKI     }
1856a1fccb47SMitsuru IWASAKI 
1857a1fccb47SMitsuru IWASAKI out:
1858a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1859a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1860a1fccb47SMitsuru IWASAKI }
1861a1fccb47SMitsuru IWASAKI 
1862a1fccb47SMitsuru IWASAKI /*
186315e32d5dSMike Smith  * Control interface.
186415e32d5dSMike Smith  *
18650ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
1866be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
1867be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
186815e32d5dSMike Smith  */
18690ae55423SMike Smith struct acpi_ioctl_hook
18700ae55423SMike Smith {
18710ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
18720ae55423SMike Smith     u_long			 cmd;
1873be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
18740ae55423SMike Smith     void			 *arg;
18750ae55423SMike Smith };
18760ae55423SMike Smith 
18770ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
18780ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
18790ae55423SMike Smith 
18800ae55423SMike Smith /*
18810ae55423SMike Smith  * Register an ioctl handler.
18820ae55423SMike Smith  */
18830ae55423SMike Smith int
1884be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
18850ae55423SMike Smith {
18860ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
18870ae55423SMike Smith 
18880ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
18890ae55423SMike Smith 	return (ENOMEM);
18900ae55423SMike Smith     hp->cmd = cmd;
18910ae55423SMike Smith     hp->fn = fn;
18920ae55423SMike Smith     hp->arg = arg;
18930ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
18940ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
18950ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
18960ae55423SMike Smith     }
18970ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
18980ae55423SMike Smith     return (0);
18990ae55423SMike Smith }
19000ae55423SMike Smith 
19010ae55423SMike Smith /*
19020ae55423SMike Smith  * Deregister an ioctl handler.
19030ae55423SMike Smith  */
19040ae55423SMike Smith void
1905be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
19060ae55423SMike Smith {
19070ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
19080ae55423SMike Smith 
19090ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
19100ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
19110ae55423SMike Smith 	    break;
19120ae55423SMike Smith 
19130ae55423SMike Smith     if (hp != NULL) {
19140ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
19150ae55423SMike Smith 	free(hp, M_ACPIDEV);
19160ae55423SMike Smith     }
19170ae55423SMike Smith }
19180ae55423SMike Smith 
191915e32d5dSMike Smith static int
19206b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
192115e32d5dSMike Smith {
192215e32d5dSMike Smith     return (0);
192315e32d5dSMike Smith }
192415e32d5dSMike Smith 
192515e32d5dSMike Smith static int
19266b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
192715e32d5dSMike Smith {
192815e32d5dSMike Smith     return (0);
192915e32d5dSMike Smith }
193015e32d5dSMike Smith 
193115e32d5dSMike Smith static int
19326b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
193315e32d5dSMike Smith {
193415e32d5dSMike Smith     struct acpi_softc		*sc;
19350ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
19360ae55423SMike Smith     int				error, xerror, state;
1937fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
193815e32d5dSMike Smith 
1939cb9b0d80SMike Smith     ACPI_LOCK;
1940cb9b0d80SMike Smith 
194115e32d5dSMike Smith     error = state = 0;
194215e32d5dSMike Smith     sc = dev->si_drv1;
194315e32d5dSMike Smith 
19440ae55423SMike Smith     /*
19450ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
19460ae55423SMike Smith      */
19470ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
19480ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
19490ae55423SMike Smith 	    if (hp->cmd == cmd) {
19500ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
19510ae55423SMike Smith 		if (xerror != 0)
19520ae55423SMike Smith 		    error = xerror;
1953917d44c8SMitsuru IWASAKI 		goto out;
19540ae55423SMike Smith 	    }
19550ae55423SMike Smith 	}
19560ae55423SMike Smith     }
19570ae55423SMike Smith 
19580ae55423SMike Smith     /*
1959a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
1960a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
1961a89fcf28STakanori Watanabe      * Not changing system behavior.
1962a89fcf28STakanori Watanabe      */
1963be2b1797SNate Lawson     if((flag & FWRITE) == 0)
1964be2b1797SNate Lawson 	return (EPERM);
1965a89fcf28STakanori Watanabe 
1966be2b1797SNate Lawson     /* Core system ioctls. */
196715e32d5dSMike Smith     switch (cmd) {
196815e32d5dSMike Smith     case ACPIIO_ENABLE:
19690ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
197015e32d5dSMike Smith 	    error = ENXIO;
197115e32d5dSMike Smith 	break;
197215e32d5dSMike Smith     case ACPIIO_DISABLE:
19730ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
197415e32d5dSMike Smith 	    error = ENXIO;
197515e32d5dSMike Smith 	break;
197615e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
197715e32d5dSMike Smith 	if (!sc->acpi_enabled) {
197815e32d5dSMike Smith 	    error = ENXIO;
197915e32d5dSMike Smith 	    break;
198015e32d5dSMike Smith 	}
198115e32d5dSMike Smith 	state = *(int *)addr;
1982be2b1797SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX)
198315e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
1984be2b1797SNate Lawson 	else
198515e32d5dSMike Smith 	    error = EINVAL;
198615e32d5dSMike Smith 	break;
198715e32d5dSMike Smith     default:
19880ae55423SMike Smith 	if (error == 0)
198915e32d5dSMike Smith 	    error = EINVAL;
199015e32d5dSMike Smith 	break;
199115e32d5dSMike Smith     }
1992917d44c8SMitsuru IWASAKI 
1993917d44c8SMitsuru IWASAKI out:
1994cb9b0d80SMike Smith     ACPI_UNLOCK;
199515e32d5dSMike Smith     return (error);
199615e32d5dSMike Smith }
199715e32d5dSMike Smith 
19981d073b1dSJohn Baldwin static int
1999d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2000d75de536SMitsuru IWASAKI {
2001d75de536SMitsuru IWASAKI     char sleep_state[4];
2002d75de536SMitsuru IWASAKI     char buf[16];
2003d75de536SMitsuru IWASAKI     int error;
2004d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2005d75de536SMitsuru IWASAKI 
2006d75de536SMitsuru IWASAKI     buf[0] = '\0';
2007d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2008d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2009d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2010d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2011d75de536SMitsuru IWASAKI 	}
2012d75de536SMitsuru IWASAKI     }
2013d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2014d75de536SMitsuru IWASAKI     return (error);
2015d75de536SMitsuru IWASAKI }
2016d75de536SMitsuru IWASAKI 
2017d75de536SMitsuru IWASAKI static int
20181d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
20191d073b1dSJohn Baldwin {
20201d073b1dSJohn Baldwin     char sleep_state[10];
20211d073b1dSJohn Baldwin     int error;
20221d073b1dSJohn Baldwin     u_int new_state, old_state;
20231d073b1dSJohn Baldwin 
20241d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2025b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
20261d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2027cb9b0d80SMike Smith     } else {
2028b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
20291d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
20301d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2031cb9b0d80SMike Smith     }
20321d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
20331d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2034be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2035be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
20361d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
20371d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
20381d073b1dSJohn Baldwin 		break;
2039cb9b0d80SMike Smith 	}
2040931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2041be2b1797SNate Lawson 	    if (new_state != old_state)
20421d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2043cb9b0d80SMike Smith 	} else {
20441d073b1dSJohn Baldwin 	    error = EINVAL;
20451d073b1dSJohn Baldwin 	}
2046cb9b0d80SMike Smith     }
2047be2b1797SNate Lawson 
20481d073b1dSJohn Baldwin     return (error);
20491d073b1dSJohn Baldwin }
20501d073b1dSJohn Baldwin 
205115e32d5dSMike Smith #ifdef ACPI_DEBUG
20520ae55423SMike Smith /*
20530ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
20540ae55423SMike Smith  *
20550ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
20560ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
20570ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
20580ae55423SMike Smith  * prefixing the bit name with !.
20590ae55423SMike Smith  */
206015e32d5dSMike Smith struct debugtag
206115e32d5dSMike Smith {
206215e32d5dSMike Smith     char	*name;
206315e32d5dSMike Smith     UINT32	value;
206415e32d5dSMike Smith };
206515e32d5dSMike Smith 
206615e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2067c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2068c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2069c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2070c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2071c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2072c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2073c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2074c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2075c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2076d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
20774c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2078d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
20794c1cdee6SMike Smith 
2080cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
20814c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2082cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2083cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2084c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2085c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2086c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
20874c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2088cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
20894c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
20904c1cdee6SMike Smith 
2091b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
2092c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
209315e32d5dSMike Smith     {NULL, 0}
209415e32d5dSMike Smith };
209515e32d5dSMike Smith 
209615e32d5dSMike Smith static struct debugtag dbg_level[] = {
20974c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
20989501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
20999501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
21004c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
21019501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
21024c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2103d62ab2f4SMitsuru IWASAKI 
2104d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
21054c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
21064c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2107d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
21084c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
21094c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
21104c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
21114c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
21124c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
21134c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
21144c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
21154c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
21164c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
21174c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
21189501b603SJohn Baldwin     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
2119d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2120d62ab2f4SMitsuru IWASAKI 
2121d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2122d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2123d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2124d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2125d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
21264c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2127d62ab2f4SMitsuru IWASAKI 
2128d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2129d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2130d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2131d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2132d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2133d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2134d62ab2f4SMitsuru IWASAKI 
2135d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
213698479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
213798479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
213898479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
213998479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
214098479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
214115e32d5dSMike Smith     {NULL, 0}
214215e32d5dSMike Smith };
214315e32d5dSMike Smith 
214415e32d5dSMike Smith static void
214515e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
214615e32d5dSMike Smith {
214715e32d5dSMike Smith     char	*ep;
214815e32d5dSMike Smith     int		i, l;
21490ae55423SMike Smith     int		set;
215015e32d5dSMike Smith 
215115e32d5dSMike Smith     while (*cp) {
215215e32d5dSMike Smith 	if (isspace(*cp)) {
215315e32d5dSMike Smith 	    cp++;
215415e32d5dSMike Smith 	    continue;
215515e32d5dSMike Smith 	}
215615e32d5dSMike Smith 	ep = cp;
215715e32d5dSMike Smith 	while (*ep && !isspace(*ep))
215815e32d5dSMike Smith 	    ep++;
21590ae55423SMike Smith 	if (*cp == '!') {
21600ae55423SMike Smith 	    set = 0;
21610ae55423SMike Smith 	    cp++;
21620ae55423SMike Smith 	    if (cp == ep)
21630ae55423SMike Smith 		continue;
21640ae55423SMike Smith 	} else {
21650ae55423SMike Smith 	    set = 1;
21660ae55423SMike Smith 	}
216715e32d5dSMike Smith 	l = ep - cp;
216815e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
216915e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2170be2b1797SNate Lawson 		if (set)
217115e32d5dSMike Smith 		    *flag |= tag[i].value;
2172be2b1797SNate Lawson 		else
21730ae55423SMike Smith 		    *flag &= ~tag[i].value;
217415e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
217515e32d5dSMike Smith 	    }
217615e32d5dSMike Smith 	}
217715e32d5dSMike Smith 	cp = ep;
217815e32d5dSMike Smith     }
217915e32d5dSMike Smith }
218015e32d5dSMike Smith 
218115e32d5dSMike Smith static void
21822a4ac806SMike Smith acpi_set_debugging(void *junk)
218315e32d5dSMike Smith {
218494aae282SMike Barcroft     char	*cp;
218515e32d5dSMike Smith 
218687b45ed5SMitsuru IWASAKI     if (!cold)
218787b45ed5SMitsuru IWASAKI 	return;
218887b45ed5SMitsuru IWASAKI 
21890ae55423SMike Smith     AcpiDbgLayer = 0;
21900ae55423SMike Smith     AcpiDbgLevel = 0;
219194aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
219215e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
219394aae282SMike Barcroft 	freeenv(cp);
219494aae282SMike Barcroft     }
219594aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
219615e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
219794aae282SMike Barcroft 	freeenv(cp);
219894aae282SMike Barcroft     }
21990ae55423SMike Smith 
2200be2b1797SNate Lawson     printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer,
2201be2b1797SNate Lawson 	   AcpiDbgLevel);
220215e32d5dSMike Smith }
2203be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2204be2b1797SNate Lawson 	NULL);
220515e32d5dSMike Smith #endif
2206f9390180SMitsuru IWASAKI 
2207f9390180SMitsuru IWASAKI static int
2208f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2209f9390180SMitsuru IWASAKI {
2210f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2211f9390180SMitsuru IWASAKI 	int	error;
2212f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2213f9390180SMitsuru IWASAKI 	va_list	ap;
2214f9390180SMitsuru IWASAKI 
2215f9390180SMitsuru IWASAKI 	error = 0;
2216f9390180SMitsuru IWASAKI 	switch (cmd) {
2217f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2218f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2219f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2220f9390180SMitsuru IWASAKI 			error = EINVAL;
2221f9390180SMitsuru IWASAKI 			goto out;
2222f9390180SMitsuru IWASAKI 		}
2223f9390180SMitsuru IWASAKI 
2224f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2225f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2226f9390180SMitsuru IWASAKI 		va_end(ap);
2227f9390180SMitsuru IWASAKI 
2228f9390180SMitsuru IWASAKI 		switch (state) {
2229f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2230f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2231f9390180SMitsuru IWASAKI 			break;
2232f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2233f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2234f9390180SMitsuru IWASAKI 			break;
2235f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2236f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2237f9390180SMitsuru IWASAKI 			break;
2238f9390180SMitsuru IWASAKI 		default:
2239f9390180SMitsuru IWASAKI 			error = EINVAL;
2240f9390180SMitsuru IWASAKI 			goto out;
2241f9390180SMitsuru IWASAKI 		}
2242f9390180SMitsuru IWASAKI 
2243f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2244f9390180SMitsuru IWASAKI 		break;
2245f9390180SMitsuru IWASAKI 	default:
2246f9390180SMitsuru IWASAKI 		error = EINVAL;
2247f9390180SMitsuru IWASAKI 		goto out;
2248f9390180SMitsuru IWASAKI 	}
2249f9390180SMitsuru IWASAKI 
2250f9390180SMitsuru IWASAKI out:
2251f9390180SMitsuru IWASAKI 	return (error);
2252f9390180SMitsuru IWASAKI }
2253f9390180SMitsuru IWASAKI 
2254f9390180SMitsuru IWASAKI static void
2255f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2256f9390180SMitsuru IWASAKI {
2257be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
22586c407052SMitsuru IWASAKI 	return;
2259f9390180SMitsuru IWASAKI 
2260f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2261f9390180SMitsuru IWASAKI }
2262f9390180SMitsuru IWASAKI 
2263f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2264