xref: /freebsd/sys/dev/acpica/acpi.c (revision fe12f24bb098d8d2a6c35394a53a65b3e64f83d9)
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>
38fe12f24bSPoul-Henning Kamp #include <sys/module.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
451611ea87SMitsuru IWASAKI #include <sys/linker.h>
46f9390180SMitsuru IWASAKI #include <sys/power.h>
4754f6bca0SNate Lawson #include <sys/sbuf.h>
48eeb3a05fSNate Lawson #include <sys/smp.h>
4915e32d5dSMike Smith 
5015e32d5dSMike Smith #include <machine/clock.h>
5115e32d5dSMike Smith #include <machine/resource.h>
52dc750869SNate Lawson #include <machine/bus.h>
53dc750869SNate Lawson #include <sys/rman.h>
54bc0f2195SMike Smith #include <isa/isavar.h>
55bc0f2195SMike Smith 
5615e32d5dSMike Smith #include "acpi.h"
5715e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5815e32d5dSMike Smith #include <dev/acpica/acpiio.h>
599b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
6015e32d5dSMike Smith 
6115e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6215e32d5dSMike Smith 
63be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
64cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
65b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
660ae55423SMike Smith 
6715e32d5dSMike Smith static d_open_t		acpiopen;
6815e32d5dSMike Smith static d_close_t	acpiclose;
6915e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7015e32d5dSMike Smith 
7115e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
72dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
73dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
747ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
757ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
767ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
777ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
7815e32d5dSMike Smith };
7915e32d5dSMike Smith 
80fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
81cb9b0d80SMike Smith struct mtx	acpi_mutex;
82fc0ea94aSJohn Baldwin #endif
83cb9b0d80SMike Smith 
843184cf5aSNate Lawson struct acpi_quirks {
853184cf5aSNate Lawson     char	*OemId;
863184cf5aSNate Lawson     uint32_t	OemRevision;
873184cf5aSNate Lawson     char	*value;
883184cf5aSNate Lawson };
893184cf5aSNate Lawson 
903184cf5aSNate Lawson #define ACPI_OEM_REV_ANY	0
913184cf5aSNate Lawson 
923184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = {
933184cf5aSNate Lawson #ifdef notyet
943184cf5aSNate Lawson     /* Bad PCI routing table.  Used on some SuperMicro boards. */
953184cf5aSNate Lawson     { "PTLTD ", 0x06040000, "pci_link" },
963184cf5aSNate Lawson #endif
973184cf5aSNate Lawson 
983184cf5aSNate Lawson     { NULL, 0, NULL }
993184cf5aSNate Lawson };
1003184cf5aSNate Lawson 
1016d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10215e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10315e32d5dSMike Smith static int	acpi_probe(device_t dev);
10415e32d5dSMike Smith static int	acpi_attach(device_t dev);
1053184cf5aSNate Lawson static void	acpi_quirks_set(void);
106be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
107be2b1797SNate Lawson 			int unit);
10815e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
109be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
110be2b1797SNate Lawson 			uintptr_t *result);
111be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
112be2b1797SNate Lawson 			uintptr_t value);
113be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
114be2b1797SNate Lawson 			int rid, u_long start, u_long count);
115be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
116be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
117be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
118be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
119be2b1797SNate Lawson 			u_long count, u_int flags);
120be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
121be2b1797SNate Lawson 			int rid, struct resource *r);
1221e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1231e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
124be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
125be2b1797SNate Lawson 			struct isa_pnp_id *ids);
12615e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
127be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
128be2b1797SNate Lawson 			void *context, void **status);
12915e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
13015e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
131eeb3a05fSNate Lawson static void	acpi_shutdown_poweroff(void *arg);
13213d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
133e8b4d56eSNate Lawson static int	acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw);
134e8b4d56eSNate Lawson static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context,
135e8b4d56eSNate Lawson 		    void **status);
136e8b4d56eSNate Lawson static int	acpi_wake_limit_walk(int sstate);
13788a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
13888a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
13915e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
14015e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
141d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1421d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
143f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
144879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
145879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
146879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
147879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
148879d6c23STakanori Watanabe 
14915e32d5dSMike Smith static device_method_t acpi_methods[] = {
15015e32d5dSMike Smith     /* Device interface */
15115e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
15215e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
15315e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
15456a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
15515e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
15615e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
15715e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
15815e32d5dSMike Smith 
15915e32d5dSMike Smith     /* Bus interface */
16015e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
16115e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
16215e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
16315e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
16415e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
16515e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
16615e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
16715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
168879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
169879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
17015e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
17115e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
17215e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
17315e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
17415e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
17515e32d5dSMike Smith 
176bc0f2195SMike Smith     /* ISA emulation */
177bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
178bc0f2195SMike Smith 
17915e32d5dSMike Smith     {0, 0}
18015e32d5dSMike Smith };
18115e32d5dSMike Smith 
18215e32d5dSMike Smith static driver_t acpi_driver = {
18315e32d5dSMike Smith     "acpi",
18415e32d5dSMike Smith     acpi_methods,
18515e32d5dSMike Smith     sizeof(struct acpi_softc),
18615e32d5dSMike Smith };
18715e32d5dSMike Smith 
1883273b005SMike Smith static devclass_t acpi_devclass;
1896d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
190a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
19115e32d5dSMike Smith 
192865b8d0bSNate Lawson static const char* sleep_state_names[] = {
193865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
194865b8d0bSNate Lawson 
1951d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1961d7b121cSNate Lawson static char acpi_ca_version[12];
1971d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1981d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
19915e32d5dSMike Smith 
20015e32d5dSMike Smith /*
201413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
202c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
203c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
204c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
205c9b8d77dSNate Lawson  * some IBMs use such code.
206413081d7SNate Lawson  */
207c9b8d77dSNate Lawson static int acpi_serialize_methods;
208413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
209413081d7SNate Lawson 
210c9b8d77dSNate Lawson /*
2116d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2126d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
213be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
214be2b1797SNate Lawson  * off it.
2156d63101aSMike Smith  */
2166d63101aSMike Smith static int
2176d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2186d63101aSMike Smith {
2196d63101aSMike Smith     switch(event) {
2206d63101aSMike Smith     case MOD_LOAD:
22187b45ed5SMitsuru IWASAKI 	if (!cold) {
22287b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2236d63101aSMike Smith 	    return (EPERM);
22487b45ed5SMitsuru IWASAKI 	}
2256d63101aSMike Smith 	break;
2266d63101aSMike Smith     case MOD_UNLOAD:
227f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2286d63101aSMike Smith 	    return (EBUSY);
229f9390180SMitsuru IWASAKI 	break;
2306d63101aSMike Smith     default:
2316d63101aSMike Smith 	break;
2326d63101aSMike Smith     }
2336d63101aSMike Smith     return (0);
2346d63101aSMike Smith }
2356d63101aSMike Smith 
2366d63101aSMike Smith /*
237bbc2815cSJohn Baldwin  * Perform early initialization.
23815e32d5dSMike Smith  */
239bbc2815cSJohn Baldwin ACPI_STATUS
240bbc2815cSJohn Baldwin acpi_Startup(void)
24115e32d5dSMike Smith {
242d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
243d786139cSMaxime Henrion     char *debugpoint;
24415e32d5dSMike Smith #endif
245bbc2815cSJohn Baldwin     static int error, started = 0;
24615e32d5dSMike Smith 
2471b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2481b8c233dSPeter Pentchev 
249bbc2815cSJohn Baldwin     if (started)
250bbc2815cSJohn Baldwin 	return_VALUE (error);
251bbc2815cSJohn Baldwin     started = 1;
25215e32d5dSMike Smith 
253fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
254be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2556008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
256fc0ea94aSJohn Baldwin #endif
257cb9b0d80SMike Smith 
258413081d7SNate Lawson     /*
259413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
260f3fc4f8bSNate Lawson      * uses UINT8 for some values and we have no tunable_byte.
261413081d7SNate Lawson      */
262f3fc4f8bSNate Lawson     AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods;
263413081d7SNate Lawson 
264be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
265d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
266d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
267d786139cSMaxime Henrion     if (debugpoint) {
268d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
26915e32d5dSMike Smith 	    acpi_EnterDebugger();
270d786139cSMaxime Henrion 	freeenv(debugpoint);
271d786139cSMaxime Henrion     }
27215e32d5dSMike Smith #endif
273b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
274bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
275bbc2815cSJohn Baldwin 	return_VALUE (error);
27615e32d5dSMike Smith     }
277d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
278d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
279d786139cSMaxime Henrion     if (debugpoint) {
280d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
28115e32d5dSMike Smith 	    acpi_EnterDebugger();
282d786139cSMaxime Henrion 	freeenv(debugpoint);
283d786139cSMaxime Henrion     }
28415e32d5dSMike Smith #endif
2851611ea87SMitsuru IWASAKI 
286b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
287bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
288bbc2815cSJohn Baldwin 	return_VALUE(error);
28915e32d5dSMike Smith     }
2903184cf5aSNate Lawson 
2913184cf5aSNate Lawson     /* Set up any quirks we have for this XSDT. */
2923184cf5aSNate Lawson     acpi_quirks_set();
2934e376d58SNate Lawson     if (acpi_disabled("acpi"))
2944e376d58SNate Lawson 	return_VALUE (AE_ERROR);
2953184cf5aSNate Lawson 
296bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
297bbc2815cSJohn Baldwin }
298bbc2815cSJohn Baldwin 
299bbc2815cSJohn Baldwin /*
300bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
301bbc2815cSJohn Baldwin  */
302bbc2815cSJohn Baldwin static void
303bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
304bbc2815cSJohn Baldwin {
305bbc2815cSJohn Baldwin     device_t	child;
306bbc2815cSJohn Baldwin 
307bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
308bbc2815cSJohn Baldwin 
309bbc2815cSJohn Baldwin     if (!cold)
310bbc2815cSJohn Baldwin 	return_VOID;
311bbc2815cSJohn Baldwin 
312bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
313bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
314bbc2815cSJohn Baldwin 	return_VOID;
315bbc2815cSJohn Baldwin 
316bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
317bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
318bbc2815cSJohn Baldwin 	return_VOID;
319bbc2815cSJohn Baldwin 
320bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
321bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
322bbc2815cSJohn Baldwin 	return_VOID;
32315e32d5dSMike Smith 
3244e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3254e376d58SNate Lawson 
326be2b1797SNate Lawson     /* Attach the actual ACPI device. */
32715e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
32815e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
3290ae55423SMike Smith 	return_VOID;
33015e32d5dSMike Smith     }
33115e32d5dSMike Smith }
33215e32d5dSMike Smith 
33315e32d5dSMike Smith /*
33415e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
33515e32d5dSMike Smith  */
33615e32d5dSMike Smith static int
33715e32d5dSMike Smith acpi_probe(device_t dev)
33815e32d5dSMike Smith {
33915e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
34015e32d5dSMike Smith     char		buf[20];
34115e32d5dSMike Smith     int			error;
3422ccd1cacSNate Lawson     struct sbuf		sb;
3432ccd1cacSNate Lawson     ACPI_STATUS		status;
344fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
34515e32d5dSMike Smith 
346b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
347f9390180SMitsuru IWASAKI 
348f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
349f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
350be2b1797SNate Lawson 
351f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
352f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
353f9390180SMitsuru IWASAKI     }
354f9390180SMitsuru IWASAKI 
355cb9b0d80SMike Smith     ACPI_LOCK;
3560ae55423SMike Smith 
357b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
358be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
359be2b1797SNate Lawson 		      AcpiFormatException(status));
360cb9b0d80SMike Smith 	error = ENXIO;
361cb9b0d80SMike Smith     } else {
3622ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3632ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3642ccd1cacSNate Lawson 	sbuf_trim(&sb);
3652ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3662ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3672ccd1cacSNate Lawson 	sbuf_trim(&sb);
3682ccd1cacSNate Lawson 	sbuf_finish(&sb);
3692ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3702ccd1cacSNate Lawson 	sbuf_delete(&sb);
371cb9b0d80SMike Smith 	error = 0;
372cb9b0d80SMike Smith     }
373cb9b0d80SMike Smith     ACPI_UNLOCK;
374cb9b0d80SMike Smith     return_VALUE(error);
37515e32d5dSMike Smith }
37615e32d5dSMike Smith 
37715e32d5dSMike Smith static int
37815e32d5dSMike Smith acpi_attach(device_t dev)
37915e32d5dSMike Smith {
38015e32d5dSMike Smith     struct acpi_softc	*sc;
381cb9b0d80SMike Smith     ACPI_STATUS		status;
382ea27c63eSNate Lawson     int			error, state;
38332d18aa5SMike Smith     UINT32		flags;
384ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
385498d464fSMitsuru IWASAKI     char		*env;
386d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
387d786139cSMaxime Henrion     char		*debugpoint;
38815e32d5dSMike Smith #endif
389fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
39015e32d5dSMike Smith 
391b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
392cb9b0d80SMike Smith     ACPI_LOCK;
39315e32d5dSMike Smith     sc = device_get_softc(dev);
39415e32d5dSMike Smith     bzero(sc, sizeof(*sc));
39515e32d5dSMike Smith     sc->acpi_dev = dev;
39615e32d5dSMike Smith 
397d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
398d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
399d786139cSMaxime Henrion     if (debugpoint) {
400d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
40115e32d5dSMike Smith 	    acpi_EnterDebugger();
402d786139cSMaxime Henrion 	freeenv(debugpoint);
403d786139cSMaxime Henrion     }
40415e32d5dSMike Smith #endif
40515e32d5dSMike Smith 
406be2b1797SNate Lawson     /* Install the default address space handlers. */
407cb9b0d80SMike Smith     error = ENXIO;
408be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
409be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
410be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
411be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
412be2b1797SNate Lawson 		      AcpiFormatException(status));
413cb9b0d80SMike Smith 	goto out;
41415e32d5dSMike Smith     }
415be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
416be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
417be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
418be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
419be2b1797SNate Lawson 		      AcpiFormatException(status));
420cb9b0d80SMike Smith 	goto out;
42115e32d5dSMike Smith     }
422be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
423be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
424be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
425be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
426be2b1797SNate Lawson 		      AcpiFormatException(status));
427cb9b0d80SMike Smith 	goto out;
42815e32d5dSMike Smith     }
42915e32d5dSMike Smith 
43015e32d5dSMike Smith     /*
43115e32d5dSMike Smith      * Bring ACPI fully online.
43215e32d5dSMike Smith      *
433be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
434be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
435be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
436be2b1797SNate Lawson      * object init pass.
43715e32d5dSMike Smith      *
43832d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
43932d18aa5SMike Smith      *
440be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
441be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
44215e32d5dSMike Smith      */
443d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
444d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
445d786139cSMaxime Henrion     if (debugpoint) {
446d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
44715e32d5dSMike Smith 	    acpi_EnterDebugger();
448d786139cSMaxime Henrion 	freeenv(debugpoint);
449d786139cSMaxime Henrion     }
45015e32d5dSMike Smith #endif
45132d18aa5SMike Smith     flags = 0;
452d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
45332d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
454b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
455be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
456be2b1797SNate Lawson 		      AcpiFormatException(status));
457cb9b0d80SMike Smith 	goto out;
45815e32d5dSMike Smith     }
45915e32d5dSMike Smith 
460f8335e3aSNate Lawson     /*
461f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
462f8335e3aSNate Lawson      * the namespace has been evaluated.
463f8335e3aSNate Lawson      */
464f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
465f8335e3aSNate Lawson 
466b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
467be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
468be2b1797SNate Lawson 		      AcpiFormatException(status));
469b69ed3f4SMitsuru IWASAKI 	goto out;
470b69ed3f4SMitsuru IWASAKI     }
471b69ed3f4SMitsuru IWASAKI 
47215e32d5dSMike Smith     /*
4731d073b1dSJohn Baldwin      * Setup our sysctl tree.
4741d073b1dSJohn Baldwin      *
4751d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4761d073b1dSJohn Baldwin      */
4771d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4781d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4791d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4801d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4811d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
482d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
483d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
484d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4851d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4861d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4871d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4881d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4891d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4901d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4911d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4921d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
493f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
494f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
495f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
496f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
497f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
498f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4992d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
500ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
501ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
502ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5031611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
5041611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
5051611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5062d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
5072d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
508c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
509c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
510c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
511bf0c18ecSNate Lawson 
512bf0c18ecSNate Lawson     /*
5132b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
514bf0c18ecSNate Lawson      * stabilize.
515bf0c18ecSNate Lawson      */
5162b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
517043498dfSNate Lawson     sc->acpi_disable_on_poweroff = 0;
5182d644607SMitsuru IWASAKI     if (bootverbose)
5192d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
520498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
521498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
522498d464fSMitsuru IWASAKI 	freeenv(env);
523498d464fSMitsuru IWASAKI     }
5241d073b1dSJohn Baldwin 
5252d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5262d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5272d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
5282d610c46SNate Lawson 
5291d073b1dSJohn Baldwin     /*
530ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
531ea27c63eSNate Lawson      * to NONE by default to avoid surprising users.
53215e32d5dSMike Smith      */
533ea27c63eSNate Lawson     sc->acpi_power_button_sx = ACPI_STATE_S5;
534ea27c63eSNate Lawson     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
535f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
536f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
53715e32d5dSMike Smith 
538ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
539ea27c63eSNate Lawson     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
540ea27c63eSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
541ea27c63eSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
542ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
543ea27c63eSNate Lawson 	    break;
544ea27c63eSNate Lawson 	}
545ea27c63eSNate Lawson 
54613d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
54715e32d5dSMike Smith 
54815e32d5dSMike Smith     /*
54915e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
55015e32d5dSMike Smith      */
551d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
552d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
553d786139cSMaxime Henrion     if (debugpoint) {
554d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
55515e32d5dSMike Smith 	    acpi_EnterDebugger();
556d786139cSMaxime Henrion 	freeenv(debugpoint);
557d786139cSMaxime Henrion     }
55815e32d5dSMike Smith #endif
55915e32d5dSMike Smith 
560be2b1797SNate Lawson     /* Register our shutdown handlers */
561be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
562be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
563be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
564be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
56515e32d5dSMike Smith 
56615e32d5dSMike Smith     /*
56715e32d5dSMike Smith      * Register our acpi event handlers.
56815e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
56915e32d5dSMike Smith      */
570be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
571be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
572be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
573be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
57415e32d5dSMike Smith 
575be2b1797SNate Lawson     /* Flag our initial states. */
57615e32d5dSMike Smith     sc->acpi_enabled = 1;
57715e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
578ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
57915e32d5dSMike Smith 
580be2b1797SNate Lawson     /* Create the control device */
581a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5822a4689e8SRobert Watson 			      "acpi");
58315e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
58415e32d5dSMike Smith 
585d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
586d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
587d786139cSMaxime Henrion     if (debugpoint) {
588be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
58915e32d5dSMike Smith 	    acpi_EnterDebugger();
590d786139cSMaxime Henrion 	freeenv(debugpoint);
591d786139cSMaxime Henrion     }
59215e32d5dSMike Smith #endif
593f86214b6SMitsuru IWASAKI 
59491da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
595be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
596c573e654SMitsuru IWASAKI 	goto out;
597c573e654SMitsuru IWASAKI #endif
598c573e654SMitsuru IWASAKI 
599be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
600f86214b6SMitsuru IWASAKI 	goto out;
601f86214b6SMitsuru IWASAKI 
602f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
603f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
604f9390180SMitsuru IWASAKI 
605eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
606eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
607eeb6dba2SJohn Baldwin 
608cb9b0d80SMike Smith     error = 0;
609cb9b0d80SMike Smith 
610cb9b0d80SMike Smith  out:
611cb9b0d80SMike Smith     ACPI_UNLOCK;
612cb9b0d80SMike Smith     return_VALUE (error);
61315e32d5dSMike Smith }
61415e32d5dSMike Smith 
6153184cf5aSNate Lawson static void
6163184cf5aSNate Lawson acpi_quirks_set()
6173184cf5aSNate Lawson {
6183184cf5aSNate Lawson     XSDT_DESCRIPTOR *xsdt;
6193184cf5aSNate Lawson     struct acpi_quirks *quirk;
6203184cf5aSNate Lawson     char *env, *tmp;
6213184cf5aSNate Lawson     int len;
6223184cf5aSNate Lawson 
6234e376d58SNate Lawson     /*
6244e376d58SNate Lawson      * If the user loaded a custom table or disabled "quirks", leave
6254e376d58SNate Lawson      * the settings alone.
6264e376d58SNate Lawson      */
6273184cf5aSNate Lawson     len = 0;
6284e376d58SNate Lawson     if ((env = getenv("acpi_dsdt_load")) != NULL) {
6294e376d58SNate Lawson 	/* XXX No strcasecmp but this is good enough. */
6304e376d58SNate Lawson 	if (*env == 'Y' || *env == 'y')
6314e376d58SNate Lawson 	    goto out;
6324e376d58SNate Lawson 	freeenv(env);
6334e376d58SNate Lawson     }
6343184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) != NULL) {
6354e376d58SNate Lawson 	if (strstr("quirks", env) != NULL)
6363184cf5aSNate Lawson 	    goto out;
6373184cf5aSNate Lawson 	len = strlen(env);
6383184cf5aSNate Lawson     }
6393184cf5aSNate Lawson 
6403184cf5aSNate Lawson     /*
6413184cf5aSNate Lawson      * Search through our quirk table and concatenate the disabled
6423184cf5aSNate Lawson      * values with whatever we find.
6433184cf5aSNate Lawson      */
6443184cf5aSNate Lawson     xsdt = AcpiGbl_XSDT;
6453184cf5aSNate Lawson     for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
6463184cf5aSNate Lawson 	if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
6473184cf5aSNate Lawson 	    (xsdt->OemRevision == quirk->OemRevision ||
6483184cf5aSNate Lawson 	    quirk->OemRevision == ACPI_OEM_REV_ANY)) {
6493184cf5aSNate Lawson 		len += strlen(quirk->value) + 2;
6503184cf5aSNate Lawson 		if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
6513184cf5aSNate Lawson 		    goto out;
6523184cf5aSNate Lawson 		sprintf(tmp, "%s %s", env ? env : "", quirk->value);
6533184cf5aSNate Lawson 		setenv("debug.acpi.disabled", tmp);
6543184cf5aSNate Lawson 		free(tmp, M_TEMP);
6553184cf5aSNate Lawson 		break;
6563184cf5aSNate Lawson 	}
6573184cf5aSNate Lawson     }
6583184cf5aSNate Lawson 
6593184cf5aSNate Lawson out:
6603184cf5aSNate Lawson     if (env)
6613184cf5aSNate Lawson 	freeenv(env);
6623184cf5aSNate Lawson }
6633184cf5aSNate Lawson 
66415e32d5dSMike Smith /*
66515e32d5dSMike Smith  * Handle a new device being added
66615e32d5dSMike Smith  */
66715e32d5dSMike Smith static device_t
66815e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
66915e32d5dSMike Smith {
67015e32d5dSMike Smith     struct acpi_device	*ad;
67115e32d5dSMike Smith     device_t		child;
67215e32d5dSMike Smith 
673be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
67415e32d5dSMike Smith 	return (NULL);
67515e32d5dSMike Smith 
67615e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
67715e32d5dSMike Smith 
67815e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
67915e32d5dSMike Smith     if (child != NULL)
68015e32d5dSMike Smith 	device_set_ivars(child, ad);
68115e32d5dSMike Smith     return (child);
68215e32d5dSMike Smith }
68315e32d5dSMike Smith 
68415e32d5dSMike Smith static int
68515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
68615e32d5dSMike Smith {
68715e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
68815e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
68915e32d5dSMike Smith     int retval = 0;
69015e32d5dSMike Smith 
69115e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
6929ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
6939ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
6949ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
6959ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
69615e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
69715e32d5dSMike Smith 
69815e32d5dSMike Smith     return (retval);
69915e32d5dSMike Smith }
70015e32d5dSMike Smith 
70163600cc3SNate Lawson /* Location hint for devctl(8) */
70263600cc3SNate Lawson static int
703247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
704247648afSTakanori Watanabe     size_t buflen)
705247648afSTakanori Watanabe {
706247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
707247648afSTakanori Watanabe 
708247648afSTakanori Watanabe     if (dinfo->ad_handle)
709247648afSTakanori Watanabe 	snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
710247648afSTakanori Watanabe     else
711247648afSTakanori Watanabe 	snprintf(buf, buflen, "magic=unknown");
712247648afSTakanori Watanabe     return (0);
713247648afSTakanori Watanabe }
714247648afSTakanori Watanabe 
71563600cc3SNate Lawson /* PnP information for devctl(8) */
71663600cc3SNate Lawson static int
717247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
718247648afSTakanori Watanabe     size_t buflen)
719247648afSTakanori Watanabe {
720247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
72163600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
72263600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
723247648afSTakanori Watanabe     char *end;
724247648afSTakanori Watanabe     int error;
725247648afSTakanori Watanabe 
726247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
727247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
728247648afSTakanori Watanabe 
729247648afSTakanori Watanabe     if (error)
730247648afSTakanori Watanabe 	snprintf(buf, buflen, "Unknown");
731247648afSTakanori Watanabe     else
732247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
733247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
734247648afSTakanori Watanabe 		 adinfo->HardwareId.Value : "UNKNOWN",
73563600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
73663600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
737247648afSTakanori Watanabe 
738247648afSTakanori Watanabe     if (adinfo)
739247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
740247648afSTakanori Watanabe 
741247648afSTakanori Watanabe     return (0);
742247648afSTakanori Watanabe }
743247648afSTakanori Watanabe 
744247648afSTakanori Watanabe /*
74515e32d5dSMike Smith  * Handle per-device ivars
74615e32d5dSMike Smith  */
74715e32d5dSMike Smith static int
74815e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
74915e32d5dSMike Smith {
75015e32d5dSMike Smith     struct acpi_device	*ad;
75115e32d5dSMike Smith 
75215e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
75315e32d5dSMike Smith 	printf("device has no ivars\n");
75415e32d5dSMike Smith 	return (ENOENT);
75515e32d5dSMike Smith     }
75615e32d5dSMike Smith 
757be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
75815e32d5dSMike Smith     switch(index) {
75915e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
76015e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
76115e32d5dSMike Smith 	break;
76215e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
76315e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
76415e32d5dSMike Smith 	break;
76515e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
76615e32d5dSMike Smith 	*(void **)result = ad->ad_private;
76715e32d5dSMike Smith 	break;
76832d18aa5SMike Smith     case ISA_IVAR_VENDORID:
76932d18aa5SMike Smith     case ISA_IVAR_SERIAL:
77032d18aa5SMike Smith     case ISA_IVAR_COMPATID:
77132d18aa5SMike Smith 	*(int *)result = -1;
77232d18aa5SMike Smith 	break;
77332d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
77432d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
77532d18aa5SMike Smith 	break;
77615e32d5dSMike Smith     default:
77715e32d5dSMike Smith 	return (ENOENT);
77815e32d5dSMike Smith     }
779be2b1797SNate Lawson 
78015e32d5dSMike Smith     return (0);
78115e32d5dSMike Smith }
78215e32d5dSMike Smith 
78315e32d5dSMike Smith static int
78415e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
78515e32d5dSMike Smith {
78615e32d5dSMike Smith     struct acpi_device	*ad;
78715e32d5dSMike Smith 
78815e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
78915e32d5dSMike Smith 	printf("device has no ivars\n");
79015e32d5dSMike Smith 	return (ENOENT);
79115e32d5dSMike Smith     }
79215e32d5dSMike Smith 
79315e32d5dSMike Smith     switch(index) {
79415e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
79515e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
79615e32d5dSMike Smith 	break;
79715e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
79815e32d5dSMike Smith 	ad->ad_magic = (int)value;
79915e32d5dSMike Smith 	break;
80015e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
80115e32d5dSMike Smith 	ad->ad_private = (void *)value;
80215e32d5dSMike Smith 	break;
80315e32d5dSMike Smith     default:
8042ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
80515e32d5dSMike Smith 	return (ENOENT);
80615e32d5dSMike Smith     }
807be2b1797SNate Lawson 
80815e32d5dSMike Smith     return (0);
80915e32d5dSMike Smith }
81015e32d5dSMike Smith 
81115e32d5dSMike Smith /*
81215e32d5dSMike Smith  * Handle child resource allocation/removal
81315e32d5dSMike Smith  */
81415e32d5dSMike Smith static int
815be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
816be2b1797SNate Lawson 		  u_long start, u_long count)
81715e32d5dSMike Smith {
81815e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
81915e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
82015e32d5dSMike Smith 
82115e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
82215e32d5dSMike Smith 
82315e32d5dSMike Smith     return(0);
82415e32d5dSMike Smith }
82515e32d5dSMike Smith 
82615e32d5dSMike Smith static int
827be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
828be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
82915e32d5dSMike Smith {
83015e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
83115e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
83215e32d5dSMike Smith     struct resource_list_entry	*rle;
83315e32d5dSMike Smith 
83415e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
83515e32d5dSMike Smith     if (!rle)
83615e32d5dSMike Smith 	return(ENOENT);
83715e32d5dSMike Smith 
83815e32d5dSMike Smith     if (startp)
83915e32d5dSMike Smith 	*startp = rle->start;
84015e32d5dSMike Smith     if (countp)
84115e32d5dSMike Smith 	*countp = rle->count;
84215e32d5dSMike Smith 
84315e32d5dSMike Smith     return (0);
84415e32d5dSMike Smith }
84515e32d5dSMike Smith 
84615e32d5dSMike Smith static struct resource *
84715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
84815e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
84915e32d5dSMike Smith {
85015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
85115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
85215e32d5dSMike Smith 
853be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
854be2b1797SNate Lawson 	    flags));
85515e32d5dSMike Smith }
85615e32d5dSMike Smith 
85715e32d5dSMike Smith static int
85815e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
85915e32d5dSMike Smith {
86015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
86115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
86215e32d5dSMike Smith 
86315e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
86415e32d5dSMike Smith }
86515e32d5dSMike Smith 
866dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
867dc750869SNate Lawson struct resource *
868dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
869dc750869SNate Lawson {
870dc750869SNate Lawson     int type;
871dc750869SNate Lawson 
872dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
873dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
874dc750869SNate Lawson 	return (NULL);
875dc750869SNate Lawson 
876dc750869SNate Lawson     switch (gas->AddressSpaceId) {
877dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
878dc750869SNate Lawson 	type = SYS_RES_MEMORY;
879dc750869SNate Lawson 	break;
880dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
881dc750869SNate Lawson 	type = SYS_RES_IOPORT;
882dc750869SNate Lawson 	break;
883dc750869SNate Lawson     default:
884dc750869SNate Lawson 	return (NULL);
885dc750869SNate Lawson     }
886dc750869SNate Lawson 
887dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
8885f96beb9SNate Lawson     return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
889dc750869SNate Lawson }
890dc750869SNate Lawson 
89115e32d5dSMike Smith /*
892bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
893bc0f2195SMike Smith  */
894bc0f2195SMike Smith #define PNP_EISAID(s)				\
895bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
896bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
897bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
898bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
899bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
900bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
901bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
902bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
903bc0f2195SMike Smith 
9041e4925e8SNate Lawson static uint32_t
90532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
906bc0f2195SMike Smith {
9071e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9081e4925e8SNate Lawson     ACPI_BUFFER		buf;
909bc0f2195SMike Smith     ACPI_HANDLE		h;
910bc0f2195SMike Smith     ACPI_STATUS		error;
911bc0f2195SMike Smith     u_int32_t		pnpid;
912fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
91332d18aa5SMike Smith 
914b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
91532d18aa5SMike Smith 
91632d18aa5SMike Smith     pnpid = 0;
917bd189fe7SNate Lawson     buf.Pointer = NULL;
918bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
919bd189fe7SNate Lawson 
92032d18aa5SMike Smith     ACPI_LOCK;
92132d18aa5SMike Smith 
9226fca9360SNate Lawson     /* Fetch and validate the HID. */
92332d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
924bd189fe7SNate Lawson 	goto out;
9256fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9266fca9360SNate Lawson     if (ACPI_FAILURE(error))
927bd189fe7SNate Lawson 	goto out;
9281e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
92932d18aa5SMike Smith 
9301e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
9311e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
932be2b1797SNate Lawson 
933bd189fe7SNate Lawson out:
934bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9351e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
93632d18aa5SMike Smith     ACPI_UNLOCK;
93732d18aa5SMike Smith     return_VALUE (pnpid);
93832d18aa5SMike Smith }
93932d18aa5SMike Smith 
9401e4925e8SNate Lawson static int
9411e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
942b2566207STakanori Watanabe {
9431e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9441e4925e8SNate Lawson     ACPI_BUFFER		buf;
945b2566207STakanori Watanabe     ACPI_HANDLE		h;
946b2566207STakanori Watanabe     ACPI_STATUS		error;
9471e4925e8SNate Lawson     uint32_t		*pnpid;
9481e4925e8SNate Lawson     int			valid, i;
949b2566207STakanori Watanabe     ACPI_LOCK_DECL;
950b2566207STakanori Watanabe 
951b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
952b2566207STakanori Watanabe 
9531e4925e8SNate Lawson     pnpid = cids;
9541e4925e8SNate Lawson     valid = 0;
955bd189fe7SNate Lawson     buf.Pointer = NULL;
956bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
957bd189fe7SNate Lawson 
958b2566207STakanori Watanabe     ACPI_LOCK;
959b2566207STakanori Watanabe 
9601e4925e8SNate Lawson     /* Fetch and validate the CID */
961b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
962bd189fe7SNate Lawson 	goto out;
9631e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9641e4925e8SNate Lawson     if (ACPI_FAILURE(error))
965bd189fe7SNate Lawson 	goto out;
9661e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
9671e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
968b2566207STakanori Watanabe 	goto out;
969b2566207STakanori Watanabe 
9701e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
9711e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
9721e4925e8SNate Lawson     for (i = 0; i < count; i++) {
9731e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9741e4925e8SNate Lawson 	    continue;
9751e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9761e4925e8SNate Lawson 	valid++;
977b2566207STakanori Watanabe     }
978b2566207STakanori Watanabe 
9791e4925e8SNate Lawson out:
980bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9811e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
9821e4925e8SNate Lawson     ACPI_UNLOCK;
9831e4925e8SNate Lawson     return_VALUE (valid);
9841e4925e8SNate Lawson }
985b2566207STakanori Watanabe 
98632d18aa5SMike Smith static int
98732d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
98832d18aa5SMike Smith {
9891e4925e8SNate Lawson     int			result, cid_count, i;
9901e4925e8SNate Lawson     uint32_t		lid, cids[8];
991bc0f2195SMike Smith 
992b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
993bc0f2195SMike Smith 
994bc0f2195SMike Smith     /*
995bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
996bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
997bc0f2195SMike Smith      * that to happen, so don't ever return it.
998bc0f2195SMike Smith      */
999bc0f2195SMike Smith     result = ENXIO;
1000bc0f2195SMike Smith 
1001be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1002b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
10031e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1004bc0f2195SMike Smith     while (ids && ids->ip_id) {
10051e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1006bc0f2195SMike Smith 	    result = 0;
1007bc0f2195SMike Smith 	    goto out;
1008bc0f2195SMike Smith 	}
10091e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
10101e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
10111e4925e8SNate Lawson 		result = 0;
10121e4925e8SNate Lawson 		goto out;
10131e4925e8SNate Lawson 	    }
10141e4925e8SNate Lawson 	}
1015bc0f2195SMike Smith 	ids++;
1016bc0f2195SMike Smith     }
1017be2b1797SNate Lawson 
1018bc0f2195SMike Smith  out:
1019bc0f2195SMike Smith     return_VALUE (result);
1020bc0f2195SMike Smith }
1021bc0f2195SMike Smith 
1022bc0f2195SMike Smith /*
102315e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
102415e32d5dSMike Smith  *
1025be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1026be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
102715e32d5dSMike Smith  */
102815e32d5dSMike Smith static void
102915e32d5dSMike Smith acpi_probe_children(device_t bus)
103015e32d5dSMike Smith {
103115e32d5dSMike Smith     ACPI_HANDLE	parent;
1032be2b1797SNate Lawson     ACPI_STATUS	status;
10337d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
103415e32d5dSMike Smith     int		i;
103515e32d5dSMike Smith 
1036b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1037cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10380ae55423SMike Smith 
1039be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
10404c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
104115e32d5dSMike Smith     bus_generic_probe(bus);
104215e32d5dSMike Smith 
104315e32d5dSMike Smith     /*
104415e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
104515e32d5dSMike Smith      * we find.
104615e32d5dSMike Smith      *
104715e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1048be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1049be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1050be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
105115e32d5dSMike Smith      */
10524c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1053be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
1054be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1055be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1056be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1057be2b1797SNate Lawson 			      bus, NULL);
1058be2b1797SNate Lawson 	}
1059be2b1797SNate Lawson     }
106015e32d5dSMike Smith 
106115e32d5dSMike Smith     /*
106215e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
106315e32d5dSMike Smith      */
10644c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
106515e32d5dSMike Smith     bus_generic_attach(bus);
106615e32d5dSMike Smith 
106715e32d5dSMike Smith     /*
106815e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
106915e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
107015e32d5dSMike Smith      */
10714c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
107215e32d5dSMike Smith     bus_generic_attach(bus);
10730ae55423SMike Smith 
107488a79fc0SNate Lawson     /* Attach wake sysctls. */
10755c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
107688a79fc0SNate Lawson 
1077bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
10780ae55423SMike Smith     return_VOID;
107915e32d5dSMike Smith }
108015e32d5dSMike Smith 
108115e32d5dSMike Smith /*
108215e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
108315e32d5dSMike Smith  * it.
108415e32d5dSMike Smith  */
108515e32d5dSMike Smith static ACPI_STATUS
108615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
108715e32d5dSMike Smith {
108815e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
108915e32d5dSMike Smith     device_t		child, bus = (device_t)context;
109015e32d5dSMike Smith 
1091b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10920ae55423SMike Smith 
1093be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
10940ae55423SMike Smith     if (acpi_avoid(handle))
10950ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
10960ae55423SMike Smith 
1097b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
109815e32d5dSMike Smith 	switch(type) {
109915e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
110015e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
110115e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
110215e32d5dSMike Smith 	case ACPI_TYPE_POWER:
11030ae55423SMike Smith 	    if (acpi_disabled("children"))
11040ae55423SMike Smith 		break;
1105be2b1797SNate Lawson 
110615e32d5dSMike Smith 	    /*
110715e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
110815e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
110915e32d5dSMike Smith 	     */
1110be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1111be2b1797SNate Lawson 			     acpi_name(handle)));
111215e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1113bc0f2195SMike Smith 	    if (child == NULL)
1114bc0f2195SMike Smith 		break;
111515e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1116bc0f2195SMike Smith 
1117e8b4d56eSNate Lawson 	    /* Check if the device can generate wake events. */
1118e8b4d56eSNate Lawson 	    if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL)))
1119e8b4d56eSNate Lawson 		device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE);
1120e8b4d56eSNate Lawson 
1121bc0f2195SMike Smith 	    /*
1122b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1123b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1124b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1125b519f9d6SMike Smith 	     */
1126be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1127b519f9d6SMike Smith 		device_disable(child);
1128b519f9d6SMike Smith 		break;
1129b519f9d6SMike Smith 	    }
1130b519f9d6SMike Smith 
1131b519f9d6SMike Smith 	    /*
1132bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1133bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1134bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1135bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1136bc0f2195SMike Smith 	     * device not to have any resources.
1137bc0f2195SMike Smith 	     */
113872ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1139bc0f2195SMike Smith 
1140be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1141b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
11420ae55423SMike Smith 	    break;
114315e32d5dSMike Smith 	}
114415e32d5dSMike Smith     }
1145be2b1797SNate Lawson 
11460ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
114715e32d5dSMike Smith }
114815e32d5dSMike Smith 
114915e32d5dSMike Smith static void
115015e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
115115e32d5dSMike Smith {
1152c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1153c6a78e98STakanori Watanabe 
1154cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1155cb9b0d80SMike Smith 
1156e8b4d56eSNate Lawson     /* Disable all wake GPEs not appropriate for this state. */
1157e8b4d56eSNate Lawson     acpi_wake_limit_walk(ACPI_STATE_S5);
1158e8b4d56eSNate Lawson 
115915e32d5dSMike Smith     /*
11600ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
116115e32d5dSMike Smith      * will be turned on again on some laptops.
116215e32d5dSMike Smith      *
116315e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
116415e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
116515e32d5dSMike Smith      *     shutdown process.
116615e32d5dSMike Smith      */
1167c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1168c6a78e98STakanori Watanabe 	acpi_Disable(sc);
116915e32d5dSMike Smith }
117015e32d5dSMike Smith 
117115e32d5dSMike Smith static void
117215e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
117315e32d5dSMike Smith {
1174d33f4987STakanori Watanabe     ACPI_STATUS	status;
1175eeb3a05fSNate Lawson     ACPI_ASSERTLOCK;
1176eeb3a05fSNate Lawson 
1177eeb3a05fSNate Lawson     /*
1178eeb3a05fSNate Lawson      * If powering off, run the actual shutdown code on each processor.
1179eeb3a05fSNate Lawson      * It will only perform the shutdown on the BSP.  Some chipsets do
1180eeb3a05fSNate Lawson      * not power off the system correctly if called from an AP.
1181eeb3a05fSNate Lawson      */
1182eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1183904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1184904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1185904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1186904bf0c2SNate Lawson 		   AcpiFormatException(status));
1187904bf0c2SNate Lawson 	    return;
1188904bf0c2SNate Lawson 	}
1189eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
1190eeb3a05fSNate Lawson 	smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL);
1191eeb3a05fSNate Lawson     } else {
1192eeb3a05fSNate Lawson 	printf("Shutting down ACPI\n");
1193eeb3a05fSNate Lawson 	AcpiTerminate();
1194eeb3a05fSNate Lawson     }
1195eeb3a05fSNate Lawson }
1196eeb3a05fSNate Lawson 
1197904bf0c2SNate Lawson /*
1198904bf0c2SNate Lawson  * Since this function may be called with locks held or in an unknown
1199904bf0c2SNate Lawson  * context, it cannot allocate memory, acquire locks, sleep, etc.
1200904bf0c2SNate Lawson  */
1201eeb3a05fSNate Lawson static void
1202eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg)
1203eeb3a05fSNate Lawson {
120415e32d5dSMike Smith     ACPI_STATUS	status;
120515e32d5dSMike Smith 
1206cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1207cb9b0d80SMike Smith 
1208eeb3a05fSNate Lawson     /* Only attempt to power off if this is the BSP (cpuid 0). */
1209eeb3a05fSNate Lawson     if (PCPU_GET(cpuid) != 0)
1210eeb3a05fSNate Lawson 	return;
1211eeb3a05fSNate Lawson 
121255398047SNate Lawson     ACPI_DISABLE_IRQS();
1213865b8d0bSNate Lawson     status = AcpiEnterSleepState(ACPI_STATE_S5);
1214be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
1215bfae45aaSMike Smith 	printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
121615e32d5dSMike Smith     } else {
121715e32d5dSMike Smith 	DELAY(1000000);
121815e32d5dSMike Smith 	printf("ACPI power-off failed - timeout\n");
121915e32d5dSMike Smith     }
122015e32d5dSMike Smith }
122115e32d5dSMike Smith 
122213d4f7baSMitsuru IWASAKI static void
122313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
122413d4f7baSMitsuru IWASAKI {
122513d4f7baSMitsuru IWASAKI     static int	first_time = 1;
122613d4f7baSMitsuru IWASAKI 
1227cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1228cb9b0d80SMike Smith 
122913d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1230be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
123159ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
123213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
123333febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1234be2b1797SNate Lawson 	if (first_time)
12356c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
123613d4f7baSMitsuru IWASAKI     }
1237be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
123859ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
123913d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
124033febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1241be2b1797SNate Lawson 	if (first_time)
12426c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
124313d4f7baSMitsuru IWASAKI     }
124413d4f7baSMitsuru IWASAKI 
124513d4f7baSMitsuru IWASAKI     first_time = 0;
124613d4f7baSMitsuru IWASAKI }
124713d4f7baSMitsuru IWASAKI 
124815e32d5dSMike Smith /*
1249c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1250c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1251c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1252c5ba0be4SMike Smith  */
1253c5ba0be4SMike Smith BOOLEAN
1254c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1255c5ba0be4SMike Smith {
12561e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1257c5ba0be4SMike Smith     ACPI_HANDLE		h;
12581e4925e8SNate Lawson     ACPI_BUFFER		buf;
1259c5ba0be4SMike Smith     ACPI_STATUS		error;
12601e4925e8SNate Lawson     int			ret;
1261c5ba0be4SMike Smith 
1262cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1263cb9b0d80SMike Smith 
12641e4925e8SNate Lawson     ret = FALSE;
1265c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1266c5ba0be4SMike Smith 	return (FALSE);
12671e4925e8SNate Lawson     buf.Pointer = NULL;
12681e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12696fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12706fca9360SNate Lawson     if (ACPI_FAILURE(error))
1271c5ba0be4SMike Smith 	return (FALSE);
12721e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1273be2b1797SNate Lawson 
1274be2b1797SNate Lawson     /* If no _STA method, must be present */
12751e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12761e4925e8SNate Lawson 	ret = TRUE;
1277be2b1797SNate Lawson 
1278be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12791e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
12801e4925e8SNate Lawson 	ret = TRUE;
1281be2b1797SNate Lawson 
12821e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12831e4925e8SNate Lawson     return (ret);
1284c5ba0be4SMike Smith }
1285c5ba0be4SMike Smith 
1286c5ba0be4SMike Smith /*
1287b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1288b53f2771SMike Smith  */
1289b53f2771SMike Smith BOOLEAN
1290b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1291b53f2771SMike Smith {
12921e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1293b53f2771SMike Smith     ACPI_HANDLE		h;
12941e4925e8SNate Lawson     ACPI_BUFFER		buf;
1295b53f2771SMike Smith     ACPI_STATUS		error;
12961e4925e8SNate Lawson     int			ret;
1297b53f2771SMike Smith 
1298b53f2771SMike Smith     ACPI_ASSERTLOCK;
1299b53f2771SMike Smith 
13001e4925e8SNate Lawson     ret = FALSE;
1301b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1302b53f2771SMike Smith 	return (FALSE);
13031e4925e8SNate Lawson     buf.Pointer = NULL;
13041e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13056fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13066fca9360SNate Lawson     if (ACPI_FAILURE(error))
1307b53f2771SMike Smith 	return (FALSE);
13081e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1309be2b1797SNate Lawson 
1310be2b1797SNate Lawson     /* If no _STA method, must be present */
13111e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
13121e4925e8SNate Lawson 	ret = TRUE;
1313be2b1797SNate Lawson 
1314be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
13151e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
13161e4925e8SNate Lawson 	ret = TRUE;
1317be2b1797SNate Lawson 
13181e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13191e4925e8SNate Lawson     return (ret);
1320b53f2771SMike Smith }
1321b53f2771SMike Smith 
1322b53f2771SMike Smith /*
132315e32d5dSMike Smith  * Match a HID string against a device
132415e32d5dSMike Smith  */
132515e32d5dSMike Smith BOOLEAN
132615e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
132715e32d5dSMike Smith {
13281e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
132915e32d5dSMike Smith     ACPI_HANDLE		h;
13301e4925e8SNate Lawson     ACPI_BUFFER		buf;
133115e32d5dSMike Smith     ACPI_STATUS		error;
13321e4925e8SNate Lawson     int			ret, i;
133315e32d5dSMike Smith 
1334cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1335cb9b0d80SMike Smith 
13361e4925e8SNate Lawson     ret = FALSE;
13374d332989SMike Smith     if (hid == NULL)
133815e32d5dSMike Smith 	return (FALSE);
133915e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
134015e32d5dSMike Smith 	return (FALSE);
13411e4925e8SNate Lawson     buf.Pointer = NULL;
13421e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13436fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13446fca9360SNate Lawson     if (ACPI_FAILURE(error))
134515e32d5dSMike Smith 	return (FALSE);
13461e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1347be2b1797SNate Lawson 
1348c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1349c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
13501e4925e8SNate Lawson 	    ret = TRUE;
1351c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
13521e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
13531e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
13541e4925e8SNate Lawson 		ret = TRUE;
13551e4925e8SNate Lawson 		break;
13561e4925e8SNate Lawson 	    }
13571e4925e8SNate Lawson 	}
13581e4925e8SNate Lawson     }
1359be2b1797SNate Lawson 
13601e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13611e4925e8SNate Lawson     return (ret);
136215e32d5dSMike Smith }
136315e32d5dSMike Smith 
136415e32d5dSMike Smith /*
1365c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1366c5ba0be4SMike Smith  * or one if its parents.
1367c5ba0be4SMike Smith  */
1368c5ba0be4SMike Smith ACPI_STATUS
1369c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1370c5ba0be4SMike Smith {
1371c5ba0be4SMike Smith     ACPI_HANDLE		r;
1372c5ba0be4SMike Smith     ACPI_STATUS		status;
1373c5ba0be4SMike Smith 
1374cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1375cb9b0d80SMike Smith 
1376be2b1797SNate Lawson     /* Walk back up the tree to the root */
1377c5ba0be4SMike Smith     for (;;) {
1378be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1379be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1380c5ba0be4SMike Smith 	    *result = r;
1381c5ba0be4SMike Smith 	    return (AE_OK);
1382c5ba0be4SMike Smith 	}
1383c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1384c5ba0be4SMike Smith 	    return (AE_OK);
1385b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1386c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1387c5ba0be4SMike Smith 	parent = r;
1388c5ba0be4SMike Smith     }
1389c5ba0be4SMike Smith }
1390c5ba0be4SMike Smith 
1391eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1392eea17c34SNate Lawson uint32_t
1393eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1394eea17c34SNate Lawson {
1395eea17c34SNate Lawson     uint32_t delta;
1396eea17c34SNate Lawson 
1397eea17c34SNate Lawson     if (end >= start)
1398eea17c34SNate Lawson 	delta = end - start;
1399eea17c34SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
14008d01ceefSNate Lawson 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1401eea17c34SNate Lawson     else
1402eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
1403eea17c34SNate Lawson     return (delta);
1404eea17c34SNate Lawson }
1405eea17c34SNate Lawson 
1406c5ba0be4SMike Smith /*
1407c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1408c5ba0be4SMike Smith  */
1409c5ba0be4SMike Smith ACPI_BUFFER *
1410c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1411c5ba0be4SMike Smith {
1412c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1413c5ba0be4SMike Smith 
1414c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1415c5ba0be4SMike Smith 	return (NULL);
1416c5ba0be4SMike Smith     buf->Length = size;
1417c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1418c5ba0be4SMike Smith     return (buf);
1419c5ba0be4SMike Smith }
1420c5ba0be4SMike Smith 
1421c310653eSNate Lawson ACPI_STATUS
1422cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1423c310653eSNate Lawson {
1424c310653eSNate Lawson     ACPI_OBJECT arg1;
1425c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1426c310653eSNate Lawson 
1427c310653eSNate Lawson     ACPI_ASSERTLOCK;
1428c310653eSNate Lawson 
1429c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1430c310653eSNate Lawson     arg1.Integer.Value = number;
1431c310653eSNate Lawson     args.Count = 1;
1432c310653eSNate Lawson     args.Pointer = &arg1;
1433c310653eSNate Lawson 
1434c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1435c310653eSNate Lawson }
1436c310653eSNate Lawson 
1437c5ba0be4SMike Smith /*
1438c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1439c5ba0be4SMike Smith  */
1440c5ba0be4SMike Smith ACPI_STATUS
1441cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1442c5ba0be4SMike Smith {
1443be2b1797SNate Lawson     ACPI_STATUS	status;
1444c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1445c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1446c5ba0be4SMike Smith 
1447cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1448cb9b0d80SMike Smith 
1449c5ba0be4SMike Smith     if (handle == NULL)
1450c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
14510c7da7acSJohn Baldwin 
14520c7da7acSJohn Baldwin     /*
14530c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
14540c7da7acSJohn Baldwin      * a method that will return an Integer.
14550c7da7acSJohn Baldwin      */
1456c5ba0be4SMike Smith     buf.Pointer = &param;
1457c5ba0be4SMike Smith     buf.Length = sizeof(param);
1458be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1459be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1460be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1461c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1462be2b1797SNate Lawson 	else
1463be2b1797SNate Lawson 	    status = AE_TYPE;
1464c5ba0be4SMike Smith     }
14650c7da7acSJohn Baldwin 
14660c7da7acSJohn Baldwin     /*
14670c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
14680c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
14690c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
14700c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
14710c7da7acSJohn Baldwin      *
14720c7da7acSJohn Baldwin      * This is a hack.
14730c7da7acSJohn Baldwin      */
1474be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1475b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1476be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
14770c7da7acSJohn Baldwin 	} else {
1478be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1479be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1480be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
14810c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
14820c7da7acSJohn Baldwin 	}
1483f97739daSNate Lawson     }
1484be2b1797SNate Lawson     return (status);
1485c5ba0be4SMike Smith }
1486c5ba0be4SMike Smith 
1487c573e654SMitsuru IWASAKI ACPI_STATUS
1488cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1489c573e654SMitsuru IWASAKI {
1490c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1491dba55fa2SNate Lawson     UINT8	*val;
1492c573e654SMitsuru IWASAKI     int		i;
1493c573e654SMitsuru IWASAKI 
1494c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1495c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1496c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1497c573e654SMitsuru IWASAKI 	return (AE_OK);
1498c573e654SMitsuru IWASAKI     }
1499c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1500c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1501c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1502c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1503be2b1797SNate Lawson 
1504c573e654SMitsuru IWASAKI     *number = 0;
1505dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1506c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1507dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1508c573e654SMitsuru IWASAKI     return (AE_OK);
1509c573e654SMitsuru IWASAKI }
1510c573e654SMitsuru IWASAKI 
1511c5ba0be4SMike Smith /*
1512c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1513c5ba0be4SMike Smith  * function for each element.
1514c5ba0be4SMike Smith  *
1515c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1516c5ba0be4SMike Smith  */
1517c5ba0be4SMike Smith ACPI_STATUS
1518be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1519be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1520c5ba0be4SMike Smith {
1521c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1522c5ba0be4SMike Smith     int		i;
1523c5ba0be4SMike Smith 
1524be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1525c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1526c5ba0be4SMike Smith 
1527be2b1797SNate Lawson     /* Iterate over components */
1528be2b1797SNate Lawson     i = 0;
1529be2b1797SNate Lawson     comp = pkg->Package.Elements;
1530be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1531c5ba0be4SMike Smith 	func(comp, arg);
1532c5ba0be4SMike Smith 
1533c5ba0be4SMike Smith     return (AE_OK);
1534c5ba0be4SMike Smith }
1535c5ba0be4SMike Smith 
15366f69255bSMike Smith /*
15376f69255bSMike Smith  * Find the (index)th resource object in a set.
15386f69255bSMike Smith  */
15396f69255bSMike Smith ACPI_STATUS
15406d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
15416f69255bSMike Smith {
15426d63101aSMike Smith     ACPI_RESOURCE	*rp;
15436f69255bSMike Smith     int			i;
15446f69255bSMike Smith 
15456d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15466f69255bSMike Smith     i = index;
15476d63101aSMike Smith     while (i-- > 0) {
1548be2b1797SNate Lawson 	/* Range check */
15496d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15506f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1551be2b1797SNate Lawson 
1552be2b1797SNate Lawson 	/* Check for terminator */
1553be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15546d63101aSMike Smith 	    return (AE_NOT_FOUND);
1555abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15566f69255bSMike Smith     }
15576f69255bSMike Smith     if (resp != NULL)
15586d63101aSMike Smith 	*resp = rp;
1559be2b1797SNate Lawson 
15606d63101aSMike Smith     return (AE_OK);
15616d63101aSMike Smith }
15626d63101aSMike Smith 
15636d63101aSMike Smith /*
15646d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
15656d63101aSMike Smith  *
15666d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
15676d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
15686d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
15696d63101aSMike Smith  * resources.
15706d63101aSMike Smith  */
15716d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
15726d63101aSMike Smith 
15736d63101aSMike Smith ACPI_STATUS
15746d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
15756d63101aSMike Smith {
15766d63101aSMike Smith     ACPI_RESOURCE	*rp;
15776d63101aSMike Smith     void		*newp;
15786d63101aSMike Smith 
1579be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
15806d63101aSMike Smith     if (buf->Pointer == NULL) {
15816d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
15826d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
15836d63101aSMike Smith 	    return (AE_NO_MEMORY);
15846d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
15856d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
15866d63101aSMike Smith 	rp->Length = 0;
15876d63101aSMike Smith     }
15886d63101aSMike Smith     if (res == NULL)
15896d63101aSMike Smith 	return (AE_OK);
15906d63101aSMike Smith 
15916d63101aSMike Smith     /*
15926d63101aSMike Smith      * Scan the current buffer looking for the terminator.
15936d63101aSMike Smith      * This will either find the terminator or hit the end
15946d63101aSMike Smith      * of the buffer and return an error.
15956d63101aSMike Smith      */
15966d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15976d63101aSMike Smith     for (;;) {
1598be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
15996d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
16006d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1601be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
16026d63101aSMike Smith 	    break;
1603abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
16046d63101aSMike Smith     }
16056d63101aSMike Smith 
16066d63101aSMike Smith     /*
16076d63101aSMike Smith      * Check the size of the buffer and expand if required.
16086d63101aSMike Smith      *
16096d63101aSMike Smith      * Required size is:
16106d63101aSMike Smith      *	size of existing resources before terminator +
16116d63101aSMike Smith      *	size of new resource and header +
16126d63101aSMike Smith      * 	size of terminator.
16136d63101aSMike Smith      *
16146d63101aSMike Smith      * Note that this loop should really only run once, unless
16156d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
16166d63101aSMike Smith      */
16176d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
16186d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
16196d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
16206d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
16216d63101aSMike Smith 	    return (AE_NO_MEMORY);
16226d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1623a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1624a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
16256d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
16266d63101aSMike Smith 	buf->Pointer = newp;
16276d63101aSMike Smith 	buf->Length += buf->Length;
16286d63101aSMike Smith     }
16296d63101aSMike Smith 
1630be2b1797SNate Lawson     /* Insert the new resource. */
16316d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
16326d63101aSMike Smith 
1633be2b1797SNate Lawson     /* And add the terminator. */
1634abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
16356d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
16366d63101aSMike Smith     rp->Length = 0;
16376d63101aSMike Smith 
16386f69255bSMike Smith     return (AE_OK);
16396f69255bSMike Smith }
1640c5ba0be4SMike Smith 
1641da14ac9fSJohn Baldwin /*
1642da14ac9fSJohn Baldwin  * Set interrupt model.
1643da14ac9fSJohn Baldwin  */
1644da14ac9fSJohn Baldwin ACPI_STATUS
1645da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1646da14ac9fSJohn Baldwin {
1647da14ac9fSJohn Baldwin 
1648c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1649da14ac9fSJohn Baldwin }
1650da14ac9fSJohn Baldwin 
1651ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1652ece50487SMitsuru IWASAKI 
1653ece50487SMitsuru IWASAKI static void
1654ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1655ece50487SMitsuru IWASAKI {
1656ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1657ece50487SMitsuru IWASAKI }
1658c30382dfSMitsuru IWASAKI 
165915e32d5dSMike Smith /*
166015e32d5dSMike Smith  * Set the system sleep state
166115e32d5dSMike Smith  *
1662be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
166315e32d5dSMike Smith  */
166415e32d5dSMike Smith ACPI_STATUS
166515e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
166615e32d5dSMike Smith {
166715e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
166856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
166956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
167015e32d5dSMike Smith 
1671b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1672cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16730ae55423SMike Smith 
167498175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
16756397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
167698175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
16776397624dSMitsuru IWASAKI 
167898175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1679ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1680ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1681ece50487SMitsuru IWASAKI 
168215e32d5dSMike Smith     switch (state) {
168315e32d5dSMike Smith     case ACPI_STATE_S1:
16846161544cSTakanori Watanabe     case ACPI_STATE_S2:
16856161544cSTakanori Watanabe     case ACPI_STATE_S3:
16866161544cSTakanori Watanabe     case ACPI_STATE_S4:
1687be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
168898175614SNate Lawson 	if (status == AE_NOT_FOUND) {
168998175614SNate Lawson 	    device_printf(sc->acpi_dev,
169098175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
169198175614SNate Lawson 	    break;
169298175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1693be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1694be2b1797SNate Lawson 			  AcpiFormatException(status));
169556d8cb57SMitsuru IWASAKI 	    break;
169656d8cb57SMitsuru IWASAKI 	}
169756d8cb57SMitsuru IWASAKI 
1698a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1699a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1700a1fccb47SMitsuru IWASAKI 
1701e8b4d56eSNate Lawson 	/* Disable all wake GPEs not appropriate for this state. */
1702e8b4d56eSNate Lawson 	acpi_wake_limit_walk(state);
1703e8b4d56eSNate Lawson 
1704be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
170515e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
170615e32d5dSMike Smith 	    /*
170715e32d5dSMike Smith 	     * Re-wake the system.
170815e32d5dSMike Smith 	     *
170915e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
171015e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
171115e32d5dSMike Smith 	     *     current bus interface does not provide for this.
171215e32d5dSMike Smith 	     */
171315e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
17140ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
171515e32d5dSMike Smith 	}
1716b9c780d6SMitsuru IWASAKI 
1717be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1718be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1719b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1720b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1721b9c780d6SMitsuru IWASAKI 	    break;
1722b9c780d6SMitsuru IWASAKI 	}
1723b9c780d6SMitsuru IWASAKI 
1724be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1725ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1726ff01efb5SMitsuru IWASAKI 
17276161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
17286161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
17296161544cSTakanori Watanabe 
1730be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
173159ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
173259ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
173359ddeb18SNate Lawson 
17346161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1735a1fccb47SMitsuru IWASAKI 	    }
17366161544cSTakanori Watanabe 
17376161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1738be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1739964679ceSMitsuru IWASAKI 		AcpiEnable();
17406161544cSTakanori Watanabe 	} else {
1741be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1742be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1743be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1744be2b1797SNate Lawson 			      AcpiFormatException(status));
1745c30382dfSMitsuru IWASAKI 		break;
174615e32d5dSMike Smith 	    }
17476161544cSTakanori Watanabe 	}
1748ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
174915e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
175015e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
175113d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
175215e32d5dSMike Smith 	break;
175315e32d5dSMike Smith     case ACPI_STATE_S5:
175415e32d5dSMike Smith 	/*
175515e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
175615e32d5dSMike Smith 	 * shutdown handlers.
175715e32d5dSMike Smith 	 */
175815e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
175915e32d5dSMike Smith 	break;
176098175614SNate Lawson     case ACPI_STATE_S0:
176115e32d5dSMike Smith     default:
176215e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
176315e32d5dSMike Smith 	break;
176415e32d5dSMike Smith     }
1765ece50487SMitsuru IWASAKI 
176698175614SNate Lawson     /* Disable a second sleep request for a short period */
1767ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1768ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1769ece50487SMitsuru IWASAKI 
17700ae55423SMike Smith     return_ACPI_STATUS (status);
177115e32d5dSMike Smith }
177215e32d5dSMike Smith 
1773e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
1774e8b4d56eSNate Lawson int
1775e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
1776e8b4d56eSNate Lawson {
1777e8b4d56eSNate Lawson     struct acpi_prw_data prw;
1778e8b4d56eSNate Lawson 
1779e8b4d56eSNate Lawson     /* Check that the device can wake the system. */
1780e8b4d56eSNate Lawson     if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0)
1781e8b4d56eSNate Lawson 	return (ENXIO);
1782e8b4d56eSNate Lawson 
1783e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
1784e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
1785e8b4d56eSNate Lawson 	return (ENXIO);
1786e8b4d56eSNate Lawson 
1787e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
1788e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
1789e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
1790e8b4d56eSNate Lawson 	return (ENXIO);
1791e8b4d56eSNate Lawson     }
1792e8b4d56eSNate Lawson 
1793e8b4d56eSNate Lawson     return (0);
1794e8b4d56eSNate Lawson }
1795e8b4d56eSNate Lawson 
1796e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
1797e8b4d56eSNate Lawson int
1798e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
1799e8b4d56eSNate Lawson {
1800e8b4d56eSNate Lawson     struct acpi_prw_data prw;
1801e8b4d56eSNate Lawson     ACPI_HANDLE handle;
1802e8b4d56eSNate Lawson     ACPI_STATUS status;
1803e8b4d56eSNate Lawson     int flags;
1804e8b4d56eSNate Lawson 
1805e8b4d56eSNate Lawson     /* Make sure the device supports waking the system. */
1806e8b4d56eSNate Lawson     flags = device_get_flags(dev);
1807e8b4d56eSNate Lawson     handle = acpi_get_handle(dev);
1808e8b4d56eSNate Lawson     if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1809e8b4d56eSNate Lawson 	return (ENXIO);
1810e8b4d56eSNate Lawson 
1811e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
1812e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
1813e8b4d56eSNate Lawson 	return (ENXIO);
1814e8b4d56eSNate Lawson 
1815e8b4d56eSNate Lawson     if (enable) {
1816e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1817e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
1818e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
1819e8b4d56eSNate Lawson 	    return (ENXIO);
1820e8b4d56eSNate Lawson 	}
1821e8b4d56eSNate Lawson 	device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
1822e8b4d56eSNate Lawson     } else {
1823e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1824e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
1825e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
1826e8b4d56eSNate Lawson 	    return (ENXIO);
1827e8b4d56eSNate Lawson 	}
1828e8b4d56eSNate Lawson 	device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
1829e8b4d56eSNate Lawson     }
1830e8b4d56eSNate Lawson 
1831e8b4d56eSNate Lawson     return (0);
1832e8b4d56eSNate Lawson }
1833e8b4d56eSNate Lawson 
1834e8b4d56eSNate Lawson /* Configure a device's GPE appropriately for the new sleep state. */
1835e8b4d56eSNate Lawson int
1836e8b4d56eSNate Lawson acpi_wake_sleep_prep(device_t dev, int sstate)
1837e8b4d56eSNate Lawson {
1838e8b4d56eSNate Lawson     struct acpi_prw_data prw;
1839e8b4d56eSNate Lawson     ACPI_HANDLE handle;
1840cc85c78cSNate Lawson     int flags;
1841e8b4d56eSNate Lawson 
1842e8b4d56eSNate Lawson     /* Check that this is an ACPI device and get its GPE. */
1843cc85c78cSNate Lawson     flags = device_get_flags(dev);
1844e8b4d56eSNate Lawson     handle = acpi_get_handle(dev);
1845cc85c78cSNate Lawson     if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1846e8b4d56eSNate Lawson 	return (ENXIO);
1847cc85c78cSNate Lawson 
1848cc85c78cSNate Lawson     /* Evaluate _PRW to find the GPE. */
1849e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
1850e8b4d56eSNate Lawson 	return (ENXIO);
1851e8b4d56eSNate Lawson 
1852e8b4d56eSNate Lawson     /*
1853cc85c78cSNate Lawson      * TBD: All Power Resources referenced by elements 2 through N
1854cc85c78cSNate Lawson      *      of the _PRW object are put into the ON state.
1855e8b4d56eSNate Lawson      */
1856cc85c78cSNate Lawson 
1857cc85c78cSNate Lawson     /*
1858cc85c78cSNate Lawson      * If the user requested that this device wake the system and the next
1859cc85c78cSNate Lawson      * sleep state is valid for this GPE, enable it and the device's wake
1860cc85c78cSNate Lawson      * capability.  The sleep state must be less than (i.e., higher power)
1861cc85c78cSNate Lawson      * or equal to the value specified by _PRW.  Return early, leaving
1862cc85c78cSNate Lawson      * the appropriate power resources enabled.
1863cc85c78cSNate Lawson      */
1864cc85c78cSNate Lawson     if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 &&
1865cc85c78cSNate Lawson 	sstate <= prw.lowest_wake) {
1866e8b4d56eSNate Lawson 	if (bootverbose)
1867cc85c78cSNate Lawson 	    device_printf(dev, "wake_prep enabled gpe %#x for state %d\n",
1868e8b4d56eSNate Lawson 		prw.gpe_bit, sstate);
1869cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1870cc85c78cSNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
1871e8b4d56eSNate Lawson 	return (0);
1872e8b4d56eSNate Lawson     }
1873e8b4d56eSNate Lawson 
1874e8b4d56eSNate Lawson     /*
1875cc85c78cSNate Lawson      * If the device wake was disabled or this sleep state is too low for
1876cc85c78cSNate Lawson      * this device, disable its wake capability and GPE.
1877e8b4d56eSNate Lawson      */
1878cc85c78cSNate Lawson     AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1879cc85c78cSNate Lawson     acpi_SetInteger(handle, "_PSW", 0);
1880e8b4d56eSNate Lawson     if (bootverbose)
1881cc85c78cSNate Lawson 	device_printf(dev, "wake_prep disabled gpe %#x for state %d\n",
1882cc85c78cSNate Lawson 	    prw.gpe_bit, sstate);
1883cc85c78cSNate Lawson 
1884cc85c78cSNate Lawson     /*
1885cc85c78cSNate Lawson      * TBD: All Power Resources referenced by elements 2 through N
1886cc85c78cSNate Lawson      *      of the _PRW object are put into the OFF state.
1887cc85c78cSNate Lawson      */
1888cc85c78cSNate Lawson 
1889cc85c78cSNate Lawson     return (0);
1890e8b4d56eSNate Lawson }
1891e8b4d56eSNate Lawson 
1892cc85c78cSNate Lawson /* Re-enable GPEs after wake. */
1893cc85c78cSNate Lawson int
1894cc85c78cSNate Lawson acpi_wake_run_prep(device_t dev)
1895cc85c78cSNate Lawson {
1896cc85c78cSNate Lawson     struct acpi_prw_data prw;
1897cc85c78cSNate Lawson     ACPI_HANDLE handle;
1898cc85c78cSNate Lawson     int flags;
1899cc85c78cSNate Lawson 
1900cc85c78cSNate Lawson     /* Check that this is an ACPI device and get its GPE. */
1901cc85c78cSNate Lawson     flags = device_get_flags(dev);
1902cc85c78cSNate Lawson     handle = acpi_get_handle(dev);
1903cc85c78cSNate Lawson     if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL)
1904cc85c78cSNate Lawson 	return (ENXIO);
1905cc85c78cSNate Lawson 
1906cc85c78cSNate Lawson     /* Evaluate _PRW to find the GPE. */
1907cc85c78cSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
1908cc85c78cSNate Lawson 	return (ENXIO);
1909cc85c78cSNate Lawson 
1910cc85c78cSNate Lawson     /*
1911cc85c78cSNate Lawson      * TBD: Be sure all Power Resources referenced by elements 2 through N
1912cc85c78cSNate Lawson      *      of the _PRW object are in the ON state.
1913cc85c78cSNate Lawson      */
1914cc85c78cSNate Lawson 
1915cc85c78cSNate Lawson     /* Disable wake capability and if the user requested, enable the GPE. */
1916cc85c78cSNate Lawson     acpi_SetInteger(handle, "_PSW", 0);
1917cc85c78cSNate Lawson     if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0)
1918cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1919e8b4d56eSNate Lawson     return (0);
1920e8b4d56eSNate Lawson }
1921e8b4d56eSNate Lawson 
1922e8b4d56eSNate Lawson static ACPI_STATUS
1923e8b4d56eSNate Lawson acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status)
1924e8b4d56eSNate Lawson {
1925e8b4d56eSNate Lawson     struct acpi_prw_data prw;
192644b8ae71SNate Lawson     int *sstate;
1927e8b4d56eSNate Lawson 
1928e8b4d56eSNate Lawson     /* It's ok not to have _PRW if the device can't wake the system. */
1929e8b4d56eSNate Lawson     if (acpi_parse_prw(h, &prw) != 0)
1930e8b4d56eSNate Lawson 	return (AE_OK);
1931e8b4d56eSNate Lawson 
193244b8ae71SNate Lawson     sstate = (int *)context;
193344b8ae71SNate Lawson     if (*sstate > prw.lowest_wake)
1934e8b4d56eSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
1935e8b4d56eSNate Lawson 
1936e8b4d56eSNate Lawson     return (AE_OK);
1937e8b4d56eSNate Lawson }
1938e8b4d56eSNate Lawson 
1939e8b4d56eSNate Lawson /* Walk all system devices, disabling them if necessary for sstate. */
1940e8b4d56eSNate Lawson static int
1941e8b4d56eSNate Lawson acpi_wake_limit_walk(int sstate)
1942e8b4d56eSNate Lawson {
1943e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
1944e8b4d56eSNate Lawson 
1945e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
1946e8b4d56eSNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100,
194744b8ae71SNate Lawson 	    acpi_wake_limit, &sstate, NULL);
1948e8b4d56eSNate Lawson     return (0);
1949e8b4d56eSNate Lawson }
1950e8b4d56eSNate Lawson 
195188a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
195288a79fc0SNate Lawson static int
195388a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
195488a79fc0SNate Lawson {
195588a79fc0SNate Lawson     int error, i, numdevs;
195688a79fc0SNate Lawson     device_t *devlist;
195788a79fc0SNate Lawson     device_t child;
195888a79fc0SNate Lawson 
195988a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
196088a79fc0SNate Lawson     if (error != 0 || numdevs == 0)
196188a79fc0SNate Lawson 	return (error);
196288a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
196388a79fc0SNate Lawson 	child = devlist[i];
196488a79fc0SNate Lawson 	if (!device_is_attached(child))
196588a79fc0SNate Lawson 	    continue;
196688a79fc0SNate Lawson 	if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) {
196788a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
196888a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
196988a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
197088a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
197188a79fc0SNate Lawson 	}
197288a79fc0SNate Lawson 	acpi_wake_sysctl_walk(child);
197388a79fc0SNate Lawson     }
197488a79fc0SNate Lawson     free(devlist, M_TEMP);
197588a79fc0SNate Lawson 
197688a79fc0SNate Lawson     return (0);
197788a79fc0SNate Lawson }
197888a79fc0SNate Lawson 
197988a79fc0SNate Lawson /* Enable or disable wake from userland. */
198088a79fc0SNate Lawson static int
198188a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
198288a79fc0SNate Lawson {
198388a79fc0SNate Lawson     int enable, error;
198488a79fc0SNate Lawson     device_t dev;
198588a79fc0SNate Lawson 
198688a79fc0SNate Lawson     dev = (device_t)arg1;
198788a79fc0SNate Lawson     enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
198888a79fc0SNate Lawson 
198988a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
199088a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
199188a79fc0SNate Lawson 	return (error);
199288a79fc0SNate Lawson     if (enable != 0 && enable != 1)
199388a79fc0SNate Lawson 	return (EINVAL);
199488a79fc0SNate Lawson 
199588a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
199688a79fc0SNate Lawson }
199788a79fc0SNate Lawson 
1998e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
1999e8b4d56eSNate Lawson static int
2000e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2001e8b4d56eSNate Lawson {
2002e8b4d56eSNate Lawson     ACPI_STATUS			status;
2003e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2004e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2005e8b4d56eSNate Lawson     int error;
2006e8b4d56eSNate Lawson 
2007e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2008e8b4d56eSNate Lawson 	return (EINVAL);
2009e8b4d56eSNate Lawson 
2010e8b4d56eSNate Lawson     /*
2011e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2012e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2013e8b4d56eSNate Lawson      */
2014e8b4d56eSNate Lawson     error = EINVAL;
2015e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2016e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2017e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2018e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2019e8b4d56eSNate Lawson 	return (ENOENT);
2020e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2021e8b4d56eSNate Lawson     if (res == NULL)
2022e8b4d56eSNate Lawson 	return (ENOENT);
2023e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2024e8b4d56eSNate Lawson 	goto out;
2025e8b4d56eSNate Lawson 
2026e8b4d56eSNate Lawson     /*
2027e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2028e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2029e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2030e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2031e8b4d56eSNate Lawson      */
2032e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2033e8b4d56eSNate Lawson 	goto out;
2034e8b4d56eSNate Lawson 
2035e8b4d56eSNate Lawson     /*
2036e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2037e8b4d56eSNate Lawson      */
2038e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2039e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2040e8b4d56eSNate Lawson 	/*
2041e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2042e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2043e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2044e8b4d56eSNate Lawson 	 * enabled for the wake event.
2045e8b4d56eSNate Lawson 	 */
2046e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2047e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2048e8b4d56eSNate Lawson 	error = 0;
2049e8b4d56eSNate Lawson 	break;
2050e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2051e8b4d56eSNate Lawson 	/*
2052e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2053e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2054e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2055e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2056e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2057e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2058e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2059e8b4d56eSNate Lawson 	 * the wake event.
2060e8b4d56eSNate Lawson 	 *
2061e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2062e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2063e8b4d56eSNate Lawson 	 */
2064e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2065e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2066e8b4d56eSNate Lawson 	    goto out;
2067e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2068e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2069e8b4d56eSNate Lawson 	    goto out;
2070e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2071e8b4d56eSNate Lawson 	    goto out;
2072e8b4d56eSNate Lawson 	error = 0;
2073e8b4d56eSNate Lawson 	break;
2074e8b4d56eSNate Lawson     default:
2075e8b4d56eSNate Lawson 	goto out;
2076e8b4d56eSNate Lawson     }
2077e8b4d56eSNate Lawson 
2078e8b4d56eSNate Lawson     /* XXX No power resource handling yet. */
2079e8b4d56eSNate Lawson     prw->power_res = NULL;
2080e8b4d56eSNate Lawson 
2081e8b4d56eSNate Lawson out:
2082e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2083e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2084e8b4d56eSNate Lawson     return (error);
2085e8b4d56eSNate Lawson }
2086e8b4d56eSNate Lawson 
208715e32d5dSMike Smith /*
208815e32d5dSMike Smith  * Enable/Disable ACPI
208915e32d5dSMike Smith  */
209015e32d5dSMike Smith ACPI_STATUS
209115e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
209215e32d5dSMike Smith {
209315e32d5dSMike Smith     ACPI_STATUS	status;
209415e32d5dSMike Smith     u_int32_t	flags;
209515e32d5dSMike Smith 
2096b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2097cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
20980ae55423SMike Smith 
209915e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
210015e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
2101be2b1797SNate Lawson     if (!sc->acpi_enabled)
210215e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
2103be2b1797SNate Lawson     else
210415e32d5dSMike Smith 	status = AE_OK;
2105be2b1797SNate Lawson 
210615e32d5dSMike Smith     if (status == AE_OK)
210715e32d5dSMike Smith 	sc->acpi_enabled = 1;
2108be2b1797SNate Lawson 
21090ae55423SMike Smith     return_ACPI_STATUS (status);
211015e32d5dSMike Smith }
211115e32d5dSMike Smith 
211215e32d5dSMike Smith ACPI_STATUS
211315e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
211415e32d5dSMike Smith {
211515e32d5dSMike Smith     ACPI_STATUS	status;
211615e32d5dSMike Smith 
2117b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2118cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
21190ae55423SMike Smith 
2120be2b1797SNate Lawson     if (sc->acpi_enabled)
212115e32d5dSMike Smith 	status = AcpiDisable();
2122be2b1797SNate Lawson     else
212315e32d5dSMike Smith 	status = AE_OK;
2124be2b1797SNate Lawson 
212515e32d5dSMike Smith     if (status == AE_OK)
212615e32d5dSMike Smith 	sc->acpi_enabled = 0;
2127be2b1797SNate Lawson 
21280ae55423SMike Smith     return_ACPI_STATUS (status);
212915e32d5dSMike Smith }
213015e32d5dSMike Smith 
213115e32d5dSMike Smith /*
213215e32d5dSMike Smith  * ACPI Event Handlers
213315e32d5dSMike Smith  */
213415e32d5dSMike Smith 
213515e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
213615e32d5dSMike Smith 
213715e32d5dSMike Smith static void
213815e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
213915e32d5dSMike Smith {
2140fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
2141b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
214215e32d5dSMike Smith 
2143cb9b0d80SMike Smith     ACPI_LOCK;
2144a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
214515e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
2146cb9b0d80SMike Smith     ACPI_UNLOCK;
21470ae55423SMike Smith     return_VOID;
214815e32d5dSMike Smith }
214915e32d5dSMike Smith 
215015e32d5dSMike Smith static void
215115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
215215e32d5dSMike Smith {
2153fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
2154b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
21550ae55423SMike Smith 
215615e32d5dSMike Smith     /* Well, what to do? :-) */
21570ae55423SMike Smith 
2158cb9b0d80SMike Smith     ACPI_LOCK;
2159cb9b0d80SMike Smith     ACPI_UNLOCK;
2160cb9b0d80SMike Smith 
21610ae55423SMike Smith     return_VOID;
216215e32d5dSMike Smith }
216315e32d5dSMike Smith 
216415e32d5dSMike Smith /*
216515e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
216615e32d5dSMike Smith  */
216715e32d5dSMike Smith UINT32
216833febf93SNate Lawson acpi_event_power_button_sleep(void *context)
216915e32d5dSMike Smith {
217015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
217115e32d5dSMike Smith 
2172b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21730ae55423SMike Smith 
217415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
21750ae55423SMike Smith 
2176b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
217715e32d5dSMike Smith }
217815e32d5dSMike Smith 
217915e32d5dSMike Smith UINT32
218033febf93SNate Lawson acpi_event_power_button_wake(void *context)
218115e32d5dSMike Smith {
218215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
218315e32d5dSMike Smith 
2184b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21850ae55423SMike Smith 
218615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
21870ae55423SMike Smith 
2188b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
218915e32d5dSMike Smith }
219015e32d5dSMike Smith 
219115e32d5dSMike Smith UINT32
219233febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
219315e32d5dSMike Smith {
219415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
219515e32d5dSMike Smith 
2196b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21970ae55423SMike Smith 
219815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
21990ae55423SMike Smith 
2200b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
220115e32d5dSMike Smith }
220215e32d5dSMike Smith 
220315e32d5dSMike Smith UINT32
220433febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
220515e32d5dSMike Smith {
220615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
220715e32d5dSMike Smith 
2208b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
22090ae55423SMike Smith 
221015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
22110ae55423SMike Smith 
2212b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
221315e32d5dSMike Smith }
221415e32d5dSMike Smith 
221515e32d5dSMike Smith /*
221615e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
221715e32d5dSMike Smith  */
221815e32d5dSMike Smith struct acpi_staticbuf {
221915e32d5dSMike Smith     ACPI_BUFFER	buffer;
222015e32d5dSMike Smith     char	data[512];
222115e32d5dSMike Smith };
222215e32d5dSMike Smith 
222315e32d5dSMike Smith char *
222415e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
222515e32d5dSMike Smith {
222615e32d5dSMike Smith     static struct acpi_staticbuf	buf;
222715e32d5dSMike Smith 
2228cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
2229cb9b0d80SMike Smith 
223015e32d5dSMike Smith     buf.buffer.Length = 512;
223115e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
223215e32d5dSMike Smith 
2233b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
223415e32d5dSMike Smith 	return (buf.buffer.Pointer);
2235be2b1797SNate Lawson 
223615e32d5dSMike Smith     return ("(unknown path)");
223715e32d5dSMike Smith }
223815e32d5dSMike Smith 
223915e32d5dSMike Smith /*
224015e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
224115e32d5dSMike Smith  * parts of the namespace.
224215e32d5dSMike Smith  */
224315e32d5dSMike Smith int
224415e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
224515e32d5dSMike Smith {
22463c600cfbSJohn Baldwin     char	*cp, *env, *np;
224715e32d5dSMike Smith     int		len;
224815e32d5dSMike Smith 
224915e32d5dSMike Smith     np = acpi_name(handle);
225015e32d5dSMike Smith     if (*np == '\\')
225115e32d5dSMike Smith 	np++;
22523c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
225315e32d5dSMike Smith 	return (0);
225415e32d5dSMike Smith 
2255be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
22563c600cfbSJohn Baldwin     cp = env;
225715e32d5dSMike Smith     for (;;) {
225815e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
225915e32d5dSMike Smith 	    cp++;
226015e32d5dSMike Smith 	if (*cp == 0)
226115e32d5dSMike Smith 	    break;
226215e32d5dSMike Smith 	len = 0;
226315e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
226415e32d5dSMike Smith 	    len++;
2265d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
22663c600cfbSJohn Baldwin 	    freeenv(env);
22670ae55423SMike Smith 	    return(1);
2268d786139cSMaxime Henrion 	}
22690ae55423SMike Smith 	cp += len;
22700ae55423SMike Smith     }
22713c600cfbSJohn Baldwin     freeenv(env);
2272be2b1797SNate Lawson 
22730ae55423SMike Smith     return (0);
22740ae55423SMike Smith }
22750ae55423SMike Smith 
22760ae55423SMike Smith /*
22770ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
22780ae55423SMike Smith  */
22790ae55423SMike Smith int
22800ae55423SMike Smith acpi_disabled(char *subsys)
22810ae55423SMike Smith {
228278689b15SMaxime Henrion     char	*cp, *env;
22830ae55423SMike Smith     int		len;
22840ae55423SMike Smith 
22853184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
22860ae55423SMike Smith 	return (0);
22873184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
228878689b15SMaxime Henrion 	freeenv(env);
22890ae55423SMike Smith 	return (1);
2290d786139cSMaxime Henrion     }
22910ae55423SMike Smith 
22923184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
229378689b15SMaxime Henrion     cp = env;
22940ae55423SMike Smith     for (;;) {
22953184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
22960ae55423SMike Smith 	    cp++;
22973184cf5aSNate Lawson 	if (*cp == '\0')
22980ae55423SMike Smith 	    break;
22990ae55423SMike Smith 	len = 0;
23003184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
23010ae55423SMike Smith 	    len++;
23023184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
230378689b15SMaxime Henrion 	    freeenv(env);
230415e32d5dSMike Smith 	    return (1);
2305d786139cSMaxime Henrion 	}
230615e32d5dSMike Smith 	cp += len;
230715e32d5dSMike Smith     }
230878689b15SMaxime Henrion     freeenv(env);
2309be2b1797SNate Lawson 
231015e32d5dSMike Smith     return (0);
231115e32d5dSMike Smith }
231215e32d5dSMike Smith 
231315e32d5dSMike Smith /*
231415e32d5dSMike Smith  * Control interface.
231515e32d5dSMike Smith  *
23160ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2317be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2318be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
231915e32d5dSMike Smith  */
23200ae55423SMike Smith struct acpi_ioctl_hook
23210ae55423SMike Smith {
23220ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
23230ae55423SMike Smith     u_long			 cmd;
2324be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
23250ae55423SMike Smith     void			 *arg;
23260ae55423SMike Smith };
23270ae55423SMike Smith 
23280ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
23290ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
23300ae55423SMike Smith 
23310ae55423SMike Smith /*
23320ae55423SMike Smith  * Register an ioctl handler.
23330ae55423SMike Smith  */
23340ae55423SMike Smith int
2335be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
23360ae55423SMike Smith {
23370ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
23380ae55423SMike Smith 
23390ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
23400ae55423SMike Smith 	return (ENOMEM);
23410ae55423SMike Smith     hp->cmd = cmd;
23420ae55423SMike Smith     hp->fn = fn;
23430ae55423SMike Smith     hp->arg = arg;
23440ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
23450ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
23460ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
23470ae55423SMike Smith     }
23480ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
23490ae55423SMike Smith     return (0);
23500ae55423SMike Smith }
23510ae55423SMike Smith 
23520ae55423SMike Smith /*
23530ae55423SMike Smith  * Deregister an ioctl handler.
23540ae55423SMike Smith  */
23550ae55423SMike Smith void
2356be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
23570ae55423SMike Smith {
23580ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
23590ae55423SMike Smith 
23600ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
23610ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
23620ae55423SMike Smith 	    break;
23630ae55423SMike Smith 
23640ae55423SMike Smith     if (hp != NULL) {
23650ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
23660ae55423SMike Smith 	free(hp, M_ACPIDEV);
23670ae55423SMike Smith     }
23680ae55423SMike Smith }
23690ae55423SMike Smith 
237015e32d5dSMike Smith static int
23716b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
237215e32d5dSMike Smith {
237315e32d5dSMike Smith     return (0);
237415e32d5dSMike Smith }
237515e32d5dSMike Smith 
237615e32d5dSMike Smith static int
23776b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
237815e32d5dSMike Smith {
237915e32d5dSMike Smith     return (0);
238015e32d5dSMike Smith }
238115e32d5dSMike Smith 
238215e32d5dSMike Smith static int
23836b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
238415e32d5dSMike Smith {
238515e32d5dSMike Smith     struct acpi_softc		*sc;
23860ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
23870ae55423SMike Smith     int				error, xerror, state;
2388fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
238915e32d5dSMike Smith 
2390cb9b0d80SMike Smith     ACPI_LOCK;
2391cb9b0d80SMike Smith 
239215e32d5dSMike Smith     error = state = 0;
239315e32d5dSMike Smith     sc = dev->si_drv1;
239415e32d5dSMike Smith 
23950ae55423SMike Smith     /*
23960ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
23970ae55423SMike Smith      */
23980ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
23990ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
24000ae55423SMike Smith 	    if (hp->cmd == cmd) {
24010ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
24020ae55423SMike Smith 		if (xerror != 0)
24030ae55423SMike Smith 		    error = xerror;
2404917d44c8SMitsuru IWASAKI 		goto out;
24050ae55423SMike Smith 	    }
24060ae55423SMike Smith 	}
24070ae55423SMike Smith     }
24080ae55423SMike Smith 
24090ae55423SMike Smith     /*
2410a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2411a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2412a89fcf28STakanori Watanabe      * Not changing system behavior.
2413a89fcf28STakanori Watanabe      */
2414be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2415be2b1797SNate Lawson 	return (EPERM);
2416a89fcf28STakanori Watanabe 
2417be2b1797SNate Lawson     /* Core system ioctls. */
241815e32d5dSMike Smith     switch (cmd) {
241915e32d5dSMike Smith     case ACPIIO_ENABLE:
24200ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
242115e32d5dSMike Smith 	    error = ENXIO;
242215e32d5dSMike Smith 	break;
242315e32d5dSMike Smith     case ACPIIO_DISABLE:
24240ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
242515e32d5dSMike Smith 	    error = ENXIO;
242615e32d5dSMike Smith 	break;
242715e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
242815e32d5dSMike Smith 	if (!sc->acpi_enabled) {
242915e32d5dSMike Smith 	    error = ENXIO;
243015e32d5dSMike Smith 	    break;
243115e32d5dSMike Smith 	}
243215e32d5dSMike Smith 	state = *(int *)addr;
24332d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
24342d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
243515e32d5dSMike Smith 		error = EINVAL;
24362d610c46SNate Lawson 	} else {
24372d610c46SNate Lawson 	    error = EINVAL;
24382d610c46SNate Lawson 	}
243915e32d5dSMike Smith 	break;
244015e32d5dSMike Smith     default:
24410ae55423SMike Smith 	if (error == 0)
244215e32d5dSMike Smith 	    error = EINVAL;
244315e32d5dSMike Smith 	break;
244415e32d5dSMike Smith     }
2445917d44c8SMitsuru IWASAKI 
2446917d44c8SMitsuru IWASAKI out:
2447cb9b0d80SMike Smith     ACPI_UNLOCK;
244815e32d5dSMike Smith     return (error);
244915e32d5dSMike Smith }
245015e32d5dSMike Smith 
24511d073b1dSJohn Baldwin static int
2452d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2453d75de536SMitsuru IWASAKI {
2454d75de536SMitsuru IWASAKI     char sleep_state[4];
2455d75de536SMitsuru IWASAKI     char buf[16];
2456d75de536SMitsuru IWASAKI     int error;
2457d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2458d75de536SMitsuru IWASAKI 
2459d75de536SMitsuru IWASAKI     buf[0] = '\0';
2460d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) {
2461d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2462d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2463d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2464d75de536SMitsuru IWASAKI 	}
2465d75de536SMitsuru IWASAKI     }
2466d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2467d75de536SMitsuru IWASAKI     return (error);
2468d75de536SMitsuru IWASAKI }
2469d75de536SMitsuru IWASAKI 
2470d75de536SMitsuru IWASAKI static int
24711d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
24721d073b1dSJohn Baldwin {
24731d073b1dSJohn Baldwin     char sleep_state[10];
24741d073b1dSJohn Baldwin     int error;
24751d073b1dSJohn Baldwin     u_int new_state, old_state;
24761d073b1dSJohn Baldwin 
24771d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2478b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX + 1) {
24791d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2480cb9b0d80SMike Smith     } else {
2481b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
24821d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
24831d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2484cb9b0d80SMike Smith     }
24851d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
24861d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2487be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2488be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
24891d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
24901d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
24911d073b1dSJohn Baldwin 		break;
2492cb9b0d80SMike Smith 	}
2493931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2494be2b1797SNate Lawson 	    if (new_state != old_state)
24951d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2496cb9b0d80SMike Smith 	} else {
24971d073b1dSJohn Baldwin 	    error = EINVAL;
24981d073b1dSJohn Baldwin 	}
2499cb9b0d80SMike Smith     }
2500be2b1797SNate Lawson 
25011d073b1dSJohn Baldwin     return (error);
25021d073b1dSJohn Baldwin }
25031d073b1dSJohn Baldwin 
25049b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
25059b937d48SNate Lawson void
25069b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
25079b937d48SNate Lawson {
25089b937d48SNate Lawson     char		notify_buf[16];
25099b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
25109b937d48SNate Lawson     ACPI_STATUS		status;
25119b937d48SNate Lawson 
25129b937d48SNate Lawson     if (subsystem == NULL)
25139b937d48SNate Lawson 	return;
25149b937d48SNate Lawson 
25159b937d48SNate Lawson     handle_buf.Pointer = NULL;
25169b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
25179b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
25189b937d48SNate Lawson     if (ACPI_FAILURE(status))
25199b937d48SNate Lawson 	return;
25209b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
25219b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
25229b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
25239b937d48SNate Lawson }
25249b937d48SNate Lawson 
252515e32d5dSMike Smith #ifdef ACPI_DEBUG
25260ae55423SMike Smith /*
25270ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
25280ae55423SMike Smith  *
25290ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
25300ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
25310ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
25320ae55423SMike Smith  * prefixing the bit name with !.
25330ae55423SMike Smith  */
253415e32d5dSMike Smith struct debugtag
253515e32d5dSMike Smith {
253615e32d5dSMike Smith     char	*name;
253715e32d5dSMike Smith     UINT32	value;
253815e32d5dSMike Smith };
253915e32d5dSMike Smith 
254015e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2541c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2542c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2543c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2544c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2545c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2546c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2547c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2548c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2549c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2550d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
25514c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2552d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2553297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
25544c1cdee6SMike Smith 
2555c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2556c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
25573184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2558c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
25593184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
25603184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
25613184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
25624c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2563cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
25643184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2565b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
256615e32d5dSMike Smith     {NULL, 0}
256715e32d5dSMike Smith };
256815e32d5dSMike Smith 
256915e32d5dSMike Smith static struct debugtag dbg_level[] = {
25704c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
25719501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
25729501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
25734c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
25749501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
25754c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2576d62ab2f4SMitsuru IWASAKI 
2577d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2578297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
25794c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
25804c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2581d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
25824c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
25834c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
25844c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
25854c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
25864c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
25874c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
25884c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
25894c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
25904c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
25914c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2592d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2593d62ab2f4SMitsuru IWASAKI 
2594d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2595d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2596d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2597d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2598d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
25994c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2600d62ab2f4SMitsuru IWASAKI 
2601d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2602d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2603d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2604d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2605d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2606d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2607d62ab2f4SMitsuru IWASAKI 
2608d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
260998479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
261098479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
261198479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
261298479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
261398479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
261415e32d5dSMike Smith     {NULL, 0}
261515e32d5dSMike Smith };
261615e32d5dSMike Smith 
261715e32d5dSMike Smith static void
261815e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
261915e32d5dSMike Smith {
262015e32d5dSMike Smith     char	*ep;
262115e32d5dSMike Smith     int		i, l;
26220ae55423SMike Smith     int		set;
262315e32d5dSMike Smith 
262415e32d5dSMike Smith     while (*cp) {
262515e32d5dSMike Smith 	if (isspace(*cp)) {
262615e32d5dSMike Smith 	    cp++;
262715e32d5dSMike Smith 	    continue;
262815e32d5dSMike Smith 	}
262915e32d5dSMike Smith 	ep = cp;
263015e32d5dSMike Smith 	while (*ep && !isspace(*ep))
263115e32d5dSMike Smith 	    ep++;
26320ae55423SMike Smith 	if (*cp == '!') {
26330ae55423SMike Smith 	    set = 0;
26340ae55423SMike Smith 	    cp++;
26350ae55423SMike Smith 	    if (cp == ep)
26360ae55423SMike Smith 		continue;
26370ae55423SMike Smith 	} else {
26380ae55423SMike Smith 	    set = 1;
26390ae55423SMike Smith 	}
264015e32d5dSMike Smith 	l = ep - cp;
264115e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
264215e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2643be2b1797SNate Lawson 		if (set)
264415e32d5dSMike Smith 		    *flag |= tag[i].value;
2645be2b1797SNate Lawson 		else
26460ae55423SMike Smith 		    *flag &= ~tag[i].value;
264715e32d5dSMike Smith 	    }
264815e32d5dSMike Smith 	}
264915e32d5dSMike Smith 	cp = ep;
265015e32d5dSMike Smith     }
265115e32d5dSMike Smith }
265215e32d5dSMike Smith 
265315e32d5dSMike Smith static void
26542a4ac806SMike Smith acpi_set_debugging(void *junk)
265515e32d5dSMike Smith {
26567e639165SNate Lawson     char	*layer, *level;
265715e32d5dSMike Smith 
26581d7b121cSNate Lawson     if (cold) {
26590ae55423SMike Smith 	AcpiDbgLayer = 0;
26600ae55423SMike Smith 	AcpiDbgLevel = 0;
26611d7b121cSNate Lawson     }
26621d7b121cSNate Lawson 
26637e639165SNate Lawson     layer = getenv("debug.acpi.layer");
26647e639165SNate Lawson     level = getenv("debug.acpi.level");
26657e639165SNate Lawson     if (layer == NULL && level == NULL)
26667e639165SNate Lawson 	return;
26670ae55423SMike Smith 
26687e639165SNate Lawson     printf("ACPI set debug");
26697e639165SNate Lawson     if (layer != NULL) {
26707e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
26717e639165SNate Lawson 	    printf(" layer '%s'", layer);
26727e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
26737e639165SNate Lawson 	freeenv(layer);
26741d7b121cSNate Lawson     }
26757e639165SNate Lawson     if (level != NULL) {
26767e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
26777e639165SNate Lawson 	    printf(" level '%s'", level);
26787e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
26797e639165SNate Lawson 	freeenv(level);
26807e639165SNate Lawson     }
26817e639165SNate Lawson     printf("\n");
268215e32d5dSMike Smith }
2683be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2684be2b1797SNate Lawson 	NULL);
26851d7b121cSNate Lawson 
26861d7b121cSNate Lawson static int
26871d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
26881d7b121cSNate Lawson {
268954f6bca0SNate Lawson     int		 error, *dbg;
26901d7b121cSNate Lawson     struct	 debugtag *tag;
269154f6bca0SNate Lawson     struct	 sbuf sb;
26921d7b121cSNate Lawson 
269354f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
269454f6bca0SNate Lawson 	return (ENOMEM);
26951d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
26961d7b121cSNate Lawson 	tag = &dbg_layer[0];
26971d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
26981d7b121cSNate Lawson     } else {
26991d7b121cSNate Lawson 	tag = &dbg_level[0];
27001d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
27011d7b121cSNate Lawson     }
27021d7b121cSNate Lawson 
27031d7b121cSNate Lawson     /* Get old values if this is a get request. */
27041d7b121cSNate Lawson     if (*dbg == 0) {
270554f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
27061d7b121cSNate Lawson     } else if (req->newptr == NULL) {
27071d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
270854f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
270954f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
27101d7b121cSNate Lawson 	}
27111d7b121cSNate Lawson     }
271254f6bca0SNate Lawson     sbuf_trim(&sb);
271354f6bca0SNate Lawson     sbuf_finish(&sb);
27141d7b121cSNate Lawson 
27157e639165SNate Lawson     /* Copy out the old values to the user. */
27167e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
271754f6bca0SNate Lawson     sbuf_delete(&sb);
27181d7b121cSNate Lawson 
27191d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
27201d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
27211d7b121cSNate Lawson 	*dbg = 0;
27221d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
27231d7b121cSNate Lawson 	acpi_set_debugging(NULL);
27241d7b121cSNate Lawson     }
27251d7b121cSNate Lawson 
27261d7b121cSNate Lawson     return (error);
27271d7b121cSNate Lawson }
27281d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
27291d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
27301d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
27311d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
273215e32d5dSMike Smith #endif
2733f9390180SMitsuru IWASAKI 
2734f9390180SMitsuru IWASAKI static int
2735f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2736f9390180SMitsuru IWASAKI {
2737f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2738f9390180SMitsuru IWASAKI 	int	error;
2739f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2740f9390180SMitsuru IWASAKI 	va_list	ap;
2741f9390180SMitsuru IWASAKI 
2742f9390180SMitsuru IWASAKI 	error = 0;
2743f9390180SMitsuru IWASAKI 	switch (cmd) {
2744f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2745f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2746f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2747f9390180SMitsuru IWASAKI 			error = EINVAL;
2748f9390180SMitsuru IWASAKI 			goto out;
2749f9390180SMitsuru IWASAKI 		}
2750f9390180SMitsuru IWASAKI 
2751f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2752f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2753f9390180SMitsuru IWASAKI 		va_end(ap);
2754f9390180SMitsuru IWASAKI 
2755f9390180SMitsuru IWASAKI 		switch (state) {
2756f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2757f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2758f9390180SMitsuru IWASAKI 			break;
2759f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2760f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2761f9390180SMitsuru IWASAKI 			break;
2762f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2763f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2764f9390180SMitsuru IWASAKI 			break;
2765f9390180SMitsuru IWASAKI 		default:
2766f9390180SMitsuru IWASAKI 			error = EINVAL;
2767f9390180SMitsuru IWASAKI 			goto out;
2768f9390180SMitsuru IWASAKI 		}
2769f9390180SMitsuru IWASAKI 
2770f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2771f9390180SMitsuru IWASAKI 		break;
2772f9390180SMitsuru IWASAKI 	default:
2773f9390180SMitsuru IWASAKI 		error = EINVAL;
2774f9390180SMitsuru IWASAKI 		goto out;
2775f9390180SMitsuru IWASAKI 	}
2776f9390180SMitsuru IWASAKI 
2777f9390180SMitsuru IWASAKI out:
2778f9390180SMitsuru IWASAKI 	return (error);
2779f9390180SMitsuru IWASAKI }
2780f9390180SMitsuru IWASAKI 
2781f9390180SMitsuru IWASAKI static void
2782f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2783f9390180SMitsuru IWASAKI {
2784be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
27856c407052SMitsuru IWASAKI 	return;
2786f9390180SMitsuru IWASAKI 
2787f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2788f9390180SMitsuru IWASAKI }
2789f9390180SMitsuru IWASAKI 
2790f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2791