xref: /freebsd/sys/dev/acpica/acpi.c (revision 2a18c71d038b24f9025e314bd3ecf18a22cc5431)
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>
49c66d2b38SJung-uk Kim #ifdef SMP
50c66d2b38SJung-uk Kim #include <sys/sched.h>
51c66d2b38SJung-uk Kim #endif
52eeb3a05fSNate Lawson #include <sys/smp.h>
53c66d2b38SJung-uk Kim #include <sys/timetc.h>
5415e32d5dSMike Smith 
55d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
56d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h>
57d320e05cSJohn Baldwin #endif
5815e32d5dSMike Smith #include <machine/resource.h>
59dc750869SNate Lawson #include <machine/bus.h>
60dc750869SNate Lawson #include <sys/rman.h>
61bc0f2195SMike Smith #include <isa/isavar.h>
62c796e27bSNate Lawson #include <isa/pnpvar.h>
63bc0f2195SMike Smith 
64129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
65129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
66129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
67129d3046SJung-uk Kim 
6815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
6915e32d5dSMike Smith #include <dev/acpica/acpiio.h>
7015e32d5dSMike Smith 
7110ce62b9SNate Lawson #include "pci_if.h"
7210ce62b9SNate Lawson #include <dev/pci/pcivar.h>
7310ce62b9SNate Lawson #include <dev/pci/pci_private.h>
7410ce62b9SNate Lawson 
752be4e471SJung-uk Kim #include <vm/vm_param.h>
762be4e471SJung-uk Kim 
7715e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
7815e32d5dSMike Smith 
79be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
80cb9b0d80SMike Smith #define _COMPONENT	ACPI_BUS
81b53f2771SMike Smith ACPI_MODULE_NAME("ACPI")
820ae55423SMike Smith 
8315e32d5dSMike Smith static d_open_t		acpiopen;
8415e32d5dSMike Smith static d_close_t	acpiclose;
8515e32d5dSMike Smith static d_ioctl_t	acpiioctl;
8615e32d5dSMike Smith 
8715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = {
88dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
897ac40f5fSPoul-Henning Kamp 	.d_open =	acpiopen,
907ac40f5fSPoul-Henning Kamp 	.d_close =	acpiclose,
917ac40f5fSPoul-Henning Kamp 	.d_ioctl =	acpiioctl,
927ac40f5fSPoul-Henning Kamp 	.d_name =	"acpi",
9315e32d5dSMike Smith };
9415e32d5dSMike Smith 
95346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */
96cb9b0d80SMike Smith struct mtx	acpi_mutex;
97cb9b0d80SMike Smith 
9889fb5af8SNate Lawson /* Bitmap of device quirks. */
9989fb5af8SNate Lawson int		acpi_quirks;
10089fb5af8SNate Lawson 
101a0e73a12SJung-uk Kim /* Supported sleep states. */
102a0e73a12SJung-uk Kim static BOOLEAN	acpi_sleep_states[ACPI_S_STATE_COUNT];
103a0e73a12SJung-uk Kim 
1046d63101aSMike Smith static int	acpi_modevent(struct module *mod, int event, void *junk);
10515e32d5dSMike Smith static int	acpi_probe(device_t dev);
10615e32d5dSMike Smith static int	acpi_attach(device_t dev);
10710ce62b9SNate Lawson static int	acpi_suspend(device_t dev);
10810ce62b9SNate Lawson static int	acpi_resume(device_t dev);
109169b539aSNate Lawson static int	acpi_shutdown(device_t dev);
110be2b1797SNate Lawson static device_t	acpi_add_child(device_t bus, int order, const char *name,
111be2b1797SNate Lawson 			int unit);
11215e32d5dSMike Smith static int	acpi_print_child(device_t bus, device_t child);
11310ce62b9SNate Lawson static void	acpi_probe_nomatch(device_t bus, device_t child);
11410ce62b9SNate Lawson static void	acpi_driver_added(device_t dev, driver_t *driver);
115be2b1797SNate Lawson static int	acpi_read_ivar(device_t dev, device_t child, int index,
116be2b1797SNate Lawson 			uintptr_t *result);
117be2b1797SNate Lawson static int	acpi_write_ivar(device_t dev, device_t child, int index,
118be2b1797SNate Lawson 			uintptr_t value);
11991233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
120adad4744SNate Lawson static int	acpi_sysres_alloc(device_t dev);
121be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child,
122be2b1797SNate Lawson 			int type, int *rid, u_long start, u_long end,
123be2b1797SNate Lawson 			u_long count, u_int flags);
124be2b1797SNate Lawson static int	acpi_release_resource(device_t bus, device_t child, int type,
125be2b1797SNate Lawson 			int rid, struct resource *r);
1268b28b622SNate Lawson static void	acpi_delete_resource(device_t bus, device_t child, int type,
1278b28b622SNate Lawson 		    int rid);
1281e4925e8SNate Lawson static uint32_t	acpi_isa_get_logicalid(device_t dev);
1291e4925e8SNate Lawson static int	acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
130ce619b2eSNate Lawson static char	*acpi_device_id_probe(device_t bus, device_t dev, char **ids);
131ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
132ce619b2eSNate Lawson 		    ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
133ce619b2eSNate Lawson 		    ACPI_BUFFER *ret);
13410ce62b9SNate Lawson static int	acpi_device_pwr_for_sleep(device_t bus, device_t dev,
13510ce62b9SNate Lawson 		    int *dstate);
136df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
137df8d2a32SNate Lawson 		    void *context, void **retval);
138df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
139df8d2a32SNate Lawson 		    int max_depth, acpi_scan_cb_t user_fn, void *arg);
14010ce62b9SNate Lawson static int	acpi_set_powerstate_method(device_t bus, device_t child,
14110ce62b9SNate Lawson 		    int state);
142be2b1797SNate Lawson static int	acpi_isa_pnp_probe(device_t bus, device_t child,
143be2b1797SNate Lawson 		    struct isa_pnp_id *ids);
14415e32d5dSMike Smith static void	acpi_probe_children(device_t bus);
145d227204dSJohn Baldwin static void	acpi_probe_order(ACPI_HANDLE handle, int *order);
146be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
147be2b1797SNate Lawson 		    void *context, void **status);
148ce619b2eSNate Lawson static BOOLEAN	acpi_MatchHid(ACPI_HANDLE h, const char *hid);
149a0e73a12SJung-uk Kim static void	acpi_sleep_enable(void *arg);
150a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc);
15100a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state);
15215e32d5dSMike Smith static void	acpi_shutdown_final(void *arg, int howto);
15313d4f7baSMitsuru IWASAKI static void	acpi_enable_fixed_events(struct acpi_softc *sc);
154d4b9ff91SNate Lawson static int	acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
155d4b9ff91SNate Lawson static int	acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
156d4b9ff91SNate Lawson static int	acpi_wake_prep_walk(int sstate);
15788a79fc0SNate Lawson static int	acpi_wake_sysctl_walk(device_t dev);
15888a79fc0SNate Lawson static int	acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
15915e32d5dSMike Smith static void	acpi_system_eventhandler_sleep(void *arg, int state);
16015e32d5dSMike Smith static void	acpi_system_eventhandler_wakeup(void *arg, int state);
161a0e73a12SJung-uk Kim static int	acpi_sname2sstate(const char *sname);
162a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate);
163d75de536SMitsuru IWASAKI static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1641d073b1dSJohn Baldwin static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
1652a18c71dSJung-uk Kim static int	acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS);
166f9390180SMitsuru IWASAKI static int	acpi_pm_func(u_long cmd, void *arg, ...);
167879d6c23STakanori Watanabe static int	acpi_child_location_str_method(device_t acdev, device_t child,
168879d6c23STakanori Watanabe 					       char *buf, size_t buflen);
169879d6c23STakanori Watanabe static int	acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
170879d6c23STakanori Watanabe 					      char *buf, size_t buflen);
171d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
172d320e05cSJohn Baldwin static void	acpi_enable_pcie(void);
173d320e05cSJohn Baldwin #endif
1740d484d24SJohn Baldwin static void	acpi_hint_device_unit(device_t acdev, device_t child,
1750d484d24SJohn Baldwin 		    const char *name, int *unitp);
176879d6c23STakanori Watanabe 
17715e32d5dSMike Smith static device_method_t acpi_methods[] = {
17815e32d5dSMike Smith     /* Device interface */
17915e32d5dSMike Smith     DEVMETHOD(device_probe,		acpi_probe),
18015e32d5dSMike Smith     DEVMETHOD(device_attach,		acpi_attach),
181169b539aSNate Lawson     DEVMETHOD(device_shutdown,		acpi_shutdown),
18256a70eadSNate Lawson     DEVMETHOD(device_detach,		bus_generic_detach),
18310ce62b9SNate Lawson     DEVMETHOD(device_suspend,		acpi_suspend),
18410ce62b9SNate Lawson     DEVMETHOD(device_resume,		acpi_resume),
18515e32d5dSMike Smith 
18615e32d5dSMike Smith     /* Bus interface */
18715e32d5dSMike Smith     DEVMETHOD(bus_add_child,		acpi_add_child),
18815e32d5dSMike Smith     DEVMETHOD(bus_print_child,		acpi_print_child),
18910ce62b9SNate Lawson     DEVMETHOD(bus_probe_nomatch,	acpi_probe_nomatch),
19010ce62b9SNate Lawson     DEVMETHOD(bus_driver_added,		acpi_driver_added),
19115e32d5dSMike Smith     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
19215e32d5dSMike Smith     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
19391233413SNate Lawson     DEVMETHOD(bus_get_resource_list,	acpi_get_rlist),
19491233413SNate Lawson     DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
19591233413SNate Lawson     DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
19615e32d5dSMike Smith     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
19715e32d5dSMike Smith     DEVMETHOD(bus_release_resource,	acpi_release_resource),
1988b28b622SNate Lawson     DEVMETHOD(bus_delete_resource,	acpi_delete_resource),
199879d6c23STakanori Watanabe     DEVMETHOD(bus_child_pnpinfo_str,	acpi_child_pnpinfo_str_method),
200879d6c23STakanori Watanabe     DEVMETHOD(bus_child_location_str,	acpi_child_location_str_method),
20115e32d5dSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
20215e32d5dSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
20315e32d5dSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
20415e32d5dSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
2050d484d24SJohn Baldwin     DEVMETHOD(bus_hint_device_unit,	acpi_hint_device_unit),
20615e32d5dSMike Smith 
207ce619b2eSNate Lawson     /* ACPI bus */
208ce619b2eSNate Lawson     DEVMETHOD(acpi_id_probe,		acpi_device_id_probe),
209ce619b2eSNate Lawson     DEVMETHOD(acpi_evaluate_object,	acpi_device_eval_obj),
21010ce62b9SNate Lawson     DEVMETHOD(acpi_pwr_for_sleep,	acpi_device_pwr_for_sleep),
211df8d2a32SNate Lawson     DEVMETHOD(acpi_scan_children,	acpi_device_scan_children),
212ce619b2eSNate Lawson 
21310ce62b9SNate Lawson     /* PCI emulation */
21410ce62b9SNate Lawson     DEVMETHOD(pci_set_powerstate,	acpi_set_powerstate_method),
21510ce62b9SNate Lawson 
216bc0f2195SMike Smith     /* ISA emulation */
217bc0f2195SMike Smith     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
218bc0f2195SMike Smith 
21915e32d5dSMike Smith     {0, 0}
22015e32d5dSMike Smith };
22115e32d5dSMike Smith 
22215e32d5dSMike Smith static driver_t acpi_driver = {
22315e32d5dSMike Smith     "acpi",
22415e32d5dSMike Smith     acpi_methods,
22515e32d5dSMike Smith     sizeof(struct acpi_softc),
22615e32d5dSMike Smith };
22715e32d5dSMike Smith 
2283273b005SMike Smith static devclass_t acpi_devclass;
2296d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
230a4ecd543SNate Lawson MODULE_VERSION(acpi, 1);
23115e32d5dSMike Smith 
23215e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus");
23315e2f34fSNate Lawson 
234adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */
235adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem;
236adad4744SNate Lawson 
237bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME	5
238bee4aa4aSNate Lawson 
2395217af30SJohn Baldwin /* Holds the description of the acpi0 device. */
2405217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2];
2415217af30SJohn Baldwin 
242197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging");
2431d7b121cSNate Lawson static char acpi_ca_version[12];
2441d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
2451d7b121cSNate Lawson 	      acpi_ca_version, 0, "Version of Intel ACPI-CA");
24615e32d5dSMike Smith 
24715e32d5dSMike Smith /*
248413081d7SNate Lawson  * Allow override of whether methods execute in parallel or not.
249c9b8d77dSNate Lawson  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
250c9b8d77dSNate Lawson  * errors for AML that really can't handle parallel method execution.
251c9b8d77dSNate Lawson  * It is off by default since this breaks recursive methods and
252c9b8d77dSNate Lawson  * some IBMs use such code.
253413081d7SNate Lawson  */
254c9b8d77dSNate Lawson static int acpi_serialize_methods;
255413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
256413081d7SNate Lawson 
2572a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */
2582a18c71dSJung-uk Kim static int acpi_debug_objects;
2592a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects);
2602a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects,
2612a18c71dSJung-uk Kim     CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I",
2622a18c71dSJung-uk Kim     "Enable Debug objects");
2632a18c71dSJung-uk Kim 
2642a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */
2652a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1;
2662a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack);
2672a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN,
2682a18c71dSJung-uk Kim     &acpi_interpreter_slack, 1, "Turn on interpreter slack mode.");
2692a18c71dSJung-uk Kim 
27010ce62b9SNate Lawson /* Power devices off and on in suspend and resume.  XXX Remove once tested. */
27110ce62b9SNate Lawson static int acpi_do_powerstate = 1;
27210ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
27310ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
27410ce62b9SNate Lawson     &acpi_do_powerstate, 1, "Turn off devices when suspending.");
27510ce62b9SNate Lawson 
2760755473bSJung-uk Kim /* Reset system clock while resuming.  XXX Remove once tested. */
2770755473bSJung-uk Kim static int acpi_reset_clock = 1;
2780755473bSJung-uk Kim TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock);
2790755473bSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW,
2800755473bSJung-uk Kim     &acpi_reset_clock, 1, "Reset system clock while resuming.");
2810755473bSJung-uk Kim 
28239da3eb3SNate Lawson /* Allow users to override quirks. */
28339da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
28439da3eb3SNate Lawson 
28593bfd059SNate Lawson static int acpi_susp_bounce;
28693bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW,
28793bfd059SNate Lawson     &acpi_susp_bounce, 0, "Don't actually suspend, just test devices.");
28893bfd059SNate Lawson 
289c9b8d77dSNate Lawson /*
2906d63101aSMike Smith  * ACPI can only be loaded as a module by the loader; activating it after
2916d63101aSMike Smith  * system bootstrap time is not useful, and can be fatal to the system.
2928c0879b6SJohn Baldwin  * It also cannot be unloaded, since the entire system bus hierarchy hangs
293be2b1797SNate Lawson  * off it.
2946d63101aSMike Smith  */
2956d63101aSMike Smith static int
2966d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk)
2976d63101aSMike Smith {
2986d63101aSMike Smith     switch (event) {
2996d63101aSMike Smith     case MOD_LOAD:
30087b45ed5SMitsuru IWASAKI 	if (!cold) {
30187b45ed5SMitsuru IWASAKI 	    printf("The ACPI driver cannot be loaded after boot.\n");
3026d63101aSMike Smith 	    return (EPERM);
30387b45ed5SMitsuru IWASAKI 	}
3046d63101aSMike Smith 	break;
3056d63101aSMike Smith     case MOD_UNLOAD:
306f9390180SMitsuru IWASAKI 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
3076d63101aSMike Smith 	    return (EBUSY);
308f9390180SMitsuru IWASAKI 	break;
3096d63101aSMike Smith     default:
3106d63101aSMike Smith 	break;
3116d63101aSMike Smith     }
3126d63101aSMike Smith     return (0);
3136d63101aSMike Smith }
3146d63101aSMike Smith 
3156d63101aSMike Smith /*
316bbc2815cSJohn Baldwin  * Perform early initialization.
31715e32d5dSMike Smith  */
318bbc2815cSJohn Baldwin ACPI_STATUS
319bbc2815cSJohn Baldwin acpi_Startup(void)
32015e32d5dSMike Smith {
32189fb5af8SNate Lawson     static int started = 0;
3222be4e471SJung-uk Kim     ACPI_STATUS status;
3232be4e471SJung-uk Kim     int val;
32415e32d5dSMike Smith 
3251b8c233dSPeter Pentchev     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3261b8c233dSPeter Pentchev 
327bee4aa4aSNate Lawson     /* Only run the startup code once.  The MADT driver also calls this. */
328bbc2815cSJohn Baldwin     if (started)
3292be4e471SJung-uk Kim 	return_VALUE (AE_OK);
330bbc2815cSJohn Baldwin     started = 1;
33115e32d5dSMike Smith 
332413081d7SNate Lawson     /*
3332be4e471SJung-uk Kim      * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing
3342be4e471SJung-uk Kim      * if more tables exist.
335413081d7SNate Lawson      */
3362be4e471SJung-uk Kim     if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
3372be4e471SJung-uk Kim 	printf("ACPI: Table initialisation failed: %s\n",
3382be4e471SJung-uk Kim 	    AcpiFormatException(status));
3392be4e471SJung-uk Kim 	return_VALUE (status);
34015e32d5dSMike Smith     }
3413184cf5aSNate Lawson 
34289fb5af8SNate Lawson     /* Set up any quirks we have for this system. */
3432be4e471SJung-uk Kim     if (acpi_quirks == ACPI_Q_OK)
34489fb5af8SNate Lawson 	acpi_table_quirks(&acpi_quirks);
34589fb5af8SNate Lawson 
34639da3eb3SNate Lawson     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
34789fb5af8SNate Lawson     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
34889fb5af8SNate Lawson 	acpi_quirks &= ~ACPI_Q_BROKEN;
34989fb5af8SNate Lawson     if (acpi_quirks & ACPI_Q_BROKEN) {
35089fb5af8SNate Lawson 	printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
3512be4e471SJung-uk Kim 	status = AE_SUPPORT;
35289fb5af8SNate Lawson     }
3533184cf5aSNate Lawson 
3542be4e471SJung-uk Kim     return_VALUE (status);
355bbc2815cSJohn Baldwin }
356bbc2815cSJohn Baldwin 
357bbc2815cSJohn Baldwin /*
3585217af30SJohn Baldwin  * Detect ACPI and perform early initialisation.
359bbc2815cSJohn Baldwin  */
3605217af30SJohn Baldwin int
3615217af30SJohn Baldwin acpi_identify(void)
362bbc2815cSJohn Baldwin {
3635217af30SJohn Baldwin     ACPI_TABLE_RSDP	*rsdp;
3645217af30SJohn Baldwin     ACPI_TABLE_HEADER	*rsdt;
3655217af30SJohn Baldwin     ACPI_PHYSICAL_ADDRESS paddr;
3665217af30SJohn Baldwin     struct sbuf		sb;
367bbc2815cSJohn Baldwin 
368bbc2815cSJohn Baldwin     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
369bbc2815cSJohn Baldwin 
370bbc2815cSJohn Baldwin     if (!cold)
3715217af30SJohn Baldwin 	return (ENXIO);
372bbc2815cSJohn Baldwin 
373bbc2815cSJohn Baldwin     /* Check that we haven't been disabled with a hint. */
374bbc2815cSJohn Baldwin     if (resource_disabled("acpi", 0))
3755217af30SJohn Baldwin 	return (ENXIO);
376bbc2815cSJohn Baldwin 
3775217af30SJohn Baldwin     /* Check for other PM systems. */
3785217af30SJohn Baldwin     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
3795217af30SJohn Baldwin 	power_pm_get_type() != POWER_PM_TYPE_ACPI) {
3805217af30SJohn Baldwin 	printf("ACPI identify failed, other PM system enabled.\n");
3815217af30SJohn Baldwin 	return (ENXIO);
3825217af30SJohn Baldwin     }
3834b1ff978SMark Santcroos 
3842be4e471SJung-uk Kim     /* Initialize root tables. */
3852be4e471SJung-uk Kim     if (ACPI_FAILURE(acpi_Startup())) {
3862be4e471SJung-uk Kim 	printf("ACPI: Try disabling either ACPI or apic support.\n");
3875217af30SJohn Baldwin 	return (ENXIO);
3882be4e471SJung-uk Kim     }
38915e32d5dSMike Smith 
3905217af30SJohn Baldwin     if ((paddr = AcpiOsGetRootPointer()) == 0 ||
3915217af30SJohn Baldwin 	(rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL)
3925217af30SJohn Baldwin 	return (ENXIO);
3935217af30SJohn Baldwin     if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0)
3945217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
3955217af30SJohn Baldwin     else
3965217af30SJohn Baldwin 	paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
3975217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
3985217af30SJohn Baldwin 
3995217af30SJohn Baldwin     if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL)
4005217af30SJohn Baldwin 	return (ENXIO);
4015217af30SJohn Baldwin     sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN);
4025217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE);
4035217af30SJohn Baldwin     sbuf_trim(&sb);
4045217af30SJohn Baldwin     sbuf_putc(&sb, ' ');
4055217af30SJohn Baldwin     sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
4065217af30SJohn Baldwin     sbuf_trim(&sb);
4075217af30SJohn Baldwin     sbuf_finish(&sb);
4085217af30SJohn Baldwin     sbuf_delete(&sb);
4095217af30SJohn Baldwin     AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
4105217af30SJohn Baldwin 
4115217af30SJohn Baldwin     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION);
4125217af30SJohn Baldwin 
4135217af30SJohn Baldwin     return (0);
41415e32d5dSMike Smith }
41515e32d5dSMike Smith 
41615e32d5dSMike Smith /*
417bee4aa4aSNate Lawson  * Fetch some descriptive data from ACPI to put in our attach message.
41815e32d5dSMike Smith  */
41915e32d5dSMike Smith static int
42015e32d5dSMike Smith acpi_probe(device_t dev)
42115e32d5dSMike Smith {
42215e32d5dSMike Smith 
423b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
424f9390180SMitsuru IWASAKI 
4255217af30SJohn Baldwin     device_set_desc(dev, acpi_desc);
426bee4aa4aSNate Lawson 
4272be4e471SJung-uk Kim     return_VALUE (0);
42815e32d5dSMike Smith }
42915e32d5dSMike Smith 
43015e32d5dSMike Smith static int
43115e32d5dSMike Smith acpi_attach(device_t dev)
43215e32d5dSMike Smith {
43315e32d5dSMike Smith     struct acpi_softc	*sc;
434cb9b0d80SMike Smith     ACPI_STATUS		status;
435ea27c63eSNate Lawson     int			error, state;
43632d18aa5SMike Smith     UINT32		flags;
437ea27c63eSNate Lawson     UINT8		TypeA, TypeB;
438498d464fSMitsuru IWASAKI     char		*env;
43915e32d5dSMike Smith 
440b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
441bee4aa4aSNate Lawson 
44215e32d5dSMike Smith     sc = device_get_softc(dev);
44315e32d5dSMike Smith     sc->acpi_dev = dev;
44400a30448SNate Lawson     callout_init(&sc->susp_force_to, TRUE);
44515e32d5dSMike Smith 
4462be4e471SJung-uk Kim     error = ENXIO;
4472be4e471SJung-uk Kim 
44891233413SNate Lawson     /* Initialize resource manager. */
44991233413SNate Lawson     acpi_rman_io.rm_type = RMAN_ARRAY;
45091233413SNate Lawson     acpi_rman_io.rm_start = 0;
45191233413SNate Lawson     acpi_rman_io.rm_end = 0xffff;
4520bba6acfSJohn Baldwin     acpi_rman_io.rm_descr = "ACPI I/O ports";
45391233413SNate Lawson     if (rman_init(&acpi_rman_io) != 0)
45491233413SNate Lawson 	panic("acpi rman_init IO ports failed");
45591233413SNate Lawson     acpi_rman_mem.rm_type = RMAN_ARRAY;
45691233413SNate Lawson     acpi_rman_mem.rm_start = 0;
45791233413SNate Lawson     acpi_rman_mem.rm_end = ~0ul;
4580bba6acfSJohn Baldwin     acpi_rman_mem.rm_descr = "ACPI I/O memory addresses";
45991233413SNate Lawson     if (rman_init(&acpi_rman_mem) != 0)
46091233413SNate Lawson 	panic("acpi rman_init memory failed");
46191233413SNate Lawson 
4622be4e471SJung-uk Kim     /* Initialise the ACPI mutex */
4632be4e471SJung-uk Kim     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
4642be4e471SJung-uk Kim 
4652be4e471SJung-uk Kim     /*
4662be4e471SJung-uk Kim      * Set the globals from our tunables.  This is needed because ACPI-CA
4672be4e471SJung-uk Kim      * uses UINT8 for some values and we have no tunable_byte.
4682be4e471SJung-uk Kim      */
4692a18c71dSJung-uk Kim     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods ? TRUE : FALSE;
4702a18c71dSJung-uk Kim     AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE;
4712a18c71dSJung-uk Kim     AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
4722a18c71dSJung-uk Kim 
4732a18c71dSJung-uk Kim #ifndef ACPI_DEBUG
4742a18c71dSJung-uk Kim     /*
4752a18c71dSJung-uk Kim      * Disable all debugging layers and levels.
4762a18c71dSJung-uk Kim      */
4772a18c71dSJung-uk Kim     AcpiDbgLayer = 0;
4782a18c71dSJung-uk Kim     AcpiDbgLevel = 0;
4792a18c71dSJung-uk Kim #endif
4802be4e471SJung-uk Kim 
4812be4e471SJung-uk Kim     /* Start up the ACPI CA subsystem. */
4822be4e471SJung-uk Kim     status = AcpiInitializeSubsystem();
4832be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4842be4e471SJung-uk Kim 	device_printf(dev, "Could not initialize Subsystem: %s\n",
4852be4e471SJung-uk Kim 		      AcpiFormatException(status));
4862be4e471SJung-uk Kim 	goto out;
4872be4e471SJung-uk Kim     }
4882be4e471SJung-uk Kim 
4892be4e471SJung-uk Kim     /* Load ACPI name space. */
4902be4e471SJung-uk Kim     status = AcpiLoadTables();
4912be4e471SJung-uk Kim     if (ACPI_FAILURE(status)) {
4922be4e471SJung-uk Kim 	device_printf(dev, "Could not load Namespace: %s\n",
4932be4e471SJung-uk Kim 		      AcpiFormatException(status));
4942be4e471SJung-uk Kim 	goto out;
4952be4e471SJung-uk Kim     }
4962be4e471SJung-uk Kim 
497d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
498d320e05cSJohn Baldwin     /* Handle MCFG table if present. */
499d320e05cSJohn Baldwin     acpi_enable_pcie();
500d320e05cSJohn Baldwin #endif
501d320e05cSJohn Baldwin 
502be2b1797SNate Lawson     /* Install the default address space handlers. */
503be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
504be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
505be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
506be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
507be2b1797SNate Lawson 		      AcpiFormatException(status));
508cb9b0d80SMike Smith 	goto out;
50915e32d5dSMike Smith     }
510be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
511be2b1797SNate Lawson 		ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
512be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
513be2b1797SNate Lawson 	device_printf(dev, "Could not initialise SystemIO handler: %s\n",
514be2b1797SNate Lawson 		      AcpiFormatException(status));
515cb9b0d80SMike Smith 	goto out;
51615e32d5dSMike Smith     }
517be2b1797SNate Lawson     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
518be2b1797SNate Lawson 		ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
519be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
520be2b1797SNate Lawson 	device_printf(dev, "could not initialise PciConfig handler: %s\n",
521be2b1797SNate Lawson 		      AcpiFormatException(status));
522cb9b0d80SMike Smith 	goto out;
52315e32d5dSMike Smith     }
52415e32d5dSMike Smith 
52515e32d5dSMike Smith     /*
526be2b1797SNate Lawson      * Note that some systems (specifically, those with namespace evaluation
527be2b1797SNate Lawson      * issues that require the avoidance of parts of the namespace) must
528be2b1797SNate Lawson      * avoid running _INI and _STA on everything, as well as dodging the final
529be2b1797SNate Lawson      * object init pass.
53015e32d5dSMike Smith      *
53132d18aa5SMike Smith      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
53232d18aa5SMike Smith      *
533be2b1797SNate Lawson      * XXX We should arrange for the object init pass after we have attached
534be2b1797SNate Lawson      *     all our child devices, but on many systems it works here.
53515e32d5dSMike Smith      */
53632d18aa5SMike Smith     flags = 0;
537d786139cSMaxime Henrion     if (testenv("debug.acpi.avoid"))
53832d18aa5SMike Smith 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
539bee4aa4aSNate Lawson 
540bee4aa4aSNate Lawson     /* Bring the hardware and basic handlers online. */
541b53f2771SMike Smith     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
542be2b1797SNate Lawson 	device_printf(dev, "Could not enable ACPI: %s\n",
543be2b1797SNate Lawson 		      AcpiFormatException(status));
544cb9b0d80SMike Smith 	goto out;
54515e32d5dSMike Smith     }
54615e32d5dSMike Smith 
547f8335e3aSNate Lawson     /*
548f8335e3aSNate Lawson      * Call the ECDT probe function to provide EC functionality before
549f8335e3aSNate Lawson      * the namespace has been evaluated.
550da72d149SNate Lawson      *
551da72d149SNate Lawson      * XXX This happens before the sysresource devices have been probed and
552da72d149SNate Lawson      * attached so its resources come from nexus0.  In practice, this isn't
553da72d149SNate Lawson      * a problem but should be addressed eventually.
554f8335e3aSNate Lawson      */
555f8335e3aSNate Lawson     acpi_ec_ecdt_probe(dev);
556f8335e3aSNate Lawson 
557bee4aa4aSNate Lawson     /* Bring device objects and regions online. */
558b69ed3f4SMitsuru IWASAKI     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
559be2b1797SNate Lawson 	device_printf(dev, "Could not initialize ACPI objects: %s\n",
560be2b1797SNate Lawson 		      AcpiFormatException(status));
561b69ed3f4SMitsuru IWASAKI 	goto out;
562b69ed3f4SMitsuru IWASAKI     }
563b69ed3f4SMitsuru IWASAKI 
56415e32d5dSMike Smith     /*
5651d073b1dSJohn Baldwin      * Setup our sysctl tree.
5661d073b1dSJohn Baldwin      *
5671d073b1dSJohn Baldwin      * XXX: This doesn't check to make sure that none of these fail.
5681d073b1dSJohn Baldwin      */
5691d073b1dSJohn Baldwin     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
5701d073b1dSJohn Baldwin     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
5711d073b1dSJohn Baldwin 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
5721d073b1dSJohn Baldwin 			       device_get_name(dev), CTLFLAG_RD, 0, "");
5731d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
574d75de536SMitsuru IWASAKI 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
575d75de536SMitsuru IWASAKI 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
576d75de536SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5771d073b1dSJohn Baldwin 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5781d073b1dSJohn Baldwin 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5791d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5801d073b1dSJohn Baldwin 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
5811d073b1dSJohn Baldwin 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
5821d073b1dSJohn Baldwin     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
5831d073b1dSJohn Baldwin 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
5841d073b1dSJohn Baldwin 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
585f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
586f86214b6SMitsuru IWASAKI 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
587f86214b6SMitsuru IWASAKI 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
588f86214b6SMitsuru IWASAKI     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
589f86214b6SMitsuru IWASAKI 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
590f86214b6SMitsuru IWASAKI 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
5912d644607SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
592197b4dccSNate Lawson 	OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0,
593197b4dccSNate Lawson 	"sleep delay");
594ff01efb5SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
595197b4dccSNate Lawson 	OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode");
5961611ea87SMitsuru IWASAKI     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
597197b4dccSNate Lawson 	OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode");
598d85e6785SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
599d85e6785SNate Lawson 	OID_AUTO, "disable_on_reboot", CTLFLAG_RW,
600d85e6785SNate Lawson 	&sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system");
601d1b16e18SNate Lawson     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
602d1b16e18SNate Lawson 	OID_AUTO, "handle_reboot", CTLFLAG_RW,
603d1b16e18SNate Lawson 	&sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot");
604bf0c18ecSNate Lawson 
605bf0c18ecSNate Lawson     /*
6062b9609abSNate Lawson      * Default to 1 second before sleeping to give some machines time to
607bf0c18ecSNate Lawson      * stabilize.
608bf0c18ecSNate Lawson      */
6092b9609abSNate Lawson     sc->acpi_sleep_delay = 1;
6102d644607SMitsuru IWASAKI     if (bootverbose)
6112d644607SMitsuru IWASAKI 	sc->acpi_verbose = 1;
612965a34fbSNate Lawson     if ((env = getenv("hw.acpi.verbose")) != NULL) {
613965a34fbSNate Lawson 	if (strcmp(env, "0") != 0)
614498d464fSMitsuru IWASAKI 	    sc->acpi_verbose = 1;
615498d464fSMitsuru IWASAKI 	freeenv(env);
616498d464fSMitsuru IWASAKI     }
6171d073b1dSJohn Baldwin 
6182d610c46SNate Lawson     /* Only enable S4BIOS by default if the FACS says it is available. */
619129d3046SJung-uk Kim     if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT)
6202d610c46SNate Lawson 	sc->acpi_s4bios = 1;
6212d610c46SNate Lawson 
622a0e73a12SJung-uk Kim     /* Probe all supported sleep states. */
623a0e73a12SJung-uk Kim     acpi_sleep_states[ACPI_STATE_S0] = TRUE;
624a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
625a0e73a12SJung-uk Kim 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
626a0e73a12SJung-uk Kim 	    acpi_sleep_states[state] = TRUE;
627a0e73a12SJung-uk Kim 
6281d073b1dSJohn Baldwin     /*
629ea27c63eSNate Lawson      * Dispatch the default sleep state to devices.  The lid switch is set
630a0e73a12SJung-uk Kim      * to UNKNOWN by default to avoid surprising users.
63115e32d5dSMike Smith      */
632a0e73a12SJung-uk Kim     sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ?
633a0e73a12SJung-uk Kim 	ACPI_STATE_S5 : ACPI_STATE_UNKNOWN;
634a0e73a12SJung-uk Kim     sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN;
635a0e73a12SJung-uk Kim     sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ?
636a0e73a12SJung-uk Kim 	ACPI_STATE_S1 : ACPI_STATE_UNKNOWN;
637a0e73a12SJung-uk Kim     sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ?
638a0e73a12SJung-uk Kim 	ACPI_STATE_S3 : ACPI_STATE_UNKNOWN;
63915e32d5dSMike Smith 
640ea27c63eSNate Lawson     /* Pick the first valid sleep state for the sleep button default. */
641a0e73a12SJung-uk Kim     sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN;
64200a30448SNate Lawson     for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++)
643a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state]) {
644ea27c63eSNate Lawson 	    sc->acpi_sleep_button_sx = state;
645ea27c63eSNate Lawson 	    break;
646ea27c63eSNate Lawson 	}
647ea27c63eSNate Lawson 
64813d4f7baSMitsuru IWASAKI     acpi_enable_fixed_events(sc);
64915e32d5dSMike Smith 
65015e32d5dSMike Smith     /*
65115e32d5dSMike Smith      * Scan the namespace and attach/initialise children.
65215e32d5dSMike Smith      */
65315e32d5dSMike Smith 
65459e472e9SNate Lawson     /* Register our shutdown handler. */
655be2b1797SNate Lawson     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
656be2b1797SNate Lawson 	SHUTDOWN_PRI_LAST);
65715e32d5dSMike Smith 
65815e32d5dSMike Smith     /*
65915e32d5dSMike Smith      * Register our acpi event handlers.
66015e32d5dSMike Smith      * XXX should be configurable eg. via userland policy manager.
66115e32d5dSMike Smith      */
662be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
663be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
664be2b1797SNate Lawson     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
665be2b1797SNate Lawson 	sc, ACPI_EVENT_PRI_LAST);
66615e32d5dSMike Smith 
667be2b1797SNate Lawson     /* Flag our initial states. */
668a0e73a12SJung-uk Kim     sc->acpi_enabled = TRUE;
66915e32d5dSMike Smith     sc->acpi_sstate = ACPI_STATE_S0;
670a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
67115e32d5dSMike Smith 
672be2b1797SNate Lawson     /* Create the control device */
673a89fcf28STakanori Watanabe     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
6742a4689e8SRobert Watson 			      "acpi");
67515e32d5dSMike Smith     sc->acpi_dev_t->si_drv1 = sc;
67615e32d5dSMike Smith 
677be2b1797SNate Lawson     if ((error = acpi_machdep_init(dev)))
678f86214b6SMitsuru IWASAKI 	goto out;
679f86214b6SMitsuru IWASAKI 
680f9390180SMitsuru IWASAKI     /* Register ACPI again to pass the correct argument of pm_func. */
681f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
682f9390180SMitsuru IWASAKI 
683eeb6dba2SJohn Baldwin     if (!acpi_disabled("bus"))
684eeb6dba2SJohn Baldwin 	acpi_probe_children(dev);
685eeb6dba2SJohn Baldwin 
686a0e73a12SJung-uk Kim     /* Allow sleep request after a while. */
687a0e73a12SJung-uk Kim     timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
688a0e73a12SJung-uk Kim 
689cb9b0d80SMike Smith     error = 0;
690cb9b0d80SMike Smith 
691cb9b0d80SMike Smith  out:
692cb9b0d80SMike Smith     return_VALUE (error);
69315e32d5dSMike Smith }
69415e32d5dSMike Smith 
695169b539aSNate Lawson static int
69610ce62b9SNate Lawson acpi_suspend(device_t dev)
69710ce62b9SNate Lawson {
69810ce62b9SNate Lawson     device_t child, *devlist;
69910ce62b9SNate Lawson     int error, i, numdevs, pstate;
70010ce62b9SNate Lawson 
701a56fe095SJohn Baldwin     GIANT_REQUIRED;
702a56fe095SJohn Baldwin 
70310ce62b9SNate Lawson     /* First give child devices a chance to suspend. */
70410ce62b9SNate Lawson     error = bus_generic_suspend(dev);
70510ce62b9SNate Lawson     if (error)
70610ce62b9SNate Lawson 	return (error);
70710ce62b9SNate Lawson 
70810ce62b9SNate Lawson     /*
70910ce62b9SNate Lawson      * Now, set them into the appropriate power state, usually D3.  If the
71010ce62b9SNate Lawson      * device has an _SxD method for the next sleep state, use that power
71110ce62b9SNate Lawson      * state instead.
71210ce62b9SNate Lawson      */
713099ea4b5SWarner Losh     error = device_get_children(dev, &devlist, &numdevs);
714099ea4b5SWarner Losh     if (error)
715099ea4b5SWarner Losh 	return (error);
71610ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
71710ce62b9SNate Lawson 	/* If the device is not attached, we've powered it down elsewhere. */
71810ce62b9SNate Lawson 	child = devlist[i];
71910ce62b9SNate Lawson 	if (!device_is_attached(child))
72010ce62b9SNate Lawson 	    continue;
72110ce62b9SNate Lawson 
72210ce62b9SNate Lawson 	/*
72310ce62b9SNate Lawson 	 * Default to D3 for all sleep states.  The _SxD method is optional
72410ce62b9SNate Lawson 	 * so set the powerstate even if it's absent.
72510ce62b9SNate Lawson 	 */
72610ce62b9SNate Lawson 	pstate = PCI_POWERSTATE_D3;
72710ce62b9SNate Lawson 	error = acpi_device_pwr_for_sleep(device_get_parent(child),
72810ce62b9SNate Lawson 	    child, &pstate);
72910ce62b9SNate Lawson 	if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
73010ce62b9SNate Lawson 	    pci_set_powerstate(child, pstate);
73110ce62b9SNate Lawson     }
73210ce62b9SNate Lawson     free(devlist, M_TEMP);
73310ce62b9SNate Lawson     error = 0;
73410ce62b9SNate Lawson 
73510ce62b9SNate Lawson     return (error);
73610ce62b9SNate Lawson }
73710ce62b9SNate Lawson 
73810ce62b9SNate Lawson static int
73910ce62b9SNate Lawson acpi_resume(device_t dev)
74010ce62b9SNate Lawson {
74110ce62b9SNate Lawson     ACPI_HANDLE handle;
742099ea4b5SWarner Losh     int i, numdevs, error;
74310ce62b9SNate Lawson     device_t child, *devlist;
74410ce62b9SNate Lawson 
745a56fe095SJohn Baldwin     GIANT_REQUIRED;
746a56fe095SJohn Baldwin 
74710ce62b9SNate Lawson     /*
74810ce62b9SNate Lawson      * Put all devices in D0 before resuming them.  Call _S0D on each one
74910ce62b9SNate Lawson      * since some systems expect this.
75010ce62b9SNate Lawson      */
751099ea4b5SWarner Losh     error = device_get_children(dev, &devlist, &numdevs);
752099ea4b5SWarner Losh     if (error)
753099ea4b5SWarner Losh 	return (error);
75410ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
75510ce62b9SNate Lawson 	child = devlist[i];
75610ce62b9SNate Lawson 	handle = acpi_get_handle(child);
75710ce62b9SNate Lawson 	if (handle)
75810ce62b9SNate Lawson 	    AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
75910ce62b9SNate Lawson 	if (device_is_attached(child) && acpi_do_powerstate)
76010ce62b9SNate Lawson 	    pci_set_powerstate(child, PCI_POWERSTATE_D0);
76110ce62b9SNate Lawson     }
76210ce62b9SNate Lawson     free(devlist, M_TEMP);
76310ce62b9SNate Lawson 
76410ce62b9SNate Lawson     return (bus_generic_resume(dev));
76510ce62b9SNate Lawson }
76610ce62b9SNate Lawson 
76710ce62b9SNate Lawson static int
768169b539aSNate Lawson acpi_shutdown(device_t dev)
769169b539aSNate Lawson {
770169b539aSNate Lawson 
771a56fe095SJohn Baldwin     GIANT_REQUIRED;
772a56fe095SJohn Baldwin 
7730e414868SNate Lawson     /* Allow children to shutdown first. */
7740e414868SNate Lawson     bus_generic_shutdown(dev);
7750e414868SNate Lawson 
776bee4aa4aSNate Lawson     /*
777bee4aa4aSNate Lawson      * Enable any GPEs that are able to power-on the system (i.e., RTC).
778bee4aa4aSNate Lawson      * Also, disable any that are not valid for this state (most).
779bee4aa4aSNate Lawson      */
780d4b9ff91SNate Lawson     acpi_wake_prep_walk(ACPI_STATE_S5);
781d4b9ff91SNate Lawson 
782169b539aSNate Lawson     return (0);
783169b539aSNate Lawson }
784169b539aSNate Lawson 
78515e32d5dSMike Smith /*
78615e32d5dSMike Smith  * Handle a new device being added
78715e32d5dSMike Smith  */
78815e32d5dSMike Smith static device_t
78915e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit)
79015e32d5dSMike Smith {
79115e32d5dSMike Smith     struct acpi_device	*ad;
79215e32d5dSMike Smith     device_t		child;
79315e32d5dSMike Smith 
794be2b1797SNate Lawson     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
79515e32d5dSMike Smith 	return (NULL);
79615e32d5dSMike Smith 
79715e32d5dSMike Smith     resource_list_init(&ad->ad_rl);
79815e32d5dSMike Smith 
79915e32d5dSMike Smith     child = device_add_child_ordered(bus, order, name, unit);
80015e32d5dSMike Smith     if (child != NULL)
80115e32d5dSMike Smith 	device_set_ivars(child, ad);
80243ce1c77SNate Lawson     else
80343ce1c77SNate Lawson 	free(ad, M_ACPIDEV);
80415e32d5dSMike Smith     return (child);
80515e32d5dSMike Smith }
80615e32d5dSMike Smith 
80715e32d5dSMike Smith static int
80815e32d5dSMike Smith acpi_print_child(device_t bus, device_t child)
80915e32d5dSMike Smith {
81015e32d5dSMike Smith     struct acpi_device	 *adev = device_get_ivars(child);
81115e32d5dSMike Smith     struct resource_list *rl = &adev->ad_rl;
81215e32d5dSMike Smith     int retval = 0;
81315e32d5dSMike Smith 
81415e32d5dSMike Smith     retval += bus_print_child_header(bus, child);
8159ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
8169ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
8179ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
8189ed79ecaSJohn Baldwin     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
819a91c5fa8SNate Lawson     if (device_get_flags(child))
820a91c5fa8SNate Lawson 	retval += printf(" flags %#x", device_get_flags(child));
8212c8d3b23SNate Lawson     retval += bus_print_child_footer(bus, child);
82215e32d5dSMike Smith 
82315e32d5dSMike Smith     return (retval);
82415e32d5dSMike Smith }
82515e32d5dSMike Smith 
82610ce62b9SNate Lawson /*
82710ce62b9SNate Lawson  * If this device is an ACPI child but no one claimed it, attempt
82810ce62b9SNate Lawson  * to power it off.  We'll power it back up when a driver is added.
82910ce62b9SNate Lawson  *
83010ce62b9SNate Lawson  * XXX Disabled for now since many necessary devices (like fdc and
83110ce62b9SNate Lawson  * ATA) don't claim the devices we created for them but still expect
83210ce62b9SNate Lawson  * them to be powered up.
83310ce62b9SNate Lawson  */
83410ce62b9SNate Lawson static void
83510ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child)
83610ce62b9SNate Lawson {
83796cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
83896cf0b6dSWarner Losh     pci_set_powerstate(child, PCI_POWERSTATE_D3);
83996cf0b6dSWarner Losh #endif
84010ce62b9SNate Lawson }
84110ce62b9SNate Lawson 
84210ce62b9SNate Lawson /*
84310ce62b9SNate Lawson  * If a new driver has a chance to probe a child, first power it up.
84410ce62b9SNate Lawson  *
84510ce62b9SNate Lawson  * XXX Disabled for now (see acpi_probe_nomatch for details).
84610ce62b9SNate Lawson  */
84710ce62b9SNate Lawson static void
84810ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver)
84910ce62b9SNate Lawson {
85010ce62b9SNate Lawson     device_t child, *devlist;
85110ce62b9SNate Lawson     int i, numdevs;
85210ce62b9SNate Lawson 
85310ce62b9SNate Lawson     DEVICE_IDENTIFY(driver, dev);
854099ea4b5SWarner Losh     if (device_get_children(dev, &devlist, &numdevs))
855099ea4b5SWarner Losh 	    return;
85610ce62b9SNate Lawson     for (i = 0; i < numdevs; i++) {
85710ce62b9SNate Lawson 	child = devlist[i];
85810ce62b9SNate Lawson 	if (device_get_state(child) == DS_NOTPRESENT) {
85996cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER
86096cf0b6dSWarner Losh 	    pci_set_powerstate(child, PCI_POWERSTATE_D0);
86110ce62b9SNate Lawson 	    if (device_probe_and_attach(child) != 0)
86296cf0b6dSWarner Losh 		pci_set_powerstate(child, PCI_POWERSTATE_D3);
86396cf0b6dSWarner Losh #else
86496cf0b6dSWarner Losh 	    device_probe_and_attach(child);
86596cf0b6dSWarner Losh #endif
86610ce62b9SNate Lawson 	}
86710ce62b9SNate Lawson     }
86810ce62b9SNate Lawson     free(devlist, M_TEMP);
86910ce62b9SNate Lawson }
87010ce62b9SNate Lawson 
87163600cc3SNate Lawson /* Location hint for devctl(8) */
87263600cc3SNate Lawson static int
873247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
874247648afSTakanori Watanabe     size_t buflen)
875247648afSTakanori Watanabe {
876247648afSTakanori Watanabe     struct acpi_device *dinfo = device_get_ivars(child);
877247648afSTakanori Watanabe 
878247648afSTakanori Watanabe     if (dinfo->ad_handle)
879d40c0f53SNate Lawson 	snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
880247648afSTakanori Watanabe     else
881d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
882247648afSTakanori Watanabe     return (0);
883247648afSTakanori Watanabe }
884247648afSTakanori Watanabe 
88563600cc3SNate Lawson /* PnP information for devctl(8) */
88663600cc3SNate Lawson static int
887247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
888247648afSTakanori Watanabe     size_t buflen)
889247648afSTakanori Watanabe {
89063600cc3SNate Lawson     struct acpi_device *dinfo = device_get_ivars(child);
89192488a57SJung-uk Kim     ACPI_DEVICE_INFO *adinfo;
892247648afSTakanori Watanabe 
89392488a57SJung-uk Kim     if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) {
894d40c0f53SNate Lawson 	snprintf(buf, buflen, "unknown");
89592488a57SJung-uk Kim 	return (0);
89692488a57SJung-uk Kim     }
89792488a57SJung-uk Kim 
898247648afSTakanori Watanabe     snprintf(buf, buflen, "_HID=%s _UID=%lu",
899247648afSTakanori Watanabe 	(adinfo->Valid & ACPI_VALID_HID) ?
90092488a57SJung-uk Kim 	adinfo->HardwareId.String : "none",
90163600cc3SNate Lawson 	(adinfo->Valid & ACPI_VALID_UID) ?
90292488a57SJung-uk Kim 	strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL);
903247648afSTakanori Watanabe     AcpiOsFree(adinfo);
904247648afSTakanori Watanabe 
905247648afSTakanori Watanabe     return (0);
906247648afSTakanori Watanabe }
907247648afSTakanori Watanabe 
908247648afSTakanori Watanabe /*
90915e32d5dSMike Smith  * Handle per-device ivars
91015e32d5dSMike Smith  */
91115e32d5dSMike Smith static int
91215e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
91315e32d5dSMike Smith {
91415e32d5dSMike Smith     struct acpi_device	*ad;
91515e32d5dSMike Smith 
91615e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9172d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
91815e32d5dSMike Smith 	return (ENOENT);
91915e32d5dSMike Smith     }
92015e32d5dSMike Smith 
921be2b1797SNate Lawson     /* ACPI and ISA compatibility ivars */
92215e32d5dSMike Smith     switch(index) {
92315e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
92415e32d5dSMike Smith 	*(ACPI_HANDLE *)result = ad->ad_handle;
92515e32d5dSMike Smith 	break;
92615e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
92715e32d5dSMike Smith 	*(void **)result = ad->ad_private;
92815e32d5dSMike Smith 	break;
929d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
930d4b9ff91SNate Lawson 	*(int *)result = ad->ad_flags;
931d4b9ff91SNate Lawson 	break;
93232d18aa5SMike Smith     case ISA_IVAR_VENDORID:
93332d18aa5SMike Smith     case ISA_IVAR_SERIAL:
93432d18aa5SMike Smith     case ISA_IVAR_COMPATID:
93532d18aa5SMike Smith 	*(int *)result = -1;
93632d18aa5SMike Smith 	break;
93732d18aa5SMike Smith     case ISA_IVAR_LOGICALID:
93832d18aa5SMike Smith 	*(int *)result = acpi_isa_get_logicalid(child);
93932d18aa5SMike Smith 	break;
94015e32d5dSMike Smith     default:
94115e32d5dSMike Smith 	return (ENOENT);
94215e32d5dSMike Smith     }
943be2b1797SNate Lawson 
94415e32d5dSMike Smith     return (0);
94515e32d5dSMike Smith }
94615e32d5dSMike Smith 
94715e32d5dSMike Smith static int
94815e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
94915e32d5dSMike Smith {
95015e32d5dSMike Smith     struct acpi_device	*ad;
95115e32d5dSMike Smith 
95215e32d5dSMike Smith     if ((ad = device_get_ivars(child)) == NULL) {
9532d0c82e8SJung-uk Kim 	device_printf(child, "device has no ivars\n");
95415e32d5dSMike Smith 	return (ENOENT);
95515e32d5dSMike Smith     }
95615e32d5dSMike Smith 
95715e32d5dSMike Smith     switch(index) {
95815e32d5dSMike Smith     case ACPI_IVAR_HANDLE:
95915e32d5dSMike Smith 	ad->ad_handle = (ACPI_HANDLE)value;
96015e32d5dSMike Smith 	break;
96115e32d5dSMike Smith     case ACPI_IVAR_PRIVATE:
96215e32d5dSMike Smith 	ad->ad_private = (void *)value;
96315e32d5dSMike Smith 	break;
964d4b9ff91SNate Lawson     case ACPI_IVAR_FLAGS:
965d4b9ff91SNate Lawson 	ad->ad_flags = (int)value;
966d4b9ff91SNate Lawson 	break;
96715e32d5dSMike Smith     default:
9682ab060eeSWarner Losh 	panic("bad ivar write request (%d)", index);
96915e32d5dSMike Smith 	return (ENOENT);
97015e32d5dSMike Smith     }
971be2b1797SNate Lawson 
97215e32d5dSMike Smith     return (0);
97315e32d5dSMike Smith }
97415e32d5dSMike Smith 
97515e32d5dSMike Smith /*
97615e32d5dSMike Smith  * Handle child resource allocation/removal
97715e32d5dSMike Smith  */
97891233413SNate Lawson static struct resource_list *
97991233413SNate Lawson acpi_get_rlist(device_t dev, device_t child)
98015e32d5dSMike Smith {
98191233413SNate Lawson     struct acpi_device		*ad;
98215e32d5dSMike Smith 
98391233413SNate Lawson     ad = device_get_ivars(child);
98491233413SNate Lawson     return (&ad->ad_rl);
98515e32d5dSMike Smith }
98615e32d5dSMike Smith 
9870d484d24SJohn Baldwin static int
9880d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value)
9890d484d24SJohn Baldwin {
9900d484d24SJohn Baldwin     struct acpi_device *ad = device_get_ivars(dev);
9910d484d24SJohn Baldwin     struct resource_list *rl = &ad->ad_rl;
9920d484d24SJohn Baldwin     struct resource_list_entry *rle;
9930d484d24SJohn Baldwin 
9940d484d24SJohn Baldwin     STAILQ_FOREACH(rle, rl, link) {
9950d484d24SJohn Baldwin 	if (rle->type != type)
9960d484d24SJohn Baldwin 	    continue;
9970d484d24SJohn Baldwin 	if (rle->start <= value && rle->end >= value)
9980d484d24SJohn Baldwin 	    return (1);
9990d484d24SJohn Baldwin     }
10000d484d24SJohn Baldwin     return (0);
10010d484d24SJohn Baldwin }
10020d484d24SJohn Baldwin 
10030d484d24SJohn Baldwin /*
10040d484d24SJohn Baldwin  * Wire device unit numbers based on resource matches in hints.
10050d484d24SJohn Baldwin  */
10060d484d24SJohn Baldwin static void
10070d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name,
10080d484d24SJohn Baldwin     int *unitp)
10090d484d24SJohn Baldwin {
10100d484d24SJohn Baldwin     const char *s;
10110d484d24SJohn Baldwin     long value;
10120d484d24SJohn Baldwin     int line, matches, unit;
10130d484d24SJohn Baldwin 
10140d484d24SJohn Baldwin     /*
10150d484d24SJohn Baldwin      * Iterate over all the hints for the devices with the specified
10160d484d24SJohn Baldwin      * name to see if one's resources are a subset of this device.
10170d484d24SJohn Baldwin      */
10180d484d24SJohn Baldwin     line = 0;
10190d484d24SJohn Baldwin     for (;;) {
10200d484d24SJohn Baldwin 	if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
10210d484d24SJohn Baldwin 	    break;
10220d484d24SJohn Baldwin 
10230d484d24SJohn Baldwin 	/* Must have an "at" for acpi or isa. */
10240d484d24SJohn Baldwin 	resource_string_value(name, unit, "at", &s);
10250d484d24SJohn Baldwin 	if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 ||
10260d484d24SJohn Baldwin 	    strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0))
10270d484d24SJohn Baldwin 	    continue;
10280d484d24SJohn Baldwin 
10290d484d24SJohn Baldwin 	/*
10302fcd493cSJohn Baldwin 	 * Check for matching resources.  We must have at least one match.
10312fcd493cSJohn Baldwin 	 * Since I/O and memory resources cannot be shared, if we get a
10322fcd493cSJohn Baldwin 	 * match on either of those, ignore any mismatches in IRQs or DRQs.
10330d484d24SJohn Baldwin 	 *
10340d484d24SJohn Baldwin 	 * XXX: We may want to revisit this to be more lenient and wire
10350d484d24SJohn Baldwin 	 * as long as it gets one match.
10360d484d24SJohn Baldwin 	 */
10370d484d24SJohn Baldwin 	matches = 0;
10380d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "port", &value) == 0) {
10392fcd493cSJohn Baldwin 	    /*
10402fcd493cSJohn Baldwin 	     * Floppy drive controllers are notorious for having a
10412fcd493cSJohn Baldwin 	     * wide variety of resources not all of which include the
10422fcd493cSJohn Baldwin 	     * first port that is specified by the hint (typically
10432fcd493cSJohn Baldwin 	     * 0x3f0) (see the comment above fdc_isa_alloc_resources()
10442fcd493cSJohn Baldwin 	     * in fdc_isa.c).  However, they do all seem to include
10452fcd493cSJohn Baldwin 	     * port + 2 (e.g. 0x3f2) so for a floppy device, look for
10462fcd493cSJohn Baldwin 	     * 'value + 2' in the port resources instead of the hint
10472fcd493cSJohn Baldwin 	     * value.
10482fcd493cSJohn Baldwin 	     */
10492fcd493cSJohn Baldwin 	    if (strcmp(name, "fdc") == 0)
10502fcd493cSJohn Baldwin 		value += 2;
10510d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value))
10520d484d24SJohn Baldwin 		matches++;
10530d484d24SJohn Baldwin 	    else
10540d484d24SJohn Baldwin 		continue;
10550d484d24SJohn Baldwin 	}
10560d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "maddr", &value) == 0) {
10570d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value))
10580d484d24SJohn Baldwin 		matches++;
10590d484d24SJohn Baldwin 	    else
10600d484d24SJohn Baldwin 		continue;
10610d484d24SJohn Baldwin 	}
10622fcd493cSJohn Baldwin 	if (matches > 0)
10632fcd493cSJohn Baldwin 	    goto matched;
10640d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "irq", &value) == 0) {
10650d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_IRQ, value))
10660d484d24SJohn Baldwin 		matches++;
10670d484d24SJohn Baldwin 	    else
10680d484d24SJohn Baldwin 		continue;
10690d484d24SJohn Baldwin 	}
10700d484d24SJohn Baldwin 	if (resource_long_value(name, unit, "drq", &value) == 0) {
10710d484d24SJohn Baldwin 	    if (acpi_match_resource_hint(child, SYS_RES_DRQ, value))
10720d484d24SJohn Baldwin 		matches++;
10730d484d24SJohn Baldwin 	    else
10740d484d24SJohn Baldwin 		continue;
10750d484d24SJohn Baldwin 	}
10760d484d24SJohn Baldwin 
10772fcd493cSJohn Baldwin     matched:
10780d484d24SJohn Baldwin 	if (matches > 0) {
10790d484d24SJohn Baldwin 	    /* We have a winner! */
10800d484d24SJohn Baldwin 	    *unitp = unit;
10810d484d24SJohn Baldwin 	    break;
10820d484d24SJohn Baldwin 	}
10830d484d24SJohn Baldwin     }
10840d484d24SJohn Baldwin }
10850d484d24SJohn Baldwin 
1086adad4744SNate Lawson /*
1087adad4744SNate Lawson  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
1088adad4744SNate Lawson  * duplicates, we merge any in the sysresource attach routine.
1089adad4744SNate Lawson  */
1090adad4744SNate Lawson static int
1091adad4744SNate Lawson acpi_sysres_alloc(device_t dev)
1092adad4744SNate Lawson {
1093adad4744SNate Lawson     struct resource *res;
1094adad4744SNate Lawson     struct resource_list *rl;
1095adad4744SNate Lawson     struct resource_list_entry *rle;
1096adad4744SNate Lawson     struct rman *rm;
1097da72d149SNate Lawson     char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
1098da72d149SNate Lawson     device_t *children;
1099da72d149SNate Lawson     int child_count, i;
1100da72d149SNate Lawson 
1101da72d149SNate Lawson     /*
1102da72d149SNate Lawson      * Probe/attach any sysresource devices.  This would be unnecessary if we
1103da72d149SNate Lawson      * had multi-pass probe/attach.
1104da72d149SNate Lawson      */
1105da72d149SNate Lawson     if (device_get_children(dev, &children, &child_count) != 0)
1106da72d149SNate Lawson 	return (ENXIO);
1107da72d149SNate Lawson     for (i = 0; i < child_count; i++) {
1108da72d149SNate Lawson 	if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL)
1109da72d149SNate Lawson 	    device_probe_and_attach(children[i]);
1110da72d149SNate Lawson     }
1111da72d149SNate Lawson     free(children, M_TEMP);
1112adad4744SNate Lawson 
1113adad4744SNate Lawson     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
1114be1bf4d2SPoul-Henning Kamp     STAILQ_FOREACH(rle, rl, link) {
1115adad4744SNate Lawson 	if (rle->res != NULL) {
1116adad4744SNate Lawson 	    device_printf(dev, "duplicate resource for %lx\n", rle->start);
1117adad4744SNate Lawson 	    continue;
1118adad4744SNate Lawson 	}
1119adad4744SNate Lawson 
1120adad4744SNate Lawson 	/* Only memory and IO resources are valid here. */
1121adad4744SNate Lawson 	switch (rle->type) {
1122adad4744SNate Lawson 	case SYS_RES_IOPORT:
1123adad4744SNate Lawson 	    rm = &acpi_rman_io;
1124adad4744SNate Lawson 	    break;
1125adad4744SNate Lawson 	case SYS_RES_MEMORY:
1126adad4744SNate Lawson 	    rm = &acpi_rman_mem;
1127adad4744SNate Lawson 	    break;
1128adad4744SNate Lawson 	default:
1129adad4744SNate Lawson 	    continue;
1130adad4744SNate Lawson 	}
1131adad4744SNate Lawson 
1132adad4744SNate Lawson 	/* Pre-allocate resource and add to our rman pool. */
1133adad4744SNate Lawson 	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
1134adad4744SNate Lawson 	    &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
1135adad4744SNate Lawson 	if (res != NULL) {
1136adad4744SNate Lawson 	    rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
1137adad4744SNate Lawson 	    rle->res = res;
1138adad4744SNate Lawson 	} else
1139adad4744SNate Lawson 	    device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
1140adad4744SNate Lawson 		rle->start, rle->count, rle->type);
1141adad4744SNate Lawson     }
1142adad4744SNate Lawson     return (0);
1143adad4744SNate Lawson }
1144adad4744SNate Lawson 
114515e32d5dSMike Smith static struct resource *
114615e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
114715e32d5dSMike Smith     u_long start, u_long end, u_long count, u_int flags)
114815e32d5dSMike Smith {
114995957f62SJohn Baldwin     ACPI_RESOURCE ares;
115015e32d5dSMike Smith     struct acpi_device *ad = device_get_ivars(child);
115115e32d5dSMike Smith     struct resource_list *rl = &ad->ad_rl;
115291233413SNate Lawson     struct resource_list_entry *rle;
115391233413SNate Lawson     struct resource *res;
115491233413SNate Lawson     struct rman *rm;
115515e32d5dSMike Smith 
115615e2f34fSNate Lawson     res = NULL;
1157397c30a8SJohn Baldwin 
1158397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1159397c30a8SJohn Baldwin     switch (type) {
1160397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1161397c30a8SJohn Baldwin 	rm = &acpi_rman_io;
1162397c30a8SJohn Baldwin 	break;
1163397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1164397c30a8SJohn Baldwin 	rm = &acpi_rman_mem;
1165397c30a8SJohn Baldwin 	break;
1166397c30a8SJohn Baldwin     default:
1167397c30a8SJohn Baldwin 	rm = NULL;
1168397c30a8SJohn Baldwin     }
1169397c30a8SJohn Baldwin 
117015e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
117115e2f34fSNate Lawson 
117291233413SNate Lawson     /*
117391233413SNate Lawson      * If this is an allocation of the "default" range for a given RID, and
117491233413SNate Lawson      * we know what the resources for this device are (i.e., they're on the
117591233413SNate Lawson      * child's resource list), use those start/end values.
117691233413SNate Lawson      */
1177f09aa88cSWarner Losh     if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) {
117891233413SNate Lawson 	rle = resource_list_find(rl, type, *rid);
117991233413SNate Lawson 	if (rle == NULL)
118015e2f34fSNate Lawson 	    goto out;
118191233413SNate Lawson 	start = rle->start;
118291233413SNate Lawson 	end = rle->end;
118391233413SNate Lawson 	count = rle->count;
118491233413SNate Lawson     }
118591233413SNate Lawson 
1186397c30a8SJohn Baldwin     /*
1187397c30a8SJohn Baldwin      * If this is an allocation of a specific range, see if we can satisfy
1188397c30a8SJohn Baldwin      * the request from our system resource regions.  If we can't, pass the
1189397c30a8SJohn Baldwin      * request up to the parent.
1190397c30a8SJohn Baldwin      */
1191147c0ad0SJohn Baldwin     if (start + count - 1 == end && rm != NULL)
1192397c30a8SJohn Baldwin 	res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1193397c30a8SJohn Baldwin 	    child);
1194397c30a8SJohn Baldwin     if (res == NULL) {
119595957f62SJohn Baldwin 	res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
119695957f62SJohn Baldwin 	    start, end, count, flags);
119795957f62SJohn Baldwin     } else {
1198c3f861f4SWarner Losh 	rman_set_rid(res, *rid);
119991233413SNate Lawson 
120091233413SNate Lawson 	/* If requested, activate the resource using the parent's method. */
120195957f62SJohn Baldwin 	if (flags & RF_ACTIVE)
120291233413SNate Lawson 	    if (bus_activate_resource(child, type, *rid, res) != 0) {
120391233413SNate Lawson 		rman_release_resource(res);
120415e2f34fSNate Lawson 		res = NULL;
120515e2f34fSNate Lawson 		goto out;
120691233413SNate Lawson 	    }
120795957f62SJohn Baldwin     }
120891233413SNate Lawson 
120995957f62SJohn Baldwin     if (res != NULL && device_get_parent(child) == bus)
121095957f62SJohn Baldwin 	switch (type) {
121195957f62SJohn Baldwin 	case SYS_RES_IRQ:
121295957f62SJohn Baldwin 	    /*
121395957f62SJohn Baldwin 	     * Since bus_config_intr() takes immediate effect, we cannot
121495957f62SJohn Baldwin 	     * configure the interrupt associated with a device when we
121595957f62SJohn Baldwin 	     * parse the resources but have to defer it until a driver
121695957f62SJohn Baldwin 	     * actually allocates the interrupt via bus_alloc_resource().
121795957f62SJohn Baldwin 	     *
121895957f62SJohn Baldwin 	     * XXX: Should we handle the lookup failing?
121995957f62SJohn Baldwin 	     */
122095957f62SJohn Baldwin 	    if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
122195957f62SJohn Baldwin 		acpi_config_intr(child, &ares);
122295957f62SJohn Baldwin 	    break;
122395957f62SJohn Baldwin 	}
122415e2f34fSNate Lawson 
122515e2f34fSNate Lawson out:
122615e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
122791233413SNate Lawson     return (res);
122815e32d5dSMike Smith }
122915e32d5dSMike Smith 
123015e32d5dSMike Smith static int
123191233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid,
123291233413SNate Lawson     struct resource *r)
123315e32d5dSMike Smith {
1234397c30a8SJohn Baldwin     struct rman *rm;
123591233413SNate Lawson     int ret;
123615e32d5dSMike Smith 
1237397c30a8SJohn Baldwin     /* We only handle memory and IO resources through rman. */
1238397c30a8SJohn Baldwin     switch (type) {
1239397c30a8SJohn Baldwin     case SYS_RES_IOPORT:
1240397c30a8SJohn Baldwin 	rm = &acpi_rman_io;
1241397c30a8SJohn Baldwin 	break;
1242397c30a8SJohn Baldwin     case SYS_RES_MEMORY:
1243397c30a8SJohn Baldwin 	rm = &acpi_rman_mem;
1244397c30a8SJohn Baldwin 	break;
1245397c30a8SJohn Baldwin     default:
1246397c30a8SJohn Baldwin 	rm = NULL;
1247397c30a8SJohn Baldwin     }
1248397c30a8SJohn Baldwin 
124915e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
125015e2f34fSNate Lawson 
125191233413SNate Lawson     /*
1252397c30a8SJohn Baldwin      * If this resource belongs to one of our internal managers,
1253397c30a8SJohn Baldwin      * deactivate it and release it to the local pool.  If it doesn't,
1254397c30a8SJohn Baldwin      * pass this request up to the parent.
125591233413SNate Lawson      */
1256397c30a8SJohn Baldwin     if (rm != NULL && rman_is_region_manager(r, rm)) {
125791233413SNate Lawson 	if (rman_get_flags(r) & RF_ACTIVE) {
125891233413SNate Lawson 	    ret = bus_deactivate_resource(child, type, rid, r);
125991233413SNate Lawson 	    if (ret != 0)
126015e2f34fSNate Lawson 		goto out;
126191233413SNate Lawson 	}
126291233413SNate Lawson 	ret = rman_release_resource(r);
126391233413SNate Lawson     } else
126491233413SNate Lawson 	ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
126591233413SNate Lawson 
126615e2f34fSNate Lawson out:
126715e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
126891233413SNate Lawson     return (ret);
126915e32d5dSMike Smith }
127015e32d5dSMike Smith 
12718b28b622SNate Lawson static void
12728b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid)
12738b28b622SNate Lawson {
12748b28b622SNate Lawson     struct resource_list *rl;
12758b28b622SNate Lawson 
12768b28b622SNate Lawson     rl = acpi_get_rlist(bus, child);
12778b28b622SNate Lawson     resource_list_delete(rl, type, rid);
12788b28b622SNate Lawson }
12798b28b622SNate Lawson 
1280dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */
1281e1c4bf3fSNate Lawson int
1282e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
1283907b6777SNate Lawson     struct resource **res, u_int flags)
1284dc750869SNate Lawson {
1285e1c4bf3fSNate Lawson     int error, res_type;
1286dc750869SNate Lawson 
1287e1c4bf3fSNate Lawson     error = ENOMEM;
12888f118e25SNate Lawson     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
1289e1c4bf3fSNate Lawson 	return (EINVAL);
1290dc750869SNate Lawson 
12918f118e25SNate Lawson     /* We only support memory and IO spaces. */
12922be4e471SJung-uk Kim     switch (gas->SpaceId) {
1293dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
1294e1c4bf3fSNate Lawson 	res_type = SYS_RES_MEMORY;
1295dc750869SNate Lawson 	break;
1296dc750869SNate Lawson     case ACPI_ADR_SPACE_SYSTEM_IO:
1297e1c4bf3fSNate Lawson 	res_type = SYS_RES_IOPORT;
1298dc750869SNate Lawson 	break;
1299dc750869SNate Lawson     default:
1300e1c4bf3fSNate Lawson 	return (EOPNOTSUPP);
1301dc750869SNate Lawson     }
1302dc750869SNate Lawson 
13032fe912dfSNate Lawson     /*
1304f45fc848SNate Lawson      * If the register width is less than 8, assume the BIOS author means
1305f45fc848SNate Lawson      * it is a bit field and just allocate a byte.
13062fe912dfSNate Lawson      */
13072be4e471SJung-uk Kim     if (gas->BitWidth && gas->BitWidth < 8)
13082be4e471SJung-uk Kim 	gas->BitWidth = 8;
13092fe912dfSNate Lawson 
13108f118e25SNate Lawson     /* Validate the address after we're sure we support the space. */
13112be4e471SJung-uk Kim     if (gas->Address == 0 || gas->BitWidth == 0)
13128f118e25SNate Lawson 	return (EINVAL);
13138f118e25SNate Lawson 
1314e1c4bf3fSNate Lawson     bus_set_resource(dev, res_type, *rid, gas->Address,
13152be4e471SJung-uk Kim 	gas->BitWidth / 8);
1316907b6777SNate Lawson     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags);
1317e1c4bf3fSNate Lawson     if (*res != NULL) {
1318e1c4bf3fSNate Lawson 	*type = res_type;
1319e1c4bf3fSNate Lawson 	error = 0;
1320ca2c69c8SNate Lawson     } else
1321ca2c69c8SNate Lawson 	bus_delete_resource(dev, res_type, *rid);
1322ca2c69c8SNate Lawson 
1323e1c4bf3fSNate Lawson     return (error);
1324dc750869SNate Lawson }
1325dc750869SNate Lawson 
1326c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */
13271e4925e8SNate Lawson static uint32_t
132832d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev)
1329bc0f2195SMike Smith {
13301e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1331bc0f2195SMike Smith     ACPI_HANDLE		h;
133292488a57SJung-uk Kim     uint32_t		pnpid;
133332d18aa5SMike Smith 
1334b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
133532d18aa5SMike Smith 
13366fca9360SNate Lawson     /* Fetch and validate the HID. */
133792488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
133892488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
133992488a57SJung-uk Kim 	return_VALUE (0);
134032d18aa5SMike Smith 
134192488a57SJung-uk Kim     pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 &&
134292488a57SJung-uk Kim 	devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ?
134392488a57SJung-uk Kim 	PNP_EISAID(devinfo->HardwareId.String) : 0;
134492488a57SJung-uk Kim     AcpiOsFree(devinfo);
1345be2b1797SNate Lawson 
134632d18aa5SMike Smith     return_VALUE (pnpid);
134732d18aa5SMike Smith }
134832d18aa5SMike Smith 
13491e4925e8SNate Lawson static int
13501e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
1351b2566207STakanori Watanabe {
13521e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
135392488a57SJung-uk Kim     ACPI_DEVICE_ID	*ids;
1354b2566207STakanori Watanabe     ACPI_HANDLE		h;
13551e4925e8SNate Lawson     uint32_t		*pnpid;
135692488a57SJung-uk Kim     int			i, valid;
1357b2566207STakanori Watanabe 
1358b2566207STakanori Watanabe     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1359b2566207STakanori Watanabe 
13601e4925e8SNate Lawson     pnpid = cids;
1361bd189fe7SNate Lawson 
13621e4925e8SNate Lawson     /* Fetch and validate the CID */
136392488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
136492488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
136592488a57SJung-uk Kim 	return_VALUE (0);
1366b2566207STakanori Watanabe 
136792488a57SJung-uk Kim     if ((devinfo->Valid & ACPI_VALID_CID) == 0) {
136892488a57SJung-uk Kim 	AcpiOsFree(devinfo);
136992488a57SJung-uk Kim 	return_VALUE (0);
1370b2566207STakanori Watanabe     }
1371b2566207STakanori Watanabe 
137292488a57SJung-uk Kim     if (devinfo->CompatibleIdList.Count < count)
137392488a57SJung-uk Kim 	count = devinfo->CompatibleIdList.Count;
137492488a57SJung-uk Kim     ids = devinfo->CompatibleIdList.Ids;
137592488a57SJung-uk Kim     for (i = 0, valid = 0; i < count; i++)
137692488a57SJung-uk Kim 	if (ids[i].Length >= ACPI_EISAID_STRING_SIZE &&
137792488a57SJung-uk Kim 	    strncmp(ids[i].String, "PNP", 3) == 0) {
137892488a57SJung-uk Kim 	    *pnpid++ = PNP_EISAID(ids[i].String);
137992488a57SJung-uk Kim 	    valid++;
138092488a57SJung-uk Kim 	}
138192488a57SJung-uk Kim     AcpiOsFree(devinfo);
138292488a57SJung-uk Kim 
13831e4925e8SNate Lawson     return_VALUE (valid);
13841e4925e8SNate Lawson }
1385b2566207STakanori Watanabe 
1386ce619b2eSNate Lawson static char *
1387ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids)
1388ce619b2eSNate Lawson {
1389ce619b2eSNate Lawson     ACPI_HANDLE h;
139092488a57SJung-uk Kim     ACPI_OBJECT_TYPE t;
1391ce619b2eSNate Lawson     int i;
1392ce619b2eSNate Lawson 
1393ce619b2eSNate Lawson     h = acpi_get_handle(dev);
139492488a57SJung-uk Kim     if (ids == NULL || h == NULL)
139592488a57SJung-uk Kim 	return (NULL);
139692488a57SJung-uk Kim     t = acpi_get_type(dev);
139792488a57SJung-uk Kim     if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR)
1398ce619b2eSNate Lawson 	return (NULL);
1399ce619b2eSNate Lawson 
1400ce619b2eSNate Lawson     /* Try to match one of the array of IDs with a HID or CID. */
1401ce619b2eSNate Lawson     for (i = 0; ids[i] != NULL; i++) {
1402ce619b2eSNate Lawson 	if (acpi_MatchHid(h, ids[i]))
1403ce619b2eSNate Lawson 	    return (ids[i]);
1404ce619b2eSNate Lawson     }
1405ce619b2eSNate Lawson     return (NULL);
1406ce619b2eSNate Lawson }
1407ce619b2eSNate Lawson 
1408ce619b2eSNate Lawson static ACPI_STATUS
1409ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
1410ce619b2eSNate Lawson     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
1411ce619b2eSNate Lawson {
1412ce619b2eSNate Lawson     ACPI_HANDLE h;
1413ce619b2eSNate Lawson 
1414df8d2a32SNate Lawson     if (dev == NULL)
1415df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1416df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1417ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1418ce619b2eSNate Lawson     return (AcpiEvaluateObject(h, pathname, parameters, ret));
1419ce619b2eSNate Lawson }
1420ce619b2eSNate Lawson 
142110ce62b9SNate Lawson static int
142210ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
142310ce62b9SNate Lawson {
142410ce62b9SNate Lawson     struct acpi_softc *sc;
142510ce62b9SNate Lawson     ACPI_HANDLE handle;
142610ce62b9SNate Lawson     ACPI_STATUS status;
142710ce62b9SNate Lawson     char sxd[8];
142810ce62b9SNate Lawson     int error;
142910ce62b9SNate Lawson 
143010ce62b9SNate Lawson     sc = device_get_softc(bus);
143110ce62b9SNate Lawson     handle = acpi_get_handle(dev);
143210ce62b9SNate Lawson 
143310ce62b9SNate Lawson     /*
143410ce62b9SNate Lawson      * XXX If we find these devices, don't try to power them down.
143510ce62b9SNate Lawson      * The serial and IRDA ports on my T23 hang the system when
143610ce62b9SNate Lawson      * set to D3 and it appears that such legacy devices may
143710ce62b9SNate Lawson      * need special handling in their drivers.
143810ce62b9SNate Lawson      */
143910ce62b9SNate Lawson     if (handle == NULL ||
144010ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0500") ||
144110ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0501") ||
144210ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0502") ||
144310ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0510") ||
144410ce62b9SNate Lawson 	acpi_MatchHid(handle, "PNP0511"))
144510ce62b9SNate Lawson 	return (ENXIO);
144610ce62b9SNate Lawson 
144710ce62b9SNate Lawson     /*
144810ce62b9SNate Lawson      * Override next state with the value from _SxD, if present.  If no
144910ce62b9SNate Lawson      * dstate argument was provided, don't fetch the return value.
145010ce62b9SNate Lawson      */
145110ce62b9SNate Lawson     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
145210ce62b9SNate Lawson     if (dstate)
145310ce62b9SNate Lawson 	status = acpi_GetInteger(handle, sxd, dstate);
145410ce62b9SNate Lawson     else
145510ce62b9SNate Lawson 	status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
145610ce62b9SNate Lawson 
145710ce62b9SNate Lawson     switch (status) {
145810ce62b9SNate Lawson     case AE_OK:
145910ce62b9SNate Lawson 	error = 0;
146010ce62b9SNate Lawson 	break;
146110ce62b9SNate Lawson     case AE_NOT_FOUND:
146210ce62b9SNate Lawson 	error = ESRCH;
146310ce62b9SNate Lawson 	break;
146410ce62b9SNate Lawson     default:
146510ce62b9SNate Lawson 	error = ENXIO;
146610ce62b9SNate Lawson 	break;
146710ce62b9SNate Lawson     }
146810ce62b9SNate Lawson 
146910ce62b9SNate Lawson     return (error);
147010ce62b9SNate Lawson }
147110ce62b9SNate Lawson 
1472df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */
1473df8d2a32SNate Lawson struct acpi_device_scan_ctx {
1474df8d2a32SNate Lawson     acpi_scan_cb_t	user_fn;
1475df8d2a32SNate Lawson     void		*arg;
1476df8d2a32SNate Lawson     ACPI_HANDLE		parent;
1477df8d2a32SNate Lawson };
1478df8d2a32SNate Lawson 
1479ce619b2eSNate Lawson static ACPI_STATUS
1480df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
1481df8d2a32SNate Lawson {
1482df8d2a32SNate Lawson     struct acpi_device_scan_ctx *ctx;
1483df8d2a32SNate Lawson     device_t dev, old_dev;
1484df8d2a32SNate Lawson     ACPI_STATUS status;
1485df8d2a32SNate Lawson     ACPI_OBJECT_TYPE type;
1486df8d2a32SNate Lawson 
1487df8d2a32SNate Lawson     /*
1488df8d2a32SNate Lawson      * Skip this device if we think we'll have trouble with it or it is
1489df8d2a32SNate Lawson      * the parent where the scan began.
1490df8d2a32SNate Lawson      */
1491df8d2a32SNate Lawson     ctx = (struct acpi_device_scan_ctx *)arg;
1492df8d2a32SNate Lawson     if (acpi_avoid(h) || h == ctx->parent)
1493df8d2a32SNate Lawson 	return (AE_OK);
1494df8d2a32SNate Lawson 
1495df8d2a32SNate Lawson     /* If this is not a valid device type (e.g., a method), skip it. */
1496df8d2a32SNate Lawson     if (ACPI_FAILURE(AcpiGetType(h, &type)))
1497df8d2a32SNate Lawson 	return (AE_OK);
1498df8d2a32SNate Lawson     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
1499df8d2a32SNate Lawson 	type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
1500df8d2a32SNate Lawson 	return (AE_OK);
1501df8d2a32SNate Lawson 
1502df8d2a32SNate Lawson     /*
1503df8d2a32SNate Lawson      * Call the user function with the current device.  If it is unchanged
1504df8d2a32SNate Lawson      * afterwards, return.  Otherwise, we update the handle to the new dev.
1505df8d2a32SNate Lawson      */
1506df8d2a32SNate Lawson     old_dev = acpi_get_device(h);
1507df8d2a32SNate Lawson     dev = old_dev;
1508df8d2a32SNate Lawson     status = ctx->user_fn(h, &dev, level, ctx->arg);
1509df8d2a32SNate Lawson     if (ACPI_FAILURE(status) || old_dev == dev)
1510df8d2a32SNate Lawson 	return (status);
1511df8d2a32SNate Lawson 
1512df8d2a32SNate Lawson     /* Remove the old child and its connection to the handle. */
1513df8d2a32SNate Lawson     if (old_dev != NULL) {
1514df8d2a32SNate Lawson 	device_delete_child(device_get_parent(old_dev), old_dev);
1515df8d2a32SNate Lawson 	AcpiDetachData(h, acpi_fake_objhandler);
1516df8d2a32SNate Lawson     }
1517df8d2a32SNate Lawson 
1518df8d2a32SNate Lawson     /* Recreate the handle association if the user created a device. */
1519df8d2a32SNate Lawson     if (dev != NULL)
1520df8d2a32SNate Lawson 	AcpiAttachData(h, acpi_fake_objhandler, dev);
1521df8d2a32SNate Lawson 
1522df8d2a32SNate Lawson     return (AE_OK);
1523df8d2a32SNate Lawson }
1524df8d2a32SNate Lawson 
1525df8d2a32SNate Lawson static ACPI_STATUS
1526df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
1527df8d2a32SNate Lawson     acpi_scan_cb_t user_fn, void *arg)
1528ce619b2eSNate Lawson {
1529ce619b2eSNate Lawson     ACPI_HANDLE h;
1530df8d2a32SNate Lawson     struct acpi_device_scan_ctx ctx;
1531ce619b2eSNate Lawson 
1532df8d2a32SNate Lawson     if (acpi_disabled("children"))
1533df8d2a32SNate Lawson 	return (AE_OK);
1534df8d2a32SNate Lawson 
1535df8d2a32SNate Lawson     if (dev == NULL)
1536df8d2a32SNate Lawson 	h = ACPI_ROOT_OBJECT;
1537df8d2a32SNate Lawson     else if ((h = acpi_get_handle(dev)) == NULL)
1538ce619b2eSNate Lawson 	return (AE_BAD_PARAMETER);
1539df8d2a32SNate Lawson     ctx.user_fn = user_fn;
1540df8d2a32SNate Lawson     ctx.arg = arg;
1541df8d2a32SNate Lawson     ctx.parent = h;
1542df8d2a32SNate Lawson     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
15432272d050SJung-uk Kim 	acpi_device_scan_cb, NULL, &ctx, NULL));
1544ce619b2eSNate Lawson }
1545ce619b2eSNate Lawson 
154610ce62b9SNate Lawson /*
154710ce62b9SNate Lawson  * Even though ACPI devices are not PCI, we use the PCI approach for setting
154810ce62b9SNate Lawson  * device power states since it's close enough to ACPI.
154910ce62b9SNate Lawson  */
155010ce62b9SNate Lawson static int
155110ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state)
155210ce62b9SNate Lawson {
155310ce62b9SNate Lawson     ACPI_HANDLE h;
155410ce62b9SNate Lawson     ACPI_STATUS status;
155510ce62b9SNate Lawson     int error;
155610ce62b9SNate Lawson 
155710ce62b9SNate Lawson     error = 0;
155810ce62b9SNate Lawson     h = acpi_get_handle(child);
1559a0e73a12SJung-uk Kim     if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX)
156010ce62b9SNate Lawson 	return (EINVAL);
156110ce62b9SNate Lawson     if (h == NULL)
156210ce62b9SNate Lawson 	return (0);
156310ce62b9SNate Lawson 
156410ce62b9SNate Lawson     /* Ignore errors if the power methods aren't present. */
156510ce62b9SNate Lawson     status = acpi_pwr_switch_consumer(h, state);
156610ce62b9SNate Lawson     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
156710ce62b9SNate Lawson 	&& status != AE_BAD_PARAMETER)
156810ce62b9SNate Lawson 	device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
156910ce62b9SNate Lawson 	    state, acpi_name(h), AcpiFormatException(status));
157010ce62b9SNate Lawson 
157110ce62b9SNate Lawson     return (error);
157210ce62b9SNate Lawson }
157310ce62b9SNate Lawson 
157432d18aa5SMike Smith static int
157532d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
157632d18aa5SMike Smith {
15771e4925e8SNate Lawson     int			result, cid_count, i;
15781e4925e8SNate Lawson     uint32_t		lid, cids[8];
1579bc0f2195SMike Smith 
1580b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1581bc0f2195SMike Smith 
1582bc0f2195SMike Smith     /*
1583bc0f2195SMike Smith      * ISA-style drivers attached to ACPI may persist and
1584bc0f2195SMike Smith      * probe manually if we return ENOENT.  We never want
1585bc0f2195SMike Smith      * that to happen, so don't ever return it.
1586bc0f2195SMike Smith      */
1587bc0f2195SMike Smith     result = ENXIO;
1588bc0f2195SMike Smith 
1589be2b1797SNate Lawson     /* Scan the supplied IDs for a match */
1590b2566207STakanori Watanabe     lid = acpi_isa_get_logicalid(child);
15911e4925e8SNate Lawson     cid_count = acpi_isa_get_compatid(child, cids, 8);
1592bc0f2195SMike Smith     while (ids && ids->ip_id) {
15931e4925e8SNate Lawson 	if (lid == ids->ip_id) {
1594bc0f2195SMike Smith 	    result = 0;
1595bc0f2195SMike Smith 	    goto out;
1596bc0f2195SMike Smith 	}
15971e4925e8SNate Lawson 	for (i = 0; i < cid_count; i++) {
15981e4925e8SNate Lawson 	    if (cids[i] == ids->ip_id) {
15991e4925e8SNate Lawson 		result = 0;
16001e4925e8SNate Lawson 		goto out;
16011e4925e8SNate Lawson 	    }
16021e4925e8SNate Lawson 	}
1603bc0f2195SMike Smith 	ids++;
1604bc0f2195SMike Smith     }
1605be2b1797SNate Lawson 
1606bc0f2195SMike Smith  out:
16079e0dd54fSNate Lawson     if (result == 0 && ids->ip_desc)
16089e0dd54fSNate Lawson 	device_set_desc(child, ids->ip_desc);
16099e0dd54fSNate Lawson 
1610bc0f2195SMike Smith     return_VALUE (result);
1611bc0f2195SMike Smith }
1612bc0f2195SMike Smith 
1613d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__)
1614d320e05cSJohn Baldwin /*
1615d320e05cSJohn Baldwin  * Look for a MCFG table.  If it is present, use the settings for
1616d320e05cSJohn Baldwin  * domain (segment) 0 to setup PCI config space access via the memory
1617d320e05cSJohn Baldwin  * map.
1618d320e05cSJohn Baldwin  */
1619d320e05cSJohn Baldwin static void
1620d320e05cSJohn Baldwin acpi_enable_pcie(void)
1621d320e05cSJohn Baldwin {
1622d320e05cSJohn Baldwin 	ACPI_TABLE_HEADER *hdr;
1623d320e05cSJohn Baldwin 	ACPI_MCFG_ALLOCATION *alloc, *end;
1624d320e05cSJohn Baldwin 	ACPI_STATUS status;
1625d320e05cSJohn Baldwin 
1626d320e05cSJohn Baldwin 	status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
1627d320e05cSJohn Baldwin 	if (ACPI_FAILURE(status))
1628d320e05cSJohn Baldwin 		return;
1629d320e05cSJohn Baldwin 
1630d320e05cSJohn Baldwin 	end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length);
1631d320e05cSJohn Baldwin 	alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1);
1632d320e05cSJohn Baldwin 	while (alloc < end) {
1633d320e05cSJohn Baldwin 		if (alloc->PciSegment == 0) {
1634d320e05cSJohn Baldwin 			pcie_cfgregopen(alloc->Address, alloc->StartBusNumber,
1635d320e05cSJohn Baldwin 			    alloc->EndBusNumber);
1636d320e05cSJohn Baldwin 			return;
1637d320e05cSJohn Baldwin 		}
1638d320e05cSJohn Baldwin 		alloc++;
1639d320e05cSJohn Baldwin 	}
1640d320e05cSJohn Baldwin }
1641d320e05cSJohn Baldwin #endif
1642d320e05cSJohn Baldwin 
1643bc0f2195SMike Smith /*
164433332dc2SNate Lawson  * Scan all of the ACPI namespace and attach child devices.
164515e32d5dSMike Smith  *
164633332dc2SNate Lawson  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
164733332dc2SNate Lawson  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
164833332dc2SNate Lawson  * However, in violation of the spec, some systems place their PCI link
164933332dc2SNate Lawson  * devices in \, so we have to walk the whole namespace.  We check the
165033332dc2SNate Lawson  * type of namespace nodes, so this should be ok.
165115e32d5dSMike Smith  */
165215e32d5dSMike Smith static void
165315e32d5dSMike Smith acpi_probe_children(device_t bus)
165415e32d5dSMike Smith {
165515e32d5dSMike Smith 
1656b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
16570ae55423SMike Smith 
165815e32d5dSMike Smith     /*
165915e32d5dSMike Smith      * Scan the namespace and insert placeholders for all the devices that
1660edc13633SNate Lawson      * we find.  We also probe/attach any early devices.
166115e32d5dSMike Smith      *
166215e32d5dSMike Smith      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
1663be2b1797SNate Lawson      * we want to create nodes for all devices, not just those that are
1664be2b1797SNate Lawson      * currently present. (This assumes that we don't want to create/remove
1665be2b1797SNate Lawson      * devices as they appear, which might be smarter.)
166615e32d5dSMike Smith      */
16674c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
166833332dc2SNate Lawson     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
16692272d050SJung-uk Kim 	NULL, bus, NULL);
167015e32d5dSMike Smith 
1671adad4744SNate Lawson     /* Pre-allocate resources for our rman from any sysresource devices. */
1672adad4744SNate Lawson     acpi_sysres_alloc(bus);
1673adad4744SNate Lawson 
1674edc13633SNate Lawson     /* Create any static children by calling device identify methods. */
1675edc13633SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
1676edc13633SNate Lawson     bus_generic_probe(bus);
1677edc13633SNate Lawson 
1678edc13633SNate Lawson     /* Probe/attach all children, created staticly and from the namespace. */
16790430ba9eSAndriy Gapon     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n"));
168015e32d5dSMike Smith     bus_generic_attach(bus);
16810ae55423SMike Smith 
168288a79fc0SNate Lawson     /* Attach wake sysctls. */
16835c9ea25eSNate Lawson     acpi_wake_sysctl_walk(bus);
168488a79fc0SNate Lawson 
1685bc0f2195SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
16860ae55423SMike Smith     return_VOID;
168715e32d5dSMike Smith }
168815e32d5dSMike Smith 
1689b82ca9a9SNate Lawson /*
1690d227204dSJohn Baldwin  * Determine the probe order for a given device.
1691b82ca9a9SNate Lawson  */
1692d227204dSJohn Baldwin static void
1693b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order)
169491233413SNate Lawson {
1695463e0f91SJohn Baldwin     ACPI_OBJECT_TYPE type;
169691233413SNate Lawson 
1697b82ca9a9SNate Lawson     /*
1698b82ca9a9SNate Lawson      * 1. I/O port and memory system resource holders
1699b82ca9a9SNate Lawson      * 2. Embedded controllers (to handle early accesses)
1700b460c6f8SJohn Baldwin      * 3. PCI Link Devices
1701b6618687SJohn Baldwin      * 100000. CPUs
1702b82ca9a9SNate Lawson      */
1703463e0f91SJohn Baldwin     AcpiGetType(handle, &type);
1704aa835160SAndriy Gapon     if (type == ACPI_TYPE_PROCESSOR)
170591233413SNate Lawson 	*order = 1;
1706aa835160SAndriy Gapon     else if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02"))
170791233413SNate Lawson 	*order = 2;
1708aa835160SAndriy Gapon     else if (acpi_MatchHid(handle, "PNP0C09"))
1709b460c6f8SJohn Baldwin 	*order = 3;
1710aa835160SAndriy Gapon     else if (acpi_MatchHid(handle, "PNP0C0F"))
1711aa835160SAndriy Gapon 	*order = 4;
171291233413SNate Lawson }
171391233413SNate Lawson 
171415e32d5dSMike Smith /*
171515e32d5dSMike Smith  * Evaluate a child device and determine whether we might attach a device to
171615e32d5dSMike Smith  * it.
171715e32d5dSMike Smith  */
171815e32d5dSMike Smith static ACPI_STATUS
171915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
172015e32d5dSMike Smith {
172115e32d5dSMike Smith     ACPI_OBJECT_TYPE type;
1722858a52f4SMitsuru IWASAKI     ACPI_HANDLE h;
172333332dc2SNate Lawson     device_t bus, child;
1724da72d149SNate Lawson     int order;
172533332dc2SNate Lawson     char *handle_str, **search;
172633332dc2SNate Lawson     static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
172715e32d5dSMike Smith 
1728b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
17290ae55423SMike Smith 
1730be2b1797SNate Lawson     /* Skip this device if we think we'll have trouble with it. */
17310ae55423SMike Smith     if (acpi_avoid(handle))
17320ae55423SMike Smith 	return_ACPI_STATUS (AE_OK);
17330ae55423SMike Smith 
173491233413SNate Lawson     bus = (device_t)context;
1735b53f2771SMike Smith     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
173615e32d5dSMike Smith 	switch (type) {
173715e32d5dSMike Smith 	case ACPI_TYPE_DEVICE:
173815e32d5dSMike Smith 	case ACPI_TYPE_PROCESSOR:
173915e32d5dSMike Smith 	case ACPI_TYPE_THERMAL:
174015e32d5dSMike Smith 	case ACPI_TYPE_POWER:
17410ae55423SMike Smith 	    if (acpi_disabled("children"))
17420ae55423SMike Smith 		break;
1743be2b1797SNate Lawson 
174415e32d5dSMike Smith 	    /*
174533332dc2SNate Lawson 	     * Since we scan from \, be sure to skip system scope objects.
174633332dc2SNate Lawson 	     * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
174733332dc2SNate Lawson 	     */
174833332dc2SNate Lawson 	    handle_str = acpi_name(handle);
174933332dc2SNate Lawson 	    for (search = scopes; *search != NULL; search++) {
175033332dc2SNate Lawson 		if (strcmp(handle_str, *search) == 0)
175133332dc2SNate Lawson 		    break;
175233332dc2SNate Lawson 	    }
175333332dc2SNate Lawson 	    if (*search != NULL)
175433332dc2SNate Lawson 		break;
175533332dc2SNate Lawson 
175633332dc2SNate Lawson 	    /*
1757463e0f91SJohn Baldwin 	     * Create a placeholder device for this node.  Sort the
1758463e0f91SJohn Baldwin 	     * placeholder so that the probe/attach passes will run
1759463e0f91SJohn Baldwin 	     * breadth-first.  Orders less than ACPI_DEV_BASE_ORDER
1760463e0f91SJohn Baldwin 	     * are reserved for special objects (i.e., system
1761b6618687SJohn Baldwin 	     * resources).  CPU devices have a very high order to
1762b6618687SJohn Baldwin 	     * ensure they are probed after other devices.
176315e32d5dSMike Smith 	     */
176433332dc2SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
1765d227204dSJohn Baldwin 	    order = level * 10 + 100;
1766da72d149SNate Lawson 	    acpi_probe_order(handle, &order);
176791233413SNate Lawson 	    child = BUS_ADD_CHILD(bus, order, NULL, -1);
1768bc0f2195SMike Smith 	    if (child == NULL)
1769bc0f2195SMike Smith 		break;
177059a890e6SNate Lawson 
177159a890e6SNate Lawson 	    /* Associate the handle with the device_t and vice versa. */
177215e32d5dSMike Smith 	    acpi_set_handle(child, handle);
177359a890e6SNate Lawson 	    AcpiAttachData(handle, acpi_fake_objhandler, child);
1774bc0f2195SMike Smith 
1775bc0f2195SMike Smith 	    /*
1776b519f9d6SMike Smith 	     * Check that the device is present.  If it's not present,
1777b519f9d6SMike Smith 	     * leave it disabled (so that we have a device_t attached to
1778b519f9d6SMike Smith 	     * the handle, but we don't probe it).
1779893f750aSNate Lawson 	     *
1780893f750aSNate Lawson 	     * XXX PCI link devices sometimes report "present" but not
1781893f750aSNate Lawson 	     * "functional" (i.e. if disabled).  Go ahead and probe them
1782893f750aSNate Lawson 	     * anyway since we may enable them later.
1783b519f9d6SMike Smith 	     */
1784858a52f4SMitsuru IWASAKI 	    if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
1785858a52f4SMitsuru IWASAKI 		/* Never disable PCI link devices. */
1786858a52f4SMitsuru IWASAKI 		if (acpi_MatchHid(handle, "PNP0C0F"))
1787858a52f4SMitsuru IWASAKI 		    break;
1788858a52f4SMitsuru IWASAKI 		/*
1789858a52f4SMitsuru IWASAKI 		 * Docking stations should remain enabled since the system
1790858a52f4SMitsuru IWASAKI 		 * may be undocked at boot.
1791858a52f4SMitsuru IWASAKI 		 */
1792858a52f4SMitsuru IWASAKI 		if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h)))
1793858a52f4SMitsuru IWASAKI 		    break;
1794858a52f4SMitsuru IWASAKI 
1795b519f9d6SMike Smith 		device_disable(child);
1796b519f9d6SMike Smith 		break;
1797b519f9d6SMike Smith 	    }
1798b519f9d6SMike Smith 
1799b519f9d6SMike Smith 	    /*
1800bc0f2195SMike Smith 	     * Get the device's resource settings and attach them.
1801bc0f2195SMike Smith 	     * Note that if the device has _PRS but no _CRS, we need
1802bc0f2195SMike Smith 	     * to decide when it's appropriate to try to configure the
1803bc0f2195SMike Smith 	     * device.  Ignore the return value here; it's OK for the
1804bc0f2195SMike Smith 	     * device not to have any resources.
1805bc0f2195SMike Smith 	     */
180672ad60adSNate Lawson 	    acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
18070ae55423SMike Smith 	    break;
180815e32d5dSMike Smith 	}
180915e32d5dSMike Smith     }
1810be2b1797SNate Lawson 
18110ae55423SMike Smith     return_ACPI_STATUS (AE_OK);
181215e32d5dSMike Smith }
181315e32d5dSMike Smith 
181459a890e6SNate Lawson /*
181559a890e6SNate Lawson  * AcpiAttachData() requires an object handler but never uses it.  This is a
181659a890e6SNate Lawson  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
181759a890e6SNate Lawson  */
181859a890e6SNate Lawson void
181992488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data)
182059a890e6SNate Lawson {
182159a890e6SNate Lawson }
182259a890e6SNate Lawson 
182315e32d5dSMike Smith static void
182415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto)
182515e32d5dSMike Smith {
18262d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
1827d33f4987STakanori Watanabe     ACPI_STATUS status;
1828eeb3a05fSNate Lawson 
1829eeb3a05fSNate Lawson     /*
183080f0e4c2SNate Lawson      * XXX Shutdown code should only run on the BSP (cpuid 0).
183180f0e4c2SNate Lawson      * Some chipsets do not power off the system correctly if called from
183280f0e4c2SNate Lawson      * an AP.
1833eeb3a05fSNate Lawson      */
1834eeb3a05fSNate Lawson     if ((howto & RB_POWEROFF) != 0) {
1835904bf0c2SNate Lawson 	status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1836904bf0c2SNate Lawson 	if (ACPI_FAILURE(status)) {
18372d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
1838904bf0c2SNate Lawson 		AcpiFormatException(status));
1839904bf0c2SNate Lawson 	    return;
1840904bf0c2SNate Lawson 	}
18412d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Powering system off\n");
184255398047SNate Lawson 	ACPI_DISABLE_IRQS();
1843865b8d0bSNate Lawson 	status = AcpiEnterSleepState(ACPI_STATE_S5);
18442d0c82e8SJung-uk Kim 	if (ACPI_FAILURE(status))
18452d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - %s\n",
18462d0c82e8SJung-uk Kim 		AcpiFormatException(status));
18472d0c82e8SJung-uk Kim 	else {
184815e32d5dSMike Smith 	    DELAY(1000000);
18492d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "power-off failed - timeout\n");
185015e32d5dSMike Smith 	}
18512be4e471SJung-uk Kim     } else if ((howto & RB_HALT) == 0 &&
18522be4e471SJung-uk Kim 	(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) &&
1853d1b16e18SNate Lawson 	sc->acpi_handle_reboot) {
1854d85e6785SNate Lawson 	/* Reboot using the reset register. */
1855129d3046SJung-uk Kim 	status = AcpiWrite(
18562be4e471SJung-uk Kim 	    AcpiGbl_FADT.ResetValue, &AcpiGbl_FADT.ResetRegister);
18572d0c82e8SJung-uk Kim 	if (ACPI_FAILURE(status))
18582d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - %s\n",
18592d0c82e8SJung-uk Kim 		AcpiFormatException(status));
18602d0c82e8SJung-uk Kim 	else {
186187a500cdSNate Lawson 	    DELAY(1000000);
18622d0c82e8SJung-uk Kim 	    device_printf(sc->acpi_dev, "reset failed - timeout\n");
186387a500cdSNate Lawson 	}
1864d85e6785SNate Lawson     } else if (sc->acpi_do_disable && panicstr == NULL) {
1865d85e6785SNate Lawson 	/*
1866d85e6785SNate Lawson 	 * Only disable ACPI if the user requested.  On some systems, writing
1867d85e6785SNate Lawson 	 * the disable value to SMI_CMD hangs the system.
1868d85e6785SNate Lawson 	 */
18692d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "Shutting down\n");
187080f0e4c2SNate Lawson 	AcpiTerminate();
187180f0e4c2SNate Lawson     }
187215e32d5dSMike Smith }
187315e32d5dSMike Smith 
187413d4f7baSMitsuru IWASAKI static void
187513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc)
187613d4f7baSMitsuru IWASAKI {
187713d4f7baSMitsuru IWASAKI     static int	first_time = 1;
187813d4f7baSMitsuru IWASAKI 
187913d4f7baSMitsuru IWASAKI     /* Enable and clear fixed events and install handlers. */
18802be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
188159ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
188213d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
188333febf93SNate Lawson 				     acpi_event_power_button_sleep, sc);
1884be2b1797SNate Lawson 	if (first_time)
18856c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Power Button (fixed)\n");
188613d4f7baSMitsuru IWASAKI     }
18872be4e471SJung-uk Kim     if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
188859ddeb18SNate Lawson 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
188913d4f7baSMitsuru IWASAKI 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
189033febf93SNate Lawson 				     acpi_event_sleep_button_sleep, sc);
1891be2b1797SNate Lawson 	if (first_time)
18926c0e8467SNate Lawson 	    device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
189313d4f7baSMitsuru IWASAKI     }
189413d4f7baSMitsuru IWASAKI 
189513d4f7baSMitsuru IWASAKI     first_time = 0;
189613d4f7baSMitsuru IWASAKI }
189713d4f7baSMitsuru IWASAKI 
189815e32d5dSMike Smith /*
1899c5ba0be4SMike Smith  * Returns true if the device is actually present and should
1900c5ba0be4SMike Smith  * be attached to.  This requires the present, enabled, UI-visible
1901c5ba0be4SMike Smith  * and diagnostics-passed bits to be set.
1902c5ba0be4SMike Smith  */
1903c5ba0be4SMike Smith BOOLEAN
1904c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev)
1905c5ba0be4SMike Smith {
19061e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1907c5ba0be4SMike Smith     ACPI_HANDLE		h;
190892488a57SJung-uk Kim     BOOLEAN		present;
1909c5ba0be4SMike Smith 
191092488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
191192488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1912c5ba0be4SMike Smith 	return (FALSE);
1913be2b1797SNate Lawson 
1914be2b1797SNate Lawson     /* If no _STA method, must be present */
191592488a57SJung-uk Kim     present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
191692488a57SJung-uk Kim 	ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
1917be2b1797SNate Lawson 
191892488a57SJung-uk Kim     AcpiOsFree(devinfo);
191992488a57SJung-uk Kim     return (present);
1920c5ba0be4SMike Smith }
1921c5ba0be4SMike Smith 
1922c5ba0be4SMike Smith /*
1923b53f2771SMike Smith  * Returns true if the battery is actually present and inserted.
1924b53f2771SMike Smith  */
1925b53f2771SMike Smith BOOLEAN
1926b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev)
1927b53f2771SMike Smith {
19281e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
1929b53f2771SMike Smith     ACPI_HANDLE		h;
193092488a57SJung-uk Kim     BOOLEAN		present;
1931b53f2771SMike Smith 
193292488a57SJung-uk Kim     if ((h = acpi_get_handle(dev)) == NULL ||
193392488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
1934b53f2771SMike Smith 	return (FALSE);
1935be2b1797SNate Lawson 
1936be2b1797SNate Lawson     /* If no _STA method, must be present */
193792488a57SJung-uk Kim     present = (devinfo->Valid & ACPI_VALID_STA) == 0 ||
193892488a57SJung-uk Kim 	ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE;
1939be2b1797SNate Lawson 
194092488a57SJung-uk Kim     AcpiOsFree(devinfo);
194192488a57SJung-uk Kim     return (present);
1942b53f2771SMike Smith }
1943b53f2771SMike Smith 
1944b53f2771SMike Smith /*
194591233413SNate Lawson  * Match a HID string against a handle
194615e32d5dSMike Smith  */
1947ce619b2eSNate Lawson static BOOLEAN
1948ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid)
194915e32d5dSMike Smith {
19501e4925e8SNate Lawson     ACPI_DEVICE_INFO	*devinfo;
195192488a57SJung-uk Kim     BOOLEAN		ret;
195292488a57SJung-uk Kim     int			i;
195392488a57SJung-uk Kim 
195492488a57SJung-uk Kim     if (hid == NULL || h == NULL ||
195592488a57SJung-uk Kim 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
195692488a57SJung-uk Kim 	return (FALSE);
195715e32d5dSMike Smith 
19581e4925e8SNate Lawson     ret = FALSE;
1959c59c9a8eSJohn Baldwin     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
196092488a57SJung-uk Kim 	strcmp(hid, devinfo->HardwareId.String) == 0)
19611e4925e8SNate Lawson 	    ret = TRUE;
196292488a57SJung-uk Kim     else if ((devinfo->Valid & ACPI_VALID_CID) != 0)
196392488a57SJung-uk Kim 	for (i = 0; i < devinfo->CompatibleIdList.Count; i++) {
196492488a57SJung-uk Kim 	    if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) {
19651e4925e8SNate Lawson 		ret = TRUE;
19661e4925e8SNate Lawson 		break;
19671e4925e8SNate Lawson 	    }
19681e4925e8SNate Lawson 	}
1969be2b1797SNate Lawson 
197092488a57SJung-uk Kim     AcpiOsFree(devinfo);
19711e4925e8SNate Lawson     return (ret);
197215e32d5dSMike Smith }
197315e32d5dSMike Smith 
197415e32d5dSMike Smith /*
1975c5ba0be4SMike Smith  * Return the handle of a named object within our scope, ie. that of (parent)
1976c5ba0be4SMike Smith  * or one if its parents.
1977c5ba0be4SMike Smith  */
1978c5ba0be4SMike Smith ACPI_STATUS
1979c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
1980c5ba0be4SMike Smith {
1981c5ba0be4SMike Smith     ACPI_HANDLE		r;
1982c5ba0be4SMike Smith     ACPI_STATUS		status;
1983c5ba0be4SMike Smith 
1984be2b1797SNate Lawson     /* Walk back up the tree to the root */
1985c5ba0be4SMike Smith     for (;;) {
1986be2b1797SNate Lawson 	status = AcpiGetHandle(parent, path, &r);
1987be2b1797SNate Lawson 	if (ACPI_SUCCESS(status)) {
1988c5ba0be4SMike Smith 	    *result = r;
1989c5ba0be4SMike Smith 	    return (AE_OK);
1990c5ba0be4SMike Smith 	}
1991bee4aa4aSNate Lawson 	/* XXX Return error here? */
1992c5ba0be4SMike Smith 	if (status != AE_NOT_FOUND)
1993c5ba0be4SMike Smith 	    return (AE_OK);
1994b53f2771SMike Smith 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
1995c5ba0be4SMike Smith 	    return (AE_NOT_FOUND);
1996c5ba0be4SMike Smith 	parent = r;
1997c5ba0be4SMike Smith     }
1998c5ba0be4SMike Smith }
1999c5ba0be4SMike Smith 
2000eea17c34SNate Lawson /* Find the difference between two PM tick counts. */
2001eea17c34SNate Lawson uint32_t
2002eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start)
2003eea17c34SNate Lawson {
2004eea17c34SNate Lawson     uint32_t delta;
2005eea17c34SNate Lawson 
2006eea17c34SNate Lawson     if (end >= start)
2007eea17c34SNate Lawson 	delta = end - start;
20082be4e471SJung-uk Kim     else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
2009eea17c34SNate Lawson 	delta = ((0xFFFFFFFF - start) + end + 1);
20102be4e471SJung-uk Kim     else
20112be4e471SJung-uk Kim 	delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
2012eea17c34SNate Lawson     return (delta);
2013eea17c34SNate Lawson }
2014eea17c34SNate Lawson 
2015c5ba0be4SMike Smith /*
2016c5ba0be4SMike Smith  * Allocate a buffer with a preset data size.
2017c5ba0be4SMike Smith  */
2018c5ba0be4SMike Smith ACPI_BUFFER *
2019c5ba0be4SMike Smith acpi_AllocBuffer(int size)
2020c5ba0be4SMike Smith {
2021c5ba0be4SMike Smith     ACPI_BUFFER	*buf;
2022c5ba0be4SMike Smith 
2023c5ba0be4SMike Smith     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
2024c5ba0be4SMike Smith 	return (NULL);
2025c5ba0be4SMike Smith     buf->Length = size;
2026c5ba0be4SMike Smith     buf->Pointer = (void *)(buf + 1);
2027c5ba0be4SMike Smith     return (buf);
2028c5ba0be4SMike Smith }
2029c5ba0be4SMike Smith 
2030c310653eSNate Lawson ACPI_STATUS
2031cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
2032c310653eSNate Lawson {
2033c310653eSNate Lawson     ACPI_OBJECT arg1;
2034c310653eSNate Lawson     ACPI_OBJECT_LIST args;
2035c310653eSNate Lawson 
2036c310653eSNate Lawson     arg1.Type = ACPI_TYPE_INTEGER;
2037c310653eSNate Lawson     arg1.Integer.Value = number;
2038c310653eSNate Lawson     args.Count = 1;
2039c310653eSNate Lawson     args.Pointer = &arg1;
2040c310653eSNate Lawson 
2041c310653eSNate Lawson     return (AcpiEvaluateObject(handle, path, &args, NULL));
2042c310653eSNate Lawson }
2043c310653eSNate Lawson 
2044c5ba0be4SMike Smith /*
2045c5ba0be4SMike Smith  * Evaluate a path that should return an integer.
2046c5ba0be4SMike Smith  */
2047c5ba0be4SMike Smith ACPI_STATUS
2048cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
2049c5ba0be4SMike Smith {
2050be2b1797SNate Lawson     ACPI_STATUS	status;
2051c5ba0be4SMike Smith     ACPI_BUFFER	buf;
2052c573e654SMitsuru IWASAKI     ACPI_OBJECT	param;
2053c5ba0be4SMike Smith 
2054c5ba0be4SMike Smith     if (handle == NULL)
2055c5ba0be4SMike Smith 	handle = ACPI_ROOT_OBJECT;
20560c7da7acSJohn Baldwin 
20570c7da7acSJohn Baldwin     /*
20580c7da7acSJohn Baldwin      * Assume that what we've been pointed at is an Integer object, or
20590c7da7acSJohn Baldwin      * a method that will return an Integer.
20600c7da7acSJohn Baldwin      */
2061c5ba0be4SMike Smith     buf.Pointer = &param;
2062c5ba0be4SMike Smith     buf.Length = sizeof(param);
2063be2b1797SNate Lawson     status = AcpiEvaluateObject(handle, path, NULL, &buf);
2064be2b1797SNate Lawson     if (ACPI_SUCCESS(status)) {
2065be2b1797SNate Lawson 	if (param.Type == ACPI_TYPE_INTEGER)
2066c5ba0be4SMike Smith 	    *number = param.Integer.Value;
2067be2b1797SNate Lawson 	else
2068be2b1797SNate Lawson 	    status = AE_TYPE;
2069c5ba0be4SMike Smith     }
20700c7da7acSJohn Baldwin 
20710c7da7acSJohn Baldwin     /*
20720c7da7acSJohn Baldwin      * In some applications, a method that's expected to return an Integer
20730c7da7acSJohn Baldwin      * may instead return a Buffer (probably to simplify some internal
20740c7da7acSJohn Baldwin      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
20750c7da7acSJohn Baldwin      * convert it into an Integer as best we can.
20760c7da7acSJohn Baldwin      *
20770c7da7acSJohn Baldwin      * This is a hack.
20780c7da7acSJohn Baldwin      */
2079be2b1797SNate Lawson     if (status == AE_BUFFER_OVERFLOW) {
2080b53f2771SMike Smith 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
2081be2b1797SNate Lawson 	    status = AE_NO_MEMORY;
20820c7da7acSJohn Baldwin 	} else {
2083be2b1797SNate Lawson 	    status = AcpiEvaluateObject(handle, path, NULL, &buf);
2084be2b1797SNate Lawson 	    if (ACPI_SUCCESS(status))
2085be2b1797SNate Lawson 		status = acpi_ConvertBufferToInteger(&buf, number);
20860c7da7acSJohn Baldwin 	    AcpiOsFree(buf.Pointer);
20870c7da7acSJohn Baldwin 	}
2088f97739daSNate Lawson     }
2089be2b1797SNate Lawson     return (status);
2090c5ba0be4SMike Smith }
2091c5ba0be4SMike Smith 
2092c573e654SMitsuru IWASAKI ACPI_STATUS
2093cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
2094c573e654SMitsuru IWASAKI {
2095c573e654SMitsuru IWASAKI     ACPI_OBJECT	*p;
2096dba55fa2SNate Lawson     UINT8	*val;
2097c573e654SMitsuru IWASAKI     int		i;
2098c573e654SMitsuru IWASAKI 
2099c573e654SMitsuru IWASAKI     p = (ACPI_OBJECT *)bufp->Pointer;
2100c573e654SMitsuru IWASAKI     if (p->Type == ACPI_TYPE_INTEGER) {
2101c573e654SMitsuru IWASAKI 	*number = p->Integer.Value;
2102c573e654SMitsuru IWASAKI 	return (AE_OK);
2103c573e654SMitsuru IWASAKI     }
2104c573e654SMitsuru IWASAKI     if (p->Type != ACPI_TYPE_BUFFER)
2105c573e654SMitsuru IWASAKI 	return (AE_TYPE);
2106c573e654SMitsuru IWASAKI     if (p->Buffer.Length > sizeof(int))
2107c573e654SMitsuru IWASAKI 	return (AE_BAD_DATA);
2108be2b1797SNate Lawson 
2109c573e654SMitsuru IWASAKI     *number = 0;
2110dba55fa2SNate Lawson     val = p->Buffer.Pointer;
2111c573e654SMitsuru IWASAKI     for (i = 0; i < p->Buffer.Length; i++)
2112dba55fa2SNate Lawson 	*number += val[i] << (i * 8);
2113c573e654SMitsuru IWASAKI     return (AE_OK);
2114c573e654SMitsuru IWASAKI }
2115c573e654SMitsuru IWASAKI 
2116c5ba0be4SMike Smith /*
2117c5ba0be4SMike Smith  * Iterate over the elements of an a package object, calling the supplied
2118c5ba0be4SMike Smith  * function for each element.
2119c5ba0be4SMike Smith  *
2120c5ba0be4SMike Smith  * XXX possible enhancement might be to abort traversal on error.
2121c5ba0be4SMike Smith  */
2122c5ba0be4SMike Smith ACPI_STATUS
2123be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
2124be2b1797SNate Lawson 	void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
2125c5ba0be4SMike Smith {
2126c5ba0be4SMike Smith     ACPI_OBJECT	*comp;
2127c5ba0be4SMike Smith     int		i;
2128c5ba0be4SMike Smith 
2129be2b1797SNate Lawson     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
2130c5ba0be4SMike Smith 	return (AE_BAD_PARAMETER);
2131c5ba0be4SMike Smith 
2132be2b1797SNate Lawson     /* Iterate over components */
2133be2b1797SNate Lawson     i = 0;
2134be2b1797SNate Lawson     comp = pkg->Package.Elements;
2135be2b1797SNate Lawson     for (; i < pkg->Package.Count; i++, comp++)
2136c5ba0be4SMike Smith 	func(comp, arg);
2137c5ba0be4SMike Smith 
2138c5ba0be4SMike Smith     return (AE_OK);
2139c5ba0be4SMike Smith }
2140c5ba0be4SMike Smith 
21416f69255bSMike Smith /*
21426f69255bSMike Smith  * Find the (index)th resource object in a set.
21436f69255bSMike Smith  */
21446f69255bSMike Smith ACPI_STATUS
21456d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
21466f69255bSMike Smith {
21476d63101aSMike Smith     ACPI_RESOURCE	*rp;
21486f69255bSMike Smith     int			i;
21496f69255bSMike Smith 
21506d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
21516f69255bSMike Smith     i = index;
21526d63101aSMike Smith     while (i-- > 0) {
2153be2b1797SNate Lawson 	/* Range check */
21546d63101aSMike Smith 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
21556f69255bSMike Smith 	    return (AE_BAD_PARAMETER);
2156be2b1797SNate Lawson 
2157be2b1797SNate Lawson 	/* Check for terminator */
2158e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
21596d63101aSMike Smith 	    return (AE_NOT_FOUND);
2160abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
21616f69255bSMike Smith     }
21626f69255bSMike Smith     if (resp != NULL)
21636d63101aSMike Smith 	*resp = rp;
2164be2b1797SNate Lawson 
21656d63101aSMike Smith     return (AE_OK);
21666d63101aSMike Smith }
21676d63101aSMike Smith 
21686d63101aSMike Smith /*
21696d63101aSMike Smith  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
21706d63101aSMike Smith  *
21716d63101aSMike Smith  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
21726d63101aSMike Smith  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
21736d63101aSMike Smith  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
21746d63101aSMike Smith  * resources.
21756d63101aSMike Smith  */
21766d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
21776d63101aSMike Smith 
21786d63101aSMike Smith ACPI_STATUS
21796d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
21806d63101aSMike Smith {
21816d63101aSMike Smith     ACPI_RESOURCE	*rp;
21826d63101aSMike Smith     void		*newp;
21836d63101aSMike Smith 
2184be2b1797SNate Lawson     /* Initialise the buffer if necessary. */
21856d63101aSMike Smith     if (buf->Pointer == NULL) {
21866d63101aSMike Smith 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
21876d63101aSMike Smith 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
21886d63101aSMike Smith 	    return (AE_NO_MEMORY);
21896d63101aSMike Smith 	rp = (ACPI_RESOURCE *)buf->Pointer;
2190e8d472a7SJung-uk Kim 	rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
21916d63101aSMike Smith 	rp->Length = 0;
21926d63101aSMike Smith     }
21936d63101aSMike Smith     if (res == NULL)
21946d63101aSMike Smith 	return (AE_OK);
21956d63101aSMike Smith 
21966d63101aSMike Smith     /*
21976d63101aSMike Smith      * Scan the current buffer looking for the terminator.
21986d63101aSMike Smith      * This will either find the terminator or hit the end
21996d63101aSMike Smith      * of the buffer and return an error.
22006d63101aSMike Smith      */
22016d63101aSMike Smith     rp = (ACPI_RESOURCE *)buf->Pointer;
22026d63101aSMike Smith     for (;;) {
2203be2b1797SNate Lawson 	/* Range check, don't go outside the buffer */
22046d63101aSMike Smith 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
22056d63101aSMike Smith 	    return (AE_BAD_PARAMETER);
2206e8d472a7SJung-uk Kim 	if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
22076d63101aSMike Smith 	    break;
2208abcbc5bcSNate Lawson 	rp = ACPI_NEXT_RESOURCE(rp);
22096d63101aSMike Smith     }
22106d63101aSMike Smith 
22116d63101aSMike Smith     /*
22126d63101aSMike Smith      * Check the size of the buffer and expand if required.
22136d63101aSMike Smith      *
22146d63101aSMike Smith      * Required size is:
22156d63101aSMike Smith      *	size of existing resources before terminator +
22166d63101aSMike Smith      *	size of new resource and header +
22176d63101aSMike Smith      * 	size of terminator.
22186d63101aSMike Smith      *
22196d63101aSMike Smith      * Note that this loop should really only run once, unless
22206d63101aSMike Smith      * for some reason we are stuffing a *really* huge resource.
22216d63101aSMike Smith      */
22226d63101aSMike Smith     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
2223e8d472a7SJung-uk Kim 	    res->Length + ACPI_RS_SIZE_NO_DATA +
2224e8d472a7SJung-uk Kim 	    ACPI_RS_SIZE_MIN) >= buf->Length) {
22256d63101aSMike Smith 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
22266d63101aSMike Smith 	    return (AE_NO_MEMORY);
22276d63101aSMike Smith 	bcopy(buf->Pointer, newp, buf->Length);
2228a692219dSMike Smith 	rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
2229a692219dSMike Smith 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
22306d63101aSMike Smith 	AcpiOsFree(buf->Pointer);
22316d63101aSMike Smith 	buf->Pointer = newp;
22326d63101aSMike Smith 	buf->Length += buf->Length;
22336d63101aSMike Smith     }
22346d63101aSMike Smith 
2235be2b1797SNate Lawson     /* Insert the new resource. */
2236e8d472a7SJung-uk Kim     bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA);
22376d63101aSMike Smith 
2238be2b1797SNate Lawson     /* And add the terminator. */
2239abcbc5bcSNate Lawson     rp = ACPI_NEXT_RESOURCE(rp);
2240e8d472a7SJung-uk Kim     rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
22416d63101aSMike Smith     rp->Length = 0;
22426d63101aSMike Smith 
22436f69255bSMike Smith     return (AE_OK);
22446f69255bSMike Smith }
2245c5ba0be4SMike Smith 
2246da14ac9fSJohn Baldwin /*
2247da14ac9fSJohn Baldwin  * Set interrupt model.
2248da14ac9fSJohn Baldwin  */
2249da14ac9fSJohn Baldwin ACPI_STATUS
2250da14ac9fSJohn Baldwin acpi_SetIntrModel(int model)
2251da14ac9fSJohn Baldwin {
2252da14ac9fSJohn Baldwin 
2253c310653eSNate Lawson     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
2254da14ac9fSJohn Baldwin }
2255da14ac9fSJohn Baldwin 
225600a30448SNate Lawson /*
2257d95e7f5aSJohn Baldwin  * Walk subtables of a table and call a callback routine for each
2258d95e7f5aSJohn Baldwin  * subtable.  The caller should provide the first subtable and a
2259d95e7f5aSJohn Baldwin  * pointer to the end of the table.  This can be used to walk tables
2260d95e7f5aSJohn Baldwin  * such as MADT and SRAT that use subtable entries.
2261d95e7f5aSJohn Baldwin  */
2262d95e7f5aSJohn Baldwin void
2263d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler,
2264d95e7f5aSJohn Baldwin     void *arg)
2265d95e7f5aSJohn Baldwin {
2266d95e7f5aSJohn Baldwin     ACPI_SUBTABLE_HEADER *entry;
2267d95e7f5aSJohn Baldwin 
2268d95e7f5aSJohn Baldwin     for (entry = first; (void *)entry < end; ) {
2269d95e7f5aSJohn Baldwin 	/* Avoid an infinite loop if we hit a bogus entry. */
2270d95e7f5aSJohn Baldwin 	if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER))
2271d95e7f5aSJohn Baldwin 	    return;
2272d95e7f5aSJohn Baldwin 
2273d95e7f5aSJohn Baldwin 	handler(entry, arg);
2274d95e7f5aSJohn Baldwin 	entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length);
2275d95e7f5aSJohn Baldwin     }
2276d95e7f5aSJohn Baldwin }
2277d95e7f5aSJohn Baldwin 
2278d95e7f5aSJohn Baldwin /*
227900a30448SNate Lawson  * DEPRECATED.  This interface has serious deficiencies and will be
228000a30448SNate Lawson  * removed.
228100a30448SNate Lawson  *
228200a30448SNate Lawson  * Immediately enter the sleep state.  In the old model, acpiconf(8) ran
228300a30448SNate Lawson  * rc.suspend and rc.resume so we don't have to notify devd(8) to do this.
228400a30448SNate Lawson  */
228500a30448SNate Lawson ACPI_STATUS
228600a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state)
228700a30448SNate Lawson {
228800a30448SNate Lawson     static int once;
228900a30448SNate Lawson 
229000a30448SNate Lawson     if (!once) {
22912d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
229200a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n");
229300a30448SNate Lawson 	once = 1;
229400a30448SNate Lawson     }
229500a30448SNate Lawson     return (acpi_EnterSleepState(sc, state));
229600a30448SNate Lawson }
229700a30448SNate Lawson 
2298c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
229900a30448SNate Lawson static void
230000a30448SNate Lawson acpi_sleep_force(void *arg)
230100a30448SNate Lawson {
23022d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
230300a30448SNate Lawson 
23042d0c82e8SJung-uk Kim     device_printf(sc->acpi_dev,
23052d0c82e8SJung-uk Kim 	"suspend request timed out, forcing sleep now\n");
230600a30448SNate Lawson     if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
23072d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev, "force sleep state S%d failed\n",
23082d0c82e8SJung-uk Kim 	    sc->acpi_next_sstate);
230900a30448SNate Lawson }
2310c66d2b38SJung-uk Kim #endif
231100a30448SNate Lawson 
231200a30448SNate Lawson /*
231300a30448SNate Lawson  * Request that the system enter the given suspend state.  All /dev/apm
231400a30448SNate Lawson  * devices and devd(8) will be notified.  Userland then has a chance to
231500a30448SNate Lawson  * save state and acknowledge the request.  The system sleeps once all
231600a30448SNate Lawson  * acks are in.
231700a30448SNate Lawson  */
231800a30448SNate Lawson int
231900a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state)
232000a30448SNate Lawson {
232171f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
232200a30448SNate Lawson     struct apm_clone_data *clone;
232300a30448SNate Lawson 
2324a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
232500a30448SNate Lawson 	return (EINVAL);
2326a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state])
2327a0e73a12SJung-uk Kim 	return (EOPNOTSUPP);
232800a30448SNate Lawson 
232900a30448SNate Lawson     /* S5 (soft-off) should be entered directly with no waiting. */
233000a30448SNate Lawson     if (state == ACPI_STATE_S5) {
233100a30448SNate Lawson 	if (ACPI_SUCCESS(acpi_EnterSleepState(sc, state)))
233200a30448SNate Lawson 	    return (0);
233300a30448SNate Lawson 	else
233400a30448SNate Lawson 	    return (ENXIO);
233500a30448SNate Lawson     }
233600a30448SNate Lawson 
233700a30448SNate Lawson     /* If a suspend request is already in progress, just return. */
233800a30448SNate Lawson     ACPI_LOCK(acpi);
233900a30448SNate Lawson     if (sc->acpi_next_sstate != 0) {
234000a30448SNate Lawson     	ACPI_UNLOCK(acpi);
234100a30448SNate Lawson 	return (0);
234200a30448SNate Lawson     }
234300a30448SNate Lawson 
234400a30448SNate Lawson     /* Record the pending state and notify all apm devices. */
234500a30448SNate Lawson     sc->acpi_next_sstate = state;
234600a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
234700a30448SNate Lawson 	clone->notify_status = APM_EV_NONE;
234800a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_DEVD) == 0) {
234900a30448SNate Lawson 	    selwakeuppri(&clone->sel_read, PZERO);
235000a30448SNate Lawson 	    KNOTE_UNLOCKED(&clone->sel_read.si_note, 0);
235100a30448SNate Lawson 	}
235200a30448SNate Lawson     }
235300a30448SNate Lawson 
23540c26519eSMitsuru IWASAKI     /* If devd(8) is not running, immediately enter the sleep state. */
2355d42f0ad8SJung-uk Kim     if (!devctl_process_running()) {
23560c26519eSMitsuru IWASAKI 	ACPI_UNLOCK(acpi);
23577572a9c7SMitsuru IWASAKI 	if (ACPI_SUCCESS(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) {
23587572a9c7SMitsuru IWASAKI 	    return (0);
23597572a9c7SMitsuru IWASAKI 	} else {
23607572a9c7SMitsuru IWASAKI 	    return (ENXIO);
23617572a9c7SMitsuru IWASAKI 	}
23620c26519eSMitsuru IWASAKI     }
23630c26519eSMitsuru IWASAKI 
236400a30448SNate Lawson     /*
236500a30448SNate Lawson      * Set a timeout to fire if userland doesn't ack the suspend request
236600a30448SNate Lawson      * in time.  This way we still eventually go to sleep if we were
236700a30448SNate Lawson      * overheating or running low on battery, even if userland is hung.
236800a30448SNate Lawson      * We cancel this timeout once all userland acks are in or the
236900a30448SNate Lawson      * suspend request is aborted.
237000a30448SNate Lawson      */
237100a30448SNate Lawson     callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc);
237200a30448SNate Lawson     ACPI_UNLOCK(acpi);
2373d42f0ad8SJung-uk Kim 
2374d42f0ad8SJung-uk Kim     /* Now notify devd(8) also. */
2375d42f0ad8SJung-uk Kim     acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state);
2376d42f0ad8SJung-uk Kim 
237700a30448SNate Lawson     return (0);
2378c66d2b38SJung-uk Kim #else
2379c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2380c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2381c66d2b38SJung-uk Kim #endif
238200a30448SNate Lawson }
238300a30448SNate Lawson 
238400a30448SNate Lawson /*
238500a30448SNate Lawson  * Acknowledge (or reject) a pending sleep state.  The caller has
238600a30448SNate Lawson  * prepared for suspend and is now ready for it to proceed.  If the
238700a30448SNate Lawson  * error argument is non-zero, it indicates suspend should be cancelled
238800a30448SNate Lawson  * and gives an errno value describing why.  Once all votes are in,
238900a30448SNate Lawson  * we suspend the system.
239000a30448SNate Lawson  */
239100a30448SNate Lawson int
239200a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error)
239300a30448SNate Lawson {
2394c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__)
239500a30448SNate Lawson     struct acpi_softc *sc;
239600a30448SNate Lawson     int ret, sleeping;
239700a30448SNate Lawson 
239800a30448SNate Lawson     /* If no pending sleep state, return an error. */
239900a30448SNate Lawson     ACPI_LOCK(acpi);
240000a30448SNate Lawson     sc = clone->acpi_sc;
240100a30448SNate Lawson     if (sc->acpi_next_sstate == 0) {
240200a30448SNate Lawson     	ACPI_UNLOCK(acpi);
240300a30448SNate Lawson 	return (ENXIO);
240400a30448SNate Lawson     }
240500a30448SNate Lawson 
240600a30448SNate Lawson     /* Caller wants to abort suspend process. */
240700a30448SNate Lawson     if (error) {
240800a30448SNate Lawson 	sc->acpi_next_sstate = 0;
240900a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
24102d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
24112d0c82e8SJung-uk Kim 	    "listener on %s cancelled the pending suspend\n",
241200a30448SNate Lawson 	    devtoname(clone->cdev));
241300a30448SNate Lawson     	ACPI_UNLOCK(acpi);
241400a30448SNate Lawson 	return (0);
241500a30448SNate Lawson     }
241600a30448SNate Lawson 
241700a30448SNate Lawson     /*
241800a30448SNate Lawson      * Mark this device as acking the suspend request.  Then, walk through
241900a30448SNate Lawson      * all devices, seeing if they agree yet.  We only count devices that
242000a30448SNate Lawson      * are writable since read-only devices couldn't ack the request.
242100a30448SNate Lawson      */
242200a30448SNate Lawson     sleeping = TRUE;
2423c66d2b38SJung-uk Kim     clone->notify_status = APM_EV_ACKED;
242400a30448SNate Lawson     STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) {
242500a30448SNate Lawson 	if ((clone->flags & ACPI_EVF_WRITE) != 0 &&
242600a30448SNate Lawson 	    clone->notify_status != APM_EV_ACKED) {
242700a30448SNate Lawson 	    sleeping = FALSE;
242800a30448SNate Lawson 	    break;
242900a30448SNate Lawson 	}
243000a30448SNate Lawson     }
243100a30448SNate Lawson 
243200a30448SNate Lawson     /* If all devices have voted "yes", we will suspend now. */
243300a30448SNate Lawson     if (sleeping)
243400a30448SNate Lawson 	callout_stop(&sc->susp_force_to);
243500a30448SNate Lawson     ACPI_UNLOCK(acpi);
243600a30448SNate Lawson     ret = 0;
243700a30448SNate Lawson     if (sleeping) {
243800a30448SNate Lawson 	if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate)))
243900a30448SNate Lawson 		ret = ENODEV;
244000a30448SNate Lawson     }
244100a30448SNate Lawson     return (ret);
2442c66d2b38SJung-uk Kim #else
2443c66d2b38SJung-uk Kim     /* This platform does not support acpi suspend/resume. */
2444c66d2b38SJung-uk Kim     return (EOPNOTSUPP);
2445c66d2b38SJung-uk Kim #endif
244600a30448SNate Lawson }
244700a30448SNate Lawson 
2448ece50487SMitsuru IWASAKI static void
2449ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg)
2450ece50487SMitsuru IWASAKI {
2451d42f0ad8SJung-uk Kim     struct acpi_softc	*sc = (struct acpi_softc *)arg;
2452bee4aa4aSNate Lawson 
2453a0e73a12SJung-uk Kim     /* Reschedule if the system is not fully up and running. */
2454a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning) {
2455a0e73a12SJung-uk Kim 	timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2456a0e73a12SJung-uk Kim 	return;
2457a0e73a12SJung-uk Kim     }
2458a0e73a12SJung-uk Kim 
2459d42f0ad8SJung-uk Kim     ACPI_LOCK(acpi);
2460a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = FALSE;
2461d42f0ad8SJung-uk Kim     ACPI_UNLOCK(acpi);
2462d42f0ad8SJung-uk Kim }
2463d42f0ad8SJung-uk Kim 
2464d42f0ad8SJung-uk Kim static ACPI_STATUS
2465d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc)
2466d42f0ad8SJung-uk Kim {
2467d42f0ad8SJung-uk Kim     ACPI_STATUS		status;
2468d42f0ad8SJung-uk Kim 
2469a0e73a12SJung-uk Kim     /* Fail if the system is not fully up and running. */
2470a0e73a12SJung-uk Kim     if (!AcpiGbl_SystemAwakeAndRunning)
2471a0e73a12SJung-uk Kim 	return (AE_ERROR);
2472a0e73a12SJung-uk Kim 
2473d42f0ad8SJung-uk Kim     ACPI_LOCK(acpi);
2474d42f0ad8SJung-uk Kim     status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK;
2475a0e73a12SJung-uk Kim     sc->acpi_sleep_disabled = TRUE;
2476d42f0ad8SJung-uk Kim     ACPI_UNLOCK(acpi);
2477d42f0ad8SJung-uk Kim 
2478d42f0ad8SJung-uk Kim     return (status);
2479ece50487SMitsuru IWASAKI }
2480c30382dfSMitsuru IWASAKI 
248115e2f34fSNate Lawson enum acpi_sleep_state {
248215e2f34fSNate Lawson     ACPI_SS_NONE,
248315e2f34fSNate Lawson     ACPI_SS_GPE_SET,
248415e2f34fSNate Lawson     ACPI_SS_DEV_SUSPEND,
248515e2f34fSNate Lawson     ACPI_SS_SLP_PREP,
248615e2f34fSNate Lawson     ACPI_SS_SLEPT,
248715e2f34fSNate Lawson };
248815e2f34fSNate Lawson 
248915e32d5dSMike Smith /*
249000a30448SNate Lawson  * Enter the desired system sleep state.
249115e32d5dSMike Smith  *
2492be2b1797SNate Lawson  * Currently we support S1-S5 but S4 is only S4BIOS
249315e32d5dSMike Smith  */
249400a30448SNate Lawson static ACPI_STATUS
249500a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state)
249615e32d5dSMike Smith {
249715e2f34fSNate Lawson     ACPI_STATUS	status;
249815e2f34fSNate Lawson     enum acpi_sleep_state slp_state;
249915e32d5dSMike Smith 
2500b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
25010ae55423SMike Smith 
2502a0e73a12SJung-uk Kim     if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX)
250333c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_BAD_PARAMETER);
2504a0e73a12SJung-uk Kim     if (!acpi_sleep_states[state]) {
2505a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n",
2506a0e73a12SJung-uk Kim 	    state);
2507a0e73a12SJung-uk Kim 	return (AE_SUPPORT);
2508a0e73a12SJung-uk Kim     }
2509a0e73a12SJung-uk Kim 
2510a0e73a12SJung-uk Kim     /* Re-entry once we're suspending is not allowed. */
2511a0e73a12SJung-uk Kim     status = acpi_sleep_disable(sc);
2512a0e73a12SJung-uk Kim     if (ACPI_FAILURE(status)) {
2513a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev,
2514a0e73a12SJung-uk Kim 	    "suspend request ignored (not ready yet)\n");
2515a0e73a12SJung-uk Kim 	return (status);
2516a0e73a12SJung-uk Kim     }
251733c70d41SAndriy Gapon 
251833c70d41SAndriy Gapon     if (state == ACPI_STATE_S5) {
251933c70d41SAndriy Gapon 	/*
252033c70d41SAndriy Gapon 	 * Shut down cleanly and power off.  This will call us back through the
252133c70d41SAndriy Gapon 	 * shutdown handlers.
252233c70d41SAndriy Gapon 	 */
252333c70d41SAndriy Gapon 	shutdown_nice(RB_POWEROFF);
252433c70d41SAndriy Gapon 	return_ACPI_STATUS (AE_OK);
252533c70d41SAndriy Gapon     }
252633c70d41SAndriy Gapon 
2527c66d2b38SJung-uk Kim #ifdef SMP
2528c66d2b38SJung-uk Kim     thread_lock(curthread);
2529c66d2b38SJung-uk Kim     sched_bind(curthread, 0);
2530c66d2b38SJung-uk Kim     thread_unlock(curthread);
2531c66d2b38SJung-uk Kim #endif
2532c66d2b38SJung-uk Kim 
2533a56fe095SJohn Baldwin     /*
2534a56fe095SJohn Baldwin      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
2535a56fe095SJohn Baldwin      * drivers need this.
2536a56fe095SJohn Baldwin      */
2537a56fe095SJohn Baldwin     mtx_lock(&Giant);
2538c66d2b38SJung-uk Kim 
253915e2f34fSNate Lawson     slp_state = ACPI_SS_NONE;
254056d8cb57SMitsuru IWASAKI 
2541a1fccb47SMitsuru IWASAKI     sc->acpi_sstate = state;
2542a1fccb47SMitsuru IWASAKI 
2543d4b9ff91SNate Lawson     /* Enable any GPEs as appropriate and requested by the user. */
2544d4b9ff91SNate Lawson     acpi_wake_prep_walk(state);
254515e2f34fSNate Lawson     slp_state = ACPI_SS_GPE_SET;
2546e8b4d56eSNate Lawson 
254715e32d5dSMike Smith     /*
2548c7f88fc3SNate Lawson      * Inform all devices that we are going to sleep.  If at least one
2549c7f88fc3SNate Lawson      * device fails, DEVICE_SUSPEND() automatically resumes the tree.
255015e32d5dSMike Smith      *
2551c7f88fc3SNate Lawson      * XXX Note that a better two-pass approach with a 'veto' pass
2552c7f88fc3SNate Lawson      * followed by a "real thing" pass would be better, but the current
2553c7f88fc3SNate Lawson      * bus interface does not provide for this.
255415e32d5dSMike Smith      */
255515e2f34fSNate Lawson     if (DEVICE_SUSPEND(root_bus) != 0) {
255615e2f34fSNate Lawson 	device_printf(sc->acpi_dev, "device_suspend failed\n");
255733c70d41SAndriy Gapon 	goto backout;
255815e2f34fSNate Lawson     }
255915e2f34fSNate Lawson     slp_state = ACPI_SS_DEV_SUSPEND;
2560b9c780d6SMitsuru IWASAKI 
256193bfd059SNate Lawson     /* If testing device suspend only, back out of everything here. */
256293bfd059SNate Lawson     if (acpi_susp_bounce)
256333c70d41SAndriy Gapon 	goto backout;
256493bfd059SNate Lawson 
2565be2b1797SNate Lawson     status = AcpiEnterSleepStatePrep(state);
2566be2b1797SNate Lawson     if (ACPI_FAILURE(status)) {
2567b9c780d6SMitsuru IWASAKI 	device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
2568b9c780d6SMitsuru IWASAKI 		      AcpiFormatException(status));
256933c70d41SAndriy Gapon 	goto backout;
2570b9c780d6SMitsuru IWASAKI     }
257115e2f34fSNate Lawson     slp_state = ACPI_SS_SLP_PREP;
2572b9c780d6SMitsuru IWASAKI 
2573be2b1797SNate Lawson     if (sc->acpi_sleep_delay > 0)
2574ff01efb5SMitsuru IWASAKI 	DELAY(sc->acpi_sleep_delay * 1000000);
2575ff01efb5SMitsuru IWASAKI 
25766161544cSTakanori Watanabe     if (state != ACPI_STATE_S1) {
25776161544cSTakanori Watanabe 	acpi_sleep_machdep(sc, state);
25786161544cSTakanori Watanabe 
25796161544cSTakanori Watanabe 	/* Re-enable ACPI hardware on wakeup from sleep state 4. */
2580be2b1797SNate Lawson 	if (state == ACPI_STATE_S4)
2581964679ceSMitsuru IWASAKI 	    AcpiEnable();
25826161544cSTakanori Watanabe     } else {
2583c7f88fc3SNate Lawson 	ACPI_DISABLE_IRQS();
2584adad4744SNate Lawson 	status = AcpiEnterSleepState(state);
2585be2b1797SNate Lawson 	if (ACPI_FAILURE(status)) {
2586be2b1797SNate Lawson 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
2587be2b1797SNate Lawson 			  AcpiFormatException(status));
258833c70d41SAndriy Gapon 	    goto backout;
258915e32d5dSMike Smith 	}
25906161544cSTakanori Watanabe     }
259115e2f34fSNate Lawson     slp_state = ACPI_SS_SLEPT;
2592ece50487SMitsuru IWASAKI 
259315e2f34fSNate Lawson     /*
259415e2f34fSNate Lawson      * Back out state according to how far along we got in the suspend
259515e2f34fSNate Lawson      * process.  This handles both the error and success cases.
259615e2f34fSNate Lawson      */
259733c70d41SAndriy Gapon backout:
259800a30448SNate Lawson     sc->acpi_next_sstate = 0;
259915e2f34fSNate Lawson     if (slp_state >= ACPI_SS_GPE_SET) {
260015e2f34fSNate Lawson 	acpi_wake_prep_walk(state);
260115e2f34fSNate Lawson 	sc->acpi_sstate = ACPI_STATE_S0;
260215e2f34fSNate Lawson     }
260315e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLP_PREP)
260415e2f34fSNate Lawson 	AcpiLeaveSleepState(state);
2605071339e2SNate Lawson     if (slp_state >= ACPI_SS_DEV_SUSPEND)
2606071339e2SNate Lawson 	DEVICE_RESUME(root_bus);
260715e2f34fSNate Lawson     if (slp_state >= ACPI_SS_SLEPT)
260815e2f34fSNate Lawson 	acpi_enable_fixed_events(sc);
260915e2f34fSNate Lawson 
2610a56fe095SJohn Baldwin     mtx_unlock(&Giant);
2611c66d2b38SJung-uk Kim 
2612c66d2b38SJung-uk Kim #ifdef SMP
2613c66d2b38SJung-uk Kim     thread_lock(curthread);
2614c66d2b38SJung-uk Kim     sched_unbind(curthread);
2615c66d2b38SJung-uk Kim     thread_unlock(curthread);
2616c66d2b38SJung-uk Kim #endif
2617c66d2b38SJung-uk Kim 
2618d42f0ad8SJung-uk Kim     /* Allow another sleep request after a while. */
2619d42f0ad8SJung-uk Kim     timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME);
2620d42f0ad8SJung-uk Kim 
2621d42f0ad8SJung-uk Kim     /* Run /etc/rc.resume after we are back. */
2622d42f0ad8SJung-uk Kim     if (devctl_process_running())
2623d42f0ad8SJung-uk Kim 	acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state);
2624d42f0ad8SJung-uk Kim 
26250ae55423SMike Smith     return_ACPI_STATUS (status);
262615e32d5dSMike Smith }
262715e32d5dSMike Smith 
26280755473bSJung-uk Kim void
26290755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc)
26300755473bSJung-uk Kim {
26310755473bSJung-uk Kim 
26320755473bSJung-uk Kim     if (!acpi_reset_clock)
26330755473bSJung-uk Kim 	return;
26340755473bSJung-uk Kim 
26350755473bSJung-uk Kim     /*
26360755473bSJung-uk Kim      * Warm up timecounter again and reset system clock.
26370755473bSJung-uk Kim      */
26380755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
26390755473bSJung-uk Kim     (void)timecounter->tc_get_timecount(timecounter);
26400755473bSJung-uk Kim     inittodr(time_second + sc->acpi_sleep_delay);
26410755473bSJung-uk Kim }
26420755473bSJung-uk Kim 
2643e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */
2644e8b4d56eSNate Lawson int
2645e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type)
2646e8b4d56eSNate Lawson {
2647e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2648e8b4d56eSNate Lawson 
2649e8b4d56eSNate Lawson     /* Evaluate _PRW to find the GPE. */
2650e8b4d56eSNate Lawson     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2651e8b4d56eSNate Lawson 	return (ENXIO);
2652e8b4d56eSNate Lawson 
2653e8b4d56eSNate Lawson     /* Set the requested type for the GPE (runtime, wake, or both). */
2654e8b4d56eSNate Lawson     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
2655e8b4d56eSNate Lawson 	device_printf(dev, "set GPE type failed\n");
2656e8b4d56eSNate Lawson 	return (ENXIO);
2657e8b4d56eSNate Lawson     }
2658e8b4d56eSNate Lawson 
2659e8b4d56eSNate Lawson     return (0);
2660e8b4d56eSNate Lawson }
2661e8b4d56eSNate Lawson 
2662e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */
2663e8b4d56eSNate Lawson int
2664e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable)
2665e8b4d56eSNate Lawson {
2666e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2667e8b4d56eSNate Lawson     ACPI_STATUS status;
2668e8b4d56eSNate Lawson     int flags;
2669e8b4d56eSNate Lawson 
2670d4b9ff91SNate Lawson     /* Make sure the device supports waking the system and get the GPE. */
26712be4e471SJung-uk Kim     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
2672e8b4d56eSNate Lawson 	return (ENXIO);
2673e8b4d56eSNate Lawson 
2674d4b9ff91SNate Lawson     flags = acpi_get_flags(dev);
2675e8b4d56eSNate Lawson     if (enable) {
2676e8b4d56eSNate Lawson 	status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2677e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2678e8b4d56eSNate Lawson 	    device_printf(dev, "enable wake failed\n");
2679e8b4d56eSNate Lawson 	    return (ENXIO);
2680e8b4d56eSNate Lawson 	}
2681d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
2682e8b4d56eSNate Lawson     } else {
2683e8b4d56eSNate Lawson 	status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2684e8b4d56eSNate Lawson 	if (ACPI_FAILURE(status)) {
2685e8b4d56eSNate Lawson 	    device_printf(dev, "disable wake failed\n");
2686e8b4d56eSNate Lawson 	    return (ENXIO);
2687e8b4d56eSNate Lawson 	}
2688d4b9ff91SNate Lawson 	acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
2689e8b4d56eSNate Lawson     }
2690e8b4d56eSNate Lawson 
2691e8b4d56eSNate Lawson     return (0);
2692e8b4d56eSNate Lawson }
2693e8b4d56eSNate Lawson 
2694d4b9ff91SNate Lawson static int
2695d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
2696e8b4d56eSNate Lawson {
2697e8b4d56eSNate Lawson     struct acpi_prw_data prw;
2698d4b9ff91SNate Lawson     device_t dev;
2699e8b4d56eSNate Lawson 
2700d4b9ff91SNate Lawson     /* Check that this is a wake-capable device and get its GPE. */
2701e8b4d56eSNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2702e8b4d56eSNate Lawson 	return (ENXIO);
2703d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2704e8b4d56eSNate Lawson 
2705e8b4d56eSNate Lawson     /*
2706d4b9ff91SNate Lawson      * The destination sleep state must be less than (i.e., higher power)
2707d4b9ff91SNate Lawson      * or equal to the value specified by _PRW.  If this GPE cannot be
2708d4b9ff91SNate Lawson      * enabled for the next sleep state, then disable it.  If it can and
2709d4b9ff91SNate Lawson      * the user requested it be enabled, turn on any required power resources
2710d4b9ff91SNate Lawson      * and set _PSW.
2711e8b4d56eSNate Lawson      */
2712d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2713cc85c78cSNate Lawson 	AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2714e8b4d56eSNate Lawson 	if (bootverbose)
2715d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
2716d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2717d4b9ff91SNate Lawson     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
2718d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 1);
2719d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 1);
2720d4b9ff91SNate Lawson 	if (bootverbose)
2721d4b9ff91SNate Lawson 	    device_printf(dev, "wake_prep enabled for %s (S%d)\n",
2722d4b9ff91SNate Lawson 		acpi_name(handle), sstate);
2723d4b9ff91SNate Lawson     }
2724cc85c78cSNate Lawson 
2725cc85c78cSNate Lawson     return (0);
2726e8b4d56eSNate Lawson }
2727e8b4d56eSNate Lawson 
2728d4b9ff91SNate Lawson static int
2729d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
2730cc85c78cSNate Lawson {
2731cc85c78cSNate Lawson     struct acpi_prw_data prw;
2732d4b9ff91SNate Lawson     device_t dev;
2733cc85c78cSNate Lawson 
2734cc85c78cSNate Lawson     /*
2735d4b9ff91SNate Lawson      * Check that this is a wake-capable device and get its GPE.  Return
2736d4b9ff91SNate Lawson      * now if the user didn't enable this device for wake.
2737cc85c78cSNate Lawson      */
2738d4b9ff91SNate Lawson     if (acpi_parse_prw(handle, &prw) != 0)
2739d4b9ff91SNate Lawson 	return (ENXIO);
2740d4b9ff91SNate Lawson     dev = acpi_get_device(handle);
2741d4b9ff91SNate Lawson     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
2742d4b9ff91SNate Lawson 	return (0);
2743cc85c78cSNate Lawson 
2744d4b9ff91SNate Lawson     /*
2745d4b9ff91SNate Lawson      * If this GPE couldn't be enabled for the previous sleep state, it was
2746d4b9ff91SNate Lawson      * disabled before going to sleep so re-enable it.  If it was enabled,
2747d4b9ff91SNate Lawson      * clear _PSW and turn off any power resources it used.
2748d4b9ff91SNate Lawson      */
2749d4b9ff91SNate Lawson     if (sstate > prw.lowest_wake) {
2750cc85c78cSNate Lawson 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
2751d4b9ff91SNate Lawson 	if (bootverbose)
2752d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
2753d4b9ff91SNate Lawson     } else {
2754d4b9ff91SNate Lawson 	acpi_SetInteger(handle, "_PSW", 0);
2755d4b9ff91SNate Lawson 	acpi_pwr_wake_enable(handle, 0);
2756d4b9ff91SNate Lawson 	if (bootverbose)
2757d4b9ff91SNate Lawson 	    device_printf(dev, "run_prep cleaned up for %s\n",
2758d4b9ff91SNate Lawson 		acpi_name(handle));
2759d4b9ff91SNate Lawson     }
2760d4b9ff91SNate Lawson 
2761e8b4d56eSNate Lawson     return (0);
2762e8b4d56eSNate Lawson }
2763e8b4d56eSNate Lawson 
2764e8b4d56eSNate Lawson static ACPI_STATUS
2765d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
2766e8b4d56eSNate Lawson {
2767d4b9ff91SNate Lawson     int sstate;
2768e8b4d56eSNate Lawson 
2769d4b9ff91SNate Lawson     /* If suspending, run the sleep prep function, otherwise wake. */
2770d4b9ff91SNate Lawson     sstate = *(int *)context;
2771d4b9ff91SNate Lawson     if (AcpiGbl_SystemAwakeAndRunning)
2772d4b9ff91SNate Lawson 	acpi_wake_sleep_prep(handle, sstate);
2773d4b9ff91SNate Lawson     else
2774d4b9ff91SNate Lawson 	acpi_wake_run_prep(handle, sstate);
2775e8b4d56eSNate Lawson     return (AE_OK);
2776e8b4d56eSNate Lawson }
2777e8b4d56eSNate Lawson 
2778d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
2779e8b4d56eSNate Lawson static int
2780d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate)
2781e8b4d56eSNate Lawson {
2782e8b4d56eSNate Lawson     ACPI_HANDLE sb_handle;
2783e8b4d56eSNate Lawson 
2784e8b4d56eSNate Lawson     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
2785d4b9ff91SNate Lawson 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
27862272d050SJung-uk Kim 	    acpi_wake_prep, NULL, &sstate, NULL);
2787e8b4d56eSNate Lawson     return (0);
2788e8b4d56eSNate Lawson }
2789e8b4d56eSNate Lawson 
279088a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
279188a79fc0SNate Lawson static int
279288a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev)
279388a79fc0SNate Lawson {
279488a79fc0SNate Lawson     int error, i, numdevs;
279588a79fc0SNate Lawson     device_t *devlist;
279688a79fc0SNate Lawson     device_t child;
2797d4b9ff91SNate Lawson     ACPI_STATUS status;
279888a79fc0SNate Lawson 
279988a79fc0SNate Lawson     error = device_get_children(dev, &devlist, &numdevs);
28001c9ec538SNate Lawson     if (error != 0 || numdevs == 0) {
28011c9ec538SNate Lawson 	if (numdevs == 0)
28021c9ec538SNate Lawson 	    free(devlist, M_TEMP);
280388a79fc0SNate Lawson 	return (error);
28041c9ec538SNate Lawson     }
280588a79fc0SNate Lawson     for (i = 0; i < numdevs; i++) {
280688a79fc0SNate Lawson 	child = devlist[i];
2807d4b9ff91SNate Lawson 	acpi_wake_sysctl_walk(child);
280888a79fc0SNate Lawson 	if (!device_is_attached(child))
280988a79fc0SNate Lawson 	    continue;
2810d4b9ff91SNate Lawson 	status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
2811d4b9ff91SNate Lawson 	if (ACPI_SUCCESS(status)) {
281288a79fc0SNate Lawson 	    SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
281388a79fc0SNate Lawson 		SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
281488a79fc0SNate Lawson 		"wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
281588a79fc0SNate Lawson 		acpi_wake_set_sysctl, "I", "Device set to wake the system");
281688a79fc0SNate Lawson 	}
281788a79fc0SNate Lawson     }
281888a79fc0SNate Lawson     free(devlist, M_TEMP);
281988a79fc0SNate Lawson 
282088a79fc0SNate Lawson     return (0);
282188a79fc0SNate Lawson }
282288a79fc0SNate Lawson 
282388a79fc0SNate Lawson /* Enable or disable wake from userland. */
282488a79fc0SNate Lawson static int
282588a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
282688a79fc0SNate Lawson {
282788a79fc0SNate Lawson     int enable, error;
282888a79fc0SNate Lawson     device_t dev;
282988a79fc0SNate Lawson 
283088a79fc0SNate Lawson     dev = (device_t)arg1;
2831d4b9ff91SNate Lawson     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
283288a79fc0SNate Lawson 
283388a79fc0SNate Lawson     error = sysctl_handle_int(oidp, &enable, 0, req);
283488a79fc0SNate Lawson     if (error != 0 || req->newptr == NULL)
283588a79fc0SNate Lawson 	return (error);
283688a79fc0SNate Lawson     if (enable != 0 && enable != 1)
283788a79fc0SNate Lawson 	return (EINVAL);
283888a79fc0SNate Lawson 
283988a79fc0SNate Lawson     return (acpi_wake_set_enable(dev, enable));
284088a79fc0SNate Lawson }
284188a79fc0SNate Lawson 
2842e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */
2843d4b9ff91SNate Lawson int
2844e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
2845e8b4d56eSNate Lawson {
2846e8b4d56eSNate Lawson     ACPI_STATUS			status;
2847e8b4d56eSNate Lawson     ACPI_BUFFER			prw_buffer;
2848e8b4d56eSNate Lawson     ACPI_OBJECT			*res, *res2;
2849d4b9ff91SNate Lawson     int				error, i, power_count;
2850e8b4d56eSNate Lawson 
2851e8b4d56eSNate Lawson     if (h == NULL || prw == NULL)
2852e8b4d56eSNate Lawson 	return (EINVAL);
2853e8b4d56eSNate Lawson 
2854e8b4d56eSNate Lawson     /*
2855e8b4d56eSNate Lawson      * The _PRW object (7.2.9) is only required for devices that have the
2856e8b4d56eSNate Lawson      * ability to wake the system from a sleeping state.
2857e8b4d56eSNate Lawson      */
2858e8b4d56eSNate Lawson     error = EINVAL;
2859e8b4d56eSNate Lawson     prw_buffer.Pointer = NULL;
2860e8b4d56eSNate Lawson     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
2861e8b4d56eSNate Lawson     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
2862e8b4d56eSNate Lawson     if (ACPI_FAILURE(status))
2863e8b4d56eSNate Lawson 	return (ENOENT);
2864e8b4d56eSNate Lawson     res = (ACPI_OBJECT *)prw_buffer.Pointer;
2865e8b4d56eSNate Lawson     if (res == NULL)
2866e8b4d56eSNate Lawson 	return (ENOENT);
2867e8b4d56eSNate Lawson     if (!ACPI_PKG_VALID(res, 2))
2868e8b4d56eSNate Lawson 	goto out;
2869e8b4d56eSNate Lawson 
2870e8b4d56eSNate Lawson     /*
2871e8b4d56eSNate Lawson      * Element 1 of the _PRW object:
2872e8b4d56eSNate Lawson      * The lowest power system sleeping state that can be entered while still
2873e8b4d56eSNate Lawson      * providing wake functionality.  The sleeping state being entered must
2874e8b4d56eSNate Lawson      * be less than (i.e., higher power) or equal to this value.
2875e8b4d56eSNate Lawson      */
2876e8b4d56eSNate Lawson     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
2877e8b4d56eSNate Lawson 	goto out;
2878e8b4d56eSNate Lawson 
2879e8b4d56eSNate Lawson     /*
2880e8b4d56eSNate Lawson      * Element 0 of the _PRW object:
2881e8b4d56eSNate Lawson      */
2882e8b4d56eSNate Lawson     switch (res->Package.Elements[0].Type) {
2883e8b4d56eSNate Lawson     case ACPI_TYPE_INTEGER:
2884e8b4d56eSNate Lawson 	/*
2885e8b4d56eSNate Lawson 	 * If the data type of this package element is numeric, then this
2886e8b4d56eSNate Lawson 	 * _PRW package element is the bit index in the GPEx_EN, in the
2887e8b4d56eSNate Lawson 	 * GPE blocks described in the FADT, of the enable bit that is
2888e8b4d56eSNate Lawson 	 * enabled for the wake event.
2889e8b4d56eSNate Lawson 	 */
2890e8b4d56eSNate Lawson 	prw->gpe_handle = NULL;
2891e8b4d56eSNate Lawson 	prw->gpe_bit = res->Package.Elements[0].Integer.Value;
2892e8b4d56eSNate Lawson 	error = 0;
2893e8b4d56eSNate Lawson 	break;
2894e8b4d56eSNate Lawson     case ACPI_TYPE_PACKAGE:
2895e8b4d56eSNate Lawson 	/*
2896e8b4d56eSNate Lawson 	 * If the data type of this package element is a package, then this
2897e8b4d56eSNate Lawson 	 * _PRW package element is itself a package containing two
2898e8b4d56eSNate Lawson 	 * elements.  The first is an object reference to the GPE Block
2899e8b4d56eSNate Lawson 	 * device that contains the GPE that will be triggered by the wake
2900e8b4d56eSNate Lawson 	 * event.  The second element is numeric and it contains the bit
2901e8b4d56eSNate Lawson 	 * index in the GPEx_EN, in the GPE Block referenced by the
2902e8b4d56eSNate Lawson 	 * first element in the package, of the enable bit that is enabled for
2903e8b4d56eSNate Lawson 	 * the wake event.
2904e8b4d56eSNate Lawson 	 *
2905e8b4d56eSNate Lawson 	 * For example, if this field is a package then it is of the form:
2906e8b4d56eSNate Lawson 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
2907e8b4d56eSNate Lawson 	 */
2908e8b4d56eSNate Lawson 	res2 = &res->Package.Elements[0];
2909e8b4d56eSNate Lawson 	if (!ACPI_PKG_VALID(res2, 2))
2910e8b4d56eSNate Lawson 	    goto out;
2911e8b4d56eSNate Lawson 	prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
2912e8b4d56eSNate Lawson 	if (prw->gpe_handle == NULL)
2913e8b4d56eSNate Lawson 	    goto out;
2914e8b4d56eSNate Lawson 	if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
2915e8b4d56eSNate Lawson 	    goto out;
2916e8b4d56eSNate Lawson 	error = 0;
2917e8b4d56eSNate Lawson 	break;
2918e8b4d56eSNate Lawson     default:
2919e8b4d56eSNate Lawson 	goto out;
2920e8b4d56eSNate Lawson     }
2921e8b4d56eSNate Lawson 
2922d4b9ff91SNate Lawson     /* Elements 2 to N of the _PRW object are power resources. */
2923d4b9ff91SNate Lawson     power_count = res->Package.Count - 2;
2924d4b9ff91SNate Lawson     if (power_count > ACPI_PRW_MAX_POWERRES) {
2925d4b9ff91SNate Lawson 	printf("ACPI device %s has too many power resources\n", acpi_name(h));
2926d4b9ff91SNate Lawson 	power_count = 0;
2927d4b9ff91SNate Lawson     }
2928d4b9ff91SNate Lawson     prw->power_res_count = power_count;
2929d4b9ff91SNate Lawson     for (i = 0; i < power_count; i++)
2930d4b9ff91SNate Lawson 	prw->power_res[i] = res->Package.Elements[i];
2931e8b4d56eSNate Lawson 
2932e8b4d56eSNate Lawson out:
2933e8b4d56eSNate Lawson     if (prw_buffer.Pointer != NULL)
2934e8b4d56eSNate Lawson 	AcpiOsFree(prw_buffer.Pointer);
2935e8b4d56eSNate Lawson     return (error);
2936e8b4d56eSNate Lawson }
2937e8b4d56eSNate Lawson 
293815e32d5dSMike Smith /*
293915e32d5dSMike Smith  * ACPI Event Handlers
294015e32d5dSMike Smith  */
294115e32d5dSMike Smith 
294215e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
294315e32d5dSMike Smith 
294415e32d5dSMike Smith static void
294515e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state)
294615e32d5dSMike Smith {
29472d0c82e8SJung-uk Kim     struct acpi_softc *sc = (struct acpi_softc *)arg;
294800a30448SNate Lawson     int ret;
2949bee4aa4aSNate Lawson 
2950b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
295115e32d5dSMike Smith 
2952a0e73a12SJung-uk Kim     /* Check if button action is disabled or unknown. */
2953a0e73a12SJung-uk Kim     if (state == ACPI_STATE_UNKNOWN)
2954813d6dcaSNate Lawson 	return;
2955813d6dcaSNate Lawson 
295600a30448SNate Lawson     /* Request that the system prepare to enter the given suspend state. */
29572d0c82e8SJung-uk Kim     ret = acpi_ReqSleepState(sc, state);
295800a30448SNate Lawson     if (ret != 0)
29592d0c82e8SJung-uk Kim 	device_printf(sc->acpi_dev,
29602d0c82e8SJung-uk Kim 	    "request to enter state S%d failed (err %d)\n", state, ret);
2961bee4aa4aSNate Lawson 
29620ae55423SMike Smith     return_VOID;
296315e32d5dSMike Smith }
296415e32d5dSMike Smith 
296515e32d5dSMike Smith static void
296615e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state)
296715e32d5dSMike Smith {
2968bee4aa4aSNate Lawson 
2969b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
29700ae55423SMike Smith 
2971bee4aa4aSNate Lawson     /* Currently, nothing to do for wakeup. */
2972cb9b0d80SMike Smith 
29730ae55423SMike Smith     return_VOID;
297415e32d5dSMike Smith }
297515e32d5dSMike Smith 
297615e32d5dSMike Smith /*
297715e32d5dSMike Smith  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
297815e32d5dSMike Smith  */
297915e32d5dSMike Smith UINT32
298033febf93SNate Lawson acpi_event_power_button_sleep(void *context)
298115e32d5dSMike Smith {
298215e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
298315e32d5dSMike Smith 
2984b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
29850ae55423SMike Smith 
298615e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
29870ae55423SMike Smith 
2988b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
298915e32d5dSMike Smith }
299015e32d5dSMike Smith 
299115e32d5dSMike Smith UINT32
299233febf93SNate Lawson acpi_event_power_button_wake(void *context)
299315e32d5dSMike Smith {
299415e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
299515e32d5dSMike Smith 
2996b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
29970ae55423SMike Smith 
299815e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
29990ae55423SMike Smith 
3000b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
300115e32d5dSMike Smith }
300215e32d5dSMike Smith 
300315e32d5dSMike Smith UINT32
300433febf93SNate Lawson acpi_event_sleep_button_sleep(void *context)
300515e32d5dSMike Smith {
300615e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
300715e32d5dSMike Smith 
3008b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
30090ae55423SMike Smith 
301015e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
30110ae55423SMike Smith 
3012b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
301315e32d5dSMike Smith }
301415e32d5dSMike Smith 
301515e32d5dSMike Smith UINT32
301633febf93SNate Lawson acpi_event_sleep_button_wake(void *context)
301715e32d5dSMike Smith {
301815e32d5dSMike Smith     struct acpi_softc	*sc = (struct acpi_softc *)context;
301915e32d5dSMike Smith 
3020b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
30210ae55423SMike Smith 
302215e32d5dSMike Smith     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
30230ae55423SMike Smith 
3024b53f2771SMike Smith     return_VALUE (ACPI_INTERRUPT_HANDLED);
302515e32d5dSMike Smith }
302615e32d5dSMike Smith 
302715e32d5dSMike Smith /*
3028bee4aa4aSNate Lawson  * XXX This static buffer is suboptimal.  There is no locking so only
3029bee4aa4aSNate Lawson  * use this for single-threaded callers.
303015e32d5dSMike Smith  */
303115e32d5dSMike Smith char *
303215e32d5dSMike Smith acpi_name(ACPI_HANDLE handle)
303315e32d5dSMike Smith {
3034bee4aa4aSNate Lawson     ACPI_BUFFER buf;
3035bee4aa4aSNate Lawson     static char data[256];
303615e32d5dSMike Smith 
3037bee4aa4aSNate Lawson     buf.Length = sizeof(data);
3038bee4aa4aSNate Lawson     buf.Pointer = data;
3039cb9b0d80SMike Smith 
30400a9a1f44SNate Lawson     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
3041bee4aa4aSNate Lawson 	return (data);
3042bee4aa4aSNate Lawson     return ("(unknown)");
304315e32d5dSMike Smith }
304415e32d5dSMike Smith 
304515e32d5dSMike Smith /*
304615e32d5dSMike Smith  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
304715e32d5dSMike Smith  * parts of the namespace.
304815e32d5dSMike Smith  */
304915e32d5dSMike Smith int
305015e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle)
305115e32d5dSMike Smith {
30523c600cfbSJohn Baldwin     char	*cp, *env, *np;
305315e32d5dSMike Smith     int		len;
305415e32d5dSMike Smith 
305515e32d5dSMike Smith     np = acpi_name(handle);
305615e32d5dSMike Smith     if (*np == '\\')
305715e32d5dSMike Smith 	np++;
30583c600cfbSJohn Baldwin     if ((env = getenv("debug.acpi.avoid")) == NULL)
305915e32d5dSMike Smith 	return (0);
306015e32d5dSMike Smith 
3061be2b1797SNate Lawson     /* Scan the avoid list checking for a match */
30623c600cfbSJohn Baldwin     cp = env;
306315e32d5dSMike Smith     for (;;) {
3064bee4aa4aSNate Lawson 	while (*cp != 0 && isspace(*cp))
306515e32d5dSMike Smith 	    cp++;
306615e32d5dSMike Smith 	if (*cp == 0)
306715e32d5dSMike Smith 	    break;
306815e32d5dSMike Smith 	len = 0;
3069bee4aa4aSNate Lawson 	while (cp[len] != 0 && !isspace(cp[len]))
307015e32d5dSMike Smith 	    len++;
3071d786139cSMaxime Henrion 	if (!strncmp(cp, np, len)) {
30723c600cfbSJohn Baldwin 	    freeenv(env);
30730ae55423SMike Smith 	    return(1);
3074d786139cSMaxime Henrion 	}
30750ae55423SMike Smith 	cp += len;
30760ae55423SMike Smith     }
30773c600cfbSJohn Baldwin     freeenv(env);
3078be2b1797SNate Lawson 
30790ae55423SMike Smith     return (0);
30800ae55423SMike Smith }
30810ae55423SMike Smith 
30820ae55423SMike Smith /*
30830ae55423SMike Smith  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
30840ae55423SMike Smith  */
30850ae55423SMike Smith int
30860ae55423SMike Smith acpi_disabled(char *subsys)
30870ae55423SMike Smith {
308878689b15SMaxime Henrion     char	*cp, *env;
30890ae55423SMike Smith     int		len;
30900ae55423SMike Smith 
30913184cf5aSNate Lawson     if ((env = getenv("debug.acpi.disabled")) == NULL)
30920ae55423SMike Smith 	return (0);
30933184cf5aSNate Lawson     if (strcmp(env, "all") == 0) {
309478689b15SMaxime Henrion 	freeenv(env);
30950ae55423SMike Smith 	return (1);
3096d786139cSMaxime Henrion     }
30970ae55423SMike Smith 
30983184cf5aSNate Lawson     /* Scan the disable list, checking for a match. */
309978689b15SMaxime Henrion     cp = env;
31000ae55423SMike Smith     for (;;) {
31013184cf5aSNate Lawson 	while (*cp != '\0' && isspace(*cp))
31020ae55423SMike Smith 	    cp++;
31033184cf5aSNate Lawson 	if (*cp == '\0')
31040ae55423SMike Smith 	    break;
31050ae55423SMike Smith 	len = 0;
31063184cf5aSNate Lawson 	while (cp[len] != '\0' && !isspace(cp[len]))
31070ae55423SMike Smith 	    len++;
31083184cf5aSNate Lawson 	if (strncmp(cp, subsys, len) == 0) {
310978689b15SMaxime Henrion 	    freeenv(env);
311015e32d5dSMike Smith 	    return (1);
3111d786139cSMaxime Henrion 	}
311215e32d5dSMike Smith 	cp += len;
311315e32d5dSMike Smith     }
311478689b15SMaxime Henrion     freeenv(env);
3115be2b1797SNate Lawson 
311615e32d5dSMike Smith     return (0);
311715e32d5dSMike Smith }
311815e32d5dSMike Smith 
311915e32d5dSMike Smith /*
312015e32d5dSMike Smith  * Control interface.
312115e32d5dSMike Smith  *
31220ae55423SMike Smith  * We multiplex ioctls for all participating ACPI devices here.  Individual
3123be2b1797SNate Lawson  * drivers wanting to be accessible via /dev/acpi should use the
3124be2b1797SNate Lawson  * register/deregister interface to make their handlers visible.
312515e32d5dSMike Smith  */
31260ae55423SMike Smith struct acpi_ioctl_hook
31270ae55423SMike Smith {
31280ae55423SMike Smith     TAILQ_ENTRY(acpi_ioctl_hook) link;
31290ae55423SMike Smith     u_long			 cmd;
3130be2b1797SNate Lawson     acpi_ioctl_fn		 fn;
31310ae55423SMike Smith     void			 *arg;
31320ae55423SMike Smith };
31330ae55423SMike Smith 
31340ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
31350ae55423SMike Smith static int				acpi_ioctl_hooks_initted;
31360ae55423SMike Smith 
31370ae55423SMike Smith int
3138be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
31390ae55423SMike Smith {
31400ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
31410ae55423SMike Smith 
31420ae55423SMike Smith     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
31430ae55423SMike Smith 	return (ENOMEM);
31440ae55423SMike Smith     hp->cmd = cmd;
31450ae55423SMike Smith     hp->fn = fn;
31460ae55423SMike Smith     hp->arg = arg;
314715e2f34fSNate Lawson 
314815e2f34fSNate Lawson     ACPI_LOCK(acpi);
31490ae55423SMike Smith     if (acpi_ioctl_hooks_initted == 0) {
31500ae55423SMike Smith 	TAILQ_INIT(&acpi_ioctl_hooks);
31510ae55423SMike Smith 	acpi_ioctl_hooks_initted = 1;
31520ae55423SMike Smith     }
31530ae55423SMike Smith     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
315415e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
315515e2f34fSNate Lawson 
31560ae55423SMike Smith     return (0);
31570ae55423SMike Smith }
31580ae55423SMike Smith 
31590ae55423SMike Smith void
3160be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
31610ae55423SMike Smith {
31620ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
31630ae55423SMike Smith 
316415e2f34fSNate Lawson     ACPI_LOCK(acpi);
31650ae55423SMike Smith     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
316615e2f34fSNate Lawson 	if (hp->cmd == cmd && hp->fn == fn)
31670ae55423SMike Smith 	    break;
31680ae55423SMike Smith 
31690ae55423SMike Smith     if (hp != NULL) {
31700ae55423SMike Smith 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
31710ae55423SMike Smith 	free(hp, M_ACPIDEV);
31720ae55423SMike Smith     }
317315e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
31740ae55423SMike Smith }
31750ae55423SMike Smith 
317615e32d5dSMike Smith static int
317700b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td)
317815e32d5dSMike Smith {
317915e32d5dSMike Smith     return (0);
318015e32d5dSMike Smith }
318115e32d5dSMike Smith 
318215e32d5dSMike Smith static int
318300b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td)
318415e32d5dSMike Smith {
318515e32d5dSMike Smith     return (0);
318615e32d5dSMike Smith }
318715e32d5dSMike Smith 
318815e32d5dSMike Smith static int
318900b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
319015e32d5dSMike Smith {
319115e32d5dSMike Smith     struct acpi_softc		*sc;
31920ae55423SMike Smith     struct acpi_ioctl_hook	*hp;
3193bee4aa4aSNate Lawson     int				error, state;
319415e32d5dSMike Smith 
3195a2e35898SDavid E. O'Brien     error = 0;
3196a2e35898SDavid E. O'Brien     hp = NULL;
319715e32d5dSMike Smith     sc = dev->si_drv1;
319815e32d5dSMike Smith 
31990ae55423SMike Smith     /*
32000ae55423SMike Smith      * Scan the list of registered ioctls, looking for handlers.
32010ae55423SMike Smith      */
320215e2f34fSNate Lawson     ACPI_LOCK(acpi);
3203bee4aa4aSNate Lawson     if (acpi_ioctl_hooks_initted)
32040ae55423SMike Smith 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
3205bee4aa4aSNate Lawson 	    if (hp->cmd == cmd)
3206bee4aa4aSNate Lawson 		break;
32070ae55423SMike Smith 	}
320815e2f34fSNate Lawson     ACPI_UNLOCK(acpi);
3209bee4aa4aSNate Lawson     if (hp)
3210bee4aa4aSNate Lawson 	return (hp->fn(cmd, addr, hp->arg));
32110ae55423SMike Smith 
32120ae55423SMike Smith     /*
3213a89fcf28STakanori Watanabe      * Core ioctls are not permitted for non-writable user.
3214a89fcf28STakanori Watanabe      * Currently, other ioctls just fetch information.
3215a89fcf28STakanori Watanabe      * Not changing system behavior.
3216a89fcf28STakanori Watanabe      */
3217be2b1797SNate Lawson     if ((flag & FWRITE) == 0)
3218be2b1797SNate Lawson 	return (EPERM);
3219a89fcf28STakanori Watanabe 
3220be2b1797SNate Lawson     /* Core system ioctls. */
322115e32d5dSMike Smith     switch (cmd) {
322200a30448SNate Lawson     case ACPIIO_REQSLPSTATE:
322300a30448SNate Lawson 	state = *(int *)addr;
322400a30448SNate Lawson 	if (state != ACPI_STATE_S5)
3225a0e73a12SJung-uk Kim 	    return (acpi_ReqSleepState(sc, state));
3226a0e73a12SJung-uk Kim 	device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n");
3227a0e73a12SJung-uk Kim 	error = EOPNOTSUPP;
322800a30448SNate Lawson 	break;
322900a30448SNate Lawson     case ACPIIO_ACKSLPSTATE:
323000a30448SNate Lawson 	error = *(int *)addr;
323100a30448SNate Lawson 	error = acpi_AckSleepState(sc->acpi_clone, error);
323200a30448SNate Lawson 	break;
323300a30448SNate Lawson     case ACPIIO_SETSLPSTATE:	/* DEPRECATED */
323415e32d5dSMike Smith 	state = *(int *)addr;
3235a0e73a12SJung-uk Kim 	if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX)
3236a0e73a12SJung-uk Kim 	    return (EINVAL);
3237a0e73a12SJung-uk Kim 	if (!acpi_sleep_states[state])
3238a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3239a0e73a12SJung-uk Kim 	if (ACPI_FAILURE(acpi_SetSleepState(sc, state)))
3240a0e73a12SJung-uk Kim 	    error = ENXIO;
324115e32d5dSMike Smith 	break;
324215e32d5dSMike Smith     default:
3243bee4aa4aSNate Lawson 	error = ENXIO;
324415e32d5dSMike Smith 	break;
324515e32d5dSMike Smith     }
3246917d44c8SMitsuru IWASAKI 
324715e32d5dSMike Smith     return (error);
324815e32d5dSMike Smith }
324915e32d5dSMike Smith 
32501d073b1dSJohn Baldwin static int
3251a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname)
3252a0e73a12SJung-uk Kim {
3253a0e73a12SJung-uk Kim     int sstate;
3254a0e73a12SJung-uk Kim 
3255a0e73a12SJung-uk Kim     if (toupper(sname[0]) == 'S') {
3256a0e73a12SJung-uk Kim 	sstate = sname[1] - '0';
3257a0e73a12SJung-uk Kim 	if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 &&
3258a0e73a12SJung-uk Kim 	    sname[2] == '\0')
3259a0e73a12SJung-uk Kim 	    return (sstate);
3260a0e73a12SJung-uk Kim     } else if (strcasecmp(sname, "NONE") == 0)
3261a0e73a12SJung-uk Kim 	return (ACPI_STATE_UNKNOWN);
3262a0e73a12SJung-uk Kim     return (-1);
3263a0e73a12SJung-uk Kim }
3264a0e73a12SJung-uk Kim 
3265a0e73a12SJung-uk Kim static const char *
3266a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate)
3267a0e73a12SJung-uk Kim {
3268a0e73a12SJung-uk Kim     static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" };
3269a0e73a12SJung-uk Kim 
3270a0e73a12SJung-uk Kim     if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5)
3271a0e73a12SJung-uk Kim 	return (snames[sstate]);
3272a0e73a12SJung-uk Kim     else if (sstate == ACPI_STATE_UNKNOWN)
3273a0e73a12SJung-uk Kim 	return ("NONE");
3274a0e73a12SJung-uk Kim     return (NULL);
3275a0e73a12SJung-uk Kim }
3276a0e73a12SJung-uk Kim 
3277a0e73a12SJung-uk Kim static int
3278d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
3279d75de536SMitsuru IWASAKI {
3280d75de536SMitsuru IWASAKI     int error;
3281bee4aa4aSNate Lawson     struct sbuf sb;
3282a0e73a12SJung-uk Kim     UINT8 state;
3283d75de536SMitsuru IWASAKI 
3284bee4aa4aSNate Lawson     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
3285a0e73a12SJung-uk Kim     for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++)
3286a0e73a12SJung-uk Kim 	if (acpi_sleep_states[state])
3287a0e73a12SJung-uk Kim 	    sbuf_printf(&sb, "%s ", acpi_sstate2sname(state));
3288bee4aa4aSNate Lawson     sbuf_trim(&sb);
3289bee4aa4aSNate Lawson     sbuf_finish(&sb);
3290bee4aa4aSNate Lawson     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
3291bee4aa4aSNate Lawson     sbuf_delete(&sb);
3292d75de536SMitsuru IWASAKI     return (error);
3293d75de536SMitsuru IWASAKI }
3294d75de536SMitsuru IWASAKI 
3295d75de536SMitsuru IWASAKI static int
32961d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
32971d073b1dSJohn Baldwin {
32981d073b1dSJohn Baldwin     char sleep_state[10];
3299a0e73a12SJung-uk Kim     int error, new_state, old_state;
33001d073b1dSJohn Baldwin 
3301a0e73a12SJung-uk Kim     old_state = *(int *)oidp->oid_arg1;
3302a0e73a12SJung-uk Kim     strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state));
33031d073b1dSJohn Baldwin     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
33041d073b1dSJohn Baldwin     if (error == 0 && req->newptr != NULL) {
3305a0e73a12SJung-uk Kim 	new_state = acpi_sname2sstate(sleep_state);
3306a0e73a12SJung-uk Kim 	if (new_state < ACPI_STATE_S1)
3307a0e73a12SJung-uk Kim 	    return (EINVAL);
330854b43614SJung-uk Kim 	if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state])
3309a0e73a12SJung-uk Kim 	    return (EOPNOTSUPP);
3310be2b1797SNate Lawson 	if (new_state != old_state)
3311a0e73a12SJung-uk Kim 	    *(int *)oidp->oid_arg1 = new_state;
33121d073b1dSJohn Baldwin     }
33131d073b1dSJohn Baldwin     return (error);
33141d073b1dSJohn Baldwin }
33151d073b1dSJohn Baldwin 
33169b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */
33179b937d48SNate Lawson void
33189b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
33199b937d48SNate Lawson {
33209b937d48SNate Lawson     char		notify_buf[16];
33219b937d48SNate Lawson     ACPI_BUFFER		handle_buf;
33229b937d48SNate Lawson     ACPI_STATUS		status;
33239b937d48SNate Lawson 
33249b937d48SNate Lawson     if (subsystem == NULL)
33259b937d48SNate Lawson 	return;
33269b937d48SNate Lawson 
33279b937d48SNate Lawson     handle_buf.Pointer = NULL;
33289b937d48SNate Lawson     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
33299b937d48SNate Lawson     status = AcpiNsHandleToPathname(h, &handle_buf);
33309b937d48SNate Lawson     if (ACPI_FAILURE(status))
33319b937d48SNate Lawson 	return;
33329b937d48SNate Lawson     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
33339b937d48SNate Lawson     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
33349b937d48SNate Lawson     AcpiOsFree(handle_buf.Pointer);
33359b937d48SNate Lawson }
33369b937d48SNate Lawson 
333715e32d5dSMike Smith #ifdef ACPI_DEBUG
33380ae55423SMike Smith /*
33390ae55423SMike Smith  * Support for parsing debug options from the kernel environment.
33400ae55423SMike Smith  *
33410ae55423SMike Smith  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
33420ae55423SMike Smith  * by specifying the names of the bits in the debug.acpi.layer and
33430ae55423SMike Smith  * debug.acpi.level environment variables.  Bits may be unset by
33440ae55423SMike Smith  * prefixing the bit name with !.
33450ae55423SMike Smith  */
334615e32d5dSMike Smith struct debugtag
334715e32d5dSMike Smith {
334815e32d5dSMike Smith     char	*name;
334915e32d5dSMike Smith     UINT32	value;
335015e32d5dSMike Smith };
335115e32d5dSMike Smith 
335215e32d5dSMike Smith static struct debugtag	dbg_layer[] = {
3353c5ba0be4SMike Smith     {"ACPI_UTILITIES",		ACPI_UTILITIES},
3354c5ba0be4SMike Smith     {"ACPI_HARDWARE",		ACPI_HARDWARE},
3355c5ba0be4SMike Smith     {"ACPI_EVENTS",		ACPI_EVENTS},
3356c5ba0be4SMike Smith     {"ACPI_TABLES",		ACPI_TABLES},
3357c5ba0be4SMike Smith     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
3358c5ba0be4SMike Smith     {"ACPI_PARSER",		ACPI_PARSER},
3359c5ba0be4SMike Smith     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
3360c5ba0be4SMike Smith     {"ACPI_EXECUTER",		ACPI_EXECUTER},
3361c5ba0be4SMike Smith     {"ACPI_RESOURCES",		ACPI_RESOURCES},
3362d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
33634c1cdee6SMike Smith     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
3364d62ab2f4SMitsuru IWASAKI     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
3365297835bcSNate Lawson     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
33664c1cdee6SMike Smith 
3367c5ba0be4SMike Smith     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
3368c5ba0be4SMike Smith     {"ACPI_BATTERY",		ACPI_BATTERY},
33693184cf5aSNate Lawson     {"ACPI_BUS",		ACPI_BUS},
3370c5ba0be4SMike Smith     {"ACPI_BUTTON",		ACPI_BUTTON},
33713184cf5aSNate Lawson     {"ACPI_EC", 		ACPI_EC},
33723184cf5aSNate Lawson     {"ACPI_FAN",		ACPI_FAN},
33733184cf5aSNate Lawson     {"ACPI_POWERRES",		ACPI_POWERRES},
33744c1cdee6SMike Smith     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
3375cb9b0d80SMike Smith     {"ACPI_THERMAL",		ACPI_THERMAL},
33763184cf5aSNate Lawson     {"ACPI_TIMER",		ACPI_TIMER},
3377b53f2771SMike Smith     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
337815e32d5dSMike Smith     {NULL, 0}
337915e32d5dSMike Smith };
338015e32d5dSMike Smith 
338115e32d5dSMike Smith static struct debugtag dbg_level[] = {
33829501b603SJohn Baldwin     {"ACPI_LV_INIT",		ACPI_LV_INIT},
33834c1cdee6SMike Smith     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
33849501b603SJohn Baldwin     {"ACPI_LV_INFO",		ACPI_LV_INFO},
33854c1cdee6SMike Smith     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
3386d62ab2f4SMitsuru IWASAKI 
3387d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 1 [Standard Trace Level] */
3388297835bcSNate Lawson     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
33894c1cdee6SMike Smith     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
33904c1cdee6SMike Smith     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
3391d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
33924c1cdee6SMike Smith     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
33934c1cdee6SMike Smith     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
33944c1cdee6SMike Smith     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
33954c1cdee6SMike Smith     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
33964c1cdee6SMike Smith     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
33974c1cdee6SMike Smith     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
33984c1cdee6SMike Smith     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
33994c1cdee6SMike Smith     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
34004c1cdee6SMike Smith     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
34014c1cdee6SMike Smith     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
3402d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
3403d62ab2f4SMitsuru IWASAKI 
3404d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 2 [Function tracing and memory allocation] */
3405d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
3406d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
3407d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
3408d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
34094c1cdee6SMike Smith     {"ACPI_LV_ALL",		ACPI_LV_ALL},
3410d62ab2f4SMitsuru IWASAKI 
3411d62ab2f4SMitsuru IWASAKI     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
3412d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
3413d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
3414d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_IO",		ACPI_LV_IO},
3415d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
3416d62ab2f4SMitsuru IWASAKI     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
3417d62ab2f4SMitsuru IWASAKI 
3418d62ab2f4SMitsuru IWASAKI     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
341998479b04SMitsuru IWASAKI     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
342098479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
342198479b04SMitsuru IWASAKI     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
342298479b04SMitsuru IWASAKI     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
342398479b04SMitsuru IWASAKI     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
342415e32d5dSMike Smith     {NULL, 0}
342515e32d5dSMike Smith };
342615e32d5dSMike Smith 
342715e32d5dSMike Smith static void
342815e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
342915e32d5dSMike Smith {
343015e32d5dSMike Smith     char	*ep;
343115e32d5dSMike Smith     int		i, l;
34320ae55423SMike Smith     int		set;
343315e32d5dSMike Smith 
343415e32d5dSMike Smith     while (*cp) {
343515e32d5dSMike Smith 	if (isspace(*cp)) {
343615e32d5dSMike Smith 	    cp++;
343715e32d5dSMike Smith 	    continue;
343815e32d5dSMike Smith 	}
343915e32d5dSMike Smith 	ep = cp;
344015e32d5dSMike Smith 	while (*ep && !isspace(*ep))
344115e32d5dSMike Smith 	    ep++;
34420ae55423SMike Smith 	if (*cp == '!') {
34430ae55423SMike Smith 	    set = 0;
34440ae55423SMike Smith 	    cp++;
34450ae55423SMike Smith 	    if (cp == ep)
34460ae55423SMike Smith 		continue;
34470ae55423SMike Smith 	} else {
34480ae55423SMike Smith 	    set = 1;
34490ae55423SMike Smith 	}
345015e32d5dSMike Smith 	l = ep - cp;
345115e32d5dSMike Smith 	for (i = 0; tag[i].name != NULL; i++) {
345215e32d5dSMike Smith 	    if (!strncmp(cp, tag[i].name, l)) {
3453be2b1797SNate Lawson 		if (set)
345415e32d5dSMike Smith 		    *flag |= tag[i].value;
3455be2b1797SNate Lawson 		else
34560ae55423SMike Smith 		    *flag &= ~tag[i].value;
345715e32d5dSMike Smith 	    }
345815e32d5dSMike Smith 	}
345915e32d5dSMike Smith 	cp = ep;
346015e32d5dSMike Smith     }
346115e32d5dSMike Smith }
346215e32d5dSMike Smith 
346315e32d5dSMike Smith static void
34642a4ac806SMike Smith acpi_set_debugging(void *junk)
346515e32d5dSMike Smith {
34667e639165SNate Lawson     char	*layer, *level;
346715e32d5dSMike Smith 
34681d7b121cSNate Lawson     if (cold) {
34690ae55423SMike Smith 	AcpiDbgLayer = 0;
34700ae55423SMike Smith 	AcpiDbgLevel = 0;
34711d7b121cSNate Lawson     }
34721d7b121cSNate Lawson 
34737e639165SNate Lawson     layer = getenv("debug.acpi.layer");
34747e639165SNate Lawson     level = getenv("debug.acpi.level");
34757e639165SNate Lawson     if (layer == NULL && level == NULL)
34767e639165SNate Lawson 	return;
34770ae55423SMike Smith 
34787e639165SNate Lawson     printf("ACPI set debug");
34797e639165SNate Lawson     if (layer != NULL) {
34807e639165SNate Lawson 	if (strcmp("NONE", layer) != 0)
34817e639165SNate Lawson 	    printf(" layer '%s'", layer);
34827e639165SNate Lawson 	acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
34837e639165SNate Lawson 	freeenv(layer);
34841d7b121cSNate Lawson     }
34857e639165SNate Lawson     if (level != NULL) {
34867e639165SNate Lawson 	if (strcmp("NONE", level) != 0)
34877e639165SNate Lawson 	    printf(" level '%s'", level);
34887e639165SNate Lawson 	acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
34897e639165SNate Lawson 	freeenv(level);
34907e639165SNate Lawson     }
34917e639165SNate Lawson     printf("\n");
349215e32d5dSMike Smith }
3493bee4aa4aSNate Lawson 
3494be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
3495be2b1797SNate Lawson 	NULL);
34961d7b121cSNate Lawson 
34971d7b121cSNate Lawson static int
34981d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
34991d7b121cSNate Lawson {
350054f6bca0SNate Lawson     int		 error, *dbg;
35011d7b121cSNate Lawson     struct	 debugtag *tag;
350254f6bca0SNate Lawson     struct	 sbuf sb;
35031d7b121cSNate Lawson 
350454f6bca0SNate Lawson     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
350554f6bca0SNate Lawson 	return (ENOMEM);
35061d7b121cSNate Lawson     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
35071d7b121cSNate Lawson 	tag = &dbg_layer[0];
35081d7b121cSNate Lawson 	dbg = &AcpiDbgLayer;
35091d7b121cSNate Lawson     } else {
35101d7b121cSNate Lawson 	tag = &dbg_level[0];
35111d7b121cSNate Lawson 	dbg = &AcpiDbgLevel;
35121d7b121cSNate Lawson     }
35131d7b121cSNate Lawson 
35141d7b121cSNate Lawson     /* Get old values if this is a get request. */
351515e2f34fSNate Lawson     ACPI_SERIAL_BEGIN(acpi);
35161d7b121cSNate Lawson     if (*dbg == 0) {
351754f6bca0SNate Lawson 	sbuf_cpy(&sb, "NONE");
35181d7b121cSNate Lawson     } else if (req->newptr == NULL) {
35191d7b121cSNate Lawson 	for (; tag->name != NULL; tag++) {
352054f6bca0SNate Lawson 	    if ((*dbg & tag->value) == tag->value)
352154f6bca0SNate Lawson 		sbuf_printf(&sb, "%s ", tag->name);
35221d7b121cSNate Lawson 	}
35231d7b121cSNate Lawson     }
352454f6bca0SNate Lawson     sbuf_trim(&sb);
352554f6bca0SNate Lawson     sbuf_finish(&sb);
35261d7b121cSNate Lawson 
35277e639165SNate Lawson     /* Copy out the old values to the user. */
35287e639165SNate Lawson     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
352954f6bca0SNate Lawson     sbuf_delete(&sb);
35301d7b121cSNate Lawson 
35311d7b121cSNate Lawson     /* If the user is setting a string, parse it. */
35321d7b121cSNate Lawson     if (error == 0 && req->newptr != NULL) {
35331d7b121cSNate Lawson 	*dbg = 0;
35341d7b121cSNate Lawson 	setenv((char *)oidp->oid_arg1, (char *)req->newptr);
35351d7b121cSNate Lawson 	acpi_set_debugging(NULL);
35361d7b121cSNate Lawson     }
353715e2f34fSNate Lawson     ACPI_SERIAL_END(acpi);
35381d7b121cSNate Lawson 
35391d7b121cSNate Lawson     return (error);
35401d7b121cSNate Lawson }
3541bee4aa4aSNate Lawson 
35421d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
35431d7b121cSNate Lawson 	    "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
35441d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
35451d7b121cSNate Lawson 	    "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
3546bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */
3547f9390180SMitsuru IWASAKI 
3548f9390180SMitsuru IWASAKI static int
35492a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS)
35502a18c71dSJung-uk Kim {
35512a18c71dSJung-uk Kim 	int	error;
35522a18c71dSJung-uk Kim 	int	old;
35532a18c71dSJung-uk Kim 
35542a18c71dSJung-uk Kim 	old = acpi_debug_objects;
35552a18c71dSJung-uk Kim 	error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req);
35562a18c71dSJung-uk Kim 	if (error != 0 || req->newptr == NULL)
35572a18c71dSJung-uk Kim 		return (error);
35582a18c71dSJung-uk Kim 	if (old == acpi_debug_objects || (old && acpi_debug_objects))
35592a18c71dSJung-uk Kim 		return (0);
35602a18c71dSJung-uk Kim 
35612a18c71dSJung-uk Kim 	ACPI_SERIAL_BEGIN(acpi);
35622a18c71dSJung-uk Kim 	AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE;
35632a18c71dSJung-uk Kim 	ACPI_SERIAL_END(acpi);
35642a18c71dSJung-uk Kim 
35652a18c71dSJung-uk Kim 	return (0);
35662a18c71dSJung-uk Kim }
35672a18c71dSJung-uk Kim 
35682a18c71dSJung-uk Kim static int
3569f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...)
3570f9390180SMitsuru IWASAKI {
3571f9390180SMitsuru IWASAKI 	int	state, acpi_state;
3572f9390180SMitsuru IWASAKI 	int	error;
3573f9390180SMitsuru IWASAKI 	struct	acpi_softc *sc;
3574f9390180SMitsuru IWASAKI 	va_list	ap;
3575f9390180SMitsuru IWASAKI 
3576f9390180SMitsuru IWASAKI 	error = 0;
3577f9390180SMitsuru IWASAKI 	switch (cmd) {
3578f9390180SMitsuru IWASAKI 	case POWER_CMD_SUSPEND:
3579f9390180SMitsuru IWASAKI 		sc = (struct acpi_softc *)arg;
3580f9390180SMitsuru IWASAKI 		if (sc == NULL) {
3581f9390180SMitsuru IWASAKI 			error = EINVAL;
3582f9390180SMitsuru IWASAKI 			goto out;
3583f9390180SMitsuru IWASAKI 		}
3584f9390180SMitsuru IWASAKI 
3585f9390180SMitsuru IWASAKI 		va_start(ap, arg);
3586f9390180SMitsuru IWASAKI 		state = va_arg(ap, int);
3587f9390180SMitsuru IWASAKI 		va_end(ap);
3588f9390180SMitsuru IWASAKI 
3589f9390180SMitsuru IWASAKI 		switch (state) {
3590f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_STANDBY:
3591f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_standby_sx;
3592f9390180SMitsuru IWASAKI 			break;
3593f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_SUSPEND:
3594f9390180SMitsuru IWASAKI 			acpi_state = sc->acpi_suspend_sx;
3595f9390180SMitsuru IWASAKI 			break;
3596f9390180SMitsuru IWASAKI 		case POWER_SLEEP_STATE_HIBERNATE:
3597f9390180SMitsuru IWASAKI 			acpi_state = ACPI_STATE_S4;
3598f9390180SMitsuru IWASAKI 			break;
3599f9390180SMitsuru IWASAKI 		default:
3600f9390180SMitsuru IWASAKI 			error = EINVAL;
3601f9390180SMitsuru IWASAKI 			goto out;
3602f9390180SMitsuru IWASAKI 		}
3603f9390180SMitsuru IWASAKI 
360400a30448SNate Lawson 		if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state)))
360500a30448SNate Lawson 			error = ENXIO;
3606f9390180SMitsuru IWASAKI 		break;
3607f9390180SMitsuru IWASAKI 	default:
3608f9390180SMitsuru IWASAKI 		error = EINVAL;
3609f9390180SMitsuru IWASAKI 		goto out;
3610f9390180SMitsuru IWASAKI 	}
3611f9390180SMitsuru IWASAKI 
3612f9390180SMitsuru IWASAKI out:
3613f9390180SMitsuru IWASAKI 	return (error);
3614f9390180SMitsuru IWASAKI }
3615f9390180SMitsuru IWASAKI 
3616f9390180SMitsuru IWASAKI static void
3617f9390180SMitsuru IWASAKI acpi_pm_register(void *arg)
3618f9390180SMitsuru IWASAKI {
3619be2b1797SNate Lawson     if (!cold || resource_disabled("acpi", 0))
36206c407052SMitsuru IWASAKI 	return;
3621f9390180SMitsuru IWASAKI 
3622f9390180SMitsuru IWASAKI     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
3623f9390180SMitsuru IWASAKI }
3624f9390180SMitsuru IWASAKI 
3625f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
3626