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 #include <sys/sched.h> 50eeb3a05fSNate Lawson #include <sys/smp.h> 51c66d2b38SJung-uk Kim #include <sys/timetc.h> 5215e32d5dSMike Smith 53d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 54d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h> 55d320e05cSJohn Baldwin #endif 5615e32d5dSMike Smith #include <machine/resource.h> 57dc750869SNate Lawson #include <machine/bus.h> 58dc750869SNate Lawson #include <sys/rman.h> 59bc0f2195SMike Smith #include <isa/isavar.h> 60c796e27bSNate Lawson #include <isa/pnpvar.h> 61bc0f2195SMike Smith 62129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h> 63129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h> 64129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h> 65129d3046SJung-uk Kim 6615e32d5dSMike Smith #include <dev/acpica/acpivar.h> 6715e32d5dSMike Smith #include <dev/acpica/acpiio.h> 6815e32d5dSMike Smith 692be4e471SJung-uk Kim #include <vm/vm_param.h> 702be4e471SJung-uk Kim 7115e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 7215e32d5dSMike Smith 73be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 74cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 75b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 760ae55423SMike Smith 7715e32d5dSMike Smith static d_open_t acpiopen; 7815e32d5dSMike Smith static d_close_t acpiclose; 7915e32d5dSMike Smith static d_ioctl_t acpiioctl; 8015e32d5dSMike Smith 8115e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 82dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 837ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 847ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 857ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 867ac40f5fSPoul-Henning Kamp .d_name = "acpi", 8715e32d5dSMike Smith }; 8815e32d5dSMike Smith 89ae19af49SJung-uk Kim struct acpi_interface { 90ae19af49SJung-uk Kim ACPI_STRING *data; 91ae19af49SJung-uk Kim int num; 92ae19af49SJung-uk Kim }; 93ae19af49SJung-uk Kim 94346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 95cb9b0d80SMike Smith struct mtx acpi_mutex; 96cb9b0d80SMike Smith 9789fb5af8SNate Lawson /* Bitmap of device quirks. */ 9889fb5af8SNate Lawson int acpi_quirks; 9989fb5af8SNate Lawson 100a0e73a12SJung-uk Kim /* Supported sleep states. */ 101a0e73a12SJung-uk Kim static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 102a0e73a12SJung-uk Kim 1036d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10415e32d5dSMike Smith static int acpi_probe(device_t dev); 10515e32d5dSMike Smith static int acpi_attach(device_t dev); 10610ce62b9SNate Lawson static int acpi_suspend(device_t dev); 10710ce62b9SNate Lawson static int acpi_resume(device_t dev); 108169b539aSNate Lawson static int acpi_shutdown(device_t dev); 1093d844eddSAndriy Gapon static device_t acpi_add_child(device_t bus, u_int order, const char *name, 110be2b1797SNate Lawson int unit); 11115e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 11210ce62b9SNate Lawson static void acpi_probe_nomatch(device_t bus, device_t child); 11310ce62b9SNate Lawson static void acpi_driver_added(device_t dev, driver_t *driver); 114be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 115be2b1797SNate Lawson uintptr_t *result); 116be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 117be2b1797SNate Lawson uintptr_t value); 11891233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 119ea233199SJohn Baldwin static void acpi_reserve_resources(device_t dev); 120adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 121ea233199SJohn Baldwin static int acpi_set_resource(device_t dev, device_t child, int type, 122ea233199SJohn Baldwin int rid, u_long start, u_long count); 123be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 124be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 125be2b1797SNate Lawson u_long count, u_int flags); 126be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 127be2b1797SNate Lawson int rid, struct resource *r); 1288b28b622SNate Lawson static void acpi_delete_resource(device_t bus, device_t child, int type, 1298b28b622SNate Lawson int rid); 1301e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1311e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 132ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 133ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 134ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 135ce619b2eSNate Lawson ACPI_BUFFER *ret); 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); 140a7a3177fSJung-uk Kim static int acpi_set_powerstate(device_t child, int state); 141be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 142be2b1797SNate Lawson struct isa_pnp_id *ids); 14315e32d5dSMike Smith static void acpi_probe_children(device_t bus); 144d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order); 145be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 146be2b1797SNate Lawson void *context, void **status); 147a0e73a12SJung-uk Kim static void acpi_sleep_enable(void *arg); 148a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 14900a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 15015e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 15113d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 152d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 153d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 154d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 15588a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 15688a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 15715e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 15815e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 159a0e73a12SJung-uk Kim static int acpi_sname2sstate(const char *sname); 160a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate); 161d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1621d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1632a18c71dSJung-uk Kim static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 164f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 165879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 166879d6c23STakanori Watanabe char *buf, size_t buflen); 167879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 168879d6c23STakanori Watanabe char *buf, size_t buflen); 169d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 170d320e05cSJohn Baldwin static void acpi_enable_pcie(void); 171d320e05cSJohn Baldwin #endif 1720d484d24SJohn Baldwin static void acpi_hint_device_unit(device_t acdev, device_t child, 1730d484d24SJohn Baldwin const char *name, int *unitp); 174ae19af49SJung-uk Kim static void acpi_reset_interfaces(device_t dev); 175879d6c23STakanori Watanabe 17615e32d5dSMike Smith static device_method_t acpi_methods[] = { 17715e32d5dSMike Smith /* Device interface */ 17815e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 17915e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 180169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 18156a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 18210ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend), 18310ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume), 18415e32d5dSMike Smith 18515e32d5dSMike Smith /* Bus interface */ 18615e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 18715e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 18810ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 18910ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added), 19015e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 19115e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 19291233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 193ea233199SJohn Baldwin DEVMETHOD(bus_set_resource, acpi_set_resource), 19491233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 19515e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 19615e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 1978b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource), 198879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 199879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 20015e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 20115e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 20215e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 20315e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 2040d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 20515e32d5dSMike Smith 206ce619b2eSNate Lawson /* ACPI bus */ 207ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 208ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 20910ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 210df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 211ce619b2eSNate Lawson 212bc0f2195SMike Smith /* ISA emulation */ 213bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 214bc0f2195SMike Smith 21515e32d5dSMike Smith {0, 0} 21615e32d5dSMike Smith }; 21715e32d5dSMike Smith 21815e32d5dSMike Smith static driver_t acpi_driver = { 21915e32d5dSMike Smith "acpi", 22015e32d5dSMike Smith acpi_methods, 22115e32d5dSMike Smith sizeof(struct acpi_softc), 22215e32d5dSMike Smith }; 22315e32d5dSMike Smith 2243273b005SMike Smith static devclass_t acpi_devclass; 2256d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 226a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 22715e32d5dSMike Smith 22815e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 22915e2f34fSNate Lawson 230adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 231adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 232adad4744SNate Lawson 233bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 234bee4aa4aSNate Lawson 2355217af30SJohn Baldwin /* Holds the description of the acpi0 device. */ 2365217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 2375217af30SJohn Baldwin 238197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 2391d7b121cSNate Lawson static char acpi_ca_version[12]; 2401d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2411d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 24215e32d5dSMike Smith 24315e32d5dSMike Smith /* 244ae19af49SJung-uk Kim * Allow overriding _OSI methods. 245ae19af49SJung-uk Kim */ 246ae19af49SJung-uk Kim static char acpi_install_interface[256]; 247ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface, 248ae19af49SJung-uk Kim sizeof(acpi_install_interface)); 249ae19af49SJung-uk Kim static char acpi_remove_interface[256]; 250ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface, 251ae19af49SJung-uk Kim sizeof(acpi_remove_interface)); 252ae19af49SJung-uk Kim 253ae19af49SJung-uk Kim /* 254413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 255c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 256c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 257c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 258c9b8d77dSNate Lawson * some IBMs use such code. 259413081d7SNate Lawson */ 260c9b8d77dSNate Lawson static int acpi_serialize_methods; 261413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 262413081d7SNate Lawson 2632a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */ 2642a18c71dSJung-uk Kim static int acpi_debug_objects; 2652a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 2662a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 2672a18c71dSJung-uk Kim CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 2682a18c71dSJung-uk Kim "Enable Debug objects"); 2692a18c71dSJung-uk Kim 2702a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */ 2712a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1; 2722a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 2732a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 2742a18c71dSJung-uk Kim &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 2752a18c71dSJung-uk Kim 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 489ae19af49SJung-uk Kim /* Override OS interfaces if the user requested. */ 490ae19af49SJung-uk Kim acpi_reset_interfaces(dev); 491ae19af49SJung-uk Kim 4922be4e471SJung-uk Kim /* Load ACPI name space. */ 4932be4e471SJung-uk Kim status = AcpiLoadTables(); 4942be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4952be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n", 4962be4e471SJung-uk Kim AcpiFormatException(status)); 4972be4e471SJung-uk Kim goto out; 4982be4e471SJung-uk Kim } 4992be4e471SJung-uk Kim 500d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 501d320e05cSJohn Baldwin /* Handle MCFG table if present. */ 502d320e05cSJohn Baldwin acpi_enable_pcie(); 503d320e05cSJohn Baldwin #endif 504d320e05cSJohn Baldwin 50515e32d5dSMike Smith /* 506be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 507be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 508be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 509be2b1797SNate Lawson * object init pass. 51015e32d5dSMike Smith * 51132d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 51232d18aa5SMike Smith * 513be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 514be2b1797SNate Lawson * all our child devices, but on many systems it works here. 51515e32d5dSMike Smith */ 51632d18aa5SMike Smith flags = 0; 517d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 51832d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 519bee4aa4aSNate Lawson 520bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 521b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 522be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 523be2b1797SNate Lawson AcpiFormatException(status)); 524cb9b0d80SMike Smith goto out; 52515e32d5dSMike Smith } 52615e32d5dSMike Smith 527f8335e3aSNate Lawson /* 528f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 529f8335e3aSNate Lawson * the namespace has been evaluated. 530da72d149SNate Lawson * 531da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and 532da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't 533da72d149SNate Lawson * a problem but should be addressed eventually. 534f8335e3aSNate Lawson */ 535f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 536f8335e3aSNate Lawson 537bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 538b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 539be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 540be2b1797SNate Lawson AcpiFormatException(status)); 541b69ed3f4SMitsuru IWASAKI goto out; 542b69ed3f4SMitsuru IWASAKI } 543b69ed3f4SMitsuru IWASAKI 54415e32d5dSMike Smith /* 5451d073b1dSJohn Baldwin * Setup our sysctl tree. 5461d073b1dSJohn Baldwin * 5471d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5481d073b1dSJohn Baldwin */ 5491d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5501d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5511d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5521d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5531d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 554d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 555d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 556d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5571d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5581d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5591d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5601d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5611d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5621d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5631d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5641d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 565f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 566f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 567f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 568f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 569f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 570f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5712d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 572197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 573197b4dccSNate Lawson "sleep delay"); 574ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 575197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 5761611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 577197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 578d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 579d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 580d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 581d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 582d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW, 583d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 584bf0c18ecSNate Lawson 585bf0c18ecSNate Lawson /* 5862b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 587bf0c18ecSNate Lawson * stabilize. 588bf0c18ecSNate Lawson */ 5892b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5902d644607SMitsuru IWASAKI if (bootverbose) 5912d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 592965a34fbSNate Lawson if ((env = getenv("hw.acpi.verbose")) != NULL) { 593965a34fbSNate Lawson if (strcmp(env, "0") != 0) 594498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 595498d464fSMitsuru IWASAKI freeenv(env); 596498d464fSMitsuru IWASAKI } 5971d073b1dSJohn Baldwin 598ac731af5SJung-uk Kim /* Only enable reboot by default if the FADT says it is available. */ 599ac731af5SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) 600ac731af5SJung-uk Kim sc->acpi_handle_reboot = 1; 601ac731af5SJung-uk Kim 6022d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 603129d3046SJung-uk Kim if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 6042d610c46SNate Lawson sc->acpi_s4bios = 1; 6052d610c46SNate Lawson 606a0e73a12SJung-uk Kim /* Probe all supported sleep states. */ 607a0e73a12SJung-uk Kim acpi_sleep_states[ACPI_STATE_S0] = TRUE; 608a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 609a0e73a12SJung-uk Kim if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 610a0e73a12SJung-uk Kim acpi_sleep_states[state] = TRUE; 611a0e73a12SJung-uk Kim 6121d073b1dSJohn Baldwin /* 613ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 614a0e73a12SJung-uk Kim * to UNKNOWN by default to avoid surprising users. 61515e32d5dSMike Smith */ 616a0e73a12SJung-uk Kim sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 617a0e73a12SJung-uk Kim ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 618a0e73a12SJung-uk Kim sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 619a0e73a12SJung-uk Kim sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 620a0e73a12SJung-uk Kim ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 621a0e73a12SJung-uk Kim sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 622a0e73a12SJung-uk Kim ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 62315e32d5dSMike Smith 624ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 625a0e73a12SJung-uk Kim sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 62600a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 627a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) { 628ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 629ea27c63eSNate Lawson break; 630ea27c63eSNate Lawson } 631ea27c63eSNate Lawson 63213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 63315e32d5dSMike Smith 63415e32d5dSMike Smith /* 63515e32d5dSMike Smith * Scan the namespace and attach/initialise children. 63615e32d5dSMike Smith */ 63715e32d5dSMike Smith 63859e472e9SNate Lawson /* Register our shutdown handler. */ 639be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 640be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 64115e32d5dSMike Smith 64215e32d5dSMike Smith /* 64315e32d5dSMike Smith * Register our acpi event handlers. 64415e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 64515e32d5dSMike Smith */ 646be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 647be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 648be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 649be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 65015e32d5dSMike Smith 651be2b1797SNate Lawson /* Flag our initial states. */ 652a0e73a12SJung-uk Kim sc->acpi_enabled = TRUE; 65315e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 654a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 65515e32d5dSMike Smith 656be2b1797SNate Lawson /* Create the control device */ 657a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 6582a4689e8SRobert Watson "acpi"); 65915e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 66015e32d5dSMike Smith 661be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 662f86214b6SMitsuru IWASAKI goto out; 663f86214b6SMitsuru IWASAKI 664f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 665f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 666f9390180SMitsuru IWASAKI 667eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 668eeb6dba2SJohn Baldwin acpi_probe_children(dev); 669eeb6dba2SJohn Baldwin 6705a77b11bSJung-uk Kim /* Update all GPEs and enable runtime GPEs. */ 6715a77b11bSJung-uk Kim status = AcpiUpdateAllGpes(); 6725a77b11bSJung-uk Kim if (ACPI_FAILURE(status)) 6735a77b11bSJung-uk Kim device_printf(dev, "Could not update all GPEs: %s\n", 6745a77b11bSJung-uk Kim AcpiFormatException(status)); 6755a77b11bSJung-uk Kim 676a0e73a12SJung-uk Kim /* Allow sleep request after a while. */ 677a0e73a12SJung-uk Kim timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 678a0e73a12SJung-uk Kim 679cb9b0d80SMike Smith error = 0; 680cb9b0d80SMike Smith 681cb9b0d80SMike Smith out: 682cb9b0d80SMike Smith return_VALUE (error); 68315e32d5dSMike Smith } 68415e32d5dSMike Smith 685a7a3177fSJung-uk Kim static void 686a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state) 687a7a3177fSJung-uk Kim { 688a7a3177fSJung-uk Kim device_t child, parent; 689a7a3177fSJung-uk Kim device_t *devlist; 690a7a3177fSJung-uk Kim struct pci_devinfo *dinfo; 691a7a3177fSJung-uk Kim int dstate, i, numdevs; 692a7a3177fSJung-uk Kim 693a7a3177fSJung-uk Kim if (device_get_children(dev, &devlist, &numdevs) != 0) 694a7a3177fSJung-uk Kim return; 695a7a3177fSJung-uk Kim 696a7a3177fSJung-uk Kim /* 697a7a3177fSJung-uk Kim * Retrieve and set D-state for the sleep state if _SxD is present. 698a7a3177fSJung-uk Kim * Skip children who aren't attached since they are handled separately. 699a7a3177fSJung-uk Kim */ 700a7a3177fSJung-uk Kim parent = device_get_parent(dev); 701a7a3177fSJung-uk Kim for (i = 0; i < numdevs; i++) { 702a7a3177fSJung-uk Kim child = devlist[i]; 703a7a3177fSJung-uk Kim dinfo = device_get_ivars(child); 704a7a3177fSJung-uk Kim dstate = state; 705a7a3177fSJung-uk Kim if (device_is_attached(child) && 706a7a3177fSJung-uk Kim acpi_device_pwr_for_sleep(parent, dev, &dstate) == 0) 707a7a3177fSJung-uk Kim acpi_set_powerstate(child, dstate); 708a7a3177fSJung-uk Kim } 709a7a3177fSJung-uk Kim free(devlist, M_TEMP); 710a7a3177fSJung-uk Kim } 711a7a3177fSJung-uk Kim 712169b539aSNate Lawson static int 71310ce62b9SNate Lawson acpi_suspend(device_t dev) 71410ce62b9SNate Lawson { 715a7a3177fSJung-uk Kim int error; 71610ce62b9SNate Lawson 717a56fe095SJohn Baldwin GIANT_REQUIRED; 718a56fe095SJohn Baldwin 71910ce62b9SNate Lawson error = bus_generic_suspend(dev); 720a7a3177fSJung-uk Kim if (error == 0) 721a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D3); 72210ce62b9SNate Lawson 72310ce62b9SNate Lawson return (error); 72410ce62b9SNate Lawson } 72510ce62b9SNate Lawson 72610ce62b9SNate Lawson static int 72710ce62b9SNate Lawson acpi_resume(device_t dev) 72810ce62b9SNate Lawson { 72910ce62b9SNate Lawson 730a56fe095SJohn Baldwin GIANT_REQUIRED; 731a56fe095SJohn Baldwin 732a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D0); 73310ce62b9SNate Lawson 73410ce62b9SNate Lawson return (bus_generic_resume(dev)); 73510ce62b9SNate Lawson } 73610ce62b9SNate Lawson 73710ce62b9SNate Lawson static int 738169b539aSNate Lawson acpi_shutdown(device_t dev) 739169b539aSNate Lawson { 740169b539aSNate Lawson 741a56fe095SJohn Baldwin GIANT_REQUIRED; 742a56fe095SJohn Baldwin 7430e414868SNate Lawson /* Allow children to shutdown first. */ 7440e414868SNate Lawson bus_generic_shutdown(dev); 7450e414868SNate Lawson 746bee4aa4aSNate Lawson /* 747bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 748bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 749bee4aa4aSNate Lawson */ 750d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 751d4b9ff91SNate Lawson 752169b539aSNate Lawson return (0); 753169b539aSNate Lawson } 754169b539aSNate Lawson 75515e32d5dSMike Smith /* 75615e32d5dSMike Smith * Handle a new device being added 75715e32d5dSMike Smith */ 75815e32d5dSMike Smith static device_t 7593d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit) 76015e32d5dSMike Smith { 76115e32d5dSMike Smith struct acpi_device *ad; 76215e32d5dSMike Smith device_t child; 76315e32d5dSMike Smith 764be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 76515e32d5dSMike Smith return (NULL); 76615e32d5dSMike Smith 76715e32d5dSMike Smith resource_list_init(&ad->ad_rl); 76815e32d5dSMike Smith 76915e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 77015e32d5dSMike Smith if (child != NULL) 77115e32d5dSMike Smith device_set_ivars(child, ad); 77243ce1c77SNate Lawson else 77343ce1c77SNate Lawson free(ad, M_ACPIDEV); 77415e32d5dSMike Smith return (child); 77515e32d5dSMike Smith } 77615e32d5dSMike Smith 77715e32d5dSMike Smith static int 77815e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 77915e32d5dSMike Smith { 78015e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 78115e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 78215e32d5dSMike Smith int retval = 0; 78315e32d5dSMike Smith 78415e32d5dSMike Smith retval += bus_print_child_header(bus, child); 7859ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 7869ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 7879ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 7889ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 789a91c5fa8SNate Lawson if (device_get_flags(child)) 790a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 7912c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 79215e32d5dSMike Smith 79315e32d5dSMike Smith return (retval); 79415e32d5dSMike Smith } 79515e32d5dSMike Smith 79610ce62b9SNate Lawson /* 79710ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt 79810ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added. 79910ce62b9SNate Lawson * 80010ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and 80110ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect 80210ce62b9SNate Lawson * them to be powered up. 80310ce62b9SNate Lawson */ 80410ce62b9SNate Lawson static void 80510ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child) 80610ce62b9SNate Lawson { 80796cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 808a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 80996cf0b6dSWarner Losh #endif 81010ce62b9SNate Lawson } 81110ce62b9SNate Lawson 81210ce62b9SNate Lawson /* 81310ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up. 81410ce62b9SNate Lawson * 81510ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details). 81610ce62b9SNate Lawson */ 81710ce62b9SNate Lawson static void 81810ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver) 81910ce62b9SNate Lawson { 82010ce62b9SNate Lawson device_t child, *devlist; 82110ce62b9SNate Lawson int i, numdevs; 82210ce62b9SNate Lawson 82310ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev); 824099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs)) 825099ea4b5SWarner Losh return; 82610ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 82710ce62b9SNate Lawson child = devlist[i]; 82810ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) { 82996cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 830a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D0); 83110ce62b9SNate Lawson if (device_probe_and_attach(child) != 0) 832a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 83396cf0b6dSWarner Losh #else 83496cf0b6dSWarner Losh device_probe_and_attach(child); 83596cf0b6dSWarner Losh #endif 83610ce62b9SNate Lawson } 83710ce62b9SNate Lawson } 83810ce62b9SNate Lawson free(devlist, M_TEMP); 83910ce62b9SNate Lawson } 84010ce62b9SNate Lawson 84163600cc3SNate Lawson /* Location hint for devctl(8) */ 84263600cc3SNate Lawson static int 843247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 844247648afSTakanori Watanabe size_t buflen) 845247648afSTakanori Watanabe { 846247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 847247648afSTakanori Watanabe 848247648afSTakanori Watanabe if (dinfo->ad_handle) 849d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 850247648afSTakanori Watanabe else 851d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 852247648afSTakanori Watanabe return (0); 853247648afSTakanori Watanabe } 854247648afSTakanori Watanabe 85563600cc3SNate Lawson /* PnP information for devctl(8) */ 85663600cc3SNate Lawson static int 857247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 858247648afSTakanori Watanabe size_t buflen) 859247648afSTakanori Watanabe { 86063600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 86192488a57SJung-uk Kim ACPI_DEVICE_INFO *adinfo; 862247648afSTakanori Watanabe 86392488a57SJung-uk Kim if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 864d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 86592488a57SJung-uk Kim return (0); 86692488a57SJung-uk Kim } 86792488a57SJung-uk Kim 868247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 869247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 87092488a57SJung-uk Kim adinfo->HardwareId.String : "none", 87163600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 87292488a57SJung-uk Kim strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 873247648afSTakanori Watanabe AcpiOsFree(adinfo); 874247648afSTakanori Watanabe 875247648afSTakanori Watanabe return (0); 876247648afSTakanori Watanabe } 877247648afSTakanori Watanabe 878247648afSTakanori Watanabe /* 87915e32d5dSMike Smith * Handle per-device ivars 88015e32d5dSMike Smith */ 88115e32d5dSMike Smith static int 88215e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 88315e32d5dSMike Smith { 88415e32d5dSMike Smith struct acpi_device *ad; 88515e32d5dSMike Smith 88615e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 8872d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 88815e32d5dSMike Smith return (ENOENT); 88915e32d5dSMike Smith } 89015e32d5dSMike Smith 891be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 89215e32d5dSMike Smith switch(index) { 89315e32d5dSMike Smith case ACPI_IVAR_HANDLE: 89415e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 89515e32d5dSMike Smith break; 89615e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 89715e32d5dSMike Smith *(void **)result = ad->ad_private; 89815e32d5dSMike Smith break; 899d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 900d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 901d4b9ff91SNate Lawson break; 90232d18aa5SMike Smith case ISA_IVAR_VENDORID: 90332d18aa5SMike Smith case ISA_IVAR_SERIAL: 90432d18aa5SMike Smith case ISA_IVAR_COMPATID: 90532d18aa5SMike Smith *(int *)result = -1; 90632d18aa5SMike Smith break; 90732d18aa5SMike Smith case ISA_IVAR_LOGICALID: 90832d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 90932d18aa5SMike Smith break; 91015e32d5dSMike Smith default: 91115e32d5dSMike Smith return (ENOENT); 91215e32d5dSMike Smith } 913be2b1797SNate Lawson 91415e32d5dSMike Smith return (0); 91515e32d5dSMike Smith } 91615e32d5dSMike Smith 91715e32d5dSMike Smith static int 91815e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 91915e32d5dSMike Smith { 92015e32d5dSMike Smith struct acpi_device *ad; 92115e32d5dSMike Smith 92215e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9232d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 92415e32d5dSMike Smith return (ENOENT); 92515e32d5dSMike Smith } 92615e32d5dSMike Smith 92715e32d5dSMike Smith switch(index) { 92815e32d5dSMike Smith case ACPI_IVAR_HANDLE: 92915e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 93015e32d5dSMike Smith break; 93115e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 93215e32d5dSMike Smith ad->ad_private = (void *)value; 93315e32d5dSMike Smith break; 934d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 935d4b9ff91SNate Lawson ad->ad_flags = (int)value; 936d4b9ff91SNate Lawson break; 93715e32d5dSMike Smith default: 9382ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 93915e32d5dSMike Smith return (ENOENT); 94015e32d5dSMike Smith } 941be2b1797SNate Lawson 94215e32d5dSMike Smith return (0); 94315e32d5dSMike Smith } 94415e32d5dSMike Smith 94515e32d5dSMike Smith /* 94615e32d5dSMike Smith * Handle child resource allocation/removal 94715e32d5dSMike Smith */ 94891233413SNate Lawson static struct resource_list * 94991233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 95015e32d5dSMike Smith { 95191233413SNate Lawson struct acpi_device *ad; 95215e32d5dSMike Smith 95391233413SNate Lawson ad = device_get_ivars(child); 95491233413SNate Lawson return (&ad->ad_rl); 95515e32d5dSMike Smith } 95615e32d5dSMike Smith 9570d484d24SJohn Baldwin static int 9580d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value) 9590d484d24SJohn Baldwin { 9600d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev); 9610d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 9620d484d24SJohn Baldwin struct resource_list_entry *rle; 9630d484d24SJohn Baldwin 9640d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 9650d484d24SJohn Baldwin if (rle->type != type) 9660d484d24SJohn Baldwin continue; 9670d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value) 9680d484d24SJohn Baldwin return (1); 9690d484d24SJohn Baldwin } 9700d484d24SJohn Baldwin return (0); 9710d484d24SJohn Baldwin } 9720d484d24SJohn Baldwin 9730d484d24SJohn Baldwin /* 9740d484d24SJohn Baldwin * Wire device unit numbers based on resource matches in hints. 9750d484d24SJohn Baldwin */ 9760d484d24SJohn Baldwin static void 9770d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 9780d484d24SJohn Baldwin int *unitp) 9790d484d24SJohn Baldwin { 9800d484d24SJohn Baldwin const char *s; 9810d484d24SJohn Baldwin long value; 9820d484d24SJohn Baldwin int line, matches, unit; 9830d484d24SJohn Baldwin 9840d484d24SJohn Baldwin /* 9850d484d24SJohn Baldwin * Iterate over all the hints for the devices with the specified 9860d484d24SJohn Baldwin * name to see if one's resources are a subset of this device. 9870d484d24SJohn Baldwin */ 9880d484d24SJohn Baldwin line = 0; 9890d484d24SJohn Baldwin for (;;) { 9900d484d24SJohn Baldwin if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 9910d484d24SJohn Baldwin break; 9920d484d24SJohn Baldwin 9930d484d24SJohn Baldwin /* Must have an "at" for acpi or isa. */ 9940d484d24SJohn Baldwin resource_string_value(name, unit, "at", &s); 9950d484d24SJohn Baldwin if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 9960d484d24SJohn Baldwin strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 9970d484d24SJohn Baldwin continue; 9980d484d24SJohn Baldwin 9990d484d24SJohn Baldwin /* 10002fcd493cSJohn Baldwin * Check for matching resources. We must have at least one match. 10012fcd493cSJohn Baldwin * Since I/O and memory resources cannot be shared, if we get a 10022fcd493cSJohn Baldwin * match on either of those, ignore any mismatches in IRQs or DRQs. 10030d484d24SJohn Baldwin * 10040d484d24SJohn Baldwin * XXX: We may want to revisit this to be more lenient and wire 10050d484d24SJohn Baldwin * as long as it gets one match. 10060d484d24SJohn Baldwin */ 10070d484d24SJohn Baldwin matches = 0; 10080d484d24SJohn Baldwin if (resource_long_value(name, unit, "port", &value) == 0) { 10092fcd493cSJohn Baldwin /* 10102fcd493cSJohn Baldwin * Floppy drive controllers are notorious for having a 10112fcd493cSJohn Baldwin * wide variety of resources not all of which include the 10122fcd493cSJohn Baldwin * first port that is specified by the hint (typically 10132fcd493cSJohn Baldwin * 0x3f0) (see the comment above fdc_isa_alloc_resources() 10142fcd493cSJohn Baldwin * in fdc_isa.c). However, they do all seem to include 10152fcd493cSJohn Baldwin * port + 2 (e.g. 0x3f2) so for a floppy device, look for 10162fcd493cSJohn Baldwin * 'value + 2' in the port resources instead of the hint 10172fcd493cSJohn Baldwin * value. 10182fcd493cSJohn Baldwin */ 10192fcd493cSJohn Baldwin if (strcmp(name, "fdc") == 0) 10202fcd493cSJohn Baldwin value += 2; 10210d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 10220d484d24SJohn Baldwin matches++; 10230d484d24SJohn Baldwin else 10240d484d24SJohn Baldwin continue; 10250d484d24SJohn Baldwin } 10260d484d24SJohn Baldwin if (resource_long_value(name, unit, "maddr", &value) == 0) { 10270d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 10280d484d24SJohn Baldwin matches++; 10290d484d24SJohn Baldwin else 10300d484d24SJohn Baldwin continue; 10310d484d24SJohn Baldwin } 10322fcd493cSJohn Baldwin if (matches > 0) 10332fcd493cSJohn Baldwin goto matched; 10340d484d24SJohn Baldwin if (resource_long_value(name, unit, "irq", &value) == 0) { 10350d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 10360d484d24SJohn Baldwin matches++; 10370d484d24SJohn Baldwin else 10380d484d24SJohn Baldwin continue; 10390d484d24SJohn Baldwin } 10400d484d24SJohn Baldwin if (resource_long_value(name, unit, "drq", &value) == 0) { 10410d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 10420d484d24SJohn Baldwin matches++; 10430d484d24SJohn Baldwin else 10440d484d24SJohn Baldwin continue; 10450d484d24SJohn Baldwin } 10460d484d24SJohn Baldwin 10472fcd493cSJohn Baldwin matched: 10480d484d24SJohn Baldwin if (matches > 0) { 10490d484d24SJohn Baldwin /* We have a winner! */ 10500d484d24SJohn Baldwin *unitp = unit; 10510d484d24SJohn Baldwin break; 10520d484d24SJohn Baldwin } 10530d484d24SJohn Baldwin } 10540d484d24SJohn Baldwin } 10550d484d24SJohn Baldwin 1056adad4744SNate Lawson /* 1057adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1058adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 1059adad4744SNate Lawson */ 1060adad4744SNate Lawson static int 1061adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 1062adad4744SNate Lawson { 1063adad4744SNate Lawson struct resource *res; 1064adad4744SNate Lawson struct resource_list *rl; 1065adad4744SNate Lawson struct resource_list_entry *rle; 1066adad4744SNate Lawson struct rman *rm; 1067da72d149SNate Lawson char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1068da72d149SNate Lawson device_t *children; 1069da72d149SNate Lawson int child_count, i; 1070da72d149SNate Lawson 1071da72d149SNate Lawson /* 1072da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we 1073da72d149SNate Lawson * had multi-pass probe/attach. 1074da72d149SNate Lawson */ 1075da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0) 1076da72d149SNate Lawson return (ENXIO); 1077da72d149SNate Lawson for (i = 0; i < child_count; i++) { 1078da72d149SNate Lawson if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1079da72d149SNate Lawson device_probe_and_attach(children[i]); 1080da72d149SNate Lawson } 1081da72d149SNate Lawson free(children, M_TEMP); 1082adad4744SNate Lawson 1083adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1084be1bf4d2SPoul-Henning Kamp STAILQ_FOREACH(rle, rl, link) { 1085adad4744SNate Lawson if (rle->res != NULL) { 1086adad4744SNate Lawson device_printf(dev, "duplicate resource for %lx\n", rle->start); 1087adad4744SNate Lawson continue; 1088adad4744SNate Lawson } 1089adad4744SNate Lawson 1090adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 1091adad4744SNate Lawson switch (rle->type) { 1092adad4744SNate Lawson case SYS_RES_IOPORT: 1093adad4744SNate Lawson rm = &acpi_rman_io; 1094adad4744SNate Lawson break; 1095adad4744SNate Lawson case SYS_RES_MEMORY: 1096adad4744SNate Lawson rm = &acpi_rman_mem; 1097adad4744SNate Lawson break; 1098adad4744SNate Lawson default: 1099adad4744SNate Lawson continue; 1100adad4744SNate Lawson } 1101adad4744SNate Lawson 1102adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 1103adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1104adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1105adad4744SNate Lawson if (res != NULL) { 1106adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1107adad4744SNate Lawson rle->res = res; 1108adad4744SNate Lawson } else 1109adad4744SNate Lawson device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 1110adad4744SNate Lawson rle->start, rle->count, rle->type); 1111adad4744SNate Lawson } 1112adad4744SNate Lawson return (0); 1113adad4744SNate Lawson } 1114adad4744SNate Lawson 1115ea233199SJohn Baldwin static char *pcilink_ids[] = { "PNP0C0F", NULL }; 1116ea233199SJohn Baldwin static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1117ea233199SJohn Baldwin 1118ea233199SJohn Baldwin /* 1119ea233199SJohn Baldwin * Reserve declared resources for devices found during attach once system 1120ea233199SJohn Baldwin * resources have been allocated. 1121ea233199SJohn Baldwin */ 1122ea233199SJohn Baldwin static void 1123ea233199SJohn Baldwin acpi_reserve_resources(device_t dev) 1124ea233199SJohn Baldwin { 1125ea233199SJohn Baldwin struct resource_list_entry *rle; 1126ea233199SJohn Baldwin struct resource_list *rl; 1127ea233199SJohn Baldwin struct acpi_device *ad; 1128ea233199SJohn Baldwin struct acpi_softc *sc; 1129ea233199SJohn Baldwin device_t *children; 1130ea233199SJohn Baldwin int child_count, i; 1131ea233199SJohn Baldwin 1132ea233199SJohn Baldwin sc = device_get_softc(dev); 1133ea233199SJohn Baldwin if (device_get_children(dev, &children, &child_count) != 0) 1134ea233199SJohn Baldwin return; 1135ea233199SJohn Baldwin for (i = 0; i < child_count; i++) { 1136ea233199SJohn Baldwin ad = device_get_ivars(children[i]); 1137ea233199SJohn Baldwin rl = &ad->ad_rl; 1138ea233199SJohn Baldwin 1139ea233199SJohn Baldwin /* Don't reserve system resources. */ 1140ea233199SJohn Baldwin if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1141ea233199SJohn Baldwin continue; 1142ea233199SJohn Baldwin 1143ea233199SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 1144ea233199SJohn Baldwin /* 1145ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things 1146ea233199SJohn Baldwin * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET 1147ea233199SJohn Baldwin * when using legacy routing). 1148ea233199SJohn Baldwin */ 1149ea233199SJohn Baldwin if (rle->type == SYS_RES_IRQ) 1150ea233199SJohn Baldwin continue; 1151ea233199SJohn Baldwin 1152ea233199SJohn Baldwin /* 1153*0d81cf12SJohn Baldwin * Don't reserve the resource if it is already allocated. 1154*0d81cf12SJohn Baldwin * The acpi_ec(4) driver can allocate its resources early 1155*0d81cf12SJohn Baldwin * if ECDT is present. 1156*0d81cf12SJohn Baldwin */ 1157*0d81cf12SJohn Baldwin if (rle->res != NULL) 1158*0d81cf12SJohn Baldwin continue; 1159*0d81cf12SJohn Baldwin 1160*0d81cf12SJohn Baldwin /* 1161ea233199SJohn Baldwin * Try to reserve the resource from our parent. If this 1162ea233199SJohn Baldwin * fails because the resource is a system resource, just 1163ea233199SJohn Baldwin * let it be. The resource range is already reserved so 1164ea233199SJohn Baldwin * that other devices will not use it. If the driver 1165ea233199SJohn Baldwin * needs to allocate the resource, then 1166ea233199SJohn Baldwin * acpi_alloc_resource() will sub-alloc from the system 1167ea233199SJohn Baldwin * resource. 1168ea233199SJohn Baldwin */ 1169ea233199SJohn Baldwin resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid, 1170ea233199SJohn Baldwin rle->start, rle->end, rle->count, 0); 1171ea233199SJohn Baldwin } 1172ea233199SJohn Baldwin } 1173ea233199SJohn Baldwin free(children, M_TEMP); 1174ea233199SJohn Baldwin sc->acpi_resources_reserved = 1; 1175ea233199SJohn Baldwin } 1176ea233199SJohn Baldwin 1177ea233199SJohn Baldwin static int 1178ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid, 1179ea233199SJohn Baldwin u_long start, u_long count) 1180ea233199SJohn Baldwin { 1181ea233199SJohn Baldwin struct acpi_softc *sc = device_get_softc(dev); 1182ea233199SJohn Baldwin struct acpi_device *ad = device_get_ivars(child); 1183ea233199SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 1184ea233199SJohn Baldwin u_long end; 1185ea233199SJohn Baldwin 1186ea233199SJohn Baldwin /* Ignore IRQ resources for PCI link devices. */ 1187ea233199SJohn Baldwin if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL) 1188ea233199SJohn Baldwin return (0); 1189ea233199SJohn Baldwin 1190ea233199SJohn Baldwin /* If the resource is already allocated, fail. */ 1191ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) 1192ea233199SJohn Baldwin return (EBUSY); 1193ea233199SJohn Baldwin 1194ea233199SJohn Baldwin /* If the resource is already reserved, release it. */ 1195ea233199SJohn Baldwin if (resource_list_reserved(rl, type, rid)) 1196ea233199SJohn Baldwin resource_list_unreserve(rl, dev, child, type, rid); 1197ea233199SJohn Baldwin 1198ea233199SJohn Baldwin /* Add the resource. */ 1199ea233199SJohn Baldwin end = (start + count - 1); 1200ea233199SJohn Baldwin resource_list_add(rl, type, rid, start, end, count); 1201ea233199SJohn Baldwin 1202ea233199SJohn Baldwin /* Don't reserve resources until the system resources are allocated. */ 1203ea233199SJohn Baldwin if (!sc->acpi_resources_reserved) 1204ea233199SJohn Baldwin return (0); 1205ea233199SJohn Baldwin 1206ea233199SJohn Baldwin /* Don't reserve system resources. */ 1207ea233199SJohn Baldwin if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL) 1208ea233199SJohn Baldwin return (0); 1209ea233199SJohn Baldwin 1210ea233199SJohn Baldwin /* 1211ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things to 1212ea233199SJohn Baldwin * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when 1213ea233199SJohn Baldwin * using legacy routing). 1214ea233199SJohn Baldwin */ 1215ea233199SJohn Baldwin if (type == SYS_RES_IRQ) 1216ea233199SJohn Baldwin return (0); 1217ea233199SJohn Baldwin 1218ea233199SJohn Baldwin /* 1219ea233199SJohn Baldwin * Reserve the resource. 1220ea233199SJohn Baldwin * 1221ea233199SJohn Baldwin * XXX: Ignores failure for now. Failure here is probably a 1222ea233199SJohn Baldwin * BIOS/firmware bug? 1223ea233199SJohn Baldwin */ 1224ea233199SJohn Baldwin resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0); 1225ea233199SJohn Baldwin return (0); 1226ea233199SJohn Baldwin } 1227ea233199SJohn Baldwin 122815e32d5dSMike Smith static struct resource * 122915e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 123015e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 123115e32d5dSMike Smith { 123295957f62SJohn Baldwin ACPI_RESOURCE ares; 1233ea233199SJohn Baldwin struct acpi_device *ad; 123491233413SNate Lawson struct resource_list_entry *rle; 1235ea233199SJohn Baldwin struct resource_list *rl; 123691233413SNate Lawson struct resource *res; 123791233413SNate Lawson struct rman *rm; 1238ea233199SJohn Baldwin int isdefault = (start == 0UL && end == ~0UL); 123915e2f34fSNate Lawson 124091233413SNate Lawson /* 1241ea233199SJohn Baldwin * First attempt at allocating the resource. For direct children, 1242ea233199SJohn Baldwin * use resource_list_alloc() to handle reserved resources. For 1243ea233199SJohn Baldwin * other dveices, pass the request up to our parent. 124491233413SNate Lawson */ 1245ea233199SJohn Baldwin if (bus == device_get_parent(child)) { 1246ea233199SJohn Baldwin ad = device_get_ivars(child); 1247ea233199SJohn Baldwin rl = &ad->ad_rl; 124891233413SNate Lawson 1249397c30a8SJohn Baldwin /* 1250ea233199SJohn Baldwin * Simulate the behavior of the ISA bus for direct children 1251ea233199SJohn Baldwin * devices. That is, if a non-default range is specified for 1252ea233199SJohn Baldwin * a resource that doesn't exist, use bus_set_resource() to 1253ea233199SJohn Baldwin * add the resource before allocating it. Note that these 1254ea233199SJohn Baldwin * resources will not be reserved. 1255397c30a8SJohn Baldwin */ 1256ea233199SJohn Baldwin if (!isdefault && resource_list_find(rl, type, *rid) == NULL) 1257ea233199SJohn Baldwin resource_list_add(rl, type, *rid, start, end, count); 1258ea233199SJohn Baldwin res = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 1259ea233199SJohn Baldwin flags); 1260ea233199SJohn Baldwin if (res != NULL && type == SYS_RES_IRQ) { 126195957f62SJohn Baldwin /* 126295957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 126395957f62SJohn Baldwin * configure the interrupt associated with a device when we 126495957f62SJohn Baldwin * parse the resources but have to defer it until a driver 126595957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 126695957f62SJohn Baldwin * 126795957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 126895957f62SJohn Baldwin */ 126995957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 127095957f62SJohn Baldwin acpi_config_intr(child, &ares); 127195957f62SJohn Baldwin } 127215e2f34fSNate Lawson 1273ea233199SJohn Baldwin /* 1274ea233199SJohn Baldwin * If this is an allocation of the "default" range for a given 1275ea233199SJohn Baldwin * RID, fetch the exact bounds for this resource from the 1276ea233199SJohn Baldwin * resource list entry to try to allocate the range from the 1277ea233199SJohn Baldwin * system resource regions. 1278ea233199SJohn Baldwin */ 1279ea233199SJohn Baldwin if (res == NULL && isdefault) { 1280ea233199SJohn Baldwin rle = resource_list_find(rl, type, *rid); 1281ea233199SJohn Baldwin if (rle != NULL) { 1282ea233199SJohn Baldwin start = rle->start; 1283ea233199SJohn Baldwin end = rle->end; 1284ea233199SJohn Baldwin count = rle->count; 1285ea233199SJohn Baldwin } 1286ea233199SJohn Baldwin } 1287ea233199SJohn Baldwin } else 1288ea233199SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1289ea233199SJohn Baldwin start, end, count, flags); 1290ea233199SJohn Baldwin if (res != NULL || start + count - 1 != end) 1291ea233199SJohn Baldwin return (res); 1292ea233199SJohn Baldwin 1293ea233199SJohn Baldwin /* 1294ea233199SJohn Baldwin * If the first attempt failed and this is an allocation of a 1295ea233199SJohn Baldwin * specific range, try to satisfy the request via a suballocation 1296ea233199SJohn Baldwin * from our system resource regions. Note that we only handle 1297ea233199SJohn Baldwin * memory and I/O port system resources. 1298ea233199SJohn Baldwin */ 1299ea233199SJohn Baldwin switch (type) { 1300ea233199SJohn Baldwin case SYS_RES_IOPORT: 1301ea233199SJohn Baldwin rm = &acpi_rman_io; 1302ea233199SJohn Baldwin break; 1303ea233199SJohn Baldwin case SYS_RES_MEMORY: 1304ea233199SJohn Baldwin rm = &acpi_rman_mem; 1305ea233199SJohn Baldwin break; 1306ea233199SJohn Baldwin default: 1307ea233199SJohn Baldwin return (NULL); 1308ea233199SJohn Baldwin } 1309ea233199SJohn Baldwin 1310ea233199SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1311ea233199SJohn Baldwin child); 1312ea233199SJohn Baldwin if (res == NULL) 1313ea233199SJohn Baldwin return (NULL); 1314ea233199SJohn Baldwin 1315ea233199SJohn Baldwin rman_set_rid(res, *rid); 1316ea233199SJohn Baldwin 1317ea233199SJohn Baldwin /* If requested, activate the resource using the parent's method. */ 1318ea233199SJohn Baldwin if (flags & RF_ACTIVE) 1319ea233199SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 1320ea233199SJohn Baldwin rman_release_resource(res); 1321ea233199SJohn Baldwin return (NULL); 1322ea233199SJohn Baldwin } 1323ea233199SJohn Baldwin 132491233413SNate Lawson return (res); 132515e32d5dSMike Smith } 132615e32d5dSMike Smith 132715e32d5dSMike Smith static int 132891233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 132991233413SNate Lawson struct resource *r) 133015e32d5dSMike Smith { 1331397c30a8SJohn Baldwin struct rman *rm; 133291233413SNate Lawson int ret; 133315e32d5dSMike Smith 1334397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1335397c30a8SJohn Baldwin switch (type) { 1336397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1337397c30a8SJohn Baldwin rm = &acpi_rman_io; 1338397c30a8SJohn Baldwin break; 1339397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1340397c30a8SJohn Baldwin rm = &acpi_rman_mem; 1341397c30a8SJohn Baldwin break; 1342397c30a8SJohn Baldwin default: 1343397c30a8SJohn Baldwin rm = NULL; 1344397c30a8SJohn Baldwin } 1345397c30a8SJohn Baldwin 134691233413SNate Lawson /* 1347397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers, 1348ea233199SJohn Baldwin * deactivate it and release it to the local pool. 134991233413SNate Lawson */ 1350397c30a8SJohn Baldwin if (rm != NULL && rman_is_region_manager(r, rm)) { 135191233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 135291233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 135391233413SNate Lawson if (ret != 0) 135491233413SNate Lawson return (ret); 135515e32d5dSMike Smith } 1356ea233199SJohn Baldwin return (rman_release_resource(r)); 1357ea233199SJohn Baldwin } 1358ea233199SJohn Baldwin 1359ea233199SJohn Baldwin return (bus_generic_rl_release_resource(bus, child, type, rid, r)); 1360ea233199SJohn Baldwin } 136115e32d5dSMike Smith 13628b28b622SNate Lawson static void 13638b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid) 13648b28b622SNate Lawson { 13658b28b622SNate Lawson struct resource_list *rl; 13668b28b622SNate Lawson 13678b28b622SNate Lawson rl = acpi_get_rlist(bus, child); 1368ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) { 1369ea233199SJohn Baldwin device_printf(bus, "delete_resource: Resource still owned by child" 1370ea233199SJohn Baldwin " (type=%d, rid=%d)\n", type, rid); 1371ea233199SJohn Baldwin return; 1372ea233199SJohn Baldwin } 1373ea233199SJohn Baldwin resource_list_unreserve(rl, bus, child, type, rid); 13748b28b622SNate Lawson resource_list_delete(rl, type, rid); 13758b28b622SNate Lawson } 13768b28b622SNate Lawson 1377dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 1378e1c4bf3fSNate Lawson int 1379e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1380907b6777SNate Lawson struct resource **res, u_int flags) 1381dc750869SNate Lawson { 1382e1c4bf3fSNate Lawson int error, res_type; 1383dc750869SNate Lawson 1384e1c4bf3fSNate Lawson error = ENOMEM; 13858f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1386e1c4bf3fSNate Lawson return (EINVAL); 1387dc750869SNate Lawson 13888f118e25SNate Lawson /* We only support memory and IO spaces. */ 13892be4e471SJung-uk Kim switch (gas->SpaceId) { 1390dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1391e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY; 1392dc750869SNate Lawson break; 1393dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 1394e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT; 1395dc750869SNate Lawson break; 1396dc750869SNate Lawson default: 1397e1c4bf3fSNate Lawson return (EOPNOTSUPP); 1398dc750869SNate Lawson } 1399dc750869SNate Lawson 14002fe912dfSNate Lawson /* 1401f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means 1402f45fc848SNate Lawson * it is a bit field and just allocate a byte. 14032fe912dfSNate Lawson */ 14042be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8) 14052be4e471SJung-uk Kim gas->BitWidth = 8; 14062fe912dfSNate Lawson 14078f118e25SNate Lawson /* Validate the address after we're sure we support the space. */ 14082be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0) 14098f118e25SNate Lawson return (EINVAL); 14108f118e25SNate Lawson 1411e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address, 14122be4e471SJung-uk Kim gas->BitWidth / 8); 1413907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1414e1c4bf3fSNate Lawson if (*res != NULL) { 1415e1c4bf3fSNate Lawson *type = res_type; 1416e1c4bf3fSNate Lawson error = 0; 1417ca2c69c8SNate Lawson } else 1418ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid); 1419ca2c69c8SNate Lawson 1420e1c4bf3fSNate Lawson return (error); 1421dc750869SNate Lawson } 1422dc750869SNate Lawson 1423c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 14241e4925e8SNate Lawson static uint32_t 142532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 1426bc0f2195SMike Smith { 14271e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1428bc0f2195SMike Smith ACPI_HANDLE h; 142992488a57SJung-uk Kim uint32_t pnpid; 143032d18aa5SMike Smith 1431b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 143232d18aa5SMike Smith 14336fca9360SNate Lawson /* Fetch and validate the HID. */ 143492488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 143592488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 143692488a57SJung-uk Kim return_VALUE (0); 143732d18aa5SMike Smith 143892488a57SJung-uk Kim pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 143992488a57SJung-uk Kim devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 144092488a57SJung-uk Kim PNP_EISAID(devinfo->HardwareId.String) : 0; 144192488a57SJung-uk Kim AcpiOsFree(devinfo); 1442be2b1797SNate Lawson 144332d18aa5SMike Smith return_VALUE (pnpid); 144432d18aa5SMike Smith } 144532d18aa5SMike Smith 14461e4925e8SNate Lawson static int 14471e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1448b2566207STakanori Watanabe { 14491e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 145092488a57SJung-uk Kim ACPI_DEVICE_ID *ids; 1451b2566207STakanori Watanabe ACPI_HANDLE h; 14521e4925e8SNate Lawson uint32_t *pnpid; 145392488a57SJung-uk Kim int i, valid; 1454b2566207STakanori Watanabe 1455b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1456b2566207STakanori Watanabe 14571e4925e8SNate Lawson pnpid = cids; 1458bd189fe7SNate Lawson 14591e4925e8SNate Lawson /* Fetch and validate the CID */ 146092488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 146192488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 146292488a57SJung-uk Kim return_VALUE (0); 1463b2566207STakanori Watanabe 146492488a57SJung-uk Kim if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 146592488a57SJung-uk Kim AcpiOsFree(devinfo); 146692488a57SJung-uk Kim return_VALUE (0); 1467b2566207STakanori Watanabe } 1468b2566207STakanori Watanabe 146992488a57SJung-uk Kim if (devinfo->CompatibleIdList.Count < count) 147092488a57SJung-uk Kim count = devinfo->CompatibleIdList.Count; 147192488a57SJung-uk Kim ids = devinfo->CompatibleIdList.Ids; 147292488a57SJung-uk Kim for (i = 0, valid = 0; i < count; i++) 147392488a57SJung-uk Kim if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 147492488a57SJung-uk Kim strncmp(ids[i].String, "PNP", 3) == 0) { 147592488a57SJung-uk Kim *pnpid++ = PNP_EISAID(ids[i].String); 147692488a57SJung-uk Kim valid++; 147792488a57SJung-uk Kim } 147892488a57SJung-uk Kim AcpiOsFree(devinfo); 147992488a57SJung-uk Kim 14801e4925e8SNate Lawson return_VALUE (valid); 14811e4925e8SNate Lawson } 1482b2566207STakanori Watanabe 1483ce619b2eSNate Lawson static char * 1484ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1485ce619b2eSNate Lawson { 1486ce619b2eSNate Lawson ACPI_HANDLE h; 148792488a57SJung-uk Kim ACPI_OBJECT_TYPE t; 1488ce619b2eSNate Lawson int i; 1489ce619b2eSNate Lawson 1490ce619b2eSNate Lawson h = acpi_get_handle(dev); 149192488a57SJung-uk Kim if (ids == NULL || h == NULL) 149292488a57SJung-uk Kim return (NULL); 149392488a57SJung-uk Kim t = acpi_get_type(dev); 149492488a57SJung-uk Kim if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 1495ce619b2eSNate Lawson return (NULL); 1496ce619b2eSNate Lawson 1497ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1498ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1499ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1500ce619b2eSNate Lawson return (ids[i]); 1501ce619b2eSNate Lawson } 1502ce619b2eSNate Lawson return (NULL); 1503ce619b2eSNate Lawson } 1504ce619b2eSNate Lawson 1505ce619b2eSNate Lawson static ACPI_STATUS 1506ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1507ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1508ce619b2eSNate Lawson { 1509ce619b2eSNate Lawson ACPI_HANDLE h; 1510ce619b2eSNate Lawson 1511df8d2a32SNate Lawson if (dev == NULL) 1512df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1513df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1514ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1515ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1516ce619b2eSNate Lawson } 1517ce619b2eSNate Lawson 151862508c53SJohn Baldwin int 151910ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 152010ce62b9SNate Lawson { 152110ce62b9SNate Lawson struct acpi_softc *sc; 152210ce62b9SNate Lawson ACPI_HANDLE handle; 152310ce62b9SNate Lawson ACPI_STATUS status; 152410ce62b9SNate Lawson char sxd[8]; 152510ce62b9SNate Lawson 152610ce62b9SNate Lawson handle = acpi_get_handle(dev); 152710ce62b9SNate Lawson 152810ce62b9SNate Lawson /* 152910ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down. 153010ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when 153110ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may 153210ce62b9SNate Lawson * need special handling in their drivers. 153310ce62b9SNate Lawson */ 1534a7a3177fSJung-uk Kim if (dstate == NULL || handle == NULL || 153510ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") || 153610ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") || 153710ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") || 153810ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") || 153910ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511")) 154010ce62b9SNate Lawson return (ENXIO); 154110ce62b9SNate Lawson 154210ce62b9SNate Lawson /* 1543a7a3177fSJung-uk Kim * Override next state with the value from _SxD, if present. 1544a7a3177fSJung-uk Kim * Note illegal _S0D is evaluated because some systems expect this. 154510ce62b9SNate Lawson */ 1546a7a3177fSJung-uk Kim sc = device_get_softc(bus); 154710ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 154810ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate); 1549a7a3177fSJung-uk Kim if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1550a7a3177fSJung-uk Kim device_printf(dev, "failed to get %s on %s: %s\n", sxd, 1551a7a3177fSJung-uk Kim acpi_name(handle), AcpiFormatException(status)); 1552a7a3177fSJung-uk Kim return (ENXIO); 155310ce62b9SNate Lawson } 155410ce62b9SNate Lawson 1555a7a3177fSJung-uk Kim return (0); 155610ce62b9SNate Lawson } 155710ce62b9SNate Lawson 1558df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1559df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1560df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1561df8d2a32SNate Lawson void *arg; 1562df8d2a32SNate Lawson ACPI_HANDLE parent; 1563df8d2a32SNate Lawson }; 1564df8d2a32SNate Lawson 1565ce619b2eSNate Lawson static ACPI_STATUS 1566df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1567df8d2a32SNate Lawson { 1568df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1569df8d2a32SNate Lawson device_t dev, old_dev; 1570df8d2a32SNate Lawson ACPI_STATUS status; 1571df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1572df8d2a32SNate Lawson 1573df8d2a32SNate Lawson /* 1574df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1575df8d2a32SNate Lawson * the parent where the scan began. 1576df8d2a32SNate Lawson */ 1577df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1578df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1579df8d2a32SNate Lawson return (AE_OK); 1580df8d2a32SNate Lawson 1581df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1582df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1583df8d2a32SNate Lawson return (AE_OK); 1584df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1585df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1586df8d2a32SNate Lawson return (AE_OK); 1587df8d2a32SNate Lawson 1588df8d2a32SNate Lawson /* 1589df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1590df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1591df8d2a32SNate Lawson */ 1592df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1593df8d2a32SNate Lawson dev = old_dev; 1594df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1595df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1596df8d2a32SNate Lawson return (status); 1597df8d2a32SNate Lawson 1598df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1599df8d2a32SNate Lawson if (old_dev != NULL) { 1600df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1601df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1602df8d2a32SNate Lawson } 1603df8d2a32SNate Lawson 1604df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1605df8d2a32SNate Lawson if (dev != NULL) 1606df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1607df8d2a32SNate Lawson 1608df8d2a32SNate Lawson return (AE_OK); 1609df8d2a32SNate Lawson } 1610df8d2a32SNate Lawson 1611df8d2a32SNate Lawson static ACPI_STATUS 1612df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1613df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1614ce619b2eSNate Lawson { 1615ce619b2eSNate Lawson ACPI_HANDLE h; 1616df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1617ce619b2eSNate Lawson 1618df8d2a32SNate Lawson if (acpi_disabled("children")) 1619df8d2a32SNate Lawson return (AE_OK); 1620df8d2a32SNate Lawson 1621df8d2a32SNate Lawson if (dev == NULL) 1622df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1623df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1624ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1625df8d2a32SNate Lawson ctx.user_fn = user_fn; 1626df8d2a32SNate Lawson ctx.arg = arg; 1627df8d2a32SNate Lawson ctx.parent = h; 1628df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 16292272d050SJung-uk Kim acpi_device_scan_cb, NULL, &ctx, NULL)); 1630ce619b2eSNate Lawson } 1631ce619b2eSNate Lawson 163210ce62b9SNate Lawson /* 163310ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting 163410ce62b9SNate Lawson * device power states since it's close enough to ACPI. 163510ce62b9SNate Lawson */ 163610ce62b9SNate Lawson static int 1637a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state) 163810ce62b9SNate Lawson { 163910ce62b9SNate Lawson ACPI_HANDLE h; 164010ce62b9SNate Lawson ACPI_STATUS status; 164110ce62b9SNate Lawson 164210ce62b9SNate Lawson h = acpi_get_handle(child); 1643a0e73a12SJung-uk Kim if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 164410ce62b9SNate Lawson return (EINVAL); 164510ce62b9SNate Lawson if (h == NULL) 164610ce62b9SNate Lawson return (0); 164710ce62b9SNate Lawson 164810ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */ 164910ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state); 1650a7a3177fSJung-uk Kim if (ACPI_SUCCESS(status)) { 1651a7a3177fSJung-uk Kim if (bootverbose) 1652a7a3177fSJung-uk Kim device_printf(child, "set ACPI power state D%d on %s\n", 1653a7a3177fSJung-uk Kim state, acpi_name(h)); 1654a7a3177fSJung-uk Kim } else if (status != AE_NOT_FOUND) 1655a7a3177fSJung-uk Kim device_printf(child, 1656a7a3177fSJung-uk Kim "failed to set ACPI power state D%d on %s: %s\n", state, 1657a7a3177fSJung-uk Kim acpi_name(h), AcpiFormatException(status)); 165810ce62b9SNate Lawson 1659a7a3177fSJung-uk Kim return (0); 166010ce62b9SNate Lawson } 166110ce62b9SNate Lawson 166232d18aa5SMike Smith static int 166332d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 166432d18aa5SMike Smith { 16651e4925e8SNate Lawson int result, cid_count, i; 16661e4925e8SNate Lawson uint32_t lid, cids[8]; 1667bc0f2195SMike Smith 1668b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1669bc0f2195SMike Smith 1670bc0f2195SMike Smith /* 1671bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1672bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1673bc0f2195SMike Smith * that to happen, so don't ever return it. 1674bc0f2195SMike Smith */ 1675bc0f2195SMike Smith result = ENXIO; 1676bc0f2195SMike Smith 1677be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1678b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 16791e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1680bc0f2195SMike Smith while (ids && ids->ip_id) { 16811e4925e8SNate Lawson if (lid == ids->ip_id) { 1682bc0f2195SMike Smith result = 0; 1683bc0f2195SMike Smith goto out; 1684bc0f2195SMike Smith } 16851e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 16861e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 16871e4925e8SNate Lawson result = 0; 16881e4925e8SNate Lawson goto out; 16891e4925e8SNate Lawson } 16901e4925e8SNate Lawson } 1691bc0f2195SMike Smith ids++; 1692bc0f2195SMike Smith } 1693be2b1797SNate Lawson 1694bc0f2195SMike Smith out: 16959e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 16969e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 16979e0dd54fSNate Lawson 1698bc0f2195SMike Smith return_VALUE (result); 1699bc0f2195SMike Smith } 1700bc0f2195SMike Smith 1701d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1702d320e05cSJohn Baldwin /* 1703d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for 1704d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory 1705d320e05cSJohn Baldwin * map. 1706d320e05cSJohn Baldwin */ 1707d320e05cSJohn Baldwin static void 1708d320e05cSJohn Baldwin acpi_enable_pcie(void) 1709d320e05cSJohn Baldwin { 1710d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr; 1711d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end; 1712d320e05cSJohn Baldwin ACPI_STATUS status; 1713d320e05cSJohn Baldwin 1714d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1715d320e05cSJohn Baldwin if (ACPI_FAILURE(status)) 1716d320e05cSJohn Baldwin return; 1717d320e05cSJohn Baldwin 1718d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1719d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1720d320e05cSJohn Baldwin while (alloc < end) { 1721d320e05cSJohn Baldwin if (alloc->PciSegment == 0) { 1722d320e05cSJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1723d320e05cSJohn Baldwin alloc->EndBusNumber); 1724d320e05cSJohn Baldwin return; 1725d320e05cSJohn Baldwin } 1726d320e05cSJohn Baldwin alloc++; 1727d320e05cSJohn Baldwin } 1728d320e05cSJohn Baldwin } 1729d320e05cSJohn Baldwin #endif 1730d320e05cSJohn Baldwin 1731bc0f2195SMike Smith /* 173233332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices. 173315e32d5dSMike Smith * 173433332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 173533332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 173633332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link 173733332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the 173833332dc2SNate Lawson * type of namespace nodes, so this should be ok. 173915e32d5dSMike Smith */ 174015e32d5dSMike Smith static void 174115e32d5dSMike Smith acpi_probe_children(device_t bus) 174215e32d5dSMike Smith { 174315e32d5dSMike Smith 1744b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 17450ae55423SMike Smith 174615e32d5dSMike Smith /* 174715e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1748edc13633SNate Lawson * we find. We also probe/attach any early devices. 174915e32d5dSMike Smith * 175015e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1751be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1752be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1753be2b1797SNate Lawson * devices as they appear, which might be smarter.) 175415e32d5dSMike Smith */ 17554c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 175633332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 17572272d050SJung-uk Kim NULL, bus, NULL); 175815e32d5dSMike Smith 1759adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1760adad4744SNate Lawson acpi_sysres_alloc(bus); 1761adad4744SNate Lawson 1762ea233199SJohn Baldwin /* Reserve resources already allocated to children. */ 1763ea233199SJohn Baldwin acpi_reserve_resources(bus); 1764ea233199SJohn Baldwin 1765edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1766edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1767edc13633SNate Lawson bus_generic_probe(bus); 1768edc13633SNate Lawson 17695e66b8e2SJohn Baldwin /* Probe/attach all children, created statically and from the namespace. */ 17700430ba9eSAndriy Gapon ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 177115e32d5dSMike Smith bus_generic_attach(bus); 17720ae55423SMike Smith 177388a79fc0SNate Lawson /* Attach wake sysctls. */ 17745c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 177588a79fc0SNate Lawson 1776bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 17770ae55423SMike Smith return_VOID; 177815e32d5dSMike Smith } 177915e32d5dSMike Smith 1780b82ca9a9SNate Lawson /* 1781d227204dSJohn Baldwin * Determine the probe order for a given device. 1782b82ca9a9SNate Lawson */ 1783d227204dSJohn Baldwin static void 1784b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 178591233413SNate Lawson { 1786463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type; 178791233413SNate Lawson 1788b82ca9a9SNate Lawson /* 1789b1f9b996SAndriy Gapon * 1. CPUs 1790b1f9b996SAndriy Gapon * 2. I/O port and memory system resource holders 1791b1f9b996SAndriy Gapon * 3. Embedded controllers (to handle early accesses) 1792b1f9b996SAndriy Gapon * 4. PCI Link Devices 1793b82ca9a9SNate Lawson */ 1794463e0f91SJohn Baldwin AcpiGetType(handle, &type); 1795aa835160SAndriy Gapon if (type == ACPI_TYPE_PROCESSOR) 179691233413SNate Lawson *order = 1; 1797aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) 179891233413SNate Lawson *order = 2; 1799aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C09")) 1800b460c6f8SJohn Baldwin *order = 3; 1801aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C0F")) 1802aa835160SAndriy Gapon *order = 4; 180391233413SNate Lawson } 180491233413SNate Lawson 180515e32d5dSMike Smith /* 180615e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 180715e32d5dSMike Smith * it. 180815e32d5dSMike Smith */ 180915e32d5dSMike Smith static ACPI_STATUS 181015e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 181115e32d5dSMike Smith { 18125a77b11bSJung-uk Kim struct acpi_prw_data prw; 181315e32d5dSMike Smith ACPI_OBJECT_TYPE type; 1814858a52f4SMitsuru IWASAKI ACPI_HANDLE h; 181533332dc2SNate Lawson device_t bus, child; 1816495a4144SJung-uk Kim char *handle_str; 1817da72d149SNate Lawson int order; 181815e32d5dSMike Smith 1819b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 18200ae55423SMike Smith 1821495a4144SJung-uk Kim if (acpi_disabled("children")) 1822495a4144SJung-uk Kim return_ACPI_STATUS (AE_OK); 1823495a4144SJung-uk Kim 1824be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 18250ae55423SMike Smith if (acpi_avoid(handle)) 18260ae55423SMike Smith return_ACPI_STATUS (AE_OK); 18270ae55423SMike Smith 182891233413SNate Lawson bus = (device_t)context; 1829b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1830495a4144SJung-uk Kim handle_str = acpi_name(handle); 183115e32d5dSMike Smith switch (type) { 183215e32d5dSMike Smith case ACPI_TYPE_DEVICE: 1833495a4144SJung-uk Kim /* 1834495a4144SJung-uk Kim * Since we scan from \, be sure to skip system scope objects. 1835495a4144SJung-uk Kim * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around 1836affa1826SJung-uk Kim * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run 1837495a4144SJung-uk Kim * during the intialization and \_TZ_ is to support Notify() on it. 1838495a4144SJung-uk Kim */ 1839495a4144SJung-uk Kim if (strcmp(handle_str, "\\_SB_") == 0 || 1840495a4144SJung-uk Kim strcmp(handle_str, "\\_TZ_") == 0) 1841495a4144SJung-uk Kim break; 18425a77b11bSJung-uk Kim if (acpi_parse_prw(handle, &prw) == 0) 18435a77b11bSJung-uk Kim AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit); 1844495a4144SJung-uk Kim /* FALLTHROUGH */ 184515e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 184615e32d5dSMike Smith case ACPI_TYPE_THERMAL: 184715e32d5dSMike Smith case ACPI_TYPE_POWER: 184833332dc2SNate Lawson /* 1849463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the 1850463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run 1851463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1852463e0f91SJohn Baldwin * are reserved for special objects (i.e., system 1853b1f9b996SAndriy Gapon * resources). 185415e32d5dSMike Smith */ 185533332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1856d227204dSJohn Baldwin order = level * 10 + 100; 1857da72d149SNate Lawson acpi_probe_order(handle, &order); 185891233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1859bc0f2195SMike Smith if (child == NULL) 1860bc0f2195SMike Smith break; 186159a890e6SNate Lawson 186259a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 186315e32d5dSMike Smith acpi_set_handle(child, handle); 186459a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1865bc0f2195SMike Smith 1866bc0f2195SMike Smith /* 1867b519f9d6SMike Smith * Check that the device is present. If it's not present, 1868b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1869b519f9d6SMike Smith * the handle, but we don't probe it). 1870893f750aSNate Lawson * 1871893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not 1872893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them 1873893f750aSNate Lawson * anyway since we may enable them later. 1874b519f9d6SMike Smith */ 1875858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1876858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */ 1877858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F")) 1878858a52f4SMitsuru IWASAKI break; 1879858a52f4SMitsuru IWASAKI /* 1880858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system 1881858a52f4SMitsuru IWASAKI * may be undocked at boot. 1882858a52f4SMitsuru IWASAKI */ 1883858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1884858a52f4SMitsuru IWASAKI break; 1885858a52f4SMitsuru IWASAKI 1886b519f9d6SMike Smith device_disable(child); 1887b519f9d6SMike Smith break; 1888b519f9d6SMike Smith } 1889b519f9d6SMike Smith 1890b519f9d6SMike Smith /* 1891bc0f2195SMike Smith * Get the device's resource settings and attach them. 1892bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1893bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1894bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1895bc0f2195SMike Smith * device not to have any resources. 1896bc0f2195SMike Smith */ 189772ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 18980ae55423SMike Smith break; 189915e32d5dSMike Smith } 190015e32d5dSMike Smith } 1901be2b1797SNate Lawson 19020ae55423SMike Smith return_ACPI_STATUS (AE_OK); 190315e32d5dSMike Smith } 190415e32d5dSMike Smith 190559a890e6SNate Lawson /* 190659a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 190759a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 190859a890e6SNate Lawson */ 190959a890e6SNate Lawson void 191092488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data) 191159a890e6SNate Lawson { 191259a890e6SNate Lawson } 191359a890e6SNate Lawson 191415e32d5dSMike Smith static void 191515e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 191615e32d5dSMike Smith { 19172d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 1918d33f4987STakanori Watanabe ACPI_STATUS status; 1919eeb3a05fSNate Lawson 1920eeb3a05fSNate Lawson /* 192180f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 192280f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 192380f0e4c2SNate Lawson * an AP. 1924eeb3a05fSNate Lawson */ 1925eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1926904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1927904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 19282d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1929904bf0c2SNate Lawson AcpiFormatException(status)); 1930904bf0c2SNate Lawson return; 1931904bf0c2SNate Lawson } 19322d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Powering system off\n"); 193355398047SNate Lawson ACPI_DISABLE_IRQS(); 1934865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 19352d0c82e8SJung-uk Kim if (ACPI_FAILURE(status)) 19362d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - %s\n", 19372d0c82e8SJung-uk Kim AcpiFormatException(status)); 19382d0c82e8SJung-uk Kim else { 193915e32d5dSMike Smith DELAY(1000000); 19402d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 194115e32d5dSMike Smith } 1942ac731af5SJung-uk Kim } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) { 1943d85e6785SNate Lawson /* Reboot using the reset register. */ 1944ac731af5SJung-uk Kim status = AcpiReset(); 1945ac731af5SJung-uk Kim if (ACPI_SUCCESS(status)) { 194687a500cdSNate Lawson DELAY(1000000); 19472d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - timeout\n"); 1948ac731af5SJung-uk Kim } else if (status != AE_NOT_EXIST) 1949ac731af5SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - %s\n", 1950ac731af5SJung-uk Kim AcpiFormatException(status)); 1951d85e6785SNate Lawson } else if (sc->acpi_do_disable && panicstr == NULL) { 1952d85e6785SNate Lawson /* 1953d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing 1954d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system. 1955d85e6785SNate Lawson */ 19562d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Shutting down\n"); 195780f0e4c2SNate Lawson AcpiTerminate(); 195880f0e4c2SNate Lawson } 195915e32d5dSMike Smith } 196015e32d5dSMike Smith 196113d4f7baSMitsuru IWASAKI static void 196213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 196313d4f7baSMitsuru IWASAKI { 196413d4f7baSMitsuru IWASAKI static int first_time = 1; 196513d4f7baSMitsuru IWASAKI 196613d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 19672be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 196859ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 196913d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 197033febf93SNate Lawson acpi_event_power_button_sleep, sc); 1971be2b1797SNate Lawson if (first_time) 19726c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 197313d4f7baSMitsuru IWASAKI } 19742be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 197559ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 197613d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 197733febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1978be2b1797SNate Lawson if (first_time) 19796c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 198013d4f7baSMitsuru IWASAKI } 198113d4f7baSMitsuru IWASAKI 198213d4f7baSMitsuru IWASAKI first_time = 0; 198313d4f7baSMitsuru IWASAKI } 198413d4f7baSMitsuru IWASAKI 198515e32d5dSMike Smith /* 1986c5ba0be4SMike Smith * Returns true if the device is actually present and should 1987c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1988c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1989c5ba0be4SMike Smith */ 1990c5ba0be4SMike Smith BOOLEAN 1991c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1992c5ba0be4SMike Smith { 19931e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1994c5ba0be4SMike Smith ACPI_HANDLE h; 199592488a57SJung-uk Kim BOOLEAN present; 1996c5ba0be4SMike Smith 199792488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 199892488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1999c5ba0be4SMike Smith return (FALSE); 2000be2b1797SNate Lawson 2001be2b1797SNate Lawson /* If no _STA method, must be present */ 200292488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 200392488a57SJung-uk Kim ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2004be2b1797SNate Lawson 200592488a57SJung-uk Kim AcpiOsFree(devinfo); 200692488a57SJung-uk Kim return (present); 2007c5ba0be4SMike Smith } 2008c5ba0be4SMike Smith 2009c5ba0be4SMike Smith /* 2010b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 2011b53f2771SMike Smith */ 2012b53f2771SMike Smith BOOLEAN 2013b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 2014b53f2771SMike Smith { 20151e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 2016b53f2771SMike Smith ACPI_HANDLE h; 201792488a57SJung-uk Kim BOOLEAN present; 2018b53f2771SMike Smith 201992488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 202092488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2021b53f2771SMike Smith return (FALSE); 2022be2b1797SNate Lawson 2023be2b1797SNate Lawson /* If no _STA method, must be present */ 202492488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 202592488a57SJung-uk Kim ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2026be2b1797SNate Lawson 202792488a57SJung-uk Kim AcpiOsFree(devinfo); 202892488a57SJung-uk Kim return (present); 2029b53f2771SMike Smith } 2030b53f2771SMike Smith 2031b53f2771SMike Smith /* 203291233413SNate Lawson * Match a HID string against a handle 203315e32d5dSMike Smith */ 20343c4c08dcSAlexander Motin BOOLEAN 2035ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 203615e32d5dSMike Smith { 20371e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 203892488a57SJung-uk Kim BOOLEAN ret; 203992488a57SJung-uk Kim int i; 204092488a57SJung-uk Kim 204192488a57SJung-uk Kim if (hid == NULL || h == NULL || 204292488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 204392488a57SJung-uk Kim return (FALSE); 204415e32d5dSMike Smith 20451e4925e8SNate Lawson ret = FALSE; 2046c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 204792488a57SJung-uk Kim strcmp(hid, devinfo->HardwareId.String) == 0) 20481e4925e8SNate Lawson ret = TRUE; 204992488a57SJung-uk Kim else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 205092488a57SJung-uk Kim for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 205192488a57SJung-uk Kim if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 20521e4925e8SNate Lawson ret = TRUE; 20531e4925e8SNate Lawson break; 20541e4925e8SNate Lawson } 20551e4925e8SNate Lawson } 2056be2b1797SNate Lawson 205792488a57SJung-uk Kim AcpiOsFree(devinfo); 20581e4925e8SNate Lawson return (ret); 205915e32d5dSMike Smith } 206015e32d5dSMike Smith 206115e32d5dSMike Smith /* 2062c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 2063c5ba0be4SMike Smith * or one if its parents. 2064c5ba0be4SMike Smith */ 2065c5ba0be4SMike Smith ACPI_STATUS 2066c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 2067c5ba0be4SMike Smith { 2068c5ba0be4SMike Smith ACPI_HANDLE r; 2069c5ba0be4SMike Smith ACPI_STATUS status; 2070c5ba0be4SMike Smith 2071be2b1797SNate Lawson /* Walk back up the tree to the root */ 2072c5ba0be4SMike Smith for (;;) { 2073be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 2074be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2075c5ba0be4SMike Smith *result = r; 2076c5ba0be4SMike Smith return (AE_OK); 2077c5ba0be4SMike Smith } 2078bee4aa4aSNate Lawson /* XXX Return error here? */ 2079c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 2080c5ba0be4SMike Smith return (AE_OK); 2081b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 2082c5ba0be4SMike Smith return (AE_NOT_FOUND); 2083c5ba0be4SMike Smith parent = r; 2084c5ba0be4SMike Smith } 2085c5ba0be4SMike Smith } 2086c5ba0be4SMike Smith 2087eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 2088eea17c34SNate Lawson uint32_t 2089eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 2090eea17c34SNate Lawson { 2091eea17c34SNate Lawson uint32_t delta; 2092eea17c34SNate Lawson 2093eea17c34SNate Lawson if (end >= start) 2094eea17c34SNate Lawson delta = end - start; 20952be4e471SJung-uk Kim else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 2096eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 20972be4e471SJung-uk Kim else 20982be4e471SJung-uk Kim delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 2099eea17c34SNate Lawson return (delta); 2100eea17c34SNate Lawson } 2101eea17c34SNate Lawson 2102c5ba0be4SMike Smith /* 2103c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 2104c5ba0be4SMike Smith */ 2105c5ba0be4SMike Smith ACPI_BUFFER * 2106c5ba0be4SMike Smith acpi_AllocBuffer(int size) 2107c5ba0be4SMike Smith { 2108c5ba0be4SMike Smith ACPI_BUFFER *buf; 2109c5ba0be4SMike Smith 2110c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2111c5ba0be4SMike Smith return (NULL); 2112c5ba0be4SMike Smith buf->Length = size; 2113c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 2114c5ba0be4SMike Smith return (buf); 2115c5ba0be4SMike Smith } 2116c5ba0be4SMike Smith 2117c310653eSNate Lawson ACPI_STATUS 2118cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2119c310653eSNate Lawson { 2120c310653eSNate Lawson ACPI_OBJECT arg1; 2121c310653eSNate Lawson ACPI_OBJECT_LIST args; 2122c310653eSNate Lawson 2123c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 2124c310653eSNate Lawson arg1.Integer.Value = number; 2125c310653eSNate Lawson args.Count = 1; 2126c310653eSNate Lawson args.Pointer = &arg1; 2127c310653eSNate Lawson 2128c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 2129c310653eSNate Lawson } 2130c310653eSNate Lawson 2131c5ba0be4SMike Smith /* 2132c5ba0be4SMike Smith * Evaluate a path that should return an integer. 2133c5ba0be4SMike Smith */ 2134c5ba0be4SMike Smith ACPI_STATUS 2135cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2136c5ba0be4SMike Smith { 2137be2b1797SNate Lawson ACPI_STATUS status; 2138c5ba0be4SMike Smith ACPI_BUFFER buf; 2139c573e654SMitsuru IWASAKI ACPI_OBJECT param; 2140c5ba0be4SMike Smith 2141c5ba0be4SMike Smith if (handle == NULL) 2142c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 21430c7da7acSJohn Baldwin 21440c7da7acSJohn Baldwin /* 21450c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 21460c7da7acSJohn Baldwin * a method that will return an Integer. 21470c7da7acSJohn Baldwin */ 2148c5ba0be4SMike Smith buf.Pointer = ¶m; 2149c5ba0be4SMike Smith buf.Length = sizeof(param); 2150be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2151be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2152be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 2153c5ba0be4SMike Smith *number = param.Integer.Value; 2154be2b1797SNate Lawson else 2155be2b1797SNate Lawson status = AE_TYPE; 2156c5ba0be4SMike Smith } 21570c7da7acSJohn Baldwin 21580c7da7acSJohn Baldwin /* 21590c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 21600c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 21610c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 21620c7da7acSJohn Baldwin * convert it into an Integer as best we can. 21630c7da7acSJohn Baldwin * 21640c7da7acSJohn Baldwin * This is a hack. 21650c7da7acSJohn Baldwin */ 2166be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 2167b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2168be2b1797SNate Lawson status = AE_NO_MEMORY; 21690c7da7acSJohn Baldwin } else { 2170be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2171be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 2172be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 21730c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 21740c7da7acSJohn Baldwin } 2175f97739daSNate Lawson } 2176be2b1797SNate Lawson return (status); 2177c5ba0be4SMike Smith } 2178c5ba0be4SMike Smith 2179c573e654SMitsuru IWASAKI ACPI_STATUS 2180cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2181c573e654SMitsuru IWASAKI { 2182c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 2183dba55fa2SNate Lawson UINT8 *val; 2184c573e654SMitsuru IWASAKI int i; 2185c573e654SMitsuru IWASAKI 2186c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 2187c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 2188c573e654SMitsuru IWASAKI *number = p->Integer.Value; 2189c573e654SMitsuru IWASAKI return (AE_OK); 2190c573e654SMitsuru IWASAKI } 2191c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 2192c573e654SMitsuru IWASAKI return (AE_TYPE); 2193c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 2194c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 2195be2b1797SNate Lawson 2196c573e654SMitsuru IWASAKI *number = 0; 2197dba55fa2SNate Lawson val = p->Buffer.Pointer; 2198c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 2199dba55fa2SNate Lawson *number += val[i] << (i * 8); 2200c573e654SMitsuru IWASAKI return (AE_OK); 2201c573e654SMitsuru IWASAKI } 2202c573e654SMitsuru IWASAKI 2203c5ba0be4SMike Smith /* 2204c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 2205c5ba0be4SMike Smith * function for each element. 2206c5ba0be4SMike Smith * 2207c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 2208c5ba0be4SMike Smith */ 2209c5ba0be4SMike Smith ACPI_STATUS 2210be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2211be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2212c5ba0be4SMike Smith { 2213c5ba0be4SMike Smith ACPI_OBJECT *comp; 2214c5ba0be4SMike Smith int i; 2215c5ba0be4SMike Smith 2216be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2217c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 2218c5ba0be4SMike Smith 2219be2b1797SNate Lawson /* Iterate over components */ 2220be2b1797SNate Lawson i = 0; 2221be2b1797SNate Lawson comp = pkg->Package.Elements; 2222be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 2223c5ba0be4SMike Smith func(comp, arg); 2224c5ba0be4SMike Smith 2225c5ba0be4SMike Smith return (AE_OK); 2226c5ba0be4SMike Smith } 2227c5ba0be4SMike Smith 22286f69255bSMike Smith /* 22296f69255bSMike Smith * Find the (index)th resource object in a set. 22306f69255bSMike Smith */ 22316f69255bSMike Smith ACPI_STATUS 22326d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 22336f69255bSMike Smith { 22346d63101aSMike Smith ACPI_RESOURCE *rp; 22356f69255bSMike Smith int i; 22366f69255bSMike Smith 22376d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 22386f69255bSMike Smith i = index; 22396d63101aSMike Smith while (i-- > 0) { 2240be2b1797SNate Lawson /* Range check */ 22416d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 22426f69255bSMike Smith return (AE_BAD_PARAMETER); 2243be2b1797SNate Lawson 2244be2b1797SNate Lawson /* Check for terminator */ 2245e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 22466d63101aSMike Smith return (AE_NOT_FOUND); 2247abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 22486f69255bSMike Smith } 22496f69255bSMike Smith if (resp != NULL) 22506d63101aSMike Smith *resp = rp; 2251be2b1797SNate Lawson 22526d63101aSMike Smith return (AE_OK); 22536d63101aSMike Smith } 22546d63101aSMike Smith 22556d63101aSMike Smith /* 22566d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 22576d63101aSMike Smith * 22586d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 22596d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 22606d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 22616d63101aSMike Smith * resources. 22626d63101aSMike Smith */ 22636d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 22646d63101aSMike Smith 22656d63101aSMike Smith ACPI_STATUS 22666d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 22676d63101aSMike Smith { 22686d63101aSMike Smith ACPI_RESOURCE *rp; 22696d63101aSMike Smith void *newp; 22706d63101aSMike Smith 2271be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 22726d63101aSMike Smith if (buf->Pointer == NULL) { 22736d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 22746d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 22756d63101aSMike Smith return (AE_NO_MEMORY); 22766d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 2277e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 22786d63101aSMike Smith rp->Length = 0; 22796d63101aSMike Smith } 22806d63101aSMike Smith if (res == NULL) 22816d63101aSMike Smith return (AE_OK); 22826d63101aSMike Smith 22836d63101aSMike Smith /* 22846d63101aSMike Smith * Scan the current buffer looking for the terminator. 22856d63101aSMike Smith * This will either find the terminator or hit the end 22866d63101aSMike Smith * of the buffer and return an error. 22876d63101aSMike Smith */ 22886d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 22896d63101aSMike Smith for (;;) { 2290be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 22916d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 22926d63101aSMike Smith return (AE_BAD_PARAMETER); 2293e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 22946d63101aSMike Smith break; 2295abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 22966d63101aSMike Smith } 22976d63101aSMike Smith 22986d63101aSMike Smith /* 22996d63101aSMike Smith * Check the size of the buffer and expand if required. 23006d63101aSMike Smith * 23016d63101aSMike Smith * Required size is: 23026d63101aSMike Smith * size of existing resources before terminator + 23036d63101aSMike Smith * size of new resource and header + 23046d63101aSMike Smith * size of terminator. 23056d63101aSMike Smith * 23066d63101aSMike Smith * Note that this loop should really only run once, unless 23076d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 23086d63101aSMike Smith */ 23096d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2310e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA + 2311e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) { 23126d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 23136d63101aSMike Smith return (AE_NO_MEMORY); 23146d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 2315a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2316a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 23176d63101aSMike Smith AcpiOsFree(buf->Pointer); 23186d63101aSMike Smith buf->Pointer = newp; 23196d63101aSMike Smith buf->Length += buf->Length; 23206d63101aSMike Smith } 23216d63101aSMike Smith 2322be2b1797SNate Lawson /* Insert the new resource. */ 2323e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 23246d63101aSMike Smith 2325be2b1797SNate Lawson /* And add the terminator. */ 2326abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 2327e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 23286d63101aSMike Smith rp->Length = 0; 23296d63101aSMike Smith 23306f69255bSMike Smith return (AE_OK); 23316f69255bSMike Smith } 2332c5ba0be4SMike Smith 2333da14ac9fSJohn Baldwin /* 2334da14ac9fSJohn Baldwin * Set interrupt model. 2335da14ac9fSJohn Baldwin */ 2336da14ac9fSJohn Baldwin ACPI_STATUS 2337da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 2338da14ac9fSJohn Baldwin { 2339da14ac9fSJohn Baldwin 2340c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2341da14ac9fSJohn Baldwin } 2342da14ac9fSJohn Baldwin 234300a30448SNate Lawson /* 2344d95e7f5aSJohn Baldwin * Walk subtables of a table and call a callback routine for each 2345d95e7f5aSJohn Baldwin * subtable. The caller should provide the first subtable and a 2346d95e7f5aSJohn Baldwin * pointer to the end of the table. This can be used to walk tables 2347d95e7f5aSJohn Baldwin * such as MADT and SRAT that use subtable entries. 2348d95e7f5aSJohn Baldwin */ 2349d95e7f5aSJohn Baldwin void 2350d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2351d95e7f5aSJohn Baldwin void *arg) 2352d95e7f5aSJohn Baldwin { 2353d95e7f5aSJohn Baldwin ACPI_SUBTABLE_HEADER *entry; 2354d95e7f5aSJohn Baldwin 2355d95e7f5aSJohn Baldwin for (entry = first; (void *)entry < end; ) { 2356d95e7f5aSJohn Baldwin /* Avoid an infinite loop if we hit a bogus entry. */ 2357d95e7f5aSJohn Baldwin if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2358d95e7f5aSJohn Baldwin return; 2359d95e7f5aSJohn Baldwin 2360d95e7f5aSJohn Baldwin handler(entry, arg); 2361d95e7f5aSJohn Baldwin entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2362d95e7f5aSJohn Baldwin } 2363d95e7f5aSJohn Baldwin } 2364d95e7f5aSJohn Baldwin 2365d95e7f5aSJohn Baldwin /* 236600a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be 236700a30448SNate Lawson * removed. 236800a30448SNate Lawson * 236900a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran 237000a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 237100a30448SNate Lawson */ 237200a30448SNate Lawson ACPI_STATUS 237300a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state) 237400a30448SNate Lawson { 237500a30448SNate Lawson static int once; 237600a30448SNate Lawson 237700a30448SNate Lawson if (!once) { 23782d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 237900a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n"); 238000a30448SNate Lawson once = 1; 238100a30448SNate Lawson } 238200a30448SNate Lawson return (acpi_EnterSleepState(sc, state)); 238300a30448SNate Lawson } 238400a30448SNate Lawson 2385c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 238600a30448SNate Lawson static void 238700a30448SNate Lawson acpi_sleep_force(void *arg) 238800a30448SNate Lawson { 23892d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 239000a30448SNate Lawson 23912d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 23922d0c82e8SJung-uk Kim "suspend request timed out, forcing sleep now\n"); 239300a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 23942d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 23952d0c82e8SJung-uk Kim sc->acpi_next_sstate); 239600a30448SNate Lawson } 2397c66d2b38SJung-uk Kim #endif 239800a30448SNate Lawson 239900a30448SNate Lawson /* 240000a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm 240100a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to 240200a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all 240300a30448SNate Lawson * acks are in. 240400a30448SNate Lawson */ 240500a30448SNate Lawson int 240600a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state) 240700a30448SNate Lawson { 240871f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 240900a30448SNate Lawson struct apm_clone_data *clone; 2410337744d9SJung-uk Kim ACPI_STATUS status; 241100a30448SNate Lawson 2412a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 241300a30448SNate Lawson return (EINVAL); 2414a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 2415a0e73a12SJung-uk Kim return (EOPNOTSUPP); 241600a30448SNate Lawson 2417337744d9SJung-uk Kim ACPI_LOCK(acpi); 241800a30448SNate Lawson 241900a30448SNate Lawson /* If a suspend request is already in progress, just return. */ 242000a30448SNate Lawson if (sc->acpi_next_sstate != 0) { 242100a30448SNate Lawson ACPI_UNLOCK(acpi); 242200a30448SNate Lawson return (0); 242300a30448SNate Lawson } 242400a30448SNate Lawson 2425337744d9SJung-uk Kim /* S5 (soft-off) should be entered directly with no waiting. */ 2426337744d9SJung-uk Kim if (state == ACPI_STATE_S5) { 2427337744d9SJung-uk Kim ACPI_UNLOCK(acpi); 2428337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2429337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2430337744d9SJung-uk Kim } 2431337744d9SJung-uk Kim 243200a30448SNate Lawson /* Record the pending state and notify all apm devices. */ 243300a30448SNate Lawson sc->acpi_next_sstate = state; 243400a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 243500a30448SNate Lawson clone->notify_status = APM_EV_NONE; 243600a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) { 243700a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO); 2438374d9c4dSJohn Baldwin KNOTE_LOCKED(&clone->sel_read.si_note, 0); 243900a30448SNate Lawson } 244000a30448SNate Lawson } 244100a30448SNate Lawson 24420c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */ 2443d42f0ad8SJung-uk Kim if (!devctl_process_running()) { 24440c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi); 2445337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2446337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 24470c26519eSMitsuru IWASAKI } 24480c26519eSMitsuru IWASAKI 244900a30448SNate Lawson /* 245000a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request 245100a30448SNate Lawson * in time. This way we still eventually go to sleep if we were 245200a30448SNate Lawson * overheating or running low on battery, even if userland is hung. 245300a30448SNate Lawson * We cancel this timeout once all userland acks are in or the 245400a30448SNate Lawson * suspend request is aborted. 245500a30448SNate Lawson */ 245600a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 245700a30448SNate Lawson ACPI_UNLOCK(acpi); 2458d42f0ad8SJung-uk Kim 2459d42f0ad8SJung-uk Kim /* Now notify devd(8) also. */ 2460d42f0ad8SJung-uk Kim acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2461d42f0ad8SJung-uk Kim 246200a30448SNate Lawson return (0); 2463c66d2b38SJung-uk Kim #else 2464c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2465c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2466c66d2b38SJung-uk Kim #endif 246700a30448SNate Lawson } 246800a30448SNate Lawson 246900a30448SNate Lawson /* 247000a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has 247100a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the 247200a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled 247300a30448SNate Lawson * and gives an errno value describing why. Once all votes are in, 247400a30448SNate Lawson * we suspend the system. 247500a30448SNate Lawson */ 247600a30448SNate Lawson int 247700a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error) 247800a30448SNate Lawson { 2479c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 248000a30448SNate Lawson struct acpi_softc *sc; 248100a30448SNate Lawson int ret, sleeping; 248200a30448SNate Lawson 248300a30448SNate Lawson /* If no pending sleep state, return an error. */ 248400a30448SNate Lawson ACPI_LOCK(acpi); 248500a30448SNate Lawson sc = clone->acpi_sc; 248600a30448SNate Lawson if (sc->acpi_next_sstate == 0) { 248700a30448SNate Lawson ACPI_UNLOCK(acpi); 248800a30448SNate Lawson return (ENXIO); 248900a30448SNate Lawson } 249000a30448SNate Lawson 249100a30448SNate Lawson /* Caller wants to abort suspend process. */ 249200a30448SNate Lawson if (error) { 249300a30448SNate Lawson sc->acpi_next_sstate = 0; 249400a30448SNate Lawson callout_stop(&sc->susp_force_to); 24952d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 24962d0c82e8SJung-uk Kim "listener on %s cancelled the pending suspend\n", 249700a30448SNate Lawson devtoname(clone->cdev)); 249800a30448SNate Lawson ACPI_UNLOCK(acpi); 249900a30448SNate Lawson return (0); 250000a30448SNate Lawson } 250100a30448SNate Lawson 250200a30448SNate Lawson /* 250300a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through 250400a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that 250500a30448SNate Lawson * are writable since read-only devices couldn't ack the request. 250600a30448SNate Lawson */ 250700a30448SNate Lawson sleeping = TRUE; 2508c66d2b38SJung-uk Kim clone->notify_status = APM_EV_ACKED; 250900a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 251000a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 && 251100a30448SNate Lawson clone->notify_status != APM_EV_ACKED) { 251200a30448SNate Lawson sleeping = FALSE; 251300a30448SNate Lawson break; 251400a30448SNate Lawson } 251500a30448SNate Lawson } 251600a30448SNate Lawson 251700a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */ 251800a30448SNate Lawson if (sleeping) 251900a30448SNate Lawson callout_stop(&sc->susp_force_to); 252000a30448SNate Lawson ACPI_UNLOCK(acpi); 252100a30448SNate Lawson ret = 0; 252200a30448SNate Lawson if (sleeping) { 252300a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 252400a30448SNate Lawson ret = ENODEV; 252500a30448SNate Lawson } 252600a30448SNate Lawson return (ret); 2527c66d2b38SJung-uk Kim #else 2528c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2529c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2530c66d2b38SJung-uk Kim #endif 253100a30448SNate Lawson } 253200a30448SNate Lawson 2533ece50487SMitsuru IWASAKI static void 2534ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 2535ece50487SMitsuru IWASAKI { 2536d42f0ad8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 2537bee4aa4aSNate Lawson 2538a0e73a12SJung-uk Kim /* Reschedule if the system is not fully up and running. */ 2539a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) { 2540a0e73a12SJung-uk Kim timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2541a0e73a12SJung-uk Kim return; 2542a0e73a12SJung-uk Kim } 2543a0e73a12SJung-uk Kim 2544d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2545a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = FALSE; 2546d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2547d42f0ad8SJung-uk Kim } 2548d42f0ad8SJung-uk Kim 2549d42f0ad8SJung-uk Kim static ACPI_STATUS 2550d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc) 2551d42f0ad8SJung-uk Kim { 2552d42f0ad8SJung-uk Kim ACPI_STATUS status; 2553d42f0ad8SJung-uk Kim 2554a0e73a12SJung-uk Kim /* Fail if the system is not fully up and running. */ 2555a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) 2556a0e73a12SJung-uk Kim return (AE_ERROR); 2557a0e73a12SJung-uk Kim 2558d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2559d42f0ad8SJung-uk Kim status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2560a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 2561d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2562d42f0ad8SJung-uk Kim 2563d42f0ad8SJung-uk Kim return (status); 2564ece50487SMitsuru IWASAKI } 2565c30382dfSMitsuru IWASAKI 256615e2f34fSNate Lawson enum acpi_sleep_state { 256715e2f34fSNate Lawson ACPI_SS_NONE, 256815e2f34fSNate Lawson ACPI_SS_GPE_SET, 256915e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 257015e2f34fSNate Lawson ACPI_SS_SLP_PREP, 257115e2f34fSNate Lawson ACPI_SS_SLEPT, 257215e2f34fSNate Lawson }; 257315e2f34fSNate Lawson 257415e32d5dSMike Smith /* 257500a30448SNate Lawson * Enter the desired system sleep state. 257615e32d5dSMike Smith * 2577be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 257815e32d5dSMike Smith */ 257900a30448SNate Lawson static ACPI_STATUS 258000a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state) 258115e32d5dSMike Smith { 258215e2f34fSNate Lawson ACPI_STATUS status; 258315e2f34fSNate Lawson enum acpi_sleep_state slp_state; 258415e32d5dSMike Smith 2585b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 25860ae55423SMike Smith 2587a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 258833c70d41SAndriy Gapon return_ACPI_STATUS (AE_BAD_PARAMETER); 2589a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) { 2590a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2591a0e73a12SJung-uk Kim state); 2592a0e73a12SJung-uk Kim return (AE_SUPPORT); 2593a0e73a12SJung-uk Kim } 2594a0e73a12SJung-uk Kim 2595a0e73a12SJung-uk Kim /* Re-entry once we're suspending is not allowed. */ 2596a0e73a12SJung-uk Kim status = acpi_sleep_disable(sc); 2597a0e73a12SJung-uk Kim if (ACPI_FAILURE(status)) { 2598a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, 2599a0e73a12SJung-uk Kim "suspend request ignored (not ready yet)\n"); 2600a0e73a12SJung-uk Kim return (status); 2601a0e73a12SJung-uk Kim } 260233c70d41SAndriy Gapon 260333c70d41SAndriy Gapon if (state == ACPI_STATE_S5) { 260433c70d41SAndriy Gapon /* 260533c70d41SAndriy Gapon * Shut down cleanly and power off. This will call us back through the 260633c70d41SAndriy Gapon * shutdown handlers. 260733c70d41SAndriy Gapon */ 260833c70d41SAndriy Gapon shutdown_nice(RB_POWEROFF); 260933c70d41SAndriy Gapon return_ACPI_STATUS (AE_OK); 261033c70d41SAndriy Gapon } 261133c70d41SAndriy Gapon 261236a483bbSJung-uk Kim if (smp_started) { 2613c66d2b38SJung-uk Kim thread_lock(curthread); 2614c66d2b38SJung-uk Kim sched_bind(curthread, 0); 2615c66d2b38SJung-uk Kim thread_unlock(curthread); 261636a483bbSJung-uk Kim } 2617c66d2b38SJung-uk Kim 2618a56fe095SJohn Baldwin /* 2619a56fe095SJohn Baldwin * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2620a56fe095SJohn Baldwin * drivers need this. 2621a56fe095SJohn Baldwin */ 2622a56fe095SJohn Baldwin mtx_lock(&Giant); 2623c66d2b38SJung-uk Kim 262415e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 262556d8cb57SMitsuru IWASAKI 2626a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 2627a1fccb47SMitsuru IWASAKI 2628d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 2629d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 263015e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 2631e8b4d56eSNate Lawson 263215e32d5dSMike Smith /* 2633c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 2634c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 263515e32d5dSMike Smith * 2636c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 2637c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 2638c7f88fc3SNate Lawson * bus interface does not provide for this. 263915e32d5dSMike Smith */ 264015e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 264115e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 264233c70d41SAndriy Gapon goto backout; 264315e2f34fSNate Lawson } 264415e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 2645b9c780d6SMitsuru IWASAKI 264693bfd059SNate Lawson /* If testing device suspend only, back out of everything here. */ 264793bfd059SNate Lawson if (acpi_susp_bounce) 264833c70d41SAndriy Gapon goto backout; 264993bfd059SNate Lawson 2650be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 2651be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2652b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2653b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 265433c70d41SAndriy Gapon goto backout; 2655b9c780d6SMitsuru IWASAKI } 265615e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 2657b9c780d6SMitsuru IWASAKI 2658be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 2659ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 2660ff01efb5SMitsuru IWASAKI 26616161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 26626161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 26636161544cSTakanori Watanabe 26646161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2665be2b1797SNate Lawson if (state == ACPI_STATE_S4) 2666964679ceSMitsuru IWASAKI AcpiEnable(); 26676161544cSTakanori Watanabe } else { 2668c7f88fc3SNate Lawson ACPI_DISABLE_IRQS(); 2669adad4744SNate Lawson status = AcpiEnterSleepState(state); 2670be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2671be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2672be2b1797SNate Lawson AcpiFormatException(status)); 267333c70d41SAndriy Gapon goto backout; 267415e32d5dSMike Smith } 26756161544cSTakanori Watanabe } 267615e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 2677ece50487SMitsuru IWASAKI 267815e2f34fSNate Lawson /* 267915e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 268015e2f34fSNate Lawson * process. This handles both the error and success cases. 268115e2f34fSNate Lawson */ 268233c70d41SAndriy Gapon backout: 268315e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 268415e2f34fSNate Lawson acpi_wake_prep_walk(state); 268515e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 268615e2f34fSNate Lawson } 268715e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLP_PREP) 268815e2f34fSNate Lawson AcpiLeaveSleepState(state); 2689071339e2SNate Lawson if (slp_state >= ACPI_SS_DEV_SUSPEND) 2690071339e2SNate Lawson DEVICE_RESUME(root_bus); 269115e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLEPT) 269215e2f34fSNate Lawson acpi_enable_fixed_events(sc); 2693337744d9SJung-uk Kim sc->acpi_next_sstate = 0; 269415e2f34fSNate Lawson 2695a56fe095SJohn Baldwin mtx_unlock(&Giant); 2696c66d2b38SJung-uk Kim 269736a483bbSJung-uk Kim if (smp_started) { 2698c66d2b38SJung-uk Kim thread_lock(curthread); 2699c66d2b38SJung-uk Kim sched_unbind(curthread); 2700c66d2b38SJung-uk Kim thread_unlock(curthread); 270136a483bbSJung-uk Kim } 2702c66d2b38SJung-uk Kim 2703d42f0ad8SJung-uk Kim /* Allow another sleep request after a while. */ 2704d42f0ad8SJung-uk Kim timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2705d42f0ad8SJung-uk Kim 2706d42f0ad8SJung-uk Kim /* Run /etc/rc.resume after we are back. */ 2707d42f0ad8SJung-uk Kim if (devctl_process_running()) 2708d42f0ad8SJung-uk Kim acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2709d42f0ad8SJung-uk Kim 27100ae55423SMike Smith return_ACPI_STATUS (status); 271115e32d5dSMike Smith } 271215e32d5dSMike Smith 27130755473bSJung-uk Kim void 27140755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc) 27150755473bSJung-uk Kim { 27160755473bSJung-uk Kim 27170755473bSJung-uk Kim if (!acpi_reset_clock) 27180755473bSJung-uk Kim return; 27190755473bSJung-uk Kim 27200755473bSJung-uk Kim /* 27210755473bSJung-uk Kim * Warm up timecounter again and reset system clock. 27220755473bSJung-uk Kim */ 27230755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 27240755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 27250755473bSJung-uk Kim inittodr(time_second + sc->acpi_sleep_delay); 27260755473bSJung-uk Kim } 27270755473bSJung-uk Kim 2728e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 2729e8b4d56eSNate Lawson int 2730e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 2731e8b4d56eSNate Lawson { 2732e8b4d56eSNate Lawson struct acpi_prw_data prw; 2733e8b4d56eSNate Lawson ACPI_STATUS status; 2734e8b4d56eSNate Lawson int flags; 2735e8b4d56eSNate Lawson 2736d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 27372be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2738e8b4d56eSNate Lawson return (ENXIO); 2739e8b4d56eSNate Lawson 2740d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 2741e8b4d56eSNate Lawson if (enable) { 27425a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 27435a77b11bSJung-uk Kim ACPI_GPE_ENABLE); 2744e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2745e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 2746e8b4d56eSNate Lawson return (ENXIO); 2747e8b4d56eSNate Lawson } 2748d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2749e8b4d56eSNate Lawson } else { 27505a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 27515a77b11bSJung-uk Kim ACPI_GPE_DISABLE); 2752e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2753e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 2754e8b4d56eSNate Lawson return (ENXIO); 2755e8b4d56eSNate Lawson } 2756d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2757e8b4d56eSNate Lawson } 2758e8b4d56eSNate Lawson 2759e8b4d56eSNate Lawson return (0); 2760e8b4d56eSNate Lawson } 2761e8b4d56eSNate Lawson 2762d4b9ff91SNate Lawson static int 2763d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2764e8b4d56eSNate Lawson { 2765e8b4d56eSNate Lawson struct acpi_prw_data prw; 2766d4b9ff91SNate Lawson device_t dev; 2767e8b4d56eSNate Lawson 2768d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 2769e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2770e8b4d56eSNate Lawson return (ENXIO); 2771d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2772e8b4d56eSNate Lawson 2773e8b4d56eSNate Lawson /* 2774d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 2775d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 2776d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 2777d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 2778d4b9ff91SNate Lawson * and set _PSW. 2779e8b4d56eSNate Lawson */ 2780d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 27815a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 2782e8b4d56eSNate Lawson if (bootverbose) 2783d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2784d4b9ff91SNate Lawson acpi_name(handle), sstate); 2785d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2786d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 2787d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 2788d4b9ff91SNate Lawson if (bootverbose) 2789d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2790d4b9ff91SNate Lawson acpi_name(handle), sstate); 2791d4b9ff91SNate Lawson } 2792cc85c78cSNate Lawson 2793cc85c78cSNate Lawson return (0); 2794e8b4d56eSNate Lawson } 2795e8b4d56eSNate Lawson 2796d4b9ff91SNate Lawson static int 2797d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2798cc85c78cSNate Lawson { 2799cc85c78cSNate Lawson struct acpi_prw_data prw; 2800d4b9ff91SNate Lawson device_t dev; 2801cc85c78cSNate Lawson 2802cc85c78cSNate Lawson /* 2803d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 2804d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 2805cc85c78cSNate Lawson */ 2806d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2807d4b9ff91SNate Lawson return (ENXIO); 2808d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2809d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2810d4b9ff91SNate Lawson return (0); 2811cc85c78cSNate Lawson 2812d4b9ff91SNate Lawson /* 2813d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 2814d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 2815d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 2816d4b9ff91SNate Lawson */ 2817d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 28185a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 2819d4b9ff91SNate Lawson if (bootverbose) 2820d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2821d4b9ff91SNate Lawson } else { 2822d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2823d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 2824d4b9ff91SNate Lawson if (bootverbose) 2825d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 2826d4b9ff91SNate Lawson acpi_name(handle)); 2827d4b9ff91SNate Lawson } 2828d4b9ff91SNate Lawson 2829e8b4d56eSNate Lawson return (0); 2830e8b4d56eSNate Lawson } 2831e8b4d56eSNate Lawson 2832e8b4d56eSNate Lawson static ACPI_STATUS 2833d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2834e8b4d56eSNate Lawson { 2835d4b9ff91SNate Lawson int sstate; 2836e8b4d56eSNate Lawson 2837d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 2838d4b9ff91SNate Lawson sstate = *(int *)context; 2839d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 2840d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 2841d4b9ff91SNate Lawson else 2842d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 2843e8b4d56eSNate Lawson return (AE_OK); 2844e8b4d56eSNate Lawson } 2845e8b4d56eSNate Lawson 2846d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2847e8b4d56eSNate Lawson static int 2848d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 2849e8b4d56eSNate Lawson { 2850e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2851e8b4d56eSNate Lawson 2852e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2853d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 28542272d050SJung-uk Kim acpi_wake_prep, NULL, &sstate, NULL); 2855e8b4d56eSNate Lawson return (0); 2856e8b4d56eSNate Lawson } 2857e8b4d56eSNate Lawson 285888a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 285988a79fc0SNate Lawson static int 286088a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 286188a79fc0SNate Lawson { 286288a79fc0SNate Lawson int error, i, numdevs; 286388a79fc0SNate Lawson device_t *devlist; 286488a79fc0SNate Lawson device_t child; 2865d4b9ff91SNate Lawson ACPI_STATUS status; 286688a79fc0SNate Lawson 286788a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 28681c9ec538SNate Lawson if (error != 0 || numdevs == 0) { 28691c9ec538SNate Lawson if (numdevs == 0) 28701c9ec538SNate Lawson free(devlist, M_TEMP); 287188a79fc0SNate Lawson return (error); 28721c9ec538SNate Lawson } 287388a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 287488a79fc0SNate Lawson child = devlist[i]; 2875d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 287688a79fc0SNate Lawson if (!device_is_attached(child)) 287788a79fc0SNate Lawson continue; 2878d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2879d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 288088a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 288188a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 288288a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 288388a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 288488a79fc0SNate Lawson } 288588a79fc0SNate Lawson } 288688a79fc0SNate Lawson free(devlist, M_TEMP); 288788a79fc0SNate Lawson 288888a79fc0SNate Lawson return (0); 288988a79fc0SNate Lawson } 289088a79fc0SNate Lawson 289188a79fc0SNate Lawson /* Enable or disable wake from userland. */ 289288a79fc0SNate Lawson static int 289388a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 289488a79fc0SNate Lawson { 289588a79fc0SNate Lawson int enable, error; 289688a79fc0SNate Lawson device_t dev; 289788a79fc0SNate Lawson 289888a79fc0SNate Lawson dev = (device_t)arg1; 2899d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 290088a79fc0SNate Lawson 290188a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 290288a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 290388a79fc0SNate Lawson return (error); 290488a79fc0SNate Lawson if (enable != 0 && enable != 1) 290588a79fc0SNate Lawson return (EINVAL); 290688a79fc0SNate Lawson 290788a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 290888a79fc0SNate Lawson } 290988a79fc0SNate Lawson 2910e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2911d4b9ff91SNate Lawson int 2912e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2913e8b4d56eSNate Lawson { 2914e8b4d56eSNate Lawson ACPI_STATUS status; 2915e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2916e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2917d4b9ff91SNate Lawson int error, i, power_count; 2918e8b4d56eSNate Lawson 2919e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2920e8b4d56eSNate Lawson return (EINVAL); 2921e8b4d56eSNate Lawson 2922e8b4d56eSNate Lawson /* 2923e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2924e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2925e8b4d56eSNate Lawson */ 2926e8b4d56eSNate Lawson error = EINVAL; 2927e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2928e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2929e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2930e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2931e8b4d56eSNate Lawson return (ENOENT); 2932e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2933e8b4d56eSNate Lawson if (res == NULL) 2934e8b4d56eSNate Lawson return (ENOENT); 2935e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2936e8b4d56eSNate Lawson goto out; 2937e8b4d56eSNate Lawson 2938e8b4d56eSNate Lawson /* 2939e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2940e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2941e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2942e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2943e8b4d56eSNate Lawson */ 2944e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2945e8b4d56eSNate Lawson goto out; 2946e8b4d56eSNate Lawson 2947e8b4d56eSNate Lawson /* 2948e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2949e8b4d56eSNate Lawson */ 2950e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2951e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2952e8b4d56eSNate Lawson /* 2953e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2954e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2955e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2956e8b4d56eSNate Lawson * enabled for the wake event. 2957e8b4d56eSNate Lawson */ 2958e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2959e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2960e8b4d56eSNate Lawson error = 0; 2961e8b4d56eSNate Lawson break; 2962e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2963e8b4d56eSNate Lawson /* 2964e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2965e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2966e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2967e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2968e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2969e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2970e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2971e8b4d56eSNate Lawson * the wake event. 2972e8b4d56eSNate Lawson * 2973e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2974e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2975e8b4d56eSNate Lawson */ 2976e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2977e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2978e8b4d56eSNate Lawson goto out; 2979e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2980e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2981e8b4d56eSNate Lawson goto out; 2982e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2983e8b4d56eSNate Lawson goto out; 2984e8b4d56eSNate Lawson error = 0; 2985e8b4d56eSNate Lawson break; 2986e8b4d56eSNate Lawson default: 2987e8b4d56eSNate Lawson goto out; 2988e8b4d56eSNate Lawson } 2989e8b4d56eSNate Lawson 2990d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 2991d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 2992d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 2993d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2994d4b9ff91SNate Lawson power_count = 0; 2995d4b9ff91SNate Lawson } 2996d4b9ff91SNate Lawson prw->power_res_count = power_count; 2997d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 2998d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 2999e8b4d56eSNate Lawson 3000e8b4d56eSNate Lawson out: 3001e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 3002e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 3003e8b4d56eSNate Lawson return (error); 3004e8b4d56eSNate Lawson } 3005e8b4d56eSNate Lawson 300615e32d5dSMike Smith /* 300715e32d5dSMike Smith * ACPI Event Handlers 300815e32d5dSMike Smith */ 300915e32d5dSMike Smith 301015e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 301115e32d5dSMike Smith 301215e32d5dSMike Smith static void 301315e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 301415e32d5dSMike Smith { 30152d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 301600a30448SNate Lawson int ret; 3017bee4aa4aSNate Lawson 3018b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 301915e32d5dSMike Smith 3020a0e73a12SJung-uk Kim /* Check if button action is disabled or unknown. */ 3021a0e73a12SJung-uk Kim if (state == ACPI_STATE_UNKNOWN) 3022813d6dcaSNate Lawson return; 3023813d6dcaSNate Lawson 302400a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */ 30252d0c82e8SJung-uk Kim ret = acpi_ReqSleepState(sc, state); 302600a30448SNate Lawson if (ret != 0) 30272d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 30282d0c82e8SJung-uk Kim "request to enter state S%d failed (err %d)\n", state, ret); 3029bee4aa4aSNate Lawson 30300ae55423SMike Smith return_VOID; 303115e32d5dSMike Smith } 303215e32d5dSMike Smith 303315e32d5dSMike Smith static void 303415e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 303515e32d5dSMike Smith { 3036bee4aa4aSNate Lawson 3037b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 30380ae55423SMike Smith 3039bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 3040cb9b0d80SMike Smith 30410ae55423SMike Smith return_VOID; 304215e32d5dSMike Smith } 304315e32d5dSMike Smith 304415e32d5dSMike Smith /* 304515e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 304615e32d5dSMike Smith */ 304715e32d5dSMike Smith UINT32 304833febf93SNate Lawson acpi_event_power_button_sleep(void *context) 304915e32d5dSMike Smith { 305015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 305115e32d5dSMike Smith 3052b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 30530ae55423SMike Smith 305415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 30550ae55423SMike Smith 3056b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 305715e32d5dSMike Smith } 305815e32d5dSMike Smith 305915e32d5dSMike Smith UINT32 306033febf93SNate Lawson acpi_event_power_button_wake(void *context) 306115e32d5dSMike Smith { 306215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 306315e32d5dSMike Smith 3064b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 30650ae55423SMike Smith 306615e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 30670ae55423SMike Smith 3068b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 306915e32d5dSMike Smith } 307015e32d5dSMike Smith 307115e32d5dSMike Smith UINT32 307233febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 307315e32d5dSMike Smith { 307415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 307515e32d5dSMike Smith 3076b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 30770ae55423SMike Smith 307815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 30790ae55423SMike Smith 3080b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 308115e32d5dSMike Smith } 308215e32d5dSMike Smith 308315e32d5dSMike Smith UINT32 308433febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 308515e32d5dSMike Smith { 308615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 308715e32d5dSMike Smith 3088b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 30890ae55423SMike Smith 309015e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 30910ae55423SMike Smith 3092b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 309315e32d5dSMike Smith } 309415e32d5dSMike Smith 309515e32d5dSMike Smith /* 3096bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 3097bee4aa4aSNate Lawson * use this for single-threaded callers. 309815e32d5dSMike Smith */ 309915e32d5dSMike Smith char * 310015e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 310115e32d5dSMike Smith { 3102bee4aa4aSNate Lawson ACPI_BUFFER buf; 3103bee4aa4aSNate Lawson static char data[256]; 310415e32d5dSMike Smith 3105bee4aa4aSNate Lawson buf.Length = sizeof(data); 3106bee4aa4aSNate Lawson buf.Pointer = data; 3107cb9b0d80SMike Smith 31080a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3109bee4aa4aSNate Lawson return (data); 3110bee4aa4aSNate Lawson return ("(unknown)"); 311115e32d5dSMike Smith } 311215e32d5dSMike Smith 311315e32d5dSMike Smith /* 311415e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 311515e32d5dSMike Smith * parts of the namespace. 311615e32d5dSMike Smith */ 311715e32d5dSMike Smith int 311815e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 311915e32d5dSMike Smith { 31203c600cfbSJohn Baldwin char *cp, *env, *np; 312115e32d5dSMike Smith int len; 312215e32d5dSMike Smith 312315e32d5dSMike Smith np = acpi_name(handle); 312415e32d5dSMike Smith if (*np == '\\') 312515e32d5dSMike Smith np++; 31263c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 312715e32d5dSMike Smith return (0); 312815e32d5dSMike Smith 3129be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 31303c600cfbSJohn Baldwin cp = env; 313115e32d5dSMike Smith for (;;) { 3132bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 313315e32d5dSMike Smith cp++; 313415e32d5dSMike Smith if (*cp == 0) 313515e32d5dSMike Smith break; 313615e32d5dSMike Smith len = 0; 3137bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 313815e32d5dSMike Smith len++; 3139d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 31403c600cfbSJohn Baldwin freeenv(env); 31410ae55423SMike Smith return(1); 3142d786139cSMaxime Henrion } 31430ae55423SMike Smith cp += len; 31440ae55423SMike Smith } 31453c600cfbSJohn Baldwin freeenv(env); 3146be2b1797SNate Lawson 31470ae55423SMike Smith return (0); 31480ae55423SMike Smith } 31490ae55423SMike Smith 31500ae55423SMike Smith /* 31510ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 31520ae55423SMike Smith */ 31530ae55423SMike Smith int 31540ae55423SMike Smith acpi_disabled(char *subsys) 31550ae55423SMike Smith { 315678689b15SMaxime Henrion char *cp, *env; 31570ae55423SMike Smith int len; 31580ae55423SMike Smith 31593184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 31600ae55423SMike Smith return (0); 31613184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 316278689b15SMaxime Henrion freeenv(env); 31630ae55423SMike Smith return (1); 3164d786139cSMaxime Henrion } 31650ae55423SMike Smith 31663184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 316778689b15SMaxime Henrion cp = env; 31680ae55423SMike Smith for (;;) { 31693184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 31700ae55423SMike Smith cp++; 31713184cf5aSNate Lawson if (*cp == '\0') 31720ae55423SMike Smith break; 31730ae55423SMike Smith len = 0; 31743184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 31750ae55423SMike Smith len++; 31763184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 317778689b15SMaxime Henrion freeenv(env); 317815e32d5dSMike Smith return (1); 3179d786139cSMaxime Henrion } 318015e32d5dSMike Smith cp += len; 318115e32d5dSMike Smith } 318278689b15SMaxime Henrion freeenv(env); 3183be2b1797SNate Lawson 318415e32d5dSMike Smith return (0); 318515e32d5dSMike Smith } 318615e32d5dSMike Smith 318715e32d5dSMike Smith /* 318815e32d5dSMike Smith * Control interface. 318915e32d5dSMike Smith * 31900ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 3191be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 3192be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 319315e32d5dSMike Smith */ 31940ae55423SMike Smith struct acpi_ioctl_hook 31950ae55423SMike Smith { 31960ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 31970ae55423SMike Smith u_long cmd; 3198be2b1797SNate Lawson acpi_ioctl_fn fn; 31990ae55423SMike Smith void *arg; 32000ae55423SMike Smith }; 32010ae55423SMike Smith 32020ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 32030ae55423SMike Smith static int acpi_ioctl_hooks_initted; 32040ae55423SMike Smith 32050ae55423SMike Smith int 3206be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 32070ae55423SMike Smith { 32080ae55423SMike Smith struct acpi_ioctl_hook *hp; 32090ae55423SMike Smith 32100ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 32110ae55423SMike Smith return (ENOMEM); 32120ae55423SMike Smith hp->cmd = cmd; 32130ae55423SMike Smith hp->fn = fn; 32140ae55423SMike Smith hp->arg = arg; 321515e2f34fSNate Lawson 321615e2f34fSNate Lawson ACPI_LOCK(acpi); 32170ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 32180ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 32190ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 32200ae55423SMike Smith } 32210ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 322215e2f34fSNate Lawson ACPI_UNLOCK(acpi); 322315e2f34fSNate Lawson 32240ae55423SMike Smith return (0); 32250ae55423SMike Smith } 32260ae55423SMike Smith 32270ae55423SMike Smith void 3228be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 32290ae55423SMike Smith { 32300ae55423SMike Smith struct acpi_ioctl_hook *hp; 32310ae55423SMike Smith 323215e2f34fSNate Lawson ACPI_LOCK(acpi); 32330ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 323415e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 32350ae55423SMike Smith break; 32360ae55423SMike Smith 32370ae55423SMike Smith if (hp != NULL) { 32380ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 32390ae55423SMike Smith free(hp, M_ACPIDEV); 32400ae55423SMike Smith } 324115e2f34fSNate Lawson ACPI_UNLOCK(acpi); 32420ae55423SMike Smith } 32430ae55423SMike Smith 324415e32d5dSMike Smith static int 324500b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 324615e32d5dSMike Smith { 324715e32d5dSMike Smith return (0); 324815e32d5dSMike Smith } 324915e32d5dSMike Smith 325015e32d5dSMike Smith static int 325100b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 325215e32d5dSMike Smith { 325315e32d5dSMike Smith return (0); 325415e32d5dSMike Smith } 325515e32d5dSMike Smith 325615e32d5dSMike Smith static int 325700b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 325815e32d5dSMike Smith { 325915e32d5dSMike Smith struct acpi_softc *sc; 32600ae55423SMike Smith struct acpi_ioctl_hook *hp; 3261bee4aa4aSNate Lawson int error, state; 326215e32d5dSMike Smith 3263a2e35898SDavid E. O'Brien error = 0; 3264a2e35898SDavid E. O'Brien hp = NULL; 326515e32d5dSMike Smith sc = dev->si_drv1; 326615e32d5dSMike Smith 32670ae55423SMike Smith /* 32680ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 32690ae55423SMike Smith */ 327015e2f34fSNate Lawson ACPI_LOCK(acpi); 3271bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 32720ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3273bee4aa4aSNate Lawson if (hp->cmd == cmd) 3274bee4aa4aSNate Lawson break; 32750ae55423SMike Smith } 327615e2f34fSNate Lawson ACPI_UNLOCK(acpi); 3277bee4aa4aSNate Lawson if (hp) 3278bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 32790ae55423SMike Smith 32800ae55423SMike Smith /* 3281a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 3282a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 3283a89fcf28STakanori Watanabe * Not changing system behavior. 3284a89fcf28STakanori Watanabe */ 3285be2b1797SNate Lawson if ((flag & FWRITE) == 0) 3286be2b1797SNate Lawson return (EPERM); 3287a89fcf28STakanori Watanabe 3288be2b1797SNate Lawson /* Core system ioctls. */ 328915e32d5dSMike Smith switch (cmd) { 329000a30448SNate Lawson case ACPIIO_REQSLPSTATE: 329100a30448SNate Lawson state = *(int *)addr; 329200a30448SNate Lawson if (state != ACPI_STATE_S5) 3293a0e73a12SJung-uk Kim return (acpi_ReqSleepState(sc, state)); 3294a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3295a0e73a12SJung-uk Kim error = EOPNOTSUPP; 329600a30448SNate Lawson break; 329700a30448SNate Lawson case ACPIIO_ACKSLPSTATE: 329800a30448SNate Lawson error = *(int *)addr; 329900a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error); 330000a30448SNate Lawson break; 330100a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 330215e32d5dSMike Smith state = *(int *)addr; 3303a0e73a12SJung-uk Kim if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3304a0e73a12SJung-uk Kim return (EINVAL); 3305a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 3306a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3307a0e73a12SJung-uk Kim if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3308a0e73a12SJung-uk Kim error = ENXIO; 330915e32d5dSMike Smith break; 331015e32d5dSMike Smith default: 3311bee4aa4aSNate Lawson error = ENXIO; 331215e32d5dSMike Smith break; 331315e32d5dSMike Smith } 3314917d44c8SMitsuru IWASAKI 331515e32d5dSMike Smith return (error); 331615e32d5dSMike Smith } 331715e32d5dSMike Smith 33181d073b1dSJohn Baldwin static int 3319a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname) 3320a0e73a12SJung-uk Kim { 3321a0e73a12SJung-uk Kim int sstate; 3322a0e73a12SJung-uk Kim 3323a0e73a12SJung-uk Kim if (toupper(sname[0]) == 'S') { 3324a0e73a12SJung-uk Kim sstate = sname[1] - '0'; 3325a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3326a0e73a12SJung-uk Kim sname[2] == '\0') 3327a0e73a12SJung-uk Kim return (sstate); 3328a0e73a12SJung-uk Kim } else if (strcasecmp(sname, "NONE") == 0) 3329a0e73a12SJung-uk Kim return (ACPI_STATE_UNKNOWN); 3330a0e73a12SJung-uk Kim return (-1); 3331a0e73a12SJung-uk Kim } 3332a0e73a12SJung-uk Kim 3333a0e73a12SJung-uk Kim static const char * 3334a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate) 3335a0e73a12SJung-uk Kim { 3336a0e73a12SJung-uk Kim static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3337a0e73a12SJung-uk Kim 3338a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3339a0e73a12SJung-uk Kim return (snames[sstate]); 3340a0e73a12SJung-uk Kim else if (sstate == ACPI_STATE_UNKNOWN) 3341a0e73a12SJung-uk Kim return ("NONE"); 3342a0e73a12SJung-uk Kim return (NULL); 3343a0e73a12SJung-uk Kim } 3344a0e73a12SJung-uk Kim 3345a0e73a12SJung-uk Kim static int 3346d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3347d75de536SMitsuru IWASAKI { 3348d75de536SMitsuru IWASAKI int error; 3349bee4aa4aSNate Lawson struct sbuf sb; 3350a0e73a12SJung-uk Kim UINT8 state; 3351d75de536SMitsuru IWASAKI 3352bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3353a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3354a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) 3355a0e73a12SJung-uk Kim sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3356bee4aa4aSNate Lawson sbuf_trim(&sb); 3357bee4aa4aSNate Lawson sbuf_finish(&sb); 3358bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3359bee4aa4aSNate Lawson sbuf_delete(&sb); 3360d75de536SMitsuru IWASAKI return (error); 3361d75de536SMitsuru IWASAKI } 3362d75de536SMitsuru IWASAKI 3363d75de536SMitsuru IWASAKI static int 33641d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 33651d073b1dSJohn Baldwin { 33661d073b1dSJohn Baldwin char sleep_state[10]; 3367a0e73a12SJung-uk Kim int error, new_state, old_state; 33681d073b1dSJohn Baldwin 3369a0e73a12SJung-uk Kim old_state = *(int *)oidp->oid_arg1; 3370a0e73a12SJung-uk Kim strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 33711d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 33721d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 3373a0e73a12SJung-uk Kim new_state = acpi_sname2sstate(sleep_state); 3374a0e73a12SJung-uk Kim if (new_state < ACPI_STATE_S1) 3375a0e73a12SJung-uk Kim return (EINVAL); 337654b43614SJung-uk Kim if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3377a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3378be2b1797SNate Lawson if (new_state != old_state) 3379a0e73a12SJung-uk Kim *(int *)oidp->oid_arg1 = new_state; 33801d073b1dSJohn Baldwin } 33811d073b1dSJohn Baldwin return (error); 33821d073b1dSJohn Baldwin } 33831d073b1dSJohn Baldwin 33849b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 33859b937d48SNate Lawson void 33869b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 33879b937d48SNate Lawson { 33889b937d48SNate Lawson char notify_buf[16]; 33899b937d48SNate Lawson ACPI_BUFFER handle_buf; 33909b937d48SNate Lawson ACPI_STATUS status; 33919b937d48SNate Lawson 33929b937d48SNate Lawson if (subsystem == NULL) 33939b937d48SNate Lawson return; 33949b937d48SNate Lawson 33959b937d48SNate Lawson handle_buf.Pointer = NULL; 33969b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 33979b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 33989b937d48SNate Lawson if (ACPI_FAILURE(status)) 33999b937d48SNate Lawson return; 34009b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 34019b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 34029b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 34039b937d48SNate Lawson } 34049b937d48SNate Lawson 340515e32d5dSMike Smith #ifdef ACPI_DEBUG 34060ae55423SMike Smith /* 34070ae55423SMike Smith * Support for parsing debug options from the kernel environment. 34080ae55423SMike Smith * 34090ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 34100ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 34110ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 34120ae55423SMike Smith * prefixing the bit name with !. 34130ae55423SMike Smith */ 341415e32d5dSMike Smith struct debugtag 341515e32d5dSMike Smith { 341615e32d5dSMike Smith char *name; 341715e32d5dSMike Smith UINT32 value; 341815e32d5dSMike Smith }; 341915e32d5dSMike Smith 342015e32d5dSMike Smith static struct debugtag dbg_layer[] = { 3421c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 3422c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 3423c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 3424c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 3425c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3426c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 3427c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3428c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 3429c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 3430d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 34314c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3432d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3433297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 34344c1cdee6SMike Smith 3435c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3436c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 34373184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 3438c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 34393184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 34403184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 34413184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 34424c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3443cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 34443184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 3445b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 344615e32d5dSMike Smith {NULL, 0} 344715e32d5dSMike Smith }; 344815e32d5dSMike Smith 344915e32d5dSMike Smith static struct debugtag dbg_level[] = { 34509501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 34514c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 34529501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 34534c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3454d62ab2f4SMitsuru IWASAKI 3455d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 3456297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 34574c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 34584c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3459d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 34604c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 34614c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 34624c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 34634c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 34644c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 34654c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 34664c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 34674c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 34684c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 34694c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3470d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3471d62ab2f4SMitsuru IWASAKI 3472d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3473d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3474d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3475d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3476d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 34774c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 3478d62ab2f4SMitsuru IWASAKI 3479d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3480d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3481d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3482d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 3483d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3484d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3485d62ab2f4SMitsuru IWASAKI 3486d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 348798479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 348898479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 348998479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 349098479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 349198479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 349215e32d5dSMike Smith {NULL, 0} 349315e32d5dSMike Smith }; 349415e32d5dSMike Smith 349515e32d5dSMike Smith static void 349615e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 349715e32d5dSMike Smith { 349815e32d5dSMike Smith char *ep; 349915e32d5dSMike Smith int i, l; 35000ae55423SMike Smith int set; 350115e32d5dSMike Smith 350215e32d5dSMike Smith while (*cp) { 350315e32d5dSMike Smith if (isspace(*cp)) { 350415e32d5dSMike Smith cp++; 350515e32d5dSMike Smith continue; 350615e32d5dSMike Smith } 350715e32d5dSMike Smith ep = cp; 350815e32d5dSMike Smith while (*ep && !isspace(*ep)) 350915e32d5dSMike Smith ep++; 35100ae55423SMike Smith if (*cp == '!') { 35110ae55423SMike Smith set = 0; 35120ae55423SMike Smith cp++; 35130ae55423SMike Smith if (cp == ep) 35140ae55423SMike Smith continue; 35150ae55423SMike Smith } else { 35160ae55423SMike Smith set = 1; 35170ae55423SMike Smith } 351815e32d5dSMike Smith l = ep - cp; 351915e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 352015e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 3521be2b1797SNate Lawson if (set) 352215e32d5dSMike Smith *flag |= tag[i].value; 3523be2b1797SNate Lawson else 35240ae55423SMike Smith *flag &= ~tag[i].value; 352515e32d5dSMike Smith } 352615e32d5dSMike Smith } 352715e32d5dSMike Smith cp = ep; 352815e32d5dSMike Smith } 352915e32d5dSMike Smith } 353015e32d5dSMike Smith 353115e32d5dSMike Smith static void 35322a4ac806SMike Smith acpi_set_debugging(void *junk) 353315e32d5dSMike Smith { 35347e639165SNate Lawson char *layer, *level; 353515e32d5dSMike Smith 35361d7b121cSNate Lawson if (cold) { 35370ae55423SMike Smith AcpiDbgLayer = 0; 35380ae55423SMike Smith AcpiDbgLevel = 0; 35391d7b121cSNate Lawson } 35401d7b121cSNate Lawson 35417e639165SNate Lawson layer = getenv("debug.acpi.layer"); 35427e639165SNate Lawson level = getenv("debug.acpi.level"); 35437e639165SNate Lawson if (layer == NULL && level == NULL) 35447e639165SNate Lawson return; 35450ae55423SMike Smith 35467e639165SNate Lawson printf("ACPI set debug"); 35477e639165SNate Lawson if (layer != NULL) { 35487e639165SNate Lawson if (strcmp("NONE", layer) != 0) 35497e639165SNate Lawson printf(" layer '%s'", layer); 35507e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 35517e639165SNate Lawson freeenv(layer); 35521d7b121cSNate Lawson } 35537e639165SNate Lawson if (level != NULL) { 35547e639165SNate Lawson if (strcmp("NONE", level) != 0) 35557e639165SNate Lawson printf(" level '%s'", level); 35567e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 35577e639165SNate Lawson freeenv(level); 35587e639165SNate Lawson } 35597e639165SNate Lawson printf("\n"); 356015e32d5dSMike Smith } 3561bee4aa4aSNate Lawson 3562be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3563be2b1797SNate Lawson NULL); 35641d7b121cSNate Lawson 35651d7b121cSNate Lawson static int 35661d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 35671d7b121cSNate Lawson { 356854f6bca0SNate Lawson int error, *dbg; 35691d7b121cSNate Lawson struct debugtag *tag; 357054f6bca0SNate Lawson struct sbuf sb; 35711d7b121cSNate Lawson 357254f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 357354f6bca0SNate Lawson return (ENOMEM); 35741d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 35751d7b121cSNate Lawson tag = &dbg_layer[0]; 35761d7b121cSNate Lawson dbg = &AcpiDbgLayer; 35771d7b121cSNate Lawson } else { 35781d7b121cSNate Lawson tag = &dbg_level[0]; 35791d7b121cSNate Lawson dbg = &AcpiDbgLevel; 35801d7b121cSNate Lawson } 35811d7b121cSNate Lawson 35821d7b121cSNate Lawson /* Get old values if this is a get request. */ 358315e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 35841d7b121cSNate Lawson if (*dbg == 0) { 358554f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 35861d7b121cSNate Lawson } else if (req->newptr == NULL) { 35871d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 358854f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 358954f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 35901d7b121cSNate Lawson } 35911d7b121cSNate Lawson } 359254f6bca0SNate Lawson sbuf_trim(&sb); 359354f6bca0SNate Lawson sbuf_finish(&sb); 35941d7b121cSNate Lawson 35957e639165SNate Lawson /* Copy out the old values to the user. */ 35967e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 359754f6bca0SNate Lawson sbuf_delete(&sb); 35981d7b121cSNate Lawson 35991d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 36001d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 36011d7b121cSNate Lawson *dbg = 0; 36021d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 36031d7b121cSNate Lawson acpi_set_debugging(NULL); 36041d7b121cSNate Lawson } 360515e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 36061d7b121cSNate Lawson 36071d7b121cSNate Lawson return (error); 36081d7b121cSNate Lawson } 3609bee4aa4aSNate Lawson 36101d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 36111d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 36121d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 36131d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3614bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 3615f9390180SMitsuru IWASAKI 3616f9390180SMitsuru IWASAKI static int 36172a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 36182a18c71dSJung-uk Kim { 36192a18c71dSJung-uk Kim int error; 36202a18c71dSJung-uk Kim int old; 36212a18c71dSJung-uk Kim 36222a18c71dSJung-uk Kim old = acpi_debug_objects; 36232a18c71dSJung-uk Kim error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 36242a18c71dSJung-uk Kim if (error != 0 || req->newptr == NULL) 36252a18c71dSJung-uk Kim return (error); 36262a18c71dSJung-uk Kim if (old == acpi_debug_objects || (old && acpi_debug_objects)) 36272a18c71dSJung-uk Kim return (0); 36282a18c71dSJung-uk Kim 36292a18c71dSJung-uk Kim ACPI_SERIAL_BEGIN(acpi); 36302a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 36312a18c71dSJung-uk Kim ACPI_SERIAL_END(acpi); 36322a18c71dSJung-uk Kim 36332a18c71dSJung-uk Kim return (0); 36342a18c71dSJung-uk Kim } 36352a18c71dSJung-uk Kim 36362a18c71dSJung-uk Kim static int 3637ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface) 3638ae19af49SJung-uk Kim { 3639ae19af49SJung-uk Kim char *p; 3640ae19af49SJung-uk Kim size_t len; 3641ae19af49SJung-uk Kim int i, j; 3642ae19af49SJung-uk Kim 3643ae19af49SJung-uk Kim p = str; 3644ae19af49SJung-uk Kim while (isspace(*p) || *p == ',') 3645ae19af49SJung-uk Kim p++; 3646ae19af49SJung-uk Kim len = strlen(p); 3647ae19af49SJung-uk Kim if (len == 0) 3648ae19af49SJung-uk Kim return (0); 3649ae19af49SJung-uk Kim p = strdup(p, M_TEMP); 3650ae19af49SJung-uk Kim for (i = 0; i < len; i++) 3651ae19af49SJung-uk Kim if (p[i] == ',') 3652ae19af49SJung-uk Kim p[i] = '\0'; 3653ae19af49SJung-uk Kim i = j = 0; 3654ae19af49SJung-uk Kim while (i < len) 3655ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 3656ae19af49SJung-uk Kim i++; 3657ae19af49SJung-uk Kim else { 3658ae19af49SJung-uk Kim i += strlen(p + i) + 1; 3659ae19af49SJung-uk Kim j++; 3660ae19af49SJung-uk Kim } 3661ae19af49SJung-uk Kim if (j == 0) { 3662ae19af49SJung-uk Kim free(p, M_TEMP); 3663ae19af49SJung-uk Kim return (0); 3664ae19af49SJung-uk Kim } 3665ae19af49SJung-uk Kim iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK); 3666ae19af49SJung-uk Kim iface->num = j; 3667ae19af49SJung-uk Kim i = j = 0; 3668ae19af49SJung-uk Kim while (i < len) 3669ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 3670ae19af49SJung-uk Kim i++; 3671ae19af49SJung-uk Kim else { 3672ae19af49SJung-uk Kim iface->data[j] = p + i; 3673ae19af49SJung-uk Kim i += strlen(p + i) + 1; 3674ae19af49SJung-uk Kim j++; 3675ae19af49SJung-uk Kim } 3676ae19af49SJung-uk Kim 3677ae19af49SJung-uk Kim return (j); 3678ae19af49SJung-uk Kim } 3679ae19af49SJung-uk Kim 3680ae19af49SJung-uk Kim static void 3681ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface) 3682ae19af49SJung-uk Kim { 3683ae19af49SJung-uk Kim 3684ae19af49SJung-uk Kim free(iface->data[0], M_TEMP); 3685ae19af49SJung-uk Kim free(iface->data, M_TEMP); 3686ae19af49SJung-uk Kim } 3687ae19af49SJung-uk Kim 3688ae19af49SJung-uk Kim static void 3689ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev) 3690ae19af49SJung-uk Kim { 3691ae19af49SJung-uk Kim struct acpi_interface list; 3692ae19af49SJung-uk Kim ACPI_STATUS status; 3693ae19af49SJung-uk Kim int i; 3694ae19af49SJung-uk Kim 3695ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) { 3696ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 3697ae19af49SJung-uk Kim status = AcpiInstallInterface(list.data[i]); 3698ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 3699ae19af49SJung-uk Kim device_printf(dev, 3700ae19af49SJung-uk Kim "failed to install _OSI(\"%s\"): %s\n", 3701ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 3702ae19af49SJung-uk Kim else if (bootverbose) 3703ae19af49SJung-uk Kim device_printf(dev, "installed _OSI(\"%s\")\n", 3704ae19af49SJung-uk Kim list.data[i]); 3705ae19af49SJung-uk Kim } 3706ae19af49SJung-uk Kim acpi_free_interfaces(&list); 3707ae19af49SJung-uk Kim } 3708ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) { 3709ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 3710ae19af49SJung-uk Kim status = AcpiRemoveInterface(list.data[i]); 3711ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 3712ae19af49SJung-uk Kim device_printf(dev, 3713ae19af49SJung-uk Kim "failed to remove _OSI(\"%s\"): %s\n", 3714ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 3715ae19af49SJung-uk Kim else if (bootverbose) 3716ae19af49SJung-uk Kim device_printf(dev, "removed _OSI(\"%s\")\n", 3717ae19af49SJung-uk Kim list.data[i]); 3718ae19af49SJung-uk Kim } 3719ae19af49SJung-uk Kim acpi_free_interfaces(&list); 3720ae19af49SJung-uk Kim } 3721ae19af49SJung-uk Kim } 3722ae19af49SJung-uk Kim 3723ae19af49SJung-uk Kim static int 3724f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 3725f9390180SMitsuru IWASAKI { 3726f9390180SMitsuru IWASAKI int state, acpi_state; 3727f9390180SMitsuru IWASAKI int error; 3728f9390180SMitsuru IWASAKI struct acpi_softc *sc; 3729f9390180SMitsuru IWASAKI va_list ap; 3730f9390180SMitsuru IWASAKI 3731f9390180SMitsuru IWASAKI error = 0; 3732f9390180SMitsuru IWASAKI switch (cmd) { 3733f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 3734f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 3735f9390180SMitsuru IWASAKI if (sc == NULL) { 3736f9390180SMitsuru IWASAKI error = EINVAL; 3737f9390180SMitsuru IWASAKI goto out; 3738f9390180SMitsuru IWASAKI } 3739f9390180SMitsuru IWASAKI 3740f9390180SMitsuru IWASAKI va_start(ap, arg); 3741f9390180SMitsuru IWASAKI state = va_arg(ap, int); 3742f9390180SMitsuru IWASAKI va_end(ap); 3743f9390180SMitsuru IWASAKI 3744f9390180SMitsuru IWASAKI switch (state) { 3745f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 3746f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 3747f9390180SMitsuru IWASAKI break; 3748f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 3749f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 3750f9390180SMitsuru IWASAKI break; 3751f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 3752f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 3753f9390180SMitsuru IWASAKI break; 3754f9390180SMitsuru IWASAKI default: 3755f9390180SMitsuru IWASAKI error = EINVAL; 3756f9390180SMitsuru IWASAKI goto out; 3757f9390180SMitsuru IWASAKI } 3758f9390180SMitsuru IWASAKI 375900a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 376000a30448SNate Lawson error = ENXIO; 3761f9390180SMitsuru IWASAKI break; 3762f9390180SMitsuru IWASAKI default: 3763f9390180SMitsuru IWASAKI error = EINVAL; 3764f9390180SMitsuru IWASAKI goto out; 3765f9390180SMitsuru IWASAKI } 3766f9390180SMitsuru IWASAKI 3767f9390180SMitsuru IWASAKI out: 3768f9390180SMitsuru IWASAKI return (error); 3769f9390180SMitsuru IWASAKI } 3770f9390180SMitsuru IWASAKI 3771f9390180SMitsuru IWASAKI static void 3772f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 3773f9390180SMitsuru IWASAKI { 3774be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 37756c407052SMitsuru IWASAKI return; 3776f9390180SMitsuru IWASAKI 3777f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3778f9390180SMitsuru IWASAKI } 3779f9390180SMitsuru IWASAKI 3780f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3781