xref: /freebsd/sys/dev/acpica/acpi.c (revision 8c0879b6f6711b233268006e4e4e670ca8c170fe)
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/resource.h>
52dc750869SNate Lawson #include <machine/bus.h>
53dc750869SNate Lawson #include <sys/rman.h>
54bc0f2195SMike Smith #include <isa/isavar.h>
55c796e27bSNate Lawson #include <isa/pnpvar.h>
56bc0f2195SMike Smith 
572a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h>
5815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5915e32d5dSMike Smith #include <dev/acpica/acpiio.h>
6087a500cdSNate Lawson #include <contrib/dev/acpica/achware.h>
619b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h>
6215e32d5dSMike Smith 
6310ce62b9SNate Lawson #include "pci_if.h"
6410ce62b9SNate Lawson #include <dev/pci/pcivar.h>
6510ce62b9SNate Lawson #include <dev/pci/pci_private.h>
6610ce62b9SNate Lawson 
672be4e471SJung-uk Kim #include <vm/vm_param.h>
682be4e471SJung-uk Kim 
6915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
7015e32d5dSMike Smith 
71be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
72cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
73b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
740ae55423SMike Smith 
7515e32d5dSMike Smith static d_open_t		acpiopen;
7615e32d5dSMike Smith static d_close_t	acpiclose;
7715e32d5dSMike Smith static d_ioctl_t	acpiioctl;
7815e32d5dSMike Smith 
7915e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
80dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
817ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
827ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
837ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
847ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
8515e32d5dSMike Smith };
8615e32d5dSMike Smith 
87346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
88cb9b0d80SMike Smith struct mtx	acpi_mutex;
89cb9b0d80SMike Smith 
9089fb5af8SNate Lawson /* Bitmap of device quirks. */
9189fb5af8SNate Lawson int		acpi_quirks;
9289fb5af8SNate Lawson 
936d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
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);
110be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
111be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
112be2b1797SNate Lawson 			u_long count, u_int flags);
113be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
114be2b1797SNate Lawson 			int rid, struct resource *r);
1158b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1168b28b622SNate Lawson 		    int rid);
1171e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1181e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
119ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
120ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
121ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
122ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
12310ce62b9SNate Lawson static int	acpi_device_pwr_for_sleep(device_t bus, device_t dev,
12410ce62b9SNate Lawson 		    int *dstate);
125df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
126df8d2a32SNate Lawson 		    void *context, void **retval);
127df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
128df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
12910ce62b9SNate Lawson static int	acpi_set_powerstate_method(device_t bus, device_t child,
13010ce62b9SNate Lawson 		    int state);
131be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
132be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
13315e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
134d227204dSJohn Baldwin static void	acpi_probe_order(ACPI_HANDLE handle, int *order);
135be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
136be2b1797SNate Lawson 		    void *context, void **status);
137ce619b2eSNate Lawson static BOOLEAN	acpi_MatchHid(ACPI_HANDLE h, const char *hid);
13800a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
13915e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
14013d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
141d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
142d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
143d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
14488a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
14588a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
14615e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
14715e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
148d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1491d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
150f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
151879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
152879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
153879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
154879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
155879d6c23STakanori Watanabe 
15615e32d5dSMike Smith static device_method_t acpi_methods[] = {
15715e32d5dSMike Smith     /* Device interface */
15815e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
15915e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
160169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
16156a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
16210ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
16310ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
16415e32d5dSMike Smith 
16515e32d5dSMike Smith     /* Bus interface */
16615e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
16715e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
16810ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
16910ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
17015e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
17115e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
17291233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
17391233413SNate Lawson     DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
17491233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
17515e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
17615e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
1778b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
178879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
179879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
18015e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
18115e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
18215e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
18315e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
18415e32d5dSMike Smith 
185ce619b2eSNate Lawson     /* ACPI bus */
186ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
187ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
18810ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
189df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
190ce619b2eSNate Lawson 
19110ce62b9SNate Lawson     /* PCI emulation */
19210ce62b9SNate Lawson     DEVMETHOD(pci_set_powerstate,	acpi_set_powerstate_method),
19310ce62b9SNate Lawson 
194bc0f2195SMike Smith     /* ISA emulation */
195bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
196bc0f2195SMike Smith 
19715e32d5dSMike Smith     {0, 0}
19815e32d5dSMike Smith };
19915e32d5dSMike Smith 
20015e32d5dSMike Smith static driver_t acpi_driver = {
20115e32d5dSMike Smith     "acpi",
20215e32d5dSMike Smith     acpi_methods,
20315e32d5dSMike Smith     sizeof(struct acpi_softc),
20415e32d5dSMike Smith };
20515e32d5dSMike Smith 
2063273b005SMike Smith static devclass_t acpi_devclass;
2076d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
208a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
20915e32d5dSMike Smith 
21015e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
21115e2f34fSNate Lawson 
212adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
213adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
214adad4744SNate Lawson 
215bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
216bee4aa4aSNate Lawson 
217865b8d0bSNate Lawson static const char* sleep_state_names[] = {
218865b8d0bSNate Lawson     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
219865b8d0bSNate Lawson 
2205217af30SJohn Baldwin /* Holds the description of the acpi0 device. */
2215217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
2225217af30SJohn Baldwin 
223197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
2241d7b121cSNate Lawson static char acpi_ca_version[12];
2251d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2261d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
22715e32d5dSMike Smith 
22815e32d5dSMike Smith /*
229413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
230c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
231c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
232c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
233c9b8d77dSNate Lawson  * some IBMs use such code.
234413081d7SNate Lawson  */
235c9b8d77dSNate Lawson static int acpi_serialize_methods;
236413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
237413081d7SNate Lawson 
23810ce62b9SNate Lawson /* Power devices off and on in suspend and resume.  XXX Remove once tested. */
23910ce62b9SNate Lawson static int acpi_do_powerstate = 1;
24010ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
24110ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
24210ce62b9SNate Lawson     &acpi_do_powerstate, 1, "Turn off devices when suspending.");
24310ce62b9SNate Lawson 
24439da3eb3SNate Lawson /* Allow users to override quirks. */
24539da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
24639da3eb3SNate Lawson 
24793bfd059SNate Lawson static int acpi_susp_bounce;
24893bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
24993bfd059SNate Lawson     &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
25093bfd059SNate Lawson 
251c9b8d77dSNate Lawson /*
2526d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2536d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
2548c0879b6SJohn Baldwin  * It also cannot be unloaded, since the entire system bus hierarchy hangs
255be2b1797SNate Lawson  * off it.
2566d63101aSMike Smith  */
2576d63101aSMike Smith static int
2586d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2596d63101aSMike Smith {
2606d63101aSMike Smith     switch (event) {
2616d63101aSMike Smith     case MOD_LOAD:
26287b45ed5SMitsuru IWASAKI 	if (!cold) {
26387b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
2646d63101aSMike Smith 	    return (EPERM);
26587b45ed5SMitsuru IWASAKI 	}
2666d63101aSMike Smith 	break;
2676d63101aSMike Smith     case MOD_UNLOAD:
268f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
2696d63101aSMike Smith 	    return (EBUSY);
270f9390180SMitsuru IWASAKI 	break;
2716d63101aSMike Smith     default:
2726d63101aSMike Smith 	break;
2736d63101aSMike Smith     }
2746d63101aSMike Smith     return (0);
2756d63101aSMike Smith }
2766d63101aSMike Smith 
2776d63101aSMike Smith /*
278bbc2815cSJohn Baldwin  * Perform early initialization.
27915e32d5dSMike Smith  */
280bbc2815cSJohn Baldwin ACPI_STATUS
281bbc2815cSJohn Baldwin acpi_Startup(void)
28215e32d5dSMike Smith {
28389fb5af8SNate Lawson     static int started = 0;
2842be4e471SJung-uk Kim     ACPI_STATUS status;
2852be4e471SJung-uk Kim     int val;
28615e32d5dSMike Smith 
2871b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
2881b8c233dSPeter Pentchev 
289bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
290bbc2815cSJohn Baldwin     if (started)
2912be4e471SJung-uk Kim 	return_VALUE (AE_OK);
292bbc2815cSJohn Baldwin     started = 1;
29315e32d5dSMike Smith 
294413081d7SNate Lawson     /*
2952be4e471SJung-uk Kim      * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
2962be4e471SJung-uk Kim      * if more tables exist.
297413081d7SNate Lawson      */
2982be4e471SJung-uk Kim     if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
2992be4e471SJung-uk Kim 	printf("ACPI: Table initialisation failed: %s\n",
3002be4e471SJung-uk Kim 	    AcpiFormatException(status));
3012be4e471SJung-uk Kim 	return_VALUE (status);
30215e32d5dSMike Smith     }
3033184cf5aSNate Lawson 
30489fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
3052be4e471SJung-uk Kim     if (acpi_quirks == ACPI_Q_OK)
30689fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
30789fb5af8SNate Lawson 
30839da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
30989fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
31089fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
31189fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
31289fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
3132be4e471SJung-uk Kim 	status = AE_SUPPORT;
31489fb5af8SNate Lawson     }
3153184cf5aSNate Lawson 
3162be4e471SJung-uk Kim     return_VALUE (status);
317bbc2815cSJohn Baldwin }
318bbc2815cSJohn Baldwin 
319bbc2815cSJohn Baldwin /*
3205217af30SJohn Baldwin  * Detect ACPI and perform early initialisation.
321bbc2815cSJohn Baldwin  */
3225217af30SJohn Baldwin int
3235217af30SJohn Baldwin acpi_identify(void)
324bbc2815cSJohn Baldwin {
3255217af30SJohn Baldwin     ACPI_TABLE_RSDP	*rsdp;
3265217af30SJohn Baldwin     ACPI_TABLE_HEADER	*rsdt;
3275217af30SJohn Baldwin     ACPI_PHYSICAL_ADDRESS paddr;
3285217af30SJohn Baldwin     struct sbuf		sb;
329bbc2815cSJohn Baldwin 
330bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
331bbc2815cSJohn Baldwin 
332bbc2815cSJohn Baldwin     if (!cold)
3335217af30SJohn Baldwin 	return (ENXIO);
334bbc2815cSJohn Baldwin 
335bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
336bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
3375217af30SJohn Baldwin 	return (ENXIO);
338bbc2815cSJohn Baldwin 
3395217af30SJohn Baldwin     /* Check for other PM systems. */
3405217af30SJohn Baldwin     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
3415217af30SJohn Baldwin 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
3425217af30SJohn Baldwin 	printf("ACPI identify failed, other PM system enabled.\n");
3435217af30SJohn Baldwin 	return (ENXIO);
3445217af30SJohn Baldwin     }
3454b1ff978SMark Santcroos 
3462be4e471SJung-uk Kim     /* Initialize root tables. */
3472be4e471SJung-uk Kim     if (ACPI_FAILURE(acpi_Startup())) {
3482be4e471SJung-uk Kim 	printf("ACPI: Try disabling either ACPI or apic support.\n");
3495217af30SJohn Baldwin 	return (ENXIO);
3502be4e471SJung-uk Kim     }
35115e32d5dSMike Smith 
3525217af30SJohn Baldwin     if ((paddr = AcpiOsGetRootPointer()) == 0 ||
3535217af30SJohn Baldwin 	(rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
3545217af30SJohn Baldwin 	return (ENXIO);
3555217af30SJohn Baldwin     if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
3565217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
3575217af30SJohn Baldwin     else
3585217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
3595217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
3605217af30SJohn Baldwin 
3615217af30SJohn Baldwin     if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
3625217af30SJohn Baldwin 	return (ENXIO);
3635217af30SJohn Baldwin     sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
3645217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
3655217af30SJohn Baldwin     sbuf_trim(&sb);
3665217af30SJohn Baldwin     sbuf_putc(&sb, ' ');
3675217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
3685217af30SJohn Baldwin     sbuf_trim(&sb);
3695217af30SJohn Baldwin     sbuf_finish(&sb);
3705217af30SJohn Baldwin     sbuf_delete(&sb);
3715217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
3725217af30SJohn Baldwin 
3735217af30SJohn Baldwin     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
3745217af30SJohn Baldwin 
3755217af30SJohn Baldwin     return (0);
37615e32d5dSMike Smith }
37715e32d5dSMike Smith 
37815e32d5dSMike Smith /*
379bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
38015e32d5dSMike Smith  */
38115e32d5dSMike Smith static int
38215e32d5dSMike Smith acpi_probe(device_t dev)
38315e32d5dSMike Smith {
38415e32d5dSMike Smith 
385b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
386f9390180SMitsuru IWASAKI 
3875217af30SJohn Baldwin     device_set_desc(dev, acpi_desc);
388bee4aa4aSNate Lawson 
3892be4e471SJung-uk Kim     return_VALUE (0);
39015e32d5dSMike Smith }
39115e32d5dSMike Smith 
39215e32d5dSMike Smith static int
39315e32d5dSMike Smith acpi_attach(device_t dev)
39415e32d5dSMike Smith {
39515e32d5dSMike Smith     struct acpi_softc	*sc;
3962be4e471SJung-uk Kim     ACPI_TABLE_FACS	*facs;
397cb9b0d80SMike Smith     ACPI_STATUS		status;
398ea27c63eSNate Lawson     int			error, state;
39932d18aa5SMike Smith     UINT32		flags;
400ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
401498d464fSMitsuru IWASAKI     char		*env;
40215e32d5dSMike Smith 
403b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
404bee4aa4aSNate Lawson 
40515e32d5dSMike Smith     sc = device_get_softc(dev);
40615e32d5dSMike Smith     sc->acpi_dev = dev;
40700a30448SNate Lawson     callout_init(&sc->susp_force_to, TRUE);
40815e32d5dSMike Smith 
4092be4e471SJung-uk Kim     error = ENXIO;
4102be4e471SJung-uk Kim 
41191233413SNate Lawson     /* Initialize resource manager. */
41291233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
41391233413SNate Lawson     acpi_rman_io.rm_start = 0;
41491233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
4150bba6acfSJohn Baldwin     acpi_rman_io.rm_descr = "ACPI I/O ports";
41691233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
41791233413SNate Lawson 	panic("acpi rman_init IO ports failed");
41891233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
41991233413SNate Lawson     acpi_rman_mem.rm_start = 0;
42091233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
4210bba6acfSJohn Baldwin     acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
42291233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
42391233413SNate Lawson 	panic("acpi rman_init memory failed");
42491233413SNate Lawson 
4252be4e471SJung-uk Kim     /* Initialise the ACPI mutex */
4262be4e471SJung-uk Kim     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
4272be4e471SJung-uk Kim 
4282be4e471SJung-uk Kim     /*
4292be4e471SJung-uk Kim      * Set the globals from our tunables.  This is needed because ACPI-CA
4302be4e471SJung-uk Kim      * uses UINT8 for some values and we have no tunable_byte.
4312be4e471SJung-uk Kim      */
4322be4e471SJung-uk Kim     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
4332be4e471SJung-uk Kim     AcpiGbl_EnableInterpreterSlack = TRUE;
4342be4e471SJung-uk Kim 
4352be4e471SJung-uk Kim     /* Start up the ACPI CA subsystem. */
4362be4e471SJung-uk Kim     status = AcpiInitializeSubsystem();
4372be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4382be4e471SJung-uk Kim 	device_printf(dev, "Could not initialize Subsystem: %s\n",
4392be4e471SJung-uk Kim 		      AcpiFormatException(status));
4402be4e471SJung-uk Kim 	goto out;
4412be4e471SJung-uk Kim     }
4422be4e471SJung-uk Kim 
4432be4e471SJung-uk Kim     /* Load ACPI name space. */
4442be4e471SJung-uk Kim     status = AcpiLoadTables();
4452be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4462be4e471SJung-uk Kim 	device_printf(dev, "Could not load Namespace: %s\n",
4472be4e471SJung-uk Kim 		      AcpiFormatException(status));
4482be4e471SJung-uk Kim 	goto out;
4492be4e471SJung-uk Kim     }
4502be4e471SJung-uk Kim 
451be2b1797SNate Lawson     /* Install the default address space handlers. */
452be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
453be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
454be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
455be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
456be2b1797SNate Lawson 		      AcpiFormatException(status));
457cb9b0d80SMike Smith 	goto out;
45815e32d5dSMike Smith     }
459be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
460be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
461be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
462be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
463be2b1797SNate Lawson 		      AcpiFormatException(status));
464cb9b0d80SMike Smith 	goto out;
46515e32d5dSMike Smith     }
466be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
467be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
468be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
469be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
470be2b1797SNate Lawson 		      AcpiFormatException(status));
471cb9b0d80SMike Smith 	goto out;
47215e32d5dSMike Smith     }
47315e32d5dSMike Smith 
47415e32d5dSMike Smith     /*
475be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
476be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
477be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
478be2b1797SNate Lawson      * object init pass.
47915e32d5dSMike Smith      *
48032d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
48132d18aa5SMike Smith      *
482be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
483be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
48415e32d5dSMike Smith      */
48532d18aa5SMike Smith     flags = 0;
486d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
48732d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
488bee4aa4aSNate Lawson 
489bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
490b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
491be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
492be2b1797SNate Lawson 		      AcpiFormatException(status));
493cb9b0d80SMike Smith 	goto out;
49415e32d5dSMike Smith     }
49515e32d5dSMike Smith 
496f8335e3aSNate Lawson     /*
497f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
498f8335e3aSNate Lawson      * the namespace has been evaluated.
499da72d149SNate Lawson      *
500da72d149SNate Lawson      * XXX This happens before the sysresource devices have been probed and
501da72d149SNate Lawson      * attached so its resources come from nexus0.  In practice, this isn't
502da72d149SNate Lawson      * a problem but should be addressed eventually.
503f8335e3aSNate Lawson      */
504f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
505f8335e3aSNate Lawson 
506bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
507b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
508be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
509be2b1797SNate Lawson 		      AcpiFormatException(status));
510b69ed3f4SMitsuru IWASAKI 	goto out;
511b69ed3f4SMitsuru IWASAKI     }
512b69ed3f4SMitsuru IWASAKI 
51315e32d5dSMike Smith     /*
5141d073b1dSJohn Baldwin      * Setup our sysctl tree.
5151d073b1dSJohn Baldwin      *
5161d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
5171d073b1dSJohn Baldwin      */
5181d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
5191d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
5201d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
5211d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
5221d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
523d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
524d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
525d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5261d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5271d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5281d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5291d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5301d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5311d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5321d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
5331d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
534f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
535f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
536f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
537f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
538f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
539f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5402d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
541197b4dccSNate Lawson 	OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
542197b4dccSNate Lawson 	"sleep delay");
543ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
544197b4dccSNate Lawson 	OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
5451611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
546197b4dccSNate Lawson 	OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
547d85e6785SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
548d85e6785SNate Lawson 	OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
549d85e6785SNate Lawson 	&sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
550d1b16e18SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
551d1b16e18SNate Lawson 	OID_AUTO, "handle_reboot", CTLFLAG_RW,
552d1b16e18SNate Lawson 	&sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
553bf0c18ecSNate Lawson 
554bf0c18ecSNate Lawson     /*
5552b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
556bf0c18ecSNate Lawson      * stabilize.
557bf0c18ecSNate Lawson      */
5582b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
5592d644607SMitsuru IWASAKI     if (bootverbose)
5602d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
561965a34fbSNate Lawson     if ((env = getenv("hw.acpi.verbose")) != NULL) {
562965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
563498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
564498d464fSMitsuru IWASAKI 	freeenv(env);
565498d464fSMitsuru IWASAKI     }
5661d073b1dSJohn Baldwin 
5672d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
5682be4e471SJung-uk Kim     status = AcpiGetTable(ACPI_SIG_FACS, 0, (ACPI_TABLE_HEADER **)&facs);
5692be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
5702be4e471SJung-uk Kim 	device_printf(dev, "couldn't get FACS: %s\n",
5712be4e471SJung-uk Kim 		      AcpiFormatException(status));
5722be4e471SJung-uk Kim 	error = ENXIO;
5732be4e471SJung-uk Kim 	goto out;
5742be4e471SJung-uk Kim     }
5752be4e471SJung-uk Kim     if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
5762d610c46SNate Lawson 	sc->acpi_s4bios = 1;
5772d610c46SNate Lawson 
5781d073b1dSJohn Baldwin     /*
579ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
580ea27c63eSNate Lawson      * to NONE by default to avoid surprising users.
58115e32d5dSMike Smith      */
582ea27c63eSNate Lawson     sc->acpi_power_button_sx = ACPI_STATE_S5;
583ea27c63eSNate Lawson     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
584f86214b6SMitsuru IWASAKI     sc->acpi_standby_sx = ACPI_STATE_S1;
585f86214b6SMitsuru IWASAKI     sc->acpi_suspend_sx = ACPI_STATE_S3;
58615e32d5dSMike Smith 
587ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
588ea27c63eSNate Lawson     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
58900a30448SNate Lawson     for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
590ea27c63eSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
591ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
592ea27c63eSNate Lawson 	    break;
593ea27c63eSNate Lawson 	}
594ea27c63eSNate Lawson 
59513d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
59615e32d5dSMike Smith 
59715e32d5dSMike Smith     /*
59815e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
59915e32d5dSMike Smith      */
60015e32d5dSMike Smith 
60159e472e9SNate Lawson     /* Register our shutdown handler. */
602be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
603be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
60415e32d5dSMike Smith 
60515e32d5dSMike Smith     /*
60615e32d5dSMike Smith      * Register our acpi event handlers.
60715e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
60815e32d5dSMike Smith      */
609be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
610be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
611be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
612be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
61315e32d5dSMike Smith 
614be2b1797SNate Lawson     /* Flag our initial states. */
61515e32d5dSMike Smith     sc->acpi_enabled = 1;
61615e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
617ece50487SMitsuru IWASAKI     sc->acpi_sleep_disabled = 0;
61815e32d5dSMike Smith 
619be2b1797SNate Lawson     /* Create the control device */
620a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
6212a4689e8SRobert Watson 			      "acpi");
62215e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
62315e32d5dSMike Smith 
624be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
625f86214b6SMitsuru IWASAKI 	goto out;
626f86214b6SMitsuru IWASAKI 
627f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
628f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
629f9390180SMitsuru IWASAKI 
630eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
631eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
632eeb6dba2SJohn Baldwin 
633cb9b0d80SMike Smith     error = 0;
634cb9b0d80SMike Smith 
635cb9b0d80SMike Smith  out:
636cb9b0d80SMike Smith     return_VALUE (error);
63715e32d5dSMike Smith }
63815e32d5dSMike Smith 
639169b539aSNate Lawson static int
64010ce62b9SNate Lawson acpi_suspend(device_t dev)
64110ce62b9SNate Lawson {
64210ce62b9SNate Lawson     device_t child, *devlist;
64310ce62b9SNate Lawson     int error, i, numdevs, pstate;
64410ce62b9SNate Lawson 
6455d3d03f1SNate Lawson     GIANT_REQUIRED;
6465d3d03f1SNate Lawson 
64710ce62b9SNate Lawson     /* First give child devices a chance to suspend. */
64810ce62b9SNate Lawson     error = bus_generic_suspend(dev);
64910ce62b9SNate Lawson     if (error)
65010ce62b9SNate Lawson 	return (error);
65110ce62b9SNate Lawson 
65210ce62b9SNate Lawson     /*
65310ce62b9SNate Lawson      * Now, set them into the appropriate power state, usually D3.  If the
65410ce62b9SNate Lawson      * device has an _SxD method for the next sleep state, use that power
65510ce62b9SNate Lawson      * state instead.
65610ce62b9SNate Lawson      */
65710ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
65810ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
65910ce62b9SNate Lawson 	/* If the device is not attached, we've powered it down elsewhere. */
66010ce62b9SNate Lawson 	child = devlist[i];
66110ce62b9SNate Lawson 	if (!device_is_attached(child))
66210ce62b9SNate Lawson 	    continue;
66310ce62b9SNate Lawson 
66410ce62b9SNate Lawson 	/*
66510ce62b9SNate Lawson 	 * Default to D3 for all sleep states.  The _SxD method is optional
66610ce62b9SNate Lawson 	 * so set the powerstate even if it's absent.
66710ce62b9SNate Lawson 	 */
66810ce62b9SNate Lawson 	pstate = PCI_POWERSTATE_D3;
66910ce62b9SNate Lawson 	error = acpi_device_pwr_for_sleep(device_get_parent(child),
67010ce62b9SNate Lawson 	    child, &pstate);
67110ce62b9SNate Lawson 	if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
67210ce62b9SNate Lawson 	    pci_set_powerstate(child, pstate);
67310ce62b9SNate Lawson     }
67410ce62b9SNate Lawson     free(devlist, M_TEMP);
67510ce62b9SNate Lawson     error = 0;
67610ce62b9SNate Lawson 
67710ce62b9SNate Lawson     return (error);
67810ce62b9SNate Lawson }
67910ce62b9SNate Lawson 
68010ce62b9SNate Lawson static int
68110ce62b9SNate Lawson acpi_resume(device_t dev)
68210ce62b9SNate Lawson {
68310ce62b9SNate Lawson     ACPI_HANDLE handle;
68410ce62b9SNate Lawson     int i, numdevs;
68510ce62b9SNate Lawson     device_t child, *devlist;
68610ce62b9SNate Lawson 
6875d3d03f1SNate Lawson     GIANT_REQUIRED;
6885d3d03f1SNate Lawson 
68910ce62b9SNate Lawson     /*
69010ce62b9SNate Lawson      * Put all devices in D0 before resuming them.  Call _S0D on each one
69110ce62b9SNate Lawson      * since some systems expect this.
69210ce62b9SNate Lawson      */
69310ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
69410ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
69510ce62b9SNate Lawson 	child = devlist[i];
69610ce62b9SNate Lawson 	handle = acpi_get_handle(child);
69710ce62b9SNate Lawson 	if (handle)
69810ce62b9SNate Lawson 	    AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
69910ce62b9SNate Lawson 	if (device_is_attached(child) && acpi_do_powerstate)
70010ce62b9SNate Lawson 	    pci_set_powerstate(child, PCI_POWERSTATE_D0);
70110ce62b9SNate Lawson     }
70210ce62b9SNate Lawson     free(devlist, M_TEMP);
70310ce62b9SNate Lawson 
70410ce62b9SNate Lawson     return (bus_generic_resume(dev));
70510ce62b9SNate Lawson }
70610ce62b9SNate Lawson 
70710ce62b9SNate Lawson static int
708169b539aSNate Lawson acpi_shutdown(device_t dev)
709169b539aSNate Lawson {
710169b539aSNate Lawson 
7115d3d03f1SNate Lawson     GIANT_REQUIRED;
7125d3d03f1SNate Lawson 
7130e414868SNate Lawson     /* Allow children to shutdown first. */
7140e414868SNate Lawson     bus_generic_shutdown(dev);
7150e414868SNate Lawson 
716bee4aa4aSNate Lawson     /*
717bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
718bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
719bee4aa4aSNate Lawson      */
720d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
721d4b9ff91SNate Lawson 
722169b539aSNate Lawson     return (0);
723169b539aSNate Lawson }
724169b539aSNate Lawson 
72515e32d5dSMike Smith /*
72615e32d5dSMike Smith  * Handle a new device being added
72715e32d5dSMike Smith  */
72815e32d5dSMike Smith static device_t
72915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
73015e32d5dSMike Smith {
73115e32d5dSMike Smith     struct acpi_device	*ad;
73215e32d5dSMike Smith     device_t		child;
73315e32d5dSMike Smith 
734be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
73515e32d5dSMike Smith 	return (NULL);
73615e32d5dSMike Smith 
73715e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
73815e32d5dSMike Smith 
73915e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
74015e32d5dSMike Smith     if (child != NULL)
74115e32d5dSMike Smith 	device_set_ivars(child, ad);
74243ce1c77SNate Lawson     else
74343ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
74415e32d5dSMike Smith     return (child);
74515e32d5dSMike Smith }
74615e32d5dSMike Smith 
74715e32d5dSMike Smith static int
74815e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
74915e32d5dSMike Smith {
75015e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
75115e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
75215e32d5dSMike Smith     int retval = 0;
75315e32d5dSMike Smith 
75415e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
7559ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
7569ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
7579ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
7589ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
759a91c5fa8SNate Lawson     if (device_get_flags(child))
760a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
7612c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
76215e32d5dSMike Smith 
76315e32d5dSMike Smith     return (retval);
76415e32d5dSMike Smith }
76515e32d5dSMike Smith 
76610ce62b9SNate Lawson /*
76710ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
76810ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
76910ce62b9SNate Lawson  *
77010ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
77110ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
77210ce62b9SNate Lawson  * them to be powered up.
77310ce62b9SNate Lawson  */
77410ce62b9SNate Lawson static void
77510ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
77610ce62b9SNate Lawson {
77710ce62b9SNate Lawson 
77810ce62b9SNate Lawson     /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
77910ce62b9SNate Lawson }
78010ce62b9SNate Lawson 
78110ce62b9SNate Lawson /*
78210ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
78310ce62b9SNate Lawson  *
78410ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
78510ce62b9SNate Lawson  */
78610ce62b9SNate Lawson static void
78710ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
78810ce62b9SNate Lawson {
78910ce62b9SNate Lawson     device_t child, *devlist;
79010ce62b9SNate Lawson     int i, numdevs;
79110ce62b9SNate Lawson 
79210ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
79310ce62b9SNate Lawson     device_get_children(dev, &devlist, &numdevs);
79410ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
79510ce62b9SNate Lawson 	child = devlist[i];
79610ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
79710ce62b9SNate Lawson 	    /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
79810ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
79910ce62b9SNate Lawson 		; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
80010ce62b9SNate Lawson 	}
80110ce62b9SNate Lawson     }
80210ce62b9SNate Lawson     free(devlist, M_TEMP);
80310ce62b9SNate Lawson }
80410ce62b9SNate Lawson 
80563600cc3SNate Lawson /* Location hint for devctl(8) */
80663600cc3SNate Lawson static int
807247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
808247648afSTakanori Watanabe     size_t buflen)
809247648afSTakanori Watanabe {
810247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
811247648afSTakanori Watanabe 
812247648afSTakanori Watanabe     if (dinfo->ad_handle)
813d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
814247648afSTakanori Watanabe     else
815d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
816247648afSTakanori Watanabe     return (0);
817247648afSTakanori Watanabe }
818247648afSTakanori Watanabe 
81963600cc3SNate Lawson /* PnP information for devctl(8) */
82063600cc3SNate Lawson static int
821247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
822247648afSTakanori Watanabe     size_t buflen)
823247648afSTakanori Watanabe {
824247648afSTakanori Watanabe     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL};
82563600cc3SNate Lawson     ACPI_DEVICE_INFO *adinfo;
82663600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
827247648afSTakanori Watanabe     char *end;
828247648afSTakanori Watanabe     int error;
829247648afSTakanori Watanabe 
830247648afSTakanori Watanabe     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
831247648afSTakanori Watanabe     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
832247648afSTakanori Watanabe     if (error)
833d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
834247648afSTakanori Watanabe     else
835247648afSTakanori Watanabe 	snprintf(buf, buflen, "_HID=%s _UID=%lu",
836247648afSTakanori Watanabe 		 (adinfo->Valid & ACPI_VALID_HID) ?
837d40c0f53SNate Lawson 		 adinfo->HardwareId.Value : "none",
83863600cc3SNate Lawson 		 (adinfo->Valid & ACPI_VALID_UID) ?
83963600cc3SNate Lawson 		 strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
840247648afSTakanori Watanabe     if (adinfo)
841247648afSTakanori Watanabe 	AcpiOsFree(adinfo);
842247648afSTakanori Watanabe 
843247648afSTakanori Watanabe     return (0);
844247648afSTakanori Watanabe }
845247648afSTakanori Watanabe 
846247648afSTakanori Watanabe /*
84715e32d5dSMike Smith  * Handle per-device ivars
84815e32d5dSMike Smith  */
84915e32d5dSMike Smith static int
85015e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
85115e32d5dSMike Smith {
85215e32d5dSMike Smith     struct acpi_device	*ad;
85315e32d5dSMike Smith 
85415e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
85515e32d5dSMike Smith 	printf("device has no ivars\n");
85615e32d5dSMike Smith 	return (ENOENT);
85715e32d5dSMike Smith     }
85815e32d5dSMike Smith 
859be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
86015e32d5dSMike Smith     switch(index) {
86115e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
86215e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
86315e32d5dSMike Smith 	break;
86415e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
86570fa7bc0SNate Lawson 	*(uintptr_t *)result = ad->ad_magic;
86615e32d5dSMike Smith 	break;
86715e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
86815e32d5dSMike Smith 	*(void **)result = ad->ad_private;
86915e32d5dSMike Smith 	break;
870d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
871d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
872d4b9ff91SNate Lawson 	break;
87332d18aa5SMike Smith     case ISA_IVAR_VENDORID:
87432d18aa5SMike Smith     case ISA_IVAR_SERIAL:
87532d18aa5SMike Smith     case ISA_IVAR_COMPATID:
87632d18aa5SMike Smith 	*(int *)result = -1;
87732d18aa5SMike Smith 	break;
87832d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
87932d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
88032d18aa5SMike Smith 	break;
88115e32d5dSMike Smith     default:
88215e32d5dSMike Smith 	return (ENOENT);
88315e32d5dSMike Smith     }
884be2b1797SNate Lawson 
88515e32d5dSMike Smith     return (0);
88615e32d5dSMike Smith }
88715e32d5dSMike Smith 
88815e32d5dSMike Smith static int
88915e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
89015e32d5dSMike Smith {
89115e32d5dSMike Smith     struct acpi_device	*ad;
89215e32d5dSMike Smith 
89315e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
89415e32d5dSMike Smith 	printf("device has no ivars\n");
89515e32d5dSMike Smith 	return (ENOENT);
89615e32d5dSMike Smith     }
89715e32d5dSMike Smith 
89815e32d5dSMike Smith     switch(index) {
89915e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
90015e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
90115e32d5dSMike Smith 	break;
90215e32d5dSMike Smith     case ACPI_IVAR_MAGIC:
90370fa7bc0SNate Lawson 	ad->ad_magic = (uintptr_t)value;
90415e32d5dSMike Smith 	break;
90515e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
90615e32d5dSMike Smith 	ad->ad_private = (void *)value;
90715e32d5dSMike Smith 	break;
908d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
909d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
910d4b9ff91SNate Lawson 	break;
91115e32d5dSMike Smith     default:
9122ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
91315e32d5dSMike Smith 	return (ENOENT);
91415e32d5dSMike Smith     }
915be2b1797SNate Lawson 
91615e32d5dSMike Smith     return (0);
91715e32d5dSMike Smith }
91815e32d5dSMike Smith 
91915e32d5dSMike Smith /*
92015e32d5dSMike Smith  * Handle child resource allocation/removal
92115e32d5dSMike Smith  */
92291233413SNate Lawson static struct resource_list *
92391233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
92415e32d5dSMike Smith {
92591233413SNate Lawson     struct acpi_device		*ad;
92615e32d5dSMike Smith 
92791233413SNate Lawson     ad = device_get_ivars(child);
92891233413SNate Lawson     return (&ad->ad_rl);
92915e32d5dSMike Smith }
93015e32d5dSMike Smith 
931adad4744SNate Lawson /*
932adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
933adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
934adad4744SNate Lawson  */
935adad4744SNate Lawson static int
936adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
937adad4744SNate Lawson {
938adad4744SNate Lawson     struct resource *res;
939adad4744SNate Lawson     struct resource_list *rl;
940adad4744SNate Lawson     struct resource_list_entry *rle;
941adad4744SNate Lawson     struct rman *rm;
942da72d149SNate Lawson     char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
943da72d149SNate Lawson     device_t *children;
944da72d149SNate Lawson     int child_count, i;
945da72d149SNate Lawson 
946da72d149SNate Lawson     /*
947da72d149SNate Lawson      * Probe/attach any sysresource devices.  This would be unnecessary if we
948da72d149SNate Lawson      * had multi-pass probe/attach.
949da72d149SNate Lawson      */
950da72d149SNate Lawson     if (device_get_children(dev, &children, &child_count) != 0)
951da72d149SNate Lawson 	return (ENXIO);
952da72d149SNate Lawson     for (i = 0; i < child_count; i++) {
953da72d149SNate Lawson 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
954da72d149SNate Lawson 	    device_probe_and_attach(children[i]);
955da72d149SNate Lawson     }
956da72d149SNate Lawson     free(children, M_TEMP);
957adad4744SNate Lawson 
958adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
959be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
960adad4744SNate Lawson 	if (rle->res != NULL) {
961adad4744SNate Lawson 	    device_printf(dev, "duplicate resource for %lx\n", rle->start);
962adad4744SNate Lawson 	    continue;
963adad4744SNate Lawson 	}
964adad4744SNate Lawson 
965adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
966adad4744SNate Lawson 	switch (rle->type) {
967adad4744SNate Lawson 	case SYS_RES_IOPORT:
968adad4744SNate Lawson 	    rm = &acpi_rman_io;
969adad4744SNate Lawson 	    break;
970adad4744SNate Lawson 	case SYS_RES_MEMORY:
971adad4744SNate Lawson 	    rm = &acpi_rman_mem;
972adad4744SNate Lawson 	    break;
973adad4744SNate Lawson 	default:
974adad4744SNate Lawson 	    continue;
975adad4744SNate Lawson 	}
976adad4744SNate Lawson 
977adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
978adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
979adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
980adad4744SNate Lawson 	if (res != NULL) {
981adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
982adad4744SNate Lawson 	    rle->res = res;
983adad4744SNate Lawson 	} else
984adad4744SNate Lawson 	    device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
985adad4744SNate Lawson 		rle->start, rle->count, rle->type);
986adad4744SNate Lawson     }
987adad4744SNate Lawson     return (0);
988adad4744SNate Lawson }
989adad4744SNate Lawson 
99015e32d5dSMike Smith static struct resource *
99115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
99215e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
99315e32d5dSMike Smith {
99495957f62SJohn Baldwin     ACPI_RESOURCE ares;
99515e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
99615e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
99791233413SNate Lawson     struct resource_list_entry *rle;
99891233413SNate Lawson     struct resource *res;
99991233413SNate Lawson     struct rman *rm;
100015e32d5dSMike Smith 
100115e2f34fSNate Lawson     res = NULL;
1002397c30a8SJohn Baldwin 
1003397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1004397c30a8SJohn Baldwin     switch (type) {
1005397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1006397c30a8SJohn Baldwin 	rm = &acpi_rman_io;
1007397c30a8SJohn Baldwin 	break;
1008397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1009397c30a8SJohn Baldwin 	rm = &acpi_rman_mem;
1010397c30a8SJohn Baldwin 	break;
1011397c30a8SJohn Baldwin     default:
1012397c30a8SJohn Baldwin 	rm = NULL;
1013397c30a8SJohn Baldwin     }
1014397c30a8SJohn Baldwin 
101515e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
101615e2f34fSNate Lawson 
101791233413SNate Lawson     /*
101891233413SNate Lawson      * If this is an allocation of the "default" range for a given RID, and
101991233413SNate Lawson      * we know what the resources for this device are (i.e., they're on the
102091233413SNate Lawson      * child's resource list), use those start/end values.
102191233413SNate Lawson      */
1022f09aa88cSWarner Losh     if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) {
102391233413SNate Lawson 	rle = resource_list_find(rl, type, *rid);
102491233413SNate Lawson 	if (rle == NULL)
102515e2f34fSNate Lawson 	    goto out;
102691233413SNate Lawson 	start = rle->start;
102791233413SNate Lawson 	end = rle->end;
102891233413SNate Lawson 	count = rle->count;
102991233413SNate Lawson     }
103091233413SNate Lawson 
1031397c30a8SJohn Baldwin     /*
1032397c30a8SJohn Baldwin      * If this is an allocation of a specific range, see if we can satisfy
1033397c30a8SJohn Baldwin      * the request from our system resource regions.  If we can't, pass the
1034397c30a8SJohn Baldwin      * request up to the parent.
1035397c30a8SJohn Baldwin      */
1036147c0ad0SJohn Baldwin     if (start + count - 1 == end && rm != NULL)
1037397c30a8SJohn Baldwin 	res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1038397c30a8SJohn Baldwin 	    child);
1039397c30a8SJohn Baldwin     if (res == NULL) {
104095957f62SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
104195957f62SJohn Baldwin 	    start, end, count, flags);
104295957f62SJohn Baldwin     } else {
1043c3f861f4SWarner Losh 	rman_set_rid(res, *rid);
104491233413SNate Lawson 
104591233413SNate Lawson 	/* If requested, activate the resource using the parent's method. */
104695957f62SJohn Baldwin 	if (flags & RF_ACTIVE)
104791233413SNate Lawson 	    if (bus_activate_resource(child, type, *rid, res) != 0) {
104891233413SNate Lawson 		rman_release_resource(res);
104915e2f34fSNate Lawson 		res = NULL;
105015e2f34fSNate Lawson 		goto out;
105191233413SNate Lawson 	    }
105295957f62SJohn Baldwin     }
105391233413SNate Lawson 
105495957f62SJohn Baldwin     if (res != NULL && device_get_parent(child) == bus)
105595957f62SJohn Baldwin 	switch (type) {
105695957f62SJohn Baldwin 	case SYS_RES_IRQ:
105795957f62SJohn Baldwin 	    /*
105895957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
105995957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
106095957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
106195957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
106295957f62SJohn Baldwin 	     *
106395957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
106495957f62SJohn Baldwin 	     */
106595957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
106695957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
106795957f62SJohn Baldwin 	    break;
106895957f62SJohn Baldwin 	}
106915e2f34fSNate Lawson 
107015e2f34fSNate Lawson out:
107115e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
107291233413SNate Lawson     return (res);
107315e32d5dSMike Smith }
107415e32d5dSMike Smith 
107515e32d5dSMike Smith static int
107691233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid,
107791233413SNate Lawson     struct resource *r)
107815e32d5dSMike Smith {
1079397c30a8SJohn Baldwin     struct rman *rm;
108091233413SNate Lawson     int ret;
108115e32d5dSMike Smith 
1082397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1083397c30a8SJohn Baldwin     switch (type) {
1084397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1085397c30a8SJohn Baldwin 	rm = &acpi_rman_io;
1086397c30a8SJohn Baldwin 	break;
1087397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1088397c30a8SJohn Baldwin 	rm = &acpi_rman_mem;
1089397c30a8SJohn Baldwin 	break;
1090397c30a8SJohn Baldwin     default:
1091397c30a8SJohn Baldwin 	rm = NULL;
1092397c30a8SJohn Baldwin     }
1093397c30a8SJohn Baldwin 
109415e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
109515e2f34fSNate Lawson 
109691233413SNate Lawson     /*
1097397c30a8SJohn Baldwin      * If this resource belongs to one of our internal managers,
1098397c30a8SJohn Baldwin      * deactivate it and release it to the local pool.  If it doesn't,
1099397c30a8SJohn Baldwin      * pass this request up to the parent.
110091233413SNate Lawson      */
1101397c30a8SJohn Baldwin     if (rm != NULL && rman_is_region_manager(r, rm)) {
110291233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
110391233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
110491233413SNate Lawson 	    if (ret != 0)
110515e2f34fSNate Lawson 		goto out;
110691233413SNate Lawson 	}
110791233413SNate Lawson 	ret = rman_release_resource(r);
110891233413SNate Lawson     } else
110991233413SNate Lawson 	ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
111091233413SNate Lawson 
111115e2f34fSNate Lawson out:
111215e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
111391233413SNate Lawson     return (ret);
111415e32d5dSMike Smith }
111515e32d5dSMike Smith 
11168b28b622SNate Lawson static void
11178b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
11188b28b622SNate Lawson {
11198b28b622SNate Lawson     struct resource_list *rl;
11208b28b622SNate Lawson 
11218b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
11228b28b622SNate Lawson     resource_list_delete(rl, type, rid);
11238b28b622SNate Lawson }
11248b28b622SNate Lawson 
1125dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1126e1c4bf3fSNate Lawson int
1127e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1128907b6777SNate Lawson     struct resource **res, u_int flags)
1129dc750869SNate Lawson {
1130e1c4bf3fSNate Lawson     int error, res_type;
1131dc750869SNate Lawson 
1132e1c4bf3fSNate Lawson     error = ENOMEM;
11338f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1134e1c4bf3fSNate Lawson 	return (EINVAL);
1135dc750869SNate Lawson 
11368f118e25SNate Lawson     /* We only support memory and IO spaces. */
11372be4e471SJung-uk Kim     switch (gas->SpaceId) {
1138dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1139e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1140dc750869SNate Lawson 	break;
1141dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1142e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1143dc750869SNate Lawson 	break;
1144dc750869SNate Lawson     default:
1145e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1146dc750869SNate Lawson     }
1147dc750869SNate Lawson 
11482fe912dfSNate Lawson     /*
1149f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1150f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
11512fe912dfSNate Lawson      */
11522be4e471SJung-uk Kim     if (gas->BitWidth && gas->BitWidth < 8)
11532be4e471SJung-uk Kim 	gas->BitWidth = 8;
11542fe912dfSNate Lawson 
11558f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
11562be4e471SJung-uk Kim     if (gas->Address == 0 || gas->BitWidth == 0)
11578f118e25SNate Lawson 	return (EINVAL);
11588f118e25SNate Lawson 
1159e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
11602be4e471SJung-uk Kim 	gas->BitWidth / 8);
1161907b6777SNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1162e1c4bf3fSNate Lawson     if (*res != NULL) {
1163e1c4bf3fSNate Lawson 	*type = res_type;
1164e1c4bf3fSNate Lawson 	error = 0;
1165ca2c69c8SNate Lawson     } else
1166ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1167ca2c69c8SNate Lawson 
1168e1c4bf3fSNate Lawson     return (error);
1169dc750869SNate Lawson }
1170dc750869SNate Lawson 
1171c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
11721e4925e8SNate Lawson static uint32_t
117332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1174bc0f2195SMike Smith {
11751e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
11761e4925e8SNate Lawson     ACPI_BUFFER		buf;
1177bc0f2195SMike Smith     ACPI_HANDLE		h;
1178bc0f2195SMike Smith     ACPI_STATUS		error;
1179bc0f2195SMike Smith     u_int32_t		pnpid;
118032d18aa5SMike Smith 
1181b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
118232d18aa5SMike Smith 
118332d18aa5SMike Smith     pnpid = 0;
1184bd189fe7SNate Lawson     buf.Pointer = NULL;
1185bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1186bd189fe7SNate Lawson 
11876fca9360SNate Lawson     /* Fetch and validate the HID. */
118832d18aa5SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1189bd189fe7SNate Lawson 	goto out;
11906fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
11916fca9360SNate Lawson     if (ACPI_FAILURE(error))
1192bd189fe7SNate Lawson 	goto out;
11931e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
119432d18aa5SMike Smith 
11951e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
11961e4925e8SNate Lawson 	pnpid = PNP_EISAID(devinfo->HardwareId.Value);
1197be2b1797SNate Lawson 
1198bd189fe7SNate Lawson out:
1199bd189fe7SNate Lawson     if (buf.Pointer != NULL)
12001e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
120132d18aa5SMike Smith     return_VALUE (pnpid);
120232d18aa5SMike Smith }
120332d18aa5SMike Smith 
12041e4925e8SNate Lawson static int
12051e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1206b2566207STakanori Watanabe {
12071e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
12081e4925e8SNate Lawson     ACPI_BUFFER		buf;
1209b2566207STakanori Watanabe     ACPI_HANDLE		h;
1210b2566207STakanori Watanabe     ACPI_STATUS		error;
12111e4925e8SNate Lawson     uint32_t		*pnpid;
12121e4925e8SNate Lawson     int			valid, i;
1213b2566207STakanori Watanabe 
1214b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1215b2566207STakanori Watanabe 
12161e4925e8SNate Lawson     pnpid = cids;
12171e4925e8SNate Lawson     valid = 0;
1218bd189fe7SNate Lawson     buf.Pointer = NULL;
1219bd189fe7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
1220bd189fe7SNate Lawson 
12211e4925e8SNate Lawson     /* Fetch and validate the CID */
1222b2566207STakanori Watanabe     if ((h = acpi_get_handle(dev)) == NULL)
1223bd189fe7SNate Lawson 	goto out;
12241e4925e8SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
12251e4925e8SNate Lawson     if (ACPI_FAILURE(error))
1226bd189fe7SNate Lawson 	goto out;
12271e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
12281e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
1229b2566207STakanori Watanabe 	goto out;
1230b2566207STakanori Watanabe 
12311e4925e8SNate Lawson     if (devinfo->CompatibilityId.Count < count)
12321e4925e8SNate Lawson 	count = devinfo->CompatibilityId.Count;
12331e4925e8SNate Lawson     for (i = 0; i < count; i++) {
12341e4925e8SNate Lawson 	if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
12351e4925e8SNate Lawson 	    continue;
12361e4925e8SNate Lawson 	*pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
12371e4925e8SNate Lawson 	valid++;
1238b2566207STakanori Watanabe     }
1239b2566207STakanori Watanabe 
12401e4925e8SNate Lawson out:
1241bd189fe7SNate Lawson     if (buf.Pointer != NULL)
12421e4925e8SNate Lawson 	AcpiOsFree(buf.Pointer);
12431e4925e8SNate Lawson     return_VALUE (valid);
12441e4925e8SNate Lawson }
1245b2566207STakanori Watanabe 
1246ce619b2eSNate Lawson static char *
1247ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1248ce619b2eSNate Lawson {
1249ce619b2eSNate Lawson     ACPI_HANDLE h;
1250ce619b2eSNate Lawson     int i;
1251ce619b2eSNate Lawson 
1252ce619b2eSNate Lawson     h = acpi_get_handle(dev);
1253ce619b2eSNate Lawson     if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
1254ce619b2eSNate Lawson 	return (NULL);
1255ce619b2eSNate Lawson 
1256ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1257ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1258ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1259ce619b2eSNate Lawson 	    return (ids[i]);
1260ce619b2eSNate Lawson     }
1261ce619b2eSNate Lawson     return (NULL);
1262ce619b2eSNate Lawson }
1263ce619b2eSNate Lawson 
1264ce619b2eSNate Lawson static ACPI_STATUS
1265ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1266ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1267ce619b2eSNate Lawson {
1268ce619b2eSNate Lawson     ACPI_HANDLE h;
1269ce619b2eSNate Lawson 
1270df8d2a32SNate Lawson     if (dev == NULL)
1271df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1272df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1273ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1274ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1275ce619b2eSNate Lawson }
1276ce619b2eSNate Lawson 
127710ce62b9SNate Lawson static int
127810ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
127910ce62b9SNate Lawson {
128010ce62b9SNate Lawson     struct acpi_softc *sc;
128110ce62b9SNate Lawson     ACPI_HANDLE handle;
128210ce62b9SNate Lawson     ACPI_STATUS status;
128310ce62b9SNate Lawson     char sxd[8];
128410ce62b9SNate Lawson     int error;
128510ce62b9SNate Lawson 
128610ce62b9SNate Lawson     sc = device_get_softc(bus);
128710ce62b9SNate Lawson     handle = acpi_get_handle(dev);
128810ce62b9SNate Lawson 
128910ce62b9SNate Lawson     /*
129010ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
129110ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
129210ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
129310ce62b9SNate Lawson      * need special handling in their drivers.
129410ce62b9SNate Lawson      */
129510ce62b9SNate Lawson     if (handle == NULL ||
129610ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
129710ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
129810ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
129910ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
130010ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
130110ce62b9SNate Lawson 	return (ENXIO);
130210ce62b9SNate Lawson 
130310ce62b9SNate Lawson     /*
130410ce62b9SNate Lawson      * Override next state with the value from _SxD, if present.  If no
130510ce62b9SNate Lawson      * dstate argument was provided, don't fetch the return value.
130610ce62b9SNate Lawson      */
130710ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
130810ce62b9SNate Lawson     if (dstate)
130910ce62b9SNate Lawson 	status = acpi_GetInteger(handle, sxd, dstate);
131010ce62b9SNate Lawson     else
131110ce62b9SNate Lawson 	status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
131210ce62b9SNate Lawson 
131310ce62b9SNate Lawson     switch (status) {
131410ce62b9SNate Lawson     case AE_OK:
131510ce62b9SNate Lawson 	error = 0;
131610ce62b9SNate Lawson 	break;
131710ce62b9SNate Lawson     case AE_NOT_FOUND:
131810ce62b9SNate Lawson 	error = ESRCH;
131910ce62b9SNate Lawson 	break;
132010ce62b9SNate Lawson     default:
132110ce62b9SNate Lawson 	error = ENXIO;
132210ce62b9SNate Lawson 	break;
132310ce62b9SNate Lawson     }
132410ce62b9SNate Lawson 
132510ce62b9SNate Lawson     return (error);
132610ce62b9SNate Lawson }
132710ce62b9SNate Lawson 
1328df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1329df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1330df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1331df8d2a32SNate Lawson     void		*arg;
1332df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1333df8d2a32SNate Lawson };
1334df8d2a32SNate Lawson 
1335ce619b2eSNate Lawson static ACPI_STATUS
1336df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1337df8d2a32SNate Lawson {
1338df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1339df8d2a32SNate Lawson     device_t dev, old_dev;
1340df8d2a32SNate Lawson     ACPI_STATUS status;
1341df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1342df8d2a32SNate Lawson 
1343df8d2a32SNate Lawson     /*
1344df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1345df8d2a32SNate Lawson      * the parent where the scan began.
1346df8d2a32SNate Lawson      */
1347df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1348df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1349df8d2a32SNate Lawson 	return (AE_OK);
1350df8d2a32SNate Lawson 
1351df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1352df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1353df8d2a32SNate Lawson 	return (AE_OK);
1354df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1355df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1356df8d2a32SNate Lawson 	return (AE_OK);
1357df8d2a32SNate Lawson 
1358df8d2a32SNate Lawson     /*
1359df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1360df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1361df8d2a32SNate Lawson      */
1362df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1363df8d2a32SNate Lawson     dev = old_dev;
1364df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1365df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1366df8d2a32SNate Lawson 	return (status);
1367df8d2a32SNate Lawson 
1368df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1369df8d2a32SNate Lawson     if (old_dev != NULL) {
1370df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1371df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1372df8d2a32SNate Lawson     }
1373df8d2a32SNate Lawson 
1374df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1375df8d2a32SNate Lawson     if (dev != NULL)
1376df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1377df8d2a32SNate Lawson 
1378df8d2a32SNate Lawson     return (AE_OK);
1379df8d2a32SNate Lawson }
1380df8d2a32SNate Lawson 
1381df8d2a32SNate Lawson static ACPI_STATUS
1382df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1383df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1384ce619b2eSNate Lawson {
1385ce619b2eSNate Lawson     ACPI_HANDLE h;
1386df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1387ce619b2eSNate Lawson 
1388df8d2a32SNate Lawson     if (acpi_disabled("children"))
1389df8d2a32SNate Lawson 	return (AE_OK);
1390df8d2a32SNate Lawson 
1391df8d2a32SNate Lawson     if (dev == NULL)
1392df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1393df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1394ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1395df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1396df8d2a32SNate Lawson     ctx.arg = arg;
1397df8d2a32SNate Lawson     ctx.parent = h;
1398df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
1399df8d2a32SNate Lawson 	acpi_device_scan_cb, &ctx, NULL));
1400ce619b2eSNate Lawson }
1401ce619b2eSNate Lawson 
140210ce62b9SNate Lawson /*
140310ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
140410ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
140510ce62b9SNate Lawson  */
140610ce62b9SNate Lawson static int
140710ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state)
140810ce62b9SNate Lawson {
140910ce62b9SNate Lawson     ACPI_HANDLE h;
141010ce62b9SNate Lawson     ACPI_STATUS status;
141110ce62b9SNate Lawson     int error;
141210ce62b9SNate Lawson 
141310ce62b9SNate Lawson     error = 0;
141410ce62b9SNate Lawson     h = acpi_get_handle(child);
141510ce62b9SNate Lawson     if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
141610ce62b9SNate Lawson 	return (EINVAL);
141710ce62b9SNate Lawson     if (h == NULL)
141810ce62b9SNate Lawson 	return (0);
141910ce62b9SNate Lawson 
142010ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
142110ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
142210ce62b9SNate Lawson     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
142310ce62b9SNate Lawson 	&& status != AE_BAD_PARAMETER)
142410ce62b9SNate Lawson 	device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
142510ce62b9SNate Lawson 	    state, acpi_name(h), AcpiFormatException(status));
142610ce62b9SNate Lawson 
142710ce62b9SNate Lawson     return (error);
142810ce62b9SNate Lawson }
142910ce62b9SNate Lawson 
143032d18aa5SMike Smith static int
143132d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
143232d18aa5SMike Smith {
14331e4925e8SNate Lawson     int			result, cid_count, i;
14341e4925e8SNate Lawson     uint32_t		lid, cids[8];
1435bc0f2195SMike Smith 
1436b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1437bc0f2195SMike Smith 
1438bc0f2195SMike Smith     /*
1439bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1440bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1441bc0f2195SMike Smith      * that to happen, so don't ever return it.
1442bc0f2195SMike Smith      */
1443bc0f2195SMike Smith     result = ENXIO;
1444bc0f2195SMike Smith 
1445be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1446b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
14471e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1448bc0f2195SMike Smith     while (ids && ids->ip_id) {
14491e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1450bc0f2195SMike Smith 	    result = 0;
1451bc0f2195SMike Smith 	    goto out;
1452bc0f2195SMike Smith 	}
14531e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
14541e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
14551e4925e8SNate Lawson 		result = 0;
14561e4925e8SNate Lawson 		goto out;
14571e4925e8SNate Lawson 	    }
14581e4925e8SNate Lawson 	}
1459bc0f2195SMike Smith 	ids++;
1460bc0f2195SMike Smith     }
1461be2b1797SNate Lawson 
1462bc0f2195SMike Smith  out:
14639e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
14649e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
14659e0dd54fSNate Lawson 
1466bc0f2195SMike Smith     return_VALUE (result);
1467bc0f2195SMike Smith }
1468bc0f2195SMike Smith 
1469bc0f2195SMike Smith /*
147033332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
147115e32d5dSMike Smith  *
147233332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
147333332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
147433332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
147533332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
147633332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
147715e32d5dSMike Smith  */
147815e32d5dSMike Smith static void
147915e32d5dSMike Smith acpi_probe_children(device_t bus)
148015e32d5dSMike Smith {
148115e32d5dSMike Smith 
1482b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
14830ae55423SMike Smith 
148415e32d5dSMike Smith     /*
148515e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1486edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
148715e32d5dSMike Smith      *
148815e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1489be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1490be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1491be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
149215e32d5dSMike Smith      */
14934c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
149433332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
1495be2b1797SNate Lawson 	bus, NULL);
149615e32d5dSMike Smith 
1497adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1498adad4744SNate Lawson     acpi_sysres_alloc(bus);
1499adad4744SNate Lawson 
1500edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1501edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1502edc13633SNate Lawson     bus_generic_probe(bus);
1503edc13633SNate Lawson 
1504edc13633SNate Lawson     /* Probe/attach all children, created staticly and from the namespace. */
15054c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
150615e32d5dSMike Smith     bus_generic_attach(bus);
150715e32d5dSMike Smith 
150815e32d5dSMike Smith     /*
150915e32d5dSMike Smith      * Some of these children may have attached others as part of their attach
151015e32d5dSMike Smith      * process (eg. the root PCI bus driver), so rescan.
151115e32d5dSMike Smith      */
15124c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
151315e32d5dSMike Smith     bus_generic_attach(bus);
15140ae55423SMike Smith 
151588a79fc0SNate Lawson     /* Attach wake sysctls. */
15165c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
151788a79fc0SNate Lawson 
1518bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
15190ae55423SMike Smith     return_VOID;
152015e32d5dSMike Smith }
152115e32d5dSMike Smith 
1522b82ca9a9SNate Lawson /*
1523d227204dSJohn Baldwin  * Determine the probe order for a given device.
1524b82ca9a9SNate Lawson  */
1525d227204dSJohn Baldwin static void
1526b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
152791233413SNate Lawson {
1528463e0f91SJohn Baldwin     ACPI_OBJECT_TYPE type;
152991233413SNate Lawson 
1530b82ca9a9SNate Lawson     /*
1531b82ca9a9SNate Lawson      * 1. I/O port and memory system resource holders
1532b82ca9a9SNate Lawson      * 2. Embedded controllers (to handle early accesses)
1533b460c6f8SJohn Baldwin      * 3. PCI Link Devices
1534b6618687SJohn Baldwin      * 100000. CPUs
1535b82ca9a9SNate Lawson      */
1536463e0f91SJohn Baldwin     AcpiGetType(handle, &type);
1537da72d149SNate Lawson     if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02"))
153891233413SNate Lawson 	*order = 1;
1539da72d149SNate Lawson     else if (acpi_MatchHid(handle, "PNP0C09"))
154091233413SNate Lawson 	*order = 2;
1541da72d149SNate Lawson     else if (acpi_MatchHid(handle, "PNP0C0F"))
1542b460c6f8SJohn Baldwin 	*order = 3;
1543d227204dSJohn Baldwin     else if (type == ACPI_TYPE_PROCESSOR)
1544b6618687SJohn Baldwin 	*order = 100000;
154591233413SNate Lawson }
154691233413SNate Lawson 
154715e32d5dSMike Smith /*
154815e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
154915e32d5dSMike Smith  * it.
155015e32d5dSMike Smith  */
155115e32d5dSMike Smith static ACPI_STATUS
155215e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
155315e32d5dSMike Smith {
155415e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
1555858a52f4SMitsuru IWASAKI     ACPI_HANDLE h;
155633332dc2SNate Lawson     device_t bus, child;
1557da72d149SNate Lawson     int order;
155833332dc2SNate Lawson     char *handle_str, **search;
155933332dc2SNate Lawson     static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
156015e32d5dSMike Smith 
1561b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
15620ae55423SMike Smith 
1563be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
15640ae55423SMike Smith     if (acpi_avoid(handle))
15650ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
15660ae55423SMike Smith 
156791233413SNate Lawson     bus = (device_t)context;
1568b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
156915e32d5dSMike Smith 	switch (type) {
157015e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
157115e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
157215e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
157315e32d5dSMike Smith 	case ACPI_TYPE_POWER:
15740ae55423SMike Smith 	    if (acpi_disabled("children"))
15750ae55423SMike Smith 		break;
1576be2b1797SNate Lawson 
157715e32d5dSMike Smith 	    /*
157833332dc2SNate Lawson 	     * Since we scan from \, be sure to skip system scope objects.
157933332dc2SNate Lawson 	     * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
158033332dc2SNate Lawson 	     */
158133332dc2SNate Lawson 	    handle_str = acpi_name(handle);
158233332dc2SNate Lawson 	    for (search = scopes; *search != NULL; search++) {
158333332dc2SNate Lawson 		if (strcmp(handle_str, *search) == 0)
158433332dc2SNate Lawson 		    break;
158533332dc2SNate Lawson 	    }
158633332dc2SNate Lawson 	    if (*search != NULL)
158733332dc2SNate Lawson 		break;
158833332dc2SNate Lawson 
158933332dc2SNate Lawson 	    /*
1590463e0f91SJohn Baldwin 	     * Create a placeholder device for this node.  Sort the
1591463e0f91SJohn Baldwin 	     * placeholder so that the probe/attach passes will run
1592463e0f91SJohn Baldwin 	     * breadth-first.  Orders less than ACPI_DEV_BASE_ORDER
1593463e0f91SJohn Baldwin 	     * are reserved for special objects (i.e., system
1594b6618687SJohn Baldwin 	     * resources).  CPU devices have a very high order to
1595b6618687SJohn Baldwin 	     * ensure they are probed after other devices.
159615e32d5dSMike Smith 	     */
159733332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1598d227204dSJohn Baldwin 	    order = level * 10 + 100;
1599da72d149SNate Lawson 	    acpi_probe_order(handle, &order);
160091233413SNate Lawson 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1601bc0f2195SMike Smith 	    if (child == NULL)
1602bc0f2195SMike Smith 		break;
160359a890e6SNate Lawson 
160459a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
160515e32d5dSMike Smith 	    acpi_set_handle(child, handle);
160659a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1607bc0f2195SMike Smith 
1608bc0f2195SMike Smith 	    /*
1609b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1610b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1611b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1612893f750aSNate Lawson 	     *
1613893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
1614893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
1615893f750aSNate Lawson 	     * anyway since we may enable them later.
1616b519f9d6SMike Smith 	     */
1617858a52f4SMitsuru IWASAKI 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1618858a52f4SMitsuru IWASAKI 		/* Never disable PCI link devices. */
1619858a52f4SMitsuru IWASAKI 		if (acpi_MatchHid(handle, "PNP0C0F"))
1620858a52f4SMitsuru IWASAKI 		    break;
1621858a52f4SMitsuru IWASAKI 		/*
1622858a52f4SMitsuru IWASAKI 		 * Docking stations should remain enabled since the system
1623858a52f4SMitsuru IWASAKI 		 * may be undocked at boot.
1624858a52f4SMitsuru IWASAKI 		 */
1625858a52f4SMitsuru IWASAKI 		if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
1626858a52f4SMitsuru IWASAKI 		    break;
1627858a52f4SMitsuru IWASAKI 
1628b519f9d6SMike Smith 		device_disable(child);
1629b519f9d6SMike Smith 		break;
1630b519f9d6SMike Smith 	    }
1631b519f9d6SMike Smith 
1632b519f9d6SMike Smith 	    /*
1633bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1634bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1635bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1636bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1637bc0f2195SMike Smith 	     * device not to have any resources.
1638bc0f2195SMike Smith 	     */
163972ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
16400ae55423SMike Smith 	    break;
164115e32d5dSMike Smith 	}
164215e32d5dSMike Smith     }
1643be2b1797SNate Lawson 
16440ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
164515e32d5dSMike Smith }
164615e32d5dSMike Smith 
164759a890e6SNate Lawson /*
164859a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
164959a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
165059a890e6SNate Lawson  */
165159a890e6SNate Lawson void
165259a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
165359a890e6SNate Lawson {
165459a890e6SNate Lawson }
165559a890e6SNate Lawson 
165615e32d5dSMike Smith static void
165715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
165815e32d5dSMike Smith {
1659d85e6785SNate Lawson     struct acpi_softc *sc;
1660d33f4987STakanori Watanabe     ACPI_STATUS status;
1661eeb3a05fSNate Lawson 
1662eeb3a05fSNate Lawson     /*
166380f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
166480f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
166580f0e4c2SNate Lawson      * an AP.
1666eeb3a05fSNate Lawson      */
1667d85e6785SNate Lawson     sc = arg;
1668eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1669904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1670904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
1671904bf0c2SNate Lawson 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
1672904bf0c2SNate Lawson 		   AcpiFormatException(status));
1673904bf0c2SNate Lawson 	    return;
1674904bf0c2SNate Lawson 	}
1675eeb3a05fSNate Lawson 	printf("Powering system off using ACPI\n");
167655398047SNate Lawson 	ACPI_DISABLE_IRQS();
1677865b8d0bSNate Lawson 	status = AcpiEnterSleepState(ACPI_STATE_S5);
1678be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
1679bfae45aaSMike Smith 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
168015e32d5dSMike Smith 	} else {
168115e32d5dSMike Smith 	    DELAY(1000000);
168215e32d5dSMike Smith 	    printf("ACPI power-off failed - timeout\n");
168315e32d5dSMike Smith 	}
16842be4e471SJung-uk Kim     } else if ((howto & RB_HALT) == 0 &&
16852be4e471SJung-uk Kim 	(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) &&
1686d1b16e18SNate Lawson 	sc->acpi_handle_reboot) {
1687d85e6785SNate Lawson 	/* Reboot using the reset register. */
168887a500cdSNate Lawson 	status = AcpiHwLowLevelWrite(
16892be4e471SJung-uk Kim 	    AcpiGbl_FADT.ResetRegister.BitWidth,
16902be4e471SJung-uk Kim 	    AcpiGbl_FADT.ResetValue, &AcpiGbl_FADT.ResetRegister);
169187a500cdSNate Lawson 	if (ACPI_FAILURE(status)) {
169287a500cdSNate Lawson 	    printf("ACPI reset failed - %s\n", AcpiFormatException(status));
169387a500cdSNate Lawson 	} else {
169487a500cdSNate Lawson 	    DELAY(1000000);
169587a500cdSNate Lawson 	    printf("ACPI reset failed - timeout\n");
169687a500cdSNate Lawson 	}
1697d85e6785SNate Lawson     } else if (sc->acpi_do_disable && panicstr == NULL) {
1698d85e6785SNate Lawson 	/*
1699d85e6785SNate Lawson 	 * Only disable ACPI if the user requested.  On some systems, writing
1700d85e6785SNate Lawson 	 * the disable value to SMI_CMD hangs the system.
1701d85e6785SNate Lawson 	 */
170280f0e4c2SNate Lawson 	printf("Shutting down ACPI\n");
170380f0e4c2SNate Lawson 	AcpiTerminate();
170480f0e4c2SNate Lawson     }
170515e32d5dSMike Smith }
170615e32d5dSMike Smith 
170713d4f7baSMitsuru IWASAKI static void
170813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
170913d4f7baSMitsuru IWASAKI {
171013d4f7baSMitsuru IWASAKI     static int	first_time = 1;
171113d4f7baSMitsuru IWASAKI 
171213d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
17132be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
171459ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
171513d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
171633febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1717be2b1797SNate Lawson 	if (first_time)
17186c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
171913d4f7baSMitsuru IWASAKI     }
17202be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
172159ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
172213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
172333febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1724be2b1797SNate Lawson 	if (first_time)
17256c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
172613d4f7baSMitsuru IWASAKI     }
172713d4f7baSMitsuru IWASAKI 
172813d4f7baSMitsuru IWASAKI     first_time = 0;
172913d4f7baSMitsuru IWASAKI }
173013d4f7baSMitsuru IWASAKI 
173115e32d5dSMike Smith /*
1732c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1733c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1734c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1735c5ba0be4SMike Smith  */
1736c5ba0be4SMike Smith BOOLEAN
1737c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1738c5ba0be4SMike Smith {
17391e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1740c5ba0be4SMike Smith     ACPI_HANDLE		h;
17411e4925e8SNate Lawson     ACPI_BUFFER		buf;
1742c5ba0be4SMike Smith     ACPI_STATUS		error;
17431e4925e8SNate Lawson     int			ret;
1744c5ba0be4SMike Smith 
17451e4925e8SNate Lawson     ret = FALSE;
1746c5ba0be4SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1747c5ba0be4SMike Smith 	return (FALSE);
17481e4925e8SNate Lawson     buf.Pointer = NULL;
17491e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17506fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17516fca9360SNate Lawson     if (ACPI_FAILURE(error))
1752c5ba0be4SMike Smith 	return (FALSE);
17531e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1754be2b1797SNate Lawson 
1755be2b1797SNate Lawson     /* If no _STA method, must be present */
17561e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
17571e4925e8SNate Lawson 	ret = TRUE;
1758be2b1797SNate Lawson 
1759be2b1797SNate Lawson     /* Return true for 'present' and 'functioning' */
176017dbe0f7SNate Lawson     if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
17611e4925e8SNate Lawson 	ret = TRUE;
1762be2b1797SNate Lawson 
17631e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17641e4925e8SNate Lawson     return (ret);
1765c5ba0be4SMike Smith }
1766c5ba0be4SMike Smith 
1767c5ba0be4SMike Smith /*
1768b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1769b53f2771SMike Smith  */
1770b53f2771SMike Smith BOOLEAN
1771b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1772b53f2771SMike Smith {
17731e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1774b53f2771SMike Smith     ACPI_HANDLE		h;
17751e4925e8SNate Lawson     ACPI_BUFFER		buf;
1776b53f2771SMike Smith     ACPI_STATUS		error;
17771e4925e8SNate Lawson     int			ret;
1778b53f2771SMike Smith 
17791e4925e8SNate Lawson     ret = FALSE;
1780b53f2771SMike Smith     if ((h = acpi_get_handle(dev)) == NULL)
1781b53f2771SMike Smith 	return (FALSE);
17821e4925e8SNate Lawson     buf.Pointer = NULL;
17831e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
17846fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
17856fca9360SNate Lawson     if (ACPI_FAILURE(error))
1786b53f2771SMike Smith 	return (FALSE);
17871e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1788be2b1797SNate Lawson 
1789be2b1797SNate Lawson     /* If no _STA method, must be present */
17901e4925e8SNate Lawson     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
17911e4925e8SNate Lawson 	ret = TRUE;
1792be2b1797SNate Lawson 
179317dbe0f7SNate Lawson     /* Return true for 'present', 'battery present', and 'functioning' */
179417dbe0f7SNate Lawson     if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
17951e4925e8SNate Lawson 	ret = TRUE;
1796be2b1797SNate Lawson 
17971e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
17981e4925e8SNate Lawson     return (ret);
1799b53f2771SMike Smith }
1800b53f2771SMike Smith 
1801b53f2771SMike Smith /*
180291233413SNate Lawson  * Match a HID string against a handle
180315e32d5dSMike Smith  */
1804ce619b2eSNate Lawson static BOOLEAN
1805ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
180615e32d5dSMike Smith {
18071e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
18081e4925e8SNate Lawson     ACPI_BUFFER		buf;
180915e32d5dSMike Smith     ACPI_STATUS		error;
18101e4925e8SNate Lawson     int			ret, i;
181115e32d5dSMike Smith 
18121e4925e8SNate Lawson     ret = FALSE;
181391233413SNate Lawson     if (hid == NULL || h == NULL)
181491233413SNate Lawson 	return (ret);
18151e4925e8SNate Lawson     buf.Pointer = NULL;
18161e4925e8SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
18176fca9360SNate Lawson     error = AcpiGetObjectInfo(h, &buf);
18186fca9360SNate Lawson     if (ACPI_FAILURE(error))
181991233413SNate Lawson 	return (ret);
18201e4925e8SNate Lawson     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
1821be2b1797SNate Lawson 
1822c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
1823c59c9a8eSJohn Baldwin 	strcmp(hid, devinfo->HardwareId.Value) == 0)
18241e4925e8SNate Lawson 	    ret = TRUE;
1825c59c9a8eSJohn Baldwin     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
18261e4925e8SNate Lawson 	for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
18271e4925e8SNate Lawson 	    if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
18281e4925e8SNate Lawson 		ret = TRUE;
18291e4925e8SNate Lawson 		break;
18301e4925e8SNate Lawson 	    }
18311e4925e8SNate Lawson 	}
18321e4925e8SNate Lawson     }
1833be2b1797SNate Lawson 
18341e4925e8SNate Lawson     AcpiOsFree(buf.Pointer);
18351e4925e8SNate Lawson     return (ret);
183615e32d5dSMike Smith }
183715e32d5dSMike Smith 
183815e32d5dSMike Smith /*
1839c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1840c5ba0be4SMike Smith  * or one if its parents.
1841c5ba0be4SMike Smith  */
1842c5ba0be4SMike Smith ACPI_STATUS
1843c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1844c5ba0be4SMike Smith {
1845c5ba0be4SMike Smith     ACPI_HANDLE		r;
1846c5ba0be4SMike Smith     ACPI_STATUS		status;
1847c5ba0be4SMike Smith 
1848be2b1797SNate Lawson     /* Walk back up the tree to the root */
1849c5ba0be4SMike Smith     for (;;) {
1850be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1851be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1852c5ba0be4SMike Smith 	    *result = r;
1853c5ba0be4SMike Smith 	    return (AE_OK);
1854c5ba0be4SMike Smith 	}
1855bee4aa4aSNate Lawson 	/* XXX Return error here? */
1856c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1857c5ba0be4SMike Smith 	    return (AE_OK);
1858b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1859c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1860c5ba0be4SMike Smith 	parent = r;
1861c5ba0be4SMike Smith     }
1862c5ba0be4SMike Smith }
1863c5ba0be4SMike Smith 
1864eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
1865eea17c34SNate Lawson uint32_t
1866eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
1867eea17c34SNate Lawson {
1868eea17c34SNate Lawson     uint32_t delta;
1869eea17c34SNate Lawson 
1870eea17c34SNate Lawson     if (end >= start)
1871eea17c34SNate Lawson 	delta = end - start;
18722be4e471SJung-uk Kim     else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
1873eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
18742be4e471SJung-uk Kim     else
18752be4e471SJung-uk Kim 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
1876eea17c34SNate Lawson     return (delta);
1877eea17c34SNate Lawson }
1878eea17c34SNate Lawson 
1879c5ba0be4SMike Smith /*
1880c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
1881c5ba0be4SMike Smith  */
1882c5ba0be4SMike Smith ACPI_BUFFER *
1883c5ba0be4SMike Smith acpi_AllocBuffer(int size)
1884c5ba0be4SMike Smith {
1885c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
1886c5ba0be4SMike Smith 
1887c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
1888c5ba0be4SMike Smith 	return (NULL);
1889c5ba0be4SMike Smith     buf->Length = size;
1890c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
1891c5ba0be4SMike Smith     return (buf);
1892c5ba0be4SMike Smith }
1893c5ba0be4SMike Smith 
1894c310653eSNate Lawson ACPI_STATUS
1895cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
1896c310653eSNate Lawson {
1897c310653eSNate Lawson     ACPI_OBJECT arg1;
1898c310653eSNate Lawson     ACPI_OBJECT_LIST args;
1899c310653eSNate Lawson 
1900c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
1901c310653eSNate Lawson     arg1.Integer.Value = number;
1902c310653eSNate Lawson     args.Count = 1;
1903c310653eSNate Lawson     args.Pointer = &arg1;
1904c310653eSNate Lawson 
1905c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
1906c310653eSNate Lawson }
1907c310653eSNate Lawson 
1908c5ba0be4SMike Smith /*
1909c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
1910c5ba0be4SMike Smith  */
1911c5ba0be4SMike Smith ACPI_STATUS
1912cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
1913c5ba0be4SMike Smith {
1914be2b1797SNate Lawson     ACPI_STATUS	status;
1915c5ba0be4SMike Smith     ACPI_BUFFER	buf;
1916c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
1917c5ba0be4SMike Smith 
1918c5ba0be4SMike Smith     if (handle == NULL)
1919c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
19200c7da7acSJohn Baldwin 
19210c7da7acSJohn Baldwin     /*
19220c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
19230c7da7acSJohn Baldwin      * a method that will return an Integer.
19240c7da7acSJohn Baldwin      */
1925c5ba0be4SMike Smith     buf.Pointer = &param;
1926c5ba0be4SMike Smith     buf.Length = sizeof(param);
1927be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
1928be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
1929be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
1930c5ba0be4SMike Smith 	    *number = param.Integer.Value;
1931be2b1797SNate Lawson 	else
1932be2b1797SNate Lawson 	    status = AE_TYPE;
1933c5ba0be4SMike Smith     }
19340c7da7acSJohn Baldwin 
19350c7da7acSJohn Baldwin     /*
19360c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
19370c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
19380c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
19390c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
19400c7da7acSJohn Baldwin      *
19410c7da7acSJohn Baldwin      * This is a hack.
19420c7da7acSJohn Baldwin      */
1943be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
1944b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
1945be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
19460c7da7acSJohn Baldwin 	} else {
1947be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
1948be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
1949be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
19500c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
19510c7da7acSJohn Baldwin 	}
1952f97739daSNate Lawson     }
1953be2b1797SNate Lawson     return (status);
1954c5ba0be4SMike Smith }
1955c5ba0be4SMike Smith 
1956c573e654SMitsuru IWASAKI ACPI_STATUS
1957cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
1958c573e654SMitsuru IWASAKI {
1959c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
1960dba55fa2SNate Lawson     UINT8	*val;
1961c573e654SMitsuru IWASAKI     int		i;
1962c573e654SMitsuru IWASAKI 
1963c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
1964c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
1965c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
1966c573e654SMitsuru IWASAKI 	return (AE_OK);
1967c573e654SMitsuru IWASAKI     }
1968c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
1969c573e654SMitsuru IWASAKI 	return (AE_TYPE);
1970c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
1971c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
1972be2b1797SNate Lawson 
1973c573e654SMitsuru IWASAKI     *number = 0;
1974dba55fa2SNate Lawson     val = p->Buffer.Pointer;
1975c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
1976dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
1977c573e654SMitsuru IWASAKI     return (AE_OK);
1978c573e654SMitsuru IWASAKI }
1979c573e654SMitsuru IWASAKI 
1980c5ba0be4SMike Smith /*
1981c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
1982c5ba0be4SMike Smith  * function for each element.
1983c5ba0be4SMike Smith  *
1984c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
1985c5ba0be4SMike Smith  */
1986c5ba0be4SMike Smith ACPI_STATUS
1987be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
1988be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
1989c5ba0be4SMike Smith {
1990c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
1991c5ba0be4SMike Smith     int		i;
1992c5ba0be4SMike Smith 
1993be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1994c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
1995c5ba0be4SMike Smith 
1996be2b1797SNate Lawson     /* Iterate over components */
1997be2b1797SNate Lawson     i = 0;
1998be2b1797SNate Lawson     comp = pkg->Package.Elements;
1999be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
2000c5ba0be4SMike Smith 	func(comp, arg);
2001c5ba0be4SMike Smith 
2002c5ba0be4SMike Smith     return (AE_OK);
2003c5ba0be4SMike Smith }
2004c5ba0be4SMike Smith 
20056f69255bSMike Smith /*
20066f69255bSMike Smith  * Find the (index)th resource object in a set.
20076f69255bSMike Smith  */
20086f69255bSMike Smith ACPI_STATUS
20096d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
20106f69255bSMike Smith {
20116d63101aSMike Smith     ACPI_RESOURCE	*rp;
20126f69255bSMike Smith     int			i;
20136f69255bSMike Smith 
20146d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
20156f69255bSMike Smith     i = index;
20166d63101aSMike Smith     while (i-- > 0) {
2017be2b1797SNate Lawson 	/* Range check */
20186d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
20196f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
2020be2b1797SNate Lawson 
2021be2b1797SNate Lawson 	/* Check for terminator */
2022e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
20236d63101aSMike Smith 	    return (AE_NOT_FOUND);
2024abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
20256f69255bSMike Smith     }
20266f69255bSMike Smith     if (resp != NULL)
20276d63101aSMike Smith 	*resp = rp;
2028be2b1797SNate Lawson 
20296d63101aSMike Smith     return (AE_OK);
20306d63101aSMike Smith }
20316d63101aSMike Smith 
20326d63101aSMike Smith /*
20336d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
20346d63101aSMike Smith  *
20356d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
20366d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
20376d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
20386d63101aSMike Smith  * resources.
20396d63101aSMike Smith  */
20406d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
20416d63101aSMike Smith 
20426d63101aSMike Smith ACPI_STATUS
20436d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
20446d63101aSMike Smith {
20456d63101aSMike Smith     ACPI_RESOURCE	*rp;
20466d63101aSMike Smith     void		*newp;
20476d63101aSMike Smith 
2048be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
20496d63101aSMike Smith     if (buf->Pointer == NULL) {
20506d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
20516d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
20526d63101aSMike Smith 	    return (AE_NO_MEMORY);
20536d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2054e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
20556d63101aSMike Smith 	rp->Length = 0;
20566d63101aSMike Smith     }
20576d63101aSMike Smith     if (res == NULL)
20586d63101aSMike Smith 	return (AE_OK);
20596d63101aSMike Smith 
20606d63101aSMike Smith     /*
20616d63101aSMike Smith      * Scan the current buffer looking for the terminator.
20626d63101aSMike Smith      * This will either find the terminator or hit the end
20636d63101aSMike Smith      * of the buffer and return an error.
20646d63101aSMike Smith      */
20656d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
20666d63101aSMike Smith     for (;;) {
2067be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
20686d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
20696d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2070e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
20716d63101aSMike Smith 	    break;
2072abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
20736d63101aSMike Smith     }
20746d63101aSMike Smith 
20756d63101aSMike Smith     /*
20766d63101aSMike Smith      * Check the size of the buffer and expand if required.
20776d63101aSMike Smith      *
20786d63101aSMike Smith      * Required size is:
20796d63101aSMike Smith      *	size of existing resources before terminator +
20806d63101aSMike Smith      *	size of new resource and header +
20816d63101aSMike Smith      * 	size of terminator.
20826d63101aSMike Smith      *
20836d63101aSMike Smith      * Note that this loop should really only run once, unless
20846d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
20856d63101aSMike Smith      */
20866d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2087e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2088e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
20896d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
20906d63101aSMike Smith 	    return (AE_NO_MEMORY);
20916d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2092a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2093a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
20946d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
20956d63101aSMike Smith 	buf->Pointer = newp;
20966d63101aSMike Smith 	buf->Length += buf->Length;
20976d63101aSMike Smith     }
20986d63101aSMike Smith 
2099be2b1797SNate Lawson     /* Insert the new resource. */
2100e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
21016d63101aSMike Smith 
2102be2b1797SNate Lawson     /* And add the terminator. */
2103abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2104e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
21056d63101aSMike Smith     rp->Length = 0;
21066d63101aSMike Smith 
21076f69255bSMike Smith     return (AE_OK);
21086f69255bSMike Smith }
2109c5ba0be4SMike Smith 
2110da14ac9fSJohn Baldwin /*
2111da14ac9fSJohn Baldwin  * Set interrupt model.
2112da14ac9fSJohn Baldwin  */
2113da14ac9fSJohn Baldwin ACPI_STATUS
2114da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2115da14ac9fSJohn Baldwin {
2116da14ac9fSJohn Baldwin 
2117c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2118da14ac9fSJohn Baldwin }
2119da14ac9fSJohn Baldwin 
212000a30448SNate Lawson /*
212100a30448SNate Lawson  * DEPRECATED.  This interface has serious deficiencies and will be
212200a30448SNate Lawson  * removed.
212300a30448SNate Lawson  *
212400a30448SNate Lawson  * Immediately enter the sleep state.  In the old model, acpiconf(8) ran
212500a30448SNate Lawson  * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
212600a30448SNate Lawson  */
212700a30448SNate Lawson ACPI_STATUS
212800a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state)
212900a30448SNate Lawson {
213000a30448SNate Lawson     static int once;
213100a30448SNate Lawson 
213200a30448SNate Lawson     if (!once) {
213300a30448SNate Lawson 	printf(
213400a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n");
213500a30448SNate Lawson 	once = 1;
213600a30448SNate Lawson     }
213700a30448SNate Lawson     return (acpi_EnterSleepState(sc, state));
213800a30448SNate Lawson }
213900a30448SNate Lawson 
214000a30448SNate Lawson static void
214100a30448SNate Lawson acpi_sleep_force(void *arg)
214200a30448SNate Lawson {
214300a30448SNate Lawson     struct acpi_softc *sc;
214400a30448SNate Lawson 
214500a30448SNate Lawson     printf("acpi: suspend request timed out, forcing sleep now\n");
214600a30448SNate Lawson     sc = arg;
214700a30448SNate Lawson     if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
214800a30448SNate Lawson 	printf("acpi: force sleep state S%d failed\n", sc->acpi_next_sstate);
214900a30448SNate Lawson }
215000a30448SNate Lawson 
215100a30448SNate Lawson /*
215200a30448SNate Lawson  * Request that the system enter the given suspend state.  All /dev/apm
215300a30448SNate Lawson  * devices and devd(8) will be notified.  Userland then has a chance to
215400a30448SNate Lawson  * save state and acknowledge the request.  The system sleeps once all
215500a30448SNate Lawson  * acks are in.
215600a30448SNate Lawson  */
215700a30448SNate Lawson int
215800a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state)
215900a30448SNate Lawson {
216000a30448SNate Lawson     struct apm_clone_data *clone;
216100a30448SNate Lawson 
216200a30448SNate Lawson     if (state < ACPI_STATE_S1 || state > ACPI_STATE_S5)
216300a30448SNate Lawson 	return (EINVAL);
216400a30448SNate Lawson 
216500a30448SNate Lawson     /* S5 (soft-off) should be entered directly with no waiting. */
216600a30448SNate Lawson     if (state == ACPI_STATE_S5) {
216700a30448SNate Lawson 	if (ACPI_SUCCESS(acpi_EnterSleepState(sc, state)))
216800a30448SNate Lawson 	    return (0);
216900a30448SNate Lawson 	else
217000a30448SNate Lawson 	    return (ENXIO);
217100a30448SNate Lawson     }
217200a30448SNate Lawson 
2173b6648efdSNate Lawson #if !defined(__i386__)
2174b6648efdSNate Lawson     /* This platform does not support acpi suspend/resume. */
2175b6648efdSNate Lawson     return (EOPNOTSUPP);
2176b6648efdSNate Lawson #endif
2177b6648efdSNate Lawson 
217800a30448SNate Lawson     /* If a suspend request is already in progress, just return. */
217900a30448SNate Lawson     ACPI_LOCK(acpi);
218000a30448SNate Lawson     if (sc->acpi_next_sstate != 0) {
218100a30448SNate Lawson     	ACPI_UNLOCK(acpi);
218200a30448SNate Lawson 	return (0);
218300a30448SNate Lawson     }
218400a30448SNate Lawson 
218500a30448SNate Lawson     /* Record the pending state and notify all apm devices. */
218600a30448SNate Lawson     sc->acpi_next_sstate = state;
218700a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
218800a30448SNate Lawson 	clone->notify_status = APM_EV_NONE;
218900a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_DEVD) == 0) {
219000a30448SNate Lawson 	    selwakeuppri(&clone->sel_read, PZERO);
219100a30448SNate Lawson 	    KNOTE_UNLOCKED(&clone->sel_read.si_note, 0);
219200a30448SNate Lawson 	}
219300a30448SNate Lawson     }
219400a30448SNate Lawson 
21950c26519eSMitsuru IWASAKI     /* If devd(8) is not running, immediately enter the sleep state. */
21960c26519eSMitsuru IWASAKI     if (devctl_process_running() == FALSE) {
21970c26519eSMitsuru IWASAKI 	ACPI_UNLOCK(acpi);
21987572a9c7SMitsuru IWASAKI 	if (ACPI_SUCCESS(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) {
21997572a9c7SMitsuru IWASAKI 	    return (0);
22007572a9c7SMitsuru IWASAKI 	} else {
22017572a9c7SMitsuru IWASAKI 	    return (ENXIO);
22027572a9c7SMitsuru IWASAKI 	}
22030c26519eSMitsuru IWASAKI     }
22040c26519eSMitsuru IWASAKI 
220500a30448SNate Lawson     /* Now notify devd(8) also. */
220600a30448SNate Lawson     acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
220700a30448SNate Lawson 
220800a30448SNate Lawson     /*
220900a30448SNate Lawson      * Set a timeout to fire if userland doesn't ack the suspend request
221000a30448SNate Lawson      * in time.  This way we still eventually go to sleep if we were
221100a30448SNate Lawson      * overheating or running low on battery, even if userland is hung.
221200a30448SNate Lawson      * We cancel this timeout once all userland acks are in or the
221300a30448SNate Lawson      * suspend request is aborted.
221400a30448SNate Lawson      */
221500a30448SNate Lawson     callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
221600a30448SNate Lawson     ACPI_UNLOCK(acpi);
221700a30448SNate Lawson     return (0);
221800a30448SNate Lawson }
221900a30448SNate Lawson 
222000a30448SNate Lawson /*
222100a30448SNate Lawson  * Acknowledge (or reject) a pending sleep state.  The caller has
222200a30448SNate Lawson  * prepared for suspend and is now ready for it to proceed.  If the
222300a30448SNate Lawson  * error argument is non-zero, it indicates suspend should be cancelled
222400a30448SNate Lawson  * and gives an errno value describing why.  Once all votes are in,
222500a30448SNate Lawson  * we suspend the system.
222600a30448SNate Lawson  */
222700a30448SNate Lawson int
222800a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error)
222900a30448SNate Lawson {
223000a30448SNate Lawson     struct acpi_softc *sc;
223100a30448SNate Lawson     int ret, sleeping;
223200a30448SNate Lawson 
2233b6648efdSNate Lawson #if !defined(__i386__)
2234b6648efdSNate Lawson     /* This platform does not support acpi suspend/resume. */
2235b6648efdSNate Lawson     return (EOPNOTSUPP);
2236b6648efdSNate Lawson #endif
2237b6648efdSNate Lawson 
223800a30448SNate Lawson     /* If no pending sleep state, return an error. */
223900a30448SNate Lawson     ACPI_LOCK(acpi);
224000a30448SNate Lawson     sc = clone->acpi_sc;
224100a30448SNate Lawson     if (sc->acpi_next_sstate == 0) {
224200a30448SNate Lawson     	ACPI_UNLOCK(acpi);
224300a30448SNate Lawson 	return (ENXIO);
224400a30448SNate Lawson     }
224500a30448SNate Lawson 
224600a30448SNate Lawson     /* Caller wants to abort suspend process. */
224700a30448SNate Lawson     if (error) {
224800a30448SNate Lawson 	sc->acpi_next_sstate = 0;
224900a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
225000a30448SNate Lawson 	printf("acpi: listener on %s cancelled the pending suspend\n",
225100a30448SNate Lawson 	    devtoname(clone->cdev));
225200a30448SNate Lawson     	ACPI_UNLOCK(acpi);
225300a30448SNate Lawson 	return (0);
225400a30448SNate Lawson     }
225500a30448SNate Lawson 
225600a30448SNate Lawson     /*
225700a30448SNate Lawson      * Mark this device as acking the suspend request.  Then, walk through
225800a30448SNate Lawson      * all devices, seeing if they agree yet.  We only count devices that
225900a30448SNate Lawson      * are writable since read-only devices couldn't ack the request.
226000a30448SNate Lawson      */
226100a30448SNate Lawson     clone->notify_status = APM_EV_ACKED;
226200a30448SNate Lawson     sleeping = TRUE;
226300a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
226400a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
226500a30448SNate Lawson 	    clone->notify_status != APM_EV_ACKED) {
226600a30448SNate Lawson 	    sleeping = FALSE;
226700a30448SNate Lawson 	    break;
226800a30448SNate Lawson 	}
226900a30448SNate Lawson     }
227000a30448SNate Lawson 
227100a30448SNate Lawson     /* If all devices have voted "yes", we will suspend now. */
227200a30448SNate Lawson     if (sleeping)
227300a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
227400a30448SNate Lawson     ACPI_UNLOCK(acpi);
227500a30448SNate Lawson     ret = 0;
227600a30448SNate Lawson     if (sleeping) {
227700a30448SNate Lawson 	if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
227800a30448SNate Lawson 		ret = ENODEV;
227900a30448SNate Lawson     }
228000a30448SNate Lawson 
228100a30448SNate Lawson     return (ret);
228200a30448SNate Lawson }
228300a30448SNate Lawson 
2284ece50487SMitsuru IWASAKI static void
2285ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2286ece50487SMitsuru IWASAKI {
2287bee4aa4aSNate Lawson 
2288ece50487SMitsuru IWASAKI     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
2289ece50487SMitsuru IWASAKI }
2290c30382dfSMitsuru IWASAKI 
229115e2f34fSNate Lawson enum acpi_sleep_state {
229215e2f34fSNate Lawson     ACPI_SS_NONE,
229315e2f34fSNate Lawson     ACPI_SS_GPE_SET,
229415e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
229515e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
229615e2f34fSNate Lawson     ACPI_SS_SLEPT,
229715e2f34fSNate Lawson };
229815e2f34fSNate Lawson 
229915e32d5dSMike Smith /*
230000a30448SNate Lawson  * Enter the desired system sleep state.
230115e32d5dSMike Smith  *
2302be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
230315e32d5dSMike Smith  */
230400a30448SNate Lawson static ACPI_STATUS
230500a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state)
230615e32d5dSMike Smith {
230715e2f34fSNate Lawson     ACPI_STATUS	status;
230856d8cb57SMitsuru IWASAKI     UINT8	TypeA;
230956d8cb57SMitsuru IWASAKI     UINT8	TypeB;
231015e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
231115e32d5dSMike Smith 
2312b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
23130ae55423SMike Smith 
231400a30448SNate Lawson     /* Re-entry once we're suspending is not allowed. */
231515e2f34fSNate Lawson     status = AE_OK;
231615e2f34fSNate Lawson     ACPI_LOCK(acpi);
231715e2f34fSNate Lawson     if (sc->acpi_sleep_disabled) {
231815e2f34fSNate Lawson 	ACPI_UNLOCK(acpi);
231915e2f34fSNate Lawson 	printf("acpi: suspend request ignored (not ready yet)\n");
232000a30448SNate Lawson 	return (AE_ERROR);
232115e2f34fSNate Lawson     }
232215e2f34fSNate Lawson     sc->acpi_sleep_disabled = 1;
232315e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
23246397624dSMitsuru IWASAKI 
23255d3d03f1SNate Lawson     /*
23265d3d03f1SNate Lawson      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
23275d3d03f1SNate Lawson      * drivers need this.
23285d3d03f1SNate Lawson      */
23295d3d03f1SNate Lawson     mtx_lock(&Giant);
233015e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
233115e32d5dSMike Smith     switch (state) {
233215e32d5dSMike Smith     case ACPI_STATE_S1:
23336161544cSTakanori Watanabe     case ACPI_STATE_S2:
23346161544cSTakanori Watanabe     case ACPI_STATE_S3:
23356161544cSTakanori Watanabe     case ACPI_STATE_S4:
2336adad4744SNate Lawson 	status = AcpiGetSleepTypeData(state, &TypeA, &TypeB);
233798175614SNate Lawson 	if (status == AE_NOT_FOUND) {
233898175614SNate Lawson 	    device_printf(sc->acpi_dev,
233998175614SNate Lawson 			  "Sleep state S%d not supported by BIOS\n", state);
234098175614SNate Lawson 	    break;
234198175614SNate Lawson 	} else if (ACPI_FAILURE(status)) {
2342be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
2343be2b1797SNate Lawson 			  AcpiFormatException(status));
234456d8cb57SMitsuru IWASAKI 	    break;
234556d8cb57SMitsuru IWASAKI 	}
234656d8cb57SMitsuru IWASAKI 
2347a1fccb47SMitsuru IWASAKI 	sc->acpi_sstate = state;
2348a1fccb47SMitsuru IWASAKI 
2349d4b9ff91SNate Lawson 	/* Enable any GPEs as appropriate and requested by the user. */
2350d4b9ff91SNate Lawson 	acpi_wake_prep_walk(state);
235115e2f34fSNate Lawson 	slp_state = ACPI_SS_GPE_SET;
2352e8b4d56eSNate Lawson 
235315e32d5dSMike Smith 	/*
2354c7f88fc3SNate Lawson 	 * Inform all devices that we are going to sleep.  If at least one
2355c7f88fc3SNate Lawson 	 * device fails, DEVICE_SUSPEND() automatically resumes the tree.
235615e32d5dSMike Smith 	 *
2357c7f88fc3SNate Lawson 	 * XXX Note that a better two-pass approach with a 'veto' pass
2358c7f88fc3SNate Lawson 	 * followed by a "real thing" pass would be better, but the current
2359c7f88fc3SNate Lawson 	 * bus interface does not provide for this.
236015e32d5dSMike Smith 	 */
236115e2f34fSNate Lawson 	if (DEVICE_SUSPEND(root_bus) != 0) {
236215e2f34fSNate Lawson 	    device_printf(sc->acpi_dev, "device_suspend failed\n");
236315e2f34fSNate Lawson 	    break;
236415e2f34fSNate Lawson 	}
236515e2f34fSNate Lawson 	slp_state = ACPI_SS_DEV_SUSPEND;
2366b9c780d6SMitsuru IWASAKI 
236793bfd059SNate Lawson 	/* If testing device suspend only, back out of everything here. */
236893bfd059SNate Lawson 	if (acpi_susp_bounce)
236993bfd059SNate Lawson 	    break;
237093bfd059SNate Lawson 
2371be2b1797SNate Lawson 	status = AcpiEnterSleepStatePrep(state);
2372be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2373b9c780d6SMitsuru IWASAKI 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2374b9c780d6SMitsuru IWASAKI 			  AcpiFormatException(status));
2375b9c780d6SMitsuru IWASAKI 	    break;
2376b9c780d6SMitsuru IWASAKI 	}
237715e2f34fSNate Lawson 	slp_state = ACPI_SS_SLP_PREP;
2378b9c780d6SMitsuru IWASAKI 
2379be2b1797SNate Lawson 	if (sc->acpi_sleep_delay > 0)
2380ff01efb5SMitsuru IWASAKI 	    DELAY(sc->acpi_sleep_delay * 1000000);
2381ff01efb5SMitsuru IWASAKI 
23826161544cSTakanori Watanabe 	if (state != ACPI_STATE_S1) {
23836161544cSTakanori Watanabe 	    acpi_sleep_machdep(sc, state);
23846161544cSTakanori Watanabe 
23856161544cSTakanori Watanabe 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
2386be2b1797SNate Lawson 	    if (state == ACPI_STATE_S4)
2387964679ceSMitsuru IWASAKI 		AcpiEnable();
23886161544cSTakanori Watanabe 	} else {
2389c7f88fc3SNate Lawson 	    ACPI_DISABLE_IRQS();
2390adad4744SNate Lawson 	    status = AcpiEnterSleepState(state);
2391be2b1797SNate Lawson 	    if (ACPI_FAILURE(status)) {
2392be2b1797SNate Lawson 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2393be2b1797SNate Lawson 			      AcpiFormatException(status));
2394c30382dfSMitsuru IWASAKI 		break;
239515e32d5dSMike Smith 	    }
23966161544cSTakanori Watanabe 	}
239715e2f34fSNate Lawson 	slp_state = ACPI_SS_SLEPT;
239815e32d5dSMike Smith 	break;
239915e32d5dSMike Smith     case ACPI_STATE_S5:
240015e32d5dSMike Smith 	/*
240115e32d5dSMike Smith 	 * Shut down cleanly and power off.  This will call us back through the
240215e32d5dSMike Smith 	 * shutdown handlers.
240315e32d5dSMike Smith 	 */
240415e32d5dSMike Smith 	shutdown_nice(RB_POWEROFF);
240515e32d5dSMike Smith 	break;
240698175614SNate Lawson     case ACPI_STATE_S0:
240715e32d5dSMike Smith     default:
240815e32d5dSMike Smith 	status = AE_BAD_PARAMETER;
240915e32d5dSMike Smith 	break;
241015e32d5dSMike Smith     }
2411ece50487SMitsuru IWASAKI 
241215e2f34fSNate Lawson     /*
241315e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
241415e2f34fSNate Lawson      * process.  This handles both the error and success cases.
241515e2f34fSNate Lawson      */
241600a30448SNate Lawson     sc->acpi_next_sstate = 0;
241715e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
241815e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
241915e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
242015e2f34fSNate Lawson     }
242115e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLP_PREP)
242215e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
2423071339e2SNate Lawson     if (slp_state >= ACPI_SS_DEV_SUSPEND)
2424071339e2SNate Lawson 	DEVICE_RESUME(root_bus);
242515e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLEPT)
242615e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
242715e2f34fSNate Lawson 
242815e2f34fSNate Lawson     /* Allow another sleep request after a while. */
242915e2f34fSNate Lawson     if (state != ACPI_STATE_S5)
243000a30448SNate Lawson 	timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
243100a30448SNate Lawson 
243200a30448SNate Lawson     /* Run /etc/rc.resume after we are back. */
243300a30448SNate Lawson     acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
2434ece50487SMitsuru IWASAKI 
24355d3d03f1SNate Lawson     mtx_unlock(&Giant);
24360ae55423SMike Smith     return_ACPI_STATUS (status);
243715e32d5dSMike Smith }
243815e32d5dSMike Smith 
2439e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
2440e8b4d56eSNate Lawson int
2441e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
2442e8b4d56eSNate Lawson {
2443e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2444e8b4d56eSNate Lawson 
2445e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
2446e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2447e8b4d56eSNate Lawson 	return (ENXIO);
2448e8b4d56eSNate Lawson 
2449e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
2450e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
2451e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
2452e8b4d56eSNate Lawson 	return (ENXIO);
2453e8b4d56eSNate Lawson     }
2454e8b4d56eSNate Lawson 
2455e8b4d56eSNate Lawson     return (0);
2456e8b4d56eSNate Lawson }
2457e8b4d56eSNate Lawson 
2458e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
2459e8b4d56eSNate Lawson int
2460e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
2461e8b4d56eSNate Lawson {
2462e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2463e8b4d56eSNate Lawson     ACPI_STATUS status;
2464e8b4d56eSNate Lawson     int flags;
2465e8b4d56eSNate Lawson 
2466d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
24672be4e471SJung-uk Kim     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2468e8b4d56eSNate Lawson 	return (ENXIO);
2469e8b4d56eSNate Lawson 
2470d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
2471e8b4d56eSNate Lawson     if (enable) {
2472e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2473e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2474e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
2475e8b4d56eSNate Lawson 	    return (ENXIO);
2476e8b4d56eSNate Lawson 	}
2477d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2478e8b4d56eSNate Lawson     } else {
2479e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2480e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2481e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
2482e8b4d56eSNate Lawson 	    return (ENXIO);
2483e8b4d56eSNate Lawson 	}
2484d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2485e8b4d56eSNate Lawson     }
2486e8b4d56eSNate Lawson 
2487e8b4d56eSNate Lawson     return (0);
2488e8b4d56eSNate Lawson }
2489e8b4d56eSNate Lawson 
2490d4b9ff91SNate Lawson static int
2491d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2492e8b4d56eSNate Lawson {
2493e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2494d4b9ff91SNate Lawson     device_t dev;
2495e8b4d56eSNate Lawson 
2496d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
2497e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2498e8b4d56eSNate Lawson 	return (ENXIO);
2499d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2500e8b4d56eSNate Lawson 
2501e8b4d56eSNate Lawson     /*
2502d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
2503d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
2504d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
2505d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
2506d4b9ff91SNate Lawson      * and set _PSW.
2507e8b4d56eSNate Lawson      */
2508d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2509cc85c78cSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2510e8b4d56eSNate Lawson 	if (bootverbose)
2511d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2512d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2513d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2514d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
2515d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
2516d4b9ff91SNate Lawson 	if (bootverbose)
2517d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2518d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2519d4b9ff91SNate Lawson     }
2520cc85c78cSNate Lawson 
2521cc85c78cSNate Lawson     return (0);
2522e8b4d56eSNate Lawson }
2523e8b4d56eSNate Lawson 
2524d4b9ff91SNate Lawson static int
2525d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2526cc85c78cSNate Lawson {
2527cc85c78cSNate Lawson     struct acpi_prw_data prw;
2528d4b9ff91SNate Lawson     device_t dev;
2529cc85c78cSNate Lawson 
2530cc85c78cSNate Lawson     /*
2531d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
2532d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
2533cc85c78cSNate Lawson      */
2534d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2535d4b9ff91SNate Lawson 	return (ENXIO);
2536d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2537d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2538d4b9ff91SNate Lawson 	return (0);
2539cc85c78cSNate Lawson 
2540d4b9ff91SNate Lawson     /*
2541d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
2542d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
2543d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
2544d4b9ff91SNate Lawson      */
2545d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2546cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2547d4b9ff91SNate Lawson 	if (bootverbose)
2548d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2549d4b9ff91SNate Lawson     } else {
2550d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
2551d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
2552d4b9ff91SNate Lawson 	if (bootverbose)
2553d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
2554d4b9ff91SNate Lawson 		acpi_name(handle));
2555d4b9ff91SNate Lawson     }
2556d4b9ff91SNate Lawson 
2557e8b4d56eSNate Lawson     return (0);
2558e8b4d56eSNate Lawson }
2559e8b4d56eSNate Lawson 
2560e8b4d56eSNate Lawson static ACPI_STATUS
2561d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2562e8b4d56eSNate Lawson {
2563d4b9ff91SNate Lawson     int sstate;
2564e8b4d56eSNate Lawson 
2565d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
2566d4b9ff91SNate Lawson     sstate = *(int *)context;
2567d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
2568d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
2569d4b9ff91SNate Lawson     else
2570d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
2571e8b4d56eSNate Lawson     return (AE_OK);
2572e8b4d56eSNate Lawson }
2573e8b4d56eSNate Lawson 
2574d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2575e8b4d56eSNate Lawson static int
2576d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
2577e8b4d56eSNate Lawson {
2578e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
2579e8b4d56eSNate Lawson 
2580e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2581d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
2582d4b9ff91SNate Lawson 	    acpi_wake_prep, &sstate, NULL);
2583e8b4d56eSNate Lawson     return (0);
2584e8b4d56eSNate Lawson }
2585e8b4d56eSNate Lawson 
258688a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
258788a79fc0SNate Lawson static int
258888a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
258988a79fc0SNate Lawson {
259088a79fc0SNate Lawson     int error, i, numdevs;
259188a79fc0SNate Lawson     device_t *devlist;
259288a79fc0SNate Lawson     device_t child;
2593d4b9ff91SNate Lawson     ACPI_STATUS status;
259488a79fc0SNate Lawson 
259588a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
25961c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
25971c9ec538SNate Lawson 	if (numdevs == 0)
25981c9ec538SNate Lawson 	    free(devlist, M_TEMP);
259988a79fc0SNate Lawson 	return (error);
26001c9ec538SNate Lawson     }
260188a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
260288a79fc0SNate Lawson 	child = devlist[i];
2603d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
260488a79fc0SNate Lawson 	if (!device_is_attached(child))
260588a79fc0SNate Lawson 	    continue;
2606d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2607d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
260888a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
260988a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
261088a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
261188a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
261288a79fc0SNate Lawson 	}
261388a79fc0SNate Lawson     }
261488a79fc0SNate Lawson     free(devlist, M_TEMP);
261588a79fc0SNate Lawson 
261688a79fc0SNate Lawson     return (0);
261788a79fc0SNate Lawson }
261888a79fc0SNate Lawson 
261988a79fc0SNate Lawson /* Enable or disable wake from userland. */
262088a79fc0SNate Lawson static int
262188a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
262288a79fc0SNate Lawson {
262388a79fc0SNate Lawson     int enable, error;
262488a79fc0SNate Lawson     device_t dev;
262588a79fc0SNate Lawson 
262688a79fc0SNate Lawson     dev = (device_t)arg1;
2627d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
262888a79fc0SNate Lawson 
262988a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
263088a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
263188a79fc0SNate Lawson 	return (error);
263288a79fc0SNate Lawson     if (enable != 0 && enable != 1)
263388a79fc0SNate Lawson 	return (EINVAL);
263488a79fc0SNate Lawson 
263588a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
263688a79fc0SNate Lawson }
263788a79fc0SNate Lawson 
2638e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
2639d4b9ff91SNate Lawson int
2640e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2641e8b4d56eSNate Lawson {
2642e8b4d56eSNate Lawson     ACPI_STATUS			status;
2643e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2644e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2645d4b9ff91SNate Lawson     int				error, i, power_count;
2646e8b4d56eSNate Lawson 
2647e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2648e8b4d56eSNate Lawson 	return (EINVAL);
2649e8b4d56eSNate Lawson 
2650e8b4d56eSNate Lawson     /*
2651e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2652e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2653e8b4d56eSNate Lawson      */
2654e8b4d56eSNate Lawson     error = EINVAL;
2655e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2656e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2657e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2658e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2659e8b4d56eSNate Lawson 	return (ENOENT);
2660e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2661e8b4d56eSNate Lawson     if (res == NULL)
2662e8b4d56eSNate Lawson 	return (ENOENT);
2663e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2664e8b4d56eSNate Lawson 	goto out;
2665e8b4d56eSNate Lawson 
2666e8b4d56eSNate Lawson     /*
2667e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2668e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2669e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2670e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2671e8b4d56eSNate Lawson      */
2672e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2673e8b4d56eSNate Lawson 	goto out;
2674e8b4d56eSNate Lawson 
2675e8b4d56eSNate Lawson     /*
2676e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2677e8b4d56eSNate Lawson      */
2678e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2679e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2680e8b4d56eSNate Lawson 	/*
2681e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2682e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2683e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2684e8b4d56eSNate Lawson 	 * enabled for the wake event.
2685e8b4d56eSNate Lawson 	 */
2686e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2687e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2688e8b4d56eSNate Lawson 	error = 0;
2689e8b4d56eSNate Lawson 	break;
2690e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2691e8b4d56eSNate Lawson 	/*
2692e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2693e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2694e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2695e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2696e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2697e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2698e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2699e8b4d56eSNate Lawson 	 * the wake event.
2700e8b4d56eSNate Lawson 	 *
2701e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2702e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2703e8b4d56eSNate Lawson 	 */
2704e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2705e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2706e8b4d56eSNate Lawson 	    goto out;
2707e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2708e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2709e8b4d56eSNate Lawson 	    goto out;
2710e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2711e8b4d56eSNate Lawson 	    goto out;
2712e8b4d56eSNate Lawson 	error = 0;
2713e8b4d56eSNate Lawson 	break;
2714e8b4d56eSNate Lawson     default:
2715e8b4d56eSNate Lawson 	goto out;
2716e8b4d56eSNate Lawson     }
2717e8b4d56eSNate Lawson 
2718d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
2719d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
2720d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
2721d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
2722d4b9ff91SNate Lawson 	power_count = 0;
2723d4b9ff91SNate Lawson     }
2724d4b9ff91SNate Lawson     prw->power_res_count = power_count;
2725d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
2726d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
2727e8b4d56eSNate Lawson 
2728e8b4d56eSNate Lawson out:
2729e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2730e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2731e8b4d56eSNate Lawson     return (error);
2732e8b4d56eSNate Lawson }
2733e8b4d56eSNate Lawson 
273415e32d5dSMike Smith /*
273515e32d5dSMike Smith  * ACPI Event Handlers
273615e32d5dSMike Smith  */
273715e32d5dSMike Smith 
273815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
273915e32d5dSMike Smith 
274015e32d5dSMike Smith static void
274115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
274215e32d5dSMike Smith {
274300a30448SNate Lawson     int ret;
2744bee4aa4aSNate Lawson 
2745b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
274615e32d5dSMike Smith 
2747813d6dcaSNate Lawson     /* Check if button action is disabled. */
2748813d6dcaSNate Lawson     if (state == ACPI_S_STATES_MAX + 1)
2749813d6dcaSNate Lawson 	return;
2750813d6dcaSNate Lawson 
275100a30448SNate Lawson     /* Request that the system prepare to enter the given suspend state. */
275200a30448SNate Lawson     ret = acpi_ReqSleepState((struct acpi_softc *)arg, state);
275300a30448SNate Lawson     if (ret != 0)
275400a30448SNate Lawson 	printf("acpi: request to enter state S%d failed (err %d)\n",
275500a30448SNate Lawson 	    state, ret);
2756bee4aa4aSNate Lawson 
27570ae55423SMike Smith     return_VOID;
275815e32d5dSMike Smith }
275915e32d5dSMike Smith 
276015e32d5dSMike Smith static void
276115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
276215e32d5dSMike Smith {
2763bee4aa4aSNate Lawson 
2764b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
27650ae55423SMike Smith 
2766bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
2767cb9b0d80SMike Smith 
27680ae55423SMike Smith     return_VOID;
276915e32d5dSMike Smith }
277015e32d5dSMike Smith 
277115e32d5dSMike Smith /*
277215e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
277315e32d5dSMike Smith  */
277415e32d5dSMike Smith UINT32
277533febf93SNate Lawson acpi_event_power_button_sleep(void *context)
277615e32d5dSMike Smith {
277715e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
277815e32d5dSMike Smith 
2779b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
27800ae55423SMike Smith 
278115e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
27820ae55423SMike Smith 
2783b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
278415e32d5dSMike Smith }
278515e32d5dSMike Smith 
278615e32d5dSMike Smith UINT32
278733febf93SNate Lawson acpi_event_power_button_wake(void *context)
278815e32d5dSMike Smith {
278915e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
279015e32d5dSMike Smith 
2791b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
27920ae55423SMike Smith 
279315e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
27940ae55423SMike Smith 
2795b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
279615e32d5dSMike Smith }
279715e32d5dSMike Smith 
279815e32d5dSMike Smith UINT32
279933febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
280015e32d5dSMike Smith {
280115e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
280215e32d5dSMike Smith 
2803b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
28040ae55423SMike Smith 
280515e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
28060ae55423SMike Smith 
2807b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
280815e32d5dSMike Smith }
280915e32d5dSMike Smith 
281015e32d5dSMike Smith UINT32
281133febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
281215e32d5dSMike Smith {
281315e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
281415e32d5dSMike Smith 
2815b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
28160ae55423SMike Smith 
281715e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
28180ae55423SMike Smith 
2819b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
282015e32d5dSMike Smith }
282115e32d5dSMike Smith 
282215e32d5dSMike Smith /*
2823bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
2824bee4aa4aSNate Lawson  * use this for single-threaded callers.
282515e32d5dSMike Smith  */
282615e32d5dSMike Smith char *
282715e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
282815e32d5dSMike Smith {
2829bee4aa4aSNate Lawson     ACPI_BUFFER buf;
2830bee4aa4aSNate Lawson     static char data[256];
283115e32d5dSMike Smith 
2832bee4aa4aSNate Lawson     buf.Length = sizeof(data);
2833bee4aa4aSNate Lawson     buf.Pointer = data;
2834cb9b0d80SMike Smith 
28350a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
2836bee4aa4aSNate Lawson 	return (data);
2837bee4aa4aSNate Lawson     return ("(unknown)");
283815e32d5dSMike Smith }
283915e32d5dSMike Smith 
284015e32d5dSMike Smith /*
284115e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
284215e32d5dSMike Smith  * parts of the namespace.
284315e32d5dSMike Smith  */
284415e32d5dSMike Smith int
284515e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
284615e32d5dSMike Smith {
28473c600cfbSJohn Baldwin     char	*cp, *env, *np;
284815e32d5dSMike Smith     int		len;
284915e32d5dSMike Smith 
285015e32d5dSMike Smith     np = acpi_name(handle);
285115e32d5dSMike Smith     if (*np == '\\')
285215e32d5dSMike Smith 	np++;
28533c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
285415e32d5dSMike Smith 	return (0);
285515e32d5dSMike Smith 
2856be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
28573c600cfbSJohn Baldwin     cp = env;
285815e32d5dSMike Smith     for (;;) {
2859bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
286015e32d5dSMike Smith 	    cp++;
286115e32d5dSMike Smith 	if (*cp == 0)
286215e32d5dSMike Smith 	    break;
286315e32d5dSMike Smith 	len = 0;
2864bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
286515e32d5dSMike Smith 	    len++;
2866d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
28673c600cfbSJohn Baldwin 	    freeenv(env);
28680ae55423SMike Smith 	    return(1);
2869d786139cSMaxime Henrion 	}
28700ae55423SMike Smith 	cp += len;
28710ae55423SMike Smith     }
28723c600cfbSJohn Baldwin     freeenv(env);
2873be2b1797SNate Lawson 
28740ae55423SMike Smith     return (0);
28750ae55423SMike Smith }
28760ae55423SMike Smith 
28770ae55423SMike Smith /*
28780ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
28790ae55423SMike Smith  */
28800ae55423SMike Smith int
28810ae55423SMike Smith acpi_disabled(char *subsys)
28820ae55423SMike Smith {
288378689b15SMaxime Henrion     char	*cp, *env;
28840ae55423SMike Smith     int		len;
28850ae55423SMike Smith 
28863184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
28870ae55423SMike Smith 	return (0);
28883184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
288978689b15SMaxime Henrion 	freeenv(env);
28900ae55423SMike Smith 	return (1);
2891d786139cSMaxime Henrion     }
28920ae55423SMike Smith 
28933184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
289478689b15SMaxime Henrion     cp = env;
28950ae55423SMike Smith     for (;;) {
28963184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
28970ae55423SMike Smith 	    cp++;
28983184cf5aSNate Lawson 	if (*cp == '\0')
28990ae55423SMike Smith 	    break;
29000ae55423SMike Smith 	len = 0;
29013184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
29020ae55423SMike Smith 	    len++;
29033184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
290478689b15SMaxime Henrion 	    freeenv(env);
290515e32d5dSMike Smith 	    return (1);
2906d786139cSMaxime Henrion 	}
290715e32d5dSMike Smith 	cp += len;
290815e32d5dSMike Smith     }
290978689b15SMaxime Henrion     freeenv(env);
2910be2b1797SNate Lawson 
291115e32d5dSMike Smith     return (0);
291215e32d5dSMike Smith }
291315e32d5dSMike Smith 
291415e32d5dSMike Smith /*
291515e32d5dSMike Smith  * Control interface.
291615e32d5dSMike Smith  *
29170ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
2918be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
2919be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
292015e32d5dSMike Smith  */
29210ae55423SMike Smith struct acpi_ioctl_hook
29220ae55423SMike Smith {
29230ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
29240ae55423SMike Smith     u_long			 cmd;
2925be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
29260ae55423SMike Smith     void			 *arg;
29270ae55423SMike Smith };
29280ae55423SMike Smith 
29290ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
29300ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
29310ae55423SMike Smith 
29320ae55423SMike Smith int
2933be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
29340ae55423SMike Smith {
29350ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
29360ae55423SMike Smith 
29370ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
29380ae55423SMike Smith 	return (ENOMEM);
29390ae55423SMike Smith     hp->cmd = cmd;
29400ae55423SMike Smith     hp->fn = fn;
29410ae55423SMike Smith     hp->arg = arg;
294215e2f34fSNate Lawson 
294315e2f34fSNate Lawson     ACPI_LOCK(acpi);
29440ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
29450ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
29460ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
29470ae55423SMike Smith     }
29480ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
294915e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
295015e2f34fSNate Lawson 
29510ae55423SMike Smith     return (0);
29520ae55423SMike Smith }
29530ae55423SMike Smith 
29540ae55423SMike Smith void
2955be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
29560ae55423SMike Smith {
29570ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
29580ae55423SMike Smith 
295915e2f34fSNate Lawson     ACPI_LOCK(acpi);
29600ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
296115e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
29620ae55423SMike Smith 	    break;
29630ae55423SMike Smith 
29640ae55423SMike Smith     if (hp != NULL) {
29650ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
29660ae55423SMike Smith 	free(hp, M_ACPIDEV);
29670ae55423SMike Smith     }
296815e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
29690ae55423SMike Smith }
29700ae55423SMike Smith 
297115e32d5dSMike Smith static int
297289c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
297315e32d5dSMike Smith {
297415e32d5dSMike Smith     return (0);
297515e32d5dSMike Smith }
297615e32d5dSMike Smith 
297715e32d5dSMike Smith static int
297889c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
297915e32d5dSMike Smith {
298015e32d5dSMike Smith     return (0);
298115e32d5dSMike Smith }
298215e32d5dSMike Smith 
298315e32d5dSMike Smith static int
298489c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
298515e32d5dSMike Smith {
298615e32d5dSMike Smith     struct acpi_softc		*sc;
29870ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
2988bee4aa4aSNate Lawson     int				error, state;
298915e32d5dSMike Smith 
2990a2e35898SDavid E. O'Brien     error = 0;
2991a2e35898SDavid E. O'Brien     hp = NULL;
299215e32d5dSMike Smith     sc = dev->si_drv1;
299315e32d5dSMike Smith 
29940ae55423SMike Smith     /*
29950ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
29960ae55423SMike Smith      */
299715e2f34fSNate Lawson     ACPI_LOCK(acpi);
2998bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
29990ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3000bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
3001bee4aa4aSNate Lawson 		break;
30020ae55423SMike Smith 	}
300315e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
3004bee4aa4aSNate Lawson     if (hp)
3005bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
30060ae55423SMike Smith 
30070ae55423SMike Smith     /*
3008a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
3009a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
3010a89fcf28STakanori Watanabe      * Not changing system behavior.
3011a89fcf28STakanori Watanabe      */
3012be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
3013be2b1797SNate Lawson 	return (EPERM);
3014a89fcf28STakanori Watanabe 
3015be2b1797SNate Lawson     /* Core system ioctls. */
301615e32d5dSMike Smith     switch (cmd) {
301700a30448SNate Lawson     case ACPIIO_REQSLPSTATE:
301800a30448SNate Lawson 	state = *(int *)addr;
301900a30448SNate Lawson 	if (state != ACPI_STATE_S5)
302000a30448SNate Lawson 	    error = acpi_ReqSleepState(sc, state);
302100a30448SNate Lawson 	else {
302200a30448SNate Lawson 	    printf("power off via acpi ioctl not supported\n");
302300a30448SNate Lawson 	    error = ENXIO;
302400a30448SNate Lawson 	}
302500a30448SNate Lawson 	break;
302600a30448SNate Lawson     case ACPIIO_ACKSLPSTATE:
302700a30448SNate Lawson 	error = *(int *)addr;
302800a30448SNate Lawson 	error = acpi_AckSleepState(sc->acpi_clone, error);
302900a30448SNate Lawson 	break;
303000a30448SNate Lawson     case ACPIIO_SETSLPSTATE:	/* DEPRECATED */
3031bee4aa4aSNate Lawson 	error = EINVAL;
303215e32d5dSMike Smith 	state = *(int *)addr;
3033bee4aa4aSNate Lawson 	if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
3034bee4aa4aSNate Lawson 	    if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
3035bee4aa4aSNate Lawson 		error = 0;
303615e32d5dSMike Smith 	break;
303715e32d5dSMike Smith     default:
3038bee4aa4aSNate Lawson 	error = ENXIO;
303915e32d5dSMike Smith 	break;
304015e32d5dSMike Smith     }
3041917d44c8SMitsuru IWASAKI 
304215e32d5dSMike Smith     return (error);
304315e32d5dSMike Smith }
304415e32d5dSMike Smith 
30451d073b1dSJohn Baldwin static int
3046d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3047d75de536SMitsuru IWASAKI {
3048d75de536SMitsuru IWASAKI     int error;
3049bee4aa4aSNate Lawson     struct sbuf sb;
3050d75de536SMitsuru IWASAKI     UINT8 state, TypeA, TypeB;
3051d75de536SMitsuru IWASAKI 
3052bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3053bee4aa4aSNate Lawson     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
3054bee4aa4aSNate Lawson 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
3055bee4aa4aSNate Lawson 	    sbuf_printf(&sb, "S%d ", state);
3056bee4aa4aSNate Lawson     sbuf_trim(&sb);
3057bee4aa4aSNate Lawson     sbuf_finish(&sb);
3058bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3059bee4aa4aSNate Lawson     sbuf_delete(&sb);
3060d75de536SMitsuru IWASAKI     return (error);
3061d75de536SMitsuru IWASAKI }
3062d75de536SMitsuru IWASAKI 
3063d75de536SMitsuru IWASAKI static int
30641d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
30651d073b1dSJohn Baldwin {
30661d073b1dSJohn Baldwin     char sleep_state[10];
30671d073b1dSJohn Baldwin     int error;
30681d073b1dSJohn Baldwin     u_int new_state, old_state;
30691d073b1dSJohn Baldwin 
30701d073b1dSJohn Baldwin     old_state = *(u_int *)oidp->oid_arg1;
3071bee4aa4aSNate Lawson     if (old_state > ACPI_S_STATES_MAX + 1)
3072bee4aa4aSNate Lawson 	strlcpy(sleep_state, "unknown", sizeof(sleep_state));
3073bee4aa4aSNate Lawson     else
3074bee4aa4aSNate Lawson 	strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
30751d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
30761d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
3077be2b1797SNate Lawson 	new_state = ACPI_STATE_S0;
3078bee4aa4aSNate Lawson 	for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
3079bee4aa4aSNate Lawson 	    if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
30801d073b1dSJohn Baldwin 		break;
3081931a10c9SMitsuru IWASAKI 	if (new_state <= ACPI_S_STATES_MAX + 1) {
3082be2b1797SNate Lawson 	    if (new_state != old_state)
30831d073b1dSJohn Baldwin 		*(u_int *)oidp->oid_arg1 = new_state;
3084bee4aa4aSNate Lawson 	} else
30851d073b1dSJohn Baldwin 	    error = EINVAL;
30861d073b1dSJohn Baldwin     }
3087be2b1797SNate Lawson 
30881d073b1dSJohn Baldwin     return (error);
30891d073b1dSJohn Baldwin }
30901d073b1dSJohn Baldwin 
30919b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
30929b937d48SNate Lawson void
30939b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
30949b937d48SNate Lawson {
30959b937d48SNate Lawson     char		notify_buf[16];
30969b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
30979b937d48SNate Lawson     ACPI_STATUS		status;
30989b937d48SNate Lawson 
30999b937d48SNate Lawson     if (subsystem == NULL)
31009b937d48SNate Lawson 	return;
31019b937d48SNate Lawson 
31029b937d48SNate Lawson     handle_buf.Pointer = NULL;
31039b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
31049b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
31059b937d48SNate Lawson     if (ACPI_FAILURE(status))
31069b937d48SNate Lawson 	return;
31079b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
31089b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
31099b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
31109b937d48SNate Lawson }
31119b937d48SNate Lawson 
311215e32d5dSMike Smith #ifdef ACPI_DEBUG
31130ae55423SMike Smith /*
31140ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
31150ae55423SMike Smith  *
31160ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
31170ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
31180ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
31190ae55423SMike Smith  * prefixing the bit name with !.
31200ae55423SMike Smith  */
312115e32d5dSMike Smith struct debugtag
312215e32d5dSMike Smith {
312315e32d5dSMike Smith     char	*name;
312415e32d5dSMike Smith     UINT32	value;
312515e32d5dSMike Smith };
312615e32d5dSMike Smith 
312715e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
3128c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
3129c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
3130c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
3131c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
3132c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
3133c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
3134c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
3135c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
3136c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
3137d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
31384c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
3139d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
3140297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
31414c1cdee6SMike Smith 
3142c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
3143c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
31443184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
3145c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
31463184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
31473184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
31483184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
31494c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
3150cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
31513184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
3152b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
315315e32d5dSMike Smith     {NULL, 0}
315415e32d5dSMike Smith };
315515e32d5dSMike Smith 
315615e32d5dSMike Smith static struct debugtag dbg_level[] = {
31574c1cdee6SMike Smith     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
31589501b603SJohn Baldwin     {"ACPI_LV_WARN",		ACPI_LV_WARN},
31599501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
31604c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
31619501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
31624c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
3163d62ab2f4SMitsuru IWASAKI 
3164d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
3165297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
31664c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
31674c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
3168d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
31694c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
31704c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
31714c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
31724c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
31734c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
31744c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
31754c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
31764c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
31774c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
31784c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
3179d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
3180d62ab2f4SMitsuru IWASAKI 
3181d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
3182d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
3183d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
3184d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
3185d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
31864c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
3187d62ab2f4SMitsuru IWASAKI 
3188d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3189d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
3190d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
3191d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
3192d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
3193d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
3194d62ab2f4SMitsuru IWASAKI 
3195d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
319698479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
319798479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
319898479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
319998479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
320098479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
320115e32d5dSMike Smith     {NULL, 0}
320215e32d5dSMike Smith };
320315e32d5dSMike Smith 
320415e32d5dSMike Smith static void
320515e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
320615e32d5dSMike Smith {
320715e32d5dSMike Smith     char	*ep;
320815e32d5dSMike Smith     int		i, l;
32090ae55423SMike Smith     int		set;
321015e32d5dSMike Smith 
321115e32d5dSMike Smith     while (*cp) {
321215e32d5dSMike Smith 	if (isspace(*cp)) {
321315e32d5dSMike Smith 	    cp++;
321415e32d5dSMike Smith 	    continue;
321515e32d5dSMike Smith 	}
321615e32d5dSMike Smith 	ep = cp;
321715e32d5dSMike Smith 	while (*ep && !isspace(*ep))
321815e32d5dSMike Smith 	    ep++;
32190ae55423SMike Smith 	if (*cp == '!') {
32200ae55423SMike Smith 	    set = 0;
32210ae55423SMike Smith 	    cp++;
32220ae55423SMike Smith 	    if (cp == ep)
32230ae55423SMike Smith 		continue;
32240ae55423SMike Smith 	} else {
32250ae55423SMike Smith 	    set = 1;
32260ae55423SMike Smith 	}
322715e32d5dSMike Smith 	l = ep - cp;
322815e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
322915e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
3230be2b1797SNate Lawson 		if (set)
323115e32d5dSMike Smith 		    *flag |= tag[i].value;
3232be2b1797SNate Lawson 		else
32330ae55423SMike Smith 		    *flag &= ~tag[i].value;
323415e32d5dSMike Smith 	    }
323515e32d5dSMike Smith 	}
323615e32d5dSMike Smith 	cp = ep;
323715e32d5dSMike Smith     }
323815e32d5dSMike Smith }
323915e32d5dSMike Smith 
324015e32d5dSMike Smith static void
32412a4ac806SMike Smith acpi_set_debugging(void *junk)
324215e32d5dSMike Smith {
32437e639165SNate Lawson     char	*layer, *level;
324415e32d5dSMike Smith 
32451d7b121cSNate Lawson     if (cold) {
32460ae55423SMike Smith 	AcpiDbgLayer = 0;
32470ae55423SMike Smith 	AcpiDbgLevel = 0;
32481d7b121cSNate Lawson     }
32491d7b121cSNate Lawson 
32507e639165SNate Lawson     layer = getenv("debug.acpi.layer");
32517e639165SNate Lawson     level = getenv("debug.acpi.level");
32527e639165SNate Lawson     if (layer == NULL && level == NULL)
32537e639165SNate Lawson 	return;
32540ae55423SMike Smith 
32557e639165SNate Lawson     printf("ACPI set debug");
32567e639165SNate Lawson     if (layer != NULL) {
32577e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
32587e639165SNate Lawson 	    printf(" layer '%s'", layer);
32597e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
32607e639165SNate Lawson 	freeenv(layer);
32611d7b121cSNate Lawson     }
32627e639165SNate Lawson     if (level != NULL) {
32637e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
32647e639165SNate Lawson 	    printf(" level '%s'", level);
32657e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
32667e639165SNate Lawson 	freeenv(level);
32677e639165SNate Lawson     }
32687e639165SNate Lawson     printf("\n");
326915e32d5dSMike Smith }
3270bee4aa4aSNate Lawson 
3271be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3272be2b1797SNate Lawson 	NULL);
32731d7b121cSNate Lawson 
32741d7b121cSNate Lawson static int
32751d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
32761d7b121cSNate Lawson {
327754f6bca0SNate Lawson     int		 error, *dbg;
32781d7b121cSNate Lawson     struct	 debugtag *tag;
327954f6bca0SNate Lawson     struct	 sbuf sb;
32801d7b121cSNate Lawson 
328154f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
328254f6bca0SNate Lawson 	return (ENOMEM);
32831d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
32841d7b121cSNate Lawson 	tag = &dbg_layer[0];
32851d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
32861d7b121cSNate Lawson     } else {
32871d7b121cSNate Lawson 	tag = &dbg_level[0];
32881d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
32891d7b121cSNate Lawson     }
32901d7b121cSNate Lawson 
32911d7b121cSNate Lawson     /* Get old values if this is a get request. */
329215e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
32931d7b121cSNate Lawson     if (*dbg == 0) {
329454f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
32951d7b121cSNate Lawson     } else if (req->newptr == NULL) {
32961d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
329754f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
329854f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
32991d7b121cSNate Lawson 	}
33001d7b121cSNate Lawson     }
330154f6bca0SNate Lawson     sbuf_trim(&sb);
330254f6bca0SNate Lawson     sbuf_finish(&sb);
33031d7b121cSNate Lawson 
33047e639165SNate Lawson     /* Copy out the old values to the user. */
33057e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
330654f6bca0SNate Lawson     sbuf_delete(&sb);
33071d7b121cSNate Lawson 
33081d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
33091d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
33101d7b121cSNate Lawson 	*dbg = 0;
33111d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
33121d7b121cSNate Lawson 	acpi_set_debugging(NULL);
33131d7b121cSNate Lawson     }
331415e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
33151d7b121cSNate Lawson 
33161d7b121cSNate Lawson     return (error);
33171d7b121cSNate Lawson }
3318bee4aa4aSNate Lawson 
33191d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
33201d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
33211d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
33221d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3323bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3324f9390180SMitsuru IWASAKI 
3325f9390180SMitsuru IWASAKI static int
3326f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
3327f9390180SMitsuru IWASAKI {
3328f9390180SMitsuru IWASAKI 	int	state, acpi_state;
3329f9390180SMitsuru IWASAKI 	int	error;
3330f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
3331f9390180SMitsuru IWASAKI 	va_list	ap;
3332f9390180SMitsuru IWASAKI 
3333f9390180SMitsuru IWASAKI 	error = 0;
3334f9390180SMitsuru IWASAKI 	switch (cmd) {
3335f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
3336f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
3337f9390180SMitsuru IWASAKI 		if (sc == NULL) {
3338f9390180SMitsuru IWASAKI 			error = EINVAL;
3339f9390180SMitsuru IWASAKI 			goto out;
3340f9390180SMitsuru IWASAKI 		}
3341f9390180SMitsuru IWASAKI 
3342f9390180SMitsuru IWASAKI 		va_start(ap, arg);
3343f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
3344f9390180SMitsuru IWASAKI 		va_end(ap);
3345f9390180SMitsuru IWASAKI 
3346f9390180SMitsuru IWASAKI 		switch (state) {
3347f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
3348f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
3349f9390180SMitsuru IWASAKI 			break;
3350f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
3351f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
3352f9390180SMitsuru IWASAKI 			break;
3353f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
3354f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
3355f9390180SMitsuru IWASAKI 			break;
3356f9390180SMitsuru IWASAKI 		default:
3357f9390180SMitsuru IWASAKI 			error = EINVAL;
3358f9390180SMitsuru IWASAKI 			goto out;
3359f9390180SMitsuru IWASAKI 		}
3360f9390180SMitsuru IWASAKI 
336100a30448SNate Lawson 		if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
336200a30448SNate Lawson 			error = ENXIO;
3363f9390180SMitsuru IWASAKI 		break;
3364f9390180SMitsuru IWASAKI 	default:
3365f9390180SMitsuru IWASAKI 		error = EINVAL;
3366f9390180SMitsuru IWASAKI 		goto out;
3367f9390180SMitsuru IWASAKI 	}
3368f9390180SMitsuru IWASAKI 
3369f9390180SMitsuru IWASAKI out:
3370f9390180SMitsuru IWASAKI 	return (error);
3371f9390180SMitsuru IWASAKI }
3372f9390180SMitsuru IWASAKI 
3373f9390180SMitsuru IWASAKI static void
3374f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
3375f9390180SMitsuru IWASAKI {
3376be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
33776c407052SMitsuru IWASAKI 	return;
3378f9390180SMitsuru IWASAKI 
3379f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3380f9390180SMitsuru IWASAKI }
3381f9390180SMitsuru IWASAKI 
3382f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
3383