xref: /freebsd/sys/dev/acpica/acpi.c (revision bd189fe7fa4b0da0fd683243e1033110b12f9908)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  *
2915e32d5dSMike Smith  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
3815e32d5dSMike Smith #include <sys/bus.h>
3915e32d5dSMike Smith #include <sys/conf.h>
4015e32d5dSMike Smith #include <sys/ioccom.h>
4115e32d5dSMike Smith #include <sys/reboot.h>
4215e32d5dSMike Smith #include <sys/sysctl.h>
4315e32d5dSMike Smith #include <sys/ctype.h>
441611ea87SMitsuru IWASAKI #include <sys/linker.h>
45f9390180SMitsuru IWASAKI #include <sys/power.h>
4654f6bca0SNate Lawson #include <sys/sbuf.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/clock.h>
4915e32d5dSMike Smith #include <machine/resource.h>
50dc750869SNate Lawson #include <machine/bus.h>
51dc750869SNate Lawson #include <sys/rman.h>
52bc0f2195SMike Smith #include <isa/isavar.h>
53bc0f2195SMike Smith 
5415e32d5dSMike Smith #include "acpi.h"
5515e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5615e32d5dSMike Smith #include <dev/acpica/acpiio.h>
579b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
5815e32d5dSMike Smith 
5915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6015e32d5dSMike Smith 
61be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
62cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
63b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
640ae55423SMike Smith 
6515e32d5dSMike Smith static d_open_t		acpiopen;
6615e32d5dSMike Smith static d_close_t	acpiclose;
6715e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6815e32d5dSMike Smith 
6915e32d5dSMike Smith #define CDEV_MAJOR 152
7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
717ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
727ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
737ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
747ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
757ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
7615e32d5dSMike Smith };
7715e32d5dSMike Smith 
781d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
79b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
801d073b1dSJohn Baldwin 
812a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
822a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
832a4ac806SMike Smith 
84fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
85cb9b0d80SMike Smith struct mtx	acpi_mutex;
86fc0ea94aSJohn Baldwin #endif
87cb9b0d80SMike Smith 
886d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
8915e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9015e32d5dSMike Smith static int	acpi_probe(device_t dev);
9115e32d5dSMike Smith static int	acpi_attach(device_t dev);
92be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
93be2b1797SNate Lawson 			int unit);
9415e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
95be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
96be2b1797SNate Lawson 			uintptr_t *result);
97be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
98be2b1797SNate Lawson 			uintptr_t value);
99be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
100be2b1797SNate Lawson 			int rid, u_long start, u_long count);
101be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
102be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
103be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
104be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
105be2b1797SNate Lawson 			u_long count, u_int flags);
106be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
107be2b1797SNate Lawson 			int rid, struct resource *r);
1081e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1091e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
110be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
111be2b1797SNate Lawson 			struct isa_pnp_id *ids);
11215e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
113be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
114be2b1797SNate Lawson 			void *context, void **status);
11515e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
11615e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
11713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
11815e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
11915e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
120d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1211d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
122f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
123f9390180SMitsuru IWASAKI 
12415e32d5dSMike Smith static device_method_t acpi_methods[] = {
12515e32d5dSMike Smith     /* Device interface */
12615e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
12715e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
12815e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
12956a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
13015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
13115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
13215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
13315e32d5dSMike Smith 
13415e32d5dSMike Smith     /* Bus interface */
13515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
13615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
13715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
13815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
13915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
14015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
14115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
14215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
14315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
14415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
14515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
14615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
14715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
14815e32d5dSMike Smith 
149bc0f2195SMike Smith     /* ISA emulation */
150bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
151bc0f2195SMike Smith 
15215e32d5dSMike Smith     {0, 0}
15315e32d5dSMike Smith };
15415e32d5dSMike Smith 
15515e32d5dSMike Smith static driver_t acpi_driver = {
15615e32d5dSMike Smith     "acpi",
15715e32d5dSMike Smith     acpi_methods,
15815e32d5dSMike Smith     sizeof(struct acpi_softc),
15915e32d5dSMike Smith };
16015e32d5dSMike Smith 
1613273b005SMike Smith static devclass_t acpi_devclass;
1626d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
163dde24897SMike Smith MODULE_VERSION(acpi, 100);
16415e32d5dSMike Smith 
1651d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1661d7b121cSNate Lawson static char acpi_ca_version[12];
1671d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1681d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
16915e32d5dSMike Smith 
17015e32d5dSMike Smith /*
1716d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1726d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
173be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
174be2b1797SNate Lawson  * off it.
1756d63101aSMike Smith  */
1766d63101aSMike Smith static int
1776d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1786d63101aSMike Smith {
1796d63101aSMike Smith     switch(event) {
1806d63101aSMike Smith     case MOD_LOAD:
18187b45ed5SMitsuru IWASAKI 	if (!cold) {
18287b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
1836d63101aSMike Smith 	    return (EPERM);
18487b45ed5SMitsuru IWASAKI 	}
1856d63101aSMike Smith 	break;
1866d63101aSMike Smith     case MOD_UNLOAD:
187f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1886d63101aSMike Smith 	    return (EBUSY);
189f9390180SMitsuru IWASAKI 	break;
1906d63101aSMike Smith     default:
1916d63101aSMike Smith 	break;
1926d63101aSMike Smith     }
1936d63101aSMike Smith     return (0);
1946d63101aSMike Smith }
1956d63101aSMike Smith 
1966d63101aSMike Smith /*
19715e32d5dSMike Smith  * Detect ACPI, perform early initialisation
19815e32d5dSMike Smith  */
19915e32d5dSMike Smith static void
20015e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20115e32d5dSMike Smith {
20215e32d5dSMike Smith     device_t	child;
20315e32d5dSMike Smith     int		error;
204d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
205d786139cSMaxime Henrion     char	*debugpoint;
20615e32d5dSMike Smith #endif
20715e32d5dSMike Smith 
208b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2090ae55423SMike Smith 
21087b45ed5SMitsuru IWASAKI     if (!cold)
21187b45ed5SMitsuru IWASAKI 	return_VOID;
212840f9b53STakanori Watanabe 
213be2b1797SNate Lawson     /* Check that we haven't been disabled with a hint. */
2148a9bc9c0SJohn Baldwin     if (resource_disabled("acpi", 0))
21532d18aa5SMike Smith 	return_VOID;
21632d18aa5SMike Smith 
2171d7b121cSNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "0x%x",
2181d7b121cSNate Lawson 	     ACPI_CA_VERSION);
2191d7b121cSNate Lawson 
220be2b1797SNate Lawson     /* Make sure we're not being doubly invoked. */
22115e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2220ae55423SMike Smith 	return_VOID;
22315e32d5dSMike Smith 
224fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
225be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2266008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
227fc0ea94aSJohn Baldwin #endif
228cb9b0d80SMike Smith 
229be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
230d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
231d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
232d786139cSMaxime Henrion     if (debugpoint) {
233d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
23415e32d5dSMike Smith 	    acpi_EnterDebugger();
235d786139cSMaxime Henrion 	freeenv(debugpoint);
236d786139cSMaxime Henrion     }
23715e32d5dSMike Smith #endif
238b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
239bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2400ae55423SMike Smith 	return_VOID;
24115e32d5dSMike Smith     }
242d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
243d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
244d786139cSMaxime Henrion     if (debugpoint) {
245d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
24615e32d5dSMike Smith 	    acpi_EnterDebugger();
247d786139cSMaxime Henrion 	freeenv(debugpoint);
248d786139cSMaxime Henrion     }
24915e32d5dSMike Smith #endif
2501611ea87SMitsuru IWASAKI 
251b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
252bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2530ae55423SMike Smith 	return_VOID;
25415e32d5dSMike Smith     }
25515e32d5dSMike Smith 
256be2b1797SNate Lawson     /* Attach the actual ACPI device. */
25715e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
25815e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
2590ae55423SMike Smith 	return_VOID;
26015e32d5dSMike Smith     }
26115e32d5dSMike Smith }
26215e32d5dSMike Smith 
26315e32d5dSMike Smith /*
26415e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
26515e32d5dSMike Smith  */
26615e32d5dSMike Smith static int
26715e32d5dSMike Smith acpi_probe(device_t dev)
26815e32d5dSMike Smith {
26915e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
27015e32d5dSMike Smith     char		buf[20];
271cb9b0d80SMike Smith     ACPI_STATUS		status;
27215e32d5dSMike Smith     int			error;
273fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
27415e32d5dSMike Smith 
275b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
276f9390180SMitsuru IWASAKI 
277f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
278f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
279be2b1797SNate Lawson 
280f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
281f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
282f9390180SMitsuru IWASAKI     }
283f9390180SMitsuru IWASAKI 
284cb9b0d80SMike Smith     ACPI_LOCK;
2850ae55423SMike Smith 
286b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
287be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
288be2b1797SNate Lawson 		      AcpiFormatException(status));
289cb9b0d80SMike Smith 	error = ENXIO;
290cb9b0d80SMike Smith     } else {
29115e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
29215e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
293cb9b0d80SMike Smith 	error = 0;
294cb9b0d80SMike Smith     }
295cb9b0d80SMike Smith     ACPI_UNLOCK;
296cb9b0d80SMike Smith     return_VALUE(error);
29715e32d5dSMike Smith }
29815e32d5dSMike Smith 
29915e32d5dSMike Smith static int
30015e32d5dSMike Smith acpi_attach(device_t dev)
30115e32d5dSMike Smith {
30215e32d5dSMike Smith     struct acpi_softc	*sc;
303cb9b0d80SMike Smith     ACPI_STATUS		status;
30415e32d5dSMike Smith     int			error;
30532d18aa5SMike Smith     UINT32		flags;
306498d464fSMitsuru IWASAKI     char		*env;
307d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
308d786139cSMaxime Henrion     char		*debugpoint;
30915e32d5dSMike Smith #endif
310fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
31115e32d5dSMike Smith 
312b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
313cb9b0d80SMike Smith     ACPI_LOCK;
31415e32d5dSMike Smith     sc = device_get_softc(dev);
31515e32d5dSMike Smith     bzero(sc, sizeof(*sc));
31615e32d5dSMike Smith     sc->acpi_dev = dev;
31715e32d5dSMike Smith 
318d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
319d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
320d786139cSMaxime Henrion     if (debugpoint) {
321d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
32215e32d5dSMike Smith 	    acpi_EnterDebugger();
323d786139cSMaxime Henrion 	freeenv(debugpoint);
324d786139cSMaxime Henrion     }
32515e32d5dSMike Smith #endif
32615e32d5dSMike Smith 
327be2b1797SNate Lawson     /* Install the default address space handlers. */
328cb9b0d80SMike Smith     error = ENXIO;
329be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
330be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
331be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
332be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
333be2b1797SNate Lawson 		      AcpiFormatException(status));
334cb9b0d80SMike Smith 	goto out;
33515e32d5dSMike Smith     }
336be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
337be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
338be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
339be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
340be2b1797SNate Lawson 		      AcpiFormatException(status));
341cb9b0d80SMike Smith 	goto out;
34215e32d5dSMike Smith     }
343be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
344be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
345be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
346be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
347be2b1797SNate Lawson 		      AcpiFormatException(status));
348cb9b0d80SMike Smith 	goto out;
34915e32d5dSMike Smith     }
35015e32d5dSMike Smith 
35115e32d5dSMike Smith     /*
35215e32d5dSMike Smith      * Bring ACPI fully online.
35315e32d5dSMike Smith      *
354be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
355be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
356be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
357be2b1797SNate Lawson      * object init pass.
35815e32d5dSMike Smith      *
35932d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
36032d18aa5SMike Smith      *
361be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
362be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
36315e32d5dSMike Smith      */
364d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
365d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
366d786139cSMaxime Henrion     if (debugpoint) {
367d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
36815e32d5dSMike Smith 	    acpi_EnterDebugger();
369d786139cSMaxime Henrion 	freeenv(debugpoint);
370d786139cSMaxime Henrion     }
37115e32d5dSMike Smith #endif
37232d18aa5SMike Smith     flags = 0;
373d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
37432d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
375b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
376be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
377be2b1797SNate Lawson 		      AcpiFormatException(status));
378cb9b0d80SMike Smith 	goto out;
37915e32d5dSMike Smith     }
38015e32d5dSMike Smith 
381f8335e3aSNate Lawson     /*
382f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
383f8335e3aSNate Lawson      * the namespace has been evaluated.
384f8335e3aSNate Lawson      */
385f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
386f8335e3aSNate Lawson 
387b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
388be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
389be2b1797SNate Lawson 		      AcpiFormatException(status));
390b69ed3f4SMitsuru IWASAKI 	goto out;
391b69ed3f4SMitsuru IWASAKI     }
392b69ed3f4SMitsuru IWASAKI 
39315e32d5dSMike Smith     /*
3941d073b1dSJohn Baldwin      * Setup our sysctl tree.
3951d073b1dSJohn Baldwin      *
3961d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3971d073b1dSJohn Baldwin      */
3981d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3991d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4001d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4011d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4021d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
403d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
404d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
405d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4061d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4071d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4081d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4091d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4101d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4111d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4121d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4131d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
414f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
415f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
416f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
417f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
418f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
419f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4202d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
421ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
422ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
423ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4241611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4251611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4261611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4272d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4282d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
429c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
430c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
431c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
432bf0c18ecSNate Lawson 
433bf0c18ecSNate Lawson     /*
434bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
435bf0c18ecSNate Lawson      * stabilize.
436bf0c18ecSNate Lawson      */
437bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
438c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
4392d644607SMitsuru IWASAKI     if (bootverbose)
4402d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
441498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
442498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
443498d464fSMitsuru IWASAKI 	freeenv(env);
444498d464fSMitsuru IWASAKI     }
4451d073b1dSJohn Baldwin 
4462d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
4472d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
4482d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
4492d610c46SNate Lawson 
4501d073b1dSJohn Baldwin     /*
45115e32d5dSMike Smith      * Dispatch the default sleep state to devices.
45215e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
45315e32d5dSMike Smith      */
45415e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
45515e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
45615e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
457f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
458f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
45915e32d5dSMike Smith 
46013d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
46115e32d5dSMike Smith 
46215e32d5dSMike Smith     /*
46315e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
46415e32d5dSMike Smith      */
465d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
466d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
467d786139cSMaxime Henrion     if (debugpoint) {
468d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
46915e32d5dSMike Smith 	    acpi_EnterDebugger();
470d786139cSMaxime Henrion 	freeenv(debugpoint);
471d786139cSMaxime Henrion     }
47215e32d5dSMike Smith #endif
47315e32d5dSMike Smith 
474be2b1797SNate Lawson     /* Register our shutdown handlers */
475be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
476be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
477be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
478be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
47915e32d5dSMike Smith 
48015e32d5dSMike Smith     /*
48115e32d5dSMike Smith      * Register our acpi event handlers.
48215e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
48315e32d5dSMike Smith      */
484be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
485be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
486be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
487be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
48815e32d5dSMike Smith 
489be2b1797SNate Lawson     /* Flag our initial states. */
49015e32d5dSMike Smith     sc->acpi_enabled = 1;
49115e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
492ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
49315e32d5dSMike Smith 
494be2b1797SNate Lawson     /* Create the control device */
495a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
4962a4689e8SRobert Watson 			      "acpi");
49715e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
49815e32d5dSMike Smith 
499d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
500d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
501d786139cSMaxime Henrion     if (debugpoint) {
502be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
50315e32d5dSMike Smith 	    acpi_EnterDebugger();
504d786139cSMaxime Henrion 	freeenv(debugpoint);
505d786139cSMaxime Henrion     }
50615e32d5dSMike Smith #endif
507f86214b6SMitsuru IWASAKI 
50891da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
509be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
510c573e654SMitsuru IWASAKI 	goto out;
511c573e654SMitsuru IWASAKI #endif
512c573e654SMitsuru IWASAKI 
513be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
514f86214b6SMitsuru IWASAKI 	goto out;
515f86214b6SMitsuru IWASAKI 
516f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
517f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
518f9390180SMitsuru IWASAKI 
519eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
520eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
521eeb6dba2SJohn Baldwin 
522cb9b0d80SMike Smith     error = 0;
523cb9b0d80SMike Smith 
524cb9b0d80SMike Smith  out:
525cb9b0d80SMike Smith     ACPI_UNLOCK;
526cb9b0d80SMike Smith     return_VALUE (error);
52715e32d5dSMike Smith }
52815e32d5dSMike Smith 
52915e32d5dSMike Smith /*
53015e32d5dSMike Smith  * Handle a new device being added
53115e32d5dSMike Smith  */
53215e32d5dSMike Smith static device_t
53315e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
53415e32d5dSMike Smith {
53515e32d5dSMike Smith     struct acpi_device	*ad;
53615e32d5dSMike Smith     device_t		child;
53715e32d5dSMike Smith 
538be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
53915e32d5dSMike Smith 	return (NULL);
54015e32d5dSMike Smith 
54115e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
54215e32d5dSMike Smith 
54315e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
54415e32d5dSMike Smith     if (child != NULL)
54515e32d5dSMike Smith 	device_set_ivars(child, ad);
54615e32d5dSMike Smith     return (child);
54715e32d5dSMike Smith }
54815e32d5dSMike Smith 
54915e32d5dSMike Smith static int
55015e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
55115e32d5dSMike Smith {
55215e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
55315e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
55415e32d5dSMike Smith     int retval = 0;
55515e32d5dSMike Smith 
55615e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
5579ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
5589ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
5599ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
5609ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
56115e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
56215e32d5dSMike Smith 
56315e32d5dSMike Smith     return (retval);
56415e32d5dSMike Smith }
56515e32d5dSMike Smith 
56615e32d5dSMike Smith 
56715e32d5dSMike Smith /*
56815e32d5dSMike Smith  * Handle per-device ivars
56915e32d5dSMike Smith  */
57015e32d5dSMike Smith static int
57115e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
57215e32d5dSMike Smith {
57315e32d5dSMike Smith     struct acpi_device	*ad;
57415e32d5dSMike Smith 
57515e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
57615e32d5dSMike Smith 	printf("device has no ivars\n");
57715e32d5dSMike Smith 	return (ENOENT);
57815e32d5dSMike Smith     }
57915e32d5dSMike Smith 
580be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
58115e32d5dSMike Smith     switch(index) {
58215e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
58315e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
58415e32d5dSMike Smith 	break;
58515e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
58615e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
58715e32d5dSMike Smith 	break;
58815e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58915e32d5dSMike Smith 	*(void **)result = ad->ad_private;
59015e32d5dSMike Smith 	break;
59132d18aa5SMike Smith     case ISA_IVAR_VENDORID:
59232d18aa5SMike Smith     case ISA_IVAR_SERIAL:
59332d18aa5SMike Smith     case ISA_IVAR_COMPATID:
59432d18aa5SMike Smith 	*(int *)result = -1;
59532d18aa5SMike Smith 	break;
59632d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
59732d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59832d18aa5SMike Smith 	break;
59915e32d5dSMike Smith     default:
60015e32d5dSMike Smith 	return (ENOENT);
60115e32d5dSMike Smith     }
602be2b1797SNate Lawson 
60315e32d5dSMike Smith     return (0);
60415e32d5dSMike Smith }
60515e32d5dSMike Smith 
60615e32d5dSMike Smith static int
60715e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
60815e32d5dSMike Smith {
60915e32d5dSMike Smith     struct acpi_device	*ad;
61015e32d5dSMike Smith 
61115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
61215e32d5dSMike Smith 	printf("device has no ivars\n");
61315e32d5dSMike Smith 	return (ENOENT);
61415e32d5dSMike Smith     }
61515e32d5dSMike Smith 
61615e32d5dSMike Smith     switch(index) {
61715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61815e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
61915e32d5dSMike Smith 	break;
62015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
62115e32d5dSMike Smith 	ad->ad_magic = (int)value;
62215e32d5dSMike Smith 	break;
62315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62415e32d5dSMike Smith 	ad->ad_private = (void *)value;
62515e32d5dSMike Smith 	break;
62615e32d5dSMike Smith     default:
6272ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
62815e32d5dSMike Smith 	return (ENOENT);
62915e32d5dSMike Smith     }
630be2b1797SNate Lawson 
63115e32d5dSMike Smith     return (0);
63215e32d5dSMike Smith }
63315e32d5dSMike Smith 
634be2b1797SNate Lawson ACPI_HANDLE
635be2b1797SNate Lawson acpi_get_handle(device_t dev)
636be2b1797SNate Lawson {
637be2b1797SNate Lawson     uintptr_t up;
638be2b1797SNate Lawson     ACPI_HANDLE	h;
639be2b1797SNate Lawson 
640be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
641be2b1797SNate Lawson 	return(NULL);
642be2b1797SNate Lawson     h = (ACPI_HANDLE)up;
643be2b1797SNate Lawson     return (h);
644be2b1797SNate Lawson }
645be2b1797SNate Lawson 
646be2b1797SNate Lawson int
647be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h)
648be2b1797SNate Lawson {
649be2b1797SNate Lawson     uintptr_t up;
650be2b1797SNate Lawson 
651be2b1797SNate Lawson     up = (uintptr_t)h;
652be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
653be2b1797SNate Lawson }
654be2b1797SNate Lawson 
655be2b1797SNate Lawson int
656be2b1797SNate Lawson acpi_get_magic(device_t dev)
657be2b1797SNate Lawson {
658be2b1797SNate Lawson     uintptr_t up;
659be2b1797SNate Lawson     int	m;
660be2b1797SNate Lawson 
661be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
662be2b1797SNate Lawson 	return(0);
663be2b1797SNate Lawson     m = (int)up;
664be2b1797SNate Lawson     return (m);
665be2b1797SNate Lawson }
666be2b1797SNate Lawson 
667be2b1797SNate Lawson int
668be2b1797SNate Lawson acpi_set_magic(device_t dev, int m)
669be2b1797SNate Lawson {
670be2b1797SNate Lawson     uintptr_t up;
671be2b1797SNate Lawson 
672be2b1797SNate Lawson     up = (uintptr_t)m;
673be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
674be2b1797SNate Lawson }
675be2b1797SNate Lawson 
676be2b1797SNate Lawson void *
677be2b1797SNate Lawson acpi_get_private(device_t dev)
678be2b1797SNate Lawson {
679be2b1797SNate Lawson     uintptr_t up;
680be2b1797SNate Lawson     void *p;
681be2b1797SNate Lawson 
682be2b1797SNate Lawson     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
683be2b1797SNate Lawson 	return (NULL);
684be2b1797SNate Lawson     p = (void *)up;
685be2b1797SNate Lawson     return (p);
686be2b1797SNate Lawson }
687be2b1797SNate Lawson 
688be2b1797SNate Lawson int
689be2b1797SNate Lawson acpi_set_private(device_t dev, void *p)
690be2b1797SNate Lawson {
691be2b1797SNate Lawson     uintptr_t up;
692be2b1797SNate Lawson 
693be2b1797SNate Lawson     up = (uintptr_t)p;
694be2b1797SNate Lawson     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
695be2b1797SNate Lawson }
696be2b1797SNate Lawson 
697be2b1797SNate Lawson ACPI_OBJECT_TYPE
698be2b1797SNate Lawson acpi_get_type(device_t dev)
699be2b1797SNate Lawson {
700be2b1797SNate Lawson     ACPI_HANDLE		h;
701be2b1797SNate Lawson     ACPI_OBJECT_TYPE	t;
702be2b1797SNate Lawson 
703be2b1797SNate Lawson     if ((h = acpi_get_handle(dev)) == NULL)
704be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
705be2b1797SNate Lawson     if (AcpiGetType(h, &t) != AE_OK)
706be2b1797SNate Lawson 	return (ACPI_TYPE_NOT_FOUND);
707be2b1797SNate Lawson     return (t);
708be2b1797SNate Lawson }
709be2b1797SNate Lawson 
71015e32d5dSMike Smith /*
71115e32d5dSMike Smith  * Handle child resource allocation/removal
71215e32d5dSMike Smith  */
71315e32d5dSMike Smith static int
714be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
715be2b1797SNate Lawson 		  u_long start, u_long count)
71615e32d5dSMike Smith {
71715e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
71815e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
71915e32d5dSMike Smith 
72015e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
72115e32d5dSMike Smith 
72215e32d5dSMike Smith     return(0);
72315e32d5dSMike Smith }
72415e32d5dSMike Smith 
72515e32d5dSMike Smith static int
726be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
727be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
72815e32d5dSMike Smith {
72915e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
73015e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
73115e32d5dSMike Smith     struct resource_list_entry	*rle;
73215e32d5dSMike Smith 
73315e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
73415e32d5dSMike Smith     if (!rle)
73515e32d5dSMike Smith 	return(ENOENT);
73615e32d5dSMike Smith 
73715e32d5dSMike Smith     if (startp)
73815e32d5dSMike Smith 	*startp = rle->start;
73915e32d5dSMike Smith     if (countp)
74015e32d5dSMike Smith 	*countp = rle->count;
74115e32d5dSMike Smith 
74215e32d5dSMike Smith     return (0);
74315e32d5dSMike Smith }
74415e32d5dSMike Smith 
74515e32d5dSMike Smith static struct resource *
74615e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
74715e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
74815e32d5dSMike Smith {
74915e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
75015e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
75115e32d5dSMike Smith 
752be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
753be2b1797SNate Lawson 	    flags));
75415e32d5dSMike Smith }
75515e32d5dSMike Smith 
75615e32d5dSMike Smith static int
75715e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
75815e32d5dSMike Smith {
75915e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
76015e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
76115e32d5dSMike Smith 
76215e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
76315e32d5dSMike Smith }
76415e32d5dSMike Smith 
765dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
766dc750869SNate Lawson struct resource *
767dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
768dc750869SNate Lawson {
769dc750869SNate Lawson     int type;
770dc750869SNate Lawson 
771dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
772dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
773dc750869SNate Lawson 	return (NULL);
774dc750869SNate Lawson 
775dc750869SNate Lawson     switch (gas->AddressSpaceId) {
776dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
777dc750869SNate Lawson 	type = SYS_RES_MEMORY;
778dc750869SNate Lawson 	break;
779dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
780dc750869SNate Lawson 	type = SYS_RES_IOPORT;
781dc750869SNate Lawson 	break;
782dc750869SNate Lawson     default:
783dc750869SNate Lawson 	return (NULL);
784dc750869SNate Lawson     }
785dc750869SNate Lawson 
786dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
787dc750869SNate Lawson     return (bus_alloc_resource(dev, type, rid, 0, ~0, 1, RF_ACTIVE));
788dc750869SNate Lawson }
789dc750869SNate Lawson 
79015e32d5dSMike Smith /*
791bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
792bc0f2195SMike Smith  */
793bc0f2195SMike Smith #define PNP_EISAID(s)				\
794bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
795bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
796bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
797bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
798bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
799bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
800bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
801bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
802bc0f2195SMike Smith 
8031e4925e8SNate Lawson static uint32_t
80432d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
805bc0f2195SMike Smith {
8061e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
8071e4925e8SNate Lawson     ACPI_BUFFER		buf;
808bc0f2195SMike Smith     ACPI_HANDLE		h;
809bc0f2195SMike Smith     ACPI_STATUS		error;
810bc0f2195SMike Smith     u_int32_t		pnpid;
811fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
81232d18aa5SMike Smith 
813b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
81432d18aa5SMike Smith 
81532d18aa5SMike Smith     pnpid = 0;
816bd189fe7SNate Lawson     buf.Pointer = NULL;
817bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
818bd189fe7SNate Lawson 
81932d18aa5SMike Smith     ACPI_LOCK;
82032d18aa5SMike Smith 
8216fca9360SNate Lawson     /* Fetch and validate the HID. */
82232d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
823bd189fe7SNate Lawson 	goto out;
8246fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
8256fca9360SNate Lawson     if (ACPI_FAILURE(error))
826bd189fe7SNate Lawson 	goto out;
8271e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
82832d18aa5SMike Smith 
8291e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
8301e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
831be2b1797SNate Lawson 
832bd189fe7SNate Lawson out:
833bd189fe7SNate Lawson     if (buf.Pointer != NULL)
8341e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
83532d18aa5SMike Smith     ACPI_UNLOCK;
83632d18aa5SMike Smith     return_VALUE (pnpid);
83732d18aa5SMike Smith }
83832d18aa5SMike Smith 
8391e4925e8SNate Lawson static int
8401e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
841b2566207STakanori Watanabe {
8421e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
8431e4925e8SNate Lawson     ACPI_BUFFER		buf;
844b2566207STakanori Watanabe     ACPI_HANDLE		h;
845b2566207STakanori Watanabe     ACPI_STATUS		error;
8461e4925e8SNate Lawson     uint32_t		*pnpid;
8471e4925e8SNate Lawson     int			valid, i;
848b2566207STakanori Watanabe     ACPI_LOCK_DECL;
849b2566207STakanori Watanabe 
850b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
851b2566207STakanori Watanabe 
8521e4925e8SNate Lawson     pnpid = cids;
8531e4925e8SNate Lawson     valid = 0;
854bd189fe7SNate Lawson     buf.Pointer = NULL;
855bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
856bd189fe7SNate Lawson 
857b2566207STakanori Watanabe     ACPI_LOCK;
858b2566207STakanori Watanabe 
8591e4925e8SNate Lawson     /* Fetch and validate the CID */
860b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
861bd189fe7SNate Lawson 	goto out;
8621e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
8631e4925e8SNate Lawson     if (ACPI_FAILURE(error))
864bd189fe7SNate Lawson 	goto out;
8651e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
8661e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
867b2566207STakanori Watanabe 	goto out;
868b2566207STakanori Watanabe 
8691e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
8701e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
8711e4925e8SNate Lawson     for (i = 0; i < count; i++) {
8721e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
8731e4925e8SNate Lawson 	    continue;
8741e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
8751e4925e8SNate Lawson 	valid++;
876b2566207STakanori Watanabe     }
877b2566207STakanori Watanabe 
8781e4925e8SNate Lawson out:
879bd189fe7SNate Lawson     if (buf.Pointer != NULL)
8801e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
8811e4925e8SNate Lawson     ACPI_UNLOCK;
8821e4925e8SNate Lawson     return_VALUE (valid);
8831e4925e8SNate Lawson }
884b2566207STakanori Watanabe 
88532d18aa5SMike Smith static int
88632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
88732d18aa5SMike Smith {
8881e4925e8SNate Lawson     int			result, cid_count, i;
8891e4925e8SNate Lawson     uint32_t		lid, cids[8];
890bc0f2195SMike Smith 
891b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
892bc0f2195SMike Smith 
893bc0f2195SMike Smith     /*
894bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
895bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
896bc0f2195SMike Smith      * that to happen, so don't ever return it.
897bc0f2195SMike Smith      */
898bc0f2195SMike Smith     result = ENXIO;
899bc0f2195SMike Smith 
900be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
901b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
9021e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
903bc0f2195SMike Smith     while (ids && ids->ip_id) {
9041e4925e8SNate Lawson 	if (lid == ids->ip_id) {
905bc0f2195SMike Smith 	    result = 0;
906bc0f2195SMike Smith 	    goto out;
907bc0f2195SMike Smith 	}
9081e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
9091e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
9101e4925e8SNate Lawson 		result = 0;
9111e4925e8SNate Lawson 		goto out;
9121e4925e8SNate Lawson 	    }
9131e4925e8SNate Lawson 	}
914bc0f2195SMike Smith 	ids++;
915bc0f2195SMike Smith     }
916be2b1797SNate Lawson 
917bc0f2195SMike Smith  out:
918bc0f2195SMike Smith     return_VALUE (result);
919bc0f2195SMike Smith }
920bc0f2195SMike Smith 
921bc0f2195SMike Smith /*
92215e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
92315e32d5dSMike Smith  *
924be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
925be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
92615e32d5dSMike Smith  */
92715e32d5dSMike Smith static void
92815e32d5dSMike Smith acpi_probe_children(device_t bus)
92915e32d5dSMike Smith {
93015e32d5dSMike Smith     ACPI_HANDLE	parent;
931be2b1797SNate Lawson     ACPI_STATUS	status;
9327d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
93315e32d5dSMike Smith     int		i;
93415e32d5dSMike Smith 
935b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
936cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
9370ae55423SMike Smith 
938be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
9394c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
94015e32d5dSMike Smith     bus_generic_probe(bus);
94115e32d5dSMike Smith 
94215e32d5dSMike Smith     /*
94315e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
94415e32d5dSMike Smith      * we find.
94515e32d5dSMike Smith      *
94615e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
947be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
948be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
949be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
95015e32d5dSMike Smith      */
9514c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
952be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
953be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
954be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
955be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
956be2b1797SNate Lawson 			      bus, NULL);
957be2b1797SNate Lawson 	}
958be2b1797SNate Lawson     }
95915e32d5dSMike Smith 
96015e32d5dSMike Smith     /*
96115e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
96215e32d5dSMike Smith      */
9634c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
96415e32d5dSMike Smith     bus_generic_attach(bus);
96515e32d5dSMike Smith 
96615e32d5dSMike Smith     /*
96715e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
96815e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
96915e32d5dSMike Smith      */
9704c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
97115e32d5dSMike Smith     bus_generic_attach(bus);
9720ae55423SMike Smith 
973bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
9740ae55423SMike Smith     return_VOID;
97515e32d5dSMike Smith }
97615e32d5dSMike Smith 
97715e32d5dSMike Smith /*
97815e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
97915e32d5dSMike Smith  * it.
98015e32d5dSMike Smith  */
98115e32d5dSMike Smith static ACPI_STATUS
98215e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
98315e32d5dSMike Smith {
98415e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
98515e32d5dSMike Smith     device_t		child, bus = (device_t)context;
98615e32d5dSMike Smith 
987b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
9880ae55423SMike Smith 
989be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
9900ae55423SMike Smith     if (acpi_avoid(handle))
9910ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
9920ae55423SMike Smith 
993b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
99415e32d5dSMike Smith 	switch(type) {
99515e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
99615e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
99715e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
99815e32d5dSMike Smith 	case ACPI_TYPE_POWER:
9990ae55423SMike Smith 	    if (acpi_disabled("children"))
10000ae55423SMike Smith 		break;
1001be2b1797SNate Lawson 
100215e32d5dSMike Smith 	    /*
100315e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
100415e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
100515e32d5dSMike Smith 	     */
1006be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1007be2b1797SNate Lawson 			     acpi_name(handle)));
100815e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1009bc0f2195SMike Smith 	    if (child == NULL)
1010bc0f2195SMike Smith 		break;
101115e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1012bc0f2195SMike Smith 
1013bc0f2195SMike Smith 	    /*
1014b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1015b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1016b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1017b519f9d6SMike Smith 	     */
1018be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1019b519f9d6SMike Smith 		device_disable(child);
1020b519f9d6SMike Smith 		break;
1021b519f9d6SMike Smith 	    }
1022b519f9d6SMike Smith 
1023b519f9d6SMike Smith 	    /*
1024bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1025bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1026bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1027bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1028bc0f2195SMike Smith 	     * device not to have any resources.
1029bc0f2195SMike Smith 	     */
1030bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
1031bc0f2195SMike Smith 
1032be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1033b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
10340ae55423SMike Smith 	    break;
103515e32d5dSMike Smith 	}
103615e32d5dSMike Smith     }
1037be2b1797SNate Lawson 
10380ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
103915e32d5dSMike Smith }
104015e32d5dSMike Smith 
104115e32d5dSMike Smith static void
104215e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
104315e32d5dSMike Smith {
1044c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1045c6a78e98STakanori Watanabe 
1046cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1047cb9b0d80SMike Smith 
104815e32d5dSMike Smith     /*
10490ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
105015e32d5dSMike Smith      * will be turned on again on some laptops.
105115e32d5dSMike Smith      *
105215e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
105315e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
105415e32d5dSMike Smith      *     shutdown process.
105515e32d5dSMike Smith      */
1056c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1057c6a78e98STakanori Watanabe 	acpi_Disable(sc);
105815e32d5dSMike Smith }
105915e32d5dSMike Smith 
106015e32d5dSMike Smith static void
106115e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
106215e32d5dSMike Smith {
106315e32d5dSMike Smith     ACPI_STATUS	status;
106415e32d5dSMike Smith 
1065cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1066cb9b0d80SMike Smith 
1067be2b1797SNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1068e1a90ae1SNate Lawson 	printf("Powering system off using ACPI\n");
1069be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(acpi_off_state);
1070be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1071b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1072b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
1073b9c780d6SMitsuru IWASAKI 	    return;
1074b9c780d6SMitsuru IWASAKI 	}
107555398047SNate Lawson 	ACPI_DISABLE_IRQS();
1076be2b1797SNate Lawson 	status = AcpiEnterSleepState(acpi_off_state);
1077be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1078bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
107915e32d5dSMike Smith 	} else {
108015e32d5dSMike Smith 	    DELAY(1000000);
108115e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
108215e32d5dSMike Smith 	}
1083494ae560SMitsuru IWASAKI     } else {
1084e1a90ae1SNate Lawson 	printf("Shutting down ACPI\n");
1085494ae560SMitsuru IWASAKI 	AcpiTerminate();
108615e32d5dSMike Smith     }
108715e32d5dSMike Smith }
108815e32d5dSMike Smith 
108913d4f7baSMitsuru IWASAKI static void
109013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
109113d4f7baSMitsuru IWASAKI {
109213d4f7baSMitsuru IWASAKI     static int	first_time = 1;
109313d4f7baSMitsuru IWASAKI 
1094cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1095cb9b0d80SMike Smith 
109613d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1097be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
109859ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0);
109959ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
110013d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
1101be2b1797SNate Lawson 				     acpi_eventhandler_power_button_for_sleep,
1102be2b1797SNate Lawson 				     sc);
1103be2b1797SNate Lawson 	if (first_time)
11046c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
110513d4f7baSMitsuru IWASAKI     }
1106be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
110759ddeb18SNate Lawson 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0);
110859ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
110913d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
1110be2b1797SNate Lawson 				     acpi_eventhandler_sleep_button_for_sleep,
1111be2b1797SNate Lawson 				     sc);
1112be2b1797SNate Lawson 	if (first_time)
11136c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
111413d4f7baSMitsuru IWASAKI     }
111513d4f7baSMitsuru IWASAKI 
111613d4f7baSMitsuru IWASAKI     first_time = 0;
111713d4f7baSMitsuru IWASAKI }
111813d4f7baSMitsuru IWASAKI 
111915e32d5dSMike Smith /*
1120c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1121c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1122c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1123c5ba0be4SMike Smith  */
1124c5ba0be4SMike Smith BOOLEAN
1125c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1126c5ba0be4SMike Smith {
11271e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1128c5ba0be4SMike Smith     ACPI_HANDLE		h;
11291e4925e8SNate Lawson     ACPI_BUFFER		buf;
1130c5ba0be4SMike Smith     ACPI_STATUS		error;
11311e4925e8SNate Lawson     int			ret;
1132c5ba0be4SMike Smith 
1133cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1134cb9b0d80SMike Smith 
11351e4925e8SNate Lawson     ret = FALSE;
1136c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1137c5ba0be4SMike Smith 	return (FALSE);
11381e4925e8SNate Lawson     buf.Pointer = NULL;
11391e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
11406fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11416fca9360SNate Lawson     if (ACPI_FAILURE(error))
1142c5ba0be4SMike Smith 	return (FALSE);
11431e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1144be2b1797SNate Lawson 
1145be2b1797SNate Lawson     /* If no _STA method, must be present */
11461e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
11471e4925e8SNate Lawson 	ret = TRUE;
1148be2b1797SNate Lawson 
1149be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
11501e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
11511e4925e8SNate Lawson 	ret = TRUE;
1152be2b1797SNate Lawson 
11531e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
11541e4925e8SNate Lawson     return (ret);
1155c5ba0be4SMike Smith }
1156c5ba0be4SMike Smith 
1157c5ba0be4SMike Smith /*
1158b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1159b53f2771SMike Smith  */
1160b53f2771SMike Smith BOOLEAN
1161b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1162b53f2771SMike Smith {
11631e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1164b53f2771SMike Smith     ACPI_HANDLE		h;
11651e4925e8SNate Lawson     ACPI_BUFFER		buf;
1166b53f2771SMike Smith     ACPI_STATUS		error;
11671e4925e8SNate Lawson     int			ret;
1168b53f2771SMike Smith 
1169b53f2771SMike Smith     ACPI_ASSERTLOCK;
1170b53f2771SMike Smith 
11711e4925e8SNate Lawson     ret = FALSE;
1172b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1173b53f2771SMike Smith 	return (FALSE);
11741e4925e8SNate Lawson     buf.Pointer = NULL;
11751e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
11766fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11776fca9360SNate Lawson     if (ACPI_FAILURE(error))
1178b53f2771SMike Smith 	return (FALSE);
11791e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1180be2b1797SNate Lawson 
1181be2b1797SNate Lawson     /* If no _STA method, must be present */
11821e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
11831e4925e8SNate Lawson 	ret = TRUE;
1184be2b1797SNate Lawson 
1185be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
11861e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
11871e4925e8SNate Lawson 	ret = TRUE;
1188be2b1797SNate Lawson 
11891e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
11901e4925e8SNate Lawson     return (ret);
1191b53f2771SMike Smith }
1192b53f2771SMike Smith 
1193b53f2771SMike Smith /*
119415e32d5dSMike Smith  * Match a HID string against a device
119515e32d5dSMike Smith  */
119615e32d5dSMike Smith BOOLEAN
119715e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
119815e32d5dSMike Smith {
11991e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
120015e32d5dSMike Smith     ACPI_HANDLE		h;
12011e4925e8SNate Lawson     ACPI_BUFFER		buf;
120215e32d5dSMike Smith     ACPI_STATUS		error;
12031e4925e8SNate Lawson     int			ret, i;
120415e32d5dSMike Smith 
1205cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1206cb9b0d80SMike Smith 
12071e4925e8SNate Lawson     ret = FALSE;
12084d332989SMike Smith     if (hid == NULL)
120915e32d5dSMike Smith 	return (FALSE);
121015e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
121115e32d5dSMike Smith 	return (FALSE);
12121e4925e8SNate Lawson     buf.Pointer = NULL;
12131e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12146fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12156fca9360SNate Lawson     if (ACPI_FAILURE(error))
121615e32d5dSMike Smith 	return (FALSE);
12171e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1218be2b1797SNate Lawson 
12191e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0) {
12201e4925e8SNate Lawson 	if (strcmp(hid, devinfo->HardwareId.Value) == 0)
12211e4925e8SNate Lawson 	    ret = TRUE;
12221e4925e8SNate Lawson     } else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
12231e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
12241e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
12251e4925e8SNate Lawson 		ret = TRUE;
12261e4925e8SNate Lawson 		break;
12271e4925e8SNate Lawson 	    }
12281e4925e8SNate Lawson 	}
12291e4925e8SNate Lawson     }
1230be2b1797SNate Lawson 
12311e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12321e4925e8SNate Lawson     return (ret);
123315e32d5dSMike Smith }
123415e32d5dSMike Smith 
123515e32d5dSMike Smith /*
1236c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1237c5ba0be4SMike Smith  * or one if its parents.
1238c5ba0be4SMike Smith  */
1239c5ba0be4SMike Smith ACPI_STATUS
1240c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1241c5ba0be4SMike Smith {
1242c5ba0be4SMike Smith     ACPI_HANDLE		r;
1243c5ba0be4SMike Smith     ACPI_STATUS		status;
1244c5ba0be4SMike Smith 
1245cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1246cb9b0d80SMike Smith 
1247be2b1797SNate Lawson     /* Walk back up the tree to the root */
1248c5ba0be4SMike Smith     for (;;) {
1249be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1250be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1251c5ba0be4SMike Smith 	    *result = r;
1252c5ba0be4SMike Smith 	    return (AE_OK);
1253c5ba0be4SMike Smith 	}
1254c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1255c5ba0be4SMike Smith 	    return (AE_OK);
1256b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1257c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1258c5ba0be4SMike Smith 	parent = r;
1259c5ba0be4SMike Smith     }
1260c5ba0be4SMike Smith }
1261c5ba0be4SMike Smith 
1262c5ba0be4SMike Smith /*
1263c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1264c5ba0be4SMike Smith  */
1265c5ba0be4SMike Smith ACPI_BUFFER *
1266c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1267c5ba0be4SMike Smith {
1268c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1269c5ba0be4SMike Smith 
1270c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1271c5ba0be4SMike Smith 	return (NULL);
1272c5ba0be4SMike Smith     buf->Length = size;
1273c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1274c5ba0be4SMike Smith     return (buf);
1275c5ba0be4SMike Smith }
1276c5ba0be4SMike Smith 
1277c5ba0be4SMike Smith /*
1278c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1279c5ba0be4SMike Smith  */
1280c5ba0be4SMike Smith ACPI_STATUS
1281c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1282c5ba0be4SMike Smith {
1283be2b1797SNate Lawson     ACPI_STATUS	status;
1284c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1285c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1286c5ba0be4SMike Smith 
1287cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1288cb9b0d80SMike Smith 
1289c5ba0be4SMike Smith     if (handle == NULL)
1290c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
12910c7da7acSJohn Baldwin 
12920c7da7acSJohn Baldwin     /*
12930c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
12940c7da7acSJohn Baldwin      * a method that will return an Integer.
12950c7da7acSJohn Baldwin      */
1296c5ba0be4SMike Smith     buf.Pointer = &param;
1297c5ba0be4SMike Smith     buf.Length = sizeof(param);
1298be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1299be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1300be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1301c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1302be2b1797SNate Lawson 	else
1303be2b1797SNate Lawson 	    status = AE_TYPE;
1304c5ba0be4SMike Smith     }
13050c7da7acSJohn Baldwin 
13060c7da7acSJohn Baldwin     /*
13070c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
13080c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
13090c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
13100c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
13110c7da7acSJohn Baldwin      *
13120c7da7acSJohn Baldwin      * This is a hack.
13130c7da7acSJohn Baldwin      */
1314be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1315b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1316be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
13170c7da7acSJohn Baldwin 	} else {
1318be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1319be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1320be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
13210c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
13220c7da7acSJohn Baldwin 	}
1323f97739daSNate Lawson     }
1324be2b1797SNate Lawson     return (status);
1325c5ba0be4SMike Smith }
1326c5ba0be4SMike Smith 
1327c573e654SMitsuru IWASAKI ACPI_STATUS
1328c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
1329c573e654SMitsuru IWASAKI {
1330c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1331c573e654SMitsuru IWASAKI     int		i;
1332c573e654SMitsuru IWASAKI 
1333c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1334c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1335c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1336c573e654SMitsuru IWASAKI 	return (AE_OK);
1337c573e654SMitsuru IWASAKI     }
1338c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1339c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1340c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1341c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1342be2b1797SNate Lawson 
1343c573e654SMitsuru IWASAKI     *number = 0;
1344c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1345c573e654SMitsuru IWASAKI 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
1346c573e654SMitsuru IWASAKI     return (AE_OK);
1347c573e654SMitsuru IWASAKI }
1348c573e654SMitsuru IWASAKI 
1349c5ba0be4SMike Smith /*
1350c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1351c5ba0be4SMike Smith  * function for each element.
1352c5ba0be4SMike Smith  *
1353c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1354c5ba0be4SMike Smith  */
1355c5ba0be4SMike Smith ACPI_STATUS
1356be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1357be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1358c5ba0be4SMike Smith {
1359c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1360c5ba0be4SMike Smith     int		i;
1361c5ba0be4SMike Smith 
1362be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1363c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1364c5ba0be4SMike Smith 
1365be2b1797SNate Lawson     /* Iterate over components */
1366be2b1797SNate Lawson     i = 0;
1367be2b1797SNate Lawson     comp = pkg->Package.Elements;
1368be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1369c5ba0be4SMike Smith 	func(comp, arg);
1370c5ba0be4SMike Smith 
1371c5ba0be4SMike Smith     return (AE_OK);
1372c5ba0be4SMike Smith }
1373c5ba0be4SMike Smith 
13746f69255bSMike Smith /*
13756f69255bSMike Smith  * Find the (index)th resource object in a set.
13766f69255bSMike Smith  */
13776f69255bSMike Smith ACPI_STATUS
13786d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
13796f69255bSMike Smith {
13806d63101aSMike Smith     ACPI_RESOURCE	*rp;
13816f69255bSMike Smith     int			i;
13826f69255bSMike Smith 
13836d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
13846f69255bSMike Smith     i = index;
13856d63101aSMike Smith     while (i-- > 0) {
1386be2b1797SNate Lawson 	/* Range check */
13876d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
13886f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1389be2b1797SNate Lawson 
1390be2b1797SNate Lawson 	/* Check for terminator */
1391be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
13926d63101aSMike Smith 	    return (AE_NOT_FOUND);
13936d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
13946f69255bSMike Smith     }
13956f69255bSMike Smith     if (resp != NULL)
13966d63101aSMike Smith 	*resp = rp;
1397be2b1797SNate Lawson 
13986d63101aSMike Smith     return (AE_OK);
13996d63101aSMike Smith }
14006d63101aSMike Smith 
14016d63101aSMike Smith /*
14026d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
14036d63101aSMike Smith  *
14046d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
14056d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
14066d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
14076d63101aSMike Smith  * resources.
14086d63101aSMike Smith  */
14096d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
14106d63101aSMike Smith 
14116d63101aSMike Smith ACPI_STATUS
14126d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
14136d63101aSMike Smith {
14146d63101aSMike Smith     ACPI_RESOURCE	*rp;
14156d63101aSMike Smith     void		*newp;
14166d63101aSMike Smith 
1417be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
14186d63101aSMike Smith     if (buf->Pointer == NULL) {
14196d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
14206d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
14216d63101aSMike Smith 	    return (AE_NO_MEMORY);
14226d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
14236d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
14246d63101aSMike Smith 	rp->Length = 0;
14256d63101aSMike Smith     }
14266d63101aSMike Smith     if (res == NULL)
14276d63101aSMike Smith 	return (AE_OK);
14286d63101aSMike Smith 
14296d63101aSMike Smith     /*
14306d63101aSMike Smith      * Scan the current buffer looking for the terminator.
14316d63101aSMike Smith      * This will either find the terminator or hit the end
14326d63101aSMike Smith      * of the buffer and return an error.
14336d63101aSMike Smith      */
14346d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
14356d63101aSMike Smith     for (;;) {
1436be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
14376d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
14386d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1439be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
14406d63101aSMike Smith 	    break;
14416d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
14426d63101aSMike Smith     }
14436d63101aSMike Smith 
14446d63101aSMike Smith     /*
14456d63101aSMike Smith      * Check the size of the buffer and expand if required.
14466d63101aSMike Smith      *
14476d63101aSMike Smith      * Required size is:
14486d63101aSMike Smith      *	size of existing resources before terminator +
14496d63101aSMike Smith      *	size of new resource and header +
14506d63101aSMike Smith      * 	size of terminator.
14516d63101aSMike Smith      *
14526d63101aSMike Smith      * Note that this loop should really only run once, unless
14536d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
14546d63101aSMike Smith      */
14556d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
14566d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
14576d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
14586d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
14596d63101aSMike Smith 	    return (AE_NO_MEMORY);
14606d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1461a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1462a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
14636d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
14646d63101aSMike Smith 	buf->Pointer = newp;
14656d63101aSMike Smith 	buf->Length += buf->Length;
14666d63101aSMike Smith     }
14676d63101aSMike Smith 
1468be2b1797SNate Lawson     /* Insert the new resource. */
14696d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
14706d63101aSMike Smith 
1471be2b1797SNate Lawson     /* And add the terminator. */
14726d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
14736d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
14746d63101aSMike Smith     rp->Length = 0;
14756d63101aSMike Smith 
14766f69255bSMike Smith     return (AE_OK);
14776f69255bSMike Smith }
1478c5ba0be4SMike Smith 
1479da14ac9fSJohn Baldwin /*
1480da14ac9fSJohn Baldwin  * Set interrupt model.
1481da14ac9fSJohn Baldwin  */
1482da14ac9fSJohn Baldwin ACPI_STATUS
1483da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1484da14ac9fSJohn Baldwin {
1485da14ac9fSJohn Baldwin     ACPI_OBJECT_LIST ArgList;
1486da14ac9fSJohn Baldwin     ACPI_OBJECT Arg;
1487da14ac9fSJohn Baldwin 
1488da14ac9fSJohn Baldwin     Arg.Type = ACPI_TYPE_INTEGER;
1489da14ac9fSJohn Baldwin     Arg.Integer.Value = model;
1490da14ac9fSJohn Baldwin     ArgList.Count = 1;
1491da14ac9fSJohn Baldwin     ArgList.Pointer = &Arg;
1492da14ac9fSJohn Baldwin     return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
1493da14ac9fSJohn Baldwin }
1494da14ac9fSJohn Baldwin 
1495ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1496ece50487SMitsuru IWASAKI 
1497ece50487SMitsuru IWASAKI static void
1498ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1499ece50487SMitsuru IWASAKI {
1500ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1501ece50487SMitsuru IWASAKI }
1502c30382dfSMitsuru IWASAKI 
150315e32d5dSMike Smith /*
150415e32d5dSMike Smith  * Set the system sleep state
150515e32d5dSMike Smith  *
1506be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
150715e32d5dSMike Smith  */
150815e32d5dSMike Smith ACPI_STATUS
150915e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
151015e32d5dSMike Smith {
151115e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
151256d8cb57SMitsuru IWASAKI     UINT8	TypeA;
151356d8cb57SMitsuru IWASAKI     UINT8	TypeB;
151415e32d5dSMike Smith 
1515b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1516cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
15170ae55423SMike Smith 
151898175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
15196397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
152098175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
15216397624dSMitsuru IWASAKI 
152298175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1523ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1524ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1525ece50487SMitsuru IWASAKI 
152615e32d5dSMike Smith     switch (state) {
152715e32d5dSMike Smith     case ACPI_STATE_S1:
15286161544cSTakanori Watanabe     case ACPI_STATE_S2:
15296161544cSTakanori Watanabe     case ACPI_STATE_S3:
15306161544cSTakanori Watanabe     case ACPI_STATE_S4:
1531be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
153298175614SNate Lawson 	if (status == AE_NOT_FOUND) {
153398175614SNate Lawson 	    device_printf(sc->acpi_dev,
153498175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
153598175614SNate Lawson 	    break;
153698175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1537be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1538be2b1797SNate Lawson 			  AcpiFormatException(status));
153956d8cb57SMitsuru IWASAKI 	    break;
154056d8cb57SMitsuru IWASAKI 	}
154156d8cb57SMitsuru IWASAKI 
1542a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1543a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1544a1fccb47SMitsuru IWASAKI 
1545be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
154615e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
154715e32d5dSMike Smith 	    /*
154815e32d5dSMike Smith 	     * Re-wake the system.
154915e32d5dSMike Smith 	     *
155015e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
155115e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
155215e32d5dSMike Smith 	     *     current bus interface does not provide for this.
155315e32d5dSMike Smith 	     */
155415e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
15550ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
155615e32d5dSMike Smith 	}
1557b9c780d6SMitsuru IWASAKI 
1558be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1559be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1560b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1561b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1562b9c780d6SMitsuru IWASAKI 	    break;
1563b9c780d6SMitsuru IWASAKI 	}
1564b9c780d6SMitsuru IWASAKI 
1565be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1566ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1567ff01efb5SMitsuru IWASAKI 
15686161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
15696161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
15706161544cSTakanori Watanabe 
1571be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
157259ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
157359ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
157459ddeb18SNate Lawson 
15756161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1576a1fccb47SMitsuru IWASAKI 	    }
15776161544cSTakanori Watanabe 
15786161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1579be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1580964679ceSMitsuru IWASAKI 		AcpiEnable();
15816161544cSTakanori Watanabe 	} else {
1582be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1583be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1584be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1585be2b1797SNate Lawson 			      AcpiFormatException(status));
1586c30382dfSMitsuru IWASAKI 		break;
158715e32d5dSMike Smith 	    }
15886161544cSTakanori Watanabe 	}
1589ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
159015e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
159115e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
159213d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
159315e32d5dSMike Smith 	break;
159415e32d5dSMike Smith     case ACPI_STATE_S5:
159515e32d5dSMike Smith 	/*
159615e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
159715e32d5dSMike Smith 	 * shutdown handlers.
159815e32d5dSMike Smith 	 */
159915e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
160015e32d5dSMike Smith 	break;
160198175614SNate Lawson     case ACPI_STATE_S0:
160215e32d5dSMike Smith     default:
160315e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
160415e32d5dSMike Smith 	break;
160515e32d5dSMike Smith     }
1606ece50487SMitsuru IWASAKI 
160798175614SNate Lawson     /* Disable a second sleep request for a short period */
1608ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1609ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1610ece50487SMitsuru IWASAKI 
16110ae55423SMike Smith     return_ACPI_STATUS (status);
161215e32d5dSMike Smith }
161315e32d5dSMike Smith 
161415e32d5dSMike Smith /*
161515e32d5dSMike Smith  * Enable/Disable ACPI
161615e32d5dSMike Smith  */
161715e32d5dSMike Smith ACPI_STATUS
161815e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
161915e32d5dSMike Smith {
162015e32d5dSMike Smith     ACPI_STATUS	status;
162115e32d5dSMike Smith     u_int32_t	flags;
162215e32d5dSMike Smith 
1623b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1624cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16250ae55423SMike Smith 
162615e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
162715e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1628be2b1797SNate Lawson     if (!sc->acpi_enabled)
162915e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1630be2b1797SNate Lawson     else
163115e32d5dSMike Smith 	status = AE_OK;
1632be2b1797SNate Lawson 
163315e32d5dSMike Smith     if (status == AE_OK)
163415e32d5dSMike Smith 	sc->acpi_enabled = 1;
1635be2b1797SNate Lawson 
16360ae55423SMike Smith     return_ACPI_STATUS (status);
163715e32d5dSMike Smith }
163815e32d5dSMike Smith 
163915e32d5dSMike Smith ACPI_STATUS
164015e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
164115e32d5dSMike Smith {
164215e32d5dSMike Smith     ACPI_STATUS	status;
164315e32d5dSMike Smith 
1644b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1645cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16460ae55423SMike Smith 
1647be2b1797SNate Lawson     if (sc->acpi_enabled)
164815e32d5dSMike Smith 	status = AcpiDisable();
1649be2b1797SNate Lawson     else
165015e32d5dSMike Smith 	status = AE_OK;
1651be2b1797SNate Lawson 
165215e32d5dSMike Smith     if (status == AE_OK)
165315e32d5dSMike Smith 	sc->acpi_enabled = 0;
1654be2b1797SNate Lawson 
16550ae55423SMike Smith     return_ACPI_STATUS (status);
165615e32d5dSMike Smith }
165715e32d5dSMike Smith 
165815e32d5dSMike Smith /*
165915e32d5dSMike Smith  * ACPI Event Handlers
166015e32d5dSMike Smith  */
166115e32d5dSMike Smith 
166215e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
166315e32d5dSMike Smith 
166415e32d5dSMike Smith static void
166515e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
166615e32d5dSMike Smith {
1667fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1668b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
166915e32d5dSMike Smith 
1670cb9b0d80SMike Smith     ACPI_LOCK;
1671a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
167215e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1673cb9b0d80SMike Smith     ACPI_UNLOCK;
16740ae55423SMike Smith     return_VOID;
167515e32d5dSMike Smith }
167615e32d5dSMike Smith 
167715e32d5dSMike Smith static void
167815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
167915e32d5dSMike Smith {
1680fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1681b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
16820ae55423SMike Smith 
168315e32d5dSMike Smith     /* Well, what to do? :-) */
16840ae55423SMike Smith 
1685cb9b0d80SMike Smith     ACPI_LOCK;
1686cb9b0d80SMike Smith     ACPI_UNLOCK;
1687cb9b0d80SMike Smith 
16880ae55423SMike Smith     return_VOID;
168915e32d5dSMike Smith }
169015e32d5dSMike Smith 
169115e32d5dSMike Smith /*
169215e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
169315e32d5dSMike Smith  */
169415e32d5dSMike Smith UINT32
169515e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
169615e32d5dSMike Smith {
169715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
169815e32d5dSMike Smith 
1699b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17000ae55423SMike Smith 
170115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
17020ae55423SMike Smith 
1703b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
170415e32d5dSMike Smith }
170515e32d5dSMike Smith 
170615e32d5dSMike Smith UINT32
170715e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
170815e32d5dSMike Smith {
170915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
171015e32d5dSMike Smith 
1711b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17120ae55423SMike Smith 
171315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
17140ae55423SMike Smith 
1715b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
171615e32d5dSMike Smith }
171715e32d5dSMike Smith 
171815e32d5dSMike Smith UINT32
171915e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
172015e32d5dSMike Smith {
172115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
172215e32d5dSMike Smith 
1723b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17240ae55423SMike Smith 
172515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
17260ae55423SMike Smith 
1727b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
172815e32d5dSMike Smith }
172915e32d5dSMike Smith 
173015e32d5dSMike Smith UINT32
173115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
173215e32d5dSMike Smith {
173315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
173415e32d5dSMike Smith 
1735b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17360ae55423SMike Smith 
173715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
17380ae55423SMike Smith 
1739b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
174015e32d5dSMike Smith }
174115e32d5dSMike Smith 
174215e32d5dSMike Smith /*
174315e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
174415e32d5dSMike Smith  */
174515e32d5dSMike Smith struct acpi_staticbuf {
174615e32d5dSMike Smith     ACPI_BUFFER	buffer;
174715e32d5dSMike Smith     char	data[512];
174815e32d5dSMike Smith };
174915e32d5dSMike Smith 
175015e32d5dSMike Smith char *
175115e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
175215e32d5dSMike Smith {
175315e32d5dSMike Smith     static struct acpi_staticbuf	buf;
175415e32d5dSMike Smith 
1755cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1756cb9b0d80SMike Smith 
175715e32d5dSMike Smith     buf.buffer.Length = 512;
175815e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
175915e32d5dSMike Smith 
1760b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
176115e32d5dSMike Smith 	return (buf.buffer.Pointer);
1762be2b1797SNate Lawson 
176315e32d5dSMike Smith     return ("(unknown path)");
176415e32d5dSMike Smith }
176515e32d5dSMike Smith 
176615e32d5dSMike Smith /*
176715e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
176815e32d5dSMike Smith  * parts of the namespace.
176915e32d5dSMike Smith  */
177015e32d5dSMike Smith int
177115e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
177215e32d5dSMike Smith {
17733c600cfbSJohn Baldwin     char	*cp, *env, *np;
177415e32d5dSMike Smith     int		len;
177515e32d5dSMike Smith 
177615e32d5dSMike Smith     np = acpi_name(handle);
177715e32d5dSMike Smith     if (*np == '\\')
177815e32d5dSMike Smith 	np++;
17793c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
178015e32d5dSMike Smith 	return (0);
178115e32d5dSMike Smith 
1782be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
17833c600cfbSJohn Baldwin     cp = env;
178415e32d5dSMike Smith     for (;;) {
178515e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
178615e32d5dSMike Smith 	    cp++;
178715e32d5dSMike Smith 	if (*cp == 0)
178815e32d5dSMike Smith 	    break;
178915e32d5dSMike Smith 	len = 0;
179015e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
179115e32d5dSMike Smith 	    len++;
1792d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
17933c600cfbSJohn Baldwin 	    freeenv(env);
17940ae55423SMike Smith 	    return(1);
1795d786139cSMaxime Henrion 	}
17960ae55423SMike Smith 	cp += len;
17970ae55423SMike Smith     }
17983c600cfbSJohn Baldwin     freeenv(env);
1799be2b1797SNate Lawson 
18000ae55423SMike Smith     return (0);
18010ae55423SMike Smith }
18020ae55423SMike Smith 
18030ae55423SMike Smith /*
18040ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
18050ae55423SMike Smith  */
18060ae55423SMike Smith int
18070ae55423SMike Smith acpi_disabled(char *subsys)
18080ae55423SMike Smith {
180978689b15SMaxime Henrion     char	*cp, *env;
18100ae55423SMike Smith     int		len;
18110ae55423SMike Smith 
181278689b15SMaxime Henrion     if ((env = getenv("debug.acpi.disable")) == NULL)
18130ae55423SMike Smith 	return (0);
181478689b15SMaxime Henrion     if (!strcmp(env, "all")) {
181578689b15SMaxime Henrion 	freeenv(env);
18160ae55423SMike Smith 	return (1);
1817d786139cSMaxime Henrion     }
18180ae55423SMike Smith 
18190ae55423SMike Smith     /* scan the disable list checking for a match */
182078689b15SMaxime Henrion     cp = env;
18210ae55423SMike Smith     for (;;) {
18220ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
18230ae55423SMike Smith 	    cp++;
18240ae55423SMike Smith 	if (*cp == 0)
18250ae55423SMike Smith 	    break;
18260ae55423SMike Smith 	len = 0;
18270ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
18280ae55423SMike Smith 	    len++;
1829d786139cSMaxime Henrion 	if (!strncmp(cp, subsys, len)) {
183078689b15SMaxime Henrion 	    freeenv(env);
183115e32d5dSMike Smith 	    return (1);
1832d786139cSMaxime Henrion 	}
183315e32d5dSMike Smith 	cp += len;
183415e32d5dSMike Smith     }
183578689b15SMaxime Henrion     freeenv(env);
1836be2b1797SNate Lawson 
183715e32d5dSMike Smith     return (0);
183815e32d5dSMike Smith }
183915e32d5dSMike Smith 
184015e32d5dSMike Smith /*
1841a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1842a1fccb47SMitsuru IWASAKI  */
1843a1fccb47SMitsuru IWASAKI void
1844a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1845a1fccb47SMitsuru IWASAKI {
1846a1fccb47SMitsuru IWASAKI     ACPI_OBJECT_LIST		ArgList;
1847a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			Arg;
1848a1fccb47SMitsuru IWASAKI 
1849a1fccb47SMitsuru IWASAKI     /*
1850a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1851a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1852a1fccb47SMitsuru IWASAKI      */
1853a1fccb47SMitsuru IWASAKI 
1854a1fccb47SMitsuru IWASAKI     ArgList.Count = 1;
1855a1fccb47SMitsuru IWASAKI     ArgList.Pointer = &Arg;
1856a1fccb47SMitsuru IWASAKI 
1857a1fccb47SMitsuru IWASAKI     Arg.Type = ACPI_TYPE_INTEGER;
1858a1fccb47SMitsuru IWASAKI     Arg.Integer.Value = enable;
1859a1fccb47SMitsuru IWASAKI 
1860a1fccb47SMitsuru IWASAKI     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
1861a1fccb47SMitsuru IWASAKI }
1862a1fccb47SMitsuru IWASAKI 
1863a1fccb47SMitsuru IWASAKI void
1864a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
1865a1fccb47SMitsuru IWASAKI {
1866a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
1867a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
1868a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
1869a1fccb47SMitsuru IWASAKI     ACPI_OBJECT			*res;
1870a1fccb47SMitsuru IWASAKI 
1871a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1872a1fccb47SMitsuru IWASAKI 
1873be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
1874be2b1797SNate Lawson     if (sc == NULL)
1875a1fccb47SMitsuru IWASAKI 	return;
1876a1fccb47SMitsuru IWASAKI 
1877a1fccb47SMitsuru IWASAKI     /*
1878a1fccb47SMitsuru IWASAKI      * _PRW object is only required for devices that have the ability
1879a1fccb47SMitsuru IWASAKI      * to wake the system from a system sleeping state.
1880a1fccb47SMitsuru IWASAKI      */
1881a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
1882a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
1883be2b1797SNate Lawson     if (ACPI_FAILURE(status))
1884a1fccb47SMitsuru IWASAKI 	return;
1885a1fccb47SMitsuru IWASAKI 
1886a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
1887be2b1797SNate Lawson     if (res == NULL)
1888a1fccb47SMitsuru IWASAKI 	return;
1889a1fccb47SMitsuru IWASAKI 
1890a1fccb47SMitsuru IWASAKI     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
1891a1fccb47SMitsuru IWASAKI 	goto out;
1892a1fccb47SMitsuru IWASAKI     }
1893a1fccb47SMitsuru IWASAKI 
1894a1fccb47SMitsuru IWASAKI     /*
1895a1fccb47SMitsuru IWASAKI      * The element 1 of the _PRW object:
1896a1fccb47SMitsuru IWASAKI      * The lowest power system sleeping state that can be entered
1897a1fccb47SMitsuru IWASAKI      * while still providing wake functionality.
1898a1fccb47SMitsuru IWASAKI      * The sleeping state being entered must be greater or equal to
1899a1fccb47SMitsuru IWASAKI      * the power state declared in element 1 of the _PRW object.
1900a1fccb47SMitsuru IWASAKI      */
1901be2b1797SNate Lawson     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
1902a1fccb47SMitsuru IWASAKI 	goto out;
1903a1fccb47SMitsuru IWASAKI 
1904be2b1797SNate Lawson     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value)
1905a1fccb47SMitsuru IWASAKI 	goto out;
1906a1fccb47SMitsuru IWASAKI 
1907a1fccb47SMitsuru IWASAKI     /*
1908a1fccb47SMitsuru IWASAKI      * The element 0 of the _PRW object:
1909a1fccb47SMitsuru IWASAKI      */
1910a1fccb47SMitsuru IWASAKI     switch(res->Package.Elements[0].Type) {
1911a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
1912a1fccb47SMitsuru IWASAKI 	/*
1913a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
1914a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
1915a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
1916a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
1917a1fccb47SMitsuru IWASAKI 	 */
1918a1fccb47SMitsuru IWASAKI 
19196fca9360SNate Lawson 	status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value,
19206fca9360SNate Lawson 			       ACPI_EVENT_WAKE_ENABLE);
1921a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
1922a1fccb47SMitsuru IWASAKI 	    printf("%s: EnableEvent Failed\n", __func__);
1923a1fccb47SMitsuru IWASAKI 	break;
1924a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
1925a1fccb47SMitsuru IWASAKI 	/*
1926be2b1797SNate Lawson 	 * XXX TBD
1927be2b1797SNate Lawson 	 *
1928a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
1929a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
1930a1fccb47SMitsuru IWASAKI 	 * elements. The first is an object reference to the GPE Block
1931a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
1932a1fccb47SMitsuru IWASAKI 	 * event. The second element is numeric and it contains the bit
1933a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
1934a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
1935a1fccb47SMitsuru IWASAKI 	 * the wake event.
1936a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
1937a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
1938a1fccb47SMitsuru IWASAKI 	 */
1939a1fccb47SMitsuru IWASAKI 	break;
1940a1fccb47SMitsuru IWASAKI     default:
1941a1fccb47SMitsuru IWASAKI 	break;
1942a1fccb47SMitsuru IWASAKI     }
1943a1fccb47SMitsuru IWASAKI 
1944a1fccb47SMitsuru IWASAKI out:
1945a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
1946a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
1947a1fccb47SMitsuru IWASAKI }
1948a1fccb47SMitsuru IWASAKI 
1949a1fccb47SMitsuru IWASAKI /*
195015e32d5dSMike Smith  * Control interface.
195115e32d5dSMike Smith  *
19520ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
1953be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
1954be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
195515e32d5dSMike Smith  */
19560ae55423SMike Smith struct acpi_ioctl_hook
19570ae55423SMike Smith {
19580ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
19590ae55423SMike Smith     u_long			 cmd;
1960be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
19610ae55423SMike Smith     void			 *arg;
19620ae55423SMike Smith };
19630ae55423SMike Smith 
19640ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
19650ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
19660ae55423SMike Smith 
19670ae55423SMike Smith /*
19680ae55423SMike Smith  * Register an ioctl handler.
19690ae55423SMike Smith  */
19700ae55423SMike Smith int
1971be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
19720ae55423SMike Smith {
19730ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
19740ae55423SMike Smith 
19750ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
19760ae55423SMike Smith 	return (ENOMEM);
19770ae55423SMike Smith     hp->cmd = cmd;
19780ae55423SMike Smith     hp->fn = fn;
19790ae55423SMike Smith     hp->arg = arg;
19800ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
19810ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
19820ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
19830ae55423SMike Smith     }
19840ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
19850ae55423SMike Smith     return (0);
19860ae55423SMike Smith }
19870ae55423SMike Smith 
19880ae55423SMike Smith /*
19890ae55423SMike Smith  * Deregister an ioctl handler.
19900ae55423SMike Smith  */
19910ae55423SMike Smith void
1992be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
19930ae55423SMike Smith {
19940ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
19950ae55423SMike Smith 
19960ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
19970ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
19980ae55423SMike Smith 	    break;
19990ae55423SMike Smith 
20000ae55423SMike Smith     if (hp != NULL) {
20010ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
20020ae55423SMike Smith 	free(hp, M_ACPIDEV);
20030ae55423SMike Smith     }
20040ae55423SMike Smith }
20050ae55423SMike Smith 
200615e32d5dSMike Smith static int
20076b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
200815e32d5dSMike Smith {
200915e32d5dSMike Smith     return (0);
201015e32d5dSMike Smith }
201115e32d5dSMike Smith 
201215e32d5dSMike Smith static int
20136b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
201415e32d5dSMike Smith {
201515e32d5dSMike Smith     return (0);
201615e32d5dSMike Smith }
201715e32d5dSMike Smith 
201815e32d5dSMike Smith static int
20196b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
202015e32d5dSMike Smith {
202115e32d5dSMike Smith     struct acpi_softc		*sc;
20220ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
20230ae55423SMike Smith     int				error, xerror, state;
2024fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
202515e32d5dSMike Smith 
2026cb9b0d80SMike Smith     ACPI_LOCK;
2027cb9b0d80SMike Smith 
202815e32d5dSMike Smith     error = state = 0;
202915e32d5dSMike Smith     sc = dev->si_drv1;
203015e32d5dSMike Smith 
20310ae55423SMike Smith     /*
20320ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
20330ae55423SMike Smith      */
20340ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
20350ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
20360ae55423SMike Smith 	    if (hp->cmd == cmd) {
20370ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
20380ae55423SMike Smith 		if (xerror != 0)
20390ae55423SMike Smith 		    error = xerror;
2040917d44c8SMitsuru IWASAKI 		goto out;
20410ae55423SMike Smith 	    }
20420ae55423SMike Smith 	}
20430ae55423SMike Smith     }
20440ae55423SMike Smith 
20450ae55423SMike Smith     /*
2046a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2047a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2048a89fcf28STakanori Watanabe      * Not changing system behavior.
2049a89fcf28STakanori Watanabe      */
2050be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2051be2b1797SNate Lawson 	return (EPERM);
2052a89fcf28STakanori Watanabe 
2053be2b1797SNate Lawson     /* Core system ioctls. */
205415e32d5dSMike Smith     switch (cmd) {
205515e32d5dSMike Smith     case ACPIIO_ENABLE:
20560ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
205715e32d5dSMike Smith 	    error = ENXIO;
205815e32d5dSMike Smith 	break;
205915e32d5dSMike Smith     case ACPIIO_DISABLE:
20600ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
206115e32d5dSMike Smith 	    error = ENXIO;
206215e32d5dSMike Smith 	break;
206315e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
206415e32d5dSMike Smith 	if (!sc->acpi_enabled) {
206515e32d5dSMike Smith 	    error = ENXIO;
206615e32d5dSMike Smith 	    break;
206715e32d5dSMike Smith 	}
206815e32d5dSMike Smith 	state = *(int *)addr;
20692d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
20702d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
207115e32d5dSMike Smith 		error = EINVAL;
20722d610c46SNate Lawson 	} else {
20732d610c46SNate Lawson 	    error = EINVAL;
20742d610c46SNate Lawson 	}
207515e32d5dSMike Smith 	break;
207615e32d5dSMike Smith     default:
20770ae55423SMike Smith 	if (error == 0)
207815e32d5dSMike Smith 	    error = EINVAL;
207915e32d5dSMike Smith 	break;
208015e32d5dSMike Smith     }
2081917d44c8SMitsuru IWASAKI 
2082917d44c8SMitsuru IWASAKI out:
2083cb9b0d80SMike Smith     ACPI_UNLOCK;
208415e32d5dSMike Smith     return (error);
208515e32d5dSMike Smith }
208615e32d5dSMike Smith 
20871d073b1dSJohn Baldwin static int
2088d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2089d75de536SMitsuru IWASAKI {
2090d75de536SMitsuru IWASAKI     char sleep_state[4];
2091d75de536SMitsuru IWASAKI     char buf[16];
2092d75de536SMitsuru IWASAKI     int error;
2093d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2094d75de536SMitsuru IWASAKI 
2095d75de536SMitsuru IWASAKI     buf[0] = '\0';
2096d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2097d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2098d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2099d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2100d75de536SMitsuru IWASAKI 	}
2101d75de536SMitsuru IWASAKI     }
2102d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2103d75de536SMitsuru IWASAKI     return (error);
2104d75de536SMitsuru IWASAKI }
2105d75de536SMitsuru IWASAKI 
2106d75de536SMitsuru IWASAKI static int
21071d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
21081d073b1dSJohn Baldwin {
21091d073b1dSJohn Baldwin     char sleep_state[10];
21101d073b1dSJohn Baldwin     int error;
21111d073b1dSJohn Baldwin     u_int new_state, old_state;
21121d073b1dSJohn Baldwin 
21131d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2114b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
21151d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2116cb9b0d80SMike Smith     } else {
2117b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
21181d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
21191d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2120cb9b0d80SMike Smith     }
21211d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
21221d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2123be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2124be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
21251d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
21261d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
21271d073b1dSJohn Baldwin 		break;
2128cb9b0d80SMike Smith 	}
2129931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2130be2b1797SNate Lawson 	    if (new_state != old_state)
21311d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2132cb9b0d80SMike Smith 	} else {
21331d073b1dSJohn Baldwin 	    error = EINVAL;
21341d073b1dSJohn Baldwin 	}
2135cb9b0d80SMike Smith     }
2136be2b1797SNate Lawson 
21371d073b1dSJohn Baldwin     return (error);
21381d073b1dSJohn Baldwin }
21391d073b1dSJohn Baldwin 
21409b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
21419b937d48SNate Lawson void
21429b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
21439b937d48SNate Lawson {
21449b937d48SNate Lawson     char		notify_buf[16];
21459b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
21469b937d48SNate Lawson     ACPI_STATUS		status;
21479b937d48SNate Lawson 
21489b937d48SNate Lawson     if (subsystem == NULL)
21499b937d48SNate Lawson 	return;
21509b937d48SNate Lawson 
21519b937d48SNate Lawson     handle_buf.Pointer = NULL;
21529b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
21539b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
21549b937d48SNate Lawson     if (ACPI_FAILURE(status))
21559b937d48SNate Lawson 	return;
21569b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
21579b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
21589b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
21599b937d48SNate Lawson }
21609b937d48SNate Lawson 
216115e32d5dSMike Smith #ifdef ACPI_DEBUG
21620ae55423SMike Smith /*
21630ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
21640ae55423SMike Smith  *
21650ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
21660ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
21670ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
21680ae55423SMike Smith  * prefixing the bit name with !.
21690ae55423SMike Smith  */
217015e32d5dSMike Smith struct debugtag
217115e32d5dSMike Smith {
217215e32d5dSMike Smith     char	*name;
217315e32d5dSMike Smith     UINT32	value;
217415e32d5dSMike Smith };
217515e32d5dSMike Smith 
217615e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2177c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2178c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2179c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2180c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2181c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2182c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2183c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2184c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2185c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2186d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
21874c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2188d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2189297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
21904c1cdee6SMike Smith 
2191cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
21924c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
2193cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
2194cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
2195c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2196c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
2197c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
21984c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2199cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
22004c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
2201b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
220215e32d5dSMike Smith     {NULL, 0}
220315e32d5dSMike Smith };
220415e32d5dSMike Smith 
220515e32d5dSMike Smith static struct debugtag dbg_level[] = {
22064c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
22079501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
22089501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
22094c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
22109501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
22114c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2212d62ab2f4SMitsuru IWASAKI 
2213d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2214297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
22154c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
22164c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2217d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
22184c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
22194c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
22204c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
22214c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
22224c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
22234c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
22244c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
22254c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
22264c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
22274c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2228d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2229d62ab2f4SMitsuru IWASAKI 
2230d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2231d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2232d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2233d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2234d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
22354c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2236d62ab2f4SMitsuru IWASAKI 
2237d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2238d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2239d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2240d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2241d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2242d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2243d62ab2f4SMitsuru IWASAKI 
2244d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
224598479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
224698479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
224798479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
224898479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
224998479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
225015e32d5dSMike Smith     {NULL, 0}
225115e32d5dSMike Smith };
225215e32d5dSMike Smith 
225315e32d5dSMike Smith static void
225415e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
225515e32d5dSMike Smith {
225615e32d5dSMike Smith     char	*ep;
225715e32d5dSMike Smith     int		i, l;
22580ae55423SMike Smith     int		set;
225915e32d5dSMike Smith 
226015e32d5dSMike Smith     while (*cp) {
226115e32d5dSMike Smith 	if (isspace(*cp)) {
226215e32d5dSMike Smith 	    cp++;
226315e32d5dSMike Smith 	    continue;
226415e32d5dSMike Smith 	}
226515e32d5dSMike Smith 	ep = cp;
226615e32d5dSMike Smith 	while (*ep && !isspace(*ep))
226715e32d5dSMike Smith 	    ep++;
22680ae55423SMike Smith 	if (*cp == '!') {
22690ae55423SMike Smith 	    set = 0;
22700ae55423SMike Smith 	    cp++;
22710ae55423SMike Smith 	    if (cp == ep)
22720ae55423SMike Smith 		continue;
22730ae55423SMike Smith 	} else {
22740ae55423SMike Smith 	    set = 1;
22750ae55423SMike Smith 	}
227615e32d5dSMike Smith 	l = ep - cp;
227715e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
227815e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2279be2b1797SNate Lawson 		if (set)
228015e32d5dSMike Smith 		    *flag |= tag[i].value;
2281be2b1797SNate Lawson 		else
22820ae55423SMike Smith 		    *flag &= ~tag[i].value;
228315e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
228415e32d5dSMike Smith 	    }
228515e32d5dSMike Smith 	}
228615e32d5dSMike Smith 	cp = ep;
228715e32d5dSMike Smith     }
228815e32d5dSMike Smith }
228915e32d5dSMike Smith 
229015e32d5dSMike Smith static void
22912a4ac806SMike Smith acpi_set_debugging(void *junk)
229215e32d5dSMike Smith {
229394aae282SMike Barcroft     char	*cp;
229415e32d5dSMike Smith 
22951d7b121cSNate Lawson     if (cold) {
22960ae55423SMike Smith 	AcpiDbgLayer = 0;
22970ae55423SMike Smith 	AcpiDbgLevel = 0;
22981d7b121cSNate Lawson     }
22991d7b121cSNate Lawson 
230094aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
230115e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
230294aae282SMike Barcroft 	freeenv(cp);
230394aae282SMike Barcroft     }
230494aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
230515e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
230694aae282SMike Barcroft 	freeenv(cp);
230794aae282SMike Barcroft     }
23080ae55423SMike Smith 
23091d7b121cSNate Lawson     if (cold) {
23101d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
23111d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
23121d7b121cSNate Lawson     }
231315e32d5dSMike Smith }
2314be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2315be2b1797SNate Lawson 	NULL);
23161d7b121cSNate Lawson 
23171d7b121cSNate Lawson static int
23181d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
23191d7b121cSNate Lawson {
232054f6bca0SNate Lawson     int		 error, *dbg;
23211d7b121cSNate Lawson     struct	 debugtag *tag;
232254f6bca0SNate Lawson     struct	 sbuf sb;
23231d7b121cSNate Lawson 
232454f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
232554f6bca0SNate Lawson 	return (ENOMEM);
23261d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
23271d7b121cSNate Lawson 	tag = &dbg_layer[0];
23281d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
23291d7b121cSNate Lawson     } else {
23301d7b121cSNate Lawson 	tag = &dbg_level[0];
23311d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
23321d7b121cSNate Lawson     }
23331d7b121cSNate Lawson 
23341d7b121cSNate Lawson     /* Get old values if this is a get request. */
23351d7b121cSNate Lawson     if (*dbg == 0) {
233654f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
23371d7b121cSNate Lawson     } else if (req->newptr == NULL) {
23381d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
233954f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
234054f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
23411d7b121cSNate Lawson 	}
23421d7b121cSNate Lawson     }
234354f6bca0SNate Lawson     sbuf_trim(&sb);
234454f6bca0SNate Lawson     sbuf_finish(&sb);
23451d7b121cSNate Lawson 
234654f6bca0SNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
234754f6bca0SNate Lawson     sbuf_delete(&sb);
23481d7b121cSNate Lawson 
23491d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
23501d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
23511d7b121cSNate Lawson 	*dbg = 0;
23521d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
23531d7b121cSNate Lawson 	acpi_set_debugging(NULL);
23541d7b121cSNate Lawson     }
23551d7b121cSNate Lawson 
23561d7b121cSNate Lawson     return (error);
23571d7b121cSNate Lawson }
23581d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
23591d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
23601d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
23611d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
236215e32d5dSMike Smith #endif
2363f9390180SMitsuru IWASAKI 
2364f9390180SMitsuru IWASAKI static int
2365f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2366f9390180SMitsuru IWASAKI {
2367f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2368f9390180SMitsuru IWASAKI 	int	error;
2369f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2370f9390180SMitsuru IWASAKI 	va_list	ap;
2371f9390180SMitsuru IWASAKI 
2372f9390180SMitsuru IWASAKI 	error = 0;
2373f9390180SMitsuru IWASAKI 	switch (cmd) {
2374f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2375f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2376f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2377f9390180SMitsuru IWASAKI 			error = EINVAL;
2378f9390180SMitsuru IWASAKI 			goto out;
2379f9390180SMitsuru IWASAKI 		}
2380f9390180SMitsuru IWASAKI 
2381f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2382f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2383f9390180SMitsuru IWASAKI 		va_end(ap);
2384f9390180SMitsuru IWASAKI 
2385f9390180SMitsuru IWASAKI 		switch (state) {
2386f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2387f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2388f9390180SMitsuru IWASAKI 			break;
2389f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2390f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2391f9390180SMitsuru IWASAKI 			break;
2392f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2393f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2394f9390180SMitsuru IWASAKI 			break;
2395f9390180SMitsuru IWASAKI 		default:
2396f9390180SMitsuru IWASAKI 			error = EINVAL;
2397f9390180SMitsuru IWASAKI 			goto out;
2398f9390180SMitsuru IWASAKI 		}
2399f9390180SMitsuru IWASAKI 
2400f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2401f9390180SMitsuru IWASAKI 		break;
2402f9390180SMitsuru IWASAKI 	default:
2403f9390180SMitsuru IWASAKI 		error = EINVAL;
2404f9390180SMitsuru IWASAKI 		goto out;
2405f9390180SMitsuru IWASAKI 	}
2406f9390180SMitsuru IWASAKI 
2407f9390180SMitsuru IWASAKI out:
2408f9390180SMitsuru IWASAKI 	return (error);
2409f9390180SMitsuru IWASAKI }
2410f9390180SMitsuru IWASAKI 
2411f9390180SMitsuru IWASAKI static void
2412f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2413f9390180SMitsuru IWASAKI {
2414be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
24156c407052SMitsuru IWASAKI 	return;
2416f9390180SMitsuru IWASAKI 
2417f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2418f9390180SMitsuru IWASAKI }
2419f9390180SMitsuru IWASAKI 
2420f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2421