xref: /freebsd/sys/dev/acpica/acpi.c (revision 904bf0c2f730e396de2501e41e4d7ffeadc8ac25)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  *
2915e32d5dSMike Smith  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
3815e32d5dSMike Smith #include <sys/bus.h>
3915e32d5dSMike Smith #include <sys/conf.h>
4015e32d5dSMike Smith #include <sys/ioccom.h>
4115e32d5dSMike Smith #include <sys/reboot.h>
4215e32d5dSMike Smith #include <sys/sysctl.h>
4315e32d5dSMike Smith #include <sys/ctype.h>
441611ea87SMitsuru IWASAKI #include <sys/linker.h>
45f9390180SMitsuru IWASAKI #include <sys/power.h>
4654f6bca0SNate Lawson #include <sys/sbuf.h>
47eeb3a05fSNate Lawson #include <sys/smp.h>
4815e32d5dSMike Smith 
4915e32d5dSMike Smith #include <machine/clock.h>
5015e32d5dSMike Smith #include <machine/resource.h>
51dc750869SNate Lawson #include <machine/bus.h>
52dc750869SNate Lawson #include <sys/rman.h>
53bc0f2195SMike Smith #include <isa/isavar.h>
54bc0f2195SMike Smith 
5515e32d5dSMike Smith #include "acpi.h"
5615e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5715e32d5dSMike Smith #include <dev/acpica/acpiio.h>
589b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
5915e32d5dSMike Smith 
6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6115e32d5dSMike Smith 
62be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
63cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
64b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
650ae55423SMike Smith 
6615e32d5dSMike Smith static d_open_t		acpiopen;
6715e32d5dSMike Smith static d_close_t	acpiclose;
6815e32d5dSMike Smith static d_ioctl_t	acpiioctl;
6915e32d5dSMike Smith 
7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
71dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
72dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
737ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
747ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
757ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
767ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
7715e32d5dSMike Smith };
7815e32d5dSMike Smith 
79fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
80cb9b0d80SMike Smith struct mtx	acpi_mutex;
81fc0ea94aSJohn Baldwin #endif
82cb9b0d80SMike Smith 
833184cf5aSNate Lawson struct acpi_quirks {
843184cf5aSNate Lawson     char	*OemId;
853184cf5aSNate Lawson     uint32_t	OemRevision;
863184cf5aSNate Lawson     char	*value;
873184cf5aSNate Lawson };
883184cf5aSNate Lawson 
893184cf5aSNate Lawson #define ACPI_OEM_REV_ANY	0
903184cf5aSNate Lawson 
913184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = {
923184cf5aSNate Lawson #ifdef notyet
933184cf5aSNate Lawson     /* Bad PCI routing table.  Used on some SuperMicro boards. */
943184cf5aSNate Lawson     { "PTLTD ", 0x06040000, "pci_link" },
953184cf5aSNate Lawson #endif
963184cf5aSNate Lawson 
973184cf5aSNate Lawson     { NULL, 0, NULL }
983184cf5aSNate Lawson };
993184cf5aSNate Lawson 
1006d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10115e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10215e32d5dSMike Smith static int	acpi_probe(device_t dev);
10315e32d5dSMike Smith static int	acpi_attach(device_t dev);
1043184cf5aSNate Lawson static void	acpi_quirks_set(void);
105be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
106be2b1797SNate Lawson 			int unit);
10715e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
108be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
109be2b1797SNate Lawson 			uintptr_t *result);
110be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
111be2b1797SNate Lawson 			uintptr_t value);
112be2b1797SNate Lawson static int	acpi_set_resource(device_t dev, device_t child, int type,
113be2b1797SNate Lawson 			int rid, u_long start, u_long count);
114be2b1797SNate Lawson static int	acpi_get_resource(device_t dev, device_t child, int type,
115be2b1797SNate Lawson 			int rid, u_long *startp, u_long *countp);
116be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
117be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
118be2b1797SNate Lawson 			u_long count, u_int flags);
119be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
120be2b1797SNate Lawson 			int rid, struct resource *r);
1211e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1221e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
123be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
124be2b1797SNate Lawson 			struct isa_pnp_id *ids);
12515e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
126be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
127be2b1797SNate Lawson 			void *context, void **status);
12815e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
12915e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
130eeb3a05fSNate Lawson static void	acpi_shutdown_poweroff(void *arg);
13113d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
13215e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13315e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
134d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1351d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
136f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
137879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
138879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
139879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
140879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
141879d6c23STakanori Watanabe 
14215e32d5dSMike Smith static device_method_t acpi_methods[] = {
14315e32d5dSMike Smith     /* Device interface */
14415e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
14515e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
14615e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
14756a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
14815e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
14915e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
15015e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
15115e32d5dSMike Smith 
15215e32d5dSMike Smith     /* Bus interface */
15315e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
15415e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
15515e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
15615e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
15715e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
15815e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
15915e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
16015e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
161879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
162879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
16315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
16415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
16515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
16615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
16715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
16815e32d5dSMike Smith 
169bc0f2195SMike Smith     /* ISA emulation */
170bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
171bc0f2195SMike Smith 
17215e32d5dSMike Smith     {0, 0}
17315e32d5dSMike Smith };
17415e32d5dSMike Smith 
17515e32d5dSMike Smith static driver_t acpi_driver = {
17615e32d5dSMike Smith     "acpi",
17715e32d5dSMike Smith     acpi_methods,
17815e32d5dSMike Smith     sizeof(struct acpi_softc),
17915e32d5dSMike Smith };
18015e32d5dSMike Smith 
1813273b005SMike Smith static devclass_t acpi_devclass;
1826d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
183a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
18415e32d5dSMike Smith 
185865b8d0bSNate Lawson static const char* sleep_state_names[] = {
186865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
187865b8d0bSNate Lawson 
1881d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
1891d7b121cSNate Lawson static char acpi_ca_version[12];
1901d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
1911d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
19215e32d5dSMike Smith 
19315e32d5dSMike Smith /*
194413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
195c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
196c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
197c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
198c9b8d77dSNate Lawson  * some IBMs use such code.
199413081d7SNate Lawson  */
200c9b8d77dSNate Lawson static int acpi_serialize_methods;
201413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
202413081d7SNate Lawson 
203c9b8d77dSNate Lawson /*
204c9b8d77dSNate Lawson  * Allow override of whether to support the _OSI method.  This allows us
205c9b8d77dSNate Lawson  * to claim compatibility with various MS OSs without changing the value
206c9b8d77dSNate Lawson  * we report for _OS.  This is enabled by default since it fixes some
207c9b8d77dSNate Lawson  * problems with interrupt routing although it can be disabled if it
208c9b8d77dSNate Lawson  * causes problems.  See the definition of "AcpiGbl_ValidOsiStrings" for
209c9b8d77dSNate Lawson  * a list of systems we claim.
210c9b8d77dSNate Lawson  */
211c2b3a864SNate Lawson static int acpi_osi_method = TRUE;
212413081d7SNate Lawson TUNABLE_INT("hw.acpi.osi_method", &acpi_osi_method);
213413081d7SNate Lawson 
214413081d7SNate Lawson /*
2156d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2166d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
217be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
218be2b1797SNate Lawson  * off it.
2196d63101aSMike Smith  */
2206d63101aSMike Smith static int
2216d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2226d63101aSMike Smith {
2236d63101aSMike Smith     switch(event) {
2246d63101aSMike Smith     case MOD_LOAD:
22587b45ed5SMitsuru IWASAKI 	if (!cold) {
22687b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2276d63101aSMike Smith 	    return (EPERM);
22887b45ed5SMitsuru IWASAKI 	}
2296d63101aSMike Smith 	break;
2306d63101aSMike Smith     case MOD_UNLOAD:
231f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2326d63101aSMike Smith 	    return (EBUSY);
233f9390180SMitsuru IWASAKI 	break;
2346d63101aSMike Smith     default:
2356d63101aSMike Smith 	break;
2366d63101aSMike Smith     }
2376d63101aSMike Smith     return (0);
2386d63101aSMike Smith }
2396d63101aSMike Smith 
2406d63101aSMike Smith /*
241bbc2815cSJohn Baldwin  * Perform early initialization.
24215e32d5dSMike Smith  */
243bbc2815cSJohn Baldwin ACPI_STATUS
244bbc2815cSJohn Baldwin acpi_Startup(void)
24515e32d5dSMike Smith {
246d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
247d786139cSMaxime Henrion     char *debugpoint;
24815e32d5dSMike Smith #endif
249bbc2815cSJohn Baldwin     static int error, started = 0;
25015e32d5dSMike Smith 
2511b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2521b8c233dSPeter Pentchev 
253bbc2815cSJohn Baldwin     if (started)
254bbc2815cSJohn Baldwin 	return_VALUE (error);
255bbc2815cSJohn Baldwin     started = 1;
25615e32d5dSMike Smith 
257fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000
258be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2596008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
260fc0ea94aSJohn Baldwin #endif
261cb9b0d80SMike Smith 
262413081d7SNate Lawson     /*
263413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
264413081d7SNate Lawson      * uses UINT8 for some values and we have no tunable_uint8.
265413081d7SNate Lawson      */
266413081d7SNate Lawson     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
267413081d7SNate Lawson     AcpiGbl_CreateOsiMethod = acpi_osi_method;
268a50f2c9fSNate Lawson     AcpiGbl_LeaveWakeGpesDisabled = FALSE;
269413081d7SNate Lawson 
270be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
271d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
272d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
273d786139cSMaxime Henrion     if (debugpoint) {
274d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "init"))
27515e32d5dSMike Smith 	    acpi_EnterDebugger();
276d786139cSMaxime Henrion 	freeenv(debugpoint);
277d786139cSMaxime Henrion     }
27815e32d5dSMike Smith #endif
279b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
280bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
281bbc2815cSJohn Baldwin 	return_VALUE (error);
28215e32d5dSMike Smith     }
283d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
284d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
285d786139cSMaxime Henrion     if (debugpoint) {
286d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "tables"))
28715e32d5dSMike Smith 	    acpi_EnterDebugger();
288d786139cSMaxime Henrion 	freeenv(debugpoint);
289d786139cSMaxime Henrion     }
29015e32d5dSMike Smith #endif
2911611ea87SMitsuru IWASAKI 
292b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
293bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
294bbc2815cSJohn Baldwin 	return_VALUE(error);
29515e32d5dSMike Smith     }
2963184cf5aSNate Lawson 
2973184cf5aSNate Lawson     /* Set up any quirks we have for this XSDT. */
2983184cf5aSNate Lawson     acpi_quirks_set();
2994e376d58SNate Lawson     if (acpi_disabled("acpi"))
3004e376d58SNate Lawson 	return_VALUE (AE_ERROR);
3013184cf5aSNate Lawson 
302bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
303bbc2815cSJohn Baldwin }
304bbc2815cSJohn Baldwin 
305bbc2815cSJohn Baldwin /*
306bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
307bbc2815cSJohn Baldwin  */
308bbc2815cSJohn Baldwin static void
309bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
310bbc2815cSJohn Baldwin {
311bbc2815cSJohn Baldwin     device_t	child;
312bbc2815cSJohn Baldwin 
313bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
314bbc2815cSJohn Baldwin 
315bbc2815cSJohn Baldwin     if (!cold)
316bbc2815cSJohn Baldwin 	return_VOID;
317bbc2815cSJohn Baldwin 
318bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
319bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
320bbc2815cSJohn Baldwin 	return_VOID;
321bbc2815cSJohn Baldwin 
322bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
323bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
324bbc2815cSJohn Baldwin 	return_VOID;
325bbc2815cSJohn Baldwin 
326bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
327bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
328bbc2815cSJohn Baldwin 	return_VOID;
32915e32d5dSMike Smith 
3304e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3314e376d58SNate Lawson 
332be2b1797SNate Lawson     /* Attach the actual ACPI device. */
33315e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
33415e32d5dSMike Smith 	device_printf(parent, "ACPI: could not attach\n");
3350ae55423SMike Smith 	return_VOID;
33615e32d5dSMike Smith     }
33715e32d5dSMike Smith }
33815e32d5dSMike Smith 
33915e32d5dSMike Smith /*
34015e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
34115e32d5dSMike Smith  */
34215e32d5dSMike Smith static int
34315e32d5dSMike Smith acpi_probe(device_t dev)
34415e32d5dSMike Smith {
34515e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
34615e32d5dSMike Smith     char		buf[20];
34715e32d5dSMike Smith     int			error;
3482ccd1cacSNate Lawson     struct sbuf		sb;
3492ccd1cacSNate Lawson     ACPI_STATUS		status;
350fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
35115e32d5dSMike Smith 
352b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
353f9390180SMitsuru IWASAKI 
354f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
355f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
356be2b1797SNate Lawson 
357f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
358f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
359f9390180SMitsuru IWASAKI     }
360f9390180SMitsuru IWASAKI 
361cb9b0d80SMike Smith     ACPI_LOCK;
3620ae55423SMike Smith 
363b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
364be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
365be2b1797SNate Lawson 		      AcpiFormatException(status));
366cb9b0d80SMike Smith 	error = ENXIO;
367cb9b0d80SMike Smith     } else {
3682ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3692ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3702ccd1cacSNate Lawson 	sbuf_trim(&sb);
3712ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3722ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3732ccd1cacSNate Lawson 	sbuf_trim(&sb);
3742ccd1cacSNate Lawson 	sbuf_finish(&sb);
3752ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3762ccd1cacSNate Lawson 	sbuf_delete(&sb);
377cb9b0d80SMike Smith 	error = 0;
378cb9b0d80SMike Smith     }
379cb9b0d80SMike Smith     ACPI_UNLOCK;
380cb9b0d80SMike Smith     return_VALUE(error);
38115e32d5dSMike Smith }
38215e32d5dSMike Smith 
38315e32d5dSMike Smith static int
38415e32d5dSMike Smith acpi_attach(device_t dev)
38515e32d5dSMike Smith {
38615e32d5dSMike Smith     struct acpi_softc	*sc;
387cb9b0d80SMike Smith     ACPI_STATUS		status;
38815e32d5dSMike Smith     int			error;
38932d18aa5SMike Smith     UINT32		flags;
390498d464fSMitsuru IWASAKI     char		*env;
391d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
392d786139cSMaxime Henrion     char		*debugpoint;
39315e32d5dSMike Smith #endif
394fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
39515e32d5dSMike Smith 
396b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
397cb9b0d80SMike Smith     ACPI_LOCK;
39815e32d5dSMike Smith     sc = device_get_softc(dev);
39915e32d5dSMike Smith     bzero(sc, sizeof(*sc));
40015e32d5dSMike Smith     sc->acpi_dev = dev;
40115e32d5dSMike Smith 
402d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
403d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
404d786139cSMaxime Henrion     if (debugpoint) {
405d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "spaces"))
40615e32d5dSMike Smith 	    acpi_EnterDebugger();
407d786139cSMaxime Henrion 	freeenv(debugpoint);
408d786139cSMaxime Henrion     }
40915e32d5dSMike Smith #endif
41015e32d5dSMike Smith 
411be2b1797SNate Lawson     /* Install the default address space handlers. */
412cb9b0d80SMike Smith     error = ENXIO;
413be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
414be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
415be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
416be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
417be2b1797SNate Lawson 		      AcpiFormatException(status));
418cb9b0d80SMike Smith 	goto out;
41915e32d5dSMike Smith     }
420be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
421be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
422be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
423be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
424be2b1797SNate Lawson 		      AcpiFormatException(status));
425cb9b0d80SMike Smith 	goto out;
42615e32d5dSMike Smith     }
427be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
428be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
429be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
430be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
431be2b1797SNate Lawson 		      AcpiFormatException(status));
432cb9b0d80SMike Smith 	goto out;
43315e32d5dSMike Smith     }
43415e32d5dSMike Smith 
43515e32d5dSMike Smith     /*
43615e32d5dSMike Smith      * Bring ACPI fully online.
43715e32d5dSMike Smith      *
438be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
439be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
440be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
441be2b1797SNate Lawson      * object init pass.
44215e32d5dSMike Smith      *
44332d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
44432d18aa5SMike Smith      *
445be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
446be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
44715e32d5dSMike Smith      */
448d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
449d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
450d786139cSMaxime Henrion     if (debugpoint) {
451d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "enable"))
45215e32d5dSMike Smith 	    acpi_EnterDebugger();
453d786139cSMaxime Henrion 	freeenv(debugpoint);
454d786139cSMaxime Henrion     }
45515e32d5dSMike Smith #endif
45632d18aa5SMike Smith     flags = 0;
457d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
45832d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
459b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
460be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
461be2b1797SNate Lawson 		      AcpiFormatException(status));
462cb9b0d80SMike Smith 	goto out;
46315e32d5dSMike Smith     }
46415e32d5dSMike Smith 
465f8335e3aSNate Lawson     /*
466f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
467f8335e3aSNate Lawson      * the namespace has been evaluated.
468f8335e3aSNate Lawson      */
469f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
470f8335e3aSNate Lawson 
471b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
472be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
473be2b1797SNate Lawson 		      AcpiFormatException(status));
474b69ed3f4SMitsuru IWASAKI 	goto out;
475b69ed3f4SMitsuru IWASAKI     }
476b69ed3f4SMitsuru IWASAKI 
47715e32d5dSMike Smith     /*
4781d073b1dSJohn Baldwin      * Setup our sysctl tree.
4791d073b1dSJohn Baldwin      *
4801d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4811d073b1dSJohn Baldwin      */
4821d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4831d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4841d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4851d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4861d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
487d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
488d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
489d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4901d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4911d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4921d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4931d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4941d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4951d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4961d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4971d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
498f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
499f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
500f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
501f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
502f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
503f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5042d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
505ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
506ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
507ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5081611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
5091611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
5101611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5112d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
5122d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
513c6a78e98STakanori Watanabe     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
514c6a78e98STakanori Watanabe 	OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
515c6a78e98STakanori Watanabe 	&sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
516bf0c18ecSNate Lawson 
517bf0c18ecSNate Lawson     /*
518bf0c18ecSNate Lawson      * Default to 5 seconds before sleeping to give some machines time to
519bf0c18ecSNate Lawson      * stabilize.
520bf0c18ecSNate Lawson      */
521bf0c18ecSNate Lawson     sc->acpi_sleep_delay = 5;
522c6a78e98STakanori Watanabe     sc->acpi_disable_on_poweroff = 1;
5232d644607SMitsuru IWASAKI     if (bootverbose)
5242d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
525498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
526498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
527498d464fSMitsuru IWASAKI 	freeenv(env);
528498d464fSMitsuru IWASAKI     }
5291d073b1dSJohn Baldwin 
5302d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5312d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5322d610c46SNate Lawson 	    sc->acpi_s4bios = 1;
5332d610c46SNate Lawson 
5341d073b1dSJohn Baldwin     /*
53515e32d5dSMike Smith      * Dispatch the default sleep state to devices.
53615e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
53715e32d5dSMike Smith      */
53815e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
53915e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
54015e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
541f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
542f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
54315e32d5dSMike Smith 
54413d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
54515e32d5dSMike Smith 
54615e32d5dSMike Smith     /*
54715e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
54815e32d5dSMike Smith      */
549d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
550d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
551d786139cSMaxime Henrion     if (debugpoint) {
552d786139cSMaxime Henrion 	if (!strcmp(debugpoint, "probe"))
55315e32d5dSMike Smith 	    acpi_EnterDebugger();
554d786139cSMaxime Henrion 	freeenv(debugpoint);
555d786139cSMaxime Henrion     }
55615e32d5dSMike Smith #endif
55715e32d5dSMike Smith 
558be2b1797SNate Lawson     /* Register our shutdown handlers */
559be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc,
560be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
561be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
562be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
56315e32d5dSMike Smith 
56415e32d5dSMike Smith     /*
56515e32d5dSMike Smith      * Register our acpi event handlers.
56615e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
56715e32d5dSMike Smith      */
568be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
569be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
570be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
571be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
57215e32d5dSMike Smith 
573be2b1797SNate Lawson     /* Flag our initial states. */
57415e32d5dSMike Smith     sc->acpi_enabled = 1;
57515e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
576ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
57715e32d5dSMike Smith 
578be2b1797SNate Lawson     /* Create the control device */
579a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5802a4689e8SRobert Watson 			      "acpi");
58115e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
58215e32d5dSMike Smith 
583d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER
584d786139cSMaxime Henrion     debugpoint = getenv("debug.acpi.debugger");
585d786139cSMaxime Henrion     if (debugpoint) {
586be2b1797SNate Lawson 	if (strcmp(debugpoint, "running") == 0)
58715e32d5dSMike Smith 	    acpi_EnterDebugger();
588d786139cSMaxime Henrion 	freeenv(debugpoint);
589d786139cSMaxime Henrion     }
59015e32d5dSMike Smith #endif
591f86214b6SMitsuru IWASAKI 
59291da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS
593be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
594c573e654SMitsuru IWASAKI 	goto out;
595c573e654SMitsuru IWASAKI #endif
596c573e654SMitsuru IWASAKI 
597be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
598f86214b6SMitsuru IWASAKI 	goto out;
599f86214b6SMitsuru IWASAKI 
600f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
601f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
602f9390180SMitsuru IWASAKI 
603eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
604eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
605eeb6dba2SJohn Baldwin 
606cb9b0d80SMike Smith     error = 0;
607cb9b0d80SMike Smith 
608cb9b0d80SMike Smith  out:
609cb9b0d80SMike Smith     ACPI_UNLOCK;
610cb9b0d80SMike Smith     return_VALUE (error);
61115e32d5dSMike Smith }
61215e32d5dSMike Smith 
6133184cf5aSNate Lawson static void
6143184cf5aSNate Lawson acpi_quirks_set()
6153184cf5aSNate Lawson {
6163184cf5aSNate Lawson     XSDT_DESCRIPTOR *xsdt;
6173184cf5aSNate Lawson     struct acpi_quirks *quirk;
6183184cf5aSNate Lawson     char *env, *tmp;
6193184cf5aSNate Lawson     int len;
6203184cf5aSNate Lawson 
6214e376d58SNate Lawson     /*
6224e376d58SNate Lawson      * If the user loaded a custom table or disabled "quirks", leave
6234e376d58SNate Lawson      * the settings alone.
6244e376d58SNate Lawson      */
6253184cf5aSNate Lawson     len = 0;
6264e376d58SNate Lawson     if ((env = getenv("acpi_dsdt_load")) != NULL) {
6274e376d58SNate Lawson 	/* XXX No strcasecmp but this is good enough. */
6284e376d58SNate Lawson 	if (*env == 'Y' || *env == 'y')
6294e376d58SNate Lawson 	    goto out;
6304e376d58SNate Lawson 	freeenv(env);
6314e376d58SNate Lawson     }
6323184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) != NULL) {
6334e376d58SNate Lawson 	if (strstr("quirks", env) != NULL)
6343184cf5aSNate Lawson 	    goto out;
6353184cf5aSNate Lawson 	len = strlen(env);
6363184cf5aSNate Lawson     }
6373184cf5aSNate Lawson 
6383184cf5aSNate Lawson     /*
6393184cf5aSNate Lawson      * Search through our quirk table and concatenate the disabled
6403184cf5aSNate Lawson      * values with whatever we find.
6413184cf5aSNate Lawson      */
6423184cf5aSNate Lawson     xsdt = AcpiGbl_XSDT;
6433184cf5aSNate Lawson     for (quirk = acpi_quirks_table; quirk->OemId; quirk++) {
6443184cf5aSNate Lawson 	if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) &&
6453184cf5aSNate Lawson 	    (xsdt->OemRevision == quirk->OemRevision ||
6463184cf5aSNate Lawson 	    quirk->OemRevision == ACPI_OEM_REV_ANY)) {
6473184cf5aSNate Lawson 		len += strlen(quirk->value) + 2;
6483184cf5aSNate Lawson 		if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL)
6493184cf5aSNate Lawson 		    goto out;
6503184cf5aSNate Lawson 		sprintf(tmp, "%s %s", env ? env : "", quirk->value);
6513184cf5aSNate Lawson 		setenv("debug.acpi.disabled", tmp);
6523184cf5aSNate Lawson 		free(tmp, M_TEMP);
6533184cf5aSNate Lawson 		break;
6543184cf5aSNate Lawson 	}
6553184cf5aSNate Lawson     }
6563184cf5aSNate Lawson 
6573184cf5aSNate Lawson out:
6583184cf5aSNate Lawson     if (env)
6593184cf5aSNate Lawson 	freeenv(env);
6603184cf5aSNate Lawson }
6613184cf5aSNate Lawson 
66215e32d5dSMike Smith /*
66315e32d5dSMike Smith  * Handle a new device being added
66415e32d5dSMike Smith  */
66515e32d5dSMike Smith static device_t
66615e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
66715e32d5dSMike Smith {
66815e32d5dSMike Smith     struct acpi_device	*ad;
66915e32d5dSMike Smith     device_t		child;
67015e32d5dSMike Smith 
671be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
67215e32d5dSMike Smith 	return (NULL);
67315e32d5dSMike Smith 
67415e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
67515e32d5dSMike Smith 
67615e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
67715e32d5dSMike Smith     if (child != NULL)
67815e32d5dSMike Smith 	device_set_ivars(child, ad);
67915e32d5dSMike Smith     return (child);
68015e32d5dSMike Smith }
68115e32d5dSMike Smith 
68215e32d5dSMike Smith static int
68315e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
68415e32d5dSMike Smith {
68515e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
68615e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
68715e32d5dSMike Smith     int retval = 0;
68815e32d5dSMike Smith 
68915e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
6909ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
6919ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
6929ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
6939ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
69415e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
69515e32d5dSMike Smith 
69615e32d5dSMike Smith     return (retval);
69715e32d5dSMike Smith }
69815e32d5dSMike Smith 
69963600cc3SNate Lawson /* Location hint for devctl(8) */
70063600cc3SNate Lawson static int
701247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
702247648afSTakanori Watanabe     size_t buflen)
703247648afSTakanori Watanabe {
704247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
705247648afSTakanori Watanabe 
706247648afSTakanori Watanabe     if (dinfo->ad_handle)
707247648afSTakanori Watanabe 	snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle));
708247648afSTakanori Watanabe     else
709247648afSTakanori Watanabe 	snprintf(buf, buflen, "magic=unknown");
710247648afSTakanori Watanabe     return (0);
711247648afSTakanori Watanabe }
712247648afSTakanori Watanabe 
71363600cc3SNate Lawson /* PnP information for devctl(8) */
71463600cc3SNate Lawson static int
715247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
716247648afSTakanori Watanabe     size_t buflen)
717247648afSTakanori Watanabe {
718247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
71963600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
72063600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
721247648afSTakanori Watanabe     char *end;
722247648afSTakanori Watanabe     int error;
723247648afSTakanori Watanabe 
724247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
725247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
726247648afSTakanori Watanabe 
727247648afSTakanori Watanabe     if (error)
728247648afSTakanori Watanabe 	snprintf(buf, buflen, "Unknown");
729247648afSTakanori Watanabe     else
730247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
731247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
732247648afSTakanori Watanabe 		 adinfo->HardwareId.Value : "UNKNOWN",
73363600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
73463600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
735247648afSTakanori Watanabe 
736247648afSTakanori Watanabe     if (adinfo)
737247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
738247648afSTakanori Watanabe 
739247648afSTakanori Watanabe     return (0);
740247648afSTakanori Watanabe }
741247648afSTakanori Watanabe 
742247648afSTakanori Watanabe /*
74315e32d5dSMike Smith  * Handle per-device ivars
74415e32d5dSMike Smith  */
74515e32d5dSMike Smith static int
74615e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
74715e32d5dSMike Smith {
74815e32d5dSMike Smith     struct acpi_device	*ad;
74915e32d5dSMike Smith 
75015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
75115e32d5dSMike Smith 	printf("device has no ivars\n");
75215e32d5dSMike Smith 	return (ENOENT);
75315e32d5dSMike Smith     }
75415e32d5dSMike Smith 
755be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
75615e32d5dSMike Smith     switch(index) {
75715e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
75815e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
75915e32d5dSMike Smith 	break;
76015e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
76115e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
76215e32d5dSMike Smith 	break;
76315e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
76415e32d5dSMike Smith 	*(void **)result = ad->ad_private;
76515e32d5dSMike Smith 	break;
76632d18aa5SMike Smith     case ISA_IVAR_VENDORID:
76732d18aa5SMike Smith     case ISA_IVAR_SERIAL:
76832d18aa5SMike Smith     case ISA_IVAR_COMPATID:
76932d18aa5SMike Smith 	*(int *)result = -1;
77032d18aa5SMike Smith 	break;
77132d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
77232d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
77332d18aa5SMike Smith 	break;
77415e32d5dSMike Smith     default:
77515e32d5dSMike Smith 	return (ENOENT);
77615e32d5dSMike Smith     }
777be2b1797SNate Lawson 
77815e32d5dSMike Smith     return (0);
77915e32d5dSMike Smith }
78015e32d5dSMike Smith 
78115e32d5dSMike Smith static int
78215e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
78315e32d5dSMike Smith {
78415e32d5dSMike Smith     struct acpi_device	*ad;
78515e32d5dSMike Smith 
78615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
78715e32d5dSMike Smith 	printf("device has no ivars\n");
78815e32d5dSMike Smith 	return (ENOENT);
78915e32d5dSMike Smith     }
79015e32d5dSMike Smith 
79115e32d5dSMike Smith     switch(index) {
79215e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
79315e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
79415e32d5dSMike Smith 	break;
79515e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
79615e32d5dSMike Smith 	ad->ad_magic = (int)value;
79715e32d5dSMike Smith 	break;
79815e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
79915e32d5dSMike Smith 	ad->ad_private = (void *)value;
80015e32d5dSMike Smith 	break;
80115e32d5dSMike Smith     default:
8022ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
80315e32d5dSMike Smith 	return (ENOENT);
80415e32d5dSMike Smith     }
805be2b1797SNate Lawson 
80615e32d5dSMike Smith     return (0);
80715e32d5dSMike Smith }
80815e32d5dSMike Smith 
80915e32d5dSMike Smith /*
81015e32d5dSMike Smith  * Handle child resource allocation/removal
81115e32d5dSMike Smith  */
81215e32d5dSMike Smith static int
813be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid,
814be2b1797SNate Lawson 		  u_long start, u_long count)
81515e32d5dSMike Smith {
81615e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
81715e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
81815e32d5dSMike Smith 
81915e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
82015e32d5dSMike Smith 
82115e32d5dSMike Smith     return(0);
82215e32d5dSMike Smith }
82315e32d5dSMike Smith 
82415e32d5dSMike Smith static int
825be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid,
826be2b1797SNate Lawson 		  u_long *startp, u_long *countp)
82715e32d5dSMike Smith {
82815e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
82915e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
83015e32d5dSMike Smith     struct resource_list_entry	*rle;
83115e32d5dSMike Smith 
83215e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
83315e32d5dSMike Smith     if (!rle)
83415e32d5dSMike Smith 	return(ENOENT);
83515e32d5dSMike Smith 
83615e32d5dSMike Smith     if (startp)
83715e32d5dSMike Smith 	*startp = rle->start;
83815e32d5dSMike Smith     if (countp)
83915e32d5dSMike Smith 	*countp = rle->count;
84015e32d5dSMike Smith 
84115e32d5dSMike Smith     return (0);
84215e32d5dSMike Smith }
84315e32d5dSMike Smith 
84415e32d5dSMike Smith static struct resource *
84515e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
84615e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
84715e32d5dSMike Smith {
84815e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
84915e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
85015e32d5dSMike Smith 
851be2b1797SNate Lawson     return (resource_list_alloc(rl, bus, child, type, rid, start, end, count,
852be2b1797SNate Lawson 	    flags));
85315e32d5dSMike Smith }
85415e32d5dSMike Smith 
85515e32d5dSMike Smith static int
85615e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
85715e32d5dSMike Smith {
85815e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
85915e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
86015e32d5dSMike Smith 
86115e32d5dSMike Smith     return (resource_list_release(rl, bus, child, type, rid, r));
86215e32d5dSMike Smith }
86315e32d5dSMike Smith 
864dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
865dc750869SNate Lawson struct resource *
866dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas)
867dc750869SNate Lawson {
868dc750869SNate Lawson     int type;
869dc750869SNate Lawson 
870dc750869SNate Lawson     if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) ||
871dc750869SNate Lawson 	gas->RegisterBitWidth < 8)
872dc750869SNate Lawson 	return (NULL);
873dc750869SNate Lawson 
874dc750869SNate Lawson     switch (gas->AddressSpaceId) {
875dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
876dc750869SNate Lawson 	type = SYS_RES_MEMORY;
877dc750869SNate Lawson 	break;
878dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
879dc750869SNate Lawson 	type = SYS_RES_IOPORT;
880dc750869SNate Lawson 	break;
881dc750869SNate Lawson     default:
882dc750869SNate Lawson 	return (NULL);
883dc750869SNate Lawson     }
884dc750869SNate Lawson 
885dc750869SNate Lawson     bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8);
8865f96beb9SNate Lawson     return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE));
887dc750869SNate Lawson }
888dc750869SNate Lawson 
88915e32d5dSMike Smith /*
890bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
891bc0f2195SMike Smith  */
892bc0f2195SMike Smith #define PNP_EISAID(s)				\
893bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
894bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
895bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
896bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
897bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
898bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
899bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
900bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
901bc0f2195SMike Smith 
9021e4925e8SNate Lawson static uint32_t
90332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
904bc0f2195SMike Smith {
9051e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9061e4925e8SNate Lawson     ACPI_BUFFER		buf;
907bc0f2195SMike Smith     ACPI_HANDLE		h;
908bc0f2195SMike Smith     ACPI_STATUS		error;
909bc0f2195SMike Smith     u_int32_t		pnpid;
910fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
91132d18aa5SMike Smith 
912b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
91332d18aa5SMike Smith 
91432d18aa5SMike Smith     pnpid = 0;
915bd189fe7SNate Lawson     buf.Pointer = NULL;
916bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
917bd189fe7SNate Lawson 
91832d18aa5SMike Smith     ACPI_LOCK;
91932d18aa5SMike Smith 
9206fca9360SNate Lawson     /* Fetch and validate the HID. */
92132d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
922bd189fe7SNate Lawson 	goto out;
9236fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9246fca9360SNate Lawson     if (ACPI_FAILURE(error))
925bd189fe7SNate Lawson 	goto out;
9261e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
92732d18aa5SMike Smith 
9281e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
9291e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
930be2b1797SNate Lawson 
931bd189fe7SNate Lawson out:
932bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9331e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
93432d18aa5SMike Smith     ACPI_UNLOCK;
93532d18aa5SMike Smith     return_VALUE (pnpid);
93632d18aa5SMike Smith }
93732d18aa5SMike Smith 
9381e4925e8SNate Lawson static int
9391e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
940b2566207STakanori Watanabe {
9411e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
9421e4925e8SNate Lawson     ACPI_BUFFER		buf;
943b2566207STakanori Watanabe     ACPI_HANDLE		h;
944b2566207STakanori Watanabe     ACPI_STATUS		error;
9451e4925e8SNate Lawson     uint32_t		*pnpid;
9461e4925e8SNate Lawson     int			valid, i;
947b2566207STakanori Watanabe     ACPI_LOCK_DECL;
948b2566207STakanori Watanabe 
949b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
950b2566207STakanori Watanabe 
9511e4925e8SNate Lawson     pnpid = cids;
9521e4925e8SNate Lawson     valid = 0;
953bd189fe7SNate Lawson     buf.Pointer = NULL;
954bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
955bd189fe7SNate Lawson 
956b2566207STakanori Watanabe     ACPI_LOCK;
957b2566207STakanori Watanabe 
9581e4925e8SNate Lawson     /* Fetch and validate the CID */
959b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
960bd189fe7SNate Lawson 	goto out;
9611e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
9621e4925e8SNate Lawson     if (ACPI_FAILURE(error))
963bd189fe7SNate Lawson 	goto out;
9641e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
9651e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
966b2566207STakanori Watanabe 	goto out;
967b2566207STakanori Watanabe 
9681e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
9691e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
9701e4925e8SNate Lawson     for (i = 0; i < count; i++) {
9711e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
9721e4925e8SNate Lawson 	    continue;
9731e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
9741e4925e8SNate Lawson 	valid++;
975b2566207STakanori Watanabe     }
976b2566207STakanori Watanabe 
9771e4925e8SNate Lawson out:
978bd189fe7SNate Lawson     if (buf.Pointer != NULL)
9791e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
9801e4925e8SNate Lawson     ACPI_UNLOCK;
9811e4925e8SNate Lawson     return_VALUE (valid);
9821e4925e8SNate Lawson }
983b2566207STakanori Watanabe 
98432d18aa5SMike Smith static int
98532d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
98632d18aa5SMike Smith {
9871e4925e8SNate Lawson     int			result, cid_count, i;
9881e4925e8SNate Lawson     uint32_t		lid, cids[8];
989bc0f2195SMike Smith 
990b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
991bc0f2195SMike Smith 
992bc0f2195SMike Smith     /*
993bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
994bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
995bc0f2195SMike Smith      * that to happen, so don't ever return it.
996bc0f2195SMike Smith      */
997bc0f2195SMike Smith     result = ENXIO;
998bc0f2195SMike Smith 
999be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1000b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
10011e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1002bc0f2195SMike Smith     while (ids && ids->ip_id) {
10031e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1004bc0f2195SMike Smith 	    result = 0;
1005bc0f2195SMike Smith 	    goto out;
1006bc0f2195SMike Smith 	}
10071e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
10081e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
10091e4925e8SNate Lawson 		result = 0;
10101e4925e8SNate Lawson 		goto out;
10111e4925e8SNate Lawson 	    }
10121e4925e8SNate Lawson 	}
1013bc0f2195SMike Smith 	ids++;
1014bc0f2195SMike Smith     }
1015be2b1797SNate Lawson 
1016bc0f2195SMike Smith  out:
1017bc0f2195SMike Smith     return_VALUE (result);
1018bc0f2195SMike Smith }
1019bc0f2195SMike Smith 
1020bc0f2195SMike Smith /*
102115e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
102215e32d5dSMike Smith  *
1023be2b1797SNate Lawson  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and
1024be2b1797SNate Lawson  * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
102515e32d5dSMike Smith  */
102615e32d5dSMike Smith static void
102715e32d5dSMike Smith acpi_probe_children(device_t bus)
102815e32d5dSMike Smith {
102915e32d5dSMike Smith     ACPI_HANDLE	parent;
1030be2b1797SNate Lawson     ACPI_STATUS	status;
10317d3bcec9SMike Smith     static char	*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
103215e32d5dSMike Smith     int		i;
103315e32d5dSMike Smith 
1034b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1035cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
10360ae55423SMike Smith 
1037be2b1797SNate Lawson     /* Create any static children by calling device identify methods. */
10384c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
103915e32d5dSMike Smith     bus_generic_probe(bus);
104015e32d5dSMike Smith 
104115e32d5dSMike Smith     /*
104215e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
104315e32d5dSMike Smith      * we find.
104415e32d5dSMike Smith      *
104515e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1046be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1047be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1048be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
104915e32d5dSMike Smith      */
10504c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
1051be2b1797SNate Lawson     for (i = 0; scopes[i] != NULL; i++) {
1052be2b1797SNate Lawson 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent);
1053be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1054be2b1797SNate Lawson 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child,
1055be2b1797SNate Lawson 			      bus, NULL);
1056be2b1797SNate Lawson 	}
1057be2b1797SNate Lawson     }
105815e32d5dSMike Smith 
105915e32d5dSMike Smith     /*
106015e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
106115e32d5dSMike Smith      */
10624c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
106315e32d5dSMike Smith     bus_generic_attach(bus);
106415e32d5dSMike Smith 
106515e32d5dSMike Smith     /*
106615e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
106715e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
106815e32d5dSMike Smith      */
10694c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
107015e32d5dSMike Smith     bus_generic_attach(bus);
10710ae55423SMike Smith 
1072bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
10730ae55423SMike Smith     return_VOID;
107415e32d5dSMike Smith }
107515e32d5dSMike Smith 
107615e32d5dSMike Smith /*
107715e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
107815e32d5dSMike Smith  * it.
107915e32d5dSMike Smith  */
108015e32d5dSMike Smith static ACPI_STATUS
108115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
108215e32d5dSMike Smith {
108315e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
108415e32d5dSMike Smith     device_t		child, bus = (device_t)context;
108515e32d5dSMike Smith 
1086b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10870ae55423SMike Smith 
1088be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
10890ae55423SMike Smith     if (acpi_avoid(handle))
10900ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
10910ae55423SMike Smith 
1092b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
109315e32d5dSMike Smith 	switch(type) {
109415e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
109515e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
109615e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
109715e32d5dSMike Smith 	case ACPI_TYPE_POWER:
10980ae55423SMike Smith 	    if (acpi_disabled("children"))
10990ae55423SMike Smith 		break;
1100be2b1797SNate Lawson 
110115e32d5dSMike Smith 	    /*
110215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
110315e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
110415e32d5dSMike Smith 	     */
1105be2b1797SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n",
1106be2b1797SNate Lawson 			     acpi_name(handle)));
110715e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
1108bc0f2195SMike Smith 	    if (child == NULL)
1109bc0f2195SMike Smith 		break;
111015e32d5dSMike Smith 	    acpi_set_handle(child, handle);
1111bc0f2195SMike Smith 
1112bc0f2195SMike Smith 	    /*
1113b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1114b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1115b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1116b519f9d6SMike Smith 	     */
1117be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1118b519f9d6SMike Smith 		device_disable(child);
1119b519f9d6SMike Smith 		break;
1120b519f9d6SMike Smith 	    }
1121b519f9d6SMike Smith 
1122b519f9d6SMike Smith 	    /*
1123bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1124bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1125bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1126bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1127bc0f2195SMike Smith 	     * device not to have any resources.
1128bc0f2195SMike Smith 	     */
112972ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1130bc0f2195SMike Smith 
1131be2b1797SNate Lawson 	    /* If we're debugging, probe/attach now rather than later */
1132b53f2771SMike Smith 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
11330ae55423SMike Smith 	    break;
113415e32d5dSMike Smith 	}
113515e32d5dSMike Smith     }
1136be2b1797SNate Lawson 
11370ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
113815e32d5dSMike Smith }
113915e32d5dSMike Smith 
114015e32d5dSMike Smith static void
114115e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
114215e32d5dSMike Smith {
1143c6a78e98STakanori Watanabe     struct acpi_softc *sc = arg;
1144c6a78e98STakanori Watanabe 
1145cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1146cb9b0d80SMike Smith 
114715e32d5dSMike Smith     /*
11480ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
114915e32d5dSMike Smith      * will be turned on again on some laptops.
115015e32d5dSMike Smith      *
115115e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
115215e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
115315e32d5dSMike Smith      *     shutdown process.
115415e32d5dSMike Smith      */
1155c6a78e98STakanori Watanabe     if (sc->acpi_disable_on_poweroff)
1156c6a78e98STakanori Watanabe 	acpi_Disable(sc);
115715e32d5dSMike Smith }
115815e32d5dSMike Smith 
115915e32d5dSMike Smith static void
116015e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
116115e32d5dSMike Smith {
1162eeb3a05fSNate Lawson 
1163eeb3a05fSNate Lawson     ACPI_ASSERTLOCK;
1164eeb3a05fSNate Lawson 
1165eeb3a05fSNate Lawson     /*
1166eeb3a05fSNate Lawson      * If powering off, run the actual shutdown code on each processor.
1167eeb3a05fSNate Lawson      * It will only perform the shutdown on the BSP.  Some chipsets do
1168eeb3a05fSNate Lawson      * not power off the system correctly if called from an AP.
1169eeb3a05fSNate Lawson      */
1170eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1171904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1172904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1173904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1174904bf0c2SNate Lawson 		   AcpiFormatException(status));
1175904bf0c2SNate Lawson 	    return;
1176904bf0c2SNate Lawson 	}
1177eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
1178eeb3a05fSNate Lawson 	smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL);
1179eeb3a05fSNate Lawson     } else {
1180eeb3a05fSNate Lawson 	printf("Shutting down ACPI\n");
1181eeb3a05fSNate Lawson 	AcpiTerminate();
1182eeb3a05fSNate Lawson     }
1183eeb3a05fSNate Lawson }
1184eeb3a05fSNate Lawson 
1185904bf0c2SNate Lawson /*
1186904bf0c2SNate Lawson  * Since this function may be called with locks held or in an unknown
1187904bf0c2SNate Lawson  * context, it cannot allocate memory, acquire locks, sleep, etc.
1188904bf0c2SNate Lawson  */
1189eeb3a05fSNate Lawson static void
1190eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg)
1191eeb3a05fSNate Lawson {
119215e32d5dSMike Smith     ACPI_STATUS	status;
119315e32d5dSMike Smith 
1194cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1195cb9b0d80SMike Smith 
1196eeb3a05fSNate Lawson     /* Only attempt to power off if this is the BSP (cpuid 0). */
1197eeb3a05fSNate Lawson     if (PCPU_GET(cpuid) != 0)
1198eeb3a05fSNate Lawson 	return;
1199eeb3a05fSNate Lawson 
120055398047SNate Lawson     ACPI_DISABLE_IRQS();
1201865b8d0bSNate Lawson     status = AcpiEnterSleepState(ACPI_STATE_S5);
1202be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
1203bfae45aaSMike Smith 	printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
120415e32d5dSMike Smith     } else {
120515e32d5dSMike Smith 	DELAY(1000000);
120615e32d5dSMike Smith 	printf("ACPI power-off failed - timeout\n");
120715e32d5dSMike Smith     }
120815e32d5dSMike Smith }
120915e32d5dSMike Smith 
121013d4f7baSMitsuru IWASAKI static void
121113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
121213d4f7baSMitsuru IWASAKI {
121313d4f7baSMitsuru IWASAKI     static int	first_time = 1;
121413d4f7baSMitsuru IWASAKI 
1215cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1216cb9b0d80SMike Smith 
121713d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1218be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
121959ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
122013d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
122133febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1222be2b1797SNate Lawson 	if (first_time)
12236c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
122413d4f7baSMitsuru IWASAKI     }
1225be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
122659ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
122713d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
122833febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1229be2b1797SNate Lawson 	if (first_time)
12306c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
123113d4f7baSMitsuru IWASAKI     }
123213d4f7baSMitsuru IWASAKI 
123313d4f7baSMitsuru IWASAKI     first_time = 0;
123413d4f7baSMitsuru IWASAKI }
123513d4f7baSMitsuru IWASAKI 
123615e32d5dSMike Smith /*
1237c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1238c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1239c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1240c5ba0be4SMike Smith  */
1241c5ba0be4SMike Smith BOOLEAN
1242c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1243c5ba0be4SMike Smith {
12441e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1245c5ba0be4SMike Smith     ACPI_HANDLE		h;
12461e4925e8SNate Lawson     ACPI_BUFFER		buf;
1247c5ba0be4SMike Smith     ACPI_STATUS		error;
12481e4925e8SNate Lawson     int			ret;
1249c5ba0be4SMike Smith 
1250cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1251cb9b0d80SMike Smith 
12521e4925e8SNate Lawson     ret = FALSE;
1253c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1254c5ba0be4SMike Smith 	return (FALSE);
12551e4925e8SNate Lawson     buf.Pointer = NULL;
12561e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12576fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12586fca9360SNate Lawson     if (ACPI_FAILURE(error))
1259c5ba0be4SMike Smith 	return (FALSE);
12601e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1261be2b1797SNate Lawson 
1262be2b1797SNate Lawson     /* If no _STA method, must be present */
12631e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
12641e4925e8SNate Lawson 	ret = TRUE;
1265be2b1797SNate Lawson 
1266be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
12671e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x9) == 0x9)
12681e4925e8SNate Lawson 	ret = TRUE;
1269be2b1797SNate Lawson 
12701e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
12711e4925e8SNate Lawson     return (ret);
1272c5ba0be4SMike Smith }
1273c5ba0be4SMike Smith 
1274c5ba0be4SMike Smith /*
1275b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1276b53f2771SMike Smith  */
1277b53f2771SMike Smith BOOLEAN
1278b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1279b53f2771SMike Smith {
12801e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1281b53f2771SMike Smith     ACPI_HANDLE		h;
12821e4925e8SNate Lawson     ACPI_BUFFER		buf;
1283b53f2771SMike Smith     ACPI_STATUS		error;
12841e4925e8SNate Lawson     int			ret;
1285b53f2771SMike Smith 
1286b53f2771SMike Smith     ACPI_ASSERTLOCK;
1287b53f2771SMike Smith 
12881e4925e8SNate Lawson     ret = FALSE;
1289b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1290b53f2771SMike Smith 	return (FALSE);
12911e4925e8SNate Lawson     buf.Pointer = NULL;
12921e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
12936fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12946fca9360SNate Lawson     if (ACPI_FAILURE(error))
1295b53f2771SMike Smith 	return (FALSE);
12961e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1297be2b1797SNate Lawson 
1298be2b1797SNate Lawson     /* If no _STA method, must be present */
12991e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
13001e4925e8SNate Lawson 	ret = TRUE;
1301be2b1797SNate Lawson 
1302be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
13031e4925e8SNate Lawson     if ((devinfo->CurrentStatus & 0x19) == 0x19)
13041e4925e8SNate Lawson 	ret = TRUE;
1305be2b1797SNate Lawson 
13061e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13071e4925e8SNate Lawson     return (ret);
1308b53f2771SMike Smith }
1309b53f2771SMike Smith 
1310b53f2771SMike Smith /*
131115e32d5dSMike Smith  * Match a HID string against a device
131215e32d5dSMike Smith  */
131315e32d5dSMike Smith BOOLEAN
131415e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
131515e32d5dSMike Smith {
13161e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
131715e32d5dSMike Smith     ACPI_HANDLE		h;
13181e4925e8SNate Lawson     ACPI_BUFFER		buf;
131915e32d5dSMike Smith     ACPI_STATUS		error;
13201e4925e8SNate Lawson     int			ret, i;
132115e32d5dSMike Smith 
1322cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1323cb9b0d80SMike Smith 
13241e4925e8SNate Lawson     ret = FALSE;
13254d332989SMike Smith     if (hid == NULL)
132615e32d5dSMike Smith 	return (FALSE);
132715e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
132815e32d5dSMike Smith 	return (FALSE);
13291e4925e8SNate Lawson     buf.Pointer = NULL;
13301e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
13316fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
13326fca9360SNate Lawson     if (ACPI_FAILURE(error))
133315e32d5dSMike Smith 	return (FALSE);
13341e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1335be2b1797SNate Lawson 
1336c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1337c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
13381e4925e8SNate Lawson 	    ret = TRUE;
1339c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
13401e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
13411e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
13421e4925e8SNate Lawson 		ret = TRUE;
13431e4925e8SNate Lawson 		break;
13441e4925e8SNate Lawson 	    }
13451e4925e8SNate Lawson 	}
13461e4925e8SNate Lawson     }
1347be2b1797SNate Lawson 
13481e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
13491e4925e8SNate Lawson     return (ret);
135015e32d5dSMike Smith }
135115e32d5dSMike Smith 
135215e32d5dSMike Smith /*
1353c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1354c5ba0be4SMike Smith  * or one if its parents.
1355c5ba0be4SMike Smith  */
1356c5ba0be4SMike Smith ACPI_STATUS
1357c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1358c5ba0be4SMike Smith {
1359c5ba0be4SMike Smith     ACPI_HANDLE		r;
1360c5ba0be4SMike Smith     ACPI_STATUS		status;
1361c5ba0be4SMike Smith 
1362cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1363cb9b0d80SMike Smith 
1364be2b1797SNate Lawson     /* Walk back up the tree to the root */
1365c5ba0be4SMike Smith     for (;;) {
1366be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1367be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1368c5ba0be4SMike Smith 	    *result = r;
1369c5ba0be4SMike Smith 	    return (AE_OK);
1370c5ba0be4SMike Smith 	}
1371c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1372c5ba0be4SMike Smith 	    return (AE_OK);
1373b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1374c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1375c5ba0be4SMike Smith 	parent = r;
1376c5ba0be4SMike Smith     }
1377c5ba0be4SMike Smith }
1378c5ba0be4SMike Smith 
1379eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1380eea17c34SNate Lawson uint32_t
1381eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1382eea17c34SNate Lawson {
1383eea17c34SNate Lawson     uint32_t delta;
1384eea17c34SNate Lawson 
1385eea17c34SNate Lawson     if (end >= start)
1386eea17c34SNate Lawson 	delta = end - start;
1387eea17c34SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
13888d01ceefSNate Lawson 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1389eea17c34SNate Lawson     else
1390eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
1391eea17c34SNate Lawson     return (delta);
1392eea17c34SNate Lawson }
1393eea17c34SNate Lawson 
1394c5ba0be4SMike Smith /*
1395c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1396c5ba0be4SMike Smith  */
1397c5ba0be4SMike Smith ACPI_BUFFER *
1398c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1399c5ba0be4SMike Smith {
1400c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1401c5ba0be4SMike Smith 
1402c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1403c5ba0be4SMike Smith 	return (NULL);
1404c5ba0be4SMike Smith     buf->Length = size;
1405c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1406c5ba0be4SMike Smith     return (buf);
1407c5ba0be4SMike Smith }
1408c5ba0be4SMike Smith 
1409c310653eSNate Lawson ACPI_STATUS
1410cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1411c310653eSNate Lawson {
1412c310653eSNate Lawson     ACPI_OBJECT arg1;
1413c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1414c310653eSNate Lawson 
1415c310653eSNate Lawson     ACPI_ASSERTLOCK;
1416c310653eSNate Lawson 
1417c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1418c310653eSNate Lawson     arg1.Integer.Value = number;
1419c310653eSNate Lawson     args.Count = 1;
1420c310653eSNate Lawson     args.Pointer = &arg1;
1421c310653eSNate Lawson 
1422c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1423c310653eSNate Lawson }
1424c310653eSNate Lawson 
1425c5ba0be4SMike Smith /*
1426c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1427c5ba0be4SMike Smith  */
1428c5ba0be4SMike Smith ACPI_STATUS
1429cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1430c5ba0be4SMike Smith {
1431be2b1797SNate Lawson     ACPI_STATUS	status;
1432c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1433c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1434c5ba0be4SMike Smith 
1435cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1436cb9b0d80SMike Smith 
1437c5ba0be4SMike Smith     if (handle == NULL)
1438c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
14390c7da7acSJohn Baldwin 
14400c7da7acSJohn Baldwin     /*
14410c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
14420c7da7acSJohn Baldwin      * a method that will return an Integer.
14430c7da7acSJohn Baldwin      */
1444c5ba0be4SMike Smith     buf.Pointer = &param;
1445c5ba0be4SMike Smith     buf.Length = sizeof(param);
1446be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1447be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1448be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1449c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1450be2b1797SNate Lawson 	else
1451be2b1797SNate Lawson 	    status = AE_TYPE;
1452c5ba0be4SMike Smith     }
14530c7da7acSJohn Baldwin 
14540c7da7acSJohn Baldwin     /*
14550c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
14560c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
14570c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
14580c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
14590c7da7acSJohn Baldwin      *
14600c7da7acSJohn Baldwin      * This is a hack.
14610c7da7acSJohn Baldwin      */
1462be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1463b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1464be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
14650c7da7acSJohn Baldwin 	} else {
1466be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1467be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1468be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
14690c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
14700c7da7acSJohn Baldwin 	}
1471f97739daSNate Lawson     }
1472be2b1797SNate Lawson     return (status);
1473c5ba0be4SMike Smith }
1474c5ba0be4SMike Smith 
1475c573e654SMitsuru IWASAKI ACPI_STATUS
1476cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1477c573e654SMitsuru IWASAKI {
1478c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1479dba55fa2SNate Lawson     UINT8	*val;
1480c573e654SMitsuru IWASAKI     int		i;
1481c573e654SMitsuru IWASAKI 
1482c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1483c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1484c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1485c573e654SMitsuru IWASAKI 	return (AE_OK);
1486c573e654SMitsuru IWASAKI     }
1487c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1488c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1489c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1490c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1491be2b1797SNate Lawson 
1492c573e654SMitsuru IWASAKI     *number = 0;
1493dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1494c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1495dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1496c573e654SMitsuru IWASAKI     return (AE_OK);
1497c573e654SMitsuru IWASAKI }
1498c573e654SMitsuru IWASAKI 
1499c5ba0be4SMike Smith /*
1500c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1501c5ba0be4SMike Smith  * function for each element.
1502c5ba0be4SMike Smith  *
1503c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1504c5ba0be4SMike Smith  */
1505c5ba0be4SMike Smith ACPI_STATUS
1506be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1507be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1508c5ba0be4SMike Smith {
1509c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1510c5ba0be4SMike Smith     int		i;
1511c5ba0be4SMike Smith 
1512be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1513c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1514c5ba0be4SMike Smith 
1515be2b1797SNate Lawson     /* Iterate over components */
1516be2b1797SNate Lawson     i = 0;
1517be2b1797SNate Lawson     comp = pkg->Package.Elements;
1518be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1519c5ba0be4SMike Smith 	func(comp, arg);
1520c5ba0be4SMike Smith 
1521c5ba0be4SMike Smith     return (AE_OK);
1522c5ba0be4SMike Smith }
1523c5ba0be4SMike Smith 
15246f69255bSMike Smith /*
15256f69255bSMike Smith  * Find the (index)th resource object in a set.
15266f69255bSMike Smith  */
15276f69255bSMike Smith ACPI_STATUS
15286d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
15296f69255bSMike Smith {
15306d63101aSMike Smith     ACPI_RESOURCE	*rp;
15316f69255bSMike Smith     int			i;
15326f69255bSMike Smith 
15336d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15346f69255bSMike Smith     i = index;
15356d63101aSMike Smith     while (i-- > 0) {
1536be2b1797SNate Lawson 	/* Range check */
15376d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15386f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1539be2b1797SNate Lawson 
1540be2b1797SNate Lawson 	/* Check for terminator */
1541be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15426d63101aSMike Smith 	    return (AE_NOT_FOUND);
1543abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15446f69255bSMike Smith     }
15456f69255bSMike Smith     if (resp != NULL)
15466d63101aSMike Smith 	*resp = rp;
1547be2b1797SNate Lawson 
15486d63101aSMike Smith     return (AE_OK);
15496d63101aSMike Smith }
15506d63101aSMike Smith 
15516d63101aSMike Smith /*
15526d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
15536d63101aSMike Smith  *
15546d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
15556d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
15566d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
15576d63101aSMike Smith  * resources.
15586d63101aSMike Smith  */
15596d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
15606d63101aSMike Smith 
15616d63101aSMike Smith ACPI_STATUS
15626d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
15636d63101aSMike Smith {
15646d63101aSMike Smith     ACPI_RESOURCE	*rp;
15656d63101aSMike Smith     void		*newp;
15666d63101aSMike Smith 
1567be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
15686d63101aSMike Smith     if (buf->Pointer == NULL) {
15696d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
15706d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
15716d63101aSMike Smith 	    return (AE_NO_MEMORY);
15726d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
15736d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
15746d63101aSMike Smith 	rp->Length = 0;
15756d63101aSMike Smith     }
15766d63101aSMike Smith     if (res == NULL)
15776d63101aSMike Smith 	return (AE_OK);
15786d63101aSMike Smith 
15796d63101aSMike Smith     /*
15806d63101aSMike Smith      * Scan the current buffer looking for the terminator.
15816d63101aSMike Smith      * This will either find the terminator or hit the end
15826d63101aSMike Smith      * of the buffer and return an error.
15836d63101aSMike Smith      */
15846d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
15856d63101aSMike Smith     for (;;) {
1586be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
15876d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
15886d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1589be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
15906d63101aSMike Smith 	    break;
1591abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
15926d63101aSMike Smith     }
15936d63101aSMike Smith 
15946d63101aSMike Smith     /*
15956d63101aSMike Smith      * Check the size of the buffer and expand if required.
15966d63101aSMike Smith      *
15976d63101aSMike Smith      * Required size is:
15986d63101aSMike Smith      *	size of existing resources before terminator +
15996d63101aSMike Smith      *	size of new resource and header +
16006d63101aSMike Smith      * 	size of terminator.
16016d63101aSMike Smith      *
16026d63101aSMike Smith      * Note that this loop should really only run once, unless
16036d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
16046d63101aSMike Smith      */
16056d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
16066d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
16076d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
16086d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
16096d63101aSMike Smith 	    return (AE_NO_MEMORY);
16106d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1611a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1612a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
16136d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
16146d63101aSMike Smith 	buf->Pointer = newp;
16156d63101aSMike Smith 	buf->Length += buf->Length;
16166d63101aSMike Smith     }
16176d63101aSMike Smith 
1618be2b1797SNate Lawson     /* Insert the new resource. */
16196d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
16206d63101aSMike Smith 
1621be2b1797SNate Lawson     /* And add the terminator. */
1622abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
16236d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
16246d63101aSMike Smith     rp->Length = 0;
16256d63101aSMike Smith 
16266f69255bSMike Smith     return (AE_OK);
16276f69255bSMike Smith }
1628c5ba0be4SMike Smith 
1629da14ac9fSJohn Baldwin /*
1630da14ac9fSJohn Baldwin  * Set interrupt model.
1631da14ac9fSJohn Baldwin  */
1632da14ac9fSJohn Baldwin ACPI_STATUS
1633da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
1634da14ac9fSJohn Baldwin {
1635da14ac9fSJohn Baldwin 
1636c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
1637da14ac9fSJohn Baldwin }
1638da14ac9fSJohn Baldwin 
1639ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME	5
1640ece50487SMitsuru IWASAKI 
1641ece50487SMitsuru IWASAKI static void
1642ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
1643ece50487SMitsuru IWASAKI {
1644ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
1645ece50487SMitsuru IWASAKI }
1646c30382dfSMitsuru IWASAKI 
164715e32d5dSMike Smith /*
164815e32d5dSMike Smith  * Set the system sleep state
164915e32d5dSMike Smith  *
1650be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
165115e32d5dSMike Smith  */
165215e32d5dSMike Smith ACPI_STATUS
165315e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
165415e32d5dSMike Smith {
165515e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
165656d8cb57SMitsuru IWASAKI     UINT8	TypeA;
165756d8cb57SMitsuru IWASAKI     UINT8	TypeB;
165815e32d5dSMike Smith 
1659b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
1660cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
16610ae55423SMike Smith 
166298175614SNate Lawson     /* Avoid reentry if already attempting to suspend. */
16636397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
166498175614SNate Lawson 	return_ACPI_STATUS (AE_BAD_PARAMETER);
16656397624dSMitsuru IWASAKI 
166698175614SNate Lawson     /* We recently woke up so don't suspend again for a while. */
1667ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1668ece50487SMitsuru IWASAKI 	return_ACPI_STATUS (AE_OK);
1669ece50487SMitsuru IWASAKI 
167015e32d5dSMike Smith     switch (state) {
167115e32d5dSMike Smith     case ACPI_STATE_S1:
16726161544cSTakanori Watanabe     case ACPI_STATE_S2:
16736161544cSTakanori Watanabe     case ACPI_STATE_S3:
16746161544cSTakanori Watanabe     case ACPI_STATE_S4:
1675be2b1797SNate Lawson 	status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB);
167698175614SNate Lawson 	if (status == AE_NOT_FOUND) {
167798175614SNate Lawson 	    device_printf(sc->acpi_dev,
167898175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
167998175614SNate Lawson 	    break;
168098175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
1681be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
1682be2b1797SNate Lawson 			  AcpiFormatException(status));
168356d8cb57SMitsuru IWASAKI 	    break;
168456d8cb57SMitsuru IWASAKI 	}
168556d8cb57SMitsuru IWASAKI 
1686a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
1687a1fccb47SMitsuru IWASAKI 	sc->acpi_sleep_disabled = 1;
1688a1fccb47SMitsuru IWASAKI 
1689be2b1797SNate Lawson 	/* Inform all devices that we are going to sleep. */
169015e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
169115e32d5dSMike Smith 	    /*
169215e32d5dSMike Smith 	     * Re-wake the system.
169315e32d5dSMike Smith 	     *
169415e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
169515e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
169615e32d5dSMike Smith 	     *     current bus interface does not provide for this.
169715e32d5dSMike Smith 	     */
169815e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
16990ae55423SMike Smith 	    return_ACPI_STATUS (AE_ERROR);
170015e32d5dSMike Smith 	}
1701b9c780d6SMitsuru IWASAKI 
1702be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
1703be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1704b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1705b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1706b9c780d6SMitsuru IWASAKI 	    break;
1707b9c780d6SMitsuru IWASAKI 	}
1708b9c780d6SMitsuru IWASAKI 
1709be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
1710ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
1711ff01efb5SMitsuru IWASAKI 
17126161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
17136161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
17146161544cSTakanori Watanabe 
1715be2b1797SNate Lawson 	    /* AcpiEnterSleepState() may be incomplete, unlock if locked. */
171659ddeb18SNate Lawson 	    if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId !=
171759ddeb18SNate Lawson 		ACPI_MUTEX_NOT_ACQUIRED) {
171859ddeb18SNate Lawson 
17196161544cSTakanori Watanabe 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
1720a1fccb47SMitsuru IWASAKI 	    }
17216161544cSTakanori Watanabe 
17226161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1723be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
1724964679ceSMitsuru IWASAKI 		AcpiEnable();
17256161544cSTakanori Watanabe 	} else {
1726be2b1797SNate Lawson 	    status = AcpiEnterSleepState((UINT8)state);
1727be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
1728be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
1729be2b1797SNate Lawson 			      AcpiFormatException(status));
1730c30382dfSMitsuru IWASAKI 		break;
173115e32d5dSMike Smith 	    }
17326161544cSTakanori Watanabe 	}
1733ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
173415e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
173515e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
173613d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
173715e32d5dSMike Smith 	break;
173815e32d5dSMike Smith     case ACPI_STATE_S5:
173915e32d5dSMike Smith 	/*
174015e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
174115e32d5dSMike Smith 	 * shutdown handlers.
174215e32d5dSMike Smith 	 */
174315e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
174415e32d5dSMike Smith 	break;
174598175614SNate Lawson     case ACPI_STATE_S0:
174615e32d5dSMike Smith     default:
174715e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
174815e32d5dSMike Smith 	break;
174915e32d5dSMike Smith     }
1750ece50487SMitsuru IWASAKI 
175198175614SNate Lawson     /* Disable a second sleep request for a short period */
1752ece50487SMitsuru IWASAKI     if (sc->acpi_sleep_disabled)
1753ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
1754ece50487SMitsuru IWASAKI 
17550ae55423SMike Smith     return_ACPI_STATUS (status);
175615e32d5dSMike Smith }
175715e32d5dSMike Smith 
175815e32d5dSMike Smith /*
175915e32d5dSMike Smith  * Enable/Disable ACPI
176015e32d5dSMike Smith  */
176115e32d5dSMike Smith ACPI_STATUS
176215e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
176315e32d5dSMike Smith {
176415e32d5dSMike Smith     ACPI_STATUS	status;
176515e32d5dSMike Smith     u_int32_t	flags;
176615e32d5dSMike Smith 
1767b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1768cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17690ae55423SMike Smith 
177015e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
177115e32d5dSMike Smith 	    ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
1772be2b1797SNate Lawson     if (!sc->acpi_enabled)
177315e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
1774be2b1797SNate Lawson     else
177515e32d5dSMike Smith 	status = AE_OK;
1776be2b1797SNate Lawson 
177715e32d5dSMike Smith     if (status == AE_OK)
177815e32d5dSMike Smith 	sc->acpi_enabled = 1;
1779be2b1797SNate Lawson 
17800ae55423SMike Smith     return_ACPI_STATUS (status);
178115e32d5dSMike Smith }
178215e32d5dSMike Smith 
178315e32d5dSMike Smith ACPI_STATUS
178415e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
178515e32d5dSMike Smith {
178615e32d5dSMike Smith     ACPI_STATUS	status;
178715e32d5dSMike Smith 
1788b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1789cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
17900ae55423SMike Smith 
1791be2b1797SNate Lawson     if (sc->acpi_enabled)
179215e32d5dSMike Smith 	status = AcpiDisable();
1793be2b1797SNate Lawson     else
179415e32d5dSMike Smith 	status = AE_OK;
1795be2b1797SNate Lawson 
179615e32d5dSMike Smith     if (status == AE_OK)
179715e32d5dSMike Smith 	sc->acpi_enabled = 0;
1798be2b1797SNate Lawson 
17990ae55423SMike Smith     return_ACPI_STATUS (status);
180015e32d5dSMike Smith }
180115e32d5dSMike Smith 
180215e32d5dSMike Smith /*
180315e32d5dSMike Smith  * ACPI Event Handlers
180415e32d5dSMike Smith  */
180515e32d5dSMike Smith 
180615e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
180715e32d5dSMike Smith 
180815e32d5dSMike Smith static void
180915e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
181015e32d5dSMike Smith {
1811fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1812b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
181315e32d5dSMike Smith 
1814cb9b0d80SMike Smith     ACPI_LOCK;
1815a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
181615e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1817cb9b0d80SMike Smith     ACPI_UNLOCK;
18180ae55423SMike Smith     return_VOID;
181915e32d5dSMike Smith }
182015e32d5dSMike Smith 
182115e32d5dSMike Smith static void
182215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
182315e32d5dSMike Smith {
1824fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1825b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
18260ae55423SMike Smith 
182715e32d5dSMike Smith     /* Well, what to do? :-) */
18280ae55423SMike Smith 
1829cb9b0d80SMike Smith     ACPI_LOCK;
1830cb9b0d80SMike Smith     ACPI_UNLOCK;
1831cb9b0d80SMike Smith 
18320ae55423SMike Smith     return_VOID;
183315e32d5dSMike Smith }
183415e32d5dSMike Smith 
183515e32d5dSMike Smith /*
183615e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
183715e32d5dSMike Smith  */
183815e32d5dSMike Smith UINT32
183933febf93SNate Lawson acpi_event_power_button_sleep(void *context)
184015e32d5dSMike Smith {
184115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
184215e32d5dSMike Smith 
1843b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18440ae55423SMike Smith 
184515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
18460ae55423SMike Smith 
1847b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
184815e32d5dSMike Smith }
184915e32d5dSMike Smith 
185015e32d5dSMike Smith UINT32
185133febf93SNate Lawson acpi_event_power_button_wake(void *context)
185215e32d5dSMike Smith {
185315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
185415e32d5dSMike Smith 
1855b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18560ae55423SMike Smith 
185715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
18580ae55423SMike Smith 
1859b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
186015e32d5dSMike Smith }
186115e32d5dSMike Smith 
186215e32d5dSMike Smith UINT32
186333febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
186415e32d5dSMike Smith {
186515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
186615e32d5dSMike Smith 
1867b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18680ae55423SMike Smith 
186915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
18700ae55423SMike Smith 
1871b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
187215e32d5dSMike Smith }
187315e32d5dSMike Smith 
187415e32d5dSMike Smith UINT32
187533febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
187615e32d5dSMike Smith {
187715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
187815e32d5dSMike Smith 
1879b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18800ae55423SMike Smith 
188115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
18820ae55423SMike Smith 
1883b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
188415e32d5dSMike Smith }
188515e32d5dSMike Smith 
188615e32d5dSMike Smith /*
188715e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
188815e32d5dSMike Smith  */
188915e32d5dSMike Smith struct acpi_staticbuf {
189015e32d5dSMike Smith     ACPI_BUFFER	buffer;
189115e32d5dSMike Smith     char	data[512];
189215e32d5dSMike Smith };
189315e32d5dSMike Smith 
189415e32d5dSMike Smith char *
189515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
189615e32d5dSMike Smith {
189715e32d5dSMike Smith     static struct acpi_staticbuf	buf;
189815e32d5dSMike Smith 
1899cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1900cb9b0d80SMike Smith 
190115e32d5dSMike Smith     buf.buffer.Length = 512;
190215e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
190315e32d5dSMike Smith 
1904b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
190515e32d5dSMike Smith 	return (buf.buffer.Pointer);
1906be2b1797SNate Lawson 
190715e32d5dSMike Smith     return ("(unknown path)");
190815e32d5dSMike Smith }
190915e32d5dSMike Smith 
191015e32d5dSMike Smith /*
191115e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
191215e32d5dSMike Smith  * parts of the namespace.
191315e32d5dSMike Smith  */
191415e32d5dSMike Smith int
191515e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
191615e32d5dSMike Smith {
19173c600cfbSJohn Baldwin     char	*cp, *env, *np;
191815e32d5dSMike Smith     int		len;
191915e32d5dSMike Smith 
192015e32d5dSMike Smith     np = acpi_name(handle);
192115e32d5dSMike Smith     if (*np == '\\')
192215e32d5dSMike Smith 	np++;
19233c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
192415e32d5dSMike Smith 	return (0);
192515e32d5dSMike Smith 
1926be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
19273c600cfbSJohn Baldwin     cp = env;
192815e32d5dSMike Smith     for (;;) {
192915e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
193015e32d5dSMike Smith 	    cp++;
193115e32d5dSMike Smith 	if (*cp == 0)
193215e32d5dSMike Smith 	    break;
193315e32d5dSMike Smith 	len = 0;
193415e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
193515e32d5dSMike Smith 	    len++;
1936d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
19373c600cfbSJohn Baldwin 	    freeenv(env);
19380ae55423SMike Smith 	    return(1);
1939d786139cSMaxime Henrion 	}
19400ae55423SMike Smith 	cp += len;
19410ae55423SMike Smith     }
19423c600cfbSJohn Baldwin     freeenv(env);
1943be2b1797SNate Lawson 
19440ae55423SMike Smith     return (0);
19450ae55423SMike Smith }
19460ae55423SMike Smith 
19470ae55423SMike Smith /*
19480ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
19490ae55423SMike Smith  */
19500ae55423SMike Smith int
19510ae55423SMike Smith acpi_disabled(char *subsys)
19520ae55423SMike Smith {
195378689b15SMaxime Henrion     char	*cp, *env;
19540ae55423SMike Smith     int		len;
19550ae55423SMike Smith 
19563184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
19570ae55423SMike Smith 	return (0);
19583184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
195978689b15SMaxime Henrion 	freeenv(env);
19600ae55423SMike Smith 	return (1);
1961d786139cSMaxime Henrion     }
19620ae55423SMike Smith 
19633184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
196478689b15SMaxime Henrion     cp = env;
19650ae55423SMike Smith     for (;;) {
19663184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
19670ae55423SMike Smith 	    cp++;
19683184cf5aSNate Lawson 	if (*cp == '\0')
19690ae55423SMike Smith 	    break;
19700ae55423SMike Smith 	len = 0;
19713184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
19720ae55423SMike Smith 	    len++;
19733184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
197478689b15SMaxime Henrion 	    freeenv(env);
197515e32d5dSMike Smith 	    return (1);
1976d786139cSMaxime Henrion 	}
197715e32d5dSMike Smith 	cp += len;
197815e32d5dSMike Smith     }
197978689b15SMaxime Henrion     freeenv(env);
1980be2b1797SNate Lawson 
198115e32d5dSMike Smith     return (0);
198215e32d5dSMike Smith }
198315e32d5dSMike Smith 
198415e32d5dSMike Smith /*
1985a1fccb47SMitsuru IWASAKI  * Device wake capability enable/disable.
1986a1fccb47SMitsuru IWASAKI  */
1987a1fccb47SMitsuru IWASAKI void
1988a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
1989a1fccb47SMitsuru IWASAKI {
1990a1fccb47SMitsuru IWASAKI     /*
1991a1fccb47SMitsuru IWASAKI      * TBD: All Power Resources referenced by elements 2 through N
1992a1fccb47SMitsuru IWASAKI      *      of the _PRW object are put into the ON state.
1993a1fccb47SMitsuru IWASAKI      */
1994a1fccb47SMitsuru IWASAKI 
1995c310653eSNate Lawson     (void)acpi_SetInteger(h, "_PSW", enable);
1996a1fccb47SMitsuru IWASAKI }
1997a1fccb47SMitsuru IWASAKI 
1998a1fccb47SMitsuru IWASAKI void
1999a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h)
2000a1fccb47SMitsuru IWASAKI {
2001a1fccb47SMitsuru IWASAKI     struct acpi_softc		*sc;
2002074a57f5SNate Lawson     uint32_t			gpe_bit, lowest_wake;
2003074a57f5SNate Lawson     ACPI_HANDLE			handle;
2004a1fccb47SMitsuru IWASAKI     ACPI_STATUS			status;
2005a1fccb47SMitsuru IWASAKI     ACPI_BUFFER			prw_buffer;
2006074a57f5SNate Lawson     ACPI_OBJECT			*res, *res2;
2007a1fccb47SMitsuru IWASAKI 
2008a1fccb47SMitsuru IWASAKI     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2009a1fccb47SMitsuru IWASAKI 
2010be2b1797SNate Lawson     sc = devclass_get_softc(acpi_devclass, 0);
2011be2b1797SNate Lawson     if (sc == NULL)
2012a1fccb47SMitsuru IWASAKI 	return;
2013a1fccb47SMitsuru IWASAKI 
2014a1fccb47SMitsuru IWASAKI     /*
2015074a57f5SNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2016074a57f5SNate Lawson      * ability to wake the system from a sleeping state.
2017a1fccb47SMitsuru IWASAKI      */
2018074a57f5SNate Lawson     prw_buffer.Pointer = NULL;
2019a1fccb47SMitsuru IWASAKI     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2020a1fccb47SMitsuru IWASAKI     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2021be2b1797SNate Lawson     if (ACPI_FAILURE(status))
2022a1fccb47SMitsuru IWASAKI 	return;
2023a1fccb47SMitsuru IWASAKI     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2024be2b1797SNate Lawson     if (res == NULL)
2025a1fccb47SMitsuru IWASAKI 	return;
2026074a57f5SNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2027a1fccb47SMitsuru IWASAKI 	goto out;
2028a1fccb47SMitsuru IWASAKI 
2029a1fccb47SMitsuru IWASAKI     /*
2030074a57f5SNate Lawson      * Element 1 of the _PRW object:
2031074a57f5SNate Lawson      * The lowest power system sleeping state that can be entered while still
2032074a57f5SNate Lawson      * providing wake functionality.  The sleeping state being entered must
2033074a57f5SNate Lawson      * be less than (i.e., higher power) or equal to this value.
2034a1fccb47SMitsuru IWASAKI      */
2035074a57f5SNate Lawson     if (acpi_PkgInt32(res, 1, &lowest_wake) != 0)
2036a1fccb47SMitsuru IWASAKI 	goto out;
2037074a57f5SNate Lawson     if (sc->acpi_sstate > lowest_wake)
2038a1fccb47SMitsuru IWASAKI 	goto out;
2039a1fccb47SMitsuru IWASAKI 
2040a1fccb47SMitsuru IWASAKI     /*
2041074a57f5SNate Lawson      * Element 0 of the _PRW object:
2042a1fccb47SMitsuru IWASAKI      */
2043a1fccb47SMitsuru IWASAKI     switch (res->Package.Elements[0].Type) {
2044a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_INTEGER:
2045a1fccb47SMitsuru IWASAKI 	/*
2046a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is numeric, then this
2047a1fccb47SMitsuru IWASAKI 	 * _PRW package element is the bit index in the GPEx_EN, in the
2048a1fccb47SMitsuru IWASAKI 	 * GPE blocks described in the FADT, of the enable bit that is
2049a1fccb47SMitsuru IWASAKI 	 * enabled for the wake event.
2050a1fccb47SMitsuru IWASAKI 	 */
2051074a57f5SNate Lawson 	gpe_bit = res->Package.Elements[0].Integer.Value;
2052074a57f5SNate Lawson 	status = AcpiEnableGpe(NULL, gpe_bit, ACPI_EVENT_WAKE_ENABLE);
2053a1fccb47SMitsuru IWASAKI 	if (ACPI_FAILURE(status))
2054074a57f5SNate Lawson 	    printf("wake enable: AcpiEnableGpe failed for %u\n",
2055074a57f5SNate Lawson 		   gpe_bit);
2056a1fccb47SMitsuru IWASAKI 	break;
2057a1fccb47SMitsuru IWASAKI     case ACPI_TYPE_PACKAGE:
2058a1fccb47SMitsuru IWASAKI 	/*
2059a1fccb47SMitsuru IWASAKI 	 * If the data type of this package element is a package, then this
2060a1fccb47SMitsuru IWASAKI 	 * _PRW package element is itself a package containing two
2061a1fccb47SMitsuru IWASAKI 	 * elements.  The first is an object reference to the GPE Block
2062a1fccb47SMitsuru IWASAKI 	 * device that contains the GPE that will be triggered by the wake
2063a1fccb47SMitsuru IWASAKI 	 * event.  The second element is numeric and it contains the bit
2064a1fccb47SMitsuru IWASAKI 	 * index in the GPEx_EN, in the GPE Block referenced by the
2065a1fccb47SMitsuru IWASAKI 	 * first element in the package, of the enable bit that is enabled for
2066a1fccb47SMitsuru IWASAKI 	 * the wake event.
2067074a57f5SNate Lawson 	 *
2068a1fccb47SMitsuru IWASAKI 	 * For example, if this field is a package then it is of the form:
2069a1fccb47SMitsuru IWASAKI 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2070a1fccb47SMitsuru IWASAKI 	 */
2071074a57f5SNate Lawson 	res2 = &res->Package.Elements[0];
2072074a57f5SNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2073074a57f5SNate Lawson 	    goto out;
2074074a57f5SNate Lawson 	handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2075074a57f5SNate Lawson 	if (handle == NULL || acpi_PkgInt32(res2, 1, &gpe_bit) != 0)
2076074a57f5SNate Lawson 	    goto out;
2077074a57f5SNate Lawson 	status = AcpiEnableGpe(handle, gpe_bit, ACPI_EVENT_WAKE_ENABLE);
2078074a57f5SNate Lawson 	if (ACPI_FAILURE(status))
2079074a57f5SNate Lawson 	    printf("wake enable: AcpiEnableGpe (package) failed for %u\n",
2080074a57f5SNate Lawson 		   gpe_bit);
2081a1fccb47SMitsuru IWASAKI 	break;
2082a1fccb47SMitsuru IWASAKI     default:
2083a1fccb47SMitsuru IWASAKI 	break;
2084a1fccb47SMitsuru IWASAKI     }
2085a1fccb47SMitsuru IWASAKI 
2086a1fccb47SMitsuru IWASAKI out:
2087a1fccb47SMitsuru IWASAKI     if (prw_buffer.Pointer != NULL)
2088a1fccb47SMitsuru IWASAKI 	AcpiOsFree(prw_buffer.Pointer);
2089a1fccb47SMitsuru IWASAKI }
2090a1fccb47SMitsuru IWASAKI 
2091a1fccb47SMitsuru IWASAKI /*
209215e32d5dSMike Smith  * Control interface.
209315e32d5dSMike Smith  *
20940ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2095be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2096be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
209715e32d5dSMike Smith  */
20980ae55423SMike Smith struct acpi_ioctl_hook
20990ae55423SMike Smith {
21000ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
21010ae55423SMike Smith     u_long			 cmd;
2102be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
21030ae55423SMike Smith     void			 *arg;
21040ae55423SMike Smith };
21050ae55423SMike Smith 
21060ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
21070ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
21080ae55423SMike Smith 
21090ae55423SMike Smith /*
21100ae55423SMike Smith  * Register an ioctl handler.
21110ae55423SMike Smith  */
21120ae55423SMike Smith int
2113be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
21140ae55423SMike Smith {
21150ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21160ae55423SMike Smith 
21170ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
21180ae55423SMike Smith 	return (ENOMEM);
21190ae55423SMike Smith     hp->cmd = cmd;
21200ae55423SMike Smith     hp->fn = fn;
21210ae55423SMike Smith     hp->arg = arg;
21220ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
21230ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
21240ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
21250ae55423SMike Smith     }
21260ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
21270ae55423SMike Smith     return (0);
21280ae55423SMike Smith }
21290ae55423SMike Smith 
21300ae55423SMike Smith /*
21310ae55423SMike Smith  * Deregister an ioctl handler.
21320ae55423SMike Smith  */
21330ae55423SMike Smith void
2134be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
21350ae55423SMike Smith {
21360ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21370ae55423SMike Smith 
21380ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
21390ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
21400ae55423SMike Smith 	    break;
21410ae55423SMike Smith 
21420ae55423SMike Smith     if (hp != NULL) {
21430ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
21440ae55423SMike Smith 	free(hp, M_ACPIDEV);
21450ae55423SMike Smith     }
21460ae55423SMike Smith }
21470ae55423SMike Smith 
214815e32d5dSMike Smith static int
21496b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
215015e32d5dSMike Smith {
215115e32d5dSMike Smith     return (0);
215215e32d5dSMike Smith }
215315e32d5dSMike Smith 
215415e32d5dSMike Smith static int
21556b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
215615e32d5dSMike Smith {
215715e32d5dSMike Smith     return (0);
215815e32d5dSMike Smith }
215915e32d5dSMike Smith 
216015e32d5dSMike Smith static int
21616b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
216215e32d5dSMike Smith {
216315e32d5dSMike Smith     struct acpi_softc		*sc;
21640ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
21650ae55423SMike Smith     int				error, xerror, state;
2166fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
216715e32d5dSMike Smith 
2168cb9b0d80SMike Smith     ACPI_LOCK;
2169cb9b0d80SMike Smith 
217015e32d5dSMike Smith     error = state = 0;
217115e32d5dSMike Smith     sc = dev->si_drv1;
217215e32d5dSMike Smith 
21730ae55423SMike Smith     /*
21740ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
21750ae55423SMike Smith      */
21760ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
21770ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
21780ae55423SMike Smith 	    if (hp->cmd == cmd) {
21790ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
21800ae55423SMike Smith 		if (xerror != 0)
21810ae55423SMike Smith 		    error = xerror;
2182917d44c8SMitsuru IWASAKI 		goto out;
21830ae55423SMike Smith 	    }
21840ae55423SMike Smith 	}
21850ae55423SMike Smith     }
21860ae55423SMike Smith 
21870ae55423SMike Smith     /*
2188a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2189a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2190a89fcf28STakanori Watanabe      * Not changing system behavior.
2191a89fcf28STakanori Watanabe      */
2192be2b1797SNate Lawson     if((flag & FWRITE) == 0)
2193be2b1797SNate Lawson 	return (EPERM);
2194a89fcf28STakanori Watanabe 
2195be2b1797SNate Lawson     /* Core system ioctls. */
219615e32d5dSMike Smith     switch (cmd) {
219715e32d5dSMike Smith     case ACPIIO_ENABLE:
21980ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
219915e32d5dSMike Smith 	    error = ENXIO;
220015e32d5dSMike Smith 	break;
220115e32d5dSMike Smith     case ACPIIO_DISABLE:
22020ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
220315e32d5dSMike Smith 	    error = ENXIO;
220415e32d5dSMike Smith 	break;
220515e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
220615e32d5dSMike Smith 	if (!sc->acpi_enabled) {
220715e32d5dSMike Smith 	    error = ENXIO;
220815e32d5dSMike Smith 	    break;
220915e32d5dSMike Smith 	}
221015e32d5dSMike Smith 	state = *(int *)addr;
22112d610c46SNate Lawson 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
22122d610c46SNate Lawson 	    if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
221315e32d5dSMike Smith 		error = EINVAL;
22142d610c46SNate Lawson 	} else {
22152d610c46SNate Lawson 	    error = EINVAL;
22162d610c46SNate Lawson 	}
221715e32d5dSMike Smith 	break;
221815e32d5dSMike Smith     default:
22190ae55423SMike Smith 	if (error == 0)
222015e32d5dSMike Smith 	    error = EINVAL;
222115e32d5dSMike Smith 	break;
222215e32d5dSMike Smith     }
2223917d44c8SMitsuru IWASAKI 
2224917d44c8SMitsuru IWASAKI out:
2225cb9b0d80SMike Smith     ACPI_UNLOCK;
222615e32d5dSMike Smith     return (error);
222715e32d5dSMike Smith }
222815e32d5dSMike Smith 
22291d073b1dSJohn Baldwin static int
2230d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2231d75de536SMitsuru IWASAKI {
2232d75de536SMitsuru IWASAKI     char sleep_state[4];
2233d75de536SMitsuru IWASAKI     char buf[16];
2234d75de536SMitsuru IWASAKI     int error;
2235d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2236d75de536SMitsuru IWASAKI 
2237d75de536SMitsuru IWASAKI     buf[0] = '\0';
2238d75de536SMitsuru IWASAKI     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
2239d75de536SMitsuru IWASAKI 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
2240d75de536SMitsuru IWASAKI 	    sprintf(sleep_state, "S%d ", state);
2241d75de536SMitsuru IWASAKI 	    strcat(buf, sleep_state);
2242d75de536SMitsuru IWASAKI 	}
2243d75de536SMitsuru IWASAKI     }
2244d75de536SMitsuru IWASAKI     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2245d75de536SMitsuru IWASAKI     return (error);
2246d75de536SMitsuru IWASAKI }
2247d75de536SMitsuru IWASAKI 
2248d75de536SMitsuru IWASAKI static int
22491d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
22501d073b1dSJohn Baldwin {
22511d073b1dSJohn Baldwin     char sleep_state[10];
22521d073b1dSJohn Baldwin     int error;
22531d073b1dSJohn Baldwin     u_int new_state, old_state;
22541d073b1dSJohn Baldwin 
22551d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2256b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
22571d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
2258cb9b0d80SMike Smith     } else {
2259b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
22601d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
22611d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
2262cb9b0d80SMike Smith     }
22631d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
22641d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2265be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2266be2b1797SNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) {
22671d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
22681d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
22691d073b1dSJohn Baldwin 		break;
2270cb9b0d80SMike Smith 	}
2271931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2272be2b1797SNate Lawson 	    if (new_state != old_state)
22731d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2274cb9b0d80SMike Smith 	} else {
22751d073b1dSJohn Baldwin 	    error = EINVAL;
22761d073b1dSJohn Baldwin 	}
2277cb9b0d80SMike Smith     }
2278be2b1797SNate Lawson 
22791d073b1dSJohn Baldwin     return (error);
22801d073b1dSJohn Baldwin }
22811d073b1dSJohn Baldwin 
22829b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
22839b937d48SNate Lawson void
22849b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
22859b937d48SNate Lawson {
22869b937d48SNate Lawson     char		notify_buf[16];
22879b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
22889b937d48SNate Lawson     ACPI_STATUS		status;
22899b937d48SNate Lawson 
22909b937d48SNate Lawson     if (subsystem == NULL)
22919b937d48SNate Lawson 	return;
22929b937d48SNate Lawson 
22939b937d48SNate Lawson     handle_buf.Pointer = NULL;
22949b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
22959b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
22969b937d48SNate Lawson     if (ACPI_FAILURE(status))
22979b937d48SNate Lawson 	return;
22989b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
22999b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
23009b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
23019b937d48SNate Lawson }
23029b937d48SNate Lawson 
230315e32d5dSMike Smith #ifdef ACPI_DEBUG
23040ae55423SMike Smith /*
23050ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
23060ae55423SMike Smith  *
23070ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
23080ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
23090ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
23100ae55423SMike Smith  * prefixing the bit name with !.
23110ae55423SMike Smith  */
231215e32d5dSMike Smith struct debugtag
231315e32d5dSMike Smith {
231415e32d5dSMike Smith     char	*name;
231515e32d5dSMike Smith     UINT32	value;
231615e32d5dSMike Smith };
231715e32d5dSMike Smith 
231815e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2319c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2320c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2321c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2322c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2323c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2324c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2325c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2326c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2327c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2328d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
23294c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2330d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2331297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
23324c1cdee6SMike Smith 
2333c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2334c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
23353184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2336c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
23373184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
23383184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
23393184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
23404c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2341cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
23423184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2343b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
234415e32d5dSMike Smith     {NULL, 0}
234515e32d5dSMike Smith };
234615e32d5dSMike Smith 
234715e32d5dSMike Smith static struct debugtag dbg_level[] = {
23484c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
23499501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
23509501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
23514c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
23529501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
23534c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2354d62ab2f4SMitsuru IWASAKI 
2355d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2356297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
23574c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
23584c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2359d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
23604c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
23614c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
23624c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
23634c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
23644c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
23654c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
23664c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
23674c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
23684c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
23694c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2370d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2371d62ab2f4SMitsuru IWASAKI 
2372d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2373d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2374d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2375d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2376d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
23774c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2378d62ab2f4SMitsuru IWASAKI 
2379d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2380d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2381d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2382d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2383d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2384d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2385d62ab2f4SMitsuru IWASAKI 
2386d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
238798479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
238898479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
238998479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
239098479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
239198479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
239215e32d5dSMike Smith     {NULL, 0}
239315e32d5dSMike Smith };
239415e32d5dSMike Smith 
239515e32d5dSMike Smith static void
239615e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
239715e32d5dSMike Smith {
239815e32d5dSMike Smith     char	*ep;
239915e32d5dSMike Smith     int		i, l;
24000ae55423SMike Smith     int		set;
240115e32d5dSMike Smith 
240215e32d5dSMike Smith     while (*cp) {
240315e32d5dSMike Smith 	if (isspace(*cp)) {
240415e32d5dSMike Smith 	    cp++;
240515e32d5dSMike Smith 	    continue;
240615e32d5dSMike Smith 	}
240715e32d5dSMike Smith 	ep = cp;
240815e32d5dSMike Smith 	while (*ep && !isspace(*ep))
240915e32d5dSMike Smith 	    ep++;
24100ae55423SMike Smith 	if (*cp == '!') {
24110ae55423SMike Smith 	    set = 0;
24120ae55423SMike Smith 	    cp++;
24130ae55423SMike Smith 	    if (cp == ep)
24140ae55423SMike Smith 		continue;
24150ae55423SMike Smith 	} else {
24160ae55423SMike Smith 	    set = 1;
24170ae55423SMike Smith 	}
241815e32d5dSMike Smith 	l = ep - cp;
241915e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
242015e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2421be2b1797SNate Lawson 		if (set)
242215e32d5dSMike Smith 		    *flag |= tag[i].value;
2423be2b1797SNate Lawson 		else
24240ae55423SMike Smith 		    *flag &= ~tag[i].value;
242515e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
242615e32d5dSMike Smith 	    }
242715e32d5dSMike Smith 	}
242815e32d5dSMike Smith 	cp = ep;
242915e32d5dSMike Smith     }
243015e32d5dSMike Smith }
243115e32d5dSMike Smith 
243215e32d5dSMike Smith static void
24332a4ac806SMike Smith acpi_set_debugging(void *junk)
243415e32d5dSMike Smith {
243594aae282SMike Barcroft     char	*cp;
243615e32d5dSMike Smith 
24371d7b121cSNate Lawson     if (cold) {
24380ae55423SMike Smith 	AcpiDbgLayer = 0;
24390ae55423SMike Smith 	AcpiDbgLevel = 0;
24401d7b121cSNate Lawson     }
24411d7b121cSNate Lawson 
244294aae282SMike Barcroft     if ((cp = getenv("debug.acpi.layer")) != NULL) {
244315e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
244494aae282SMike Barcroft 	freeenv(cp);
244594aae282SMike Barcroft     }
244694aae282SMike Barcroft     if ((cp = getenv("debug.acpi.level")) != NULL) {
244715e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
244894aae282SMike Barcroft 	freeenv(cp);
244994aae282SMike Barcroft     }
24500ae55423SMike Smith 
24511d7b121cSNate Lawson     if (cold) {
24521d7b121cSNate Lawson 	printf("ACPI debug layer 0x%x debug level 0x%x\n",
24531d7b121cSNate Lawson 	       AcpiDbgLayer, AcpiDbgLevel);
24541d7b121cSNate Lawson     }
245515e32d5dSMike Smith }
2456be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2457be2b1797SNate Lawson 	NULL);
24581d7b121cSNate Lawson 
24591d7b121cSNate Lawson static int
24601d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
24611d7b121cSNate Lawson {
246254f6bca0SNate Lawson     int		 error, *dbg;
24631d7b121cSNate Lawson     struct	 debugtag *tag;
246454f6bca0SNate Lawson     struct	 sbuf sb;
24651d7b121cSNate Lawson 
246654f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
246754f6bca0SNate Lawson 	return (ENOMEM);
24681d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
24691d7b121cSNate Lawson 	tag = &dbg_layer[0];
24701d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
24711d7b121cSNate Lawson     } else {
24721d7b121cSNate Lawson 	tag = &dbg_level[0];
24731d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
24741d7b121cSNate Lawson     }
24751d7b121cSNate Lawson 
24761d7b121cSNate Lawson     /* Get old values if this is a get request. */
24771d7b121cSNate Lawson     if (*dbg == 0) {
247854f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
24791d7b121cSNate Lawson     } else if (req->newptr == NULL) {
24801d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
248154f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
248254f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
24831d7b121cSNate Lawson 	}
24841d7b121cSNate Lawson     }
248554f6bca0SNate Lawson     sbuf_trim(&sb);
248654f6bca0SNate Lawson     sbuf_finish(&sb);
24871d7b121cSNate Lawson 
248854f6bca0SNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
248954f6bca0SNate Lawson     sbuf_delete(&sb);
24901d7b121cSNate Lawson 
24911d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
24921d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
24931d7b121cSNate Lawson 	*dbg = 0;
24941d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
24951d7b121cSNate Lawson 	acpi_set_debugging(NULL);
24961d7b121cSNate Lawson     }
24971d7b121cSNate Lawson 
24981d7b121cSNate Lawson     return (error);
24991d7b121cSNate Lawson }
25001d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
25011d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
25021d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
25031d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
250415e32d5dSMike Smith #endif
2505f9390180SMitsuru IWASAKI 
2506f9390180SMitsuru IWASAKI static int
2507f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
2508f9390180SMitsuru IWASAKI {
2509f9390180SMitsuru IWASAKI 	int	state, acpi_state;
2510f9390180SMitsuru IWASAKI 	int	error;
2511f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
2512f9390180SMitsuru IWASAKI 	va_list	ap;
2513f9390180SMitsuru IWASAKI 
2514f9390180SMitsuru IWASAKI 	error = 0;
2515f9390180SMitsuru IWASAKI 	switch (cmd) {
2516f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
2517f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
2518f9390180SMitsuru IWASAKI 		if (sc == NULL) {
2519f9390180SMitsuru IWASAKI 			error = EINVAL;
2520f9390180SMitsuru IWASAKI 			goto out;
2521f9390180SMitsuru IWASAKI 		}
2522f9390180SMitsuru IWASAKI 
2523f9390180SMitsuru IWASAKI 		va_start(ap, arg);
2524f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
2525f9390180SMitsuru IWASAKI 		va_end(ap);
2526f9390180SMitsuru IWASAKI 
2527f9390180SMitsuru IWASAKI 		switch (state) {
2528f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
2529f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
2530f9390180SMitsuru IWASAKI 			break;
2531f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
2532f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
2533f9390180SMitsuru IWASAKI 			break;
2534f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
2535f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
2536f9390180SMitsuru IWASAKI 			break;
2537f9390180SMitsuru IWASAKI 		default:
2538f9390180SMitsuru IWASAKI 			error = EINVAL;
2539f9390180SMitsuru IWASAKI 			goto out;
2540f9390180SMitsuru IWASAKI 		}
2541f9390180SMitsuru IWASAKI 
2542f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
2543f9390180SMitsuru IWASAKI 		break;
2544f9390180SMitsuru IWASAKI 	default:
2545f9390180SMitsuru IWASAKI 		error = EINVAL;
2546f9390180SMitsuru IWASAKI 		goto out;
2547f9390180SMitsuru IWASAKI 	}
2548f9390180SMitsuru IWASAKI 
2549f9390180SMitsuru IWASAKI out:
2550f9390180SMitsuru IWASAKI 	return (error);
2551f9390180SMitsuru IWASAKI }
2552f9390180SMitsuru IWASAKI 
2553f9390180SMitsuru IWASAKI static void
2554f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
2555f9390180SMitsuru IWASAKI {
2556be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
25576c407052SMitsuru IWASAKI 	return;
2558f9390180SMitsuru IWASAKI 
2559f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
2560f9390180SMitsuru IWASAKI }
2561f9390180SMitsuru IWASAKI 
2562f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
2563