xref: /freebsd/sys/dev/acpica/acpi.c (revision e1c4bf3f42ad89f53c7d5e9b7d21ece67a3084bb)
115e32d5dSMike Smith /*-
215e32d5dSMike Smith  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
315e32d5dSMike Smith  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4cb9b0d80SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
515e32d5dSMike Smith  * Copyright (c) 2000 BSDi
615e32d5dSMike Smith  * All rights reserved.
715e32d5dSMike Smith  *
815e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
915e32d5dSMike Smith  * modification, are permitted provided that the following conditions
1015e32d5dSMike Smith  * are met:
1115e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1215e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1315e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1415e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1515e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1615e32d5dSMike Smith  *
1715e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1815e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2015e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2115e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2215e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2315e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2415e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2515e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2615e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2715e32d5dSMike Smith  * SUCH DAMAGE.
2815e32d5dSMike Smith  *
2915e32d5dSMike Smith  *	$FreeBSD$
3015e32d5dSMike Smith  */
3115e32d5dSMike Smith 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3715e32d5dSMike Smith #include <sys/malloc.h>
38fe12f24bSPoul-Henning Kamp #include <sys/module.h>
3915e32d5dSMike Smith #include <sys/bus.h>
4015e32d5dSMike Smith #include <sys/conf.h>
4115e32d5dSMike Smith #include <sys/ioccom.h>
4215e32d5dSMike Smith #include <sys/reboot.h>
4315e32d5dSMike Smith #include <sys/sysctl.h>
4415e32d5dSMike Smith #include <sys/ctype.h>
451611ea87SMitsuru IWASAKI #include <sys/linker.h>
46f9390180SMitsuru IWASAKI #include <sys/power.h>
4754f6bca0SNate Lawson #include <sys/sbuf.h>
48eeb3a05fSNate Lawson #include <sys/smp.h>
4915e32d5dSMike Smith 
5015e32d5dSMike Smith #include <machine/clock.h>
5115e32d5dSMike Smith #include <machine/resource.h>
52dc750869SNate Lawson #include <machine/bus.h>
53dc750869SNate Lawson #include <sys/rman.h>
54bc0f2195SMike Smith #include <isa/isavar.h>
55c796e27bSNate Lawson #include <isa/pnpvar.h>
56bc0f2195SMike Smith 
5715e32d5dSMike Smith #include "acpi.h"
5815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5915e32d5dSMike Smith #include <dev/acpica/acpiio.h>
609b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
6115e32d5dSMike Smith 
6210ce62b9SNate Lawson #include "pci_if.h"
6310ce62b9SNate Lawson #include <dev/pci/pcivar.h>
6410ce62b9SNate Lawson #include <dev/pci/pci_private.h>
6510ce62b9SNate Lawson 
6615e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6715e32d5dSMike Smith 
68be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
69cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
70b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
710ae55423SMike 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 static struct cdevsw acpi_cdevsw = {
77dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
787ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
797ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
807ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
817ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
8215e32d5dSMike Smith };
8315e32d5dSMike Smith 
84346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
85cb9b0d80SMike Smith struct mtx	acpi_mutex;
86cb9b0d80SMike Smith 
8789fb5af8SNate Lawson /* Bitmap of device quirks. */
8889fb5af8SNate Lawson int		acpi_quirks;
8989fb5af8SNate Lawson 
906d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9115e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9215e32d5dSMike Smith static int	acpi_probe(device_t dev);
9315e32d5dSMike Smith static int	acpi_attach(device_t dev);
9410ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
9510ce62b9SNate Lawson static int	acpi_resume(device_t dev);
96169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
97be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
98be2b1797SNate Lawson 			int unit);
9915e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10010ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
10110ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
102be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
103be2b1797SNate Lawson 			uintptr_t *result);
104be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
105be2b1797SNate Lawson 			uintptr_t value);
10691233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
107adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
108adad4744SNate Lawson static struct resource_list_entry *acpi_sysres_find(device_t dev, int type,
109adad4744SNate Lawson 		    u_long addr);
110be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
111be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
112be2b1797SNate Lawson 			u_long count, u_int flags);
113be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
114be2b1797SNate Lawson 			int rid, struct resource *r);
1151e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1161e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
117ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
118ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
119ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
120ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
12110ce62b9SNate Lawson static int	acpi_device_pwr_for_sleep(device_t bus, device_t dev,
12210ce62b9SNate Lawson 		    int *dstate);
123df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
124df8d2a32SNate Lawson 		    void *context, void **retval);
125df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
126df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
12710ce62b9SNate Lawson static int	acpi_set_powerstate_method(device_t bus, device_t child,
12810ce62b9SNate Lawson 		    int state);
129be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
130be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
13115e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
132b82ca9a9SNate Lawson static int	acpi_probe_order(ACPI_HANDLE handle, int *order);
133be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
134be2b1797SNate Lawson 		    void *context, void **status);
135ce619b2eSNate Lawson static BOOLEAN	acpi_MatchHid(ACPI_HANDLE h, const char *hid);
13615e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
13713d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
138d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
139d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
140d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
14188a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
14288a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
14315e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
14415e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
145d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1461d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
147f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
148879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
149879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
150879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
151879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
152879d6c23STakanori Watanabe 
15315e32d5dSMike Smith static device_method_t acpi_methods[] = {
15415e32d5dSMike Smith     /* Device interface */
15515e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
15615e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
15715e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
158169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
15956a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
16010ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
16110ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
16215e32d5dSMike Smith 
16315e32d5dSMike Smith     /* Bus interface */
16415e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
16515e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
16610ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
16710ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
16815e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
16915e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
17091233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
17191233413SNate Lawson     DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
17291233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
17315e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
17415e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
175879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
176879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
17715e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
17815e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
17915e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
18015e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
18115e32d5dSMike Smith 
182ce619b2eSNate Lawson     /* ACPI bus */
183ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
184ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
18510ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
186df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
187ce619b2eSNate Lawson 
18810ce62b9SNate Lawson     /* PCI emulation */
18910ce62b9SNate Lawson     DEVMETHOD(pci_set_powerstate,	acpi_set_powerstate_method),
19010ce62b9SNate Lawson 
191bc0f2195SMike Smith     /* ISA emulation */
192bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
193bc0f2195SMike Smith 
19415e32d5dSMike Smith     {0, 0}
19515e32d5dSMike Smith };
19615e32d5dSMike Smith 
19715e32d5dSMike Smith static driver_t acpi_driver = {
19815e32d5dSMike Smith     "acpi",
19915e32d5dSMike Smith     acpi_methods,
20015e32d5dSMike Smith     sizeof(struct acpi_softc),
20115e32d5dSMike Smith };
20215e32d5dSMike Smith 
2033273b005SMike Smith static devclass_t acpi_devclass;
2046d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
205a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
20615e32d5dSMike Smith 
20715e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
20815e2f34fSNate Lawson 
209adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
210adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
211adad4744SNate Lawson 
212bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
213bee4aa4aSNate Lawson 
214865b8d0bSNate Lawson static const char* sleep_state_names[] = {
215865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
216865b8d0bSNate Lawson 
2171d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
2181d7b121cSNate Lawson static char acpi_ca_version[12];
2191d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2201d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
22115e32d5dSMike Smith 
22215e32d5dSMike Smith /*
223413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
224c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
225c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
226c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
227c9b8d77dSNate Lawson  * some IBMs use such code.
228413081d7SNate Lawson  */
229c9b8d77dSNate Lawson static int acpi_serialize_methods;
230413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
231413081d7SNate Lawson 
23210ce62b9SNate Lawson /* Power devices off and on in suspend and resume.  XXX Remove once tested. */
23310ce62b9SNate Lawson static int acpi_do_powerstate = 1;
23410ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
23510ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
23610ce62b9SNate Lawson     &acpi_do_powerstate, 1, "Turn off devices when suspending.");
23710ce62b9SNate Lawson 
238c9b8d77dSNate Lawson /*
2396d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2406d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
241be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
242be2b1797SNate Lawson  * off it.
2436d63101aSMike Smith  */
2446d63101aSMike Smith static int
2456d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2466d63101aSMike Smith {
2476d63101aSMike Smith     switch (event) {
2486d63101aSMike Smith     case MOD_LOAD:
24987b45ed5SMitsuru IWASAKI 	if (!cold) {
25087b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2516d63101aSMike Smith 	    return (EPERM);
25287b45ed5SMitsuru IWASAKI 	}
2536d63101aSMike Smith 	break;
2546d63101aSMike Smith     case MOD_UNLOAD:
255f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2566d63101aSMike Smith 	    return (EBUSY);
257f9390180SMitsuru IWASAKI 	break;
2586d63101aSMike Smith     default:
2596d63101aSMike Smith 	break;
2606d63101aSMike Smith     }
2616d63101aSMike Smith     return (0);
2626d63101aSMike Smith }
2636d63101aSMike Smith 
2646d63101aSMike Smith /*
265bbc2815cSJohn Baldwin  * Perform early initialization.
26615e32d5dSMike Smith  */
267bbc2815cSJohn Baldwin ACPI_STATUS
268bbc2815cSJohn Baldwin acpi_Startup(void)
26915e32d5dSMike Smith {
27089fb5af8SNate Lawson     static int started = 0;
27189fb5af8SNate Lawson     int error, val;
27215e32d5dSMike Smith 
2731b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2741b8c233dSPeter Pentchev 
275bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
276bbc2815cSJohn Baldwin     if (started)
27789fb5af8SNate Lawson 	return_VALUE (0);
278bbc2815cSJohn Baldwin     started = 1;
27915e32d5dSMike Smith 
280be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2816008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
282cb9b0d80SMike Smith 
283413081d7SNate Lawson     /*
284413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
285f3fc4f8bSNate Lawson      * uses UINT8 for some values and we have no tunable_byte.
286413081d7SNate Lawson      */
287834a79deSNate Lawson     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
288834a79deSNate Lawson     AcpiGbl_EnableInterpreterSlack = TRUE;
289413081d7SNate Lawson 
290be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
291b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
292bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
293bbc2815cSJohn Baldwin 	return_VALUE (error);
29415e32d5dSMike Smith     }
2951611ea87SMitsuru IWASAKI 
296b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
297bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
29889fb5af8SNate Lawson 	AcpiTerminate();
299bbc2815cSJohn Baldwin 	return_VALUE (error);
30015e32d5dSMike Smith     }
3013184cf5aSNate Lawson 
30289fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
30389fb5af8SNate Lawson     acpi_table_quirks(&acpi_quirks);
30489fb5af8SNate Lawson 
30589fb5af8SNate Lawson     /* If the user manually set the disabled hint to 0, override any quirk. */
30689fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
30789fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
30889fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
30989fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
31089fb5af8SNate Lawson 	AcpiTerminate();
3114e376d58SNate Lawson 	return_VALUE (AE_ERROR);
31289fb5af8SNate Lawson     }
3133184cf5aSNate Lawson 
314bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
315bbc2815cSJohn Baldwin }
316bbc2815cSJohn Baldwin 
317bbc2815cSJohn Baldwin /*
318bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
319bbc2815cSJohn Baldwin  */
320bbc2815cSJohn Baldwin static void
321bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
322bbc2815cSJohn Baldwin {
323bbc2815cSJohn Baldwin     device_t	child;
324bbc2815cSJohn Baldwin 
325bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
326bbc2815cSJohn Baldwin 
327bbc2815cSJohn Baldwin     if (!cold)
328bbc2815cSJohn Baldwin 	return_VOID;
329bbc2815cSJohn Baldwin 
330bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
331bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
332bbc2815cSJohn Baldwin 	return_VOID;
333bbc2815cSJohn Baldwin 
334bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
335bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
336bbc2815cSJohn Baldwin 	return_VOID;
337bbc2815cSJohn Baldwin 
338bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
339bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
340bbc2815cSJohn Baldwin 	return_VOID;
34115e32d5dSMike Smith 
3424e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3434e376d58SNate Lawson 
344be2b1797SNate Lawson     /* Attach the actual ACPI device. */
34515e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
346bee4aa4aSNate Lawson 	device_printf(parent, "device_identify failed\n");
3470ae55423SMike Smith 	return_VOID;
34815e32d5dSMike Smith     }
34915e32d5dSMike Smith }
35015e32d5dSMike Smith 
35115e32d5dSMike Smith /*
352bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
35315e32d5dSMike Smith  */
35415e32d5dSMike Smith static int
35515e32d5dSMike Smith acpi_probe(device_t dev)
35615e32d5dSMike Smith {
35715e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
35815e32d5dSMike Smith     char		buf[20];
35915e32d5dSMike Smith     int			error;
3602ccd1cacSNate Lawson     struct sbuf		sb;
3612ccd1cacSNate Lawson     ACPI_STATUS		status;
36215e32d5dSMike Smith 
363b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
364f9390180SMitsuru IWASAKI 
365f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
366f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
367bee4aa4aSNate Lawson 	device_printf(dev, "probe failed, other PM system enabled.\n");
368f9390180SMitsuru IWASAKI 	return_VALUE (ENXIO);
369f9390180SMitsuru IWASAKI     }
370f9390180SMitsuru IWASAKI 
371b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
372be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
373be2b1797SNate Lawson 		      AcpiFormatException(status));
374cb9b0d80SMike Smith 	error = ENXIO;
375cb9b0d80SMike Smith     } else {
3762ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3772ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3782ccd1cacSNate Lawson 	sbuf_trim(&sb);
3792ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3802ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3812ccd1cacSNate Lawson 	sbuf_trim(&sb);
3822ccd1cacSNate Lawson 	sbuf_finish(&sb);
3832ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3842ccd1cacSNate Lawson 	sbuf_delete(&sb);
385cb9b0d80SMike Smith 	error = 0;
386cb9b0d80SMike Smith     }
387bee4aa4aSNate Lawson 
388cb9b0d80SMike Smith     return_VALUE (error);
38915e32d5dSMike Smith }
39015e32d5dSMike Smith 
39115e32d5dSMike Smith static int
39215e32d5dSMike Smith acpi_attach(device_t dev)
39315e32d5dSMike Smith {
39415e32d5dSMike Smith     struct acpi_softc	*sc;
395cb9b0d80SMike Smith     ACPI_STATUS		status;
396ea27c63eSNate Lawson     int			error, state;
39732d18aa5SMike Smith     UINT32		flags;
398ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
399498d464fSMitsuru IWASAKI     char		*env;
40015e32d5dSMike Smith 
401b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
402bee4aa4aSNate Lawson 
40315e32d5dSMike Smith     sc = device_get_softc(dev);
40415e32d5dSMike Smith     sc->acpi_dev = dev;
40515e32d5dSMike Smith 
40691233413SNate Lawson     /* Initialize resource manager. */
40791233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
40891233413SNate Lawson     acpi_rman_io.rm_start = 0;
40991233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
41091233413SNate Lawson     acpi_rman_io.rm_descr = "I/O ports";
41191233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
41291233413SNate Lawson 	panic("acpi rman_init IO ports failed");
41391233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
41491233413SNate Lawson     acpi_rman_mem.rm_start = 0;
41591233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
41691233413SNate Lawson     acpi_rman_mem.rm_descr = "I/O memory addresses";
41791233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
41891233413SNate Lawson 	panic("acpi rman_init memory failed");
41991233413SNate Lawson 
420be2b1797SNate Lawson     /* Install the default address space handlers. */
421cb9b0d80SMike Smith     error = ENXIO;
422be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
423be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
424be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
425be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
426be2b1797SNate Lawson 		      AcpiFormatException(status));
427cb9b0d80SMike Smith 	goto out;
42815e32d5dSMike Smith     }
429be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
430be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
431be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
432be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
433be2b1797SNate Lawson 		      AcpiFormatException(status));
434cb9b0d80SMike Smith 	goto out;
43515e32d5dSMike Smith     }
436be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
437be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
438be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
439be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
440be2b1797SNate Lawson 		      AcpiFormatException(status));
441cb9b0d80SMike Smith 	goto out;
44215e32d5dSMike Smith     }
44315e32d5dSMike Smith 
44415e32d5dSMike Smith     /*
445be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
446be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
447be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
448be2b1797SNate Lawson      * object init pass.
44915e32d5dSMike Smith      *
45032d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
45132d18aa5SMike Smith      *
452be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
453be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
45415e32d5dSMike Smith      */
45532d18aa5SMike Smith     flags = 0;
456d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
45732d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
458bee4aa4aSNate Lawson 
459bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
460b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
461be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
462be2b1797SNate Lawson 		      AcpiFormatException(status));
463cb9b0d80SMike Smith 	goto out;
46415e32d5dSMike Smith     }
46515e32d5dSMike Smith 
466f8335e3aSNate Lawson     /*
467f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
468f8335e3aSNate Lawson      * the namespace has been evaluated.
469f8335e3aSNate Lawson      */
470f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
471f8335e3aSNate Lawson 
472bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
473b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
474be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
475be2b1797SNate Lawson 		      AcpiFormatException(status));
476b69ed3f4SMitsuru IWASAKI 	goto out;
477b69ed3f4SMitsuru IWASAKI     }
478b69ed3f4SMitsuru IWASAKI 
47915e32d5dSMike Smith     /*
4801d073b1dSJohn Baldwin      * Setup our sysctl tree.
4811d073b1dSJohn Baldwin      *
4821d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4831d073b1dSJohn Baldwin      */
4841d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4851d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4861d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4871d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4881d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
489d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
490d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
491d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4921d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4931d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4941d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4951d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
4961d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
4971d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
4981d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
4991d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
500f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
501f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
502f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
503f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
504f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
505f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5062d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
507ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
508ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
509ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5101611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
5111611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
5121611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5132d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
5142d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
515bf0c18ecSNate Lawson 
516bf0c18ecSNate Lawson     /*
5172b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
518bf0c18ecSNate Lawson      * stabilize.
519bf0c18ecSNate Lawson      */
5202b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
5212d644607SMitsuru IWASAKI     if (bootverbose)
5222d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
523498d464fSMitsuru IWASAKI     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
524498d464fSMitsuru IWASAKI 	sc->acpi_verbose = 1;
525498d464fSMitsuru IWASAKI 	freeenv(env);
526498d464fSMitsuru IWASAKI     }
5271d073b1dSJohn Baldwin 
5282d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5292d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5302d610c46SNate Lawson 	sc->acpi_s4bios = 1;
5312d610c46SNate Lawson 
5321d073b1dSJohn Baldwin     /*
533ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
534ea27c63eSNate Lawson      * to NONE by default to avoid surprising users.
53515e32d5dSMike Smith      */
536ea27c63eSNate Lawson     sc->acpi_power_button_sx = ACPI_STATE_S5;
537ea27c63eSNate Lawson     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
538f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
539f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
54015e32d5dSMike Smith 
541ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
542ea27c63eSNate Lawson     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
543ea27c63eSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
544ea27c63eSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
545ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
546ea27c63eSNate Lawson 	    break;
547ea27c63eSNate Lawson 	}
548ea27c63eSNate Lawson 
54913d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
55015e32d5dSMike Smith 
55115e32d5dSMike Smith     /*
55215e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
55315e32d5dSMike Smith      */
55415e32d5dSMike Smith 
55559e472e9SNate Lawson     /* Register our shutdown handler. */
556be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
557be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
55815e32d5dSMike Smith 
55915e32d5dSMike Smith     /*
56015e32d5dSMike Smith      * Register our acpi event handlers.
56115e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
56215e32d5dSMike Smith      */
563be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
564be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
565be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
566be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
56715e32d5dSMike Smith 
568be2b1797SNate Lawson     /* Flag our initial states. */
56915e32d5dSMike Smith     sc->acpi_enabled = 1;
57015e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
571ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
57215e32d5dSMike Smith 
573be2b1797SNate Lawson     /* Create the control device */
574a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5752a4689e8SRobert Watson 			      "acpi");
57615e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
57715e32d5dSMike Smith 
578be2b1797SNate Lawson     if ((error = acpi_task_thread_init()))
579c573e654SMitsuru IWASAKI 	goto out;
580c573e654SMitsuru IWASAKI 
581be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
582f86214b6SMitsuru IWASAKI 	goto out;
583f86214b6SMitsuru IWASAKI 
584f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
585f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
586f9390180SMitsuru IWASAKI 
587eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
588eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
589eeb6dba2SJohn Baldwin 
590cb9b0d80SMike Smith     error = 0;
591cb9b0d80SMike Smith 
592cb9b0d80SMike Smith  out:
593cb9b0d80SMike Smith     return_VALUE (error);
59415e32d5dSMike Smith }
59515e32d5dSMike Smith 
596169b539aSNate Lawson static int
59710ce62b9SNate Lawson acpi_suspend(device_t dev)
59810ce62b9SNate Lawson {
59910ce62b9SNate Lawson     struct acpi_softc *sc;
60010ce62b9SNate Lawson     device_t child, *devlist;
60110ce62b9SNate Lawson     int error, i, numdevs, pstate;
60210ce62b9SNate Lawson 
6035d3d03f1SNate Lawson     GIANT_REQUIRED;
6045d3d03f1SNate Lawson 
60510ce62b9SNate Lawson     /* First give child devices a chance to suspend. */
60610ce62b9SNate Lawson     error = bus_generic_suspend(dev);
60710ce62b9SNate Lawson     if (error)
60810ce62b9SNate Lawson 	return (error);
60910ce62b9SNate Lawson 
61010ce62b9SNate Lawson     /*
61110ce62b9SNate Lawson      * Now, set them into the appropriate power state, usually D3.  If the
61210ce62b9SNate Lawson      * device has an _SxD method for the next sleep state, use that power
61310ce62b9SNate Lawson      * state instead.
61410ce62b9SNate Lawson      */
61510ce62b9SNate Lawson     sc = device_get_softc(dev);
61610ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
61710ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
61810ce62b9SNate Lawson 	/* If the device is not attached, we've powered it down elsewhere. */
61910ce62b9SNate Lawson 	child = devlist[i];
62010ce62b9SNate Lawson 	if (!device_is_attached(child))
62110ce62b9SNate Lawson 	    continue;
62210ce62b9SNate Lawson 
62310ce62b9SNate Lawson 	/*
62410ce62b9SNate Lawson 	 * Default to D3 for all sleep states.  The _SxD method is optional
62510ce62b9SNate Lawson 	 * so set the powerstate even if it's absent.
62610ce62b9SNate Lawson 	 */
62710ce62b9SNate Lawson 	pstate = PCI_POWERSTATE_D3;
62810ce62b9SNate Lawson 	error = acpi_device_pwr_for_sleep(device_get_parent(child),
62910ce62b9SNate Lawson 	    child, &pstate);
63010ce62b9SNate Lawson 	if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
63110ce62b9SNate Lawson 	    pci_set_powerstate(child, pstate);
63210ce62b9SNate Lawson     }
63310ce62b9SNate Lawson     free(devlist, M_TEMP);
63410ce62b9SNate Lawson     error = 0;
63510ce62b9SNate Lawson 
63610ce62b9SNate Lawson     return (error);
63710ce62b9SNate Lawson }
63810ce62b9SNate Lawson 
63910ce62b9SNate Lawson static int
64010ce62b9SNate Lawson acpi_resume(device_t dev)
64110ce62b9SNate Lawson {
64210ce62b9SNate Lawson     ACPI_HANDLE handle;
64310ce62b9SNate Lawson     int i, numdevs;
64410ce62b9SNate Lawson     device_t child, *devlist;
64510ce62b9SNate Lawson 
6465d3d03f1SNate Lawson     GIANT_REQUIRED;
6475d3d03f1SNate Lawson 
64810ce62b9SNate Lawson     /*
64910ce62b9SNate Lawson      * Put all devices in D0 before resuming them.  Call _S0D on each one
65010ce62b9SNate Lawson      * since some systems expect this.
65110ce62b9SNate Lawson      */
65210ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
65310ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
65410ce62b9SNate Lawson 	child = devlist[i];
65510ce62b9SNate Lawson 	handle = acpi_get_handle(child);
65610ce62b9SNate Lawson 	if (handle)
65710ce62b9SNate Lawson 	    AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
65810ce62b9SNate Lawson 	if (device_is_attached(child) && acpi_do_powerstate)
65910ce62b9SNate Lawson 	    pci_set_powerstate(child, PCI_POWERSTATE_D0);
66010ce62b9SNate Lawson     }
66110ce62b9SNate Lawson     free(devlist, M_TEMP);
66210ce62b9SNate Lawson 
66310ce62b9SNate Lawson     return (bus_generic_resume(dev));
66410ce62b9SNate Lawson }
66510ce62b9SNate Lawson 
66610ce62b9SNate Lawson static int
667169b539aSNate Lawson acpi_shutdown(device_t dev)
668169b539aSNate Lawson {
669169b539aSNate Lawson 
6705d3d03f1SNate Lawson     GIANT_REQUIRED;
6715d3d03f1SNate Lawson 
6720e414868SNate Lawson     /* Allow children to shutdown first. */
6730e414868SNate Lawson     bus_generic_shutdown(dev);
6740e414868SNate Lawson 
675bee4aa4aSNate Lawson     /*
676bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
677bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
678bee4aa4aSNate Lawson      */
679d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
680d4b9ff91SNate Lawson 
681169b539aSNate Lawson     return (0);
682169b539aSNate Lawson }
683169b539aSNate Lawson 
68415e32d5dSMike Smith /*
68515e32d5dSMike Smith  * Handle a new device being added
68615e32d5dSMike Smith  */
68715e32d5dSMike Smith static device_t
68815e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
68915e32d5dSMike Smith {
69015e32d5dSMike Smith     struct acpi_device	*ad;
69115e32d5dSMike Smith     device_t		child;
69215e32d5dSMike Smith 
693be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
69415e32d5dSMike Smith 	return (NULL);
69515e32d5dSMike Smith 
69615e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
69715e32d5dSMike Smith 
69815e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
69915e32d5dSMike Smith     if (child != NULL)
70015e32d5dSMike Smith 	device_set_ivars(child, ad);
70115e32d5dSMike Smith     return (child);
70215e32d5dSMike Smith }
70315e32d5dSMike Smith 
70415e32d5dSMike Smith static int
70515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
70615e32d5dSMike Smith {
70715e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
70815e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
70915e32d5dSMike Smith     int retval = 0;
71015e32d5dSMike Smith 
71115e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
7129ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
7139ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
7149ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
7159ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
716a91c5fa8SNate Lawson     if (device_get_flags(child))
717a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
7182c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
71915e32d5dSMike Smith 
72015e32d5dSMike Smith     return (retval);
72115e32d5dSMike Smith }
72215e32d5dSMike Smith 
72310ce62b9SNate Lawson /*
72410ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
72510ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
72610ce62b9SNate Lawson  *
72710ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
72810ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
72910ce62b9SNate Lawson  * them to be powered up.
73010ce62b9SNate Lawson  */
73110ce62b9SNate Lawson static void
73210ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
73310ce62b9SNate Lawson {
73410ce62b9SNate Lawson 
73510ce62b9SNate Lawson     /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
73610ce62b9SNate Lawson }
73710ce62b9SNate Lawson 
73810ce62b9SNate Lawson /*
73910ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
74010ce62b9SNate Lawson  *
74110ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
74210ce62b9SNate Lawson  */
74310ce62b9SNate Lawson static void
74410ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
74510ce62b9SNate Lawson {
74610ce62b9SNate Lawson     device_t child, *devlist;
74710ce62b9SNate Lawson     int i, numdevs;
74810ce62b9SNate Lawson 
74910ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
75010ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
75110ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
75210ce62b9SNate Lawson 	child = devlist[i];
75310ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
75410ce62b9SNate Lawson 	    /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
75510ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
75610ce62b9SNate Lawson 		; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
75710ce62b9SNate Lawson 	}
75810ce62b9SNate Lawson     }
75910ce62b9SNate Lawson     free(devlist, M_TEMP);
76010ce62b9SNate Lawson }
76110ce62b9SNate Lawson 
76263600cc3SNate Lawson /* Location hint for devctl(8) */
76363600cc3SNate Lawson static int
764247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
765247648afSTakanori Watanabe     size_t buflen)
766247648afSTakanori Watanabe {
767247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
768247648afSTakanori Watanabe 
769247648afSTakanori Watanabe     if (dinfo->ad_handle)
770d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
771247648afSTakanori Watanabe     else
772d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
773247648afSTakanori Watanabe     return (0);
774247648afSTakanori Watanabe }
775247648afSTakanori Watanabe 
77663600cc3SNate Lawson /* PnP information for devctl(8) */
77763600cc3SNate Lawson static int
778247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
779247648afSTakanori Watanabe     size_t buflen)
780247648afSTakanori Watanabe {
781247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
78263600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
78363600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
784247648afSTakanori Watanabe     char *end;
785247648afSTakanori Watanabe     int error;
786247648afSTakanori Watanabe 
787247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
788247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
789247648afSTakanori Watanabe     if (error)
790d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
791247648afSTakanori Watanabe     else
792247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
793247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
794d40c0f53SNate Lawson 		 adinfo->HardwareId.Value : "none",
79563600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
79663600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
797247648afSTakanori Watanabe     if (adinfo)
798247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
799247648afSTakanori Watanabe 
800247648afSTakanori Watanabe     return (0);
801247648afSTakanori Watanabe }
802247648afSTakanori Watanabe 
803247648afSTakanori Watanabe /*
80415e32d5dSMike Smith  * Handle per-device ivars
80515e32d5dSMike Smith  */
80615e32d5dSMike Smith static int
80715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
80815e32d5dSMike Smith {
80915e32d5dSMike Smith     struct acpi_device	*ad;
81015e32d5dSMike Smith 
81115e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
81215e32d5dSMike Smith 	printf("device has no ivars\n");
81315e32d5dSMike Smith 	return (ENOENT);
81415e32d5dSMike Smith     }
81515e32d5dSMike Smith 
816be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
81715e32d5dSMike Smith     switch(index) {
81815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
81915e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
82015e32d5dSMike Smith 	break;
82115e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
82215e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
82315e32d5dSMike Smith 	break;
82415e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
82515e32d5dSMike Smith 	*(void **)result = ad->ad_private;
82615e32d5dSMike Smith 	break;
827d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
828d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
829d4b9ff91SNate Lawson 	break;
83032d18aa5SMike Smith     case ISA_IVAR_VENDORID:
83132d18aa5SMike Smith     case ISA_IVAR_SERIAL:
83232d18aa5SMike Smith     case ISA_IVAR_COMPATID:
83332d18aa5SMike Smith 	*(int *)result = -1;
83432d18aa5SMike Smith 	break;
83532d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
83632d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
83732d18aa5SMike Smith 	break;
83815e32d5dSMike Smith     default:
83915e32d5dSMike Smith 	return (ENOENT);
84015e32d5dSMike Smith     }
841be2b1797SNate Lawson 
84215e32d5dSMike Smith     return (0);
84315e32d5dSMike Smith }
84415e32d5dSMike Smith 
84515e32d5dSMike Smith static int
84615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
84715e32d5dSMike Smith {
84815e32d5dSMike Smith     struct acpi_device	*ad;
84915e32d5dSMike Smith 
85015e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
85115e32d5dSMike Smith 	printf("device has no ivars\n");
85215e32d5dSMike Smith 	return (ENOENT);
85315e32d5dSMike Smith     }
85415e32d5dSMike Smith 
85515e32d5dSMike Smith     switch(index) {
85615e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
85715e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
85815e32d5dSMike Smith 	break;
85915e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
86015e32d5dSMike Smith 	ad->ad_magic = (int)value;
86115e32d5dSMike Smith 	break;
86215e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
86315e32d5dSMike Smith 	ad->ad_private = (void *)value;
86415e32d5dSMike Smith 	break;
865d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
866d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
867d4b9ff91SNate Lawson 	break;
86815e32d5dSMike Smith     default:
8692ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
87015e32d5dSMike Smith 	return (ENOENT);
87115e32d5dSMike Smith     }
872be2b1797SNate Lawson 
87315e32d5dSMike Smith     return (0);
87415e32d5dSMike Smith }
87515e32d5dSMike Smith 
87615e32d5dSMike Smith /*
87715e32d5dSMike Smith  * Handle child resource allocation/removal
87815e32d5dSMike Smith  */
87991233413SNate Lawson static struct resource_list *
88091233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
88115e32d5dSMike Smith {
88291233413SNate Lawson     struct acpi_device		*ad;
88315e32d5dSMike Smith 
88491233413SNate Lawson     ad = device_get_ivars(child);
88591233413SNate Lawson     return (&ad->ad_rl);
88615e32d5dSMike Smith }
88715e32d5dSMike Smith 
888adad4744SNate Lawson /*
889adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
890adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
891adad4744SNate Lawson  */
892adad4744SNate Lawson static int
893adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
894adad4744SNate Lawson {
895adad4744SNate Lawson     struct resource *res;
896adad4744SNate Lawson     struct resource_list *rl;
897adad4744SNate Lawson     struct resource_list_entry *rle;
898adad4744SNate Lawson     struct rman *rm;
899adad4744SNate Lawson 
900adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
901adad4744SNate Lawson     SLIST_FOREACH(rle, rl, link) {
902adad4744SNate Lawson 	if (rle->res != NULL) {
903adad4744SNate Lawson 	    device_printf(dev, "duplicate resource for %lx\n", rle->start);
904adad4744SNate Lawson 	    continue;
905adad4744SNate Lawson 	}
906adad4744SNate Lawson 
907adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
908adad4744SNate Lawson 	switch (rle->type) {
909adad4744SNate Lawson 	case SYS_RES_IOPORT:
910adad4744SNate Lawson 	    rm = &acpi_rman_io;
911adad4744SNate Lawson 	    break;
912adad4744SNate Lawson 	case SYS_RES_MEMORY:
913adad4744SNate Lawson 	    rm = &acpi_rman_mem;
914adad4744SNate Lawson 	    break;
915adad4744SNate Lawson 	default:
916adad4744SNate Lawson 	    continue;
917adad4744SNate Lawson 	}
918adad4744SNate Lawson 
919adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
920adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
921adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
922adad4744SNate Lawson 	if (res != NULL) {
923adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
924adad4744SNate Lawson 	    rle->res = res;
925adad4744SNate Lawson 	} else
926adad4744SNate Lawson 	    device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
927adad4744SNate Lawson 		rle->start, rle->count, rle->type);
928adad4744SNate Lawson     }
929adad4744SNate Lawson     return (0);
930adad4744SNate Lawson }
931adad4744SNate Lawson 
932adad4744SNate Lawson /* Find if we manage a given resource. */
933adad4744SNate Lawson static struct resource_list_entry *
934adad4744SNate Lawson acpi_sysres_find(device_t dev, int type, u_long addr)
935adad4744SNate Lawson {
936adad4744SNate Lawson     struct resource_list *rl;
937adad4744SNate Lawson     struct resource_list_entry *rle;
938adad4744SNate Lawson 
939adad4744SNate Lawson     ACPI_SERIAL_ASSERT(acpi);
940adad4744SNate Lawson 
941adad4744SNate Lawson     /* We only consider IO and memory resources for our pool. */
942adad4744SNate Lawson     rle = NULL;
943adad4744SNate Lawson     if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY)
944adad4744SNate Lawson 	goto out;
945adad4744SNate Lawson 
946adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
947adad4744SNate Lawson     SLIST_FOREACH(rle, rl, link) {
948adad4744SNate Lawson 	if (type == rle->type && addr >= rle->start &&
949adad4744SNate Lawson 	    addr < rle->start + rle->count)
950adad4744SNate Lawson 	    break;
951adad4744SNate Lawson     }
952adad4744SNate Lawson 
953adad4744SNate Lawson out:
954adad4744SNate Lawson     return (rle);
955adad4744SNate Lawson }
956adad4744SNate Lawson 
95715e32d5dSMike Smith static struct resource *
95815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
95915e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
96015e32d5dSMike Smith {
96195957f62SJohn Baldwin     ACPI_RESOURCE ares;
96215e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
96315e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
96491233413SNate Lawson     struct resource_list_entry *rle;
96591233413SNate Lawson     struct resource *res;
96691233413SNate Lawson     struct rman *rm;
96715e32d5dSMike Smith 
96815e2f34fSNate Lawson     res = NULL;
96915e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
97015e2f34fSNate Lawson 
97191233413SNate Lawson     /*
97291233413SNate Lawson      * If this is an allocation of the "default" range for a given RID, and
97391233413SNate Lawson      * we know what the resources for this device are (i.e., they're on the
97491233413SNate Lawson      * child's resource list), use those start/end values.
97591233413SNate Lawson      */
97691233413SNate Lawson     if (start == 0UL && end == ~0UL) {
97791233413SNate Lawson 	rle = resource_list_find(rl, type, *rid);
97891233413SNate Lawson 	if (rle == NULL)
97915e2f34fSNate Lawson 	    goto out;
98091233413SNate Lawson 	start = rle->start;
98191233413SNate Lawson 	end = rle->end;
98291233413SNate Lawson 	count = rle->count;
98391233413SNate Lawson     }
98491233413SNate Lawson 
98591233413SNate Lawson     /* If we don't manage this address, pass the request up to the parent. */
986adad4744SNate Lawson     rle = acpi_sysres_find(bus, type, start);
98791233413SNate Lawson     if (rle == NULL) {
98895957f62SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
98995957f62SJohn Baldwin 	    start, end, count, flags);
99095957f62SJohn Baldwin     } else {
99191233413SNate Lawson 
99291233413SNate Lawson 	/* We only handle memory and IO resources through rman. */
99391233413SNate Lawson 	switch (type) {
99491233413SNate Lawson 	case SYS_RES_IOPORT:
99591233413SNate Lawson 	    rm = &acpi_rman_io;
99691233413SNate Lawson 	    break;
99791233413SNate Lawson 	case SYS_RES_MEMORY:
99891233413SNate Lawson 	    rm = &acpi_rman_mem;
99991233413SNate Lawson 	    break;
100091233413SNate Lawson 	default:
100191233413SNate Lawson 	    panic("acpi_alloc_resource: invalid res type %d", type);
100291233413SNate Lawson 	}
100391233413SNate Lawson 
100491233413SNate Lawson 	/* If we do know it, allocate it from the local pool. */
100595957f62SJohn Baldwin 	res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
100695957f62SJohn Baldwin 	    child);
100791233413SNate Lawson 	if (res == NULL)
100815e2f34fSNate Lawson 	    goto out;
100991233413SNate Lawson 
1010a3236570SWarner Losh 	/* Copy the bus tag and handle from the pre-allocated resource. */
101191233413SNate Lawson 	rman_set_bustag(res, rman_get_bustag(rle->res));
1012ed67d9deSWarner Losh 	rman_set_bushandle(res, rman_get_start(res));
101391233413SNate Lawson 
101491233413SNate Lawson 	/* If requested, activate the resource using the parent's method. */
101595957f62SJohn Baldwin 	if (flags & RF_ACTIVE)
101691233413SNate Lawson 	    if (bus_activate_resource(child, type, *rid, res) != 0) {
101791233413SNate Lawson 		rman_release_resource(res);
101815e2f34fSNate Lawson 		res = NULL;
101915e2f34fSNate Lawson 		goto out;
102091233413SNate Lawson 	    }
102195957f62SJohn Baldwin     }
102291233413SNate Lawson 
102395957f62SJohn Baldwin     if (res != NULL && device_get_parent(child) == bus)
102495957f62SJohn Baldwin 	switch (type) {
102595957f62SJohn Baldwin 	case SYS_RES_IRQ:
102695957f62SJohn Baldwin 	    /*
102795957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
102895957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
102995957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
103095957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
103195957f62SJohn Baldwin 	     *
103295957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
103395957f62SJohn Baldwin 	     */
103495957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
103595957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
103695957f62SJohn Baldwin 	    break;
103795957f62SJohn Baldwin 	}
103815e2f34fSNate Lawson 
103915e2f34fSNate Lawson out:
104015e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
104191233413SNate Lawson     return (res);
104215e32d5dSMike Smith }
104315e32d5dSMike Smith 
104415e32d5dSMike Smith static int
104591233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid,
104691233413SNate Lawson     struct resource *r)
104715e32d5dSMike Smith {
104891233413SNate Lawson     int ret;
104915e32d5dSMike Smith 
105015e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
105115e2f34fSNate Lawson 
105291233413SNate Lawson     /*
105391233413SNate Lawson      * If we know about this address, deactivate it and release it to the
105491233413SNate Lawson      * local pool.  If we don't, pass this request up to the parent.
105591233413SNate Lawson      */
1056adad4744SNate Lawson     if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) {
105791233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
105891233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
105991233413SNate Lawson 	    if (ret != 0)
106015e2f34fSNate Lawson 		goto out;
106191233413SNate Lawson 	}
106291233413SNate Lawson 	ret = rman_release_resource(r);
106391233413SNate Lawson     } else
106491233413SNate Lawson 	ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
106591233413SNate Lawson 
106615e2f34fSNate Lawson out:
106715e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
106891233413SNate Lawson     return (ret);
106915e32d5dSMike Smith }
107015e32d5dSMike Smith 
1071dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1072e1c4bf3fSNate Lawson int
1073e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1074e1c4bf3fSNate Lawson     struct resource **res)
1075dc750869SNate Lawson {
1076e1c4bf3fSNate Lawson     int error, res_type;
1077dc750869SNate Lawson 
1078e1c4bf3fSNate Lawson     error = ENOMEM;
1079e1c4bf3fSNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL ||
1080e1c4bf3fSNate Lawson 	!ACPI_VALID_ADDRESS(gas->Address) || gas->RegisterBitWidth < 8)
1081e1c4bf3fSNate Lawson 	return (EINVAL);
1082dc750869SNate Lawson 
1083dc750869SNate Lawson     switch (gas->AddressSpaceId) {
1084dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1085e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1086dc750869SNate Lawson 	break;
1087dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1088e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1089dc750869SNate Lawson 	break;
1090dc750869SNate Lawson     default:
1091e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1092dc750869SNate Lawson     }
1093dc750869SNate Lawson 
1094e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
1095e1c4bf3fSNate Lawson 	gas->RegisterBitWidth / 8);
1096e1c4bf3fSNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE);
1097e1c4bf3fSNate Lawson     if (*res != NULL) {
1098e1c4bf3fSNate Lawson 	*type = res_type;
1099e1c4bf3fSNate Lawson 	error = 0;
1100e1c4bf3fSNate Lawson     }
1101e1c4bf3fSNate Lawson     return (error);
1102dc750869SNate Lawson }
1103dc750869SNate Lawson 
1104c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
11051e4925e8SNate Lawson static uint32_t
110632d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1107bc0f2195SMike Smith {
11081e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11091e4925e8SNate Lawson     ACPI_BUFFER		buf;
1110bc0f2195SMike Smith     ACPI_HANDLE		h;
1111bc0f2195SMike Smith     ACPI_STATUS		error;
1112bc0f2195SMike Smith     u_int32_t		pnpid;
111332d18aa5SMike Smith 
1114b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
111532d18aa5SMike Smith 
111632d18aa5SMike Smith     pnpid = 0;
1117bd189fe7SNate Lawson     buf.Pointer = NULL;
1118bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1119bd189fe7SNate Lawson 
11206fca9360SNate Lawson     /* Fetch and validate the HID. */
112132d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1122bd189fe7SNate Lawson 	goto out;
11236fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11246fca9360SNate Lawson     if (ACPI_FAILURE(error))
1125bd189fe7SNate Lawson 	goto out;
11261e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
112732d18aa5SMike Smith 
11281e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
11291e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1130be2b1797SNate Lawson 
1131bd189fe7SNate Lawson out:
1132bd189fe7SNate Lawson     if (buf.Pointer != NULL)
11331e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
113432d18aa5SMike Smith     return_VALUE (pnpid);
113532d18aa5SMike Smith }
113632d18aa5SMike Smith 
11371e4925e8SNate Lawson static int
11381e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1139b2566207STakanori Watanabe {
11401e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11411e4925e8SNate Lawson     ACPI_BUFFER		buf;
1142b2566207STakanori Watanabe     ACPI_HANDLE		h;
1143b2566207STakanori Watanabe     ACPI_STATUS		error;
11441e4925e8SNate Lawson     uint32_t		*pnpid;
11451e4925e8SNate Lawson     int			valid, i;
1146b2566207STakanori Watanabe 
1147b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1148b2566207STakanori Watanabe 
11491e4925e8SNate Lawson     pnpid = cids;
11501e4925e8SNate Lawson     valid = 0;
1151bd189fe7SNate Lawson     buf.Pointer = NULL;
1152bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1153bd189fe7SNate Lawson 
11541e4925e8SNate Lawson     /* Fetch and validate the CID */
1155b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
1156bd189fe7SNate Lawson 	goto out;
11571e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11581e4925e8SNate Lawson     if (ACPI_FAILURE(error))
1159bd189fe7SNate Lawson 	goto out;
11601e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
11611e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1162b2566207STakanori Watanabe 	goto out;
1163b2566207STakanori Watanabe 
11641e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
11651e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
11661e4925e8SNate Lawson     for (i = 0; i < count; i++) {
11671e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
11681e4925e8SNate Lawson 	    continue;
11691e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
11701e4925e8SNate Lawson 	valid++;
1171b2566207STakanori Watanabe     }
1172b2566207STakanori Watanabe 
11731e4925e8SNate Lawson out:
1174bd189fe7SNate Lawson     if (buf.Pointer != NULL)
11751e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
11761e4925e8SNate Lawson     return_VALUE (valid);
11771e4925e8SNate Lawson }
1178b2566207STakanori Watanabe 
1179ce619b2eSNate Lawson static char *
1180ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1181ce619b2eSNate Lawson {
1182ce619b2eSNate Lawson     ACPI_HANDLE h;
1183ce619b2eSNate Lawson     int i;
1184ce619b2eSNate Lawson 
1185ce619b2eSNate Lawson     h = acpi_get_handle(dev);
1186ce619b2eSNate Lawson     if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
1187ce619b2eSNate Lawson 	return (NULL);
1188ce619b2eSNate Lawson 
1189ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1190ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1191ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1192ce619b2eSNate Lawson 	    return (ids[i]);
1193ce619b2eSNate Lawson     }
1194ce619b2eSNate Lawson     return (NULL);
1195ce619b2eSNate Lawson }
1196ce619b2eSNate Lawson 
1197ce619b2eSNate Lawson static ACPI_STATUS
1198ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1199ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1200ce619b2eSNate Lawson {
1201ce619b2eSNate Lawson     ACPI_HANDLE h;
1202ce619b2eSNate Lawson 
1203df8d2a32SNate Lawson     if (dev == NULL)
1204df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1205df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1206ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1207ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1208ce619b2eSNate Lawson }
1209ce619b2eSNate Lawson 
121010ce62b9SNate Lawson static int
121110ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
121210ce62b9SNate Lawson {
121310ce62b9SNate Lawson     struct acpi_softc *sc;
121410ce62b9SNate Lawson     ACPI_HANDLE handle;
121510ce62b9SNate Lawson     ACPI_STATUS status;
121610ce62b9SNate Lawson     char sxd[8];
121710ce62b9SNate Lawson     int error;
121810ce62b9SNate Lawson 
121910ce62b9SNate Lawson     sc = device_get_softc(bus);
122010ce62b9SNate Lawson     handle = acpi_get_handle(dev);
122110ce62b9SNate Lawson 
122210ce62b9SNate Lawson     /*
122310ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
122410ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
122510ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
122610ce62b9SNate Lawson      * need special handling in their drivers.
122710ce62b9SNate Lawson      */
122810ce62b9SNate Lawson     if (handle == NULL ||
122910ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
123010ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
123110ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
123210ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
123310ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
123410ce62b9SNate Lawson 	return (ENXIO);
123510ce62b9SNate Lawson 
123610ce62b9SNate Lawson     /*
123710ce62b9SNate Lawson      * Override next state with the value from _SxD, if present.  If no
123810ce62b9SNate Lawson      * dstate argument was provided, don't fetch the return value.
123910ce62b9SNate Lawson      */
124010ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
124110ce62b9SNate Lawson     if (dstate)
124210ce62b9SNate Lawson 	status = acpi_GetInteger(handle, sxd, dstate);
124310ce62b9SNate Lawson     else
124410ce62b9SNate Lawson 	status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
124510ce62b9SNate Lawson 
124610ce62b9SNate Lawson     switch (status) {
124710ce62b9SNate Lawson     case AE_OK:
124810ce62b9SNate Lawson 	error = 0;
124910ce62b9SNate Lawson 	break;
125010ce62b9SNate Lawson     case AE_NOT_FOUND:
125110ce62b9SNate Lawson 	error = ESRCH;
125210ce62b9SNate Lawson 	break;
125310ce62b9SNate Lawson     default:
125410ce62b9SNate Lawson 	error = ENXIO;
125510ce62b9SNate Lawson 	break;
125610ce62b9SNate Lawson     }
125710ce62b9SNate Lawson 
125810ce62b9SNate Lawson     return (error);
125910ce62b9SNate Lawson }
126010ce62b9SNate Lawson 
1261df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1262df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1263df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1264df8d2a32SNate Lawson     void		*arg;
1265df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1266df8d2a32SNate Lawson };
1267df8d2a32SNate Lawson 
1268ce619b2eSNate Lawson static ACPI_STATUS
1269df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1270df8d2a32SNate Lawson {
1271df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1272df8d2a32SNate Lawson     device_t dev, old_dev;
1273df8d2a32SNate Lawson     ACPI_STATUS status;
1274df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1275df8d2a32SNate Lawson 
1276df8d2a32SNate Lawson     /*
1277df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1278df8d2a32SNate Lawson      * the parent where the scan began.
1279df8d2a32SNate Lawson      */
1280df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1281df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1282df8d2a32SNate Lawson 	return (AE_OK);
1283df8d2a32SNate Lawson 
1284df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1285df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1286df8d2a32SNate Lawson 	return (AE_OK);
1287df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1288df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1289df8d2a32SNate Lawson 	return (AE_OK);
1290df8d2a32SNate Lawson 
1291df8d2a32SNate Lawson     /*
1292df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1293df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1294df8d2a32SNate Lawson      */
1295df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1296df8d2a32SNate Lawson     dev = old_dev;
1297df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1298df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1299df8d2a32SNate Lawson 	return (status);
1300df8d2a32SNate Lawson 
1301df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1302df8d2a32SNate Lawson     if (old_dev != NULL) {
1303df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1304df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1305df8d2a32SNate Lawson     }
1306df8d2a32SNate Lawson 
1307df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1308df8d2a32SNate Lawson     if (dev != NULL)
1309df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1310df8d2a32SNate Lawson 
1311df8d2a32SNate Lawson     return (AE_OK);
1312df8d2a32SNate Lawson }
1313df8d2a32SNate Lawson 
1314df8d2a32SNate Lawson static ACPI_STATUS
1315df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1316df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1317ce619b2eSNate Lawson {
1318ce619b2eSNate Lawson     ACPI_HANDLE h;
1319df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1320ce619b2eSNate Lawson 
1321df8d2a32SNate Lawson     if (acpi_disabled("children"))
1322df8d2a32SNate Lawson 	return (AE_OK);
1323df8d2a32SNate Lawson 
1324df8d2a32SNate Lawson     if (dev == NULL)
1325df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1326df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1327ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1328df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1329df8d2a32SNate Lawson     ctx.arg = arg;
1330df8d2a32SNate Lawson     ctx.parent = h;
1331df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1332df8d2a32SNate Lawson 	acpi_device_scan_cb, &ctx, NULL));
1333ce619b2eSNate Lawson }
1334ce619b2eSNate Lawson 
133510ce62b9SNate Lawson /*
133610ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
133710ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
133810ce62b9SNate Lawson  */
133910ce62b9SNate Lawson static int
134010ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state)
134110ce62b9SNate Lawson {
134210ce62b9SNate Lawson     ACPI_HANDLE h;
134310ce62b9SNate Lawson     ACPI_STATUS status;
134410ce62b9SNate Lawson     int error;
134510ce62b9SNate Lawson 
134610ce62b9SNate Lawson     error = 0;
134710ce62b9SNate Lawson     h = acpi_get_handle(child);
134810ce62b9SNate Lawson     if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
134910ce62b9SNate Lawson 	return (EINVAL);
135010ce62b9SNate Lawson     if (h == NULL)
135110ce62b9SNate Lawson 	return (0);
135210ce62b9SNate Lawson 
135310ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
135410ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
135510ce62b9SNate Lawson     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
135610ce62b9SNate Lawson 	&& status != AE_BAD_PARAMETER)
135710ce62b9SNate Lawson 	device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
135810ce62b9SNate Lawson 	    state, acpi_name(h), AcpiFormatException(status));
135910ce62b9SNate Lawson 
136010ce62b9SNate Lawson     return (error);
136110ce62b9SNate Lawson }
136210ce62b9SNate Lawson 
136332d18aa5SMike Smith static int
136432d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
136532d18aa5SMike Smith {
13661e4925e8SNate Lawson     int			result, cid_count, i;
13671e4925e8SNate Lawson     uint32_t		lid, cids[8];
1368bc0f2195SMike Smith 
1369b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1370bc0f2195SMike Smith 
1371bc0f2195SMike Smith     /*
1372bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1373bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1374bc0f2195SMike Smith      * that to happen, so don't ever return it.
1375bc0f2195SMike Smith      */
1376bc0f2195SMike Smith     result = ENXIO;
1377bc0f2195SMike Smith 
1378be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1379b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
13801e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1381bc0f2195SMike Smith     while (ids && ids->ip_id) {
13821e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1383bc0f2195SMike Smith 	    result = 0;
1384bc0f2195SMike Smith 	    goto out;
1385bc0f2195SMike Smith 	}
13861e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
13871e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
13881e4925e8SNate Lawson 		result = 0;
13891e4925e8SNate Lawson 		goto out;
13901e4925e8SNate Lawson 	    }
13911e4925e8SNate Lawson 	}
1392bc0f2195SMike Smith 	ids++;
1393bc0f2195SMike Smith     }
1394be2b1797SNate Lawson 
1395bc0f2195SMike Smith  out:
13969e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
13979e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
13989e0dd54fSNate Lawson 
1399bc0f2195SMike Smith     return_VALUE (result);
1400bc0f2195SMike Smith }
1401bc0f2195SMike Smith 
1402bc0f2195SMike Smith /*
140333332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
140415e32d5dSMike Smith  *
140533332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
140633332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
140733332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
140833332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
140933332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
141015e32d5dSMike Smith  */
141115e32d5dSMike Smith static void
141215e32d5dSMike Smith acpi_probe_children(device_t bus)
141315e32d5dSMike Smith {
141415e32d5dSMike Smith 
1415b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14160ae55423SMike Smith 
141715e32d5dSMike Smith     /*
141815e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1419edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
142015e32d5dSMike Smith      *
142115e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1422be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1423be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1424be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
142515e32d5dSMike Smith      */
14264c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
142733332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1428be2b1797SNate Lawson 	bus, NULL);
142915e32d5dSMike Smith 
1430adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1431adad4744SNate Lawson     acpi_sysres_alloc(bus);
1432adad4744SNate Lawson 
1433edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1434edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1435edc13633SNate Lawson     bus_generic_probe(bus);
1436edc13633SNate Lawson 
1437edc13633SNate Lawson     /* Probe/attach all children, created staticly and from the namespace. */
14384c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
143915e32d5dSMike Smith     bus_generic_attach(bus);
144015e32d5dSMike Smith 
144115e32d5dSMike Smith     /*
144215e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
144315e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
144415e32d5dSMike Smith      */
14454c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
144615e32d5dSMike Smith     bus_generic_attach(bus);
14470ae55423SMike Smith 
144888a79fc0SNate Lawson     /* Attach wake sysctls. */
14495c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
145088a79fc0SNate Lawson 
1451bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
14520ae55423SMike Smith     return_VOID;
145315e32d5dSMike Smith }
145415e32d5dSMike Smith 
1455b82ca9a9SNate Lawson /*
1456b82ca9a9SNate Lawson  * Determine the probe order for a given device and return non-zero if it
1457b82ca9a9SNate Lawson  * should be attached immediately.
1458b82ca9a9SNate Lawson  */
145991233413SNate Lawson static int
1460b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
146191233413SNate Lawson {
146291233413SNate Lawson     int ret;
146391233413SNate Lawson 
1464b82ca9a9SNate Lawson     /*
1465b82ca9a9SNate Lawson      * 1. I/O port and memory system resource holders
1466b82ca9a9SNate Lawson      * 2. Embedded controllers (to handle early accesses)
1467b82ca9a9SNate Lawson      */
146891233413SNate Lawson     ret = 0;
146991233413SNate Lawson     if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) {
147091233413SNate Lawson 	*order = 1;
147191233413SNate Lawson 	ret = 1;
1472b82ca9a9SNate Lawson     } else if (acpi_MatchHid(handle, "PNP0C09")) {
147391233413SNate Lawson 	*order = 2;
147491233413SNate Lawson 	ret = 1;
147591233413SNate Lawson     }
147691233413SNate Lawson 
147791233413SNate Lawson     return (ret);
147891233413SNate Lawson }
147991233413SNate Lawson 
148015e32d5dSMike Smith /*
148115e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
148215e32d5dSMike Smith  * it.
148315e32d5dSMike Smith  */
148415e32d5dSMike Smith static ACPI_STATUS
148515e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
148615e32d5dSMike Smith {
148715e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
148833332dc2SNate Lawson     device_t bus, child;
148991233413SNate Lawson     int order, probe_now;
149033332dc2SNate Lawson     char *handle_str, **search;
149133332dc2SNate Lawson     static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
149215e32d5dSMike Smith 
1493b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14940ae55423SMike Smith 
1495be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
14960ae55423SMike Smith     if (acpi_avoid(handle))
14970ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
14980ae55423SMike Smith 
149991233413SNate Lawson     bus = (device_t)context;
1500b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
150115e32d5dSMike Smith 	switch (type) {
150215e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
150315e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
150415e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
150515e32d5dSMike Smith 	case ACPI_TYPE_POWER:
15060ae55423SMike Smith 	    if (acpi_disabled("children"))
15070ae55423SMike Smith 		break;
1508be2b1797SNate Lawson 
150915e32d5dSMike Smith 	    /*
151033332dc2SNate Lawson 	     * Since we scan from \, be sure to skip system scope objects.
151133332dc2SNate Lawson 	     * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
151233332dc2SNate Lawson 	     */
151333332dc2SNate Lawson 	    handle_str = acpi_name(handle);
151433332dc2SNate Lawson 	    for (search = scopes; *search != NULL; search++) {
151533332dc2SNate Lawson 		if (strcmp(handle_str, *search) == 0)
151633332dc2SNate Lawson 		    break;
151733332dc2SNate Lawson 	    }
151833332dc2SNate Lawson 	    if (*search != NULL)
151933332dc2SNate Lawson 		break;
152033332dc2SNate Lawson 
152133332dc2SNate Lawson 	    /*
152215e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
1523b82ca9a9SNate Lawson 	     * so that the probe/attach passes will run breadth-first.  Orders
1524b82ca9a9SNate Lawson 	     * less than 10 are reserved for special objects (i.e., system
1525b82ca9a9SNate Lawson 	     * resources).  Larger values are used for all other devices.
152615e32d5dSMike Smith 	     */
152733332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1528b82ca9a9SNate Lawson 	    order = (level + 1) * 10;
1529b82ca9a9SNate Lawson 	    probe_now = acpi_probe_order(handle, &order);
153091233413SNate Lawson 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1531bc0f2195SMike Smith 	    if (child == NULL)
1532bc0f2195SMike Smith 		break;
153359a890e6SNate Lawson 
153459a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
153515e32d5dSMike Smith 	    acpi_set_handle(child, handle);
153659a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1537bc0f2195SMike Smith 
1538bc0f2195SMike Smith 	    /*
1539b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1540b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1541b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1542b519f9d6SMike Smith 	     */
1543be2b1797SNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1544b519f9d6SMike Smith 		device_disable(child);
1545b519f9d6SMike Smith 		break;
1546b519f9d6SMike Smith 	    }
1547b519f9d6SMike Smith 
1548b519f9d6SMike Smith 	    /*
1549bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1550bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1551bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1552bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1553bc0f2195SMike Smith 	     * device not to have any resources.
1554bc0f2195SMike Smith 	     */
155572ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1556bc0f2195SMike Smith 
1557f504b607SNate Lawson 	    /* If order was overridden, probe/attach now rather than later. */
155891233413SNate Lawson 	    if (probe_now)
155991233413SNate Lawson 		device_probe_and_attach(child);
15600ae55423SMike Smith 	    break;
156115e32d5dSMike Smith 	}
156215e32d5dSMike Smith     }
1563be2b1797SNate Lawson 
15640ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
156515e32d5dSMike Smith }
156615e32d5dSMike Smith 
156759a890e6SNate Lawson /*
156859a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
156959a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
157059a890e6SNate Lawson  */
157159a890e6SNate Lawson void
157259a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
157359a890e6SNate Lawson {
157459a890e6SNate Lawson }
157559a890e6SNate Lawson 
157615e32d5dSMike Smith static void
157715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
157815e32d5dSMike Smith {
1579d33f4987STakanori Watanabe     ACPI_STATUS	status;
1580eeb3a05fSNate Lawson 
1581eeb3a05fSNate Lawson     /*
158280f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
158380f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
158480f0e4c2SNate Lawson      * an AP.
1585eeb3a05fSNate Lawson      */
1586eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1587904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1588904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1589904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1590904bf0c2SNate Lawson 		   AcpiFormatException(status));
1591904bf0c2SNate Lawson 	    return;
1592904bf0c2SNate Lawson 	}
1593eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
159455398047SNate Lawson 	ACPI_DISABLE_IRQS();
1595865b8d0bSNate Lawson 	status = AcpiEnterSleepState(ACPI_STATE_S5);
1596be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1597bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
159815e32d5dSMike Smith 	} else {
159915e32d5dSMike Smith 	    DELAY(1000000);
160015e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
160115e32d5dSMike Smith 	}
1602ba36768bSNate Lawson     } else if (panicstr == NULL) {
160380f0e4c2SNate Lawson 	printf("Shutting down ACPI\n");
160480f0e4c2SNate Lawson 	AcpiTerminate();
160580f0e4c2SNate Lawson     }
160615e32d5dSMike Smith }
160715e32d5dSMike Smith 
160813d4f7baSMitsuru IWASAKI static void
160913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
161013d4f7baSMitsuru IWASAKI {
161113d4f7baSMitsuru IWASAKI     static int	first_time = 1;
161213d4f7baSMitsuru IWASAKI 
161313d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1614be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
161559ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
161613d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
161733febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1618be2b1797SNate Lawson 	if (first_time)
16196c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
162013d4f7baSMitsuru IWASAKI     }
1621be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
162259ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
162313d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
162433febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1625be2b1797SNate Lawson 	if (first_time)
16266c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
162713d4f7baSMitsuru IWASAKI     }
162813d4f7baSMitsuru IWASAKI 
162913d4f7baSMitsuru IWASAKI     first_time = 0;
163013d4f7baSMitsuru IWASAKI }
163113d4f7baSMitsuru IWASAKI 
163215e32d5dSMike Smith /*
1633c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1634c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1635c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1636c5ba0be4SMike Smith  */
1637c5ba0be4SMike Smith BOOLEAN
1638c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1639c5ba0be4SMike Smith {
16401e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1641c5ba0be4SMike Smith     ACPI_HANDLE		h;
16421e4925e8SNate Lawson     ACPI_BUFFER		buf;
1643c5ba0be4SMike Smith     ACPI_STATUS		error;
16441e4925e8SNate Lawson     int			ret;
1645c5ba0be4SMike Smith 
16461e4925e8SNate Lawson     ret = FALSE;
1647c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1648c5ba0be4SMike Smith 	return (FALSE);
16491e4925e8SNate Lawson     buf.Pointer = NULL;
16501e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
16516fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
16526fca9360SNate Lawson     if (ACPI_FAILURE(error))
1653c5ba0be4SMike Smith 	return (FALSE);
16541e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1655be2b1797SNate Lawson 
1656be2b1797SNate Lawson     /* If no _STA method, must be present */
16571e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
16581e4925e8SNate Lawson 	ret = TRUE;
1659be2b1797SNate Lawson 
1660be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
166117dbe0f7SNate Lawson     if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
16621e4925e8SNate Lawson 	ret = TRUE;
1663be2b1797SNate Lawson 
16641e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
16651e4925e8SNate Lawson     return (ret);
1666c5ba0be4SMike Smith }
1667c5ba0be4SMike Smith 
1668c5ba0be4SMike Smith /*
1669b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1670b53f2771SMike Smith  */
1671b53f2771SMike Smith BOOLEAN
1672b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1673b53f2771SMike Smith {
16741e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1675b53f2771SMike Smith     ACPI_HANDLE		h;
16761e4925e8SNate Lawson     ACPI_BUFFER		buf;
1677b53f2771SMike Smith     ACPI_STATUS		error;
16781e4925e8SNate Lawson     int			ret;
1679b53f2771SMike Smith 
16801e4925e8SNate Lawson     ret = FALSE;
1681b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1682b53f2771SMike Smith 	return (FALSE);
16831e4925e8SNate Lawson     buf.Pointer = NULL;
16841e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
16856fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
16866fca9360SNate Lawson     if (ACPI_FAILURE(error))
1687b53f2771SMike Smith 	return (FALSE);
16881e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1689be2b1797SNate Lawson 
1690be2b1797SNate Lawson     /* If no _STA method, must be present */
16911e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
16921e4925e8SNate Lawson 	ret = TRUE;
1693be2b1797SNate Lawson 
169417dbe0f7SNate Lawson     /* Return true for 'present', 'battery present', and 'functioning' */
169517dbe0f7SNate Lawson     if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
16961e4925e8SNate Lawson 	ret = TRUE;
1697be2b1797SNate Lawson 
16981e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
16991e4925e8SNate Lawson     return (ret);
1700b53f2771SMike Smith }
1701b53f2771SMike Smith 
1702b53f2771SMike Smith /*
170391233413SNate Lawson  * Match a HID string against a handle
170415e32d5dSMike Smith  */
1705ce619b2eSNate Lawson static BOOLEAN
1706ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
170715e32d5dSMike Smith {
17081e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
17091e4925e8SNate Lawson     ACPI_BUFFER		buf;
171015e32d5dSMike Smith     ACPI_STATUS		error;
17111e4925e8SNate Lawson     int			ret, i;
171215e32d5dSMike Smith 
17131e4925e8SNate Lawson     ret = FALSE;
171491233413SNate Lawson     if (hid == NULL || h == NULL)
171591233413SNate Lawson 	return (ret);
17161e4925e8SNate Lawson     buf.Pointer = NULL;
17171e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17186fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17196fca9360SNate Lawson     if (ACPI_FAILURE(error))
172091233413SNate Lawson 	return (ret);
17211e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1722be2b1797SNate Lawson 
1723c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1724c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
17251e4925e8SNate Lawson 	    ret = TRUE;
1726c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
17271e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
17281e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
17291e4925e8SNate Lawson 		ret = TRUE;
17301e4925e8SNate Lawson 		break;
17311e4925e8SNate Lawson 	    }
17321e4925e8SNate Lawson 	}
17331e4925e8SNate Lawson     }
1734be2b1797SNate Lawson 
17351e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17361e4925e8SNate Lawson     return (ret);
173715e32d5dSMike Smith }
173815e32d5dSMike Smith 
173915e32d5dSMike Smith /*
1740c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1741c5ba0be4SMike Smith  * or one if its parents.
1742c5ba0be4SMike Smith  */
1743c5ba0be4SMike Smith ACPI_STATUS
1744c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1745c5ba0be4SMike Smith {
1746c5ba0be4SMike Smith     ACPI_HANDLE		r;
1747c5ba0be4SMike Smith     ACPI_STATUS		status;
1748c5ba0be4SMike Smith 
1749be2b1797SNate Lawson     /* Walk back up the tree to the root */
1750c5ba0be4SMike Smith     for (;;) {
1751be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1752be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1753c5ba0be4SMike Smith 	    *result = r;
1754c5ba0be4SMike Smith 	    return (AE_OK);
1755c5ba0be4SMike Smith 	}
1756bee4aa4aSNate Lawson 	/* XXX Return error here? */
1757c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1758c5ba0be4SMike Smith 	    return (AE_OK);
1759b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1760c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1761c5ba0be4SMike Smith 	parent = r;
1762c5ba0be4SMike Smith     }
1763c5ba0be4SMike Smith }
1764c5ba0be4SMike Smith 
1765eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1766eea17c34SNate Lawson uint32_t
1767eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1768eea17c34SNate Lawson {
1769eea17c34SNate Lawson     uint32_t delta;
1770eea17c34SNate Lawson 
1771eea17c34SNate Lawson     if (end >= start)
1772eea17c34SNate Lawson 	delta = end - start;
1773eea17c34SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
17748d01ceefSNate Lawson 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1775eea17c34SNate Lawson     else
1776eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
1777eea17c34SNate Lawson     return (delta);
1778eea17c34SNate Lawson }
1779eea17c34SNate Lawson 
1780c5ba0be4SMike Smith /*
1781c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1782c5ba0be4SMike Smith  */
1783c5ba0be4SMike Smith ACPI_BUFFER *
1784c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1785c5ba0be4SMike Smith {
1786c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1787c5ba0be4SMike Smith 
1788c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1789c5ba0be4SMike Smith 	return (NULL);
1790c5ba0be4SMike Smith     buf->Length = size;
1791c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1792c5ba0be4SMike Smith     return (buf);
1793c5ba0be4SMike Smith }
1794c5ba0be4SMike Smith 
1795c310653eSNate Lawson ACPI_STATUS
1796cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1797c310653eSNate Lawson {
1798c310653eSNate Lawson     ACPI_OBJECT arg1;
1799c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1800c310653eSNate Lawson 
1801c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1802c310653eSNate Lawson     arg1.Integer.Value = number;
1803c310653eSNate Lawson     args.Count = 1;
1804c310653eSNate Lawson     args.Pointer = &arg1;
1805c310653eSNate Lawson 
1806c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1807c310653eSNate Lawson }
1808c310653eSNate Lawson 
1809c5ba0be4SMike Smith /*
1810c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1811c5ba0be4SMike Smith  */
1812c5ba0be4SMike Smith ACPI_STATUS
1813cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1814c5ba0be4SMike Smith {
1815be2b1797SNate Lawson     ACPI_STATUS	status;
1816c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1817c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1818c5ba0be4SMike Smith 
1819c5ba0be4SMike Smith     if (handle == NULL)
1820c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
18210c7da7acSJohn Baldwin 
18220c7da7acSJohn Baldwin     /*
18230c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
18240c7da7acSJohn Baldwin      * a method that will return an Integer.
18250c7da7acSJohn Baldwin      */
1826c5ba0be4SMike Smith     buf.Pointer = &param;
1827c5ba0be4SMike Smith     buf.Length = sizeof(param);
1828be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1829be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1830be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1831c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1832be2b1797SNate Lawson 	else
1833be2b1797SNate Lawson 	    status = AE_TYPE;
1834c5ba0be4SMike Smith     }
18350c7da7acSJohn Baldwin 
18360c7da7acSJohn Baldwin     /*
18370c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
18380c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
18390c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
18400c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
18410c7da7acSJohn Baldwin      *
18420c7da7acSJohn Baldwin      * This is a hack.
18430c7da7acSJohn Baldwin      */
1844be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1845b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1846be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
18470c7da7acSJohn Baldwin 	} else {
1848be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1849be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1850be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
18510c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
18520c7da7acSJohn Baldwin 	}
1853f97739daSNate Lawson     }
1854be2b1797SNate Lawson     return (status);
1855c5ba0be4SMike Smith }
1856c5ba0be4SMike Smith 
1857c573e654SMitsuru IWASAKI ACPI_STATUS
1858cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1859c573e654SMitsuru IWASAKI {
1860c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1861dba55fa2SNate Lawson     UINT8	*val;
1862c573e654SMitsuru IWASAKI     int		i;
1863c573e654SMitsuru IWASAKI 
1864c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1865c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1866c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1867c573e654SMitsuru IWASAKI 	return (AE_OK);
1868c573e654SMitsuru IWASAKI     }
1869c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1870c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1871c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1872c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1873be2b1797SNate Lawson 
1874c573e654SMitsuru IWASAKI     *number = 0;
1875dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1876c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1877dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1878c573e654SMitsuru IWASAKI     return (AE_OK);
1879c573e654SMitsuru IWASAKI }
1880c573e654SMitsuru IWASAKI 
1881c5ba0be4SMike Smith /*
1882c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1883c5ba0be4SMike Smith  * function for each element.
1884c5ba0be4SMike Smith  *
1885c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1886c5ba0be4SMike Smith  */
1887c5ba0be4SMike Smith ACPI_STATUS
1888be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1889be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1890c5ba0be4SMike Smith {
1891c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1892c5ba0be4SMike Smith     int		i;
1893c5ba0be4SMike Smith 
1894be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1895c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1896c5ba0be4SMike Smith 
1897be2b1797SNate Lawson     /* Iterate over components */
1898be2b1797SNate Lawson     i = 0;
1899be2b1797SNate Lawson     comp = pkg->Package.Elements;
1900be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1901c5ba0be4SMike Smith 	func(comp, arg);
1902c5ba0be4SMike Smith 
1903c5ba0be4SMike Smith     return (AE_OK);
1904c5ba0be4SMike Smith }
1905c5ba0be4SMike Smith 
19066f69255bSMike Smith /*
19076f69255bSMike Smith  * Find the (index)th resource object in a set.
19086f69255bSMike Smith  */
19096f69255bSMike Smith ACPI_STATUS
19106d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
19116f69255bSMike Smith {
19126d63101aSMike Smith     ACPI_RESOURCE	*rp;
19136f69255bSMike Smith     int			i;
19146f69255bSMike Smith 
19156d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
19166f69255bSMike Smith     i = index;
19176d63101aSMike Smith     while (i-- > 0) {
1918be2b1797SNate Lawson 	/* Range check */
19196d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
19206f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1921be2b1797SNate Lawson 
1922be2b1797SNate Lawson 	/* Check for terminator */
1923be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
19246d63101aSMike Smith 	    return (AE_NOT_FOUND);
1925abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
19266f69255bSMike Smith     }
19276f69255bSMike Smith     if (resp != NULL)
19286d63101aSMike Smith 	*resp = rp;
1929be2b1797SNate Lawson 
19306d63101aSMike Smith     return (AE_OK);
19316d63101aSMike Smith }
19326d63101aSMike Smith 
19336d63101aSMike Smith /*
19346d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
19356d63101aSMike Smith  *
19366d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
19376d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
19386d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
19396d63101aSMike Smith  * resources.
19406d63101aSMike Smith  */
19416d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
19426d63101aSMike Smith 
19436d63101aSMike Smith ACPI_STATUS
19446d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
19456d63101aSMike Smith {
19466d63101aSMike Smith     ACPI_RESOURCE	*rp;
19476d63101aSMike Smith     void		*newp;
19486d63101aSMike Smith 
1949be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
19506d63101aSMike Smith     if (buf->Pointer == NULL) {
19516d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
19526d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
19536d63101aSMike Smith 	    return (AE_NO_MEMORY);
19546d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
19556d63101aSMike Smith 	rp->Id = ACPI_RSTYPE_END_TAG;
19566d63101aSMike Smith 	rp->Length = 0;
19576d63101aSMike Smith     }
19586d63101aSMike Smith     if (res == NULL)
19596d63101aSMike Smith 	return (AE_OK);
19606d63101aSMike Smith 
19616d63101aSMike Smith     /*
19626d63101aSMike Smith      * Scan the current buffer looking for the terminator.
19636d63101aSMike Smith      * This will either find the terminator or hit the end
19646d63101aSMike Smith      * of the buffer and return an error.
19656d63101aSMike Smith      */
19666d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
19676d63101aSMike Smith     for (;;) {
1968be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
19696d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
19706d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
1971be2b1797SNate Lawson 	if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
19726d63101aSMike Smith 	    break;
1973abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
19746d63101aSMike Smith     }
19756d63101aSMike Smith 
19766d63101aSMike Smith     /*
19776d63101aSMike Smith      * Check the size of the buffer and expand if required.
19786d63101aSMike Smith      *
19796d63101aSMike Smith      * Required size is:
19806d63101aSMike Smith      *	size of existing resources before terminator +
19816d63101aSMike Smith      *	size of new resource and header +
19826d63101aSMike Smith      * 	size of terminator.
19836d63101aSMike Smith      *
19846d63101aSMike Smith      * Note that this loop should really only run once, unless
19856d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
19866d63101aSMike Smith      */
19876d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
19886d63101aSMike Smith 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
19896d63101aSMike Smith 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
19906d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
19916d63101aSMike Smith 	    return (AE_NO_MEMORY);
19926d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
1993a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1994a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
19956d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
19966d63101aSMike Smith 	buf->Pointer = newp;
19976d63101aSMike Smith 	buf->Length += buf->Length;
19986d63101aSMike Smith     }
19996d63101aSMike Smith 
2000be2b1797SNate Lawson     /* Insert the new resource. */
20016d63101aSMike Smith     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
20026d63101aSMike Smith 
2003be2b1797SNate Lawson     /* And add the terminator. */
2004abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
20056d63101aSMike Smith     rp->Id = ACPI_RSTYPE_END_TAG;
20066d63101aSMike Smith     rp->Length = 0;
20076d63101aSMike Smith 
20086f69255bSMike Smith     return (AE_OK);
20096f69255bSMike Smith }
2010c5ba0be4SMike Smith 
2011da14ac9fSJohn Baldwin /*
2012da14ac9fSJohn Baldwin  * Set interrupt model.
2013da14ac9fSJohn Baldwin  */
2014da14ac9fSJohn Baldwin ACPI_STATUS
2015da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2016da14ac9fSJohn Baldwin {
2017da14ac9fSJohn Baldwin 
2018c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2019da14ac9fSJohn Baldwin }
2020da14ac9fSJohn Baldwin 
2021ece50487SMitsuru IWASAKI static void
2022ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2023ece50487SMitsuru IWASAKI {
2024bee4aa4aSNate Lawson 
2025ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
2026ece50487SMitsuru IWASAKI }
2027c30382dfSMitsuru IWASAKI 
202815e2f34fSNate Lawson enum acpi_sleep_state {
202915e2f34fSNate Lawson     ACPI_SS_NONE,
203015e2f34fSNate Lawson     ACPI_SS_GPE_SET,
203115e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
203215e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
203315e2f34fSNate Lawson     ACPI_SS_SLEPT,
203415e2f34fSNate Lawson };
203515e2f34fSNate Lawson 
203615e32d5dSMike Smith /*
203715e32d5dSMike Smith  * Set the system sleep state
203815e32d5dSMike Smith  *
2039be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
204015e32d5dSMike Smith  */
204115e32d5dSMike Smith ACPI_STATUS
204215e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
204315e32d5dSMike Smith {
204415e2f34fSNate Lawson     ACPI_STATUS	status;
204556d8cb57SMitsuru IWASAKI     UINT8	TypeA;
204656d8cb57SMitsuru IWASAKI     UINT8	TypeB;
204715e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
204815e32d5dSMike Smith 
2049b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
20500ae55423SMike Smith 
205115e2f34fSNate Lawson     status = AE_OK;
205215e2f34fSNate Lawson     ACPI_LOCK(acpi);
205315e2f34fSNate Lawson     if (sc->acpi_sleep_disabled) {
20546397624dSMitsuru IWASAKI 	if (sc->acpi_sstate != ACPI_STATE_S0)
205515e2f34fSNate Lawson 	    status = AE_ERROR;
205615e2f34fSNate Lawson 	ACPI_UNLOCK(acpi);
205715e2f34fSNate Lawson 	printf("acpi: suspend request ignored (not ready yet)\n");
205815e2f34fSNate Lawson 	return (status);
205915e2f34fSNate Lawson     }
206015e2f34fSNate Lawson     sc->acpi_sleep_disabled = 1;
206115e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
20626397624dSMitsuru IWASAKI 
20635d3d03f1SNate Lawson     /*
20645d3d03f1SNate Lawson      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
20655d3d03f1SNate Lawson      * drivers need this.
20665d3d03f1SNate Lawson      */
20675d3d03f1SNate Lawson     mtx_lock(&Giant);
206815e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
206915e32d5dSMike Smith     switch (state) {
207015e32d5dSMike Smith     case ACPI_STATE_S1:
20716161544cSTakanori Watanabe     case ACPI_STATE_S2:
20726161544cSTakanori Watanabe     case ACPI_STATE_S3:
20736161544cSTakanori Watanabe     case ACPI_STATE_S4:
2074adad4744SNate Lawson 	status = AcpiGetSleepTypeData(state, &TypeA, &TypeB);
207598175614SNate Lawson 	if (status == AE_NOT_FOUND) {
207698175614SNate Lawson 	    device_printf(sc->acpi_dev,
207798175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
207898175614SNate Lawson 	    break;
207998175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
2080be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
2081be2b1797SNate Lawson 			  AcpiFormatException(status));
208256d8cb57SMitsuru IWASAKI 	    break;
208356d8cb57SMitsuru IWASAKI 	}
208456d8cb57SMitsuru IWASAKI 
2085a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
2086a1fccb47SMitsuru IWASAKI 
2087d4b9ff91SNate Lawson 	/* Enable any GPEs as appropriate and requested by the user. */
2088d4b9ff91SNate Lawson 	acpi_wake_prep_walk(state);
208915e2f34fSNate Lawson 	slp_state = ACPI_SS_GPE_SET;
2090e8b4d56eSNate Lawson 
209115e32d5dSMike Smith 	/*
2092c7f88fc3SNate Lawson 	 * Inform all devices that we are going to sleep.  If at least one
2093c7f88fc3SNate Lawson 	 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
209415e32d5dSMike Smith 	 *
2095c7f88fc3SNate Lawson 	 * XXX Note that a better two-pass approach with a 'veto' pass
2096c7f88fc3SNate Lawson 	 * followed by a "real thing" pass would be better, but the current
2097c7f88fc3SNate Lawson 	 * bus interface does not provide for this.
209815e32d5dSMike Smith 	 */
209915e2f34fSNate Lawson 	if (DEVICE_SUSPEND(root_bus) != 0) {
210015e2f34fSNate Lawson 	    device_printf(sc->acpi_dev, "device_suspend failed\n");
210115e2f34fSNate Lawson 	    break;
210215e2f34fSNate Lawson 	}
210315e2f34fSNate Lawson 	slp_state = ACPI_SS_DEV_SUSPEND;
2104b9c780d6SMitsuru IWASAKI 
2105be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
2106be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2107b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2108b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
2109b9c780d6SMitsuru IWASAKI 	    break;
2110b9c780d6SMitsuru IWASAKI 	}
211115e2f34fSNate Lawson 	slp_state = ACPI_SS_SLP_PREP;
2112b9c780d6SMitsuru IWASAKI 
2113be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
2114ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
2115ff01efb5SMitsuru IWASAKI 
21166161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
21176161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
21186161544cSTakanori Watanabe 
21196161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
2120be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
2121964679ceSMitsuru IWASAKI 		AcpiEnable();
21226161544cSTakanori Watanabe 	} else {
2123c7f88fc3SNate Lawson 	    ACPI_DISABLE_IRQS();
2124adad4744SNate Lawson 	    status = AcpiEnterSleepState(state);
2125be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
2126be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2127be2b1797SNate Lawson 			      AcpiFormatException(status));
2128c30382dfSMitsuru IWASAKI 		break;
212915e32d5dSMike Smith 	    }
21306161544cSTakanori Watanabe 	}
213115e2f34fSNate Lawson 	slp_state = ACPI_SS_SLEPT;
213215e32d5dSMike Smith 	break;
213315e32d5dSMike Smith     case ACPI_STATE_S5:
213415e32d5dSMike Smith 	/*
213515e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
213615e32d5dSMike Smith 	 * shutdown handlers.
213715e32d5dSMike Smith 	 */
213815e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
213915e32d5dSMike Smith 	break;
214098175614SNate Lawson     case ACPI_STATE_S0:
214115e32d5dSMike Smith     default:
214215e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
214315e32d5dSMike Smith 	break;
214415e32d5dSMike Smith     }
2145ece50487SMitsuru IWASAKI 
214615e2f34fSNate Lawson     /*
214715e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
214815e2f34fSNate Lawson      * process.  This handles both the error and success cases.
214915e2f34fSNate Lawson      */
215015e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
215115e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
215215e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
215315e2f34fSNate Lawson     }
215415e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLP_PREP)
215515e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
2156071339e2SNate Lawson     if (slp_state >= ACPI_SS_DEV_SUSPEND)
2157071339e2SNate Lawson 	DEVICE_RESUME(root_bus);
215815e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLEPT)
215915e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
216015e2f34fSNate Lawson 
216115e2f34fSNate Lawson     /* Allow another sleep request after a while. */
216215e2f34fSNate Lawson     if (state != ACPI_STATE_S5)
2163ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
2164ece50487SMitsuru IWASAKI 
21655d3d03f1SNate Lawson     mtx_unlock(&Giant);
21660ae55423SMike Smith     return_ACPI_STATUS (status);
216715e32d5dSMike Smith }
216815e32d5dSMike Smith 
2169e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
2170e8b4d56eSNate Lawson int
2171e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
2172e8b4d56eSNate Lawson {
2173e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2174e8b4d56eSNate Lawson 
2175e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
2176e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2177e8b4d56eSNate Lawson 	return (ENXIO);
2178e8b4d56eSNate Lawson 
2179e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
2180e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
2181e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
2182e8b4d56eSNate Lawson 	return (ENXIO);
2183e8b4d56eSNate Lawson     }
2184e8b4d56eSNate Lawson 
2185e8b4d56eSNate Lawson     return (0);
2186e8b4d56eSNate Lawson }
2187e8b4d56eSNate Lawson 
2188e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
2189e8b4d56eSNate Lawson int
2190e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
2191e8b4d56eSNate Lawson {
2192e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2193e8b4d56eSNate Lawson     ACPI_HANDLE handle;
2194e8b4d56eSNate Lawson     ACPI_STATUS status;
2195e8b4d56eSNate Lawson     int flags;
2196e8b4d56eSNate Lawson 
2197d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
2198e8b4d56eSNate Lawson     handle = acpi_get_handle(dev);
2199e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2200e8b4d56eSNate Lawson 	return (ENXIO);
2201e8b4d56eSNate Lawson 
2202d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
2203e8b4d56eSNate Lawson     if (enable) {
2204e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2205e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2206e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
2207e8b4d56eSNate Lawson 	    return (ENXIO);
2208e8b4d56eSNate Lawson 	}
2209d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2210e8b4d56eSNate Lawson     } else {
2211e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2212e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2213e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
2214e8b4d56eSNate Lawson 	    return (ENXIO);
2215e8b4d56eSNate Lawson 	}
2216d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2217e8b4d56eSNate Lawson     }
2218e8b4d56eSNate Lawson 
2219e8b4d56eSNate Lawson     return (0);
2220e8b4d56eSNate Lawson }
2221e8b4d56eSNate Lawson 
2222d4b9ff91SNate Lawson static int
2223d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2224e8b4d56eSNate Lawson {
2225e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2226d4b9ff91SNate Lawson     device_t dev;
2227e8b4d56eSNate Lawson 
2228d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
2229e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2230e8b4d56eSNate Lawson 	return (ENXIO);
2231d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2232e8b4d56eSNate Lawson 
2233e8b4d56eSNate Lawson     /*
2234d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
2235d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
2236d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
2237d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
2238d4b9ff91SNate Lawson      * and set _PSW.
2239e8b4d56eSNate Lawson      */
2240d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2241cc85c78cSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2242e8b4d56eSNate Lawson 	if (bootverbose)
2243d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2244d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2245d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2246d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
2247d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
2248d4b9ff91SNate Lawson 	if (bootverbose)
2249d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2250d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2251d4b9ff91SNate Lawson     }
2252cc85c78cSNate Lawson 
2253cc85c78cSNate Lawson     return (0);
2254e8b4d56eSNate Lawson }
2255e8b4d56eSNate Lawson 
2256d4b9ff91SNate Lawson static int
2257d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2258cc85c78cSNate Lawson {
2259cc85c78cSNate Lawson     struct acpi_prw_data prw;
2260d4b9ff91SNate Lawson     device_t dev;
2261cc85c78cSNate Lawson 
2262cc85c78cSNate Lawson     /*
2263d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
2264d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
2265cc85c78cSNate Lawson      */
2266d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2267d4b9ff91SNate Lawson 	return (ENXIO);
2268d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2269d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2270d4b9ff91SNate Lawson 	return (0);
2271cc85c78cSNate Lawson 
2272d4b9ff91SNate Lawson     /*
2273d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
2274d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
2275d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
2276d4b9ff91SNate Lawson      */
2277d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2278cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2279d4b9ff91SNate Lawson 	if (bootverbose)
2280d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2281d4b9ff91SNate Lawson     } else {
2282d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
2283d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
2284d4b9ff91SNate Lawson 	if (bootverbose)
2285d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
2286d4b9ff91SNate Lawson 		acpi_name(handle));
2287d4b9ff91SNate Lawson     }
2288d4b9ff91SNate Lawson 
2289e8b4d56eSNate Lawson     return (0);
2290e8b4d56eSNate Lawson }
2291e8b4d56eSNate Lawson 
2292e8b4d56eSNate Lawson static ACPI_STATUS
2293d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2294e8b4d56eSNate Lawson {
2295d4b9ff91SNate Lawson     int sstate;
2296e8b4d56eSNate Lawson 
2297d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
2298d4b9ff91SNate Lawson     sstate = *(int *)context;
2299d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
2300d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
2301d4b9ff91SNate Lawson     else
2302d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
2303e8b4d56eSNate Lawson     return (AE_OK);
2304e8b4d56eSNate Lawson }
2305e8b4d56eSNate Lawson 
2306d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2307e8b4d56eSNate Lawson static int
2308d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
2309e8b4d56eSNate Lawson {
2310e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
2311e8b4d56eSNate Lawson 
2312e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2313d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
2314d4b9ff91SNate Lawson 	    acpi_wake_prep, &sstate, NULL);
2315e8b4d56eSNate Lawson     return (0);
2316e8b4d56eSNate Lawson }
2317e8b4d56eSNate Lawson 
231888a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
231988a79fc0SNate Lawson static int
232088a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
232188a79fc0SNate Lawson {
232288a79fc0SNate Lawson     int error, i, numdevs;
232388a79fc0SNate Lawson     device_t *devlist;
232488a79fc0SNate Lawson     device_t child;
2325d4b9ff91SNate Lawson     ACPI_STATUS status;
232688a79fc0SNate Lawson 
232788a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
232888a79fc0SNate Lawson     if (error != 0 || numdevs == 0)
232988a79fc0SNate Lawson 	return (error);
233088a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
233188a79fc0SNate Lawson 	child = devlist[i];
2332d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
233388a79fc0SNate Lawson 	if (!device_is_attached(child))
233488a79fc0SNate Lawson 	    continue;
2335d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2336d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
233788a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
233888a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
233988a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
234088a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
234188a79fc0SNate Lawson 	}
234288a79fc0SNate Lawson     }
234388a79fc0SNate Lawson     free(devlist, M_TEMP);
234488a79fc0SNate Lawson 
234588a79fc0SNate Lawson     return (0);
234688a79fc0SNate Lawson }
234788a79fc0SNate Lawson 
234888a79fc0SNate Lawson /* Enable or disable wake from userland. */
234988a79fc0SNate Lawson static int
235088a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
235188a79fc0SNate Lawson {
235288a79fc0SNate Lawson     int enable, error;
235388a79fc0SNate Lawson     device_t dev;
235488a79fc0SNate Lawson 
235588a79fc0SNate Lawson     dev = (device_t)arg1;
2356d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
235788a79fc0SNate Lawson 
235888a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
235988a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
236088a79fc0SNate Lawson 	return (error);
236188a79fc0SNate Lawson     if (enable != 0 && enable != 1)
236288a79fc0SNate Lawson 	return (EINVAL);
236388a79fc0SNate Lawson 
236488a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
236588a79fc0SNate Lawson }
236688a79fc0SNate Lawson 
2367e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
2368d4b9ff91SNate Lawson int
2369e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2370e8b4d56eSNate Lawson {
2371e8b4d56eSNate Lawson     ACPI_STATUS			status;
2372e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2373e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2374d4b9ff91SNate Lawson     int				error, i, power_count;
2375e8b4d56eSNate Lawson 
2376e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2377e8b4d56eSNate Lawson 	return (EINVAL);
2378e8b4d56eSNate Lawson 
2379e8b4d56eSNate Lawson     /*
2380e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2381e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2382e8b4d56eSNate Lawson      */
2383e8b4d56eSNate Lawson     error = EINVAL;
2384e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2385e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2386e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2387e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2388e8b4d56eSNate Lawson 	return (ENOENT);
2389e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2390e8b4d56eSNate Lawson     if (res == NULL)
2391e8b4d56eSNate Lawson 	return (ENOENT);
2392e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2393e8b4d56eSNate Lawson 	goto out;
2394e8b4d56eSNate Lawson 
2395e8b4d56eSNate Lawson     /*
2396e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2397e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2398e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2399e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2400e8b4d56eSNate Lawson      */
2401e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2402e8b4d56eSNate Lawson 	goto out;
2403e8b4d56eSNate Lawson 
2404e8b4d56eSNate Lawson     /*
2405e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2406e8b4d56eSNate Lawson      */
2407e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2408e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2409e8b4d56eSNate Lawson 	/*
2410e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2411e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2412e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2413e8b4d56eSNate Lawson 	 * enabled for the wake event.
2414e8b4d56eSNate Lawson 	 */
2415e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2416e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2417e8b4d56eSNate Lawson 	error = 0;
2418e8b4d56eSNate Lawson 	break;
2419e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2420e8b4d56eSNate Lawson 	/*
2421e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2422e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2423e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2424e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2425e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2426e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2427e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2428e8b4d56eSNate Lawson 	 * the wake event.
2429e8b4d56eSNate Lawson 	 *
2430e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2431e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2432e8b4d56eSNate Lawson 	 */
2433e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2434e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2435e8b4d56eSNate Lawson 	    goto out;
2436e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2437e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2438e8b4d56eSNate Lawson 	    goto out;
2439e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2440e8b4d56eSNate Lawson 	    goto out;
2441e8b4d56eSNate Lawson 	error = 0;
2442e8b4d56eSNate Lawson 	break;
2443e8b4d56eSNate Lawson     default:
2444e8b4d56eSNate Lawson 	goto out;
2445e8b4d56eSNate Lawson     }
2446e8b4d56eSNate Lawson 
2447d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
2448d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
2449d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
2450d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
2451d4b9ff91SNate Lawson 	power_count = 0;
2452d4b9ff91SNate Lawson     }
2453d4b9ff91SNate Lawson     prw->power_res_count = power_count;
2454d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
2455d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
2456e8b4d56eSNate Lawson 
2457e8b4d56eSNate Lawson out:
2458e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2459e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2460e8b4d56eSNate Lawson     return (error);
2461e8b4d56eSNate Lawson }
2462e8b4d56eSNate Lawson 
246315e32d5dSMike Smith /*
246415e32d5dSMike Smith  * ACPI Event Handlers
246515e32d5dSMike Smith  */
246615e32d5dSMike Smith 
246715e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
246815e32d5dSMike Smith 
246915e32d5dSMike Smith static void
247015e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
247115e32d5dSMike Smith {
2472bee4aa4aSNate Lawson 
2473b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
247415e32d5dSMike Smith 
2475a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
247615e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
2477bee4aa4aSNate Lawson 
24780ae55423SMike Smith     return_VOID;
247915e32d5dSMike Smith }
248015e32d5dSMike Smith 
248115e32d5dSMike Smith static void
248215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
248315e32d5dSMike Smith {
2484bee4aa4aSNate Lawson 
2485b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
24860ae55423SMike Smith 
2487bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
2488cb9b0d80SMike Smith 
24890ae55423SMike Smith     return_VOID;
249015e32d5dSMike Smith }
249115e32d5dSMike Smith 
249215e32d5dSMike Smith /*
249315e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
249415e32d5dSMike Smith  */
249515e32d5dSMike Smith UINT32
249633febf93SNate Lawson acpi_event_power_button_sleep(void *context)
249715e32d5dSMike Smith {
249815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
249915e32d5dSMike Smith 
2500b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25010ae55423SMike Smith 
250215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
25030ae55423SMike Smith 
2504b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
250515e32d5dSMike Smith }
250615e32d5dSMike Smith 
250715e32d5dSMike Smith UINT32
250833febf93SNate Lawson acpi_event_power_button_wake(void *context)
250915e32d5dSMike Smith {
251015e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
251115e32d5dSMike Smith 
2512b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25130ae55423SMike Smith 
251415e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
25150ae55423SMike Smith 
2516b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
251715e32d5dSMike Smith }
251815e32d5dSMike Smith 
251915e32d5dSMike Smith UINT32
252033febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
252115e32d5dSMike Smith {
252215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
252315e32d5dSMike Smith 
2524b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25250ae55423SMike Smith 
252615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
25270ae55423SMike Smith 
2528b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
252915e32d5dSMike Smith }
253015e32d5dSMike Smith 
253115e32d5dSMike Smith UINT32
253233febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
253315e32d5dSMike Smith {
253415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
253515e32d5dSMike Smith 
2536b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25370ae55423SMike Smith 
253815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
25390ae55423SMike Smith 
2540b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
254115e32d5dSMike Smith }
254215e32d5dSMike Smith 
254315e32d5dSMike Smith /*
2544bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
2545bee4aa4aSNate Lawson  * use this for single-threaded callers.
254615e32d5dSMike Smith  */
254715e32d5dSMike Smith char *
254815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
254915e32d5dSMike Smith {
2550bee4aa4aSNate Lawson     ACPI_BUFFER buf;
2551bee4aa4aSNate Lawson     static char data[256];
255215e32d5dSMike Smith 
2553bee4aa4aSNate Lawson     buf.Length = sizeof(data);
2554bee4aa4aSNate Lawson     buf.Pointer = data;
2555cb9b0d80SMike Smith 
25560a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
2557bee4aa4aSNate Lawson 	return (data);
2558bee4aa4aSNate Lawson     return ("(unknown)");
255915e32d5dSMike Smith }
256015e32d5dSMike Smith 
256115e32d5dSMike Smith /*
256215e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
256315e32d5dSMike Smith  * parts of the namespace.
256415e32d5dSMike Smith  */
256515e32d5dSMike Smith int
256615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
256715e32d5dSMike Smith {
25683c600cfbSJohn Baldwin     char	*cp, *env, *np;
256915e32d5dSMike Smith     int		len;
257015e32d5dSMike Smith 
257115e32d5dSMike Smith     np = acpi_name(handle);
257215e32d5dSMike Smith     if (*np == '\\')
257315e32d5dSMike Smith 	np++;
25743c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
257515e32d5dSMike Smith 	return (0);
257615e32d5dSMike Smith 
2577be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
25783c600cfbSJohn Baldwin     cp = env;
257915e32d5dSMike Smith     for (;;) {
2580bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
258115e32d5dSMike Smith 	    cp++;
258215e32d5dSMike Smith 	if (*cp == 0)
258315e32d5dSMike Smith 	    break;
258415e32d5dSMike Smith 	len = 0;
2585bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
258615e32d5dSMike Smith 	    len++;
2587d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
25883c600cfbSJohn Baldwin 	    freeenv(env);
25890ae55423SMike Smith 	    return(1);
2590d786139cSMaxime Henrion 	}
25910ae55423SMike Smith 	cp += len;
25920ae55423SMike Smith     }
25933c600cfbSJohn Baldwin     freeenv(env);
2594be2b1797SNate Lawson 
25950ae55423SMike Smith     return (0);
25960ae55423SMike Smith }
25970ae55423SMike Smith 
25980ae55423SMike Smith /*
25990ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
26000ae55423SMike Smith  */
26010ae55423SMike Smith int
26020ae55423SMike Smith acpi_disabled(char *subsys)
26030ae55423SMike Smith {
260478689b15SMaxime Henrion     char	*cp, *env;
26050ae55423SMike Smith     int		len;
26060ae55423SMike Smith 
26073184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
26080ae55423SMike Smith 	return (0);
26093184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
261078689b15SMaxime Henrion 	freeenv(env);
26110ae55423SMike Smith 	return (1);
2612d786139cSMaxime Henrion     }
26130ae55423SMike Smith 
26143184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
261578689b15SMaxime Henrion     cp = env;
26160ae55423SMike Smith     for (;;) {
26173184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
26180ae55423SMike Smith 	    cp++;
26193184cf5aSNate Lawson 	if (*cp == '\0')
26200ae55423SMike Smith 	    break;
26210ae55423SMike Smith 	len = 0;
26223184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
26230ae55423SMike Smith 	    len++;
26243184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
262578689b15SMaxime Henrion 	    freeenv(env);
262615e32d5dSMike Smith 	    return (1);
2627d786139cSMaxime Henrion 	}
262815e32d5dSMike Smith 	cp += len;
262915e32d5dSMike Smith     }
263078689b15SMaxime Henrion     freeenv(env);
2631be2b1797SNate Lawson 
263215e32d5dSMike Smith     return (0);
263315e32d5dSMike Smith }
263415e32d5dSMike Smith 
263515e32d5dSMike Smith /*
263615e32d5dSMike Smith  * Control interface.
263715e32d5dSMike Smith  *
26380ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2639be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2640be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
264115e32d5dSMike Smith  */
26420ae55423SMike Smith struct acpi_ioctl_hook
26430ae55423SMike Smith {
26440ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
26450ae55423SMike Smith     u_long			 cmd;
2646be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
26470ae55423SMike Smith     void			 *arg;
26480ae55423SMike Smith };
26490ae55423SMike Smith 
26500ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
26510ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
26520ae55423SMike Smith 
26530ae55423SMike Smith int
2654be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
26550ae55423SMike Smith {
26560ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
26570ae55423SMike Smith 
26580ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
26590ae55423SMike Smith 	return (ENOMEM);
26600ae55423SMike Smith     hp->cmd = cmd;
26610ae55423SMike Smith     hp->fn = fn;
26620ae55423SMike Smith     hp->arg = arg;
266315e2f34fSNate Lawson 
266415e2f34fSNate Lawson     ACPI_LOCK(acpi);
26650ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
26660ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
26670ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
26680ae55423SMike Smith     }
26690ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
267015e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
267115e2f34fSNate Lawson 
26720ae55423SMike Smith     return (0);
26730ae55423SMike Smith }
26740ae55423SMike Smith 
26750ae55423SMike Smith void
2676be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
26770ae55423SMike Smith {
26780ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
26790ae55423SMike Smith 
268015e2f34fSNate Lawson     ACPI_LOCK(acpi);
26810ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
268215e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
26830ae55423SMike Smith 	    break;
26840ae55423SMike Smith 
26850ae55423SMike Smith     if (hp != NULL) {
26860ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
26870ae55423SMike Smith 	free(hp, M_ACPIDEV);
26880ae55423SMike Smith     }
268915e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
26900ae55423SMike Smith }
26910ae55423SMike Smith 
269215e32d5dSMike Smith static int
269389c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
269415e32d5dSMike Smith {
269515e32d5dSMike Smith     return (0);
269615e32d5dSMike Smith }
269715e32d5dSMike Smith 
269815e32d5dSMike Smith static int
269989c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
270015e32d5dSMike Smith {
270115e32d5dSMike Smith     return (0);
270215e32d5dSMike Smith }
270315e32d5dSMike Smith 
270415e32d5dSMike Smith static int
270589c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
270615e32d5dSMike Smith {
270715e32d5dSMike Smith     struct acpi_softc		*sc;
27080ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
2709bee4aa4aSNate Lawson     int				error, state;
271015e32d5dSMike Smith 
2711a2e35898SDavid E. O'Brien     error = 0;
2712a2e35898SDavid E. O'Brien     hp = NULL;
271315e32d5dSMike Smith     sc = dev->si_drv1;
271415e32d5dSMike Smith 
27150ae55423SMike Smith     /*
27160ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
27170ae55423SMike Smith      */
271815e2f34fSNate Lawson     ACPI_LOCK(acpi);
2719bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
27200ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2721bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
2722bee4aa4aSNate Lawson 		break;
27230ae55423SMike Smith 	}
272415e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
2725bee4aa4aSNate Lawson     if (hp)
2726bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
27270ae55423SMike Smith 
27280ae55423SMike Smith     /*
2729a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2730a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2731a89fcf28STakanori Watanabe      * Not changing system behavior.
2732a89fcf28STakanori Watanabe      */
2733be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
2734be2b1797SNate Lawson 	return (EPERM);
2735a89fcf28STakanori Watanabe 
2736be2b1797SNate Lawson     /* Core system ioctls. */
273715e32d5dSMike Smith     switch (cmd) {
273815e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
2739bee4aa4aSNate Lawson 	error = EINVAL;
274015e32d5dSMike Smith 	state = *(int *)addr;
2741bee4aa4aSNate Lawson 	if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2742bee4aa4aSNate Lawson 	    if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
2743bee4aa4aSNate Lawson 		error = 0;
274415e32d5dSMike Smith 	break;
274515e32d5dSMike Smith     default:
2746bee4aa4aSNate Lawson 	error = ENXIO;
274715e32d5dSMike Smith 	break;
274815e32d5dSMike Smith     }
2749917d44c8SMitsuru IWASAKI 
275015e32d5dSMike Smith     return (error);
275115e32d5dSMike Smith }
275215e32d5dSMike Smith 
27531d073b1dSJohn Baldwin static int
2754d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2755d75de536SMitsuru IWASAKI {
2756d75de536SMitsuru IWASAKI     int error;
2757bee4aa4aSNate Lawson     struct sbuf sb;
2758d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2759d75de536SMitsuru IWASAKI 
2760bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
2761bee4aa4aSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
2762bee4aa4aSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
2763bee4aa4aSNate Lawson 	    sbuf_printf(&sb, "S%d ", state);
2764bee4aa4aSNate Lawson     sbuf_trim(&sb);
2765bee4aa4aSNate Lawson     sbuf_finish(&sb);
2766bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2767bee4aa4aSNate Lawson     sbuf_delete(&sb);
2768d75de536SMitsuru IWASAKI     return (error);
2769d75de536SMitsuru IWASAKI }
2770d75de536SMitsuru IWASAKI 
2771d75de536SMitsuru IWASAKI static int
27721d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
27731d073b1dSJohn Baldwin {
27741d073b1dSJohn Baldwin     char sleep_state[10];
27751d073b1dSJohn Baldwin     int error;
27761d073b1dSJohn Baldwin     u_int new_state, old_state;
27771d073b1dSJohn Baldwin 
27781d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2779bee4aa4aSNate Lawson     if (old_state > ACPI_S_STATES_MAX + 1)
2780bee4aa4aSNate Lawson 	strlcpy(sleep_state, "unknown", sizeof(sleep_state));
2781bee4aa4aSNate Lawson     else
2782bee4aa4aSNate Lawson 	strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
27831d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
27841d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2785be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2786bee4aa4aSNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
2787bee4aa4aSNate Lawson 	    if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
27881d073b1dSJohn Baldwin 		break;
2789931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2790be2b1797SNate Lawson 	    if (new_state != old_state)
27911d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2792bee4aa4aSNate Lawson 	} else
27931d073b1dSJohn Baldwin 	    error = EINVAL;
27941d073b1dSJohn Baldwin     }
2795be2b1797SNate Lawson 
27961d073b1dSJohn Baldwin     return (error);
27971d073b1dSJohn Baldwin }
27981d073b1dSJohn Baldwin 
27999b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
28009b937d48SNate Lawson void
28019b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
28029b937d48SNate Lawson {
28039b937d48SNate Lawson     char		notify_buf[16];
28049b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
28059b937d48SNate Lawson     ACPI_STATUS		status;
28069b937d48SNate Lawson 
28079b937d48SNate Lawson     if (subsystem == NULL)
28089b937d48SNate Lawson 	return;
28099b937d48SNate Lawson 
28109b937d48SNate Lawson     handle_buf.Pointer = NULL;
28119b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
28129b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
28139b937d48SNate Lawson     if (ACPI_FAILURE(status))
28149b937d48SNate Lawson 	return;
28159b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
28169b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
28179b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
28189b937d48SNate Lawson }
28199b937d48SNate Lawson 
282015e32d5dSMike Smith #ifdef ACPI_DEBUG
28210ae55423SMike Smith /*
28220ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
28230ae55423SMike Smith  *
28240ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
28250ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
28260ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
28270ae55423SMike Smith  * prefixing the bit name with !.
28280ae55423SMike Smith  */
282915e32d5dSMike Smith struct debugtag
283015e32d5dSMike Smith {
283115e32d5dSMike Smith     char	*name;
283215e32d5dSMike Smith     UINT32	value;
283315e32d5dSMike Smith };
283415e32d5dSMike Smith 
283515e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2836c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2837c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2838c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2839c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2840c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2841c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2842c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2843c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2844c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2845d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
28464c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2847d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2848297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
28494c1cdee6SMike Smith 
2850c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2851c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
28523184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2853c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
28543184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
28553184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
28563184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
28574c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2858cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
28593184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2860b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
286115e32d5dSMike Smith     {NULL, 0}
286215e32d5dSMike Smith };
286315e32d5dSMike Smith 
286415e32d5dSMike Smith static struct debugtag dbg_level[] = {
28654c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
28669501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
28679501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
28684c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
28699501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
28704c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2871d62ab2f4SMitsuru IWASAKI 
2872d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2873297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
28744c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
28754c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2876d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
28774c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
28784c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
28794c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
28804c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
28814c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
28824c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
28834c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
28844c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
28854c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
28864c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2887d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2888d62ab2f4SMitsuru IWASAKI 
2889d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2890d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2891d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2892d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2893d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
28944c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2895d62ab2f4SMitsuru IWASAKI 
2896d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2897d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2898d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2899d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2900d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2901d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2902d62ab2f4SMitsuru IWASAKI 
2903d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
290498479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
290598479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
290698479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
290798479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
290898479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
290915e32d5dSMike Smith     {NULL, 0}
291015e32d5dSMike Smith };
291115e32d5dSMike Smith 
291215e32d5dSMike Smith static void
291315e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
291415e32d5dSMike Smith {
291515e32d5dSMike Smith     char	*ep;
291615e32d5dSMike Smith     int		i, l;
29170ae55423SMike Smith     int		set;
291815e32d5dSMike Smith 
291915e32d5dSMike Smith     while (*cp) {
292015e32d5dSMike Smith 	if (isspace(*cp)) {
292115e32d5dSMike Smith 	    cp++;
292215e32d5dSMike Smith 	    continue;
292315e32d5dSMike Smith 	}
292415e32d5dSMike Smith 	ep = cp;
292515e32d5dSMike Smith 	while (*ep && !isspace(*ep))
292615e32d5dSMike Smith 	    ep++;
29270ae55423SMike Smith 	if (*cp == '!') {
29280ae55423SMike Smith 	    set = 0;
29290ae55423SMike Smith 	    cp++;
29300ae55423SMike Smith 	    if (cp == ep)
29310ae55423SMike Smith 		continue;
29320ae55423SMike Smith 	} else {
29330ae55423SMike Smith 	    set = 1;
29340ae55423SMike Smith 	}
293515e32d5dSMike Smith 	l = ep - cp;
293615e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
293715e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2938be2b1797SNate Lawson 		if (set)
293915e32d5dSMike Smith 		    *flag |= tag[i].value;
2940be2b1797SNate Lawson 		else
29410ae55423SMike Smith 		    *flag &= ~tag[i].value;
294215e32d5dSMike Smith 	    }
294315e32d5dSMike Smith 	}
294415e32d5dSMike Smith 	cp = ep;
294515e32d5dSMike Smith     }
294615e32d5dSMike Smith }
294715e32d5dSMike Smith 
294815e32d5dSMike Smith static void
29492a4ac806SMike Smith acpi_set_debugging(void *junk)
295015e32d5dSMike Smith {
29517e639165SNate Lawson     char	*layer, *level;
295215e32d5dSMike Smith 
29531d7b121cSNate Lawson     if (cold) {
29540ae55423SMike Smith 	AcpiDbgLayer = 0;
29550ae55423SMike Smith 	AcpiDbgLevel = 0;
29561d7b121cSNate Lawson     }
29571d7b121cSNate Lawson 
29587e639165SNate Lawson     layer = getenv("debug.acpi.layer");
29597e639165SNate Lawson     level = getenv("debug.acpi.level");
29607e639165SNate Lawson     if (layer == NULL && level == NULL)
29617e639165SNate Lawson 	return;
29620ae55423SMike Smith 
29637e639165SNate Lawson     printf("ACPI set debug");
29647e639165SNate Lawson     if (layer != NULL) {
29657e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
29667e639165SNate Lawson 	    printf(" layer '%s'", layer);
29677e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
29687e639165SNate Lawson 	freeenv(layer);
29691d7b121cSNate Lawson     }
29707e639165SNate Lawson     if (level != NULL) {
29717e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
29727e639165SNate Lawson 	    printf(" level '%s'", level);
29737e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
29747e639165SNate Lawson 	freeenv(level);
29757e639165SNate Lawson     }
29767e639165SNate Lawson     printf("\n");
297715e32d5dSMike Smith }
2978bee4aa4aSNate Lawson 
2979be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
2980be2b1797SNate Lawson 	NULL);
29811d7b121cSNate Lawson 
29821d7b121cSNate Lawson static int
29831d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
29841d7b121cSNate Lawson {
298554f6bca0SNate Lawson     int		 error, *dbg;
29861d7b121cSNate Lawson     struct	 debugtag *tag;
298754f6bca0SNate Lawson     struct	 sbuf sb;
29881d7b121cSNate Lawson 
298954f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
299054f6bca0SNate Lawson 	return (ENOMEM);
29911d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
29921d7b121cSNate Lawson 	tag = &dbg_layer[0];
29931d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
29941d7b121cSNate Lawson     } else {
29951d7b121cSNate Lawson 	tag = &dbg_level[0];
29961d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
29971d7b121cSNate Lawson     }
29981d7b121cSNate Lawson 
29991d7b121cSNate Lawson     /* Get old values if this is a get request. */
300015e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
30011d7b121cSNate Lawson     if (*dbg == 0) {
300254f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
30031d7b121cSNate Lawson     } else if (req->newptr == NULL) {
30041d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
300554f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
300654f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
30071d7b121cSNate Lawson 	}
30081d7b121cSNate Lawson     }
300954f6bca0SNate Lawson     sbuf_trim(&sb);
301054f6bca0SNate Lawson     sbuf_finish(&sb);
30111d7b121cSNate Lawson 
30127e639165SNate Lawson     /* Copy out the old values to the user. */
30137e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
301454f6bca0SNate Lawson     sbuf_delete(&sb);
30151d7b121cSNate Lawson 
30161d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
30171d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
30181d7b121cSNate Lawson 	*dbg = 0;
30191d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
30201d7b121cSNate Lawson 	acpi_set_debugging(NULL);
30211d7b121cSNate Lawson     }
302215e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
30231d7b121cSNate Lawson 
30241d7b121cSNate Lawson     return (error);
30251d7b121cSNate Lawson }
3026bee4aa4aSNate Lawson 
30271d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
30281d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
30291d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
30301d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3031bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3032f9390180SMitsuru IWASAKI 
3033f9390180SMitsuru IWASAKI static int
3034f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
3035f9390180SMitsuru IWASAKI {
3036f9390180SMitsuru IWASAKI 	int	state, acpi_state;
3037f9390180SMitsuru IWASAKI 	int	error;
3038f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
3039f9390180SMitsuru IWASAKI 	va_list	ap;
3040f9390180SMitsuru IWASAKI 
3041f9390180SMitsuru IWASAKI 	error = 0;
3042f9390180SMitsuru IWASAKI 	switch (cmd) {
3043f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
3044f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
3045f9390180SMitsuru IWASAKI 		if (sc == NULL) {
3046f9390180SMitsuru IWASAKI 			error = EINVAL;
3047f9390180SMitsuru IWASAKI 			goto out;
3048f9390180SMitsuru IWASAKI 		}
3049f9390180SMitsuru IWASAKI 
3050f9390180SMitsuru IWASAKI 		va_start(ap, arg);
3051f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
3052f9390180SMitsuru IWASAKI 		va_end(ap);
3053f9390180SMitsuru IWASAKI 
3054f9390180SMitsuru IWASAKI 		switch (state) {
3055f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
3056f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
3057f9390180SMitsuru IWASAKI 			break;
3058f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
3059f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
3060f9390180SMitsuru IWASAKI 			break;
3061f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
3062f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
3063f9390180SMitsuru IWASAKI 			break;
3064f9390180SMitsuru IWASAKI 		default:
3065f9390180SMitsuru IWASAKI 			error = EINVAL;
3066f9390180SMitsuru IWASAKI 			goto out;
3067f9390180SMitsuru IWASAKI 		}
3068f9390180SMitsuru IWASAKI 
3069f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
3070f9390180SMitsuru IWASAKI 		break;
3071f9390180SMitsuru IWASAKI 	default:
3072f9390180SMitsuru IWASAKI 		error = EINVAL;
3073f9390180SMitsuru IWASAKI 		goto out;
3074f9390180SMitsuru IWASAKI 	}
3075f9390180SMitsuru IWASAKI 
3076f9390180SMitsuru IWASAKI out:
3077f9390180SMitsuru IWASAKI 	return (error);
3078f9390180SMitsuru IWASAKI }
3079f9390180SMitsuru IWASAKI 
3080f9390180SMitsuru IWASAKI static void
3081f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
3082f9390180SMitsuru IWASAKI {
3083be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
30846c407052SMitsuru IWASAKI 	return;
3085f9390180SMitsuru IWASAKI 
3086f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3087f9390180SMitsuru IWASAKI }
3088f9390180SMitsuru IWASAKI 
3089f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
3090