xref: /freebsd/sys/dev/acpica/acpi.c (revision b9c780d64e2d0309dfe5c48e138582e10408d9d1)
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>
36f34fa851SJohn Baldwin #include <sys/lock.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
38b2c9c0daSMitsuru IWASAKI #include <sys/mutex.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
451611ea87SMitsuru IWASAKI #include <sys/linker.h>
46f9390180SMitsuru IWASAKI #include <sys/power.h>
4715e32d5dSMike Smith 
4815e32d5dSMike Smith #include <machine/clock.h>
4915e32d5dSMike Smith #include <machine/resource.h>
5015e32d5dSMike Smith 
51bc0f2195SMike Smith #include <isa/isavar.h>
52bc0f2195SMike Smith 
5315e32d5dSMike Smith #include "acpi.h"
5415e32d5dSMike Smith 
551611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h>
561611ea87SMitsuru IWASAKI 
5715e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5815e32d5dSMike Smith #include <dev/acpica/acpiio.h>
5915e32d5dSMike Smith 
6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6115e32d5dSMike Smith 
6215e32d5dSMike Smith /*
630ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
640ae55423SMike Smith  */
65cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
660ae55423SMike Smith MODULE_NAME("ACPI")
670ae55423SMike Smith 
680ae55423SMike Smith /*
6915e32d5dSMike Smith  * Character device
7015e32d5dSMike Smith  */
7115e32d5dSMike Smith 
7215e32d5dSMike Smith static d_open_t		acpiopen;
7315e32d5dSMike Smith static d_close_t	acpiclose;
7415e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7515e32d5dSMike Smith 
7615e32d5dSMike Smith #define CDEV_MAJOR 152
7715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
7815e32d5dSMike Smith     acpiopen,
7915e32d5dSMike Smith     acpiclose,
8015e32d5dSMike Smith     noread,
8115e32d5dSMike Smith     nowrite,
8215e32d5dSMike Smith     acpiioctl,
8315e32d5dSMike Smith     nopoll,
8415e32d5dSMike Smith     nommap,
8515e32d5dSMike Smith     nostrategy,
8615e32d5dSMike Smith     "acpi",
8715e32d5dSMike Smith     CDEV_MAJOR,
8815e32d5dSMike Smith     nodump,
8915e32d5dSMike Smith     nopsize,
90606e1d68SJohn Baldwin     0
9115e32d5dSMike Smith };
9215e32d5dSMike Smith 
931d073b1dSJohn Baldwin static const char* sleep_state_names[] = {
94b8670bedSMitsuru IWASAKI     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
951d073b1dSJohn Baldwin 
962a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */
972a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5;
982a4ac806SMike Smith 
99cb9b0d80SMike Smith struct mtx	acpi_mutex;
100cb9b0d80SMike Smith 
1016d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10215e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
10315e32d5dSMike Smith static int	acpi_probe(device_t dev);
10415e32d5dSMike Smith static int	acpi_attach(device_t dev);
10515e32d5dSMike Smith static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
10615e32d5dSMike Smith static int	acpi_print_resources(struct resource_list *rl, const char *name, int type,
10715e32d5dSMike Smith 				     const char *format);
10815e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10915e32d5dSMike Smith static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
11015e32d5dSMike Smith static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
11115e32d5dSMike Smith static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
11215e32d5dSMike Smith 				  u_long count);
11315e32d5dSMike Smith static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
11415e32d5dSMike Smith 				  u_long *countp);
11515e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
11615e32d5dSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
11715e32d5dSMike Smith static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
11832d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev);
119bc0f2195SMike Smith static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
12015e32d5dSMike Smith 
12115e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
12215e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
12315e32d5dSMike Smith 
12415e32d5dSMike Smith static void	acpi_shutdown_pre_sync(void *arg, int howto);
12515e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
12615e32d5dSMike Smith 
12713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
12813d4f7baSMitsuru IWASAKI 
12915e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
13015e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
1311d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
13215e32d5dSMike Smith 
133f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
134f9390180SMitsuru IWASAKI 
13515e32d5dSMike Smith static device_method_t acpi_methods[] = {
13615e32d5dSMike Smith     /* Device interface */
13715e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
13815e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
13915e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
14015e32d5dSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
14115e32d5dSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
14215e32d5dSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
14315e32d5dSMike Smith 
14415e32d5dSMike Smith     /* Bus interface */
14515e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
14615e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
14715e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
14815e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
14915e32d5dSMike Smith     DEVMETHOD(bus_set_resource,		acpi_set_resource),
15015e32d5dSMike Smith     DEVMETHOD(bus_get_resource,		acpi_get_resource),
15115e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
15215e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
15315e32d5dSMike Smith     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
15415e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
15515e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
15615e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
15715e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
15815e32d5dSMike Smith 
159bc0f2195SMike Smith     /* ISA emulation */
160bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
161bc0f2195SMike Smith 
16215e32d5dSMike Smith     {0, 0}
16315e32d5dSMike Smith };
16415e32d5dSMike Smith 
16515e32d5dSMike Smith static driver_t acpi_driver = {
16615e32d5dSMike Smith     "acpi",
16715e32d5dSMike Smith     acpi_methods,
16815e32d5dSMike Smith     sizeof(struct acpi_softc),
16915e32d5dSMike Smith };
17015e32d5dSMike Smith 
17115e32d5dSMike Smith devclass_t acpi_devclass;
1726d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
173dde24897SMike Smith MODULE_VERSION(acpi, 100);
17415e32d5dSMike Smith 
17515e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
17615e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
177dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION;
178dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
17915e32d5dSMike Smith 
18015e32d5dSMike Smith /*
1816d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
1826d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
1836d63101aSMike Smith  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
1846d63101aSMike Smith  */
1856d63101aSMike Smith static int
1866d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
1876d63101aSMike Smith {
1886d63101aSMike Smith     switch(event) {
1896d63101aSMike Smith     case MOD_LOAD:
1906d63101aSMike Smith 	if (!cold)
1916d63101aSMike Smith 	    return(EPERM);
1926d63101aSMike Smith 	break;
1936d63101aSMike Smith     case MOD_UNLOAD:
194f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
1956d63101aSMike Smith 	    return(EBUSY);
196f9390180SMitsuru IWASAKI 	break;
1976d63101aSMike Smith     default:
1986d63101aSMike Smith 	break;
1996d63101aSMike Smith     }
2006d63101aSMike Smith     return(0);
2016d63101aSMike Smith }
2026d63101aSMike Smith 
2036d63101aSMike Smith /*
20415e32d5dSMike Smith  * Detect ACPI, perform early initialisation
20515e32d5dSMike Smith  */
20615e32d5dSMike Smith static void
20715e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent)
20815e32d5dSMike Smith {
20915e32d5dSMike Smith     device_t			child;
21015e32d5dSMike Smith     int				error;
2111611ea87SMitsuru IWASAKI     caddr_t			acpi_dsdt, p;
21215e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
21315e32d5dSMike Smith     char			*debugpoint = getenv("debug.acpi.debugger");
21415e32d5dSMike Smith #endif
21515e32d5dSMike Smith 
2162a4ac806SMike Smith     FUNCTION_TRACE(__func__);
2170ae55423SMike Smith 
218840f9b53STakanori Watanabe     if(!cold){
219840f9b53STakanori Watanabe 	    printf("Don't load this driver from userland!!\n");
220840f9b53STakanori Watanabe 	    return ;
221840f9b53STakanori Watanabe     }
222840f9b53STakanori Watanabe 
22315e32d5dSMike Smith     /*
22432d18aa5SMike Smith      * Check that we haven't been disabled with a hint.
22532d18aa5SMike Smith      */
22632d18aa5SMike Smith     if (!resource_int_value("acpi", 0, "disabled", &error) &&
22732d18aa5SMike Smith 	(error != 0))
22832d18aa5SMike Smith 	return_VOID;
22932d18aa5SMike Smith 
23032d18aa5SMike Smith     /*
23115e32d5dSMike Smith      * Make sure we're not being doubly invoked.
23215e32d5dSMike Smith      */
23315e32d5dSMike Smith     if (device_find_child(parent, "acpi", 0) != NULL)
2340ae55423SMike Smith 	return_VOID;
23515e32d5dSMike Smith 
236cb9b0d80SMike Smith     /* initialise the ACPI mutex */
237cb9b0d80SMike Smith     mtx_init(&acpi_mutex, "ACPI global lock", MTX_DEF);
238cb9b0d80SMike Smith 
23915e32d5dSMike Smith     /*
2402a4ac806SMike Smith      * Start up the ACPI CA subsystem.
24115e32d5dSMike Smith      */
24215e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
24315e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "init"))
24415e32d5dSMike Smith 	acpi_EnterDebugger();
24515e32d5dSMike Smith #endif
24615e32d5dSMike Smith     if ((error = AcpiInitializeSubsystem()) != AE_OK) {
247bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
2480ae55423SMike Smith 	return_VOID;
24915e32d5dSMike Smith     }
25015e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
25115e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "tables"))
25215e32d5dSMike Smith 	acpi_EnterDebugger();
25315e32d5dSMike Smith #endif
2541611ea87SMitsuru IWASAKI 
2551611ea87SMitsuru IWASAKI     if ((acpi_dsdt = preload_search_by_type("acpi_dsdt")) != NULL) {
2561611ea87SMitsuru IWASAKI         if ((p = preload_search_info(acpi_dsdt, MODINFO_ADDR)) != NULL) {
2571611ea87SMitsuru IWASAKI 	    error = AcpiSetDsdtTablePtr(*(void **)p);
2581611ea87SMitsuru IWASAKI             if (error != AE_OK) {
2591611ea87SMitsuru IWASAKI 		printf("ACPI: DSDT overriding failed: %s\n",
2601611ea87SMitsuru IWASAKI 		       AcpiFormatException(error));
2611611ea87SMitsuru IWASAKI 	    } else {
2621611ea87SMitsuru IWASAKI 		printf("ACPI: DSDT was overridden.\n");
2631611ea87SMitsuru IWASAKI 	    }
2641611ea87SMitsuru IWASAKI         }
2651611ea87SMitsuru IWASAKI     }
2661611ea87SMitsuru IWASAKI 
267acf72ef4SMike Smith     if ((error = AcpiLoadTables()) != AE_OK) {
268bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
2690ae55423SMike Smith 	return_VOID;
27015e32d5dSMike Smith     }
27115e32d5dSMike Smith 
27215e32d5dSMike Smith     /*
27315e32d5dSMike Smith      * Attach the actual ACPI device.
27415e32d5dSMike Smith      */
27515e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
27615e32d5dSMike Smith 	    device_printf(parent, "ACPI: could not attach\n");
2770ae55423SMike Smith 	    return_VOID;
27815e32d5dSMike Smith     }
27915e32d5dSMike Smith }
28015e32d5dSMike Smith 
28115e32d5dSMike Smith /*
28215e32d5dSMike Smith  * Fetch some descriptive data from ACPI to put in our attach message
28315e32d5dSMike Smith  */
28415e32d5dSMike Smith static int
28515e32d5dSMike Smith acpi_probe(device_t dev)
28615e32d5dSMike Smith {
28715e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
28815e32d5dSMike Smith     char		buf[20];
289cb9b0d80SMike Smith     ACPI_STATUS		status;
29015e32d5dSMike Smith     int			error;
29115e32d5dSMike Smith 
2922a4ac806SMike Smith     FUNCTION_TRACE(__func__);
293f9390180SMitsuru IWASAKI 
294f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
295f9390180SMitsuru IWASAKI         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
296f9390180SMitsuru IWASAKI 	device_printf(dev, "Other PM system enabled.\n");
297f9390180SMitsuru IWASAKI 	return_VALUE(ENXIO);
298f9390180SMitsuru IWASAKI     }
299f9390180SMitsuru IWASAKI 
300cb9b0d80SMike Smith     ACPI_LOCK;
3010ae55423SMike Smith 
302cb9b0d80SMike Smith     if ((status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) {
303bfae45aaSMike Smith 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
304cb9b0d80SMike Smith 	error = ENXIO;
305cb9b0d80SMike Smith     } else {
30615e32d5dSMike Smith 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
30715e32d5dSMike Smith 	device_set_desc_copy(dev, buf);
308cb9b0d80SMike Smith 	error = 0;
309cb9b0d80SMike Smith     }
310cb9b0d80SMike Smith     ACPI_UNLOCK;
311cb9b0d80SMike Smith     return_VALUE(error);
31215e32d5dSMike Smith }
31315e32d5dSMike Smith 
31415e32d5dSMike Smith static int
31515e32d5dSMike Smith acpi_attach(device_t dev)
31615e32d5dSMike Smith {
31715e32d5dSMike Smith     struct acpi_softc	*sc;
318cb9b0d80SMike Smith     ACPI_STATUS		status;
31915e32d5dSMike Smith     int			error;
32032d18aa5SMike Smith     UINT32		flags;
32132d18aa5SMike Smith 
32215e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
32315e32d5dSMike Smith     char		*debugpoint = getenv("debug.acpi.debugger");
32415e32d5dSMike Smith #endif
32515e32d5dSMike Smith 
3262a4ac806SMike Smith     FUNCTION_TRACE(__func__);
327cb9b0d80SMike Smith     ACPI_LOCK;
32815e32d5dSMike Smith     sc = device_get_softc(dev);
32915e32d5dSMike Smith     bzero(sc, sizeof(*sc));
33015e32d5dSMike Smith     sc->acpi_dev = dev;
33115e32d5dSMike Smith 
33215e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
33315e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "spaces"))
33415e32d5dSMike Smith 	acpi_EnterDebugger();
33515e32d5dSMike Smith #endif
33615e32d5dSMike Smith 
33715e32d5dSMike Smith     /*
33815e32d5dSMike Smith      * Install the default address space handlers.
33915e32d5dSMike Smith      */
340cb9b0d80SMike Smith     error = ENXIO;
341cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3422a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
34315e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
34415e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
345bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
346cb9b0d80SMike Smith 	goto out;
34715e32d5dSMike Smith     }
348cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3492a4ac806SMike Smith 						ACPI_ADR_SPACE_SYSTEM_IO,
35015e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
35115e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
352bfae45aaSMike Smith 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
353cb9b0d80SMike Smith 	goto out;
35415e32d5dSMike Smith     }
355cb9b0d80SMike Smith     if ((status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
3562a4ac806SMike Smith 						ACPI_ADR_SPACE_PCI_CONFIG,
35715e32d5dSMike Smith 						ACPI_DEFAULT_HANDLER,
35815e32d5dSMike Smith 						NULL, NULL)) != AE_OK) {
359bfae45aaSMike Smith 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
360cb9b0d80SMike Smith 	goto out;
36115e32d5dSMike Smith     }
36215e32d5dSMike Smith 
36315e32d5dSMike Smith     /*
36415e32d5dSMike Smith      * Bring ACPI fully online.
36515e32d5dSMike Smith      *
36632d18aa5SMike Smith      * Note that some systems (specifically, those with namespace evaluation issues
36732d18aa5SMike Smith      * that require the avoidance of parts of the namespace) must avoid running _INI
36832d18aa5SMike Smith      * and _STA on everything, as well as dodging the final object init pass.
36915e32d5dSMike Smith      *
37032d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
37132d18aa5SMike Smith      *
37232d18aa5SMike Smith      * XXX We should arrange for the object init pass after we have attached all our
37332d18aa5SMike Smith      *     child devices, but on many systems it works here.
37415e32d5dSMike Smith      */
37515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
37615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "enable"))
37715e32d5dSMike Smith 	acpi_EnterDebugger();
37815e32d5dSMike Smith #endif
37932d18aa5SMike Smith     flags = 0;
380b525621aSMike Smith     if (getenv("debug.acpi.avoid") != NULL)
38132d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
38232d18aa5SMike Smith     if ((status = AcpiEnableSubsystem(flags)) != AE_OK) {
383bfae45aaSMike Smith 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
384cb9b0d80SMike Smith 	goto out;
38515e32d5dSMike Smith     }
38615e32d5dSMike Smith 
38715e32d5dSMike Smith     /*
3881d073b1dSJohn Baldwin      * Setup our sysctl tree.
3891d073b1dSJohn Baldwin      *
3901d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
3911d073b1dSJohn Baldwin      */
3921d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
3931d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
3941d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
3951d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
3961d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
3971d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
3981d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
3991d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4001d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4011d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4021d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4031d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4041d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
405f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
406f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
407f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
408f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
409f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
410f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
4112d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4121611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
4131611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
4141611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4152d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
4162d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
4172d644607SMitsuru IWASAKI     if (bootverbose)
4182d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
4191d073b1dSJohn Baldwin 
4201d073b1dSJohn Baldwin     /*
42115e32d5dSMike Smith      * Dispatch the default sleep state to devices.
42215e32d5dSMike Smith      * TBD: should be configured from userland policy manager.
42315e32d5dSMike Smith      */
42415e32d5dSMike Smith     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
42515e32d5dSMike Smith     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
42615e32d5dSMike Smith     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
427f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
428f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
42915e32d5dSMike Smith 
43013d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
43115e32d5dSMike Smith 
43215e32d5dSMike Smith     /*
43315e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
43415e32d5dSMike Smith      */
43515e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
43615e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "probe"))
43715e32d5dSMike Smith 	acpi_EnterDebugger();
43815e32d5dSMike Smith #endif
4390ae55423SMike Smith     if (!acpi_disabled("bus"))
44015e32d5dSMike Smith 	acpi_probe_children(dev);
44115e32d5dSMike Smith 
44215e32d5dSMike Smith     /*
44315e32d5dSMike Smith      * Register our shutdown handlers
44415e32d5dSMike Smith      */
44515e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
44615e32d5dSMike Smith     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
44715e32d5dSMike Smith 
44815e32d5dSMike Smith     /*
44915e32d5dSMike Smith      * Register our acpi event handlers.
45015e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
45115e32d5dSMike Smith      */
45215e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
45315e32d5dSMike Smith     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
45415e32d5dSMike Smith 
45515e32d5dSMike Smith     /*
45615e32d5dSMike Smith      * Flag our initial states.
45715e32d5dSMike Smith      */
45815e32d5dSMike Smith     sc->acpi_enabled = 1;
45915e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
46015e32d5dSMike Smith 
46115e32d5dSMike Smith     /*
46215e32d5dSMike Smith      * Create the control device
46315e32d5dSMike Smith      */
46415e32d5dSMike Smith     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
46515e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
46615e32d5dSMike Smith 
46715e32d5dSMike Smith #ifdef ENABLE_DEBUGGER
46815e32d5dSMike Smith     if (debugpoint && !strcmp(debugpoint, "running"))
46915e32d5dSMike Smith 	acpi_EnterDebugger();
47015e32d5dSMike Smith #endif
471f86214b6SMitsuru IWASAKI 
472f86214b6SMitsuru IWASAKI     if ((error = acpi_machdep_init(dev))) {
473f86214b6SMitsuru IWASAKI 	goto out;
474f86214b6SMitsuru IWASAKI     }
475f86214b6SMitsuru IWASAKI 
476f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
477f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
478f9390180SMitsuru IWASAKI 
479cb9b0d80SMike Smith     error = 0;
480cb9b0d80SMike Smith 
481cb9b0d80SMike Smith  out:
482cb9b0d80SMike Smith     ACPI_UNLOCK;
483cb9b0d80SMike Smith     return_VALUE(error);
48415e32d5dSMike Smith }
48515e32d5dSMike Smith 
48615e32d5dSMike Smith /*
48715e32d5dSMike Smith  * Handle a new device being added
48815e32d5dSMike Smith  */
48915e32d5dSMike Smith static device_t
49015e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
49115e32d5dSMike Smith {
49215e32d5dSMike Smith     struct acpi_device	*ad;
49315e32d5dSMike Smith     device_t		child;
49415e32d5dSMike Smith 
49515e32d5dSMike Smith     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
49615e32d5dSMike Smith 	return(NULL);
49715e32d5dSMike Smith     bzero(ad, sizeof(*ad));
49815e32d5dSMike Smith 
49915e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
50015e32d5dSMike Smith 
50115e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
50215e32d5dSMike Smith     if (child != NULL)
50315e32d5dSMike Smith 	device_set_ivars(child, ad);
50415e32d5dSMike Smith     return(child);
50515e32d5dSMike Smith }
50615e32d5dSMike Smith 
50715e32d5dSMike Smith /*
50815e32d5dSMike Smith  * Print child device resource usage
50915e32d5dSMike Smith  */
51015e32d5dSMike Smith static int
51115e32d5dSMike Smith acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
51215e32d5dSMike Smith {
51315e32d5dSMike Smith     struct resource_list_entry	*rle;
51415e32d5dSMike Smith     int				printed, retval;
51515e32d5dSMike Smith 
51615e32d5dSMike Smith     printed = 0;
51715e32d5dSMike Smith     retval = 0;
51815e32d5dSMike Smith 
51915e32d5dSMike Smith     if (!SLIST_FIRST(rl))
52015e32d5dSMike Smith 	return(0);
52115e32d5dSMike Smith 
52215e32d5dSMike Smith     /* Yes, this is kinda cheating */
52315e32d5dSMike Smith     SLIST_FOREACH(rle, rl, link) {
52415e32d5dSMike Smith 	if (rle->type == type) {
52515e32d5dSMike Smith 	    if (printed == 0)
52615e32d5dSMike Smith 		retval += printf(" %s ", name);
52715e32d5dSMike Smith 	    else if (printed > 0)
52815e32d5dSMike Smith 		retval += printf(",");
52915e32d5dSMike Smith 	    printed++;
53015e32d5dSMike Smith 	    retval += printf(format, rle->start);
53115e32d5dSMike Smith 	    if (rle->count > 1) {
53215e32d5dSMike Smith 		retval += printf("-");
53315e32d5dSMike Smith 		retval += printf(format, rle->start +
53415e32d5dSMike Smith 				 rle->count - 1);
53515e32d5dSMike Smith 	    }
53615e32d5dSMike Smith 	}
53715e32d5dSMike Smith     }
53815e32d5dSMike Smith     return(retval);
53915e32d5dSMike Smith }
54015e32d5dSMike Smith 
54115e32d5dSMike Smith static int
54215e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
54315e32d5dSMike Smith {
54415e32d5dSMike Smith     struct acpi_device		*adev = device_get_ivars(child);
54515e32d5dSMike Smith     struct resource_list	*rl = &adev->ad_rl;
54615e32d5dSMike Smith     int retval = 0;
54715e32d5dSMike Smith 
54815e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
54915e32d5dSMike Smith     retval += acpi_print_resources(rl, "port",  SYS_RES_IOPORT, "%#lx");
55015e32d5dSMike Smith     retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
55115e32d5dSMike Smith     retval += acpi_print_resources(rl, "irq",   SYS_RES_IRQ,    "%ld");
55215e32d5dSMike Smith     retval += bus_print_child_footer(bus, child);
55315e32d5dSMike Smith 
55415e32d5dSMike Smith     return(retval);
55515e32d5dSMike Smith }
55615e32d5dSMike Smith 
55715e32d5dSMike Smith 
55815e32d5dSMike Smith /*
55915e32d5dSMike Smith  * Handle per-device ivars
56015e32d5dSMike Smith  */
56115e32d5dSMike Smith static int
56215e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
56315e32d5dSMike Smith {
56415e32d5dSMike Smith     struct acpi_device	*ad;
56515e32d5dSMike Smith 
56615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
56715e32d5dSMike Smith 	printf("device has no ivars\n");
56815e32d5dSMike Smith 	return(ENOENT);
56915e32d5dSMike Smith     }
57015e32d5dSMike Smith 
57115e32d5dSMike Smith     switch(index) {
57215e32d5dSMike Smith 	/* ACPI ivars */
57315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
57415e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
57515e32d5dSMike Smith 	break;
57615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
57715e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
57815e32d5dSMike Smith 	break;
57915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
58015e32d5dSMike Smith 	*(void **)result = ad->ad_private;
58115e32d5dSMike Smith 	break;
58215e32d5dSMike Smith 
58332d18aa5SMike Smith 	/* ISA compatibility */
58432d18aa5SMike Smith     case ISA_IVAR_VENDORID:
58532d18aa5SMike Smith     case ISA_IVAR_SERIAL:
58632d18aa5SMike Smith     case ISA_IVAR_COMPATID:
58732d18aa5SMike Smith 	*(int *)result = -1;
58832d18aa5SMike Smith 	break;
58932d18aa5SMike Smith 
59032d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
59132d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
59232d18aa5SMike Smith 	break;
59332d18aa5SMike Smith 
59415e32d5dSMike Smith     default:
59515e32d5dSMike Smith 	panic("bad ivar read request (%d)\n", index);
59615e32d5dSMike Smith 	return(ENOENT);
59715e32d5dSMike Smith     }
59815e32d5dSMike Smith     return(0);
59915e32d5dSMike Smith }
60015e32d5dSMike Smith 
60115e32d5dSMike Smith static int
60215e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
60315e32d5dSMike Smith {
60415e32d5dSMike Smith     struct acpi_device	*ad;
60515e32d5dSMike Smith 
60615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
60715e32d5dSMike Smith 	printf("device has no ivars\n");
60815e32d5dSMike Smith 	return(ENOENT);
60915e32d5dSMike Smith     }
61015e32d5dSMike Smith 
61115e32d5dSMike Smith     switch(index) {
61215e32d5dSMike Smith 	/* ACPI ivars */
61315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
61415e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
61515e32d5dSMike Smith 	break;
61615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
61715e32d5dSMike Smith 	ad->ad_magic = (int )value;
61815e32d5dSMike Smith 	break;
61915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
62015e32d5dSMike Smith 	ad->ad_private = (void *)value;
62115e32d5dSMike Smith 	break;
62215e32d5dSMike Smith 
62315e32d5dSMike Smith     default:
62415e32d5dSMike Smith 	panic("bad ivar write request (%d)\n", index);
62515e32d5dSMike Smith 	return(ENOENT);
62615e32d5dSMike Smith     }
62715e32d5dSMike Smith     return(0);
62815e32d5dSMike Smith }
62915e32d5dSMike Smith 
63015e32d5dSMike Smith /*
63115e32d5dSMike Smith  * Handle child resource allocation/removal
63215e32d5dSMike Smith  */
63315e32d5dSMike Smith static int
63415e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
63515e32d5dSMike Smith {
63615e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
63715e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
63815e32d5dSMike Smith 
63915e32d5dSMike Smith     resource_list_add(rl, type, rid, start, start + count -1, count);
64015e32d5dSMike Smith 
64115e32d5dSMike Smith     return(0);
64215e32d5dSMike Smith }
64315e32d5dSMike Smith 
64415e32d5dSMike Smith static int
64515e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
64615e32d5dSMike Smith {
64715e32d5dSMike Smith     struct acpi_device		*ad = device_get_ivars(child);
64815e32d5dSMike Smith     struct resource_list	*rl = &ad->ad_rl;
64915e32d5dSMike Smith     struct resource_list_entry	*rle;
65015e32d5dSMike Smith 
65115e32d5dSMike Smith     rle = resource_list_find(rl, type, rid);
65215e32d5dSMike Smith     if (!rle)
65315e32d5dSMike Smith 	return(ENOENT);
65415e32d5dSMike Smith 
65515e32d5dSMike Smith     if (startp)
65615e32d5dSMike Smith 	*startp = rle->start;
65715e32d5dSMike Smith     if (countp)
65815e32d5dSMike Smith 	*countp = rle->count;
65915e32d5dSMike Smith 
66015e32d5dSMike Smith     return(0);
66115e32d5dSMike Smith }
66215e32d5dSMike Smith 
66315e32d5dSMike Smith static struct resource *
66415e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
66515e32d5dSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
66615e32d5dSMike Smith {
66715e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
66815e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
66915e32d5dSMike Smith 
67015e32d5dSMike Smith     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
67115e32d5dSMike Smith }
67215e32d5dSMike Smith 
67315e32d5dSMike Smith static int
67415e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
67515e32d5dSMike Smith {
67615e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
67715e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
67815e32d5dSMike Smith 
67915e32d5dSMike Smith     return(resource_list_release(rl, bus, child, type, rid, r));
68015e32d5dSMike Smith }
68115e32d5dSMike Smith 
68215e32d5dSMike Smith /*
683bc0f2195SMike Smith  * Handle ISA-like devices probing for a PnP ID to match.
684bc0f2195SMike Smith  */
685bc0f2195SMike Smith #define PNP_EISAID(s)				\
686bc0f2195SMike Smith 	((((s[0] - '@') & 0x1f) << 2)		\
687bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x18) >> 3)		\
688bc0f2195SMike Smith 	 | (((s[1] - '@') & 0x07) << 13)	\
689bc0f2195SMike Smith 	 | (((s[2] - '@') & 0x1f) << 8)		\
690bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[4]) << 16)		\
691bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[3]) << 20)		\
692bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[6]) << 24)		\
693bc0f2195SMike Smith 	 | (PNP_HEXTONUM(s[5]) << 28))
694bc0f2195SMike Smith 
69532d18aa5SMike Smith static u_int32_t
69632d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
697bc0f2195SMike Smith {
698bc0f2195SMike Smith     ACPI_HANDLE		h;
699bc0f2195SMike Smith     ACPI_DEVICE_INFO	devinfo;
700bc0f2195SMike Smith     ACPI_STATUS		error;
701bc0f2195SMike Smith     u_int32_t		pnpid;
70232d18aa5SMike Smith 
70332d18aa5SMike Smith     FUNCTION_TRACE(__func__);
70432d18aa5SMike Smith 
70532d18aa5SMike Smith     pnpid = 0;
70632d18aa5SMike Smith     ACPI_LOCK;
70732d18aa5SMike Smith 
70832d18aa5SMike Smith     /* fetch and validate the HID */
70932d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
71032d18aa5SMike Smith 	goto out;
71132d18aa5SMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
71232d18aa5SMike Smith 	goto out;
71332d18aa5SMike Smith     if (!(devinfo.Valid & ACPI_VALID_HID))
71432d18aa5SMike Smith 	goto out;
71532d18aa5SMike Smith 
71632d18aa5SMike Smith     pnpid = PNP_EISAID(devinfo.HardwareId);
71732d18aa5SMike Smith out:
71832d18aa5SMike Smith     ACPI_UNLOCK;
71932d18aa5SMike Smith     return_VALUE(pnpid);
72032d18aa5SMike Smith }
72132d18aa5SMike Smith 
72232d18aa5SMike Smith static int
72332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
72432d18aa5SMike Smith {
725bc0f2195SMike Smith     int			result;
72632d18aa5SMike Smith     u_int32_t		pnpid;
727bc0f2195SMike Smith 
728bc0f2195SMike Smith     FUNCTION_TRACE(__func__);
729bc0f2195SMike Smith 
730bc0f2195SMike Smith     /*
731bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
732bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
733bc0f2195SMike Smith      * that to happen, so don't ever return it.
734bc0f2195SMike Smith      */
735bc0f2195SMike Smith     result = ENXIO;
736bc0f2195SMike Smith 
737bc0f2195SMike Smith     /* scan the supplied IDs for a match */
73832d18aa5SMike Smith     pnpid = acpi_isa_get_logicalid(child);
739bc0f2195SMike Smith     while (ids && ids->ip_id) {
740bc0f2195SMike Smith 	if (pnpid == ids->ip_id) {
741bc0f2195SMike Smith 	    result = 0;
742bc0f2195SMike Smith 	    goto out;
743bc0f2195SMike Smith 	}
744bc0f2195SMike Smith 	ids++;
745bc0f2195SMike Smith     }
746bc0f2195SMike Smith  out:
747bc0f2195SMike Smith     return_VALUE(result);
748bc0f2195SMike Smith }
749bc0f2195SMike Smith 
750bc0f2195SMike Smith /*
75115e32d5dSMike Smith  * Scan relevant portions of the ACPI namespace and attach child devices.
75215e32d5dSMike Smith  *
7537d3bcec9SMike Smith  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes,
7547d3bcec9SMike Smith  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
75515e32d5dSMike Smith  */
75615e32d5dSMike Smith static void
75715e32d5dSMike Smith acpi_probe_children(device_t bus)
75815e32d5dSMike Smith {
75915e32d5dSMike Smith     ACPI_HANDLE		parent;
7607d3bcec9SMike Smith     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
76115e32d5dSMike Smith     int			i;
76215e32d5dSMike Smith 
7632a4ac806SMike Smith     FUNCTION_TRACE(__func__);
764cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
7650ae55423SMike Smith 
76615e32d5dSMike Smith     /*
76715e32d5dSMike Smith      * Create any static children by calling device identify methods.
76815e32d5dSMike Smith      */
7694c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
77015e32d5dSMike Smith     bus_generic_probe(bus);
77115e32d5dSMike Smith 
77215e32d5dSMike Smith     /*
77315e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
77415e32d5dSMike Smith      * we find.
77515e32d5dSMike Smith      *
77615e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
77715e32d5dSMike Smith      * we want to create nodes for all devices, not just those that are currently
77815e32d5dSMike Smith      * present. (This assumes that we don't want to create/remove devices as they
77915e32d5dSMike Smith      * appear, which might be smarter.)
78015e32d5dSMike Smith      */
7814c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
78215e32d5dSMike Smith     for (i = 0; scopes[i] != NULL; i++)
78315e32d5dSMike Smith 	if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK)
78415e32d5dSMike Smith 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
78515e32d5dSMike Smith 
78615e32d5dSMike Smith     /*
78715e32d5dSMike Smith      * Scan all of the child devices we have created and let them probe/attach.
78815e32d5dSMike Smith      */
7894c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
79015e32d5dSMike Smith     bus_generic_attach(bus);
79115e32d5dSMike Smith 
79215e32d5dSMike Smith     /*
79315e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
79415e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
79515e32d5dSMike Smith      */
7964c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
79715e32d5dSMike Smith     bus_generic_attach(bus);
7980ae55423SMike Smith 
799bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
8000ae55423SMike Smith     return_VOID;
80115e32d5dSMike Smith }
80215e32d5dSMike Smith 
80315e32d5dSMike Smith /*
80415e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
80515e32d5dSMike Smith  * it.
80615e32d5dSMike Smith  */
80715e32d5dSMike Smith static ACPI_STATUS
80815e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
80915e32d5dSMike Smith {
81015e32d5dSMike Smith     ACPI_OBJECT_TYPE	type;
81115e32d5dSMike Smith     device_t		child, bus = (device_t)context;
81215e32d5dSMike Smith 
8132a4ac806SMike Smith     FUNCTION_TRACE(__func__);
8140ae55423SMike Smith 
8150ae55423SMike Smith     /*
8160ae55423SMike Smith      * Skip this device if we think we'll have trouble with it.
8170ae55423SMike Smith      */
8180ae55423SMike Smith     if (acpi_avoid(handle))
8190ae55423SMike Smith 	return_ACPI_STATUS(AE_OK);
8200ae55423SMike Smith 
82115e32d5dSMike Smith     if (AcpiGetType(handle, &type) == AE_OK) {
82215e32d5dSMike Smith 	switch(type) {
82315e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
82415e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
82515e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
82615e32d5dSMike Smith 	case ACPI_TYPE_POWER:
8270ae55423SMike Smith 	    if (acpi_disabled("children"))
8280ae55423SMike Smith 		break;
82915e32d5dSMike Smith 	    /*
83015e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
83115e32d5dSMike Smith 	     * so that the probe/attach passes will run breadth-first.
83215e32d5dSMike Smith 	     */
8334c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
83415e32d5dSMike Smith 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
835bc0f2195SMike Smith 	    if (child == NULL)
836bc0f2195SMike Smith 		break;
83715e32d5dSMike Smith 	    acpi_set_handle(child, handle);
838bc0f2195SMike Smith 
839bc0f2195SMike Smith 	    /*
840b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
841b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
842b519f9d6SMike Smith 	     * the handle, but we don't probe it).
843b519f9d6SMike Smith 	     */
84423b4e251SMitsuru IWASAKI 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
845b519f9d6SMike Smith 		device_disable(child);
846b519f9d6SMike Smith 		break;
847b519f9d6SMike Smith 	    }
848b519f9d6SMike Smith 
849b519f9d6SMike Smith 	    /*
850bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
851bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
852bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
853bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
854bc0f2195SMike Smith 	     * device not to have any resources.
855bc0f2195SMike Smith 	     */
856bc0f2195SMike Smith 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
857bc0f2195SMike Smith 
858bc0f2195SMike Smith 	    /* if we're debugging, probe/attach now rather than later */
8590ae55423SMike Smith 	    DEBUG_EXEC(device_probe_and_attach(child));
8600ae55423SMike Smith 	    break;
86115e32d5dSMike Smith 	}
86215e32d5dSMike Smith     }
8630ae55423SMike Smith     return_ACPI_STATUS(AE_OK);
86415e32d5dSMike Smith }
86515e32d5dSMike Smith 
86615e32d5dSMike Smith static void
86715e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto)
86815e32d5dSMike Smith {
869cb9b0d80SMike Smith 
870cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
871cb9b0d80SMike Smith 
87215e32d5dSMike Smith     /*
8730ae55423SMike Smith      * Disable all ACPI events before soft off, otherwise the system
87415e32d5dSMike Smith      * will be turned on again on some laptops.
87515e32d5dSMike Smith      *
87615e32d5dSMike Smith      * XXX this should probably be restricted to masking some events just
87715e32d5dSMike Smith      *     before powering down, since we may still need ACPI during the
87815e32d5dSMike Smith      *     shutdown process.
87915e32d5dSMike Smith      */
88015e32d5dSMike Smith     acpi_Disable((struct acpi_softc *)arg);
88115e32d5dSMike Smith }
88215e32d5dSMike Smith 
88315e32d5dSMike Smith static void
88415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
88515e32d5dSMike Smith {
88615e32d5dSMike Smith     ACPI_STATUS	status;
88715e32d5dSMike Smith 
888cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
889cb9b0d80SMike Smith 
8905f3e4175SMitsuru IWASAKI     if (howto & RB_POWEROFF) {
89115e32d5dSMike Smith 	printf("Power system off using ACPI...\n");
892b9c780d6SMitsuru IWASAKI 	status = AcpiEnterSleepStatePrep(acpi_off_state);
893b9c780d6SMitsuru IWASAKI 	if (status != AE_OK) {
894b9c780d6SMitsuru IWASAKI 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
895b9c780d6SMitsuru IWASAKI 		   AcpiFormatException(status));
896b9c780d6SMitsuru IWASAKI 	    return;
897b9c780d6SMitsuru IWASAKI 	}
8982a4ac806SMike Smith 	if ((status = AcpiEnterSleepState(acpi_off_state)) != AE_OK) {
899bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
90015e32d5dSMike Smith 	} else {
90115e32d5dSMike Smith 	    DELAY(1000000);
90215e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
90315e32d5dSMike Smith 	}
90415e32d5dSMike Smith     }
90515e32d5dSMike Smith }
90615e32d5dSMike Smith 
90713d4f7baSMitsuru IWASAKI static void
90813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
90913d4f7baSMitsuru IWASAKI {
91013d4f7baSMitsuru IWASAKI     static int	first_time = 1;
91113d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
91213d4f7baSMitsuru IWASAKI 
913cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
914cb9b0d80SMike Smith 
91513d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
916cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
91743896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
91813d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
91913d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
92013d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_power_button_for_sleep, sc);
92113d4f7baSMitsuru IWASAKI 	if (first_time) {
92213d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
92313d4f7baSMitsuru IWASAKI 	}
92413d4f7baSMitsuru IWASAKI     }
925cb9b0d80SMike Smith     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
92643896e91SMike Smith 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
92713d4f7baSMitsuru IWASAKI 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
92813d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
92913d4f7baSMitsuru IWASAKI 				     acpi_eventhandler_sleep_button_for_sleep, sc);
93013d4f7baSMitsuru IWASAKI 	if (first_time) {
93113d4f7baSMitsuru IWASAKI 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
93213d4f7baSMitsuru IWASAKI 	}
93313d4f7baSMitsuru IWASAKI     }
93413d4f7baSMitsuru IWASAKI 
93513d4f7baSMitsuru IWASAKI     first_time = 0;
93613d4f7baSMitsuru IWASAKI }
93713d4f7baSMitsuru IWASAKI 
93815e32d5dSMike Smith /*
939c5ba0be4SMike Smith  * Returns true if the device is actually present and should
940c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
941c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
942c5ba0be4SMike Smith  */
943c5ba0be4SMike Smith BOOLEAN
944c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
945c5ba0be4SMike Smith {
946c5ba0be4SMike Smith     ACPI_HANDLE		h;
947c5ba0be4SMike Smith     ACPI_DEVICE_INFO	devinfo;
948c5ba0be4SMike Smith     ACPI_STATUS		error;
949c5ba0be4SMike Smith 
950cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
951cb9b0d80SMike Smith 
952c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
953c5ba0be4SMike Smith 	return(FALSE);
954c5ba0be4SMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
955c5ba0be4SMike Smith 	return(FALSE);
95643896e91SMike Smith     /* if no _STA method, must be present */
95743896e91SMike Smith     if (!(devinfo.Valid & ACPI_VALID_STA))
95843896e91SMike Smith 	return(TRUE);
95943896e91SMike Smith     /* return true for 'present' and 'functioning' */
96043896e91SMike Smith     if ((devinfo.CurrentStatus & 0x9) == 0x9)
961c5ba0be4SMike Smith 	return(TRUE);
962c5ba0be4SMike Smith     return(FALSE);
963c5ba0be4SMike Smith }
964c5ba0be4SMike Smith 
965c5ba0be4SMike Smith /*
96615e32d5dSMike Smith  * Match a HID string against a device
96715e32d5dSMike Smith  */
96815e32d5dSMike Smith BOOLEAN
96915e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid)
97015e32d5dSMike Smith {
97115e32d5dSMike Smith     ACPI_HANDLE		h;
97215e32d5dSMike Smith     ACPI_DEVICE_INFO	devinfo;
97315e32d5dSMike Smith     ACPI_STATUS		error;
974ed136da6SDoug Rabson     int			cid;
97515e32d5dSMike Smith 
976cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
977cb9b0d80SMike Smith 
9784d332989SMike Smith     if (hid == NULL)
97915e32d5dSMike Smith 	return(FALSE);
98015e32d5dSMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
98115e32d5dSMike Smith 	return(FALSE);
98215e32d5dSMike Smith     if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
98315e32d5dSMike Smith 	return(FALSE);
9844d332989SMike Smith     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
98515e32d5dSMike Smith 	return(TRUE);
986ed136da6SDoug Rabson     if ((error = acpi_EvaluateInteger(h, "_CID", &cid)) != AE_OK)
987ed136da6SDoug Rabson 	return(FALSE);
988ed136da6SDoug Rabson     if (cid == PNP_EISAID(hid))
989ed136da6SDoug Rabson 	return(TRUE);
99015e32d5dSMike Smith     return(FALSE);
99115e32d5dSMike Smith }
99215e32d5dSMike Smith 
99315e32d5dSMike Smith /*
994c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
995c5ba0be4SMike Smith  * or one if its parents.
996c5ba0be4SMike Smith  */
997c5ba0be4SMike Smith ACPI_STATUS
998c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
999c5ba0be4SMike Smith {
1000c5ba0be4SMike Smith     ACPI_HANDLE		r;
1001c5ba0be4SMike Smith     ACPI_STATUS		status;
1002c5ba0be4SMike Smith 
1003cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1004cb9b0d80SMike Smith 
1005c5ba0be4SMike Smith     /* walk back up the tree to the root */
1006c5ba0be4SMike Smith     for (;;) {
1007c5ba0be4SMike Smith 	status = AcpiGetHandle(parent, path, &r);
1008c5ba0be4SMike Smith 	if (status == AE_OK) {
1009c5ba0be4SMike Smith 	    *result = r;
1010c5ba0be4SMike Smith 	    return(AE_OK);
1011c5ba0be4SMike Smith 	}
1012c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1013c5ba0be4SMike Smith 	    return(AE_OK);
1014c5ba0be4SMike Smith 	if (AcpiGetParent(parent, &r) != AE_OK)
1015c5ba0be4SMike Smith 	    return(AE_NOT_FOUND);
1016c5ba0be4SMike Smith 	parent = r;
1017c5ba0be4SMike Smith     }
1018c5ba0be4SMike Smith }
1019c5ba0be4SMike Smith 
1020c5ba0be4SMike Smith /*
1021c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1022c5ba0be4SMike Smith  */
1023c5ba0be4SMike Smith ACPI_BUFFER *
1024c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1025c5ba0be4SMike Smith {
1026c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1027c5ba0be4SMike Smith 
1028c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1029c5ba0be4SMike Smith 	return(NULL);
1030c5ba0be4SMike Smith     buf->Length = size;
1031c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1032c5ba0be4SMike Smith     return(buf);
1033c5ba0be4SMike Smith }
1034c5ba0be4SMike Smith 
1035c5ba0be4SMike Smith /*
103615e32d5dSMike Smith  * Perform the tedious double-get procedure required for fetching something into
103715e32d5dSMike Smith  * an ACPI_BUFFER that has not been initialised.
103815e32d5dSMike Smith  */
103915e32d5dSMike Smith ACPI_STATUS
104015e32d5dSMike Smith acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf)
104115e32d5dSMike Smith {
104215e32d5dSMike Smith     ACPI_STATUS	status;
104315e32d5dSMike Smith 
1044cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1045cb9b0d80SMike Smith 
104615e32d5dSMike Smith     buf->Length = 0;
104715e32d5dSMike Smith     buf->Pointer = NULL;
104815e32d5dSMike Smith 
104915e32d5dSMike Smith     if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW)
105015e32d5dSMike Smith 	return(status);
105115e32d5dSMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
105215e32d5dSMike Smith 	return(AE_NO_MEMORY);
105315e32d5dSMike Smith     return(func(handle, buf));
105415e32d5dSMike Smith }
105515e32d5dSMike Smith 
105615e32d5dSMike Smith /*
10577d3bcec9SMike Smith  * Perform the tedious double-get procedure required for fetching a table into
10587d3bcec9SMike Smith  * an ACPI_BUFFER that has not been initialised.
10597d3bcec9SMike Smith  */
10607d3bcec9SMike Smith ACPI_STATUS
10617d3bcec9SMike Smith acpi_GetTableIntoBuffer(ACPI_TABLE_TYPE table, UINT32 instance, ACPI_BUFFER *buf)
10627d3bcec9SMike Smith {
10637d3bcec9SMike Smith     ACPI_STATUS	status;
10647d3bcec9SMike Smith 
10657d3bcec9SMike Smith     ACPI_ASSERTLOCK;
10667d3bcec9SMike Smith 
10677d3bcec9SMike Smith     buf->Length = 0;
10687d3bcec9SMike Smith     buf->Pointer = NULL;
10697d3bcec9SMike Smith 
10707d3bcec9SMike Smith     if ((status = AcpiGetTable(table, instance, buf)) != AE_BUFFER_OVERFLOW)
10717d3bcec9SMike Smith 	return(status);
10727d3bcec9SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
10737d3bcec9SMike Smith 	return(AE_NO_MEMORY);
10747d3bcec9SMike Smith     return(AcpiGetTable(table, instance, buf));
10757d3bcec9SMike Smith }
10767d3bcec9SMike Smith 
10777d3bcec9SMike Smith /*
1078c5ba0be4SMike Smith  * Perform the tedious double-evaluate procedure for evaluating something into
1079c5ba0be4SMike Smith  * an ACPI_BUFFER that has not been initialised.  Note that this evaluates
1080c5ba0be4SMike Smith  * twice, so avoid applying this to things that may have side-effects.
1081c5ba0be4SMike Smith  *
1082c5ba0be4SMike Smith  * This is like AcpiEvaluateObject with automatic buffer allocation.
108315e32d5dSMike Smith  */
1084c5ba0be4SMike Smith ACPI_STATUS
1085c5ba0be4SMike Smith acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, ACPI_OBJECT_LIST *params,
1086c5ba0be4SMike Smith 			ACPI_BUFFER *buf)
108715e32d5dSMike Smith {
1088c5ba0be4SMike Smith     ACPI_STATUS	status;
108915e32d5dSMike Smith 
1090cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1091cb9b0d80SMike Smith 
1092c5ba0be4SMike Smith     buf->Length = 0;
1093c5ba0be4SMike Smith     buf->Pointer = NULL;
1094c5ba0be4SMike Smith 
1095c5ba0be4SMike Smith     if ((status = AcpiEvaluateObject(object, pathname, params, buf)) != AE_BUFFER_OVERFLOW)
1096c5ba0be4SMike Smith 	return(status);
1097c5ba0be4SMike Smith     if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
1098c5ba0be4SMike Smith 	return(AE_NO_MEMORY);
1099c5ba0be4SMike Smith     return(AcpiEvaluateObject(object, pathname, params, buf));
110015e32d5dSMike Smith }
110115e32d5dSMike Smith 
1102c5ba0be4SMike Smith /*
1103c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1104c5ba0be4SMike Smith  */
1105c5ba0be4SMike Smith ACPI_STATUS
1106c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
1107c5ba0be4SMike Smith {
1108c5ba0be4SMike Smith     ACPI_STATUS	error;
1109c5ba0be4SMike Smith     ACPI_BUFFER	buf;
11100c7da7acSJohn Baldwin     ACPI_OBJECT	param, *p;
11110c7da7acSJohn Baldwin     int		i;
1112c5ba0be4SMike Smith 
1113cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1114cb9b0d80SMike Smith 
1115c5ba0be4SMike Smith     if (handle == NULL)
1116c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
11170c7da7acSJohn Baldwin 
11180c7da7acSJohn Baldwin     /*
11190c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
11200c7da7acSJohn Baldwin      * a method that will return an Integer.
11210c7da7acSJohn Baldwin      */
1122c5ba0be4SMike Smith     buf.Pointer = &param;
1123c5ba0be4SMike Smith     buf.Length = sizeof(param);
1124c5ba0be4SMike Smith     if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
1125c5ba0be4SMike Smith 	if (param.Type == ACPI_TYPE_INTEGER) {
1126c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1127c5ba0be4SMike Smith 	} else {
1128c5ba0be4SMike Smith 	    error = AE_TYPE;
1129c5ba0be4SMike Smith 	}
1130c5ba0be4SMike Smith     }
11310c7da7acSJohn Baldwin 
11320c7da7acSJohn Baldwin     /*
11330c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
11340c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
11350c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
11360c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
11370c7da7acSJohn Baldwin      *
11380c7da7acSJohn Baldwin      * This is a hack.
11390c7da7acSJohn Baldwin      */
11400c7da7acSJohn Baldwin     if (error == AE_BUFFER_OVERFLOW) {
11410c7da7acSJohn Baldwin 	if ((buf.Pointer = AcpiOsCallocate(buf.Length)) == NULL) {
11420c7da7acSJohn Baldwin 	    error = AE_NO_MEMORY;
11430c7da7acSJohn Baldwin 	} else {
11440c7da7acSJohn Baldwin 	    if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
11450c7da7acSJohn Baldwin 		p = (ACPI_OBJECT *)buf.Pointer;
11460c7da7acSJohn Baldwin 		if (p->Type != ACPI_TYPE_BUFFER) {
11470c7da7acSJohn Baldwin 		    error = AE_TYPE;
11480c7da7acSJohn Baldwin 		} else {
11490c7da7acSJohn Baldwin 		    if (p->Buffer.Length > sizeof(int)) {
11500c7da7acSJohn Baldwin 			error = AE_BAD_DATA;
11510c7da7acSJohn Baldwin 		    } else {
11520c7da7acSJohn Baldwin 			*number = 0;
11530c7da7acSJohn Baldwin 			for (i = 0; i < p->Buffer.Length; i++)
11540c7da7acSJohn Baldwin 			    *number += (*(p->Buffer.Pointer + i) << (i * 8));
11550c7da7acSJohn Baldwin 		    }
11560c7da7acSJohn Baldwin 		}
11570c7da7acSJohn Baldwin 	    }
11580c7da7acSJohn Baldwin 	}
11590c7da7acSJohn Baldwin 	AcpiOsFree(buf.Pointer);
11600c7da7acSJohn Baldwin     }
1161c5ba0be4SMike Smith     return(error);
1162c5ba0be4SMike Smith }
1163c5ba0be4SMike Smith 
1164c5ba0be4SMike Smith /*
1165c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1166c5ba0be4SMike Smith  * function for each element.
1167c5ba0be4SMike Smith  *
1168c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1169c5ba0be4SMike Smith  */
1170c5ba0be4SMike Smith ACPI_STATUS
1171c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
1172c5ba0be4SMike Smith {
1173c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1174c5ba0be4SMike Smith     int		i;
1175c5ba0be4SMike Smith 
1176c5ba0be4SMike Smith     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
1177c5ba0be4SMike Smith 	return(AE_BAD_PARAMETER);
1178c5ba0be4SMike Smith 
1179c5ba0be4SMike Smith     /* iterate over components */
1180c5ba0be4SMike Smith     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
1181c5ba0be4SMike Smith 	func(comp, arg);
1182c5ba0be4SMike Smith 
1183c5ba0be4SMike Smith     return(AE_OK);
1184c5ba0be4SMike Smith }
1185c5ba0be4SMike Smith 
11866f69255bSMike Smith /*
11876f69255bSMike Smith  * Find the (index)th resource object in a set.
11886f69255bSMike Smith  */
11896f69255bSMike Smith ACPI_STATUS
11906d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
11916f69255bSMike Smith {
11926d63101aSMike Smith     ACPI_RESOURCE	*rp;
11936f69255bSMike Smith     int			i;
11946f69255bSMike Smith 
11956d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
11966f69255bSMike Smith     i = index;
11976d63101aSMike Smith     while (i-- > 0) {
11986f69255bSMike Smith 	/* range check */
11996d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12006f69255bSMike Smith 	    return(AE_BAD_PARAMETER);
12016d63101aSMike Smith 	/* check for terminator */
12026d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12036d63101aSMike Smith 	    (rp->Length == 0))
12046d63101aSMike Smith 	    return(AE_NOT_FOUND);
12056d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12066f69255bSMike Smith     }
12076f69255bSMike Smith     if (resp != NULL)
12086d63101aSMike Smith 	*resp = rp;
12096d63101aSMike Smith     return(AE_OK);
12106d63101aSMike Smith }
12116d63101aSMike Smith 
12126d63101aSMike Smith /*
12136d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
12146d63101aSMike Smith  *
12156d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
12166d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
12176d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
12186d63101aSMike Smith  * resources.
12196d63101aSMike Smith  */
12206d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
12216d63101aSMike Smith 
12226d63101aSMike Smith ACPI_STATUS
12236d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
12246d63101aSMike Smith {
12256d63101aSMike Smith     ACPI_RESOURCE	*rp;
12266d63101aSMike Smith     void		*newp;
12276d63101aSMike Smith 
12286d63101aSMike Smith     /*
12296d63101aSMike Smith      * Initialise the buffer if necessary.
12306d63101aSMike Smith      */
12316d63101aSMike Smith     if (buf->Pointer == NULL) {
12326d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
12336d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
12346d63101aSMike Smith 	    return(AE_NO_MEMORY);
12356d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
12366d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
12376d63101aSMike Smith 	rp->Length = 0;
12386d63101aSMike Smith     }
12396d63101aSMike Smith     if (res == NULL)
12406d63101aSMike Smith 	return(AE_OK);
12416d63101aSMike Smith 
12426d63101aSMike Smith     /*
12436d63101aSMike Smith      * Scan the current buffer looking for the terminator.
12446d63101aSMike Smith      * This will either find the terminator or hit the end
12456d63101aSMike Smith      * of the buffer and return an error.
12466d63101aSMike Smith      */
12476d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
12486d63101aSMike Smith     for (;;) {
12496d63101aSMike Smith 	/* range check, don't go outside the buffer */
12506d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
12516d63101aSMike Smith 	    return(AE_BAD_PARAMETER);
12526d63101aSMike Smith 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
12536d63101aSMike Smith 	    (rp->Length == 0)) {
12546d63101aSMike Smith 	    break;
12556d63101aSMike Smith 	}
12566d63101aSMike Smith 	rp = ACPI_RESOURCE_NEXT(rp);
12576d63101aSMike Smith     }
12586d63101aSMike Smith 
12596d63101aSMike Smith     /*
12606d63101aSMike Smith      * Check the size of the buffer and expand if required.
12616d63101aSMike Smith      *
12626d63101aSMike Smith      * Required size is:
12636d63101aSMike Smith      *	size of existing resources before terminator +
12646d63101aSMike Smith      *	size of new resource and header +
12656d63101aSMike Smith      * 	size of terminator.
12666d63101aSMike Smith      *
12676d63101aSMike Smith      * Note that this loop should really only run once, unless
12686d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
12696d63101aSMike Smith      */
12706d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
12716d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
12726d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
12736d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
12746d63101aSMike Smith 	    return(AE_NO_MEMORY);
12756d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1276a692219dSMike Smith         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1277a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
12786d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
12796d63101aSMike Smith 	buf->Pointer = newp;
12806d63101aSMike Smith 	buf->Length += buf->Length;
12816d63101aSMike Smith     }
12826d63101aSMike Smith 
12836d63101aSMike Smith     /*
12846d63101aSMike Smith      * Insert the new resource.
12856d63101aSMike Smith      */
12866d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
12876d63101aSMike Smith 
12886d63101aSMike Smith     /*
12896d63101aSMike Smith      * And add the terminator.
12906d63101aSMike Smith      */
12916d63101aSMike Smith     rp = ACPI_RESOURCE_NEXT(rp);
12926d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
12936d63101aSMike Smith     rp->Length = 0;
12946d63101aSMike Smith 
12956f69255bSMike Smith     return(AE_OK);
12966f69255bSMike Smith }
1297c5ba0be4SMike Smith 
1298c30382dfSMitsuru IWASAKI 
129915e32d5dSMike Smith /*
130015e32d5dSMike Smith  * Set the system sleep state
130115e32d5dSMike Smith  *
130215e32d5dSMike Smith  * Currently we only support S1 and S5
130315e32d5dSMike Smith  */
130415e32d5dSMike Smith ACPI_STATUS
130515e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
130615e32d5dSMike Smith {
130715e32d5dSMike Smith     ACPI_STATUS	status = AE_OK;
130856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
130956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
131015e32d5dSMike Smith 
13112a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
1312cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
13130ae55423SMike Smith 
13146397624dSMitsuru IWASAKI     if (sc->acpi_sstate != ACPI_STATE_S0)
13156397624dSMitsuru IWASAKI 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
13166397624dSMitsuru IWASAKI 
131715e32d5dSMike Smith     switch (state) {
131815e32d5dSMike Smith     case ACPI_STATE_S0:	/* XXX only for testing */
131991467fc6SMike Smith 	status = AcpiEnterSleepState((UINT8)state);
132015e32d5dSMike Smith 	if (status != AE_OK) {
1321bfae45aaSMike Smith 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
132215e32d5dSMike Smith 	}
132315e32d5dSMike Smith 	break;
132415e32d5dSMike Smith 
132515e32d5dSMike Smith     case ACPI_STATE_S1:
13266161544cSTakanori Watanabe     case ACPI_STATE_S2:
13276161544cSTakanori Watanabe     case ACPI_STATE_S3:
13286161544cSTakanori Watanabe     case ACPI_STATE_S4:
132956d8cb57SMitsuru IWASAKI 	status = AcpiHwObtainSleepTypeRegisterData((UINT8)state, &TypeA, &TypeB);
133056d8cb57SMitsuru IWASAKI 	if (status != AE_OK) {
133156d8cb57SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiHwObtainSleepTypeRegisterData failed - %s\n", AcpiFormatException(status));
133256d8cb57SMitsuru IWASAKI 	    break;
133356d8cb57SMitsuru IWASAKI 	}
133456d8cb57SMitsuru IWASAKI 
133515e32d5dSMike Smith 	/*
133615e32d5dSMike Smith 	 * Inform all devices that we are going to sleep.
133715e32d5dSMike Smith 	 */
133815e32d5dSMike Smith 	if (DEVICE_SUSPEND(root_bus) != 0) {
133915e32d5dSMike Smith 	    /*
134015e32d5dSMike Smith 	     * Re-wake the system.
134115e32d5dSMike Smith 	     *
134215e32d5dSMike Smith 	     * XXX note that a better two-pass approach with a 'veto' pass
134315e32d5dSMike Smith 	     *     followed by a "real thing" pass would be better, but the
134415e32d5dSMike Smith 	     *     current bus interface does not provide for this.
134515e32d5dSMike Smith 	     */
134615e32d5dSMike Smith 	    DEVICE_RESUME(root_bus);
13470ae55423SMike Smith 	    return_ACPI_STATUS(AE_ERROR);
134815e32d5dSMike Smith 	}
1349b9c780d6SMitsuru IWASAKI 
1350b9c780d6SMitsuru IWASAKI 	status = AcpiEnterSleepStatePrep(state);
1351b9c780d6SMitsuru IWASAKI 	if (status != AE_OK) {
1352b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1353b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
1354b9c780d6SMitsuru IWASAKI 	    break;
1355b9c780d6SMitsuru IWASAKI 	}
1356b9c780d6SMitsuru IWASAKI 
135715e32d5dSMike Smith 	sc->acpi_sstate = state;
13586161544cSTakanori Watanabe 
13596161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
13606161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
13616161544cSTakanori Watanabe 
13626161544cSTakanori Watanabe 	    /* AcpiEnterSleepState() maybe incompleted, unlock here. */
13636161544cSTakanori Watanabe 	    AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
13646161544cSTakanori Watanabe 
13656161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
1366964679ceSMitsuru IWASAKI 	    if (state == ACPI_STATE_S4) {
1367964679ceSMitsuru IWASAKI 		AcpiEnable();
13686161544cSTakanori Watanabe 	    }
13696161544cSTakanori Watanabe 	} else {
137091467fc6SMike Smith 	    status = AcpiEnterSleepState((UINT8)state);
137115e32d5dSMike Smith 	    if (status != AE_OK) {
1372bfae45aaSMike Smith 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
1373c30382dfSMitsuru IWASAKI 		break;
137415e32d5dSMike Smith 	    }
13756161544cSTakanori Watanabe 	}
1376ff741befSTakanori Watanabe 	AcpiLeaveSleepState((UINT8)state);
137715e32d5dSMike Smith 	DEVICE_RESUME(root_bus);
137815e32d5dSMike Smith 	sc->acpi_sstate = ACPI_STATE_S0;
137913d4f7baSMitsuru IWASAKI 	acpi_enable_fixed_events(sc);
138015e32d5dSMike Smith 	break;
138115e32d5dSMike Smith 
138215e32d5dSMike Smith     case ACPI_STATE_S5:
138315e32d5dSMike Smith 	/*
138415e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
138515e32d5dSMike Smith 	 * shutdown handlers.
138615e32d5dSMike Smith 	 */
138715e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
138815e32d5dSMike Smith 	break;
138915e32d5dSMike Smith 
139015e32d5dSMike Smith     default:
139115e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
139215e32d5dSMike Smith 	break;
139315e32d5dSMike Smith     }
13940ae55423SMike Smith     return_ACPI_STATUS(status);
139515e32d5dSMike Smith }
139615e32d5dSMike Smith 
139715e32d5dSMike Smith /*
139815e32d5dSMike Smith  * Enable/Disable ACPI
139915e32d5dSMike Smith  */
140015e32d5dSMike Smith ACPI_STATUS
140115e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc)
140215e32d5dSMike Smith {
140315e32d5dSMike Smith     ACPI_STATUS	status;
140415e32d5dSMike Smith     u_int32_t	flags;
140515e32d5dSMike Smith 
14062a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1407cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14080ae55423SMike Smith 
140915e32d5dSMike Smith     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
141015e32d5dSMike Smith             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
141115e32d5dSMike Smith     if (!sc->acpi_enabled) {
141215e32d5dSMike Smith 	status = AcpiEnableSubsystem(flags);
141315e32d5dSMike Smith     } else {
141415e32d5dSMike Smith 	status = AE_OK;
141515e32d5dSMike Smith     }
141615e32d5dSMike Smith     if (status == AE_OK)
141715e32d5dSMike Smith 	sc->acpi_enabled = 1;
14180ae55423SMike Smith     return_ACPI_STATUS(status);
141915e32d5dSMike Smith }
142015e32d5dSMike Smith 
142115e32d5dSMike Smith ACPI_STATUS
142215e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc)
142315e32d5dSMike Smith {
142415e32d5dSMike Smith     ACPI_STATUS	status;
142515e32d5dSMike Smith 
14262a4ac806SMike Smith     FUNCTION_TRACE(__func__);
1427cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
14280ae55423SMike Smith 
142915e32d5dSMike Smith     if (sc->acpi_enabled) {
143015e32d5dSMike Smith 	status = AcpiDisable();
143115e32d5dSMike Smith     } else {
143215e32d5dSMike Smith 	status = AE_OK;
143315e32d5dSMike Smith     }
143415e32d5dSMike Smith     if (status == AE_OK)
143515e32d5dSMike Smith 	sc->acpi_enabled = 0;
14360ae55423SMike Smith     return_ACPI_STATUS(status);
143715e32d5dSMike Smith }
143815e32d5dSMike Smith 
143915e32d5dSMike Smith /*
144015e32d5dSMike Smith  * ACPI Event Handlers
144115e32d5dSMike Smith  */
144215e32d5dSMike Smith 
144315e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
144415e32d5dSMike Smith 
144515e32d5dSMike Smith static void
144615e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
144715e32d5dSMike Smith {
14482a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
144915e32d5dSMike Smith 
1450cb9b0d80SMike Smith     ACPI_LOCK;
1451a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
145215e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
1453cb9b0d80SMike Smith     ACPI_UNLOCK;
14540ae55423SMike Smith     return_VOID;
145515e32d5dSMike Smith }
145615e32d5dSMike Smith 
145715e32d5dSMike Smith static void
145815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
145915e32d5dSMike Smith {
14602a4ac806SMike Smith     FUNCTION_TRACE_U32(__func__, state);
14610ae55423SMike Smith 
146215e32d5dSMike Smith     /* Well, what to do? :-) */
14630ae55423SMike Smith 
1464cb9b0d80SMike Smith     ACPI_LOCK;
1465cb9b0d80SMike Smith     ACPI_UNLOCK;
1466cb9b0d80SMike Smith 
14670ae55423SMike Smith     return_VOID;
146815e32d5dSMike Smith }
146915e32d5dSMike Smith 
147015e32d5dSMike Smith /*
147115e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
147215e32d5dSMike Smith  */
147315e32d5dSMike Smith UINT32
147415e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context)
147515e32d5dSMike Smith {
147615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
147715e32d5dSMike Smith 
14782a4ac806SMike Smith     FUNCTION_TRACE(__func__);
14790ae55423SMike Smith 
148015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
14810ae55423SMike Smith 
14820ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
148315e32d5dSMike Smith }
148415e32d5dSMike Smith 
148515e32d5dSMike Smith UINT32
148615e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context)
148715e32d5dSMike Smith {
148815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
148915e32d5dSMike Smith 
14902a4ac806SMike Smith     FUNCTION_TRACE(__func__);
14910ae55423SMike Smith 
149215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
14930ae55423SMike Smith 
14940ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
149515e32d5dSMike Smith }
149615e32d5dSMike Smith 
149715e32d5dSMike Smith UINT32
149815e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context)
149915e32d5dSMike Smith {
150015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
150115e32d5dSMike Smith 
15022a4ac806SMike Smith     FUNCTION_TRACE(__func__);
15030ae55423SMike Smith 
150415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
15050ae55423SMike Smith 
15060ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
150715e32d5dSMike Smith }
150815e32d5dSMike Smith 
150915e32d5dSMike Smith UINT32
151015e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context)
151115e32d5dSMike Smith {
151215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
151315e32d5dSMike Smith 
15142a4ac806SMike Smith     FUNCTION_TRACE(__func__);
15150ae55423SMike Smith 
151615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
15170ae55423SMike Smith 
15180ae55423SMike Smith     return_VALUE(INTERRUPT_HANDLED);
151915e32d5dSMike Smith }
152015e32d5dSMike Smith 
152115e32d5dSMike Smith /*
152215e32d5dSMike Smith  * XXX This is kinda ugly, and should not be here.
152315e32d5dSMike Smith  */
152415e32d5dSMike Smith struct acpi_staticbuf {
152515e32d5dSMike Smith     ACPI_BUFFER	buffer;
152615e32d5dSMike Smith     char	data[512];
152715e32d5dSMike Smith };
152815e32d5dSMike Smith 
152915e32d5dSMike Smith char *
153015e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
153115e32d5dSMike Smith {
153215e32d5dSMike Smith     static struct acpi_staticbuf	buf;
153315e32d5dSMike Smith 
1534cb9b0d80SMike Smith     ACPI_ASSERTLOCK;
1535cb9b0d80SMike Smith 
153615e32d5dSMike Smith     buf.buffer.Length = 512;
153715e32d5dSMike Smith     buf.buffer.Pointer = &buf.data[0];
153815e32d5dSMike Smith 
153915e32d5dSMike Smith     if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK)
154015e32d5dSMike Smith 	return(buf.buffer.Pointer);
154115e32d5dSMike Smith     return("(unknown path)");
154215e32d5dSMike Smith }
154315e32d5dSMike Smith 
154415e32d5dSMike Smith /*
154515e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
154615e32d5dSMike Smith  * parts of the namespace.
154715e32d5dSMike Smith  */
154815e32d5dSMike Smith int
154915e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
155015e32d5dSMike Smith {
155115e32d5dSMike Smith     char	*cp, *np;
155215e32d5dSMike Smith     int		len;
155315e32d5dSMike Smith 
155415e32d5dSMike Smith     np = acpi_name(handle);
155515e32d5dSMike Smith     if (*np == '\\')
155615e32d5dSMike Smith 	np++;
155715e32d5dSMike Smith     if ((cp = getenv("debug.acpi.avoid")) == NULL)
155815e32d5dSMike Smith 	return(0);
155915e32d5dSMike Smith 
156015e32d5dSMike Smith     /* scan the avoid list checking for a match */
156115e32d5dSMike Smith     for (;;) {
156215e32d5dSMike Smith 	while ((*cp != 0) && isspace(*cp))
156315e32d5dSMike Smith 	    cp++;
156415e32d5dSMike Smith 	if (*cp == 0)
156515e32d5dSMike Smith 	    break;
156615e32d5dSMike Smith 	len = 0;
156715e32d5dSMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
156815e32d5dSMike Smith 	    len++;
15694c1cdee6SMike Smith 	if (!strncmp(cp, np, len))
15700ae55423SMike Smith 	    return(1);
15710ae55423SMike Smith 	cp += len;
15720ae55423SMike Smith     }
15730ae55423SMike Smith     return(0);
15740ae55423SMike Smith }
15750ae55423SMike Smith 
15760ae55423SMike Smith /*
15770ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
15780ae55423SMike Smith  */
15790ae55423SMike Smith int
15800ae55423SMike Smith acpi_disabled(char *subsys)
15810ae55423SMike Smith {
15820ae55423SMike Smith     char	*cp;
15830ae55423SMike Smith     int		len;
15840ae55423SMike Smith 
15850ae55423SMike Smith     if ((cp = getenv("debug.acpi.disable")) == NULL)
15860ae55423SMike Smith 	return(0);
15870ae55423SMike Smith     if (!strcmp(cp, "all"))
15880ae55423SMike Smith 	return(1);
15890ae55423SMike Smith 
15900ae55423SMike Smith     /* scan the disable list checking for a match */
15910ae55423SMike Smith     for (;;) {
15920ae55423SMike Smith 	while ((*cp != 0) && isspace(*cp))
15930ae55423SMike Smith 	    cp++;
15940ae55423SMike Smith 	if (*cp == 0)
15950ae55423SMike Smith 	    break;
15960ae55423SMike Smith 	len = 0;
15970ae55423SMike Smith 	while ((cp[len] != 0) && !isspace(cp[len]))
15980ae55423SMike Smith 	    len++;
15994c1cdee6SMike Smith 	if (!strncmp(cp, subsys, len))
160015e32d5dSMike Smith 	    return(1);
160115e32d5dSMike Smith 	cp += len;
160215e32d5dSMike Smith     }
160315e32d5dSMike Smith     return(0);
160415e32d5dSMike Smith }
160515e32d5dSMike Smith 
160615e32d5dSMike Smith /*
160715e32d5dSMike Smith  * Control interface.
160815e32d5dSMike Smith  *
16090ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
16100ae55423SMike Smith  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
16110ae55423SMike Smith  * interface to make their handlers visible.
161215e32d5dSMike Smith  */
16130ae55423SMike Smith struct acpi_ioctl_hook
16140ae55423SMike Smith {
16150ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook)	link;
16160ae55423SMike Smith     u_long				cmd;
16170ae55423SMike Smith     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
16180ae55423SMike Smith     void				*arg;
16190ae55423SMike Smith };
16200ae55423SMike Smith 
16210ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
16220ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
16230ae55423SMike Smith 
16240ae55423SMike Smith /*
16250ae55423SMike Smith  * Register an ioctl handler.
16260ae55423SMike Smith  */
16270ae55423SMike Smith int
16280ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
16290ae55423SMike Smith {
16300ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16310ae55423SMike Smith 
16320ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
16330ae55423SMike Smith 	return(ENOMEM);
16340ae55423SMike Smith     hp->cmd = cmd;
16350ae55423SMike Smith     hp->fn = fn;
16360ae55423SMike Smith     hp->arg = arg;
16370ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
16380ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
16390ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
16400ae55423SMike Smith     }
16410ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
16420ae55423SMike Smith     return(0);
16430ae55423SMike Smith }
16440ae55423SMike Smith 
16450ae55423SMike Smith /*
16460ae55423SMike Smith  * Deregister an ioctl handler.
16470ae55423SMike Smith  */
16480ae55423SMike Smith void
16490ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
16500ae55423SMike Smith {
16510ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16520ae55423SMike Smith 
16530ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
16540ae55423SMike Smith 	if ((hp->cmd == cmd) && (hp->fn == fn))
16550ae55423SMike Smith 	    break;
16560ae55423SMike Smith 
16570ae55423SMike Smith     if (hp != NULL) {
16580ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
16590ae55423SMike Smith 	free(hp, M_ACPIDEV);
16600ae55423SMike Smith     }
16610ae55423SMike Smith }
16620ae55423SMike Smith 
166315e32d5dSMike Smith static int
1664b40ce416SJulian Elischer acpiopen(dev_t dev, int flag, int fmt, struct thread *td)
166515e32d5dSMike Smith {
166615e32d5dSMike Smith     return(0);
166715e32d5dSMike Smith }
166815e32d5dSMike Smith 
166915e32d5dSMike Smith static int
1670b40ce416SJulian Elischer acpiclose(dev_t dev, int flag, int fmt, struct thread *td)
167115e32d5dSMike Smith {
167215e32d5dSMike Smith     return(0);
167315e32d5dSMike Smith }
167415e32d5dSMike Smith 
167515e32d5dSMike Smith static int
1676b40ce416SJulian Elischer acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
167715e32d5dSMike Smith {
167815e32d5dSMike Smith     struct acpi_softc		*sc;
16790ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
16800ae55423SMike Smith     int				error, xerror, state;
168115e32d5dSMike Smith 
1682cb9b0d80SMike Smith     ACPI_LOCK;
1683cb9b0d80SMike Smith 
168415e32d5dSMike Smith     error = state = 0;
168515e32d5dSMike Smith     sc = dev->si_drv1;
168615e32d5dSMike Smith 
16870ae55423SMike Smith     /*
16880ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
16890ae55423SMike Smith      */
16900ae55423SMike Smith     if (acpi_ioctl_hooks_initted) {
16910ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
16920ae55423SMike Smith 	    if (hp->cmd == cmd) {
16930ae55423SMike Smith 		xerror = hp->fn(cmd, addr, hp->arg);
16940ae55423SMike Smith 		if (xerror != 0)
16950ae55423SMike Smith 		    error = xerror;
1696917d44c8SMitsuru IWASAKI 		goto out;
16970ae55423SMike Smith 	    }
16980ae55423SMike Smith 	}
16990ae55423SMike Smith     }
17000ae55423SMike Smith 
17010ae55423SMike Smith     /*
17020ae55423SMike Smith      * Core system ioctls.
17030ae55423SMike Smith      */
170415e32d5dSMike Smith     switch (cmd) {
170515e32d5dSMike Smith     case ACPIIO_ENABLE:
17060ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Enable(sc)))
170715e32d5dSMike Smith 	    error = ENXIO;
170815e32d5dSMike Smith 	break;
170915e32d5dSMike Smith 
171015e32d5dSMike Smith     case ACPIIO_DISABLE:
17110ae55423SMike Smith 	if (ACPI_FAILURE(acpi_Disable(sc)))
171215e32d5dSMike Smith 	    error = ENXIO;
171315e32d5dSMike Smith 	break;
171415e32d5dSMike Smith 
171515e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
171615e32d5dSMike Smith 	if (!sc->acpi_enabled) {
171715e32d5dSMike Smith 	    error = ENXIO;
171815e32d5dSMike Smith 	    break;
171915e32d5dSMike Smith 	}
172015e32d5dSMike Smith 	state = *(int *)addr;
1721a5d1879bSMitsuru IWASAKI 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
172215e32d5dSMike Smith 	    acpi_SetSleepState(sc, state);
172315e32d5dSMike Smith 	} else {
172415e32d5dSMike Smith 	    error = EINVAL;
172515e32d5dSMike Smith 	}
172615e32d5dSMike Smith 	break;
172715e32d5dSMike Smith 
172815e32d5dSMike Smith     default:
17290ae55423SMike Smith 	if (error == 0)
173015e32d5dSMike Smith 	    error = EINVAL;
173115e32d5dSMike Smith 	break;
173215e32d5dSMike Smith     }
1733917d44c8SMitsuru IWASAKI 
1734917d44c8SMitsuru IWASAKI out:
1735cb9b0d80SMike Smith     ACPI_UNLOCK;
173615e32d5dSMike Smith     return(error);
173715e32d5dSMike Smith }
173815e32d5dSMike Smith 
17391d073b1dSJohn Baldwin static int
17401d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
17411d073b1dSJohn Baldwin {
17421d073b1dSJohn Baldwin     char sleep_state[10];
17431d073b1dSJohn Baldwin     int error;
17441d073b1dSJohn Baldwin     u_int new_state, old_state;
17451d073b1dSJohn Baldwin 
17461d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
1747b8670bedSMitsuru IWASAKI     if (old_state > ACPI_S_STATES_MAX+1) {
17481d073b1dSJohn Baldwin 	strcpy(sleep_state, "unknown");
1749cb9b0d80SMike Smith     } else {
1750b8670bedSMitsuru IWASAKI 	bzero(sleep_state, sizeof(sleep_state));
17511d073b1dSJohn Baldwin 	strncpy(sleep_state, sleep_state_names[old_state],
17521d073b1dSJohn Baldwin 		sizeof(sleep_state_names[old_state]));
1753cb9b0d80SMike Smith     }
17541d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
17551d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
1756b8670bedSMitsuru IWASAKI 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
17571d073b1dSJohn Baldwin 	    if (strncmp(sleep_state, sleep_state_names[new_state],
17581d073b1dSJohn Baldwin 			sizeof(sleep_state)) == 0)
17591d073b1dSJohn Baldwin 		break;
1760cb9b0d80SMike Smith 	}
1761b8670bedSMitsuru IWASAKI 	if ((new_state != old_state) && (new_state <= ACPI_S_STATES_MAX+1)) {
17621d073b1dSJohn Baldwin 	    *(u_int *)oidp->oid_arg1 = new_state;
1763cb9b0d80SMike Smith 	} else {
17641d073b1dSJohn Baldwin 	    error = EINVAL;
17651d073b1dSJohn Baldwin 	}
1766cb9b0d80SMike Smith     }
17671d073b1dSJohn Baldwin     return(error);
17681d073b1dSJohn Baldwin }
17691d073b1dSJohn Baldwin 
177015e32d5dSMike Smith #ifdef ACPI_DEBUG
17710ae55423SMike Smith /*
17720ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
17730ae55423SMike Smith  *
17740ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
17750ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
17760ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
17770ae55423SMike Smith  * prefixing the bit name with !.
17780ae55423SMike Smith  */
177915e32d5dSMike Smith struct debugtag
178015e32d5dSMike Smith {
178115e32d5dSMike Smith     char	*name;
178215e32d5dSMike Smith     UINT32	value;
178315e32d5dSMike Smith };
178415e32d5dSMike Smith 
178515e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
1786c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
1787c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
1788c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
1789c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
1790c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
1791c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
1792c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
1793c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
1794c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
17954c1cdee6SMike Smith     {"ACPI_DEBUGGER",		ACPI_DEBUGGER},
17964c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
17974c1cdee6SMike Smith 
1798cb9b0d80SMike Smith     {"ACPI_BUS",		ACPI_BUS},
17994c1cdee6SMike Smith     {"ACPI_SYSTEM",		ACPI_SYSTEM},
1800cb9b0d80SMike Smith     {"ACPI_POWER",		ACPI_POWER},
1801cb9b0d80SMike Smith     {"ACPI_EC", 		ACPI_EC},
1802c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
1803c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
1804c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
18054c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
1806cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
18074c1cdee6SMike Smith     {"ACPI_FAN",		ACPI_FAN},
18084c1cdee6SMike Smith 
1809c5ba0be4SMike Smith     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
181015e32d5dSMike Smith     {NULL, 0}
181115e32d5dSMike Smith };
181215e32d5dSMike Smith 
181315e32d5dSMike Smith static struct debugtag dbg_level[] = {
18144c1cdee6SMike Smith     {"ACPI_LV_OK",		ACPI_LV_OK},
18154c1cdee6SMike Smith     {"ACPI_LV_INFO",		ACPI_LV_INFO},
18164c1cdee6SMike Smith     {"ACPI_LV_WARN",		ACPI_LV_WARN},
18174c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
18184c1cdee6SMike Smith     {"ACPI_LV_FATAL",		ACPI_LV_FATAL},
18194c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
18204c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
18214c1cdee6SMike Smith     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
18224c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
18234c1cdee6SMike Smith     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
18244c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
18254c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
18264c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
18274c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
18284c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
18294c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
18304c1cdee6SMike Smith     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
18314c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
18324c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
18334c1cdee6SMike Smith     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
18344c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
18354c1cdee6SMike Smith     {"ACPI_LV_IO",		ACPI_LV_IO},
18364c1cdee6SMike Smith     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
18374c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
18384c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
18394c1cdee6SMike Smith     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
18404c1cdee6SMike Smith     {"ACPI_LV_INIT",		ACPI_LV_INIT},
18414c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
18424c1cdee6SMike Smith     {"ACPI_DB_AML_DISASSEMBLE",	ACPI_DB_AML_DISASSEMBLE},
18434c1cdee6SMike Smith     {"ACPI_DB_VERBOSE_INFO",	ACPI_DB_VERBOSE_INFO},
18444c1cdee6SMike Smith     {"ACPI_DB_FULL_TABLES",	ACPI_DB_FULL_TABLES},
18454c1cdee6SMike Smith     {"ACPI_DB_EVENTS",		ACPI_DB_EVENTS},
18464c1cdee6SMike Smith     {"ACPI_DB_VERBOSE",		ACPI_DB_VERBOSE},
184715e32d5dSMike Smith     {NULL, 0}
184815e32d5dSMike Smith };
184915e32d5dSMike Smith 
185015e32d5dSMike Smith static void
185115e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
185215e32d5dSMike Smith {
185315e32d5dSMike Smith     char	*ep;
185415e32d5dSMike Smith     int		i, l;
18550ae55423SMike Smith     int		set;
185615e32d5dSMike Smith 
185715e32d5dSMike Smith     while (*cp) {
185815e32d5dSMike Smith 	if (isspace(*cp)) {
185915e32d5dSMike Smith 	    cp++;
186015e32d5dSMike Smith 	    continue;
186115e32d5dSMike Smith 	}
186215e32d5dSMike Smith 	ep = cp;
186315e32d5dSMike Smith 	while (*ep && !isspace(*ep))
186415e32d5dSMike Smith 	    ep++;
18650ae55423SMike Smith 	if (*cp == '!') {
18660ae55423SMike Smith 	    set = 0;
18670ae55423SMike Smith 	    cp++;
18680ae55423SMike Smith 	    if (cp == ep)
18690ae55423SMike Smith 		continue;
18700ae55423SMike Smith 	} else {
18710ae55423SMike Smith 	    set = 1;
18720ae55423SMike Smith 	}
187315e32d5dSMike Smith 	l = ep - cp;
187415e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
187515e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
18760ae55423SMike Smith 		if (set) {
187715e32d5dSMike Smith 		    *flag |= tag[i].value;
18780ae55423SMike Smith 		} else {
18790ae55423SMike Smith 		    *flag &= ~tag[i].value;
18800ae55423SMike Smith 		}
188115e32d5dSMike Smith 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
188215e32d5dSMike Smith 	    }
188315e32d5dSMike Smith 	}
188415e32d5dSMike Smith 	cp = ep;
188515e32d5dSMike Smith     }
188615e32d5dSMike Smith }
188715e32d5dSMike Smith 
188815e32d5dSMike Smith static void
18892a4ac806SMike Smith acpi_set_debugging(void *junk)
189015e32d5dSMike Smith {
189115e32d5dSMike Smith     char	*cp;
189215e32d5dSMike Smith 
18930ae55423SMike Smith     AcpiDbgLayer = 0;
18940ae55423SMike Smith     AcpiDbgLevel = 0;
189515e32d5dSMike Smith     if ((cp = getenv("debug.acpi.layer")) != NULL)
189615e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
189715e32d5dSMike Smith     if ((cp = getenv("debug.acpi.level")) != NULL)
189815e32d5dSMike Smith 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
18990ae55423SMike Smith 
19000ae55423SMike Smith     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
190115e32d5dSMike Smith }
19022a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
190315e32d5dSMike Smith #endif
1904f9390180SMitsuru IWASAKI 
1905f9390180SMitsuru IWASAKI static int
1906f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
1907f9390180SMitsuru IWASAKI {
1908f9390180SMitsuru IWASAKI 	int	state, acpi_state;
1909f9390180SMitsuru IWASAKI 	int	error;
1910f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
1911f9390180SMitsuru IWASAKI 	va_list	ap;
1912f9390180SMitsuru IWASAKI 
1913f9390180SMitsuru IWASAKI 	error = 0;
1914f9390180SMitsuru IWASAKI 	switch (cmd) {
1915f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
1916f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
1917f9390180SMitsuru IWASAKI 		if (sc == NULL) {
1918f9390180SMitsuru IWASAKI 			error = EINVAL;
1919f9390180SMitsuru IWASAKI 			goto out;
1920f9390180SMitsuru IWASAKI 		}
1921f9390180SMitsuru IWASAKI 
1922f9390180SMitsuru IWASAKI 		va_start(ap, arg);
1923f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
1924f9390180SMitsuru IWASAKI 		va_end(ap);
1925f9390180SMitsuru IWASAKI 
1926f9390180SMitsuru IWASAKI 		switch (state) {
1927f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
1928f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
1929f9390180SMitsuru IWASAKI 			break;
1930f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
1931f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
1932f9390180SMitsuru IWASAKI 			break;
1933f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
1934f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
1935f9390180SMitsuru IWASAKI 			break;
1936f9390180SMitsuru IWASAKI 		default:
1937f9390180SMitsuru IWASAKI 			error = EINVAL;
1938f9390180SMitsuru IWASAKI 			goto out;
1939f9390180SMitsuru IWASAKI 		}
1940f9390180SMitsuru IWASAKI 
1941f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
1942f9390180SMitsuru IWASAKI 		break;
1943f9390180SMitsuru IWASAKI 
1944f9390180SMitsuru IWASAKI 	default:
1945f9390180SMitsuru IWASAKI 		error = EINVAL;
1946f9390180SMitsuru IWASAKI 		goto out;
1947f9390180SMitsuru IWASAKI 	}
1948f9390180SMitsuru IWASAKI 
1949f9390180SMitsuru IWASAKI out:
1950f9390180SMitsuru IWASAKI 	return (error);
1951f9390180SMitsuru IWASAKI }
1952f9390180SMitsuru IWASAKI 
1953f9390180SMitsuru IWASAKI static void
1954f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
1955f9390180SMitsuru IWASAKI {
1956f9390180SMitsuru IWASAKI 
1957f9390180SMitsuru IWASAKI 	power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
1958f9390180SMitsuru IWASAKI }
1959f9390180SMitsuru IWASAKI 
1960f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
1961f9390180SMitsuru IWASAKI 
1962