xref: /freebsd/sys/dev/acpica/acpi.c (revision 87a500cd3b0e8d2f51ebb44fd5392d727788187d)
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 
30dad97feeSDavid E. O'Brien #include <sys/cdefs.h>
31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$");
32dad97feeSDavid E. O'Brien 
3315e32d5dSMike Smith #include "opt_acpi.h"
3415e32d5dSMike Smith #include <sys/param.h>
3515e32d5dSMike Smith #include <sys/kernel.h>
36fb919e4dSMark Murray #include <sys/proc.h>
37a89fcf28STakanori Watanabe #include <sys/fcntl.h>
3815e32d5dSMike Smith #include <sys/malloc.h>
39fe12f24bSPoul-Henning Kamp #include <sys/module.h>
4015e32d5dSMike Smith #include <sys/bus.h>
4115e32d5dSMike Smith #include <sys/conf.h>
4215e32d5dSMike Smith #include <sys/ioccom.h>
4315e32d5dSMike Smith #include <sys/reboot.h>
4415e32d5dSMike Smith #include <sys/sysctl.h>
4515e32d5dSMike Smith #include <sys/ctype.h>
461611ea87SMitsuru IWASAKI #include <sys/linker.h>
47f9390180SMitsuru IWASAKI #include <sys/power.h>
4854f6bca0SNate Lawson #include <sys/sbuf.h>
49eeb3a05fSNate Lawson #include <sys/smp.h>
5015e32d5dSMike Smith 
5115e32d5dSMike Smith #include <machine/clock.h>
5215e32d5dSMike Smith #include <machine/resource.h>
53dc750869SNate Lawson #include <machine/bus.h>
54dc750869SNate Lawson #include <sys/rman.h>
55bc0f2195SMike Smith #include <isa/isavar.h>
56c796e27bSNate Lawson #include <isa/pnpvar.h>
57bc0f2195SMike Smith 
582a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h>
5915e32d5dSMike Smith #include <dev/acpica/acpivar.h>
6015e32d5dSMike Smith #include <dev/acpica/acpiio.h>
6187a500cdSNate Lawson #include <contrib/dev/acpica/achware.h>
629b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
6315e32d5dSMike Smith 
6410ce62b9SNate Lawson #include "pci_if.h"
6510ce62b9SNate Lawson #include <dev/pci/pcivar.h>
6610ce62b9SNate Lawson #include <dev/pci/pci_private.h>
6710ce62b9SNate Lawson 
6815e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
6915e32d5dSMike Smith 
70be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
71cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
72b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
730ae55423SMike Smith 
7415e32d5dSMike Smith static d_open_t		acpiopen;
7515e32d5dSMike Smith static d_close_t	acpiclose;
7615e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7715e32d5dSMike Smith 
7815e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
79dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
807ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
817ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
827ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
837ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
8415e32d5dSMike Smith };
8515e32d5dSMike Smith 
86346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
87cb9b0d80SMike Smith struct mtx	acpi_mutex;
88cb9b0d80SMike Smith 
8989fb5af8SNate Lawson /* Bitmap of device quirks. */
9089fb5af8SNate Lawson int		acpi_quirks;
9189fb5af8SNate Lawson 
926d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
9315e32d5dSMike Smith static void	acpi_identify(driver_t *driver, device_t parent);
9415e32d5dSMike Smith static int	acpi_probe(device_t dev);
9515e32d5dSMike Smith static int	acpi_attach(device_t dev);
9610ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
9710ce62b9SNate Lawson static int	acpi_resume(device_t dev);
98169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
99be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
100be2b1797SNate Lawson 			int unit);
10115e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
10210ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
10310ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
104be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
105be2b1797SNate Lawson 			uintptr_t *result);
106be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
107be2b1797SNate Lawson 			uintptr_t value);
10891233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
109adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
110adad4744SNate Lawson static struct resource_list_entry *acpi_sysres_find(device_t dev, int type,
111adad4744SNate Lawson 		    u_long addr);
112be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
113be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
114be2b1797SNate Lawson 			u_long count, u_int flags);
115be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
116be2b1797SNate Lawson 			int rid, struct resource *r);
1178b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1188b28b622SNate Lawson 		    int rid);
1191e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1201e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
121ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
122ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
123ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
124ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
12510ce62b9SNate Lawson static int	acpi_device_pwr_for_sleep(device_t bus, device_t dev,
12610ce62b9SNate Lawson 		    int *dstate);
127df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
128df8d2a32SNate Lawson 		    void *context, void **retval);
129df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
130df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
13110ce62b9SNate Lawson static int	acpi_set_powerstate_method(device_t bus, device_t child,
13210ce62b9SNate Lawson 		    int state);
133be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
134be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
13515e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
136b82ca9a9SNate Lawson static int	acpi_probe_order(ACPI_HANDLE handle, int *order);
137be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
138be2b1797SNate Lawson 		    void *context, void **status);
139ce619b2eSNate Lawson static BOOLEAN	acpi_MatchHid(ACPI_HANDLE h, const char *hid);
14015e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
14113d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
142d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
143d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
144d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
14588a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
14688a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
14715e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
14815e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
149d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1501d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
151f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
152879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
153879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
154879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
155879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
156879d6c23STakanori Watanabe 
15715e32d5dSMike Smith static device_method_t acpi_methods[] = {
15815e32d5dSMike Smith     /* Device interface */
15915e32d5dSMike Smith     DEVMETHOD(device_identify,		acpi_identify),
16015e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
16115e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
162169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
16356a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
16410ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
16510ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
16615e32d5dSMike Smith 
16715e32d5dSMike Smith     /* Bus interface */
16815e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
16915e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
17010ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
17110ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
17215e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
17315e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
17491233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
17591233413SNate Lawson     DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
17691233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
17715e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
17815e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
1798b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
180879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
181879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
18215e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
18315e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
18415e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
18515e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
18615e32d5dSMike Smith 
187ce619b2eSNate Lawson     /* ACPI bus */
188ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
189ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
19010ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
191df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
192ce619b2eSNate Lawson 
19310ce62b9SNate Lawson     /* PCI emulation */
19410ce62b9SNate Lawson     DEVMETHOD(pci_set_powerstate,	acpi_set_powerstate_method),
19510ce62b9SNate Lawson 
196bc0f2195SMike Smith     /* ISA emulation */
197bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
198bc0f2195SMike Smith 
19915e32d5dSMike Smith     {0, 0}
20015e32d5dSMike Smith };
20115e32d5dSMike Smith 
20215e32d5dSMike Smith static driver_t acpi_driver = {
20315e32d5dSMike Smith     "acpi",
20415e32d5dSMike Smith     acpi_methods,
20515e32d5dSMike Smith     sizeof(struct acpi_softc),
20615e32d5dSMike Smith };
20715e32d5dSMike Smith 
2083273b005SMike Smith static devclass_t acpi_devclass;
2096d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
210a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
21115e32d5dSMike Smith 
21215e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
21315e2f34fSNate Lawson 
214adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
215adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
216adad4744SNate Lawson 
217bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
218bee4aa4aSNate Lawson 
219865b8d0bSNate Lawson static const char* sleep_state_names[] = {
220865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
221865b8d0bSNate Lawson 
2221d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
2231d7b121cSNate Lawson static char acpi_ca_version[12];
2241d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2251d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
22615e32d5dSMike Smith 
22715e32d5dSMike Smith /*
228413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
229c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
230c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
231c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
232c9b8d77dSNate Lawson  * some IBMs use such code.
233413081d7SNate Lawson  */
234c9b8d77dSNate Lawson static int acpi_serialize_methods;
235413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
236413081d7SNate Lawson 
23710ce62b9SNate Lawson /* Power devices off and on in suspend and resume.  XXX Remove once tested. */
23810ce62b9SNate Lawson static int acpi_do_powerstate = 1;
23910ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
24010ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
24110ce62b9SNate Lawson     &acpi_do_powerstate, 1, "Turn off devices when suspending.");
24210ce62b9SNate Lawson 
24339da3eb3SNate Lawson /* Allow users to override quirks. */
24439da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
24539da3eb3SNate Lawson 
246c9b8d77dSNate Lawson /*
2476d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2486d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
249be2b1797SNate Lawson  * It also cannot be unloaded, since the entire system bus heirarchy hangs
250be2b1797SNate Lawson  * off it.
2516d63101aSMike Smith  */
2526d63101aSMike Smith static int
2536d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2546d63101aSMike Smith {
2556d63101aSMike Smith     switch (event) {
2566d63101aSMike Smith     case MOD_LOAD:
25787b45ed5SMitsuru IWASAKI 	if (!cold) {
25887b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2596d63101aSMike Smith 	    return (EPERM);
26087b45ed5SMitsuru IWASAKI 	}
2616d63101aSMike Smith 	break;
2626d63101aSMike Smith     case MOD_UNLOAD:
263f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2646d63101aSMike Smith 	    return (EBUSY);
265f9390180SMitsuru IWASAKI 	break;
2666d63101aSMike Smith     default:
2676d63101aSMike Smith 	break;
2686d63101aSMike Smith     }
2696d63101aSMike Smith     return (0);
2706d63101aSMike Smith }
2716d63101aSMike Smith 
2726d63101aSMike Smith /*
273bbc2815cSJohn Baldwin  * Perform early initialization.
27415e32d5dSMike Smith  */
275bbc2815cSJohn Baldwin ACPI_STATUS
276bbc2815cSJohn Baldwin acpi_Startup(void)
27715e32d5dSMike Smith {
27889fb5af8SNate Lawson     static int started = 0;
27989fb5af8SNate Lawson     int error, val;
28015e32d5dSMike Smith 
2811b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2821b8c233dSPeter Pentchev 
283bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
284bbc2815cSJohn Baldwin     if (started)
28589fb5af8SNate Lawson 	return_VALUE (0);
286bbc2815cSJohn Baldwin     started = 1;
28715e32d5dSMike Smith 
288be2b1797SNate Lawson     /* Initialise the ACPI mutex */
2896008862bSJohn Baldwin     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
290cb9b0d80SMike Smith 
291413081d7SNate Lawson     /*
292413081d7SNate Lawson      * Set the globals from our tunables.  This is needed because ACPI-CA
293f3fc4f8bSNate Lawson      * uses UINT8 for some values and we have no tunable_byte.
294413081d7SNate Lawson      */
295834a79deSNate Lawson     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
296834a79deSNate Lawson     AcpiGbl_EnableInterpreterSlack = TRUE;
297413081d7SNate Lawson 
298be2b1797SNate Lawson     /* Start up the ACPI CA subsystem. */
299b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
300bfae45aaSMike Smith 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
301bbc2815cSJohn Baldwin 	return_VALUE (error);
30215e32d5dSMike Smith     }
3031611ea87SMitsuru IWASAKI 
304b53f2771SMike Smith     if (ACPI_FAILURE(error = AcpiLoadTables())) {
305bfae45aaSMike Smith 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
30689fb5af8SNate Lawson 	AcpiTerminate();
307bbc2815cSJohn Baldwin 	return_VALUE (error);
30815e32d5dSMike Smith     }
3093184cf5aSNate Lawson 
31089fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
31139da3eb3SNate Lawson     if (acpi_quirks == 0)
31289fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
31389fb5af8SNate Lawson 
31439da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
31589fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
31689fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
31789fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
31889fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
31989fb5af8SNate Lawson 	AcpiTerminate();
3204e376d58SNate Lawson 	return_VALUE (AE_ERROR);
32189fb5af8SNate Lawson     }
3223184cf5aSNate Lawson 
323bbc2815cSJohn Baldwin     return_VALUE (AE_OK);
324bbc2815cSJohn Baldwin }
325bbc2815cSJohn Baldwin 
326bbc2815cSJohn Baldwin /*
327bbc2815cSJohn Baldwin  * Detect ACPI, perform early initialisation
328bbc2815cSJohn Baldwin  */
329bbc2815cSJohn Baldwin static void
330bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent)
331bbc2815cSJohn Baldwin {
332bbc2815cSJohn Baldwin     device_t	child;
333bbc2815cSJohn Baldwin 
334bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
335bbc2815cSJohn Baldwin 
336bbc2815cSJohn Baldwin     if (!cold)
337bbc2815cSJohn Baldwin 	return_VOID;
338bbc2815cSJohn Baldwin 
339bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
340bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
341bbc2815cSJohn Baldwin 	return_VOID;
342bbc2815cSJohn Baldwin 
343bbc2815cSJohn Baldwin     /* Make sure we're not being doubly invoked. */
344bbc2815cSJohn Baldwin     if (device_find_child(parent, "acpi", 0) != NULL)
345bbc2815cSJohn Baldwin 	return_VOID;
346bbc2815cSJohn Baldwin 
347bbc2815cSJohn Baldwin     /* Initialize ACPI-CA. */
348bbc2815cSJohn Baldwin     if (ACPI_FAILURE(acpi_Startup()))
349bbc2815cSJohn Baldwin 	return_VOID;
35015e32d5dSMike Smith 
3514e376d58SNate Lawson     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
3524e376d58SNate Lawson 
353be2b1797SNate Lawson     /* Attach the actual ACPI device. */
35415e32d5dSMike Smith     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
355bee4aa4aSNate Lawson 	device_printf(parent, "device_identify failed\n");
3560ae55423SMike Smith 	return_VOID;
35715e32d5dSMike Smith     }
35815e32d5dSMike Smith }
35915e32d5dSMike Smith 
36015e32d5dSMike Smith /*
361bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
36215e32d5dSMike Smith  */
36315e32d5dSMike Smith static int
36415e32d5dSMike Smith acpi_probe(device_t dev)
36515e32d5dSMike Smith {
36615e32d5dSMike Smith     ACPI_TABLE_HEADER	th;
36715e32d5dSMike Smith     char		buf[20];
36815e32d5dSMike Smith     int			error;
3692ccd1cacSNate Lawson     struct sbuf		sb;
3702ccd1cacSNate Lawson     ACPI_STATUS		status;
37115e32d5dSMike Smith 
372b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
373f9390180SMitsuru IWASAKI 
374f9390180SMitsuru IWASAKI     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
375f9390180SMitsuru IWASAKI 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
376bee4aa4aSNate Lawson 	device_printf(dev, "probe failed, other PM system enabled.\n");
377f9390180SMitsuru IWASAKI 	return_VALUE (ENXIO);
378f9390180SMitsuru IWASAKI     }
379f9390180SMitsuru IWASAKI 
380b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
381be2b1797SNate Lawson 	device_printf(dev, "couldn't get XSDT header: %s\n",
382be2b1797SNate Lawson 		      AcpiFormatException(status));
383cb9b0d80SMike Smith 	error = ENXIO;
384cb9b0d80SMike Smith     } else {
3852ccd1cacSNate Lawson 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
3862ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemId, 6);
3872ccd1cacSNate Lawson 	sbuf_trim(&sb);
3882ccd1cacSNate Lawson 	sbuf_putc(&sb, ' ');
3892ccd1cacSNate Lawson 	sbuf_bcat(&sb, th.OemTableId, 8);
3902ccd1cacSNate Lawson 	sbuf_trim(&sb);
3912ccd1cacSNate Lawson 	sbuf_finish(&sb);
3922ccd1cacSNate Lawson 	device_set_desc_copy(dev, sbuf_data(&sb));
3932ccd1cacSNate Lawson 	sbuf_delete(&sb);
394cb9b0d80SMike Smith 	error = 0;
395cb9b0d80SMike Smith     }
396bee4aa4aSNate Lawson 
397cb9b0d80SMike Smith     return_VALUE (error);
39815e32d5dSMike Smith }
39915e32d5dSMike Smith 
40015e32d5dSMike Smith static int
40115e32d5dSMike Smith acpi_attach(device_t dev)
40215e32d5dSMike Smith {
40315e32d5dSMike Smith     struct acpi_softc	*sc;
404cb9b0d80SMike Smith     ACPI_STATUS		status;
405ea27c63eSNate Lawson     int			error, state;
40632d18aa5SMike Smith     UINT32		flags;
407ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
408498d464fSMitsuru IWASAKI     char		*env;
40915e32d5dSMike Smith 
410b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
411bee4aa4aSNate Lawson 
41215e32d5dSMike Smith     sc = device_get_softc(dev);
41315e32d5dSMike Smith     sc->acpi_dev = dev;
41415e32d5dSMike Smith 
41591233413SNate Lawson     /* Initialize resource manager. */
41691233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
41791233413SNate Lawson     acpi_rman_io.rm_start = 0;
41891233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
41991233413SNate Lawson     acpi_rman_io.rm_descr = "I/O ports";
42091233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
42191233413SNate Lawson 	panic("acpi rman_init IO ports failed");
42291233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
42391233413SNate Lawson     acpi_rman_mem.rm_start = 0;
42491233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
42591233413SNate Lawson     acpi_rman_mem.rm_descr = "I/O memory addresses";
42691233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
42791233413SNate Lawson 	panic("acpi rman_init memory failed");
42891233413SNate Lawson 
429be2b1797SNate Lawson     /* Install the default address space handlers. */
430cb9b0d80SMike Smith     error = ENXIO;
431be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
432be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
433be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
434be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
435be2b1797SNate Lawson 		      AcpiFormatException(status));
436cb9b0d80SMike Smith 	goto out;
43715e32d5dSMike Smith     }
438be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
439be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
440be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
441be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
442be2b1797SNate Lawson 		      AcpiFormatException(status));
443cb9b0d80SMike Smith 	goto out;
44415e32d5dSMike Smith     }
445be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
446be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
447be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
448be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
449be2b1797SNate Lawson 		      AcpiFormatException(status));
450cb9b0d80SMike Smith 	goto out;
45115e32d5dSMike Smith     }
45215e32d5dSMike Smith 
45315e32d5dSMike Smith     /*
454be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
455be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
456be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
457be2b1797SNate Lawson      * object init pass.
45815e32d5dSMike Smith      *
45932d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
46032d18aa5SMike Smith      *
461be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
462be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
46315e32d5dSMike Smith      */
46432d18aa5SMike Smith     flags = 0;
465d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
46632d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
467bee4aa4aSNate Lawson 
468bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
469b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
470be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
471be2b1797SNate Lawson 		      AcpiFormatException(status));
472cb9b0d80SMike Smith 	goto out;
47315e32d5dSMike Smith     }
47415e32d5dSMike Smith 
475f8335e3aSNate Lawson     /*
476f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
477f8335e3aSNate Lawson      * the namespace has been evaluated.
478f8335e3aSNate Lawson      */
479f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
480f8335e3aSNate Lawson 
481bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
482b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
483be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
484be2b1797SNate Lawson 		      AcpiFormatException(status));
485b69ed3f4SMitsuru IWASAKI 	goto out;
486b69ed3f4SMitsuru IWASAKI     }
487b69ed3f4SMitsuru IWASAKI 
48815e32d5dSMike Smith     /*
4891d073b1dSJohn Baldwin      * Setup our sysctl tree.
4901d073b1dSJohn Baldwin      *
4911d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
4921d073b1dSJohn Baldwin      */
4931d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
4941d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
4951d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
4961d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
4971d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
498d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
499d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
500d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5011d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5021d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5031d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5041d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5051d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5061d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5071d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
5081d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
509f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
510f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
511f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
512f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
513f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
514f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5152d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
516ff01efb5SMitsuru IWASAKI 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
517ff01efb5SMitsuru IWASAKI 	&sc->acpi_sleep_delay, 0, "sleep delay");
518ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5191611ea87SMitsuru IWASAKI 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
5201611ea87SMitsuru IWASAKI 	&sc->acpi_s4bios, 0, "S4BIOS mode");
5211611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5222d644607SMitsuru IWASAKI 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
5232d644607SMitsuru IWASAKI 	&sc->acpi_verbose, 0, "verbose mode");
524bf0c18ecSNate Lawson 
525bf0c18ecSNate Lawson     /*
5262b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
527bf0c18ecSNate Lawson      * stabilize.
528bf0c18ecSNate Lawson      */
5292b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
5302d644607SMitsuru IWASAKI     if (bootverbose)
5312d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
532965a34fbSNate Lawson     if ((env = getenv("hw.acpi.verbose")) != NULL) {
533965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
534498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
535498d464fSMitsuru IWASAKI 	freeenv(env);
536498d464fSMitsuru IWASAKI     }
5371d073b1dSJohn Baldwin 
5382d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5392d610c46SNate Lawson     if (AcpiGbl_FACS->S4Bios_f != 0)
5402d610c46SNate Lawson 	sc->acpi_s4bios = 1;
5412d610c46SNate Lawson 
5421d073b1dSJohn Baldwin     /*
543ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
544ea27c63eSNate Lawson      * to NONE by default to avoid surprising users.
54515e32d5dSMike Smith      */
546ea27c63eSNate Lawson     sc->acpi_power_button_sx = ACPI_STATE_S5;
547ea27c63eSNate Lawson     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
548f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
549f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
55015e32d5dSMike Smith 
551ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
552ea27c63eSNate Lawson     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
553ea27c63eSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
554ea27c63eSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
555ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
556ea27c63eSNate Lawson 	    break;
557ea27c63eSNate Lawson 	}
558ea27c63eSNate Lawson 
55913d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
56015e32d5dSMike Smith 
56115e32d5dSMike Smith     /*
56215e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
56315e32d5dSMike Smith      */
56415e32d5dSMike Smith 
56559e472e9SNate Lawson     /* Register our shutdown handler. */
566be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
567be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
56815e32d5dSMike Smith 
56915e32d5dSMike Smith     /*
57015e32d5dSMike Smith      * Register our acpi event handlers.
57115e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
57215e32d5dSMike Smith      */
573be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
574be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
575be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
576be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
57715e32d5dSMike Smith 
578be2b1797SNate Lawson     /* Flag our initial states. */
57915e32d5dSMike Smith     sc->acpi_enabled = 1;
58015e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
581ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
58215e32d5dSMike Smith 
583be2b1797SNate Lawson     /* Create the control device */
584a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
5852a4689e8SRobert Watson 			      "acpi");
58615e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
58715e32d5dSMike Smith 
588be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
589f86214b6SMitsuru IWASAKI 	goto out;
590f86214b6SMitsuru IWASAKI 
591f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
592f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
593f9390180SMitsuru IWASAKI 
594eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
595eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
596eeb6dba2SJohn Baldwin 
597cb9b0d80SMike Smith     error = 0;
598cb9b0d80SMike Smith 
599cb9b0d80SMike Smith  out:
600cb9b0d80SMike Smith     return_VALUE (error);
60115e32d5dSMike Smith }
60215e32d5dSMike Smith 
603169b539aSNate Lawson static int
60410ce62b9SNate Lawson acpi_suspend(device_t dev)
60510ce62b9SNate Lawson {
60610ce62b9SNate Lawson     device_t child, *devlist;
60710ce62b9SNate Lawson     int error, i, numdevs, pstate;
60810ce62b9SNate Lawson 
6095d3d03f1SNate Lawson     GIANT_REQUIRED;
6105d3d03f1SNate Lawson 
61110ce62b9SNate Lawson     /* First give child devices a chance to suspend. */
61210ce62b9SNate Lawson     error = bus_generic_suspend(dev);
61310ce62b9SNate Lawson     if (error)
61410ce62b9SNate Lawson 	return (error);
61510ce62b9SNate Lawson 
61610ce62b9SNate Lawson     /*
61710ce62b9SNate Lawson      * Now, set them into the appropriate power state, usually D3.  If the
61810ce62b9SNate Lawson      * device has an _SxD method for the next sleep state, use that power
61910ce62b9SNate Lawson      * state instead.
62010ce62b9SNate Lawson      */
62110ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
62210ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
62310ce62b9SNate Lawson 	/* If the device is not attached, we've powered it down elsewhere. */
62410ce62b9SNate Lawson 	child = devlist[i];
62510ce62b9SNate Lawson 	if (!device_is_attached(child))
62610ce62b9SNate Lawson 	    continue;
62710ce62b9SNate Lawson 
62810ce62b9SNate Lawson 	/*
62910ce62b9SNate Lawson 	 * Default to D3 for all sleep states.  The _SxD method is optional
63010ce62b9SNate Lawson 	 * so set the powerstate even if it's absent.
63110ce62b9SNate Lawson 	 */
63210ce62b9SNate Lawson 	pstate = PCI_POWERSTATE_D3;
63310ce62b9SNate Lawson 	error = acpi_device_pwr_for_sleep(device_get_parent(child),
63410ce62b9SNate Lawson 	    child, &pstate);
63510ce62b9SNate Lawson 	if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
63610ce62b9SNate Lawson 	    pci_set_powerstate(child, pstate);
63710ce62b9SNate Lawson     }
63810ce62b9SNate Lawson     free(devlist, M_TEMP);
63910ce62b9SNate Lawson     error = 0;
64010ce62b9SNate Lawson 
64110ce62b9SNate Lawson     return (error);
64210ce62b9SNate Lawson }
64310ce62b9SNate Lawson 
64410ce62b9SNate Lawson static int
64510ce62b9SNate Lawson acpi_resume(device_t dev)
64610ce62b9SNate Lawson {
64710ce62b9SNate Lawson     ACPI_HANDLE handle;
64810ce62b9SNate Lawson     int i, numdevs;
64910ce62b9SNate Lawson     device_t child, *devlist;
65010ce62b9SNate Lawson 
6515d3d03f1SNate Lawson     GIANT_REQUIRED;
6525d3d03f1SNate Lawson 
65310ce62b9SNate Lawson     /*
65410ce62b9SNate Lawson      * Put all devices in D0 before resuming them.  Call _S0D on each one
65510ce62b9SNate Lawson      * since some systems expect this.
65610ce62b9SNate Lawson      */
65710ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
65810ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
65910ce62b9SNate Lawson 	child = devlist[i];
66010ce62b9SNate Lawson 	handle = acpi_get_handle(child);
66110ce62b9SNate Lawson 	if (handle)
66210ce62b9SNate Lawson 	    AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
66310ce62b9SNate Lawson 	if (device_is_attached(child) && acpi_do_powerstate)
66410ce62b9SNate Lawson 	    pci_set_powerstate(child, PCI_POWERSTATE_D0);
66510ce62b9SNate Lawson     }
66610ce62b9SNate Lawson     free(devlist, M_TEMP);
66710ce62b9SNate Lawson 
66810ce62b9SNate Lawson     return (bus_generic_resume(dev));
66910ce62b9SNate Lawson }
67010ce62b9SNate Lawson 
67110ce62b9SNate Lawson static int
672169b539aSNate Lawson acpi_shutdown(device_t dev)
673169b539aSNate Lawson {
674169b539aSNate Lawson 
6755d3d03f1SNate Lawson     GIANT_REQUIRED;
6765d3d03f1SNate Lawson 
6770e414868SNate Lawson     /* Allow children to shutdown first. */
6780e414868SNate Lawson     bus_generic_shutdown(dev);
6790e414868SNate Lawson 
680bee4aa4aSNate Lawson     /*
681bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
682bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
683bee4aa4aSNate Lawson      */
684d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
685d4b9ff91SNate Lawson 
686169b539aSNate Lawson     return (0);
687169b539aSNate Lawson }
688169b539aSNate Lawson 
68915e32d5dSMike Smith /*
69015e32d5dSMike Smith  * Handle a new device being added
69115e32d5dSMike Smith  */
69215e32d5dSMike Smith static device_t
69315e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
69415e32d5dSMike Smith {
69515e32d5dSMike Smith     struct acpi_device	*ad;
69615e32d5dSMike Smith     device_t		child;
69715e32d5dSMike Smith 
698be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
69915e32d5dSMike Smith 	return (NULL);
70015e32d5dSMike Smith 
70115e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
70215e32d5dSMike Smith 
70315e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
70415e32d5dSMike Smith     if (child != NULL)
70515e32d5dSMike Smith 	device_set_ivars(child, ad);
70643ce1c77SNate Lawson     else
70743ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
70815e32d5dSMike Smith     return (child);
70915e32d5dSMike Smith }
71015e32d5dSMike Smith 
71115e32d5dSMike Smith static int
71215e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
71315e32d5dSMike Smith {
71415e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
71515e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
71615e32d5dSMike Smith     int retval = 0;
71715e32d5dSMike Smith 
71815e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
7199ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
7209ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
7219ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
7229ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
723a91c5fa8SNate Lawson     if (device_get_flags(child))
724a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
7252c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
72615e32d5dSMike Smith 
72715e32d5dSMike Smith     return (retval);
72815e32d5dSMike Smith }
72915e32d5dSMike Smith 
73010ce62b9SNate Lawson /*
73110ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
73210ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
73310ce62b9SNate Lawson  *
73410ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
73510ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
73610ce62b9SNate Lawson  * them to be powered up.
73710ce62b9SNate Lawson  */
73810ce62b9SNate Lawson static void
73910ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
74010ce62b9SNate Lawson {
74110ce62b9SNate Lawson 
74210ce62b9SNate Lawson     /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
74310ce62b9SNate Lawson }
74410ce62b9SNate Lawson 
74510ce62b9SNate Lawson /*
74610ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
74710ce62b9SNate Lawson  *
74810ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
74910ce62b9SNate Lawson  */
75010ce62b9SNate Lawson static void
75110ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
75210ce62b9SNate Lawson {
75310ce62b9SNate Lawson     device_t child, *devlist;
75410ce62b9SNate Lawson     int i, numdevs;
75510ce62b9SNate Lawson 
75610ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
75710ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
75810ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
75910ce62b9SNate Lawson 	child = devlist[i];
76010ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
76110ce62b9SNate Lawson 	    /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
76210ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
76310ce62b9SNate Lawson 		; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
76410ce62b9SNate Lawson 	}
76510ce62b9SNate Lawson     }
76610ce62b9SNate Lawson     free(devlist, M_TEMP);
76710ce62b9SNate Lawson }
76810ce62b9SNate Lawson 
76963600cc3SNate Lawson /* Location hint for devctl(8) */
77063600cc3SNate Lawson static int
771247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
772247648afSTakanori Watanabe     size_t buflen)
773247648afSTakanori Watanabe {
774247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
775247648afSTakanori Watanabe 
776247648afSTakanori Watanabe     if (dinfo->ad_handle)
777d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
778247648afSTakanori Watanabe     else
779d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
780247648afSTakanori Watanabe     return (0);
781247648afSTakanori Watanabe }
782247648afSTakanori Watanabe 
78363600cc3SNate Lawson /* PnP information for devctl(8) */
78463600cc3SNate Lawson static int
785247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
786247648afSTakanori Watanabe     size_t buflen)
787247648afSTakanori Watanabe {
788247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
78963600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
79063600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
791247648afSTakanori Watanabe     char *end;
792247648afSTakanori Watanabe     int error;
793247648afSTakanori Watanabe 
794247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
795247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
796247648afSTakanori Watanabe     if (error)
797d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
798247648afSTakanori Watanabe     else
799247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
800247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
801d40c0f53SNate Lawson 		 adinfo->HardwareId.Value : "none",
80263600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
80363600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
804247648afSTakanori Watanabe     if (adinfo)
805247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
806247648afSTakanori Watanabe 
807247648afSTakanori Watanabe     return (0);
808247648afSTakanori Watanabe }
809247648afSTakanori Watanabe 
810247648afSTakanori Watanabe /*
81115e32d5dSMike Smith  * Handle per-device ivars
81215e32d5dSMike Smith  */
81315e32d5dSMike Smith static int
81415e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
81515e32d5dSMike Smith {
81615e32d5dSMike Smith     struct acpi_device	*ad;
81715e32d5dSMike Smith 
81815e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
81915e32d5dSMike Smith 	printf("device has no ivars\n");
82015e32d5dSMike Smith 	return (ENOENT);
82115e32d5dSMike Smith     }
82215e32d5dSMike Smith 
823be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
82415e32d5dSMike Smith     switch(index) {
82515e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
82615e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
82715e32d5dSMike Smith 	break;
82815e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
82915e32d5dSMike Smith 	*(int *)result = ad->ad_magic;
83015e32d5dSMike Smith 	break;
83115e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
83215e32d5dSMike Smith 	*(void **)result = ad->ad_private;
83315e32d5dSMike Smith 	break;
834d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
835d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
836d4b9ff91SNate Lawson 	break;
83732d18aa5SMike Smith     case ISA_IVAR_VENDORID:
83832d18aa5SMike Smith     case ISA_IVAR_SERIAL:
83932d18aa5SMike Smith     case ISA_IVAR_COMPATID:
84032d18aa5SMike Smith 	*(int *)result = -1;
84132d18aa5SMike Smith 	break;
84232d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
84332d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
84432d18aa5SMike Smith 	break;
84515e32d5dSMike Smith     default:
84615e32d5dSMike Smith 	return (ENOENT);
84715e32d5dSMike Smith     }
848be2b1797SNate Lawson 
84915e32d5dSMike Smith     return (0);
85015e32d5dSMike Smith }
85115e32d5dSMike Smith 
85215e32d5dSMike Smith static int
85315e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
85415e32d5dSMike Smith {
85515e32d5dSMike Smith     struct acpi_device	*ad;
85615e32d5dSMike Smith 
85715e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
85815e32d5dSMike Smith 	printf("device has no ivars\n");
85915e32d5dSMike Smith 	return (ENOENT);
86015e32d5dSMike Smith     }
86115e32d5dSMike Smith 
86215e32d5dSMike Smith     switch(index) {
86315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
86415e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
86515e32d5dSMike Smith 	break;
86615e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
86715e32d5dSMike Smith 	ad->ad_magic = (int)value;
86815e32d5dSMike Smith 	break;
86915e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
87015e32d5dSMike Smith 	ad->ad_private = (void *)value;
87115e32d5dSMike Smith 	break;
872d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
873d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
874d4b9ff91SNate Lawson 	break;
87515e32d5dSMike Smith     default:
8762ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
87715e32d5dSMike Smith 	return (ENOENT);
87815e32d5dSMike Smith     }
879be2b1797SNate Lawson 
88015e32d5dSMike Smith     return (0);
88115e32d5dSMike Smith }
88215e32d5dSMike Smith 
88315e32d5dSMike Smith /*
88415e32d5dSMike Smith  * Handle child resource allocation/removal
88515e32d5dSMike Smith  */
88691233413SNate Lawson static struct resource_list *
88791233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
88815e32d5dSMike Smith {
88991233413SNate Lawson     struct acpi_device		*ad;
89015e32d5dSMike Smith 
89191233413SNate Lawson     ad = device_get_ivars(child);
89291233413SNate Lawson     return (&ad->ad_rl);
89315e32d5dSMike Smith }
89415e32d5dSMike Smith 
895adad4744SNate Lawson /*
896adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
897adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
898adad4744SNate Lawson  */
899adad4744SNate Lawson static int
900adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
901adad4744SNate Lawson {
902adad4744SNate Lawson     struct resource *res;
903adad4744SNate Lawson     struct resource_list *rl;
904adad4744SNate Lawson     struct resource_list_entry *rle;
905adad4744SNate Lawson     struct rman *rm;
906adad4744SNate Lawson 
907adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
908be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
909adad4744SNate Lawson 	if (rle->res != NULL) {
910adad4744SNate Lawson 	    device_printf(dev, "duplicate resource for %lx\n", rle->start);
911adad4744SNate Lawson 	    continue;
912adad4744SNate Lawson 	}
913adad4744SNate Lawson 
914adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
915adad4744SNate Lawson 	switch (rle->type) {
916adad4744SNate Lawson 	case SYS_RES_IOPORT:
917adad4744SNate Lawson 	    rm = &acpi_rman_io;
918adad4744SNate Lawson 	    break;
919adad4744SNate Lawson 	case SYS_RES_MEMORY:
920adad4744SNate Lawson 	    rm = &acpi_rman_mem;
921adad4744SNate Lawson 	    break;
922adad4744SNate Lawson 	default:
923adad4744SNate Lawson 	    continue;
924adad4744SNate Lawson 	}
925adad4744SNate Lawson 
926adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
927adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
928adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
929adad4744SNate Lawson 	if (res != NULL) {
930adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
931adad4744SNate Lawson 	    rle->res = res;
932adad4744SNate Lawson 	} else
933adad4744SNate Lawson 	    device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
934adad4744SNate Lawson 		rle->start, rle->count, rle->type);
935adad4744SNate Lawson     }
936adad4744SNate Lawson     return (0);
937adad4744SNate Lawson }
938adad4744SNate Lawson 
939adad4744SNate Lawson /* Find if we manage a given resource. */
940adad4744SNate Lawson static struct resource_list_entry *
941adad4744SNate Lawson acpi_sysres_find(device_t dev, int type, u_long addr)
942adad4744SNate Lawson {
943adad4744SNate Lawson     struct resource_list *rl;
944adad4744SNate Lawson     struct resource_list_entry *rle;
945adad4744SNate Lawson 
946adad4744SNate Lawson     ACPI_SERIAL_ASSERT(acpi);
947adad4744SNate Lawson 
948adad4744SNate Lawson     /* We only consider IO and memory resources for our pool. */
949adad4744SNate Lawson     rle = NULL;
950adad4744SNate Lawson     if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY)
951adad4744SNate Lawson 	goto out;
952adad4744SNate Lawson 
953adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
954be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
955adad4744SNate Lawson 	if (type == rle->type && addr >= rle->start &&
956adad4744SNate Lawson 	    addr < rle->start + rle->count)
957adad4744SNate Lawson 	    break;
958adad4744SNate Lawson     }
959adad4744SNate Lawson 
960adad4744SNate Lawson out:
961adad4744SNate Lawson     return (rle);
962adad4744SNate Lawson }
963adad4744SNate Lawson 
96415e32d5dSMike Smith static struct resource *
96515e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
96615e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
96715e32d5dSMike Smith {
96895957f62SJohn Baldwin     ACPI_RESOURCE ares;
96915e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
97015e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
97191233413SNate Lawson     struct resource_list_entry *rle;
97291233413SNate Lawson     struct resource *res;
97391233413SNate Lawson     struct rman *rm;
97415e32d5dSMike Smith 
97515e2f34fSNate Lawson     res = NULL;
97615e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
97715e2f34fSNate Lawson 
97891233413SNate Lawson     /*
97991233413SNate Lawson      * If this is an allocation of the "default" range for a given RID, and
98091233413SNate Lawson      * we know what the resources for this device are (i.e., they're on the
98191233413SNate Lawson      * child's resource list), use those start/end values.
98291233413SNate Lawson      */
983f09aa88cSWarner Losh     if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) {
98491233413SNate Lawson 	rle = resource_list_find(rl, type, *rid);
98591233413SNate Lawson 	if (rle == NULL)
98615e2f34fSNate Lawson 	    goto out;
98791233413SNate Lawson 	start = rle->start;
98891233413SNate Lawson 	end = rle->end;
98991233413SNate Lawson 	count = rle->count;
99091233413SNate Lawson     }
99191233413SNate Lawson 
99291233413SNate Lawson     /* If we don't manage this address, pass the request up to the parent. */
993adad4744SNate Lawson     rle = acpi_sysres_find(bus, type, start);
99491233413SNate Lawson     if (rle == NULL) {
99595957f62SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
99695957f62SJohn Baldwin 	    start, end, count, flags);
99795957f62SJohn Baldwin     } else {
99891233413SNate Lawson 
99991233413SNate Lawson 	/* We only handle memory and IO resources through rman. */
100091233413SNate Lawson 	switch (type) {
100191233413SNate Lawson 	case SYS_RES_IOPORT:
100291233413SNate Lawson 	    rm = &acpi_rman_io;
100391233413SNate Lawson 	    break;
100491233413SNate Lawson 	case SYS_RES_MEMORY:
100591233413SNate Lawson 	    rm = &acpi_rman_mem;
100691233413SNate Lawson 	    break;
100791233413SNate Lawson 	default:
100891233413SNate Lawson 	    panic("acpi_alloc_resource: invalid res type %d", type);
100991233413SNate Lawson 	}
101091233413SNate Lawson 
101191233413SNate Lawson 	/* If we do know it, allocate it from the local pool. */
101295957f62SJohn Baldwin 	res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
101395957f62SJohn Baldwin 	    child);
101491233413SNate Lawson 	if (res == NULL)
101515e2f34fSNate Lawson 	    goto out;
101691233413SNate Lawson 
1017a3236570SWarner Losh 	/* Copy the bus tag and handle from the pre-allocated resource. */
101891233413SNate Lawson 	rman_set_bustag(res, rman_get_bustag(rle->res));
1019ed67d9deSWarner Losh 	rman_set_bushandle(res, rman_get_start(res));
102091233413SNate Lawson 
102191233413SNate Lawson 	/* If requested, activate the resource using the parent's method. */
102295957f62SJohn Baldwin 	if (flags & RF_ACTIVE)
102391233413SNate Lawson 	    if (bus_activate_resource(child, type, *rid, res) != 0) {
102491233413SNate Lawson 		rman_release_resource(res);
102515e2f34fSNate Lawson 		res = NULL;
102615e2f34fSNate Lawson 		goto out;
102791233413SNate Lawson 	    }
102895957f62SJohn Baldwin     }
102991233413SNate Lawson 
103095957f62SJohn Baldwin     if (res != NULL && device_get_parent(child) == bus)
103195957f62SJohn Baldwin 	switch (type) {
103295957f62SJohn Baldwin 	case SYS_RES_IRQ:
103395957f62SJohn Baldwin 	    /*
103495957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
103595957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
103695957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
103795957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
103895957f62SJohn Baldwin 	     *
103995957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
104095957f62SJohn Baldwin 	     */
104195957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
104295957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
104395957f62SJohn Baldwin 	    break;
104495957f62SJohn Baldwin 	}
104515e2f34fSNate Lawson 
104615e2f34fSNate Lawson out:
104715e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
104891233413SNate Lawson     return (res);
104915e32d5dSMike Smith }
105015e32d5dSMike Smith 
105115e32d5dSMike Smith static int
105291233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid,
105391233413SNate Lawson     struct resource *r)
105415e32d5dSMike Smith {
105591233413SNate Lawson     int ret;
105615e32d5dSMike Smith 
105715e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
105815e2f34fSNate Lawson 
105991233413SNate Lawson     /*
106091233413SNate Lawson      * If we know about this address, deactivate it and release it to the
106191233413SNate Lawson      * local pool.  If we don't, pass this request up to the parent.
106291233413SNate Lawson      */
1063adad4744SNate Lawson     if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) {
106491233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
106591233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
106691233413SNate Lawson 	    if (ret != 0)
106715e2f34fSNate Lawson 		goto out;
106891233413SNate Lawson 	}
106991233413SNate Lawson 	ret = rman_release_resource(r);
107091233413SNate Lawson     } else
107191233413SNate Lawson 	ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
107291233413SNate Lawson 
107315e2f34fSNate Lawson out:
107415e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
107591233413SNate Lawson     return (ret);
107615e32d5dSMike Smith }
107715e32d5dSMike Smith 
10788b28b622SNate Lawson static void
10798b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
10808b28b622SNate Lawson {
10818b28b622SNate Lawson     struct resource_list *rl;
10828b28b622SNate Lawson 
10838b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
10848b28b622SNate Lawson     resource_list_delete(rl, type, rid);
10858b28b622SNate Lawson }
10868b28b622SNate Lawson 
1087dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1088e1c4bf3fSNate Lawson int
1089e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1090e1c4bf3fSNate Lawson     struct resource **res)
1091dc750869SNate Lawson {
1092e1c4bf3fSNate Lawson     int error, res_type;
1093dc750869SNate Lawson 
1094e1c4bf3fSNate Lawson     error = ENOMEM;
10958f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1096e1c4bf3fSNate Lawson 	return (EINVAL);
1097dc750869SNate Lawson 
10988f118e25SNate Lawson     /* We only support memory and IO spaces. */
1099dc750869SNate Lawson     switch (gas->AddressSpaceId) {
1100dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1101e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1102dc750869SNate Lawson 	break;
1103dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1104e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1105dc750869SNate Lawson 	break;
1106dc750869SNate Lawson     default:
1107e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1108dc750869SNate Lawson     }
1109dc750869SNate Lawson 
11102fe912dfSNate Lawson     /*
1111f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1112f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
11132fe912dfSNate Lawson      */
1114f45fc848SNate Lawson     if (gas->RegisterBitWidth && gas->RegisterBitWidth < 8)
1115f45fc848SNate Lawson 	gas->RegisterBitWidth = 8;
11162fe912dfSNate Lawson 
11178f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
1118f45fc848SNate Lawson     if (!ACPI_VALID_ADDRESS(gas->Address) || gas->RegisterBitWidth == 0)
11198f118e25SNate Lawson 	return (EINVAL);
11208f118e25SNate Lawson 
1121e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
1122e1c4bf3fSNate Lawson 	gas->RegisterBitWidth / 8);
1123e1c4bf3fSNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE);
1124e1c4bf3fSNate Lawson     if (*res != NULL) {
1125e1c4bf3fSNate Lawson 	*type = res_type;
1126e1c4bf3fSNate Lawson 	error = 0;
1127ca2c69c8SNate Lawson     } else
1128ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1129ca2c69c8SNate Lawson 
1130e1c4bf3fSNate Lawson     return (error);
1131dc750869SNate Lawson }
1132dc750869SNate Lawson 
1133c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
11341e4925e8SNate Lawson static uint32_t
113532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1136bc0f2195SMike Smith {
11371e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11381e4925e8SNate Lawson     ACPI_BUFFER		buf;
1139bc0f2195SMike Smith     ACPI_HANDLE		h;
1140bc0f2195SMike Smith     ACPI_STATUS		error;
1141bc0f2195SMike Smith     u_int32_t		pnpid;
114232d18aa5SMike Smith 
1143b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
114432d18aa5SMike Smith 
114532d18aa5SMike Smith     pnpid = 0;
1146bd189fe7SNate Lawson     buf.Pointer = NULL;
1147bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1148bd189fe7SNate Lawson 
11496fca9360SNate Lawson     /* Fetch and validate the HID. */
115032d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1151bd189fe7SNate Lawson 	goto out;
11526fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11536fca9360SNate Lawson     if (ACPI_FAILURE(error))
1154bd189fe7SNate Lawson 	goto out;
11551e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
115632d18aa5SMike Smith 
11571e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
11581e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1159be2b1797SNate Lawson 
1160bd189fe7SNate Lawson out:
1161bd189fe7SNate Lawson     if (buf.Pointer != NULL)
11621e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
116332d18aa5SMike Smith     return_VALUE (pnpid);
116432d18aa5SMike Smith }
116532d18aa5SMike Smith 
11661e4925e8SNate Lawson static int
11671e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1168b2566207STakanori Watanabe {
11691e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11701e4925e8SNate Lawson     ACPI_BUFFER		buf;
1171b2566207STakanori Watanabe     ACPI_HANDLE		h;
1172b2566207STakanori Watanabe     ACPI_STATUS		error;
11731e4925e8SNate Lawson     uint32_t		*pnpid;
11741e4925e8SNate Lawson     int			valid, i;
1175b2566207STakanori Watanabe 
1176b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1177b2566207STakanori Watanabe 
11781e4925e8SNate Lawson     pnpid = cids;
11791e4925e8SNate Lawson     valid = 0;
1180bd189fe7SNate Lawson     buf.Pointer = NULL;
1181bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1182bd189fe7SNate Lawson 
11831e4925e8SNate Lawson     /* Fetch and validate the CID */
1184b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
1185bd189fe7SNate Lawson 	goto out;
11861e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11871e4925e8SNate Lawson     if (ACPI_FAILURE(error))
1188bd189fe7SNate Lawson 	goto out;
11891e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
11901e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1191b2566207STakanori Watanabe 	goto out;
1192b2566207STakanori Watanabe 
11931e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
11941e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
11951e4925e8SNate Lawson     for (i = 0; i < count; i++) {
11961e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
11971e4925e8SNate Lawson 	    continue;
11981e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
11991e4925e8SNate Lawson 	valid++;
1200b2566207STakanori Watanabe     }
1201b2566207STakanori Watanabe 
12021e4925e8SNate Lawson out:
1203bd189fe7SNate Lawson     if (buf.Pointer != NULL)
12041e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
12051e4925e8SNate Lawson     return_VALUE (valid);
12061e4925e8SNate Lawson }
1207b2566207STakanori Watanabe 
1208ce619b2eSNate Lawson static char *
1209ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1210ce619b2eSNate Lawson {
1211ce619b2eSNate Lawson     ACPI_HANDLE h;
1212ce619b2eSNate Lawson     int i;
1213ce619b2eSNate Lawson 
1214ce619b2eSNate Lawson     h = acpi_get_handle(dev);
1215ce619b2eSNate Lawson     if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
1216ce619b2eSNate Lawson 	return (NULL);
1217ce619b2eSNate Lawson 
1218ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1219ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1220ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1221ce619b2eSNate Lawson 	    return (ids[i]);
1222ce619b2eSNate Lawson     }
1223ce619b2eSNate Lawson     return (NULL);
1224ce619b2eSNate Lawson }
1225ce619b2eSNate Lawson 
1226ce619b2eSNate Lawson static ACPI_STATUS
1227ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1228ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1229ce619b2eSNate Lawson {
1230ce619b2eSNate Lawson     ACPI_HANDLE h;
1231ce619b2eSNate Lawson 
1232df8d2a32SNate Lawson     if (dev == NULL)
1233df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1234df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1235ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1236ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1237ce619b2eSNate Lawson }
1238ce619b2eSNate Lawson 
123910ce62b9SNate Lawson static int
124010ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
124110ce62b9SNate Lawson {
124210ce62b9SNate Lawson     struct acpi_softc *sc;
124310ce62b9SNate Lawson     ACPI_HANDLE handle;
124410ce62b9SNate Lawson     ACPI_STATUS status;
124510ce62b9SNate Lawson     char sxd[8];
124610ce62b9SNate Lawson     int error;
124710ce62b9SNate Lawson 
124810ce62b9SNate Lawson     sc = device_get_softc(bus);
124910ce62b9SNate Lawson     handle = acpi_get_handle(dev);
125010ce62b9SNate Lawson 
125110ce62b9SNate Lawson     /*
125210ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
125310ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
125410ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
125510ce62b9SNate Lawson      * need special handling in their drivers.
125610ce62b9SNate Lawson      */
125710ce62b9SNate Lawson     if (handle == NULL ||
125810ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
125910ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
126010ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
126110ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
126210ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
126310ce62b9SNate Lawson 	return (ENXIO);
126410ce62b9SNate Lawson 
126510ce62b9SNate Lawson     /*
126610ce62b9SNate Lawson      * Override next state with the value from _SxD, if present.  If no
126710ce62b9SNate Lawson      * dstate argument was provided, don't fetch the return value.
126810ce62b9SNate Lawson      */
126910ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
127010ce62b9SNate Lawson     if (dstate)
127110ce62b9SNate Lawson 	status = acpi_GetInteger(handle, sxd, dstate);
127210ce62b9SNate Lawson     else
127310ce62b9SNate Lawson 	status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
127410ce62b9SNate Lawson 
127510ce62b9SNate Lawson     switch (status) {
127610ce62b9SNate Lawson     case AE_OK:
127710ce62b9SNate Lawson 	error = 0;
127810ce62b9SNate Lawson 	break;
127910ce62b9SNate Lawson     case AE_NOT_FOUND:
128010ce62b9SNate Lawson 	error = ESRCH;
128110ce62b9SNate Lawson 	break;
128210ce62b9SNate Lawson     default:
128310ce62b9SNate Lawson 	error = ENXIO;
128410ce62b9SNate Lawson 	break;
128510ce62b9SNate Lawson     }
128610ce62b9SNate Lawson 
128710ce62b9SNate Lawson     return (error);
128810ce62b9SNate Lawson }
128910ce62b9SNate Lawson 
1290df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1291df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1292df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1293df8d2a32SNate Lawson     void		*arg;
1294df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1295df8d2a32SNate Lawson };
1296df8d2a32SNate Lawson 
1297ce619b2eSNate Lawson static ACPI_STATUS
1298df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1299df8d2a32SNate Lawson {
1300df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1301df8d2a32SNate Lawson     device_t dev, old_dev;
1302df8d2a32SNate Lawson     ACPI_STATUS status;
1303df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1304df8d2a32SNate Lawson 
1305df8d2a32SNate Lawson     /*
1306df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1307df8d2a32SNate Lawson      * the parent where the scan began.
1308df8d2a32SNate Lawson      */
1309df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1310df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1311df8d2a32SNate Lawson 	return (AE_OK);
1312df8d2a32SNate Lawson 
1313df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1314df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1315df8d2a32SNate Lawson 	return (AE_OK);
1316df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1317df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1318df8d2a32SNate Lawson 	return (AE_OK);
1319df8d2a32SNate Lawson 
1320df8d2a32SNate Lawson     /*
1321df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1322df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1323df8d2a32SNate Lawson      */
1324df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1325df8d2a32SNate Lawson     dev = old_dev;
1326df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1327df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1328df8d2a32SNate Lawson 	return (status);
1329df8d2a32SNate Lawson 
1330df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1331df8d2a32SNate Lawson     if (old_dev != NULL) {
1332df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1333df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1334df8d2a32SNate Lawson     }
1335df8d2a32SNate Lawson 
1336df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1337df8d2a32SNate Lawson     if (dev != NULL)
1338df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1339df8d2a32SNate Lawson 
1340df8d2a32SNate Lawson     return (AE_OK);
1341df8d2a32SNate Lawson }
1342df8d2a32SNate Lawson 
1343df8d2a32SNate Lawson static ACPI_STATUS
1344df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1345df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1346ce619b2eSNate Lawson {
1347ce619b2eSNate Lawson     ACPI_HANDLE h;
1348df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1349ce619b2eSNate Lawson 
1350df8d2a32SNate Lawson     if (acpi_disabled("children"))
1351df8d2a32SNate Lawson 	return (AE_OK);
1352df8d2a32SNate Lawson 
1353df8d2a32SNate Lawson     if (dev == NULL)
1354df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1355df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1356ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1357df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1358df8d2a32SNate Lawson     ctx.arg = arg;
1359df8d2a32SNate Lawson     ctx.parent = h;
1360df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1361df8d2a32SNate Lawson 	acpi_device_scan_cb, &ctx, NULL));
1362ce619b2eSNate Lawson }
1363ce619b2eSNate Lawson 
136410ce62b9SNate Lawson /*
136510ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
136610ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
136710ce62b9SNate Lawson  */
136810ce62b9SNate Lawson static int
136910ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state)
137010ce62b9SNate Lawson {
137110ce62b9SNate Lawson     ACPI_HANDLE h;
137210ce62b9SNate Lawson     ACPI_STATUS status;
137310ce62b9SNate Lawson     int error;
137410ce62b9SNate Lawson 
137510ce62b9SNate Lawson     error = 0;
137610ce62b9SNate Lawson     h = acpi_get_handle(child);
137710ce62b9SNate Lawson     if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
137810ce62b9SNate Lawson 	return (EINVAL);
137910ce62b9SNate Lawson     if (h == NULL)
138010ce62b9SNate Lawson 	return (0);
138110ce62b9SNate Lawson 
138210ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
138310ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
138410ce62b9SNate Lawson     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
138510ce62b9SNate Lawson 	&& status != AE_BAD_PARAMETER)
138610ce62b9SNate Lawson 	device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
138710ce62b9SNate Lawson 	    state, acpi_name(h), AcpiFormatException(status));
138810ce62b9SNate Lawson 
138910ce62b9SNate Lawson     return (error);
139010ce62b9SNate Lawson }
139110ce62b9SNate Lawson 
139232d18aa5SMike Smith static int
139332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
139432d18aa5SMike Smith {
13951e4925e8SNate Lawson     int			result, cid_count, i;
13961e4925e8SNate Lawson     uint32_t		lid, cids[8];
1397bc0f2195SMike Smith 
1398b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1399bc0f2195SMike Smith 
1400bc0f2195SMike Smith     /*
1401bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1402bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1403bc0f2195SMike Smith      * that to happen, so don't ever return it.
1404bc0f2195SMike Smith      */
1405bc0f2195SMike Smith     result = ENXIO;
1406bc0f2195SMike Smith 
1407be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1408b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
14091e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1410bc0f2195SMike Smith     while (ids && ids->ip_id) {
14111e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1412bc0f2195SMike Smith 	    result = 0;
1413bc0f2195SMike Smith 	    goto out;
1414bc0f2195SMike Smith 	}
14151e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
14161e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
14171e4925e8SNate Lawson 		result = 0;
14181e4925e8SNate Lawson 		goto out;
14191e4925e8SNate Lawson 	    }
14201e4925e8SNate Lawson 	}
1421bc0f2195SMike Smith 	ids++;
1422bc0f2195SMike Smith     }
1423be2b1797SNate Lawson 
1424bc0f2195SMike Smith  out:
14259e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
14269e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
14279e0dd54fSNate Lawson 
1428bc0f2195SMike Smith     return_VALUE (result);
1429bc0f2195SMike Smith }
1430bc0f2195SMike Smith 
1431bc0f2195SMike Smith /*
143233332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
143315e32d5dSMike Smith  *
143433332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
143533332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
143633332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
143733332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
143833332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
143915e32d5dSMike Smith  */
144015e32d5dSMike Smith static void
144115e32d5dSMike Smith acpi_probe_children(device_t bus)
144215e32d5dSMike Smith {
144315e32d5dSMike Smith 
1444b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14450ae55423SMike Smith 
144615e32d5dSMike Smith     /*
144715e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1448edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
144915e32d5dSMike Smith      *
145015e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1451be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1452be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1453be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
145415e32d5dSMike Smith      */
14554c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
145633332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1457be2b1797SNate Lawson 	bus, NULL);
145815e32d5dSMike Smith 
1459adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1460adad4744SNate Lawson     acpi_sysres_alloc(bus);
1461adad4744SNate Lawson 
1462edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1463edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1464edc13633SNate Lawson     bus_generic_probe(bus);
1465edc13633SNate Lawson 
1466edc13633SNate Lawson     /* Probe/attach all children, created staticly and from the namespace. */
14674c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
146815e32d5dSMike Smith     bus_generic_attach(bus);
146915e32d5dSMike Smith 
147015e32d5dSMike Smith     /*
147115e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
147215e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
147315e32d5dSMike Smith      */
14744c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
147515e32d5dSMike Smith     bus_generic_attach(bus);
14760ae55423SMike Smith 
147788a79fc0SNate Lawson     /* Attach wake sysctls. */
14785c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
147988a79fc0SNate Lawson 
1480bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
14810ae55423SMike Smith     return_VOID;
148215e32d5dSMike Smith }
148315e32d5dSMike Smith 
1484b82ca9a9SNate Lawson /*
1485b82ca9a9SNate Lawson  * Determine the probe order for a given device and return non-zero if it
1486b82ca9a9SNate Lawson  * should be attached immediately.
1487b82ca9a9SNate Lawson  */
148891233413SNate Lawson static int
1489b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
149091233413SNate Lawson {
149191233413SNate Lawson     int ret;
149291233413SNate Lawson 
1493b82ca9a9SNate Lawson     /*
1494b82ca9a9SNate Lawson      * 1. I/O port and memory system resource holders
1495b82ca9a9SNate Lawson      * 2. Embedded controllers (to handle early accesses)
1496b460c6f8SJohn Baldwin      * 3. PCI Link Devices
1497b82ca9a9SNate Lawson      */
149891233413SNate Lawson     ret = 0;
149991233413SNate Lawson     if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) {
150091233413SNate Lawson 	*order = 1;
15013a6497c1SJohn Baldwin 	ret = 1;
1502b82ca9a9SNate Lawson     } else if (acpi_MatchHid(handle, "PNP0C09")) {
150391233413SNate Lawson 	*order = 2;
15043a6497c1SJohn Baldwin 	ret = 1;
1505b460c6f8SJohn Baldwin     } else if (acpi_MatchHid(handle, "PNP0C0F")) {
1506b460c6f8SJohn Baldwin 	*order = 3;
15073a6497c1SJohn Baldwin 	ret = 1;
150891233413SNate Lawson     }
150991233413SNate Lawson 
151091233413SNate Lawson     return (ret);
151191233413SNate Lawson }
151291233413SNate Lawson 
151315e32d5dSMike Smith /*
151415e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
151515e32d5dSMike Smith  * it.
151615e32d5dSMike Smith  */
151715e32d5dSMike Smith static ACPI_STATUS
151815e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
151915e32d5dSMike Smith {
152015e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
152133332dc2SNate Lawson     device_t bus, child;
152291233413SNate Lawson     int order, probe_now;
152333332dc2SNate Lawson     char *handle_str, **search;
152433332dc2SNate Lawson     static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
152515e32d5dSMike Smith 
1526b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15270ae55423SMike Smith 
1528be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
15290ae55423SMike Smith     if (acpi_avoid(handle))
15300ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
15310ae55423SMike Smith 
153291233413SNate Lawson     bus = (device_t)context;
1533b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
153415e32d5dSMike Smith 	switch (type) {
153515e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
153615e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
153715e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
153815e32d5dSMike Smith 	case ACPI_TYPE_POWER:
15390ae55423SMike Smith 	    if (acpi_disabled("children"))
15400ae55423SMike Smith 		break;
1541be2b1797SNate Lawson 
154215e32d5dSMike Smith 	    /*
154333332dc2SNate Lawson 	     * Since we scan from \, be sure to skip system scope objects.
154433332dc2SNate Lawson 	     * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
154533332dc2SNate Lawson 	     */
154633332dc2SNate Lawson 	    handle_str = acpi_name(handle);
154733332dc2SNate Lawson 	    for (search = scopes; *search != NULL; search++) {
154833332dc2SNate Lawson 		if (strcmp(handle_str, *search) == 0)
154933332dc2SNate Lawson 		    break;
155033332dc2SNate Lawson 	    }
155133332dc2SNate Lawson 	    if (*search != NULL)
155233332dc2SNate Lawson 		break;
155333332dc2SNate Lawson 
155433332dc2SNate Lawson 	    /*
155515e32d5dSMike Smith 	     * Create a placeholder device for this node.  Sort the placeholder
1556b82ca9a9SNate Lawson 	     * so that the probe/attach passes will run breadth-first.  Orders
1557b82ca9a9SNate Lawson 	     * less than 10 are reserved for special objects (i.e., system
1558b82ca9a9SNate Lawson 	     * resources).  Larger values are used for all other devices.
155915e32d5dSMike Smith 	     */
156033332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1561b82ca9a9SNate Lawson 	    order = (level + 1) * 10;
1562b82ca9a9SNate Lawson 	    probe_now = acpi_probe_order(handle, &order);
156391233413SNate Lawson 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1564bc0f2195SMike Smith 	    if (child == NULL)
1565bc0f2195SMike Smith 		break;
156659a890e6SNate Lawson 
156759a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
156815e32d5dSMike Smith 	    acpi_set_handle(child, handle);
156959a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1570bc0f2195SMike Smith 
1571bc0f2195SMike Smith 	    /*
1572b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1573b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1574b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1575893f750aSNate Lawson 	     *
1576893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
1577893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
1578893f750aSNate Lawson 	     * anyway since we may enable them later.
1579b519f9d6SMike Smith 	     */
1580893f750aSNate Lawson 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child) &&
1581893f750aSNate Lawson 		!acpi_MatchHid(handle, "PNP0C0F")) {
1582b519f9d6SMike Smith 		device_disable(child);
1583b519f9d6SMike Smith 		break;
1584b519f9d6SMike Smith 	    }
1585b519f9d6SMike Smith 
1586b519f9d6SMike Smith 	    /*
1587bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1588bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1589bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1590bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1591bc0f2195SMike Smith 	     * device not to have any resources.
1592bc0f2195SMike Smith 	     */
159372ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
1594bc0f2195SMike Smith 
1595f504b607SNate Lawson 	    /* If order was overridden, probe/attach now rather than later. */
159691233413SNate Lawson 	    if (probe_now)
159791233413SNate Lawson 		device_probe_and_attach(child);
15980ae55423SMike Smith 	    break;
159915e32d5dSMike Smith 	}
160015e32d5dSMike Smith     }
1601be2b1797SNate Lawson 
16020ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
160315e32d5dSMike Smith }
160415e32d5dSMike Smith 
160559a890e6SNate Lawson /*
160659a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
160759a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
160859a890e6SNate Lawson  */
160959a890e6SNate Lawson void
161059a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
161159a890e6SNate Lawson {
161259a890e6SNate Lawson }
161359a890e6SNate Lawson 
161415e32d5dSMike Smith static void
161515e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
161615e32d5dSMike Smith {
1617d33f4987STakanori Watanabe     ACPI_STATUS	status;
1618eeb3a05fSNate Lawson 
1619eeb3a05fSNate Lawson     /*
162080f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
162180f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
162280f0e4c2SNate Lawson      * an AP.
1623eeb3a05fSNate Lawson      */
1624eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1625904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1626904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1627904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1628904bf0c2SNate Lawson 		   AcpiFormatException(status));
1629904bf0c2SNate Lawson 	    return;
1630904bf0c2SNate Lawson 	}
1631eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
163255398047SNate Lawson 	ACPI_DISABLE_IRQS();
1633865b8d0bSNate Lawson 	status = AcpiEnterSleepState(ACPI_STATE_S5);
1634be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1635bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
163615e32d5dSMike Smith 	} else {
163715e32d5dSMike Smith 	    DELAY(1000000);
163815e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
163915e32d5dSMike Smith 	}
164087a500cdSNate Lawson     } else if ((howto & RB_AUTOBOOT) != 0 && AcpiGbl_FADT->ResetRegSup) {
164187a500cdSNate Lawson 	status = AcpiHwLowLevelWrite(
164287a500cdSNate Lawson 	    AcpiGbl_FADT->ResetRegister.RegisterBitWidth,
164387a500cdSNate Lawson 	    AcpiGbl_FADT->ResetValue, &AcpiGbl_FADT->ResetRegister);
164487a500cdSNate Lawson 	if (ACPI_FAILURE(status)) {
164587a500cdSNate Lawson 	    printf("ACPI reset failed - %s\n", AcpiFormatException(status));
164687a500cdSNate Lawson 	} else {
164787a500cdSNate Lawson 	    DELAY(1000000);
164887a500cdSNate Lawson 	    printf("ACPI reset failed - timeout\n");
164987a500cdSNate Lawson 	}
1650ba36768bSNate Lawson     } else if (panicstr == NULL) {
165180f0e4c2SNate Lawson 	printf("Shutting down ACPI\n");
165280f0e4c2SNate Lawson 	AcpiTerminate();
165380f0e4c2SNate Lawson     }
165415e32d5dSMike Smith }
165515e32d5dSMike Smith 
165613d4f7baSMitsuru IWASAKI static void
165713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
165813d4f7baSMitsuru IWASAKI {
165913d4f7baSMitsuru IWASAKI     static int	first_time = 1;
166013d4f7baSMitsuru IWASAKI 
166113d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
1662be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
166359ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
166413d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
166533febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1666be2b1797SNate Lawson 	if (first_time)
16676c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
166813d4f7baSMitsuru IWASAKI     }
1669be2b1797SNate Lawson     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
167059ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
167113d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
167233febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1673be2b1797SNate Lawson 	if (first_time)
16746c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
167513d4f7baSMitsuru IWASAKI     }
167613d4f7baSMitsuru IWASAKI 
167713d4f7baSMitsuru IWASAKI     first_time = 0;
167813d4f7baSMitsuru IWASAKI }
167913d4f7baSMitsuru IWASAKI 
168015e32d5dSMike Smith /*
1681c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1682c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1683c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1684c5ba0be4SMike Smith  */
1685c5ba0be4SMike Smith BOOLEAN
1686c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1687c5ba0be4SMike Smith {
16881e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1689c5ba0be4SMike Smith     ACPI_HANDLE		h;
16901e4925e8SNate Lawson     ACPI_BUFFER		buf;
1691c5ba0be4SMike Smith     ACPI_STATUS		error;
16921e4925e8SNate Lawson     int			ret;
1693c5ba0be4SMike Smith 
16941e4925e8SNate Lawson     ret = FALSE;
1695c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1696c5ba0be4SMike Smith 	return (FALSE);
16971e4925e8SNate Lawson     buf.Pointer = NULL;
16981e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
16996fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17006fca9360SNate Lawson     if (ACPI_FAILURE(error))
1701c5ba0be4SMike Smith 	return (FALSE);
17021e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1703be2b1797SNate Lawson 
1704be2b1797SNate Lawson     /* If no _STA method, must be present */
17051e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
17061e4925e8SNate Lawson 	ret = TRUE;
1707be2b1797SNate Lawson 
1708be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
170917dbe0f7SNate Lawson     if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
17101e4925e8SNate Lawson 	ret = TRUE;
1711be2b1797SNate Lawson 
17121e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17131e4925e8SNate Lawson     return (ret);
1714c5ba0be4SMike Smith }
1715c5ba0be4SMike Smith 
1716c5ba0be4SMike Smith /*
1717b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1718b53f2771SMike Smith  */
1719b53f2771SMike Smith BOOLEAN
1720b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1721b53f2771SMike Smith {
17221e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1723b53f2771SMike Smith     ACPI_HANDLE		h;
17241e4925e8SNate Lawson     ACPI_BUFFER		buf;
1725b53f2771SMike Smith     ACPI_STATUS		error;
17261e4925e8SNate Lawson     int			ret;
1727b53f2771SMike Smith 
17281e4925e8SNate Lawson     ret = FALSE;
1729b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1730b53f2771SMike Smith 	return (FALSE);
17311e4925e8SNate Lawson     buf.Pointer = NULL;
17321e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17336fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17346fca9360SNate Lawson     if (ACPI_FAILURE(error))
1735b53f2771SMike Smith 	return (FALSE);
17361e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1737be2b1797SNate Lawson 
1738be2b1797SNate Lawson     /* If no _STA method, must be present */
17391e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
17401e4925e8SNate Lawson 	ret = TRUE;
1741be2b1797SNate Lawson 
174217dbe0f7SNate Lawson     /* Return true for 'present', 'battery present', and 'functioning' */
174317dbe0f7SNate Lawson     if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
17441e4925e8SNate Lawson 	ret = TRUE;
1745be2b1797SNate Lawson 
17461e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17471e4925e8SNate Lawson     return (ret);
1748b53f2771SMike Smith }
1749b53f2771SMike Smith 
1750b53f2771SMike Smith /*
175191233413SNate Lawson  * Match a HID string against a handle
175215e32d5dSMike Smith  */
1753ce619b2eSNate Lawson static BOOLEAN
1754ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
175515e32d5dSMike Smith {
17561e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
17571e4925e8SNate Lawson     ACPI_BUFFER		buf;
175815e32d5dSMike Smith     ACPI_STATUS		error;
17591e4925e8SNate Lawson     int			ret, i;
176015e32d5dSMike Smith 
17611e4925e8SNate Lawson     ret = FALSE;
176291233413SNate Lawson     if (hid == NULL || h == NULL)
176391233413SNate Lawson 	return (ret);
17641e4925e8SNate Lawson     buf.Pointer = NULL;
17651e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17666fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17676fca9360SNate Lawson     if (ACPI_FAILURE(error))
176891233413SNate Lawson 	return (ret);
17691e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1770be2b1797SNate Lawson 
1771c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1772c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
17731e4925e8SNate Lawson 	    ret = TRUE;
1774c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
17751e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
17761e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
17771e4925e8SNate Lawson 		ret = TRUE;
17781e4925e8SNate Lawson 		break;
17791e4925e8SNate Lawson 	    }
17801e4925e8SNate Lawson 	}
17811e4925e8SNate Lawson     }
1782be2b1797SNate Lawson 
17831e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17841e4925e8SNate Lawson     return (ret);
178515e32d5dSMike Smith }
178615e32d5dSMike Smith 
178715e32d5dSMike Smith /*
1788c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1789c5ba0be4SMike Smith  * or one if its parents.
1790c5ba0be4SMike Smith  */
1791c5ba0be4SMike Smith ACPI_STATUS
1792c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1793c5ba0be4SMike Smith {
1794c5ba0be4SMike Smith     ACPI_HANDLE		r;
1795c5ba0be4SMike Smith     ACPI_STATUS		status;
1796c5ba0be4SMike Smith 
1797be2b1797SNate Lawson     /* Walk back up the tree to the root */
1798c5ba0be4SMike Smith     for (;;) {
1799be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1800be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1801c5ba0be4SMike Smith 	    *result = r;
1802c5ba0be4SMike Smith 	    return (AE_OK);
1803c5ba0be4SMike Smith 	}
1804bee4aa4aSNate Lawson 	/* XXX Return error here? */
1805c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1806c5ba0be4SMike Smith 	    return (AE_OK);
1807b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1808c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1809c5ba0be4SMike Smith 	parent = r;
1810c5ba0be4SMike Smith     }
1811c5ba0be4SMike Smith }
1812c5ba0be4SMike Smith 
1813eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1814eea17c34SNate Lawson uint32_t
1815eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1816eea17c34SNate Lawson {
1817eea17c34SNate Lawson     uint32_t delta;
1818eea17c34SNate Lawson 
1819eea17c34SNate Lawson     if (end >= start)
1820eea17c34SNate Lawson 	delta = end - start;
1821eea17c34SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
18228d01ceefSNate Lawson 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1823eea17c34SNate Lawson     else
1824eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
1825eea17c34SNate Lawson     return (delta);
1826eea17c34SNate Lawson }
1827eea17c34SNate Lawson 
1828c5ba0be4SMike Smith /*
1829c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1830c5ba0be4SMike Smith  */
1831c5ba0be4SMike Smith ACPI_BUFFER *
1832c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1833c5ba0be4SMike Smith {
1834c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1835c5ba0be4SMike Smith 
1836c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1837c5ba0be4SMike Smith 	return (NULL);
1838c5ba0be4SMike Smith     buf->Length = size;
1839c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1840c5ba0be4SMike Smith     return (buf);
1841c5ba0be4SMike Smith }
1842c5ba0be4SMike Smith 
1843c310653eSNate Lawson ACPI_STATUS
1844cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1845c310653eSNate Lawson {
1846c310653eSNate Lawson     ACPI_OBJECT arg1;
1847c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1848c310653eSNate Lawson 
1849c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1850c310653eSNate Lawson     arg1.Integer.Value = number;
1851c310653eSNate Lawson     args.Count = 1;
1852c310653eSNate Lawson     args.Pointer = &arg1;
1853c310653eSNate Lawson 
1854c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1855c310653eSNate Lawson }
1856c310653eSNate Lawson 
1857c5ba0be4SMike Smith /*
1858c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1859c5ba0be4SMike Smith  */
1860c5ba0be4SMike Smith ACPI_STATUS
1861cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1862c5ba0be4SMike Smith {
1863be2b1797SNate Lawson     ACPI_STATUS	status;
1864c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1865c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1866c5ba0be4SMike Smith 
1867c5ba0be4SMike Smith     if (handle == NULL)
1868c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
18690c7da7acSJohn Baldwin 
18700c7da7acSJohn Baldwin     /*
18710c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
18720c7da7acSJohn Baldwin      * a method that will return an Integer.
18730c7da7acSJohn Baldwin      */
1874c5ba0be4SMike Smith     buf.Pointer = &param;
1875c5ba0be4SMike Smith     buf.Length = sizeof(param);
1876be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1877be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1878be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1879c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1880be2b1797SNate Lawson 	else
1881be2b1797SNate Lawson 	    status = AE_TYPE;
1882c5ba0be4SMike Smith     }
18830c7da7acSJohn Baldwin 
18840c7da7acSJohn Baldwin     /*
18850c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
18860c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
18870c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
18880c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
18890c7da7acSJohn Baldwin      *
18900c7da7acSJohn Baldwin      * This is a hack.
18910c7da7acSJohn Baldwin      */
1892be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1893b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1894be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
18950c7da7acSJohn Baldwin 	} else {
1896be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1897be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1898be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
18990c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
19000c7da7acSJohn Baldwin 	}
1901f97739daSNate Lawson     }
1902be2b1797SNate Lawson     return (status);
1903c5ba0be4SMike Smith }
1904c5ba0be4SMike Smith 
1905c573e654SMitsuru IWASAKI ACPI_STATUS
1906cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1907c573e654SMitsuru IWASAKI {
1908c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1909dba55fa2SNate Lawson     UINT8	*val;
1910c573e654SMitsuru IWASAKI     int		i;
1911c573e654SMitsuru IWASAKI 
1912c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1913c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1914c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1915c573e654SMitsuru IWASAKI 	return (AE_OK);
1916c573e654SMitsuru IWASAKI     }
1917c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1918c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1919c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1920c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1921be2b1797SNate Lawson 
1922c573e654SMitsuru IWASAKI     *number = 0;
1923dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1924c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1925dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1926c573e654SMitsuru IWASAKI     return (AE_OK);
1927c573e654SMitsuru IWASAKI }
1928c573e654SMitsuru IWASAKI 
1929c5ba0be4SMike Smith /*
1930c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1931c5ba0be4SMike Smith  * function for each element.
1932c5ba0be4SMike Smith  *
1933c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1934c5ba0be4SMike Smith  */
1935c5ba0be4SMike Smith ACPI_STATUS
1936be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1937be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1938c5ba0be4SMike Smith {
1939c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1940c5ba0be4SMike Smith     int		i;
1941c5ba0be4SMike Smith 
1942be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1943c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1944c5ba0be4SMike Smith 
1945be2b1797SNate Lawson     /* Iterate over components */
1946be2b1797SNate Lawson     i = 0;
1947be2b1797SNate Lawson     comp = pkg->Package.Elements;
1948be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
1949c5ba0be4SMike Smith 	func(comp, arg);
1950c5ba0be4SMike Smith 
1951c5ba0be4SMike Smith     return (AE_OK);
1952c5ba0be4SMike Smith }
1953c5ba0be4SMike Smith 
19546f69255bSMike Smith /*
19556f69255bSMike Smith  * Find the (index)th resource object in a set.
19566f69255bSMike Smith  */
19576f69255bSMike Smith ACPI_STATUS
19586d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
19596f69255bSMike Smith {
19606d63101aSMike Smith     ACPI_RESOURCE	*rp;
19616f69255bSMike Smith     int			i;
19626f69255bSMike Smith 
19636d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
19646f69255bSMike Smith     i = index;
19656d63101aSMike Smith     while (i-- > 0) {
1966be2b1797SNate Lawson 	/* Range check */
19676d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
19686f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
1969be2b1797SNate Lawson 
1970be2b1797SNate Lawson 	/* Check for terminator */
1971e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
19726d63101aSMike Smith 	    return (AE_NOT_FOUND);
1973abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
19746f69255bSMike Smith     }
19756f69255bSMike Smith     if (resp != NULL)
19766d63101aSMike Smith 	*resp = rp;
1977be2b1797SNate Lawson 
19786d63101aSMike Smith     return (AE_OK);
19796d63101aSMike Smith }
19806d63101aSMike Smith 
19816d63101aSMike Smith /*
19826d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
19836d63101aSMike Smith  *
19846d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
19856d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
19866d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
19876d63101aSMike Smith  * resources.
19886d63101aSMike Smith  */
19896d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
19906d63101aSMike Smith 
19916d63101aSMike Smith ACPI_STATUS
19926d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
19936d63101aSMike Smith {
19946d63101aSMike Smith     ACPI_RESOURCE	*rp;
19956d63101aSMike Smith     void		*newp;
19966d63101aSMike Smith 
1997be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
19986d63101aSMike Smith     if (buf->Pointer == NULL) {
19996d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
20006d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
20016d63101aSMike Smith 	    return (AE_NO_MEMORY);
20026d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2003e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
20046d63101aSMike Smith 	rp->Length = 0;
20056d63101aSMike Smith     }
20066d63101aSMike Smith     if (res == NULL)
20076d63101aSMike Smith 	return (AE_OK);
20086d63101aSMike Smith 
20096d63101aSMike Smith     /*
20106d63101aSMike Smith      * Scan the current buffer looking for the terminator.
20116d63101aSMike Smith      * This will either find the terminator or hit the end
20126d63101aSMike Smith      * of the buffer and return an error.
20136d63101aSMike Smith      */
20146d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
20156d63101aSMike Smith     for (;;) {
2016be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
20176d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
20186d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2019e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
20206d63101aSMike Smith 	    break;
2021abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
20226d63101aSMike Smith     }
20236d63101aSMike Smith 
20246d63101aSMike Smith     /*
20256d63101aSMike Smith      * Check the size of the buffer and expand if required.
20266d63101aSMike Smith      *
20276d63101aSMike Smith      * Required size is:
20286d63101aSMike Smith      *	size of existing resources before terminator +
20296d63101aSMike Smith      *	size of new resource and header +
20306d63101aSMike Smith      * 	size of terminator.
20316d63101aSMike Smith      *
20326d63101aSMike Smith      * Note that this loop should really only run once, unless
20336d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
20346d63101aSMike Smith      */
20356d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2036e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2037e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
20386d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
20396d63101aSMike Smith 	    return (AE_NO_MEMORY);
20406d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2041a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2042a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
20436d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
20446d63101aSMike Smith 	buf->Pointer = newp;
20456d63101aSMike Smith 	buf->Length += buf->Length;
20466d63101aSMike Smith     }
20476d63101aSMike Smith 
2048be2b1797SNate Lawson     /* Insert the new resource. */
2049e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
20506d63101aSMike Smith 
2051be2b1797SNate Lawson     /* And add the terminator. */
2052abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2053e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
20546d63101aSMike Smith     rp->Length = 0;
20556d63101aSMike Smith 
20566f69255bSMike Smith     return (AE_OK);
20576f69255bSMike Smith }
2058c5ba0be4SMike Smith 
2059da14ac9fSJohn Baldwin /*
2060da14ac9fSJohn Baldwin  * Set interrupt model.
2061da14ac9fSJohn Baldwin  */
2062da14ac9fSJohn Baldwin ACPI_STATUS
2063da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2064da14ac9fSJohn Baldwin {
2065da14ac9fSJohn Baldwin 
2066c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2067da14ac9fSJohn Baldwin }
2068da14ac9fSJohn Baldwin 
2069ece50487SMitsuru IWASAKI static void
2070ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2071ece50487SMitsuru IWASAKI {
2072bee4aa4aSNate Lawson 
2073ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
2074ece50487SMitsuru IWASAKI }
2075c30382dfSMitsuru IWASAKI 
207615e2f34fSNate Lawson enum acpi_sleep_state {
207715e2f34fSNate Lawson     ACPI_SS_NONE,
207815e2f34fSNate Lawson     ACPI_SS_GPE_SET,
207915e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
208015e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
208115e2f34fSNate Lawson     ACPI_SS_SLEPT,
208215e2f34fSNate Lawson };
208315e2f34fSNate Lawson 
208415e32d5dSMike Smith /*
208515e32d5dSMike Smith  * Set the system sleep state
208615e32d5dSMike Smith  *
2087be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
208815e32d5dSMike Smith  */
208915e32d5dSMike Smith ACPI_STATUS
209015e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state)
209115e32d5dSMike Smith {
209215e2f34fSNate Lawson     ACPI_STATUS	status;
209356d8cb57SMitsuru IWASAKI     UINT8	TypeA;
209456d8cb57SMitsuru IWASAKI     UINT8	TypeB;
209515e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
209615e32d5dSMike Smith 
2097b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
20980ae55423SMike Smith 
209915e2f34fSNate Lawson     status = AE_OK;
210015e2f34fSNate Lawson     ACPI_LOCK(acpi);
210115e2f34fSNate Lawson     if (sc->acpi_sleep_disabled) {
21026397624dSMitsuru IWASAKI 	if (sc->acpi_sstate != ACPI_STATE_S0)
210315e2f34fSNate Lawson 	    status = AE_ERROR;
210415e2f34fSNate Lawson 	ACPI_UNLOCK(acpi);
210515e2f34fSNate Lawson 	printf("acpi: suspend request ignored (not ready yet)\n");
210615e2f34fSNate Lawson 	return (status);
210715e2f34fSNate Lawson     }
210815e2f34fSNate Lawson     sc->acpi_sleep_disabled = 1;
210915e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
21106397624dSMitsuru IWASAKI 
21115d3d03f1SNate Lawson     /*
21125d3d03f1SNate Lawson      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
21135d3d03f1SNate Lawson      * drivers need this.
21145d3d03f1SNate Lawson      */
21155d3d03f1SNate Lawson     mtx_lock(&Giant);
211615e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
211715e32d5dSMike Smith     switch (state) {
211815e32d5dSMike Smith     case ACPI_STATE_S1:
21196161544cSTakanori Watanabe     case ACPI_STATE_S2:
21206161544cSTakanori Watanabe     case ACPI_STATE_S3:
21216161544cSTakanori Watanabe     case ACPI_STATE_S4:
2122adad4744SNate Lawson 	status = AcpiGetSleepTypeData(state, &TypeA, &TypeB);
212398175614SNate Lawson 	if (status == AE_NOT_FOUND) {
212498175614SNate Lawson 	    device_printf(sc->acpi_dev,
212598175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
212698175614SNate Lawson 	    break;
212798175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
2128be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
2129be2b1797SNate Lawson 			  AcpiFormatException(status));
213056d8cb57SMitsuru IWASAKI 	    break;
213156d8cb57SMitsuru IWASAKI 	}
213256d8cb57SMitsuru IWASAKI 
2133a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
2134a1fccb47SMitsuru IWASAKI 
2135d4b9ff91SNate Lawson 	/* Enable any GPEs as appropriate and requested by the user. */
2136d4b9ff91SNate Lawson 	acpi_wake_prep_walk(state);
213715e2f34fSNate Lawson 	slp_state = ACPI_SS_GPE_SET;
2138e8b4d56eSNate Lawson 
213915e32d5dSMike Smith 	/*
2140c7f88fc3SNate Lawson 	 * Inform all devices that we are going to sleep.  If at least one
2141c7f88fc3SNate Lawson 	 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
214215e32d5dSMike Smith 	 *
2143c7f88fc3SNate Lawson 	 * XXX Note that a better two-pass approach with a 'veto' pass
2144c7f88fc3SNate Lawson 	 * followed by a "real thing" pass would be better, but the current
2145c7f88fc3SNate Lawson 	 * bus interface does not provide for this.
214615e32d5dSMike Smith 	 */
214715e2f34fSNate Lawson 	if (DEVICE_SUSPEND(root_bus) != 0) {
214815e2f34fSNate Lawson 	    device_printf(sc->acpi_dev, "device_suspend failed\n");
214915e2f34fSNate Lawson 	    break;
215015e2f34fSNate Lawson 	}
215115e2f34fSNate Lawson 	slp_state = ACPI_SS_DEV_SUSPEND;
2152b9c780d6SMitsuru IWASAKI 
2153be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
2154be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2155b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2156b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
2157b9c780d6SMitsuru IWASAKI 	    break;
2158b9c780d6SMitsuru IWASAKI 	}
215915e2f34fSNate Lawson 	slp_state = ACPI_SS_SLP_PREP;
2160b9c780d6SMitsuru IWASAKI 
2161be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
2162ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
2163ff01efb5SMitsuru IWASAKI 
21646161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
21656161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
21666161544cSTakanori Watanabe 
21676161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
2168be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
2169964679ceSMitsuru IWASAKI 		AcpiEnable();
21706161544cSTakanori Watanabe 	} else {
2171c7f88fc3SNate Lawson 	    ACPI_DISABLE_IRQS();
2172adad4744SNate Lawson 	    status = AcpiEnterSleepState(state);
2173be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
2174be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2175be2b1797SNate Lawson 			      AcpiFormatException(status));
2176c30382dfSMitsuru IWASAKI 		break;
217715e32d5dSMike Smith 	    }
21786161544cSTakanori Watanabe 	}
217915e2f34fSNate Lawson 	slp_state = ACPI_SS_SLEPT;
218015e32d5dSMike Smith 	break;
218115e32d5dSMike Smith     case ACPI_STATE_S5:
218215e32d5dSMike Smith 	/*
218315e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
218415e32d5dSMike Smith 	 * shutdown handlers.
218515e32d5dSMike Smith 	 */
218615e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
218715e32d5dSMike Smith 	break;
218898175614SNate Lawson     case ACPI_STATE_S0:
218915e32d5dSMike Smith     default:
219015e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
219115e32d5dSMike Smith 	break;
219215e32d5dSMike Smith     }
2193ece50487SMitsuru IWASAKI 
219415e2f34fSNate Lawson     /*
219515e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
219615e2f34fSNate Lawson      * process.  This handles both the error and success cases.
219715e2f34fSNate Lawson      */
219815e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
219915e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
220015e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
220115e2f34fSNate Lawson     }
220215e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLP_PREP)
220315e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
2204071339e2SNate Lawson     if (slp_state >= ACPI_SS_DEV_SUSPEND)
2205071339e2SNate Lawson 	DEVICE_RESUME(root_bus);
220615e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLEPT)
220715e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
220815e2f34fSNate Lawson 
220915e2f34fSNate Lawson     /* Allow another sleep request after a while. */
221015e2f34fSNate Lawson     if (state != ACPI_STATE_S5)
2211ece50487SMitsuru IWASAKI 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
2212ece50487SMitsuru IWASAKI 
22135d3d03f1SNate Lawson     mtx_unlock(&Giant);
22140ae55423SMike Smith     return_ACPI_STATUS (status);
221515e32d5dSMike Smith }
221615e32d5dSMike Smith 
2217e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
2218e8b4d56eSNate Lawson int
2219e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
2220e8b4d56eSNate Lawson {
2221e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2222e8b4d56eSNate Lawson 
2223e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
2224e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2225e8b4d56eSNate Lawson 	return (ENXIO);
2226e8b4d56eSNate Lawson 
2227e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
2228e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
2229e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
2230e8b4d56eSNate Lawson 	return (ENXIO);
2231e8b4d56eSNate Lawson     }
2232e8b4d56eSNate Lawson 
2233e8b4d56eSNate Lawson     return (0);
2234e8b4d56eSNate Lawson }
2235e8b4d56eSNate Lawson 
2236e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
2237e8b4d56eSNate Lawson int
2238e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
2239e8b4d56eSNate Lawson {
2240e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2241e8b4d56eSNate Lawson     ACPI_HANDLE handle;
2242e8b4d56eSNate Lawson     ACPI_STATUS status;
2243e8b4d56eSNate Lawson     int flags;
2244e8b4d56eSNate Lawson 
2245d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
2246e8b4d56eSNate Lawson     handle = acpi_get_handle(dev);
2247e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2248e8b4d56eSNate Lawson 	return (ENXIO);
2249e8b4d56eSNate Lawson 
2250d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
2251e8b4d56eSNate Lawson     if (enable) {
2252e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2253e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2254e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
2255e8b4d56eSNate Lawson 	    return (ENXIO);
2256e8b4d56eSNate Lawson 	}
2257d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2258e8b4d56eSNate Lawson     } else {
2259e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2260e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2261e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
2262e8b4d56eSNate Lawson 	    return (ENXIO);
2263e8b4d56eSNate Lawson 	}
2264d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2265e8b4d56eSNate Lawson     }
2266e8b4d56eSNate Lawson 
2267e8b4d56eSNate Lawson     return (0);
2268e8b4d56eSNate Lawson }
2269e8b4d56eSNate Lawson 
2270d4b9ff91SNate Lawson static int
2271d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2272e8b4d56eSNate Lawson {
2273e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2274d4b9ff91SNate Lawson     device_t dev;
2275e8b4d56eSNate Lawson 
2276d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
2277e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2278e8b4d56eSNate Lawson 	return (ENXIO);
2279d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2280e8b4d56eSNate Lawson 
2281e8b4d56eSNate Lawson     /*
2282d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
2283d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
2284d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
2285d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
2286d4b9ff91SNate Lawson      * and set _PSW.
2287e8b4d56eSNate Lawson      */
2288d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2289cc85c78cSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2290e8b4d56eSNate Lawson 	if (bootverbose)
2291d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2292d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2293d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2294d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
2295d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
2296d4b9ff91SNate Lawson 	if (bootverbose)
2297d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2298d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2299d4b9ff91SNate Lawson     }
2300cc85c78cSNate Lawson 
2301cc85c78cSNate Lawson     return (0);
2302e8b4d56eSNate Lawson }
2303e8b4d56eSNate Lawson 
2304d4b9ff91SNate Lawson static int
2305d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2306cc85c78cSNate Lawson {
2307cc85c78cSNate Lawson     struct acpi_prw_data prw;
2308d4b9ff91SNate Lawson     device_t dev;
2309cc85c78cSNate Lawson 
2310cc85c78cSNate Lawson     /*
2311d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
2312d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
2313cc85c78cSNate Lawson      */
2314d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2315d4b9ff91SNate Lawson 	return (ENXIO);
2316d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2317d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2318d4b9ff91SNate Lawson 	return (0);
2319cc85c78cSNate Lawson 
2320d4b9ff91SNate Lawson     /*
2321d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
2322d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
2323d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
2324d4b9ff91SNate Lawson      */
2325d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2326cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2327d4b9ff91SNate Lawson 	if (bootverbose)
2328d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2329d4b9ff91SNate Lawson     } else {
2330d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
2331d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
2332d4b9ff91SNate Lawson 	if (bootverbose)
2333d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
2334d4b9ff91SNate Lawson 		acpi_name(handle));
2335d4b9ff91SNate Lawson     }
2336d4b9ff91SNate Lawson 
2337e8b4d56eSNate Lawson     return (0);
2338e8b4d56eSNate Lawson }
2339e8b4d56eSNate Lawson 
2340e8b4d56eSNate Lawson static ACPI_STATUS
2341d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2342e8b4d56eSNate Lawson {
2343d4b9ff91SNate Lawson     int sstate;
2344e8b4d56eSNate Lawson 
2345d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
2346d4b9ff91SNate Lawson     sstate = *(int *)context;
2347d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
2348d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
2349d4b9ff91SNate Lawson     else
2350d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
2351e8b4d56eSNate Lawson     return (AE_OK);
2352e8b4d56eSNate Lawson }
2353e8b4d56eSNate Lawson 
2354d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2355e8b4d56eSNate Lawson static int
2356d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
2357e8b4d56eSNate Lawson {
2358e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
2359e8b4d56eSNate Lawson 
2360e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2361d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
2362d4b9ff91SNate Lawson 	    acpi_wake_prep, &sstate, NULL);
2363e8b4d56eSNate Lawson     return (0);
2364e8b4d56eSNate Lawson }
2365e8b4d56eSNate Lawson 
236688a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
236788a79fc0SNate Lawson static int
236888a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
236988a79fc0SNate Lawson {
237088a79fc0SNate Lawson     int error, i, numdevs;
237188a79fc0SNate Lawson     device_t *devlist;
237288a79fc0SNate Lawson     device_t child;
2373d4b9ff91SNate Lawson     ACPI_STATUS status;
237488a79fc0SNate Lawson 
237588a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
23761c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
23771c9ec538SNate Lawson 	if (numdevs == 0)
23781c9ec538SNate Lawson 	    free(devlist, M_TEMP);
237988a79fc0SNate Lawson 	return (error);
23801c9ec538SNate Lawson     }
238188a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
238288a79fc0SNate Lawson 	child = devlist[i];
2383d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
238488a79fc0SNate Lawson 	if (!device_is_attached(child))
238588a79fc0SNate Lawson 	    continue;
2386d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2387d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
238888a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
238988a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
239088a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
239188a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
239288a79fc0SNate Lawson 	}
239388a79fc0SNate Lawson     }
239488a79fc0SNate Lawson     free(devlist, M_TEMP);
239588a79fc0SNate Lawson 
239688a79fc0SNate Lawson     return (0);
239788a79fc0SNate Lawson }
239888a79fc0SNate Lawson 
239988a79fc0SNate Lawson /* Enable or disable wake from userland. */
240088a79fc0SNate Lawson static int
240188a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
240288a79fc0SNate Lawson {
240388a79fc0SNate Lawson     int enable, error;
240488a79fc0SNate Lawson     device_t dev;
240588a79fc0SNate Lawson 
240688a79fc0SNate Lawson     dev = (device_t)arg1;
2407d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
240888a79fc0SNate Lawson 
240988a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
241088a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
241188a79fc0SNate Lawson 	return (error);
241288a79fc0SNate Lawson     if (enable != 0 && enable != 1)
241388a79fc0SNate Lawson 	return (EINVAL);
241488a79fc0SNate Lawson 
241588a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
241688a79fc0SNate Lawson }
241788a79fc0SNate Lawson 
2418e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
2419d4b9ff91SNate Lawson int
2420e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2421e8b4d56eSNate Lawson {
2422e8b4d56eSNate Lawson     ACPI_STATUS			status;
2423e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2424e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2425d4b9ff91SNate Lawson     int				error, i, power_count;
2426e8b4d56eSNate Lawson 
2427e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2428e8b4d56eSNate Lawson 	return (EINVAL);
2429e8b4d56eSNate Lawson 
2430e8b4d56eSNate Lawson     /*
2431e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2432e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2433e8b4d56eSNate Lawson      */
2434e8b4d56eSNate Lawson     error = EINVAL;
2435e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2436e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2437e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2438e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2439e8b4d56eSNate Lawson 	return (ENOENT);
2440e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2441e8b4d56eSNate Lawson     if (res == NULL)
2442e8b4d56eSNate Lawson 	return (ENOENT);
2443e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2444e8b4d56eSNate Lawson 	goto out;
2445e8b4d56eSNate Lawson 
2446e8b4d56eSNate Lawson     /*
2447e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2448e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2449e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2450e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2451e8b4d56eSNate Lawson      */
2452e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2453e8b4d56eSNate Lawson 	goto out;
2454e8b4d56eSNate Lawson 
2455e8b4d56eSNate Lawson     /*
2456e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2457e8b4d56eSNate Lawson      */
2458e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2459e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2460e8b4d56eSNate Lawson 	/*
2461e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2462e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2463e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2464e8b4d56eSNate Lawson 	 * enabled for the wake event.
2465e8b4d56eSNate Lawson 	 */
2466e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2467e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2468e8b4d56eSNate Lawson 	error = 0;
2469e8b4d56eSNate Lawson 	break;
2470e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2471e8b4d56eSNate Lawson 	/*
2472e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2473e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2474e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2475e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2476e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2477e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2478e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2479e8b4d56eSNate Lawson 	 * the wake event.
2480e8b4d56eSNate Lawson 	 *
2481e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2482e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2483e8b4d56eSNate Lawson 	 */
2484e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2485e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2486e8b4d56eSNate Lawson 	    goto out;
2487e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2488e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2489e8b4d56eSNate Lawson 	    goto out;
2490e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2491e8b4d56eSNate Lawson 	    goto out;
2492e8b4d56eSNate Lawson 	error = 0;
2493e8b4d56eSNate Lawson 	break;
2494e8b4d56eSNate Lawson     default:
2495e8b4d56eSNate Lawson 	goto out;
2496e8b4d56eSNate Lawson     }
2497e8b4d56eSNate Lawson 
2498d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
2499d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
2500d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
2501d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
2502d4b9ff91SNate Lawson 	power_count = 0;
2503d4b9ff91SNate Lawson     }
2504d4b9ff91SNate Lawson     prw->power_res_count = power_count;
2505d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
2506d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
2507e8b4d56eSNate Lawson 
2508e8b4d56eSNate Lawson out:
2509e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2510e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2511e8b4d56eSNate Lawson     return (error);
2512e8b4d56eSNate Lawson }
2513e8b4d56eSNate Lawson 
251415e32d5dSMike Smith /*
251515e32d5dSMike Smith  * ACPI Event Handlers
251615e32d5dSMike Smith  */
251715e32d5dSMike Smith 
251815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
251915e32d5dSMike Smith 
252015e32d5dSMike Smith static void
252115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
252215e32d5dSMike Smith {
2523bee4aa4aSNate Lawson 
2524b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
252515e32d5dSMike Smith 
2526a5d1879bSMitsuru IWASAKI     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
252715e32d5dSMike Smith 	acpi_SetSleepState((struct acpi_softc *)arg, state);
2528bee4aa4aSNate Lawson 
25290ae55423SMike Smith     return_VOID;
253015e32d5dSMike Smith }
253115e32d5dSMike Smith 
253215e32d5dSMike Smith static void
253315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
253415e32d5dSMike Smith {
2535bee4aa4aSNate Lawson 
2536b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
25370ae55423SMike Smith 
2538bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
2539cb9b0d80SMike Smith 
25400ae55423SMike Smith     return_VOID;
254115e32d5dSMike Smith }
254215e32d5dSMike Smith 
254315e32d5dSMike Smith /*
254415e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
254515e32d5dSMike Smith  */
254615e32d5dSMike Smith UINT32
254733febf93SNate Lawson acpi_event_power_button_sleep(void *context)
254815e32d5dSMike Smith {
254915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
255015e32d5dSMike Smith 
2551b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25520ae55423SMike Smith 
255315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
25540ae55423SMike Smith 
2555b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
255615e32d5dSMike Smith }
255715e32d5dSMike Smith 
255815e32d5dSMike Smith UINT32
255933febf93SNate Lawson acpi_event_power_button_wake(void *context)
256015e32d5dSMike Smith {
256115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
256215e32d5dSMike Smith 
2563b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25640ae55423SMike Smith 
256515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
25660ae55423SMike Smith 
2567b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
256815e32d5dSMike Smith }
256915e32d5dSMike Smith 
257015e32d5dSMike Smith UINT32
257133febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
257215e32d5dSMike Smith {
257315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
257415e32d5dSMike Smith 
2575b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25760ae55423SMike Smith 
257715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
25780ae55423SMike Smith 
2579b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
258015e32d5dSMike Smith }
258115e32d5dSMike Smith 
258215e32d5dSMike Smith UINT32
258333febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
258415e32d5dSMike Smith {
258515e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
258615e32d5dSMike Smith 
2587b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
25880ae55423SMike Smith 
258915e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
25900ae55423SMike Smith 
2591b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
259215e32d5dSMike Smith }
259315e32d5dSMike Smith 
259415e32d5dSMike Smith /*
2595bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
2596bee4aa4aSNate Lawson  * use this for single-threaded callers.
259715e32d5dSMike Smith  */
259815e32d5dSMike Smith char *
259915e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
260015e32d5dSMike Smith {
2601bee4aa4aSNate Lawson     ACPI_BUFFER buf;
2602bee4aa4aSNate Lawson     static char data[256];
260315e32d5dSMike Smith 
2604bee4aa4aSNate Lawson     buf.Length = sizeof(data);
2605bee4aa4aSNate Lawson     buf.Pointer = data;
2606cb9b0d80SMike Smith 
26070a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
2608bee4aa4aSNate Lawson 	return (data);
2609bee4aa4aSNate Lawson     return ("(unknown)");
261015e32d5dSMike Smith }
261115e32d5dSMike Smith 
261215e32d5dSMike Smith /*
261315e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
261415e32d5dSMike Smith  * parts of the namespace.
261515e32d5dSMike Smith  */
261615e32d5dSMike Smith int
261715e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
261815e32d5dSMike Smith {
26193c600cfbSJohn Baldwin     char	*cp, *env, *np;
262015e32d5dSMike Smith     int		len;
262115e32d5dSMike Smith 
262215e32d5dSMike Smith     np = acpi_name(handle);
262315e32d5dSMike Smith     if (*np == '\\')
262415e32d5dSMike Smith 	np++;
26253c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
262615e32d5dSMike Smith 	return (0);
262715e32d5dSMike Smith 
2628be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
26293c600cfbSJohn Baldwin     cp = env;
263015e32d5dSMike Smith     for (;;) {
2631bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
263215e32d5dSMike Smith 	    cp++;
263315e32d5dSMike Smith 	if (*cp == 0)
263415e32d5dSMike Smith 	    break;
263515e32d5dSMike Smith 	len = 0;
2636bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
263715e32d5dSMike Smith 	    len++;
2638d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
26393c600cfbSJohn Baldwin 	    freeenv(env);
26400ae55423SMike Smith 	    return(1);
2641d786139cSMaxime Henrion 	}
26420ae55423SMike Smith 	cp += len;
26430ae55423SMike Smith     }
26443c600cfbSJohn Baldwin     freeenv(env);
2645be2b1797SNate Lawson 
26460ae55423SMike Smith     return (0);
26470ae55423SMike Smith }
26480ae55423SMike Smith 
26490ae55423SMike Smith /*
26500ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
26510ae55423SMike Smith  */
26520ae55423SMike Smith int
26530ae55423SMike Smith acpi_disabled(char *subsys)
26540ae55423SMike Smith {
265578689b15SMaxime Henrion     char	*cp, *env;
26560ae55423SMike Smith     int		len;
26570ae55423SMike Smith 
26583184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
26590ae55423SMike Smith 	return (0);
26603184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
266178689b15SMaxime Henrion 	freeenv(env);
26620ae55423SMike Smith 	return (1);
2663d786139cSMaxime Henrion     }
26640ae55423SMike Smith 
26653184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
266678689b15SMaxime Henrion     cp = env;
26670ae55423SMike Smith     for (;;) {
26683184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
26690ae55423SMike Smith 	    cp++;
26703184cf5aSNate Lawson 	if (*cp == '\0')
26710ae55423SMike Smith 	    break;
26720ae55423SMike Smith 	len = 0;
26733184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
26740ae55423SMike Smith 	    len++;
26753184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
267678689b15SMaxime Henrion 	    freeenv(env);
267715e32d5dSMike Smith 	    return (1);
2678d786139cSMaxime Henrion 	}
267915e32d5dSMike Smith 	cp += len;
268015e32d5dSMike Smith     }
268178689b15SMaxime Henrion     freeenv(env);
2682be2b1797SNate Lawson 
268315e32d5dSMike Smith     return (0);
268415e32d5dSMike Smith }
268515e32d5dSMike Smith 
268615e32d5dSMike Smith /*
268715e32d5dSMike Smith  * Control interface.
268815e32d5dSMike Smith  *
26890ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2690be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2691be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
269215e32d5dSMike Smith  */
26930ae55423SMike Smith struct acpi_ioctl_hook
26940ae55423SMike Smith {
26950ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
26960ae55423SMike Smith     u_long			 cmd;
2697be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
26980ae55423SMike Smith     void			 *arg;
26990ae55423SMike Smith };
27000ae55423SMike Smith 
27010ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
27020ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
27030ae55423SMike Smith 
27040ae55423SMike Smith int
2705be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
27060ae55423SMike Smith {
27070ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
27080ae55423SMike Smith 
27090ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
27100ae55423SMike Smith 	return (ENOMEM);
27110ae55423SMike Smith     hp->cmd = cmd;
27120ae55423SMike Smith     hp->fn = fn;
27130ae55423SMike Smith     hp->arg = arg;
271415e2f34fSNate Lawson 
271515e2f34fSNate Lawson     ACPI_LOCK(acpi);
27160ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
27170ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
27180ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
27190ae55423SMike Smith     }
27200ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
272115e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
272215e2f34fSNate Lawson 
27230ae55423SMike Smith     return (0);
27240ae55423SMike Smith }
27250ae55423SMike Smith 
27260ae55423SMike Smith void
2727be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
27280ae55423SMike Smith {
27290ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
27300ae55423SMike Smith 
273115e2f34fSNate Lawson     ACPI_LOCK(acpi);
27320ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
273315e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
27340ae55423SMike Smith 	    break;
27350ae55423SMike Smith 
27360ae55423SMike Smith     if (hp != NULL) {
27370ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
27380ae55423SMike Smith 	free(hp, M_ACPIDEV);
27390ae55423SMike Smith     }
274015e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
27410ae55423SMike Smith }
27420ae55423SMike Smith 
274315e32d5dSMike Smith static int
274489c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
274515e32d5dSMike Smith {
274615e32d5dSMike Smith     return (0);
274715e32d5dSMike Smith }
274815e32d5dSMike Smith 
274915e32d5dSMike Smith static int
275089c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
275115e32d5dSMike Smith {
275215e32d5dSMike Smith     return (0);
275315e32d5dSMike Smith }
275415e32d5dSMike Smith 
275515e32d5dSMike Smith static int
275689c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
275715e32d5dSMike Smith {
275815e32d5dSMike Smith     struct acpi_softc		*sc;
27590ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
2760bee4aa4aSNate Lawson     int				error, state;
276115e32d5dSMike Smith 
2762a2e35898SDavid E. O'Brien     error = 0;
2763a2e35898SDavid E. O'Brien     hp = NULL;
276415e32d5dSMike Smith     sc = dev->si_drv1;
276515e32d5dSMike Smith 
27660ae55423SMike Smith     /*
27670ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
27680ae55423SMike Smith      */
276915e2f34fSNate Lawson     ACPI_LOCK(acpi);
2770bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
27710ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
2772bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
2773bee4aa4aSNate Lawson 		break;
27740ae55423SMike Smith 	}
277515e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
2776bee4aa4aSNate Lawson     if (hp)
2777bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
27780ae55423SMike Smith 
27790ae55423SMike Smith     /*
2780a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
2781a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
2782a89fcf28STakanori Watanabe      * Not changing system behavior.
2783a89fcf28STakanori Watanabe      */
2784be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
2785be2b1797SNate Lawson 	return (EPERM);
2786a89fcf28STakanori Watanabe 
2787be2b1797SNate Lawson     /* Core system ioctls. */
278815e32d5dSMike Smith     switch (cmd) {
278915e32d5dSMike Smith     case ACPIIO_SETSLPSTATE:
2790bee4aa4aSNate Lawson 	error = EINVAL;
279115e32d5dSMike Smith 	state = *(int *)addr;
2792bee4aa4aSNate Lawson 	if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
2793bee4aa4aSNate Lawson 	    if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
2794bee4aa4aSNate Lawson 		error = 0;
279515e32d5dSMike Smith 	break;
279615e32d5dSMike Smith     default:
2797bee4aa4aSNate Lawson 	error = ENXIO;
279815e32d5dSMike Smith 	break;
279915e32d5dSMike Smith     }
2800917d44c8SMitsuru IWASAKI 
280115e32d5dSMike Smith     return (error);
280215e32d5dSMike Smith }
280315e32d5dSMike Smith 
28041d073b1dSJohn Baldwin static int
2805d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
2806d75de536SMitsuru IWASAKI {
2807d75de536SMitsuru IWASAKI     int error;
2808bee4aa4aSNate Lawson     struct sbuf sb;
2809d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
2810d75de536SMitsuru IWASAKI 
2811bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
2812bee4aa4aSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
2813bee4aa4aSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
2814bee4aa4aSNate Lawson 	    sbuf_printf(&sb, "S%d ", state);
2815bee4aa4aSNate Lawson     sbuf_trim(&sb);
2816bee4aa4aSNate Lawson     sbuf_finish(&sb);
2817bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2818bee4aa4aSNate Lawson     sbuf_delete(&sb);
2819d75de536SMitsuru IWASAKI     return (error);
2820d75de536SMitsuru IWASAKI }
2821d75de536SMitsuru IWASAKI 
2822d75de536SMitsuru IWASAKI static int
28231d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
28241d073b1dSJohn Baldwin {
28251d073b1dSJohn Baldwin     char sleep_state[10];
28261d073b1dSJohn Baldwin     int error;
28271d073b1dSJohn Baldwin     u_int new_state, old_state;
28281d073b1dSJohn Baldwin 
28291d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
2830bee4aa4aSNate Lawson     if (old_state > ACPI_S_STATES_MAX + 1)
2831bee4aa4aSNate Lawson 	strlcpy(sleep_state, "unknown", sizeof(sleep_state));
2832bee4aa4aSNate Lawson     else
2833bee4aa4aSNate Lawson 	strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
28341d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
28351d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
2836be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
2837bee4aa4aSNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
2838bee4aa4aSNate Lawson 	    if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
28391d073b1dSJohn Baldwin 		break;
2840931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
2841be2b1797SNate Lawson 	    if (new_state != old_state)
28421d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
2843bee4aa4aSNate Lawson 	} else
28441d073b1dSJohn Baldwin 	    error = EINVAL;
28451d073b1dSJohn Baldwin     }
2846be2b1797SNate Lawson 
28471d073b1dSJohn Baldwin     return (error);
28481d073b1dSJohn Baldwin }
28491d073b1dSJohn Baldwin 
28509b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
28519b937d48SNate Lawson void
28529b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
28539b937d48SNate Lawson {
28549b937d48SNate Lawson     char		notify_buf[16];
28559b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
28569b937d48SNate Lawson     ACPI_STATUS		status;
28579b937d48SNate Lawson 
28589b937d48SNate Lawson     if (subsystem == NULL)
28599b937d48SNate Lawson 	return;
28609b937d48SNate Lawson 
28619b937d48SNate Lawson     handle_buf.Pointer = NULL;
28629b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
28639b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
28649b937d48SNate Lawson     if (ACPI_FAILURE(status))
28659b937d48SNate Lawson 	return;
28669b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
28679b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
28689b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
28699b937d48SNate Lawson }
28709b937d48SNate Lawson 
287115e32d5dSMike Smith #ifdef ACPI_DEBUG
28720ae55423SMike Smith /*
28730ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
28740ae55423SMike Smith  *
28750ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
28760ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
28770ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
28780ae55423SMike Smith  * prefixing the bit name with !.
28790ae55423SMike Smith  */
288015e32d5dSMike Smith struct debugtag
288115e32d5dSMike Smith {
288215e32d5dSMike Smith     char	*name;
288315e32d5dSMike Smith     UINT32	value;
288415e32d5dSMike Smith };
288515e32d5dSMike Smith 
288615e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
2887c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
2888c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
2889c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
2890c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
2891c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
2892c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
2893c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
2894c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
2895c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
2896d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
28974c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
2898d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
2899297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
29004c1cdee6SMike Smith 
2901c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
2902c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
29033184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
2904c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
29053184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
29063184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
29073184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
29084c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
2909cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
29103184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
2911b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
291215e32d5dSMike Smith     {NULL, 0}
291315e32d5dSMike Smith };
291415e32d5dSMike Smith 
291515e32d5dSMike Smith static struct debugtag dbg_level[] = {
29164c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
29179501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
29189501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
29194c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
29209501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
29214c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
2922d62ab2f4SMitsuru IWASAKI 
2923d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
2924297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
29254c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
29264c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
2927d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
29284c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
29294c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
29304c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
29314c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
29324c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
29334c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
29344c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
29354c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
29364c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
29374c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
2938d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
2939d62ab2f4SMitsuru IWASAKI 
2940d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
2941d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
2942d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
2943d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
2944d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
29454c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
2946d62ab2f4SMitsuru IWASAKI 
2947d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
2948d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
2949d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
2950d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
2951d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
2952d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
2953d62ab2f4SMitsuru IWASAKI 
2954d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
295598479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
295698479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
295798479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
295898479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
295998479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
296015e32d5dSMike Smith     {NULL, 0}
296115e32d5dSMike Smith };
296215e32d5dSMike Smith 
296315e32d5dSMike Smith static void
296415e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
296515e32d5dSMike Smith {
296615e32d5dSMike Smith     char	*ep;
296715e32d5dSMike Smith     int		i, l;
29680ae55423SMike Smith     int		set;
296915e32d5dSMike Smith 
297015e32d5dSMike Smith     while (*cp) {
297115e32d5dSMike Smith 	if (isspace(*cp)) {
297215e32d5dSMike Smith 	    cp++;
297315e32d5dSMike Smith 	    continue;
297415e32d5dSMike Smith 	}
297515e32d5dSMike Smith 	ep = cp;
297615e32d5dSMike Smith 	while (*ep && !isspace(*ep))
297715e32d5dSMike Smith 	    ep++;
29780ae55423SMike Smith 	if (*cp == '!') {
29790ae55423SMike Smith 	    set = 0;
29800ae55423SMike Smith 	    cp++;
29810ae55423SMike Smith 	    if (cp == ep)
29820ae55423SMike Smith 		continue;
29830ae55423SMike Smith 	} else {
29840ae55423SMike Smith 	    set = 1;
29850ae55423SMike Smith 	}
298615e32d5dSMike Smith 	l = ep - cp;
298715e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
298815e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
2989be2b1797SNate Lawson 		if (set)
299015e32d5dSMike Smith 		    *flag |= tag[i].value;
2991be2b1797SNate Lawson 		else
29920ae55423SMike Smith 		    *flag &= ~tag[i].value;
299315e32d5dSMike Smith 	    }
299415e32d5dSMike Smith 	}
299515e32d5dSMike Smith 	cp = ep;
299615e32d5dSMike Smith     }
299715e32d5dSMike Smith }
299815e32d5dSMike Smith 
299915e32d5dSMike Smith static void
30002a4ac806SMike Smith acpi_set_debugging(void *junk)
300115e32d5dSMike Smith {
30027e639165SNate Lawson     char	*layer, *level;
300315e32d5dSMike Smith 
30041d7b121cSNate Lawson     if (cold) {
30050ae55423SMike Smith 	AcpiDbgLayer = 0;
30060ae55423SMike Smith 	AcpiDbgLevel = 0;
30071d7b121cSNate Lawson     }
30081d7b121cSNate Lawson 
30097e639165SNate Lawson     layer = getenv("debug.acpi.layer");
30107e639165SNate Lawson     level = getenv("debug.acpi.level");
30117e639165SNate Lawson     if (layer == NULL && level == NULL)
30127e639165SNate Lawson 	return;
30130ae55423SMike Smith 
30147e639165SNate Lawson     printf("ACPI set debug");
30157e639165SNate Lawson     if (layer != NULL) {
30167e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
30177e639165SNate Lawson 	    printf(" layer '%s'", layer);
30187e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
30197e639165SNate Lawson 	freeenv(layer);
30201d7b121cSNate Lawson     }
30217e639165SNate Lawson     if (level != NULL) {
30227e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
30237e639165SNate Lawson 	    printf(" level '%s'", level);
30247e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
30257e639165SNate Lawson 	freeenv(level);
30267e639165SNate Lawson     }
30277e639165SNate Lawson     printf("\n");
302815e32d5dSMike Smith }
3029bee4aa4aSNate Lawson 
3030be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3031be2b1797SNate Lawson 	NULL);
30321d7b121cSNate Lawson 
30331d7b121cSNate Lawson static int
30341d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
30351d7b121cSNate Lawson {
303654f6bca0SNate Lawson     int		 error, *dbg;
30371d7b121cSNate Lawson     struct	 debugtag *tag;
303854f6bca0SNate Lawson     struct	 sbuf sb;
30391d7b121cSNate Lawson 
304054f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
304154f6bca0SNate Lawson 	return (ENOMEM);
30421d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
30431d7b121cSNate Lawson 	tag = &dbg_layer[0];
30441d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
30451d7b121cSNate Lawson     } else {
30461d7b121cSNate Lawson 	tag = &dbg_level[0];
30471d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
30481d7b121cSNate Lawson     }
30491d7b121cSNate Lawson 
30501d7b121cSNate Lawson     /* Get old values if this is a get request. */
305115e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
30521d7b121cSNate Lawson     if (*dbg == 0) {
305354f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
30541d7b121cSNate Lawson     } else if (req->newptr == NULL) {
30551d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
305654f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
305754f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
30581d7b121cSNate Lawson 	}
30591d7b121cSNate Lawson     }
306054f6bca0SNate Lawson     sbuf_trim(&sb);
306154f6bca0SNate Lawson     sbuf_finish(&sb);
30621d7b121cSNate Lawson 
30637e639165SNate Lawson     /* Copy out the old values to the user. */
30647e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
306554f6bca0SNate Lawson     sbuf_delete(&sb);
30661d7b121cSNate Lawson 
30671d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
30681d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
30691d7b121cSNate Lawson 	*dbg = 0;
30701d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
30711d7b121cSNate Lawson 	acpi_set_debugging(NULL);
30721d7b121cSNate Lawson     }
307315e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
30741d7b121cSNate Lawson 
30751d7b121cSNate Lawson     return (error);
30761d7b121cSNate Lawson }
3077bee4aa4aSNate Lawson 
30781d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
30791d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
30801d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
30811d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3082bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3083f9390180SMitsuru IWASAKI 
3084f9390180SMitsuru IWASAKI static int
3085f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
3086f9390180SMitsuru IWASAKI {
3087f9390180SMitsuru IWASAKI 	int	state, acpi_state;
3088f9390180SMitsuru IWASAKI 	int	error;
3089f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
3090f9390180SMitsuru IWASAKI 	va_list	ap;
3091f9390180SMitsuru IWASAKI 
3092f9390180SMitsuru IWASAKI 	error = 0;
3093f9390180SMitsuru IWASAKI 	switch (cmd) {
3094f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
3095f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
3096f9390180SMitsuru IWASAKI 		if (sc == NULL) {
3097f9390180SMitsuru IWASAKI 			error = EINVAL;
3098f9390180SMitsuru IWASAKI 			goto out;
3099f9390180SMitsuru IWASAKI 		}
3100f9390180SMitsuru IWASAKI 
3101f9390180SMitsuru IWASAKI 		va_start(ap, arg);
3102f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
3103f9390180SMitsuru IWASAKI 		va_end(ap);
3104f9390180SMitsuru IWASAKI 
3105f9390180SMitsuru IWASAKI 		switch (state) {
3106f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
3107f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
3108f9390180SMitsuru IWASAKI 			break;
3109f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
3110f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
3111f9390180SMitsuru IWASAKI 			break;
3112f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
3113f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
3114f9390180SMitsuru IWASAKI 			break;
3115f9390180SMitsuru IWASAKI 		default:
3116f9390180SMitsuru IWASAKI 			error = EINVAL;
3117f9390180SMitsuru IWASAKI 			goto out;
3118f9390180SMitsuru IWASAKI 		}
3119f9390180SMitsuru IWASAKI 
3120f9390180SMitsuru IWASAKI 		acpi_SetSleepState(sc, acpi_state);
3121f9390180SMitsuru IWASAKI 		break;
3122f9390180SMitsuru IWASAKI 	default:
3123f9390180SMitsuru IWASAKI 		error = EINVAL;
3124f9390180SMitsuru IWASAKI 		goto out;
3125f9390180SMitsuru IWASAKI 	}
3126f9390180SMitsuru IWASAKI 
3127f9390180SMitsuru IWASAKI out:
3128f9390180SMitsuru IWASAKI 	return (error);
3129f9390180SMitsuru IWASAKI }
3130f9390180SMitsuru IWASAKI 
3131f9390180SMitsuru IWASAKI static void
3132f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
3133f9390180SMitsuru IWASAKI {
3134be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
31356c407052SMitsuru IWASAKI 	return;
3136f9390180SMitsuru IWASAKI 
3137f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3138f9390180SMitsuru IWASAKI }
3139f9390180SMitsuru IWASAKI 
3140f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
3141