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 71d745c852SEd Schouten static 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; 96fadf3fb9SJohn Baldwin struct callout acpi_sleep_timer; 97cb9b0d80SMike Smith 9889fb5af8SNate Lawson /* Bitmap of device quirks. */ 9989fb5af8SNate Lawson int acpi_quirks; 10089fb5af8SNate Lawson 101a0e73a12SJung-uk Kim /* Supported sleep states. */ 102a0e73a12SJung-uk Kim static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 103a0e73a12SJung-uk Kim 1046d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10515e32d5dSMike Smith static int acpi_probe(device_t dev); 10615e32d5dSMike Smith static int acpi_attach(device_t dev); 10710ce62b9SNate Lawson static int acpi_suspend(device_t dev); 10810ce62b9SNate Lawson static int acpi_resume(device_t dev); 109169b539aSNate Lawson static int acpi_shutdown(device_t dev); 1103d844eddSAndriy Gapon static device_t acpi_add_child(device_t bus, u_int order, const char *name, 111be2b1797SNate Lawson int unit); 11215e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 11310ce62b9SNate Lawson static void acpi_probe_nomatch(device_t bus, device_t child); 11410ce62b9SNate Lawson static void acpi_driver_added(device_t dev, driver_t *driver); 115be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 116be2b1797SNate Lawson uintptr_t *result); 117be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 118be2b1797SNate Lawson uintptr_t value); 11991233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 120ea233199SJohn Baldwin static void acpi_reserve_resources(device_t dev); 121adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 122ea233199SJohn Baldwin static int acpi_set_resource(device_t dev, device_t child, int type, 123ea233199SJohn Baldwin int rid, u_long start, u_long count); 124be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 125be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 126be2b1797SNate Lawson u_long count, u_int flags); 127049dc0d1SJohn Baldwin static int acpi_adjust_resource(device_t bus, device_t child, int type, 128049dc0d1SJohn Baldwin struct resource *r, u_long start, u_long end); 129be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 130be2b1797SNate Lawson int rid, struct resource *r); 1318b28b622SNate Lawson static void acpi_delete_resource(device_t bus, device_t child, int type, 1328b28b622SNate Lawson int rid); 1331e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1341e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 135ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 136ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 137ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 138ce619b2eSNate Lawson ACPI_BUFFER *ret); 139df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 140df8d2a32SNate Lawson void *context, void **retval); 141df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 142df8d2a32SNate Lawson int max_depth, acpi_scan_cb_t user_fn, void *arg); 143a7a3177fSJung-uk Kim static int acpi_set_powerstate(device_t child, int state); 144be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 145be2b1797SNate Lawson struct isa_pnp_id *ids); 14615e32d5dSMike Smith static void acpi_probe_children(device_t bus); 147d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order); 148be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 149be2b1797SNate Lawson void *context, void **status); 150a0e73a12SJung-uk Kim static void acpi_sleep_enable(void *arg); 151a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 15200a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 15315e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 15413d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 155183c8af3SJohn Baldwin static BOOLEAN acpi_has_hid(ACPI_HANDLE handle); 156a0a15716SJung-uk Kim static void acpi_resync_clock(struct acpi_softc *sc); 157d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 158d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 159d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 16088a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 16188a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 16215e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 16315e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 164a0e73a12SJung-uk Kim static int acpi_sname2sstate(const char *sname); 165a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate); 166d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1671d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1682a18c71dSJung-uk Kim static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 169f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 170879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 171879d6c23STakanori Watanabe char *buf, size_t buflen); 172879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 173879d6c23STakanori Watanabe char *buf, size_t buflen); 174d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 175d320e05cSJohn Baldwin static void acpi_enable_pcie(void); 176d320e05cSJohn Baldwin #endif 1770d484d24SJohn Baldwin static void acpi_hint_device_unit(device_t acdev, device_t child, 1780d484d24SJohn Baldwin const char *name, int *unitp); 179ae19af49SJung-uk Kim static void acpi_reset_interfaces(device_t dev); 180879d6c23STakanori Watanabe 18115e32d5dSMike Smith static device_method_t acpi_methods[] = { 18215e32d5dSMike Smith /* Device interface */ 18315e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 18415e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 185169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 18656a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 18710ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend), 18810ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume), 18915e32d5dSMike Smith 19015e32d5dSMike Smith /* Bus interface */ 19115e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 19215e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 19310ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 19410ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added), 19515e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 19615e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 19791233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 198ea233199SJohn Baldwin DEVMETHOD(bus_set_resource, acpi_set_resource), 19991233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 20015e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 201049dc0d1SJohn Baldwin DEVMETHOD(bus_adjust_resource, acpi_adjust_resource), 20215e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 2038b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource), 204879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 205879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 20615e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 20715e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 20815e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 20915e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 2100d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 211ffcf962dSAdrian Chadd DEVMETHOD(bus_get_domain, acpi_get_domain), 21215e32d5dSMike Smith 213ce619b2eSNate Lawson /* ACPI bus */ 214ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 215ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 21610ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 217df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 218ce619b2eSNate Lawson 219bc0f2195SMike Smith /* ISA emulation */ 220bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 221bc0f2195SMike Smith 22261bfd867SSofian Brabez DEVMETHOD_END 22315e32d5dSMike Smith }; 22415e32d5dSMike Smith 22515e32d5dSMike Smith static driver_t acpi_driver = { 22615e32d5dSMike Smith "acpi", 22715e32d5dSMike Smith acpi_methods, 22815e32d5dSMike Smith sizeof(struct acpi_softc), 22915e32d5dSMike Smith }; 23015e32d5dSMike Smith 2313273b005SMike Smith static devclass_t acpi_devclass; 2326d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 233a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 23415e32d5dSMike Smith 23515e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 23615e2f34fSNate Lawson 237adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 238adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 239adad4744SNate Lawson 240bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 241bee4aa4aSNate Lawson 2425217af30SJohn Baldwin /* Holds the description of the acpi0 device. */ 2435217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 2445217af30SJohn Baldwin 245197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 2461d7b121cSNate Lawson static char acpi_ca_version[12]; 2471d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2481d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 24915e32d5dSMike Smith 25015e32d5dSMike Smith /* 251ae19af49SJung-uk Kim * Allow overriding _OSI methods. 252ae19af49SJung-uk Kim */ 253ae19af49SJung-uk Kim static char acpi_install_interface[256]; 254ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface, 255ae19af49SJung-uk Kim sizeof(acpi_install_interface)); 256ae19af49SJung-uk Kim static char acpi_remove_interface[256]; 257ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface, 258ae19af49SJung-uk Kim sizeof(acpi_remove_interface)); 259ae19af49SJung-uk Kim 2602a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */ 2612a18c71dSJung-uk Kim static int acpi_debug_objects; 2622a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 2632a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 2642a18c71dSJung-uk Kim CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 2652a18c71dSJung-uk Kim "Enable Debug objects"); 2662a18c71dSJung-uk Kim 2672a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */ 2682a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1; 2692a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 2702a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 2712a18c71dSJung-uk Kim &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 2722a18c71dSJung-uk Kim 273313a0c13SJung-uk Kim /* Ignore register widths set by FADT and use default widths instead. */ 274313a0c13SJung-uk Kim static int acpi_ignore_reg_width = 1; 275313a0c13SJung-uk Kim TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width); 276313a0c13SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN, 277313a0c13SJung-uk Kim &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT"); 278313a0c13SJung-uk Kim 279ebbed20dSAndriy Gapon #ifdef __amd64__ 2800755473bSJung-uk Kim /* Reset system clock while resuming. XXX Remove once tested. */ 2810755473bSJung-uk Kim static int acpi_reset_clock = 1; 2820755473bSJung-uk Kim TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock); 2830755473bSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW, 2840755473bSJung-uk Kim &acpi_reset_clock, 1, "Reset system clock while resuming."); 285a0a15716SJung-uk Kim #endif 2860755473bSJung-uk Kim 28739da3eb3SNate Lawson /* Allow users to override quirks. */ 28839da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 28939da3eb3SNate Lawson 29093bfd059SNate Lawson static int acpi_susp_bounce; 29193bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 29293bfd059SNate Lawson &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 29393bfd059SNate Lawson 294c9b8d77dSNate Lawson /* 2956d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2966d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 2978c0879b6SJohn Baldwin * It also cannot be unloaded, since the entire system bus hierarchy hangs 298be2b1797SNate Lawson * off it. 2996d63101aSMike Smith */ 3006d63101aSMike Smith static int 3016d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 3026d63101aSMike Smith { 3036d63101aSMike Smith switch (event) { 3046d63101aSMike Smith case MOD_LOAD: 30587b45ed5SMitsuru IWASAKI if (!cold) { 30687b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 3076d63101aSMike Smith return (EPERM); 30887b45ed5SMitsuru IWASAKI } 3096d63101aSMike Smith break; 3106d63101aSMike Smith case MOD_UNLOAD: 311f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 3126d63101aSMike Smith return (EBUSY); 313f9390180SMitsuru IWASAKI break; 3146d63101aSMike Smith default: 3156d63101aSMike Smith break; 3166d63101aSMike Smith } 3176d63101aSMike Smith return (0); 3186d63101aSMike Smith } 3196d63101aSMike Smith 3206d63101aSMike Smith /* 321bbc2815cSJohn Baldwin * Perform early initialization. 32215e32d5dSMike Smith */ 323bbc2815cSJohn Baldwin ACPI_STATUS 324bbc2815cSJohn Baldwin acpi_Startup(void) 32515e32d5dSMike Smith { 32689fb5af8SNate Lawson static int started = 0; 3272be4e471SJung-uk Kim ACPI_STATUS status; 3282be4e471SJung-uk Kim int val; 32915e32d5dSMike Smith 3301b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3311b8c233dSPeter Pentchev 332bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */ 333bbc2815cSJohn Baldwin if (started) 3342be4e471SJung-uk Kim return_VALUE (AE_OK); 335bbc2815cSJohn Baldwin started = 1; 33615e32d5dSMike Smith 337413081d7SNate Lawson /* 3382be4e471SJung-uk Kim * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 3392be4e471SJung-uk Kim * if more tables exist. 340413081d7SNate Lawson */ 3412be4e471SJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 3422be4e471SJung-uk Kim printf("ACPI: Table initialisation failed: %s\n", 3432be4e471SJung-uk Kim AcpiFormatException(status)); 3442be4e471SJung-uk Kim return_VALUE (status); 34515e32d5dSMike Smith } 3463184cf5aSNate Lawson 34789fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 3482be4e471SJung-uk Kim if (acpi_quirks == ACPI_Q_OK) 34989fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 35089fb5af8SNate Lawson 35139da3eb3SNate Lawson /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 35289fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 35389fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 35489fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 35589fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 3562be4e471SJung-uk Kim status = AE_SUPPORT; 35789fb5af8SNate Lawson } 3583184cf5aSNate Lawson 3592be4e471SJung-uk Kim return_VALUE (status); 360bbc2815cSJohn Baldwin } 361bbc2815cSJohn Baldwin 362bbc2815cSJohn Baldwin /* 3635217af30SJohn Baldwin * Detect ACPI and perform early initialisation. 364bbc2815cSJohn Baldwin */ 3655217af30SJohn Baldwin int 3665217af30SJohn Baldwin acpi_identify(void) 367bbc2815cSJohn Baldwin { 3685217af30SJohn Baldwin ACPI_TABLE_RSDP *rsdp; 3695217af30SJohn Baldwin ACPI_TABLE_HEADER *rsdt; 3705217af30SJohn Baldwin ACPI_PHYSICAL_ADDRESS paddr; 3715217af30SJohn Baldwin struct sbuf sb; 372bbc2815cSJohn Baldwin 373bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 374bbc2815cSJohn Baldwin 375bbc2815cSJohn Baldwin if (!cold) 3765217af30SJohn Baldwin return (ENXIO); 377bbc2815cSJohn Baldwin 378a8de37b0SEitan Adler /* Check that we haven't been disabled with a hint. */ 379a8de37b0SEitan Adler if (resource_disabled("acpi", 0)) 380a8de37b0SEitan Adler return (ENXIO); 381a8de37b0SEitan Adler 3825217af30SJohn Baldwin /* Check for other PM systems. */ 3835217af30SJohn Baldwin if (power_pm_get_type() != POWER_PM_TYPE_NONE && 3845217af30SJohn Baldwin power_pm_get_type() != POWER_PM_TYPE_ACPI) { 3855217af30SJohn Baldwin printf("ACPI identify failed, other PM system enabled.\n"); 3865217af30SJohn Baldwin return (ENXIO); 3875217af30SJohn Baldwin } 3884b1ff978SMark Santcroos 3892be4e471SJung-uk Kim /* Initialize root tables. */ 3902be4e471SJung-uk Kim if (ACPI_FAILURE(acpi_Startup())) { 3912be4e471SJung-uk Kim printf("ACPI: Try disabling either ACPI or apic support.\n"); 3925217af30SJohn Baldwin return (ENXIO); 3932be4e471SJung-uk Kim } 39415e32d5dSMike Smith 3955217af30SJohn Baldwin if ((paddr = AcpiOsGetRootPointer()) == 0 || 3965217af30SJohn Baldwin (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 3975217af30SJohn Baldwin return (ENXIO); 3985217af30SJohn Baldwin if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 3995217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 4005217af30SJohn Baldwin else 4015217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 4025217af30SJohn Baldwin AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 4035217af30SJohn Baldwin 4045217af30SJohn Baldwin if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 4055217af30SJohn Baldwin return (ENXIO); 4065217af30SJohn Baldwin sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 4075217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 4085217af30SJohn Baldwin sbuf_trim(&sb); 4095217af30SJohn Baldwin sbuf_putc(&sb, ' '); 4105217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 4115217af30SJohn Baldwin sbuf_trim(&sb); 4125217af30SJohn Baldwin sbuf_finish(&sb); 4135217af30SJohn Baldwin sbuf_delete(&sb); 4145217af30SJohn Baldwin AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 4155217af30SJohn Baldwin 4165217af30SJohn Baldwin snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 4175217af30SJohn Baldwin 4185217af30SJohn Baldwin return (0); 41915e32d5dSMike Smith } 42015e32d5dSMike Smith 42115e32d5dSMike Smith /* 422bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message. 42315e32d5dSMike Smith */ 42415e32d5dSMike Smith static int 42515e32d5dSMike Smith acpi_probe(device_t dev) 42615e32d5dSMike Smith { 42715e32d5dSMike Smith 428b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 429f9390180SMitsuru IWASAKI 4305217af30SJohn Baldwin device_set_desc(dev, acpi_desc); 431bee4aa4aSNate Lawson 432feeec74dSNathan Whitehorn return_VALUE (BUS_PROBE_NOWILDCARD); 43315e32d5dSMike Smith } 43415e32d5dSMike Smith 43515e32d5dSMike Smith static int 43615e32d5dSMike Smith acpi_attach(device_t dev) 43715e32d5dSMike Smith { 43815e32d5dSMike Smith struct acpi_softc *sc; 439cb9b0d80SMike Smith ACPI_STATUS status; 440ea27c63eSNate Lawson int error, state; 44132d18aa5SMike Smith UINT32 flags; 442ea27c63eSNate Lawson UINT8 TypeA, TypeB; 443498d464fSMitsuru IWASAKI char *env; 44415e32d5dSMike Smith 445b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 446bee4aa4aSNate Lawson 44715e32d5dSMike Smith sc = device_get_softc(dev); 44815e32d5dSMike Smith sc->acpi_dev = dev; 44900a30448SNate Lawson callout_init(&sc->susp_force_to, TRUE); 45015e32d5dSMike Smith 4512be4e471SJung-uk Kim error = ENXIO; 4522be4e471SJung-uk Kim 45391233413SNate Lawson /* Initialize resource manager. */ 45491233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 45591233413SNate Lawson acpi_rman_io.rm_start = 0; 45691233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 4570bba6acfSJohn Baldwin acpi_rman_io.rm_descr = "ACPI I/O ports"; 45891233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 45991233413SNate Lawson panic("acpi rman_init IO ports failed"); 46091233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 46191233413SNate Lawson acpi_rman_mem.rm_start = 0; 46291233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 4630bba6acfSJohn Baldwin acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 46491233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 46591233413SNate Lawson panic("acpi rman_init memory failed"); 46691233413SNate Lawson 4672be4e471SJung-uk Kim /* Initialise the ACPI mutex */ 4682be4e471SJung-uk Kim mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 4692be4e471SJung-uk Kim 4702be4e471SJung-uk Kim /* 4712be4e471SJung-uk Kim * Set the globals from our tunables. This is needed because ACPI-CA 4722be4e471SJung-uk Kim * uses UINT8 for some values and we have no tunable_byte. 4732be4e471SJung-uk Kim */ 4742a18c71dSJung-uk Kim AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE; 4752a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 476313a0c13SJung-uk Kim AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE; 4772a18c71dSJung-uk Kim 4782a18c71dSJung-uk Kim #ifndef ACPI_DEBUG 4792a18c71dSJung-uk Kim /* 4802a18c71dSJung-uk Kim * Disable all debugging layers and levels. 4812a18c71dSJung-uk Kim */ 4822a18c71dSJung-uk Kim AcpiDbgLayer = 0; 4832a18c71dSJung-uk Kim AcpiDbgLevel = 0; 4842a18c71dSJung-uk Kim #endif 4852be4e471SJung-uk Kim 4862be4e471SJung-uk Kim /* Start up the ACPI CA subsystem. */ 4872be4e471SJung-uk Kim status = AcpiInitializeSubsystem(); 4882be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4892be4e471SJung-uk Kim device_printf(dev, "Could not initialize Subsystem: %s\n", 4902be4e471SJung-uk Kim AcpiFormatException(status)); 4912be4e471SJung-uk Kim goto out; 4922be4e471SJung-uk Kim } 4932be4e471SJung-uk Kim 494ae19af49SJung-uk Kim /* Override OS interfaces if the user requested. */ 495ae19af49SJung-uk Kim acpi_reset_interfaces(dev); 496ae19af49SJung-uk Kim 4972be4e471SJung-uk Kim /* Load ACPI name space. */ 4982be4e471SJung-uk Kim status = AcpiLoadTables(); 4992be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 5002be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n", 5012be4e471SJung-uk Kim AcpiFormatException(status)); 5022be4e471SJung-uk Kim goto out; 5032be4e471SJung-uk Kim } 5042be4e471SJung-uk Kim 505d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 506d320e05cSJohn Baldwin /* Handle MCFG table if present. */ 507d320e05cSJohn Baldwin acpi_enable_pcie(); 508d320e05cSJohn Baldwin #endif 509d320e05cSJohn Baldwin 51015e32d5dSMike Smith /* 511be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 512be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 513be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 514be2b1797SNate Lawson * object init pass. 51515e32d5dSMike Smith * 51632d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 51732d18aa5SMike Smith * 518be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 519be2b1797SNate Lawson * all our child devices, but on many systems it works here. 52015e32d5dSMike Smith */ 52132d18aa5SMike Smith flags = 0; 522d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 52332d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 524bee4aa4aSNate Lawson 525bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 526b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 527be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 528be2b1797SNate Lawson AcpiFormatException(status)); 529cb9b0d80SMike Smith goto out; 53015e32d5dSMike Smith } 53115e32d5dSMike Smith 532f8335e3aSNate Lawson /* 533f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 534f8335e3aSNate Lawson * the namespace has been evaluated. 535da72d149SNate Lawson * 536da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and 537da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't 538da72d149SNate Lawson * a problem but should be addressed eventually. 539f8335e3aSNate Lawson */ 540f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 541f8335e3aSNate Lawson 542bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 543b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 544be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 545be2b1797SNate Lawson AcpiFormatException(status)); 546b69ed3f4SMitsuru IWASAKI goto out; 547b69ed3f4SMitsuru IWASAKI } 548b69ed3f4SMitsuru IWASAKI 54915e32d5dSMike Smith /* 5501d073b1dSJohn Baldwin * Setup our sysctl tree. 5511d073b1dSJohn Baldwin * 5521d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5531d073b1dSJohn Baldwin */ 5541d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5551d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5561d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5571d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5581d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 559d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 560d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 561d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5621d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5631d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5641d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5651d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5661d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5671d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5681d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5691d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 570f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 571f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 572f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 573f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 574f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 575f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5762d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 577197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 57831f301d0SChristian Brueffer "sleep delay in seconds"); 579ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 580197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 5811611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 582197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 583d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 584d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 585d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 586d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 587d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW, 588d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 589bf0c18ecSNate Lawson 590bf0c18ecSNate Lawson /* 5912b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 592bf0c18ecSNate Lawson * stabilize. 593bf0c18ecSNate Lawson */ 5942b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5952d644607SMitsuru IWASAKI if (bootverbose) 5962d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 5972be111bfSDavide Italiano if ((env = kern_getenv("hw.acpi.verbose")) != NULL) { 598965a34fbSNate Lawson if (strcmp(env, "0") != 0) 599498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 600498d464fSMitsuru IWASAKI freeenv(env); 601498d464fSMitsuru IWASAKI } 6021d073b1dSJohn Baldwin 603ac731af5SJung-uk Kim /* Only enable reboot by default if the FADT says it is available. */ 604ac731af5SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) 605ac731af5SJung-uk Kim sc->acpi_handle_reboot = 1; 606ac731af5SJung-uk Kim 6072d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 608129d3046SJung-uk Kim if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 6092d610c46SNate Lawson sc->acpi_s4bios = 1; 6102d610c46SNate Lawson 611a0e73a12SJung-uk Kim /* Probe all supported sleep states. */ 612a0e73a12SJung-uk Kim acpi_sleep_states[ACPI_STATE_S0] = TRUE; 613a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 614efcc2a30SJung-uk Kim if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT, 615efcc2a30SJung-uk Kim __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) && 616efcc2a30SJung-uk Kim ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 617a0e73a12SJung-uk Kim acpi_sleep_states[state] = TRUE; 618a0e73a12SJung-uk Kim 6191d073b1dSJohn Baldwin /* 620ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 621a0e73a12SJung-uk Kim * to UNKNOWN by default to avoid surprising users. 62215e32d5dSMike Smith */ 623a0e73a12SJung-uk Kim sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 624a0e73a12SJung-uk Kim ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 625a0e73a12SJung-uk Kim sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 626a0e73a12SJung-uk Kim sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 627a0e73a12SJung-uk Kim ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 628a0e73a12SJung-uk Kim sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 629a0e73a12SJung-uk Kim ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 63015e32d5dSMike Smith 631ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 632a0e73a12SJung-uk Kim sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 63300a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 634a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) { 635ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 636ea27c63eSNate Lawson break; 637ea27c63eSNate Lawson } 638ea27c63eSNate Lawson 63913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 64015e32d5dSMike Smith 64115e32d5dSMike Smith /* 64215e32d5dSMike Smith * Scan the namespace and attach/initialise children. 64315e32d5dSMike Smith */ 64415e32d5dSMike Smith 64559e472e9SNate Lawson /* Register our shutdown handler. */ 646be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 647be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 64815e32d5dSMike Smith 64915e32d5dSMike Smith /* 65015e32d5dSMike Smith * Register our acpi event handlers. 65115e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 65215e32d5dSMike Smith */ 653be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 654be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 655be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 656be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 65715e32d5dSMike Smith 658be2b1797SNate Lawson /* Flag our initial states. */ 659a0e73a12SJung-uk Kim sc->acpi_enabled = TRUE; 66015e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 661a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 66215e32d5dSMike Smith 663be2b1797SNate Lawson /* Create the control device */ 664a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 6652a4689e8SRobert Watson "acpi"); 66615e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 66715e32d5dSMike Smith 668be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 669f86214b6SMitsuru IWASAKI goto out; 670f86214b6SMitsuru IWASAKI 671f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 672f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 673f9390180SMitsuru IWASAKI 674eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 675eeb6dba2SJohn Baldwin acpi_probe_children(dev); 676eeb6dba2SJohn Baldwin 6775a77b11bSJung-uk Kim /* Update all GPEs and enable runtime GPEs. */ 6785a77b11bSJung-uk Kim status = AcpiUpdateAllGpes(); 6795a77b11bSJung-uk Kim if (ACPI_FAILURE(status)) 6805a77b11bSJung-uk Kim device_printf(dev, "Could not update all GPEs: %s\n", 6815a77b11bSJung-uk Kim AcpiFormatException(status)); 6825a77b11bSJung-uk Kim 683a0e73a12SJung-uk Kim /* Allow sleep request after a while. */ 684fadf3fb9SJohn Baldwin callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0); 685fadf3fb9SJohn Baldwin callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME, 686fadf3fb9SJohn Baldwin acpi_sleep_enable, sc); 687a0e73a12SJung-uk Kim 688cb9b0d80SMike Smith error = 0; 689cb9b0d80SMike Smith 690cb9b0d80SMike Smith out: 691cb9b0d80SMike Smith return_VALUE (error); 69215e32d5dSMike Smith } 69315e32d5dSMike Smith 694a7a3177fSJung-uk Kim static void 695a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state) 696a7a3177fSJung-uk Kim { 697*44aba0f6SJung-uk Kim device_t child; 698a7a3177fSJung-uk Kim device_t *devlist; 699a7a3177fSJung-uk Kim int dstate, i, numdevs; 700a7a3177fSJung-uk Kim 701a7a3177fSJung-uk Kim if (device_get_children(dev, &devlist, &numdevs) != 0) 702a7a3177fSJung-uk Kim return; 703a7a3177fSJung-uk Kim 704a7a3177fSJung-uk Kim /* 705a7a3177fSJung-uk Kim * Retrieve and set D-state for the sleep state if _SxD is present. 706a7a3177fSJung-uk Kim * Skip children who aren't attached since they are handled separately. 707a7a3177fSJung-uk Kim */ 708a7a3177fSJung-uk Kim for (i = 0; i < numdevs; i++) { 709a7a3177fSJung-uk Kim child = devlist[i]; 710a7a3177fSJung-uk Kim dstate = state; 711a7a3177fSJung-uk Kim if (device_is_attached(child) && 712*44aba0f6SJung-uk Kim acpi_device_pwr_for_sleep(dev, child, &dstate) == 0) 713a7a3177fSJung-uk Kim acpi_set_powerstate(child, dstate); 714a7a3177fSJung-uk Kim } 715a7a3177fSJung-uk Kim free(devlist, M_TEMP); 716a7a3177fSJung-uk Kim } 717a7a3177fSJung-uk Kim 718169b539aSNate Lawson static int 71910ce62b9SNate Lawson acpi_suspend(device_t dev) 72010ce62b9SNate Lawson { 721a7a3177fSJung-uk Kim int error; 72210ce62b9SNate Lawson 723a56fe095SJohn Baldwin GIANT_REQUIRED; 724a56fe095SJohn Baldwin 72510ce62b9SNate Lawson error = bus_generic_suspend(dev); 726a7a3177fSJung-uk Kim if (error == 0) 727a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D3); 72810ce62b9SNate Lawson 72910ce62b9SNate Lawson return (error); 73010ce62b9SNate Lawson } 73110ce62b9SNate Lawson 73210ce62b9SNate Lawson static int 73310ce62b9SNate Lawson acpi_resume(device_t dev) 73410ce62b9SNate Lawson { 73510ce62b9SNate Lawson 736a56fe095SJohn Baldwin GIANT_REQUIRED; 737a56fe095SJohn Baldwin 738a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D0); 73910ce62b9SNate Lawson 74010ce62b9SNate Lawson return (bus_generic_resume(dev)); 74110ce62b9SNate Lawson } 74210ce62b9SNate Lawson 74310ce62b9SNate Lawson static int 744169b539aSNate Lawson acpi_shutdown(device_t dev) 745169b539aSNate Lawson { 746169b539aSNate Lawson 747a56fe095SJohn Baldwin GIANT_REQUIRED; 748a56fe095SJohn Baldwin 7490e414868SNate Lawson /* Allow children to shutdown first. */ 7500e414868SNate Lawson bus_generic_shutdown(dev); 7510e414868SNate Lawson 752bee4aa4aSNate Lawson /* 753bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 754bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 755bee4aa4aSNate Lawson */ 756d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 757d4b9ff91SNate Lawson 758169b539aSNate Lawson return (0); 759169b539aSNate Lawson } 760169b539aSNate Lawson 76115e32d5dSMike Smith /* 76215e32d5dSMike Smith * Handle a new device being added 76315e32d5dSMike Smith */ 76415e32d5dSMike Smith static device_t 7653d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit) 76615e32d5dSMike Smith { 76715e32d5dSMike Smith struct acpi_device *ad; 76815e32d5dSMike Smith device_t child; 76915e32d5dSMike Smith 770be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 77115e32d5dSMike Smith return (NULL); 77215e32d5dSMike Smith 77315e32d5dSMike Smith resource_list_init(&ad->ad_rl); 77415e32d5dSMike Smith 77515e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 77615e32d5dSMike Smith if (child != NULL) 77715e32d5dSMike Smith device_set_ivars(child, ad); 77843ce1c77SNate Lawson else 77943ce1c77SNate Lawson free(ad, M_ACPIDEV); 78015e32d5dSMike Smith return (child); 78115e32d5dSMike Smith } 78215e32d5dSMike Smith 78315e32d5dSMike Smith static int 78415e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 78515e32d5dSMike Smith { 78615e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 78715e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 78815e32d5dSMike Smith int retval = 0; 78915e32d5dSMike Smith 79015e32d5dSMike Smith retval += bus_print_child_header(bus, child); 7919ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 7929ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 7939ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 7949ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 795a91c5fa8SNate Lawson if (device_get_flags(child)) 796a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 797ffcf962dSAdrian Chadd retval += bus_print_child_domain(bus, child); 7982c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 79915e32d5dSMike Smith 80015e32d5dSMike Smith return (retval); 80115e32d5dSMike Smith } 80215e32d5dSMike Smith 80310ce62b9SNate Lawson /* 80410ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt 80510ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added. 80610ce62b9SNate Lawson * 80710ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and 80810ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect 80910ce62b9SNate Lawson * them to be powered up. 81010ce62b9SNate Lawson */ 81110ce62b9SNate Lawson static void 81210ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child) 81310ce62b9SNate Lawson { 81496cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 815a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 81696cf0b6dSWarner Losh #endif 81710ce62b9SNate Lawson } 81810ce62b9SNate Lawson 81910ce62b9SNate Lawson /* 82010ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up. 82110ce62b9SNate Lawson * 82210ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details). 82310ce62b9SNate Lawson */ 82410ce62b9SNate Lawson static void 82510ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver) 82610ce62b9SNate Lawson { 82710ce62b9SNate Lawson device_t child, *devlist; 82810ce62b9SNate Lawson int i, numdevs; 82910ce62b9SNate Lawson 83010ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev); 831099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs)) 832099ea4b5SWarner Losh return; 83310ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 83410ce62b9SNate Lawson child = devlist[i]; 83510ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) { 83696cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 837a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D0); 83810ce62b9SNate Lawson if (device_probe_and_attach(child) != 0) 839a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 84096cf0b6dSWarner Losh #else 84196cf0b6dSWarner Losh device_probe_and_attach(child); 84296cf0b6dSWarner Losh #endif 84310ce62b9SNate Lawson } 84410ce62b9SNate Lawson } 84510ce62b9SNate Lawson free(devlist, M_TEMP); 84610ce62b9SNate Lawson } 84710ce62b9SNate Lawson 84863600cc3SNate Lawson /* Location hint for devctl(8) */ 84963600cc3SNate Lawson static int 850247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 851247648afSTakanori Watanabe size_t buflen) 852247648afSTakanori Watanabe { 853247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 8549cf5a6aaSAdrian Chadd char buf2[32]; 8559cf5a6aaSAdrian Chadd int pxm; 856247648afSTakanori Watanabe 8579cf5a6aaSAdrian Chadd if (dinfo->ad_handle) { 858d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 8599cf5a6aaSAdrian Chadd if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) { 8609cf5a6aaSAdrian Chadd snprintf(buf2, 32, " _PXM=%d", pxm); 8619cf5a6aaSAdrian Chadd strlcat(buf, buf2, buflen); 8629cf5a6aaSAdrian Chadd } 8639cf5a6aaSAdrian Chadd } else { 864d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 8659cf5a6aaSAdrian Chadd } 866247648afSTakanori Watanabe return (0); 867247648afSTakanori Watanabe } 868247648afSTakanori Watanabe 86963600cc3SNate Lawson /* PnP information for devctl(8) */ 87063600cc3SNate Lawson static int 871247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 872247648afSTakanori Watanabe size_t buflen) 873247648afSTakanori Watanabe { 87463600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 87592488a57SJung-uk Kim ACPI_DEVICE_INFO *adinfo; 876247648afSTakanori Watanabe 87792488a57SJung-uk Kim if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 878d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 87992488a57SJung-uk Kim return (0); 88092488a57SJung-uk Kim } 88192488a57SJung-uk Kim 882247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 883247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 88492488a57SJung-uk Kim adinfo->HardwareId.String : "none", 88563600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 88692488a57SJung-uk Kim strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 887247648afSTakanori Watanabe AcpiOsFree(adinfo); 888247648afSTakanori Watanabe 889247648afSTakanori Watanabe return (0); 890247648afSTakanori Watanabe } 891247648afSTakanori Watanabe 892247648afSTakanori Watanabe /* 89315e32d5dSMike Smith * Handle per-device ivars 89415e32d5dSMike Smith */ 89515e32d5dSMike Smith static int 89615e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 89715e32d5dSMike Smith { 89815e32d5dSMike Smith struct acpi_device *ad; 89915e32d5dSMike Smith 90015e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9012d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 90215e32d5dSMike Smith return (ENOENT); 90315e32d5dSMike Smith } 90415e32d5dSMike Smith 905be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 90615e32d5dSMike Smith switch(index) { 90715e32d5dSMike Smith case ACPI_IVAR_HANDLE: 90815e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 90915e32d5dSMike Smith break; 91015e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 91115e32d5dSMike Smith *(void **)result = ad->ad_private; 91215e32d5dSMike Smith break; 913d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 914d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 915d4b9ff91SNate Lawson break; 91632d18aa5SMike Smith case ISA_IVAR_VENDORID: 91732d18aa5SMike Smith case ISA_IVAR_SERIAL: 91832d18aa5SMike Smith case ISA_IVAR_COMPATID: 91932d18aa5SMike Smith *(int *)result = -1; 92032d18aa5SMike Smith break; 92132d18aa5SMike Smith case ISA_IVAR_LOGICALID: 92232d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 92332d18aa5SMike Smith break; 92415e32d5dSMike Smith default: 92515e32d5dSMike Smith return (ENOENT); 92615e32d5dSMike Smith } 927be2b1797SNate Lawson 92815e32d5dSMike Smith return (0); 92915e32d5dSMike Smith } 93015e32d5dSMike Smith 93115e32d5dSMike Smith static int 93215e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 93315e32d5dSMike Smith { 93415e32d5dSMike Smith struct acpi_device *ad; 93515e32d5dSMike Smith 93615e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9372d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 93815e32d5dSMike Smith return (ENOENT); 93915e32d5dSMike Smith } 94015e32d5dSMike Smith 94115e32d5dSMike Smith switch(index) { 94215e32d5dSMike Smith case ACPI_IVAR_HANDLE: 94315e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 94415e32d5dSMike Smith break; 94515e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 94615e32d5dSMike Smith ad->ad_private = (void *)value; 94715e32d5dSMike Smith break; 948d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 949d4b9ff91SNate Lawson ad->ad_flags = (int)value; 950d4b9ff91SNate Lawson break; 95115e32d5dSMike Smith default: 9522ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 95315e32d5dSMike Smith return (ENOENT); 95415e32d5dSMike Smith } 955be2b1797SNate Lawson 95615e32d5dSMike Smith return (0); 95715e32d5dSMike Smith } 95815e32d5dSMike Smith 95915e32d5dSMike Smith /* 96015e32d5dSMike Smith * Handle child resource allocation/removal 96115e32d5dSMike Smith */ 96291233413SNate Lawson static struct resource_list * 96391233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 96415e32d5dSMike Smith { 96591233413SNate Lawson struct acpi_device *ad; 96615e32d5dSMike Smith 96791233413SNate Lawson ad = device_get_ivars(child); 96891233413SNate Lawson return (&ad->ad_rl); 96915e32d5dSMike Smith } 97015e32d5dSMike Smith 9710d484d24SJohn Baldwin static int 9720d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value) 9730d484d24SJohn Baldwin { 9740d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev); 9750d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 9760d484d24SJohn Baldwin struct resource_list_entry *rle; 9770d484d24SJohn Baldwin 9780d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 9790d484d24SJohn Baldwin if (rle->type != type) 9800d484d24SJohn Baldwin continue; 9810d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value) 9820d484d24SJohn Baldwin return (1); 9830d484d24SJohn Baldwin } 9840d484d24SJohn Baldwin return (0); 9850d484d24SJohn Baldwin } 9860d484d24SJohn Baldwin 9870d484d24SJohn Baldwin /* 9880d484d24SJohn Baldwin * Wire device unit numbers based on resource matches in hints. 9890d484d24SJohn Baldwin */ 9900d484d24SJohn Baldwin static void 9910d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 9920d484d24SJohn Baldwin int *unitp) 9930d484d24SJohn Baldwin { 9940d484d24SJohn Baldwin const char *s; 9950d484d24SJohn Baldwin long value; 9960d484d24SJohn Baldwin int line, matches, unit; 9970d484d24SJohn Baldwin 9980d484d24SJohn Baldwin /* 9990d484d24SJohn Baldwin * Iterate over all the hints for the devices with the specified 10000d484d24SJohn Baldwin * name to see if one's resources are a subset of this device. 10010d484d24SJohn Baldwin */ 10020d484d24SJohn Baldwin line = 0; 10030d484d24SJohn Baldwin for (;;) { 10040d484d24SJohn Baldwin if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 10050d484d24SJohn Baldwin break; 10060d484d24SJohn Baldwin 10070d484d24SJohn Baldwin /* Must have an "at" for acpi or isa. */ 10080d484d24SJohn Baldwin resource_string_value(name, unit, "at", &s); 10090d484d24SJohn Baldwin if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 10100d484d24SJohn Baldwin strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 10110d484d24SJohn Baldwin continue; 10120d484d24SJohn Baldwin 10130d484d24SJohn Baldwin /* 10142fcd493cSJohn Baldwin * Check for matching resources. We must have at least one match. 10152fcd493cSJohn Baldwin * Since I/O and memory resources cannot be shared, if we get a 10162fcd493cSJohn Baldwin * match on either of those, ignore any mismatches in IRQs or DRQs. 10170d484d24SJohn Baldwin * 10180d484d24SJohn Baldwin * XXX: We may want to revisit this to be more lenient and wire 10190d484d24SJohn Baldwin * as long as it gets one match. 10200d484d24SJohn Baldwin */ 10210d484d24SJohn Baldwin matches = 0; 10220d484d24SJohn Baldwin if (resource_long_value(name, unit, "port", &value) == 0) { 10232fcd493cSJohn Baldwin /* 10242fcd493cSJohn Baldwin * Floppy drive controllers are notorious for having a 10252fcd493cSJohn Baldwin * wide variety of resources not all of which include the 10262fcd493cSJohn Baldwin * first port that is specified by the hint (typically 10272fcd493cSJohn Baldwin * 0x3f0) (see the comment above fdc_isa_alloc_resources() 10282fcd493cSJohn Baldwin * in fdc_isa.c). However, they do all seem to include 10292fcd493cSJohn Baldwin * port + 2 (e.g. 0x3f2) so for a floppy device, look for 10302fcd493cSJohn Baldwin * 'value + 2' in the port resources instead of the hint 10312fcd493cSJohn Baldwin * value. 10322fcd493cSJohn Baldwin */ 10332fcd493cSJohn Baldwin if (strcmp(name, "fdc") == 0) 10342fcd493cSJohn Baldwin value += 2; 10350d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 10360d484d24SJohn Baldwin matches++; 10370d484d24SJohn Baldwin else 10380d484d24SJohn Baldwin continue; 10390d484d24SJohn Baldwin } 10400d484d24SJohn Baldwin if (resource_long_value(name, unit, "maddr", &value) == 0) { 10410d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 10420d484d24SJohn Baldwin matches++; 10430d484d24SJohn Baldwin else 10440d484d24SJohn Baldwin continue; 10450d484d24SJohn Baldwin } 10462fcd493cSJohn Baldwin if (matches > 0) 10472fcd493cSJohn Baldwin goto matched; 10480d484d24SJohn Baldwin if (resource_long_value(name, unit, "irq", &value) == 0) { 10490d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 10500d484d24SJohn Baldwin matches++; 10510d484d24SJohn Baldwin else 10520d484d24SJohn Baldwin continue; 10530d484d24SJohn Baldwin } 10540d484d24SJohn Baldwin if (resource_long_value(name, unit, "drq", &value) == 0) { 10550d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 10560d484d24SJohn Baldwin matches++; 10570d484d24SJohn Baldwin else 10580d484d24SJohn Baldwin continue; 10590d484d24SJohn Baldwin } 10600d484d24SJohn Baldwin 10612fcd493cSJohn Baldwin matched: 10620d484d24SJohn Baldwin if (matches > 0) { 10630d484d24SJohn Baldwin /* We have a winner! */ 10640d484d24SJohn Baldwin *unitp = unit; 10650d484d24SJohn Baldwin break; 10660d484d24SJohn Baldwin } 10670d484d24SJohn Baldwin } 10680d484d24SJohn Baldwin } 10690d484d24SJohn Baldwin 1070adad4744SNate Lawson /* 1071ffcf962dSAdrian Chadd * Fech the NUMA domain for the given device. 1072ffcf962dSAdrian Chadd * 1073ffcf962dSAdrian Chadd * If a device has a _PXM method, map that to a NUMA domain. 1074ffcf962dSAdrian Chadd * 1075ffcf962dSAdrian Chadd * If none is found, then it'll call the parent method. 1076ffcf962dSAdrian Chadd * If there's no domain, return ENOENT. 1077ffcf962dSAdrian Chadd */ 1078ffcf962dSAdrian Chadd int 1079ffcf962dSAdrian Chadd acpi_get_domain(device_t dev, device_t child, int *domain) 1080ffcf962dSAdrian Chadd { 1081ffcf962dSAdrian Chadd #if MAXMEMDOM > 1 1082ffcf962dSAdrian Chadd ACPI_HANDLE h; 1083ffcf962dSAdrian Chadd int d, pxm; 1084ffcf962dSAdrian Chadd 1085ffcf962dSAdrian Chadd h = acpi_get_handle(child); 1086ffcf962dSAdrian Chadd if ((h != NULL) && 1087ffcf962dSAdrian Chadd ACPI_SUCCESS(acpi_GetInteger(h, "_PXM", &pxm))) { 1088ffcf962dSAdrian Chadd d = acpi_map_pxm_to_vm_domainid(pxm); 1089ffcf962dSAdrian Chadd if (d < 0) 1090ffcf962dSAdrian Chadd return (ENOENT); 1091ffcf962dSAdrian Chadd *domain = d; 1092ffcf962dSAdrian Chadd return (0); 1093ffcf962dSAdrian Chadd } 1094ffcf962dSAdrian Chadd #endif 1095ffcf962dSAdrian Chadd /* No _PXM node; go up a level */ 1096ffcf962dSAdrian Chadd return (bus_generic_get_domain(dev, child, domain)); 1097ffcf962dSAdrian Chadd } 1098ffcf962dSAdrian Chadd 1099ffcf962dSAdrian Chadd /* 1100adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1101adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 1102adad4744SNate Lawson */ 1103adad4744SNate Lawson static int 1104adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 1105adad4744SNate Lawson { 1106adad4744SNate Lawson struct resource *res; 1107adad4744SNate Lawson struct resource_list *rl; 1108adad4744SNate Lawson struct resource_list_entry *rle; 1109adad4744SNate Lawson struct rman *rm; 1110da72d149SNate Lawson char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1111da72d149SNate Lawson device_t *children; 1112da72d149SNate Lawson int child_count, i; 1113da72d149SNate Lawson 1114da72d149SNate Lawson /* 1115da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we 1116da72d149SNate Lawson * had multi-pass probe/attach. 1117da72d149SNate Lawson */ 1118da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0) 1119da72d149SNate Lawson return (ENXIO); 1120da72d149SNate Lawson for (i = 0; i < child_count; i++) { 1121da72d149SNate Lawson if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1122da72d149SNate Lawson device_probe_and_attach(children[i]); 1123da72d149SNate Lawson } 1124da72d149SNate Lawson free(children, M_TEMP); 1125adad4744SNate Lawson 1126adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1127be1bf4d2SPoul-Henning Kamp STAILQ_FOREACH(rle, rl, link) { 1128adad4744SNate Lawson if (rle->res != NULL) { 1129adad4744SNate Lawson device_printf(dev, "duplicate resource for %lx\n", rle->start); 1130adad4744SNate Lawson continue; 1131adad4744SNate Lawson } 1132adad4744SNate Lawson 1133adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 1134adad4744SNate Lawson switch (rle->type) { 1135adad4744SNate Lawson case SYS_RES_IOPORT: 1136adad4744SNate Lawson rm = &acpi_rman_io; 1137adad4744SNate Lawson break; 1138adad4744SNate Lawson case SYS_RES_MEMORY: 1139adad4744SNate Lawson rm = &acpi_rman_mem; 1140adad4744SNate Lawson break; 1141adad4744SNate Lawson default: 1142adad4744SNate Lawson continue; 1143adad4744SNate Lawson } 1144adad4744SNate Lawson 1145adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 1146adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1147adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1148adad4744SNate Lawson if (res != NULL) { 1149adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1150adad4744SNate Lawson rle->res = res; 1151adad4744SNate Lawson } else 1152adad4744SNate Lawson device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 1153adad4744SNate Lawson rle->start, rle->count, rle->type); 1154adad4744SNate Lawson } 1155adad4744SNate Lawson return (0); 1156adad4744SNate Lawson } 1157adad4744SNate Lawson 1158ea233199SJohn Baldwin static char *pcilink_ids[] = { "PNP0C0F", NULL }; 1159ea233199SJohn Baldwin static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1160ea233199SJohn Baldwin 1161ea233199SJohn Baldwin /* 1162ea233199SJohn Baldwin * Reserve declared resources for devices found during attach once system 1163ea233199SJohn Baldwin * resources have been allocated. 1164ea233199SJohn Baldwin */ 1165ea233199SJohn Baldwin static void 1166ea233199SJohn Baldwin acpi_reserve_resources(device_t dev) 1167ea233199SJohn Baldwin { 1168ea233199SJohn Baldwin struct resource_list_entry *rle; 1169ea233199SJohn Baldwin struct resource_list *rl; 1170ea233199SJohn Baldwin struct acpi_device *ad; 1171ea233199SJohn Baldwin struct acpi_softc *sc; 1172ea233199SJohn Baldwin device_t *children; 1173ea233199SJohn Baldwin int child_count, i; 1174ea233199SJohn Baldwin 1175ea233199SJohn Baldwin sc = device_get_softc(dev); 1176ea233199SJohn Baldwin if (device_get_children(dev, &children, &child_count) != 0) 1177ea233199SJohn Baldwin return; 1178ea233199SJohn Baldwin for (i = 0; i < child_count; i++) { 1179ea233199SJohn Baldwin ad = device_get_ivars(children[i]); 1180ea233199SJohn Baldwin rl = &ad->ad_rl; 1181ea233199SJohn Baldwin 1182ea233199SJohn Baldwin /* Don't reserve system resources. */ 1183ea233199SJohn Baldwin if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1184ea233199SJohn Baldwin continue; 1185ea233199SJohn Baldwin 1186ea233199SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 1187ea233199SJohn Baldwin /* 1188ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things 1189ea233199SJohn Baldwin * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET 1190ea233199SJohn Baldwin * when using legacy routing). 1191ea233199SJohn Baldwin */ 1192ea233199SJohn Baldwin if (rle->type == SYS_RES_IRQ) 1193ea233199SJohn Baldwin continue; 1194ea233199SJohn Baldwin 1195ea233199SJohn Baldwin /* 11960d81cf12SJohn Baldwin * Don't reserve the resource if it is already allocated. 11970d81cf12SJohn Baldwin * The acpi_ec(4) driver can allocate its resources early 11980d81cf12SJohn Baldwin * if ECDT is present. 11990d81cf12SJohn Baldwin */ 12000d81cf12SJohn Baldwin if (rle->res != NULL) 12010d81cf12SJohn Baldwin continue; 12020d81cf12SJohn Baldwin 12030d81cf12SJohn Baldwin /* 1204ea233199SJohn Baldwin * Try to reserve the resource from our parent. If this 1205ea233199SJohn Baldwin * fails because the resource is a system resource, just 1206ea233199SJohn Baldwin * let it be. The resource range is already reserved so 1207ea233199SJohn Baldwin * that other devices will not use it. If the driver 1208ea233199SJohn Baldwin * needs to allocate the resource, then 1209ea233199SJohn Baldwin * acpi_alloc_resource() will sub-alloc from the system 1210ea233199SJohn Baldwin * resource. 1211ea233199SJohn Baldwin */ 1212ea233199SJohn Baldwin resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid, 1213ea233199SJohn Baldwin rle->start, rle->end, rle->count, 0); 1214ea233199SJohn Baldwin } 1215ea233199SJohn Baldwin } 1216ea233199SJohn Baldwin free(children, M_TEMP); 1217ea233199SJohn Baldwin sc->acpi_resources_reserved = 1; 1218ea233199SJohn Baldwin } 1219ea233199SJohn Baldwin 1220ea233199SJohn Baldwin static int 1221ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid, 1222ea233199SJohn Baldwin u_long start, u_long count) 1223ea233199SJohn Baldwin { 1224ea233199SJohn Baldwin struct acpi_softc *sc = device_get_softc(dev); 1225ea233199SJohn Baldwin struct acpi_device *ad = device_get_ivars(child); 1226ea233199SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 12277e3343bfSJohn Baldwin ACPI_DEVICE_INFO *devinfo; 1228ea233199SJohn Baldwin u_long end; 1229ea233199SJohn Baldwin 1230ea233199SJohn Baldwin /* Ignore IRQ resources for PCI link devices. */ 1231ea233199SJohn Baldwin if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL) 1232ea233199SJohn Baldwin return (0); 1233ea233199SJohn Baldwin 12347e3343bfSJohn Baldwin /* 1235d2dc06caSJohn Baldwin * Ignore most resources for PCI root bridges. Some BIOSes 12367e3343bfSJohn Baldwin * incorrectly enumerate the memory ranges they decode as plain 1237d2dc06caSJohn Baldwin * memory resources instead of as ResourceProducer ranges. Other 1238d2dc06caSJohn Baldwin * BIOSes incorrectly list system resource entries for I/O ranges 1239d2dc06caSJohn Baldwin * under the PCI bridge. Do allow the one known-correct case on 1240d2dc06caSJohn Baldwin * x86 of a PCI bridge claiming the I/O ports used for PCI config 1241d2dc06caSJohn Baldwin * access. 12427e3343bfSJohn Baldwin */ 1243d2dc06caSJohn Baldwin if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 12447e3343bfSJohn Baldwin if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) { 12457e3343bfSJohn Baldwin if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) { 1246d2dc06caSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1247d2dc06caSJohn Baldwin if (!(type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT)) 1248d2dc06caSJohn Baldwin #endif 1249d2dc06caSJohn Baldwin { 12507e3343bfSJohn Baldwin AcpiOsFree(devinfo); 12517e3343bfSJohn Baldwin return (0); 12527e3343bfSJohn Baldwin } 1253d2dc06caSJohn Baldwin } 12547e3343bfSJohn Baldwin AcpiOsFree(devinfo); 12557e3343bfSJohn Baldwin } 12567e3343bfSJohn Baldwin } 12577e3343bfSJohn Baldwin 1258ea233199SJohn Baldwin /* If the resource is already allocated, fail. */ 1259ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) 1260ea233199SJohn Baldwin return (EBUSY); 1261ea233199SJohn Baldwin 1262ea233199SJohn Baldwin /* If the resource is already reserved, release it. */ 1263ea233199SJohn Baldwin if (resource_list_reserved(rl, type, rid)) 1264ea233199SJohn Baldwin resource_list_unreserve(rl, dev, child, type, rid); 1265ea233199SJohn Baldwin 1266ea233199SJohn Baldwin /* Add the resource. */ 1267ea233199SJohn Baldwin end = (start + count - 1); 1268ea233199SJohn Baldwin resource_list_add(rl, type, rid, start, end, count); 1269ea233199SJohn Baldwin 1270ea233199SJohn Baldwin /* Don't reserve resources until the system resources are allocated. */ 1271ea233199SJohn Baldwin if (!sc->acpi_resources_reserved) 1272ea233199SJohn Baldwin return (0); 1273ea233199SJohn Baldwin 1274ea233199SJohn Baldwin /* Don't reserve system resources. */ 1275ea233199SJohn Baldwin if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL) 1276ea233199SJohn Baldwin return (0); 1277ea233199SJohn Baldwin 1278ea233199SJohn Baldwin /* 1279ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things to 1280ea233199SJohn Baldwin * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when 1281ea233199SJohn Baldwin * using legacy routing). 1282ea233199SJohn Baldwin */ 1283ea233199SJohn Baldwin if (type == SYS_RES_IRQ) 1284ea233199SJohn Baldwin return (0); 1285ea233199SJohn Baldwin 1286ea233199SJohn Baldwin /* 1287ea233199SJohn Baldwin * Reserve the resource. 1288ea233199SJohn Baldwin * 1289ea233199SJohn Baldwin * XXX: Ignores failure for now. Failure here is probably a 1290ea233199SJohn Baldwin * BIOS/firmware bug? 1291ea233199SJohn Baldwin */ 1292ea233199SJohn Baldwin resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0); 1293ea233199SJohn Baldwin return (0); 1294ea233199SJohn Baldwin } 1295ea233199SJohn Baldwin 129615e32d5dSMike Smith static struct resource * 129715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 129815e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 129915e32d5dSMike Smith { 130095957f62SJohn Baldwin ACPI_RESOURCE ares; 1301ea233199SJohn Baldwin struct acpi_device *ad; 130291233413SNate Lawson struct resource_list_entry *rle; 1303ea233199SJohn Baldwin struct resource_list *rl; 130491233413SNate Lawson struct resource *res; 1305ea233199SJohn Baldwin int isdefault = (start == 0UL && end == ~0UL); 130615e2f34fSNate Lawson 130791233413SNate Lawson /* 1308ea233199SJohn Baldwin * First attempt at allocating the resource. For direct children, 1309ea233199SJohn Baldwin * use resource_list_alloc() to handle reserved resources. For 1310f6133e71SJohn Baldwin * other devices, pass the request up to our parent. 131191233413SNate Lawson */ 1312ea233199SJohn Baldwin if (bus == device_get_parent(child)) { 1313ea233199SJohn Baldwin ad = device_get_ivars(child); 1314ea233199SJohn Baldwin rl = &ad->ad_rl; 131591233413SNate Lawson 1316397c30a8SJohn Baldwin /* 1317ea233199SJohn Baldwin * Simulate the behavior of the ISA bus for direct children 1318ea233199SJohn Baldwin * devices. That is, if a non-default range is specified for 1319ea233199SJohn Baldwin * a resource that doesn't exist, use bus_set_resource() to 1320ea233199SJohn Baldwin * add the resource before allocating it. Note that these 1321ea233199SJohn Baldwin * resources will not be reserved. 1322397c30a8SJohn Baldwin */ 1323ea233199SJohn Baldwin if (!isdefault && resource_list_find(rl, type, *rid) == NULL) 1324ea233199SJohn Baldwin resource_list_add(rl, type, *rid, start, end, count); 1325ea233199SJohn Baldwin res = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 1326ea233199SJohn Baldwin flags); 1327ea233199SJohn Baldwin if (res != NULL && type == SYS_RES_IRQ) { 132895957f62SJohn Baldwin /* 132995957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 133095957f62SJohn Baldwin * configure the interrupt associated with a device when we 133195957f62SJohn Baldwin * parse the resources but have to defer it until a driver 133295957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 133395957f62SJohn Baldwin * 133495957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 133595957f62SJohn Baldwin */ 133695957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 133795957f62SJohn Baldwin acpi_config_intr(child, &ares); 133895957f62SJohn Baldwin } 133915e2f34fSNate Lawson 1340ea233199SJohn Baldwin /* 1341ea233199SJohn Baldwin * If this is an allocation of the "default" range for a given 1342ea233199SJohn Baldwin * RID, fetch the exact bounds for this resource from the 1343ea233199SJohn Baldwin * resource list entry to try to allocate the range from the 1344ea233199SJohn Baldwin * system resource regions. 1345ea233199SJohn Baldwin */ 1346ea233199SJohn Baldwin if (res == NULL && isdefault) { 1347ea233199SJohn Baldwin rle = resource_list_find(rl, type, *rid); 1348ea233199SJohn Baldwin if (rle != NULL) { 1349ea233199SJohn Baldwin start = rle->start; 1350ea233199SJohn Baldwin end = rle->end; 1351ea233199SJohn Baldwin count = rle->count; 1352ea233199SJohn Baldwin } 1353ea233199SJohn Baldwin } 1354ea233199SJohn Baldwin } else 1355ea233199SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1356ea233199SJohn Baldwin start, end, count, flags); 1357ea233199SJohn Baldwin 1358ea233199SJohn Baldwin /* 1359ea233199SJohn Baldwin * If the first attempt failed and this is an allocation of a 1360ea233199SJohn Baldwin * specific range, try to satisfy the request via a suballocation 13615d0d779bSJohn Baldwin * from our system resource regions. 1362ea233199SJohn Baldwin */ 13635d0d779bSJohn Baldwin if (res == NULL && start + count - 1 == end) 13645d0d779bSJohn Baldwin res = acpi_alloc_sysres(child, type, rid, start, end, count, flags); 13655d0d779bSJohn Baldwin return (res); 13665d0d779bSJohn Baldwin } 13675d0d779bSJohn Baldwin 13685d0d779bSJohn Baldwin /* 13695d0d779bSJohn Baldwin * Attempt to allocate a specific resource range from the system 13705d0d779bSJohn Baldwin * resource ranges. Note that we only handle memory and I/O port 13715d0d779bSJohn Baldwin * system resources. 13725d0d779bSJohn Baldwin */ 13735d0d779bSJohn Baldwin struct resource * 13745d0d779bSJohn Baldwin acpi_alloc_sysres(device_t child, int type, int *rid, u_long start, u_long end, 13755d0d779bSJohn Baldwin u_long count, u_int flags) 13765d0d779bSJohn Baldwin { 13775d0d779bSJohn Baldwin struct rman *rm; 13785d0d779bSJohn Baldwin struct resource *res; 13795d0d779bSJohn Baldwin 1380ea233199SJohn Baldwin switch (type) { 1381ea233199SJohn Baldwin case SYS_RES_IOPORT: 1382ea233199SJohn Baldwin rm = &acpi_rman_io; 1383ea233199SJohn Baldwin break; 1384ea233199SJohn Baldwin case SYS_RES_MEMORY: 1385ea233199SJohn Baldwin rm = &acpi_rman_mem; 1386ea233199SJohn Baldwin break; 1387ea233199SJohn Baldwin default: 1388ea233199SJohn Baldwin return (NULL); 1389ea233199SJohn Baldwin } 1390ea233199SJohn Baldwin 13915d0d779bSJohn Baldwin KASSERT(start + count - 1 == end, ("wildcard resource range")); 1392ea233199SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1393ea233199SJohn Baldwin child); 1394ea233199SJohn Baldwin if (res == NULL) 1395ea233199SJohn Baldwin return (NULL); 1396ea233199SJohn Baldwin 1397ea233199SJohn Baldwin rman_set_rid(res, *rid); 1398ea233199SJohn Baldwin 1399ea233199SJohn Baldwin /* If requested, activate the resource using the parent's method. */ 1400ea233199SJohn Baldwin if (flags & RF_ACTIVE) 1401ea233199SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 1402ea233199SJohn Baldwin rman_release_resource(res); 1403ea233199SJohn Baldwin return (NULL); 1404ea233199SJohn Baldwin } 1405ea233199SJohn Baldwin 140691233413SNate Lawson return (res); 140715e32d5dSMike Smith } 140815e32d5dSMike Smith 140915e32d5dSMike Smith static int 1410049dc0d1SJohn Baldwin acpi_is_resource_managed(int type, struct resource *r) 141115e32d5dSMike Smith { 141215e32d5dSMike Smith 1413397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1414397c30a8SJohn Baldwin switch (type) { 1415397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1416049dc0d1SJohn Baldwin return (rman_is_region_manager(r, &acpi_rman_io)); 1417397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1418049dc0d1SJohn Baldwin return (rman_is_region_manager(r, &acpi_rman_mem)); 1419397c30a8SJohn Baldwin } 1420049dc0d1SJohn Baldwin return (0); 1421049dc0d1SJohn Baldwin } 1422049dc0d1SJohn Baldwin 1423049dc0d1SJohn Baldwin static int 1424049dc0d1SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 1425049dc0d1SJohn Baldwin u_long start, u_long end) 1426049dc0d1SJohn Baldwin { 1427049dc0d1SJohn Baldwin 1428049dc0d1SJohn Baldwin if (acpi_is_resource_managed(type, r)) 1429049dc0d1SJohn Baldwin return (rman_adjust_resource(r, start, end)); 1430049dc0d1SJohn Baldwin return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 1431049dc0d1SJohn Baldwin } 1432049dc0d1SJohn Baldwin 1433049dc0d1SJohn Baldwin static int 1434049dc0d1SJohn Baldwin acpi_release_resource(device_t bus, device_t child, int type, int rid, 1435049dc0d1SJohn Baldwin struct resource *r) 1436049dc0d1SJohn Baldwin { 1437049dc0d1SJohn Baldwin int ret; 1438397c30a8SJohn Baldwin 143991233413SNate Lawson /* 1440397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers, 1441ea233199SJohn Baldwin * deactivate it and release it to the local pool. 144291233413SNate Lawson */ 1443049dc0d1SJohn Baldwin if (acpi_is_resource_managed(type, r)) { 144491233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 144591233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 144691233413SNate Lawson if (ret != 0) 144791233413SNate Lawson return (ret); 144815e32d5dSMike Smith } 1449ea233199SJohn Baldwin return (rman_release_resource(r)); 1450ea233199SJohn Baldwin } 1451ea233199SJohn Baldwin 1452ea233199SJohn Baldwin return (bus_generic_rl_release_resource(bus, child, type, rid, r)); 1453ea233199SJohn Baldwin } 145415e32d5dSMike Smith 14558b28b622SNate Lawson static void 14568b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid) 14578b28b622SNate Lawson { 14588b28b622SNate Lawson struct resource_list *rl; 14598b28b622SNate Lawson 14608b28b622SNate Lawson rl = acpi_get_rlist(bus, child); 1461ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) { 1462ea233199SJohn Baldwin device_printf(bus, "delete_resource: Resource still owned by child" 1463ea233199SJohn Baldwin " (type=%d, rid=%d)\n", type, rid); 1464ea233199SJohn Baldwin return; 1465ea233199SJohn Baldwin } 1466ea233199SJohn Baldwin resource_list_unreserve(rl, bus, child, type, rid); 14678b28b622SNate Lawson resource_list_delete(rl, type, rid); 14688b28b622SNate Lawson } 14698b28b622SNate Lawson 1470dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 1471e1c4bf3fSNate Lawson int 1472e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1473907b6777SNate Lawson struct resource **res, u_int flags) 1474dc750869SNate Lawson { 1475e1c4bf3fSNate Lawson int error, res_type; 1476dc750869SNate Lawson 1477e1c4bf3fSNate Lawson error = ENOMEM; 14788f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1479e1c4bf3fSNate Lawson return (EINVAL); 1480dc750869SNate Lawson 14818f118e25SNate Lawson /* We only support memory and IO spaces. */ 14822be4e471SJung-uk Kim switch (gas->SpaceId) { 1483dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1484e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY; 1485dc750869SNate Lawson break; 1486dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 1487e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT; 1488dc750869SNate Lawson break; 1489dc750869SNate Lawson default: 1490e1c4bf3fSNate Lawson return (EOPNOTSUPP); 1491dc750869SNate Lawson } 1492dc750869SNate Lawson 14932fe912dfSNate Lawson /* 1494f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means 1495f45fc848SNate Lawson * it is a bit field and just allocate a byte. 14962fe912dfSNate Lawson */ 14972be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8) 14982be4e471SJung-uk Kim gas->BitWidth = 8; 14992fe912dfSNate Lawson 15008f118e25SNate Lawson /* Validate the address after we're sure we support the space. */ 15012be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0) 15028f118e25SNate Lawson return (EINVAL); 15038f118e25SNate Lawson 1504e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address, 15052be4e471SJung-uk Kim gas->BitWidth / 8); 1506907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1507e1c4bf3fSNate Lawson if (*res != NULL) { 1508e1c4bf3fSNate Lawson *type = res_type; 1509e1c4bf3fSNate Lawson error = 0; 1510ca2c69c8SNate Lawson } else 1511ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid); 1512ca2c69c8SNate Lawson 1513e1c4bf3fSNate Lawson return (error); 1514dc750869SNate Lawson } 1515dc750869SNate Lawson 1516c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 15171e4925e8SNate Lawson static uint32_t 151832d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 1519bc0f2195SMike Smith { 15201e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1521bc0f2195SMike Smith ACPI_HANDLE h; 152292488a57SJung-uk Kim uint32_t pnpid; 152332d18aa5SMike Smith 1524b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 152532d18aa5SMike Smith 15266fca9360SNate Lawson /* Fetch and validate the HID. */ 152792488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 152892488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 152992488a57SJung-uk Kim return_VALUE (0); 153032d18aa5SMike Smith 153192488a57SJung-uk Kim pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 153292488a57SJung-uk Kim devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 153392488a57SJung-uk Kim PNP_EISAID(devinfo->HardwareId.String) : 0; 153492488a57SJung-uk Kim AcpiOsFree(devinfo); 1535be2b1797SNate Lawson 153632d18aa5SMike Smith return_VALUE (pnpid); 153732d18aa5SMike Smith } 153832d18aa5SMike Smith 15391e4925e8SNate Lawson static int 15401e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1541b2566207STakanori Watanabe { 15421e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 15438ef1a331SJung-uk Kim ACPI_PNP_DEVICE_ID *ids; 1544b2566207STakanori Watanabe ACPI_HANDLE h; 15451e4925e8SNate Lawson uint32_t *pnpid; 154692488a57SJung-uk Kim int i, valid; 1547b2566207STakanori Watanabe 1548b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1549b2566207STakanori Watanabe 15501e4925e8SNate Lawson pnpid = cids; 1551bd189fe7SNate Lawson 15521e4925e8SNate Lawson /* Fetch and validate the CID */ 155392488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 155492488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 155592488a57SJung-uk Kim return_VALUE (0); 1556b2566207STakanori Watanabe 155792488a57SJung-uk Kim if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 155892488a57SJung-uk Kim AcpiOsFree(devinfo); 155992488a57SJung-uk Kim return_VALUE (0); 1560b2566207STakanori Watanabe } 1561b2566207STakanori Watanabe 156292488a57SJung-uk Kim if (devinfo->CompatibleIdList.Count < count) 156392488a57SJung-uk Kim count = devinfo->CompatibleIdList.Count; 156492488a57SJung-uk Kim ids = devinfo->CompatibleIdList.Ids; 156592488a57SJung-uk Kim for (i = 0, valid = 0; i < count; i++) 156692488a57SJung-uk Kim if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 156792488a57SJung-uk Kim strncmp(ids[i].String, "PNP", 3) == 0) { 156892488a57SJung-uk Kim *pnpid++ = PNP_EISAID(ids[i].String); 156992488a57SJung-uk Kim valid++; 157092488a57SJung-uk Kim } 157192488a57SJung-uk Kim AcpiOsFree(devinfo); 157292488a57SJung-uk Kim 15731e4925e8SNate Lawson return_VALUE (valid); 15741e4925e8SNate Lawson } 1575b2566207STakanori Watanabe 1576ce619b2eSNate Lawson static char * 1577ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1578ce619b2eSNate Lawson { 1579ce619b2eSNate Lawson ACPI_HANDLE h; 158092488a57SJung-uk Kim ACPI_OBJECT_TYPE t; 1581ce619b2eSNate Lawson int i; 1582ce619b2eSNate Lawson 1583ce619b2eSNate Lawson h = acpi_get_handle(dev); 158492488a57SJung-uk Kim if (ids == NULL || h == NULL) 158592488a57SJung-uk Kim return (NULL); 158692488a57SJung-uk Kim t = acpi_get_type(dev); 158792488a57SJung-uk Kim if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 1588ce619b2eSNate Lawson return (NULL); 1589ce619b2eSNate Lawson 1590ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1591ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1592ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1593ce619b2eSNate Lawson return (ids[i]); 1594ce619b2eSNate Lawson } 1595ce619b2eSNate Lawson return (NULL); 1596ce619b2eSNate Lawson } 1597ce619b2eSNate Lawson 1598ce619b2eSNate Lawson static ACPI_STATUS 1599ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1600ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1601ce619b2eSNate Lawson { 1602ce619b2eSNate Lawson ACPI_HANDLE h; 1603ce619b2eSNate Lawson 1604df8d2a32SNate Lawson if (dev == NULL) 1605df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1606df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1607ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1608ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1609ce619b2eSNate Lawson } 1610ce619b2eSNate Lawson 161162508c53SJohn Baldwin int 161210ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 161310ce62b9SNate Lawson { 161410ce62b9SNate Lawson struct acpi_softc *sc; 161510ce62b9SNate Lawson ACPI_HANDLE handle; 161610ce62b9SNate Lawson ACPI_STATUS status; 161710ce62b9SNate Lawson char sxd[8]; 161810ce62b9SNate Lawson 161910ce62b9SNate Lawson handle = acpi_get_handle(dev); 162010ce62b9SNate Lawson 162110ce62b9SNate Lawson /* 162210ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down. 162310ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when 162410ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may 162510ce62b9SNate Lawson * need special handling in their drivers. 162610ce62b9SNate Lawson */ 1627a7a3177fSJung-uk Kim if (dstate == NULL || handle == NULL || 162810ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") || 162910ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") || 163010ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") || 163110ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") || 163210ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511")) 163310ce62b9SNate Lawson return (ENXIO); 163410ce62b9SNate Lawson 163510ce62b9SNate Lawson /* 1636a7a3177fSJung-uk Kim * Override next state with the value from _SxD, if present. 1637a7a3177fSJung-uk Kim * Note illegal _S0D is evaluated because some systems expect this. 163810ce62b9SNate Lawson */ 1639a7a3177fSJung-uk Kim sc = device_get_softc(bus); 164010ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 164110ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate); 1642a7a3177fSJung-uk Kim if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1643a7a3177fSJung-uk Kim device_printf(dev, "failed to get %s on %s: %s\n", sxd, 1644a7a3177fSJung-uk Kim acpi_name(handle), AcpiFormatException(status)); 1645a7a3177fSJung-uk Kim return (ENXIO); 164610ce62b9SNate Lawson } 164710ce62b9SNate Lawson 1648a7a3177fSJung-uk Kim return (0); 164910ce62b9SNate Lawson } 165010ce62b9SNate Lawson 1651df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1652df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1653df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1654df8d2a32SNate Lawson void *arg; 1655df8d2a32SNate Lawson ACPI_HANDLE parent; 1656df8d2a32SNate Lawson }; 1657df8d2a32SNate Lawson 1658ce619b2eSNate Lawson static ACPI_STATUS 1659df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1660df8d2a32SNate Lawson { 1661df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1662df8d2a32SNate Lawson device_t dev, old_dev; 1663df8d2a32SNate Lawson ACPI_STATUS status; 1664df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1665df8d2a32SNate Lawson 1666df8d2a32SNate Lawson /* 1667df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1668df8d2a32SNate Lawson * the parent where the scan began. 1669df8d2a32SNate Lawson */ 1670df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1671df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1672df8d2a32SNate Lawson return (AE_OK); 1673df8d2a32SNate Lawson 1674df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1675df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1676df8d2a32SNate Lawson return (AE_OK); 1677df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1678df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1679df8d2a32SNate Lawson return (AE_OK); 1680df8d2a32SNate Lawson 1681df8d2a32SNate Lawson /* 1682df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1683df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1684df8d2a32SNate Lawson */ 1685df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1686df8d2a32SNate Lawson dev = old_dev; 1687df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1688df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1689df8d2a32SNate Lawson return (status); 1690df8d2a32SNate Lawson 1691df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1692df8d2a32SNate Lawson if (old_dev != NULL) { 1693df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1694df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1695df8d2a32SNate Lawson } 1696df8d2a32SNate Lawson 1697df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1698df8d2a32SNate Lawson if (dev != NULL) 1699df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1700df8d2a32SNate Lawson 1701df8d2a32SNate Lawson return (AE_OK); 1702df8d2a32SNate Lawson } 1703df8d2a32SNate Lawson 1704df8d2a32SNate Lawson static ACPI_STATUS 1705df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1706df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1707ce619b2eSNate Lawson { 1708ce619b2eSNate Lawson ACPI_HANDLE h; 1709df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1710ce619b2eSNate Lawson 1711df8d2a32SNate Lawson if (acpi_disabled("children")) 1712df8d2a32SNate Lawson return (AE_OK); 1713df8d2a32SNate Lawson 1714df8d2a32SNate Lawson if (dev == NULL) 1715df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1716df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1717ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1718df8d2a32SNate Lawson ctx.user_fn = user_fn; 1719df8d2a32SNate Lawson ctx.arg = arg; 1720df8d2a32SNate Lawson ctx.parent = h; 1721df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 17222272d050SJung-uk Kim acpi_device_scan_cb, NULL, &ctx, NULL)); 1723ce619b2eSNate Lawson } 1724ce619b2eSNate Lawson 172510ce62b9SNate Lawson /* 172610ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting 172710ce62b9SNate Lawson * device power states since it's close enough to ACPI. 172810ce62b9SNate Lawson */ 172910ce62b9SNate Lawson static int 1730a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state) 173110ce62b9SNate Lawson { 173210ce62b9SNate Lawson ACPI_HANDLE h; 173310ce62b9SNate Lawson ACPI_STATUS status; 173410ce62b9SNate Lawson 173510ce62b9SNate Lawson h = acpi_get_handle(child); 1736a0e73a12SJung-uk Kim if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 173710ce62b9SNate Lawson return (EINVAL); 173810ce62b9SNate Lawson if (h == NULL) 173910ce62b9SNate Lawson return (0); 174010ce62b9SNate Lawson 174110ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */ 174210ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state); 1743a7a3177fSJung-uk Kim if (ACPI_SUCCESS(status)) { 1744a7a3177fSJung-uk Kim if (bootverbose) 1745a7a3177fSJung-uk Kim device_printf(child, "set ACPI power state D%d on %s\n", 1746a7a3177fSJung-uk Kim state, acpi_name(h)); 1747a7a3177fSJung-uk Kim } else if (status != AE_NOT_FOUND) 1748a7a3177fSJung-uk Kim device_printf(child, 1749a7a3177fSJung-uk Kim "failed to set ACPI power state D%d on %s: %s\n", state, 1750a7a3177fSJung-uk Kim acpi_name(h), AcpiFormatException(status)); 175110ce62b9SNate Lawson 1752a7a3177fSJung-uk Kim return (0); 175310ce62b9SNate Lawson } 175410ce62b9SNate Lawson 175532d18aa5SMike Smith static int 175632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 175732d18aa5SMike Smith { 17581e4925e8SNate Lawson int result, cid_count, i; 17591e4925e8SNate Lawson uint32_t lid, cids[8]; 1760bc0f2195SMike Smith 1761b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1762bc0f2195SMike Smith 1763bc0f2195SMike Smith /* 1764bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1765bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1766bc0f2195SMike Smith * that to happen, so don't ever return it. 1767bc0f2195SMike Smith */ 1768bc0f2195SMike Smith result = ENXIO; 1769bc0f2195SMike Smith 1770be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1771b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 17721e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1773bc0f2195SMike Smith while (ids && ids->ip_id) { 17741e4925e8SNate Lawson if (lid == ids->ip_id) { 1775bc0f2195SMike Smith result = 0; 1776bc0f2195SMike Smith goto out; 1777bc0f2195SMike Smith } 17781e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 17791e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 17801e4925e8SNate Lawson result = 0; 17811e4925e8SNate Lawson goto out; 17821e4925e8SNate Lawson } 17831e4925e8SNate Lawson } 1784bc0f2195SMike Smith ids++; 1785bc0f2195SMike Smith } 1786be2b1797SNate Lawson 1787bc0f2195SMike Smith out: 17889e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 17899e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 17909e0dd54fSNate Lawson 1791bc0f2195SMike Smith return_VALUE (result); 1792bc0f2195SMike Smith } 1793bc0f2195SMike Smith 1794d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1795d320e05cSJohn Baldwin /* 1796d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for 1797d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory 1798d320e05cSJohn Baldwin * map. 1799d320e05cSJohn Baldwin */ 1800d320e05cSJohn Baldwin static void 1801d320e05cSJohn Baldwin acpi_enable_pcie(void) 1802d320e05cSJohn Baldwin { 1803d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr; 1804d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end; 1805d320e05cSJohn Baldwin ACPI_STATUS status; 1806d320e05cSJohn Baldwin 1807d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1808d320e05cSJohn Baldwin if (ACPI_FAILURE(status)) 1809d320e05cSJohn Baldwin return; 1810d320e05cSJohn Baldwin 1811d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1812d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1813d320e05cSJohn Baldwin while (alloc < end) { 1814d320e05cSJohn Baldwin if (alloc->PciSegment == 0) { 1815d320e05cSJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1816d320e05cSJohn Baldwin alloc->EndBusNumber); 1817d320e05cSJohn Baldwin return; 1818d320e05cSJohn Baldwin } 1819d320e05cSJohn Baldwin alloc++; 1820d320e05cSJohn Baldwin } 1821d320e05cSJohn Baldwin } 1822d320e05cSJohn Baldwin #endif 1823d320e05cSJohn Baldwin 1824bc0f2195SMike Smith /* 182533332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices. 182615e32d5dSMike Smith * 182733332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 182833332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 182933332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link 183033332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the 183133332dc2SNate Lawson * type of namespace nodes, so this should be ok. 183215e32d5dSMike Smith */ 183315e32d5dSMike Smith static void 183415e32d5dSMike Smith acpi_probe_children(device_t bus) 183515e32d5dSMike Smith { 183615e32d5dSMike Smith 1837b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 18380ae55423SMike Smith 183915e32d5dSMike Smith /* 184015e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1841edc13633SNate Lawson * we find. We also probe/attach any early devices. 184215e32d5dSMike Smith * 184315e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1844be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1845be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1846be2b1797SNate Lawson * devices as they appear, which might be smarter.) 184715e32d5dSMike Smith */ 18484c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 184933332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 18502272d050SJung-uk Kim NULL, bus, NULL); 185115e32d5dSMike Smith 1852adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1853adad4744SNate Lawson acpi_sysres_alloc(bus); 1854adad4744SNate Lawson 1855ea233199SJohn Baldwin /* Reserve resources already allocated to children. */ 1856ea233199SJohn Baldwin acpi_reserve_resources(bus); 1857ea233199SJohn Baldwin 1858edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1859edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1860edc13633SNate Lawson bus_generic_probe(bus); 1861edc13633SNate Lawson 18625e66b8e2SJohn Baldwin /* Probe/attach all children, created statically and from the namespace. */ 18630430ba9eSAndriy Gapon ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 186415e32d5dSMike Smith bus_generic_attach(bus); 18650ae55423SMike Smith 186688a79fc0SNate Lawson /* Attach wake sysctls. */ 18675c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 186888a79fc0SNate Lawson 1869bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 18700ae55423SMike Smith return_VOID; 187115e32d5dSMike Smith } 187215e32d5dSMike Smith 1873b82ca9a9SNate Lawson /* 1874d227204dSJohn Baldwin * Determine the probe order for a given device. 1875b82ca9a9SNate Lawson */ 1876d227204dSJohn Baldwin static void 1877b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 187891233413SNate Lawson { 1879463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type; 188091233413SNate Lawson 1881b82ca9a9SNate Lawson /* 1882404b0d10SJung-uk Kim * 0. CPUs 1883404b0d10SJung-uk Kim * 1. I/O port and memory system resource holders 1884404b0d10SJung-uk Kim * 2. Clocks and timers (to handle early accesses) 1885b1f9b996SAndriy Gapon * 3. Embedded controllers (to handle early accesses) 1886b1f9b996SAndriy Gapon * 4. PCI Link Devices 1887b82ca9a9SNate Lawson */ 1888463e0f91SJohn Baldwin AcpiGetType(handle, &type); 1889aa835160SAndriy Gapon if (type == ACPI_TYPE_PROCESSOR) 1890404b0d10SJung-uk Kim *order = 0; 1891404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0C01") || 1892404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0C02")) 189391233413SNate Lawson *order = 1; 1894404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0100") || 1895404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0103") || 1896404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0B00")) 189791233413SNate Lawson *order = 2; 1898aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C09")) 1899b460c6f8SJohn Baldwin *order = 3; 1900aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C0F")) 1901aa835160SAndriy Gapon *order = 4; 190291233413SNate Lawson } 190391233413SNate Lawson 190415e32d5dSMike Smith /* 190515e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 190615e32d5dSMike Smith * it. 190715e32d5dSMike Smith */ 190815e32d5dSMike Smith static ACPI_STATUS 190915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 191015e32d5dSMike Smith { 19115a77b11bSJung-uk Kim struct acpi_prw_data prw; 191215e32d5dSMike Smith ACPI_OBJECT_TYPE type; 1913858a52f4SMitsuru IWASAKI ACPI_HANDLE h; 191433332dc2SNate Lawson device_t bus, child; 1915495a4144SJung-uk Kim char *handle_str; 1916e4cd9dcfSJohn Baldwin int order; 191715e32d5dSMike Smith 1918b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19190ae55423SMike Smith 1920495a4144SJung-uk Kim if (acpi_disabled("children")) 1921495a4144SJung-uk Kim return_ACPI_STATUS (AE_OK); 1922495a4144SJung-uk Kim 1923be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 19240ae55423SMike Smith if (acpi_avoid(handle)) 19250ae55423SMike Smith return_ACPI_STATUS (AE_OK); 19260ae55423SMike Smith 192791233413SNate Lawson bus = (device_t)context; 1928b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1929495a4144SJung-uk Kim handle_str = acpi_name(handle); 193015e32d5dSMike Smith switch (type) { 193115e32d5dSMike Smith case ACPI_TYPE_DEVICE: 1932495a4144SJung-uk Kim /* 1933495a4144SJung-uk Kim * Since we scan from \, be sure to skip system scope objects. 1934495a4144SJung-uk Kim * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around 1935affa1826SJung-uk Kim * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run 1936495a4144SJung-uk Kim * during the intialization and \_TZ_ is to support Notify() on it. 1937495a4144SJung-uk Kim */ 1938495a4144SJung-uk Kim if (strcmp(handle_str, "\\_SB_") == 0 || 1939495a4144SJung-uk Kim strcmp(handle_str, "\\_TZ_") == 0) 1940495a4144SJung-uk Kim break; 19415a77b11bSJung-uk Kim if (acpi_parse_prw(handle, &prw) == 0) 19425a77b11bSJung-uk Kim AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit); 1943183c8af3SJohn Baldwin 1944183c8af3SJohn Baldwin /* 1945183c8af3SJohn Baldwin * Ignore devices that do not have a _HID or _CID. They should 1946183c8af3SJohn Baldwin * be discovered by other buses (e.g. the PCI bus driver). 1947183c8af3SJohn Baldwin */ 1948183c8af3SJohn Baldwin if (!acpi_has_hid(handle)) 1949183c8af3SJohn Baldwin break; 1950495a4144SJung-uk Kim /* FALLTHROUGH */ 195115e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 195215e32d5dSMike Smith case ACPI_TYPE_THERMAL: 195315e32d5dSMike Smith case ACPI_TYPE_POWER: 195433332dc2SNate Lawson /* 1955463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the 1956463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run 1957463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1958463e0f91SJohn Baldwin * are reserved for special objects (i.e., system 1959b1f9b996SAndriy Gapon * resources). 196015e32d5dSMike Smith */ 196133332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1962404b0d10SJung-uk Kim order = level * 10 + ACPI_DEV_BASE_ORDER; 1963da72d149SNate Lawson acpi_probe_order(handle, &order); 1964e4cd9dcfSJohn Baldwin child = BUS_ADD_CHILD(bus, order, NULL, -1); 1965bc0f2195SMike Smith if (child == NULL) 1966bc0f2195SMike Smith break; 196759a890e6SNate Lawson 196859a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 196915e32d5dSMike Smith acpi_set_handle(child, handle); 197059a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1971bc0f2195SMike Smith 1972bc0f2195SMike Smith /* 1973b519f9d6SMike Smith * Check that the device is present. If it's not present, 1974b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1975b519f9d6SMike Smith * the handle, but we don't probe it). 1976893f750aSNate Lawson * 1977893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not 1978893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them 1979893f750aSNate Lawson * anyway since we may enable them later. 1980b519f9d6SMike Smith */ 1981858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1982858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */ 1983858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F")) 1984858a52f4SMitsuru IWASAKI break; 1985858a52f4SMitsuru IWASAKI /* 1986858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system 1987858a52f4SMitsuru IWASAKI * may be undocked at boot. 1988858a52f4SMitsuru IWASAKI */ 1989858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1990858a52f4SMitsuru IWASAKI break; 1991858a52f4SMitsuru IWASAKI 1992b519f9d6SMike Smith device_disable(child); 1993b519f9d6SMike Smith break; 1994b519f9d6SMike Smith } 1995b519f9d6SMike Smith 1996b519f9d6SMike Smith /* 1997bc0f2195SMike Smith * Get the device's resource settings and attach them. 1998bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1999bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 2000bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 2001bc0f2195SMike Smith * device not to have any resources. 2002bc0f2195SMike Smith */ 200372ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 20040ae55423SMike Smith break; 200515e32d5dSMike Smith } 200615e32d5dSMike Smith } 2007be2b1797SNate Lawson 20080ae55423SMike Smith return_ACPI_STATUS (AE_OK); 200915e32d5dSMike Smith } 201015e32d5dSMike Smith 201159a890e6SNate Lawson /* 201259a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 201359a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 201459a890e6SNate Lawson */ 201559a890e6SNate Lawson void 201692488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data) 201759a890e6SNate Lawson { 201859a890e6SNate Lawson } 201959a890e6SNate Lawson 202015e32d5dSMike Smith static void 202115e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 202215e32d5dSMike Smith { 20232d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 202471804adcSJung-uk Kim register_t intr; 2025d33f4987STakanori Watanabe ACPI_STATUS status; 2026eeb3a05fSNate Lawson 2027eeb3a05fSNate Lawson /* 202880f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 202980f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 203080f0e4c2SNate Lawson * an AP. 2031eeb3a05fSNate Lawson */ 2032eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 2033904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 2034904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 20352d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2036904bf0c2SNate Lawson AcpiFormatException(status)); 2037904bf0c2SNate Lawson return; 2038904bf0c2SNate Lawson } 20392d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Powering system off\n"); 204071804adcSJung-uk Kim intr = intr_disable(); 20411df130f1SJung-uk Kim status = AcpiEnterSleepState(ACPI_STATE_S5); 204271804adcSJung-uk Kim if (ACPI_FAILURE(status)) { 204371804adcSJung-uk Kim intr_restore(intr); 20442d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - %s\n", 20452d0c82e8SJung-uk Kim AcpiFormatException(status)); 204671804adcSJung-uk Kim } else { 204715e32d5dSMike Smith DELAY(1000000); 204871804adcSJung-uk Kim intr_restore(intr); 20492d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 205015e32d5dSMike Smith } 2051ac731af5SJung-uk Kim } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) { 2052d85e6785SNate Lawson /* Reboot using the reset register. */ 2053ac731af5SJung-uk Kim status = AcpiReset(); 2054ac731af5SJung-uk Kim if (ACPI_SUCCESS(status)) { 205587a500cdSNate Lawson DELAY(1000000); 20562d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - timeout\n"); 2057ac731af5SJung-uk Kim } else if (status != AE_NOT_EXIST) 2058ac731af5SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - %s\n", 2059ac731af5SJung-uk Kim AcpiFormatException(status)); 2060d85e6785SNate Lawson } else if (sc->acpi_do_disable && panicstr == NULL) { 2061d85e6785SNate Lawson /* 2062d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing 2063d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system. 2064d85e6785SNate Lawson */ 20652d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Shutting down\n"); 206680f0e4c2SNate Lawson AcpiTerminate(); 206780f0e4c2SNate Lawson } 206815e32d5dSMike Smith } 206915e32d5dSMike Smith 207013d4f7baSMitsuru IWASAKI static void 207113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 207213d4f7baSMitsuru IWASAKI { 207313d4f7baSMitsuru IWASAKI static int first_time = 1; 207413d4f7baSMitsuru IWASAKI 207513d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 20762be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 207759ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 207813d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 207933febf93SNate Lawson acpi_event_power_button_sleep, sc); 2080be2b1797SNate Lawson if (first_time) 20816c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 208213d4f7baSMitsuru IWASAKI } 20832be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 208459ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 208513d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 208633febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 2087be2b1797SNate Lawson if (first_time) 20886c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 208913d4f7baSMitsuru IWASAKI } 209013d4f7baSMitsuru IWASAKI 209113d4f7baSMitsuru IWASAKI first_time = 0; 209213d4f7baSMitsuru IWASAKI } 209313d4f7baSMitsuru IWASAKI 209415e32d5dSMike Smith /* 2095c5ba0be4SMike Smith * Returns true if the device is actually present and should 2096c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 2097c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 2098c5ba0be4SMike Smith */ 2099c5ba0be4SMike Smith BOOLEAN 2100c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 2101c5ba0be4SMike Smith { 21021e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 2103c5ba0be4SMike Smith ACPI_HANDLE h; 210492488a57SJung-uk Kim BOOLEAN present; 2105c5ba0be4SMike Smith 210692488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 210792488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2108c5ba0be4SMike Smith return (FALSE); 2109be2b1797SNate Lawson 2110be2b1797SNate Lawson /* If no _STA method, must be present */ 211192488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 211292488a57SJung-uk Kim ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2113be2b1797SNate Lawson 211492488a57SJung-uk Kim AcpiOsFree(devinfo); 211592488a57SJung-uk Kim return (present); 2116c5ba0be4SMike Smith } 2117c5ba0be4SMike Smith 2118c5ba0be4SMike Smith /* 2119b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 2120b53f2771SMike Smith */ 2121b53f2771SMike Smith BOOLEAN 2122b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 2123b53f2771SMike Smith { 21241e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 2125b53f2771SMike Smith ACPI_HANDLE h; 212692488a57SJung-uk Kim BOOLEAN present; 2127b53f2771SMike Smith 212892488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 212992488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2130b53f2771SMike Smith return (FALSE); 2131be2b1797SNate Lawson 2132be2b1797SNate Lawson /* If no _STA method, must be present */ 213392488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 213492488a57SJung-uk Kim ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2135be2b1797SNate Lawson 213692488a57SJung-uk Kim AcpiOsFree(devinfo); 213792488a57SJung-uk Kim return (present); 2138b53f2771SMike Smith } 2139b53f2771SMike Smith 2140b53f2771SMike Smith /* 2141183c8af3SJohn Baldwin * Returns true if a device has at least one valid device ID. 2142183c8af3SJohn Baldwin */ 2143183c8af3SJohn Baldwin static BOOLEAN 2144183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h) 2145183c8af3SJohn Baldwin { 2146183c8af3SJohn Baldwin ACPI_DEVICE_INFO *devinfo; 2147183c8af3SJohn Baldwin BOOLEAN ret; 2148183c8af3SJohn Baldwin 2149183c8af3SJohn Baldwin if (h == NULL || 2150183c8af3SJohn Baldwin ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2151183c8af3SJohn Baldwin return (FALSE); 2152183c8af3SJohn Baldwin 2153183c8af3SJohn Baldwin ret = FALSE; 2154183c8af3SJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0) 2155183c8af3SJohn Baldwin ret = TRUE; 2156183c8af3SJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2157183c8af3SJohn Baldwin if (devinfo->CompatibleIdList.Count > 0) 2158183c8af3SJohn Baldwin ret = TRUE; 2159183c8af3SJohn Baldwin 2160183c8af3SJohn Baldwin AcpiOsFree(devinfo); 2161183c8af3SJohn Baldwin return (ret); 2162183c8af3SJohn Baldwin } 2163183c8af3SJohn Baldwin 2164183c8af3SJohn Baldwin /* 216591233413SNate Lawson * Match a HID string against a handle 216615e32d5dSMike Smith */ 21673c4c08dcSAlexander Motin BOOLEAN 2168ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 216915e32d5dSMike Smith { 21701e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 217192488a57SJung-uk Kim BOOLEAN ret; 217292488a57SJung-uk Kim int i; 217392488a57SJung-uk Kim 217492488a57SJung-uk Kim if (hid == NULL || h == NULL || 217592488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 217692488a57SJung-uk Kim return (FALSE); 217715e32d5dSMike Smith 21781e4925e8SNate Lawson ret = FALSE; 2179c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 218092488a57SJung-uk Kim strcmp(hid, devinfo->HardwareId.String) == 0) 21811e4925e8SNate Lawson ret = TRUE; 218292488a57SJung-uk Kim else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 218392488a57SJung-uk Kim for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 218492488a57SJung-uk Kim if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 21851e4925e8SNate Lawson ret = TRUE; 21861e4925e8SNate Lawson break; 21871e4925e8SNate Lawson } 21881e4925e8SNate Lawson } 2189be2b1797SNate Lawson 219092488a57SJung-uk Kim AcpiOsFree(devinfo); 21911e4925e8SNate Lawson return (ret); 219215e32d5dSMike Smith } 219315e32d5dSMike Smith 219415e32d5dSMike Smith /* 2195c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 2196c5ba0be4SMike Smith * or one if its parents. 2197c5ba0be4SMike Smith */ 2198c5ba0be4SMike Smith ACPI_STATUS 2199c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 2200c5ba0be4SMike Smith { 2201c5ba0be4SMike Smith ACPI_HANDLE r; 2202c5ba0be4SMike Smith ACPI_STATUS status; 2203c5ba0be4SMike Smith 2204be2b1797SNate Lawson /* Walk back up the tree to the root */ 2205c5ba0be4SMike Smith for (;;) { 2206be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 2207be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2208c5ba0be4SMike Smith *result = r; 2209c5ba0be4SMike Smith return (AE_OK); 2210c5ba0be4SMike Smith } 2211bee4aa4aSNate Lawson /* XXX Return error here? */ 2212c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 2213c5ba0be4SMike Smith return (AE_OK); 2214b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 2215c5ba0be4SMike Smith return (AE_NOT_FOUND); 2216c5ba0be4SMike Smith parent = r; 2217c5ba0be4SMike Smith } 2218c5ba0be4SMike Smith } 2219c5ba0be4SMike Smith 2220c5ba0be4SMike Smith /* 2221c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 2222c5ba0be4SMike Smith */ 2223c5ba0be4SMike Smith ACPI_BUFFER * 2224c5ba0be4SMike Smith acpi_AllocBuffer(int size) 2225c5ba0be4SMike Smith { 2226c5ba0be4SMike Smith ACPI_BUFFER *buf; 2227c5ba0be4SMike Smith 2228c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2229c5ba0be4SMike Smith return (NULL); 2230c5ba0be4SMike Smith buf->Length = size; 2231c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 2232c5ba0be4SMike Smith return (buf); 2233c5ba0be4SMike Smith } 2234c5ba0be4SMike Smith 2235c310653eSNate Lawson ACPI_STATUS 2236cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2237c310653eSNate Lawson { 2238c310653eSNate Lawson ACPI_OBJECT arg1; 2239c310653eSNate Lawson ACPI_OBJECT_LIST args; 2240c310653eSNate Lawson 2241c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 2242c310653eSNate Lawson arg1.Integer.Value = number; 2243c310653eSNate Lawson args.Count = 1; 2244c310653eSNate Lawson args.Pointer = &arg1; 2245c310653eSNate Lawson 2246c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 2247c310653eSNate Lawson } 2248c310653eSNate Lawson 2249c5ba0be4SMike Smith /* 2250c5ba0be4SMike Smith * Evaluate a path that should return an integer. 2251c5ba0be4SMike Smith */ 2252c5ba0be4SMike Smith ACPI_STATUS 2253cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2254c5ba0be4SMike Smith { 2255be2b1797SNate Lawson ACPI_STATUS status; 2256c5ba0be4SMike Smith ACPI_BUFFER buf; 2257c573e654SMitsuru IWASAKI ACPI_OBJECT param; 2258c5ba0be4SMike Smith 2259c5ba0be4SMike Smith if (handle == NULL) 2260c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 22610c7da7acSJohn Baldwin 22620c7da7acSJohn Baldwin /* 22630c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 22640c7da7acSJohn Baldwin * a method that will return an Integer. 22650c7da7acSJohn Baldwin */ 2266c5ba0be4SMike Smith buf.Pointer = ¶m; 2267c5ba0be4SMike Smith buf.Length = sizeof(param); 2268be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2269be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2270be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 2271c5ba0be4SMike Smith *number = param.Integer.Value; 2272be2b1797SNate Lawson else 2273be2b1797SNate Lawson status = AE_TYPE; 2274c5ba0be4SMike Smith } 22750c7da7acSJohn Baldwin 22760c7da7acSJohn Baldwin /* 22770c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 22780c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 22790c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 22800c7da7acSJohn Baldwin * convert it into an Integer as best we can. 22810c7da7acSJohn Baldwin * 22820c7da7acSJohn Baldwin * This is a hack. 22830c7da7acSJohn Baldwin */ 2284be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 2285b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2286be2b1797SNate Lawson status = AE_NO_MEMORY; 22870c7da7acSJohn Baldwin } else { 2288be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2289be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 2290be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 22910c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 22920c7da7acSJohn Baldwin } 2293f97739daSNate Lawson } 2294be2b1797SNate Lawson return (status); 2295c5ba0be4SMike Smith } 2296c5ba0be4SMike Smith 2297c573e654SMitsuru IWASAKI ACPI_STATUS 2298cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2299c573e654SMitsuru IWASAKI { 2300c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 2301dba55fa2SNate Lawson UINT8 *val; 2302c573e654SMitsuru IWASAKI int i; 2303c573e654SMitsuru IWASAKI 2304c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 2305c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 2306c573e654SMitsuru IWASAKI *number = p->Integer.Value; 2307c573e654SMitsuru IWASAKI return (AE_OK); 2308c573e654SMitsuru IWASAKI } 2309c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 2310c573e654SMitsuru IWASAKI return (AE_TYPE); 2311c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 2312c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 2313be2b1797SNate Lawson 2314c573e654SMitsuru IWASAKI *number = 0; 2315dba55fa2SNate Lawson val = p->Buffer.Pointer; 2316c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 2317dba55fa2SNate Lawson *number += val[i] << (i * 8); 2318c573e654SMitsuru IWASAKI return (AE_OK); 2319c573e654SMitsuru IWASAKI } 2320c573e654SMitsuru IWASAKI 2321c5ba0be4SMike Smith /* 2322c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 2323c5ba0be4SMike Smith * function for each element. 2324c5ba0be4SMike Smith * 2325c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 2326c5ba0be4SMike Smith */ 2327c5ba0be4SMike Smith ACPI_STATUS 2328be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2329be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2330c5ba0be4SMike Smith { 2331c5ba0be4SMike Smith ACPI_OBJECT *comp; 2332c5ba0be4SMike Smith int i; 2333c5ba0be4SMike Smith 2334be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2335c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 2336c5ba0be4SMike Smith 2337be2b1797SNate Lawson /* Iterate over components */ 2338be2b1797SNate Lawson i = 0; 2339be2b1797SNate Lawson comp = pkg->Package.Elements; 2340be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 2341c5ba0be4SMike Smith func(comp, arg); 2342c5ba0be4SMike Smith 2343c5ba0be4SMike Smith return (AE_OK); 2344c5ba0be4SMike Smith } 2345c5ba0be4SMike Smith 23466f69255bSMike Smith /* 23476f69255bSMike Smith * Find the (index)th resource object in a set. 23486f69255bSMike Smith */ 23496f69255bSMike Smith ACPI_STATUS 23506d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 23516f69255bSMike Smith { 23526d63101aSMike Smith ACPI_RESOURCE *rp; 23536f69255bSMike Smith int i; 23546f69255bSMike Smith 23556d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 23566f69255bSMike Smith i = index; 23576d63101aSMike Smith while (i-- > 0) { 2358be2b1797SNate Lawson /* Range check */ 23596d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 23606f69255bSMike Smith return (AE_BAD_PARAMETER); 2361be2b1797SNate Lawson 2362be2b1797SNate Lawson /* Check for terminator */ 2363e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 23646d63101aSMike Smith return (AE_NOT_FOUND); 2365abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 23666f69255bSMike Smith } 23676f69255bSMike Smith if (resp != NULL) 23686d63101aSMike Smith *resp = rp; 2369be2b1797SNate Lawson 23706d63101aSMike Smith return (AE_OK); 23716d63101aSMike Smith } 23726d63101aSMike Smith 23736d63101aSMike Smith /* 23746d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 23756d63101aSMike Smith * 23766d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 23776d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 23786d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 23796d63101aSMike Smith * resources. 23806d63101aSMike Smith */ 23816d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 23826d63101aSMike Smith 23836d63101aSMike Smith ACPI_STATUS 23846d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 23856d63101aSMike Smith { 23866d63101aSMike Smith ACPI_RESOURCE *rp; 23876d63101aSMike Smith void *newp; 23886d63101aSMike Smith 2389be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 23906d63101aSMike Smith if (buf->Pointer == NULL) { 23916d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 23926d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 23936d63101aSMike Smith return (AE_NO_MEMORY); 23946d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 2395e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 239660d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN; 23976d63101aSMike Smith } 23986d63101aSMike Smith if (res == NULL) 23996d63101aSMike Smith return (AE_OK); 24006d63101aSMike Smith 24016d63101aSMike Smith /* 24026d63101aSMike Smith * Scan the current buffer looking for the terminator. 24036d63101aSMike Smith * This will either find the terminator or hit the end 24046d63101aSMike Smith * of the buffer and return an error. 24056d63101aSMike Smith */ 24066d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 24076d63101aSMike Smith for (;;) { 2408be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 24096d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 24106d63101aSMike Smith return (AE_BAD_PARAMETER); 2411e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 24126d63101aSMike Smith break; 2413abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 24146d63101aSMike Smith } 24156d63101aSMike Smith 24166d63101aSMike Smith /* 24176d63101aSMike Smith * Check the size of the buffer and expand if required. 24186d63101aSMike Smith * 24196d63101aSMike Smith * Required size is: 24206d63101aSMike Smith * size of existing resources before terminator + 24216d63101aSMike Smith * size of new resource and header + 24226d63101aSMike Smith * size of terminator. 24236d63101aSMike Smith * 24246d63101aSMike Smith * Note that this loop should really only run once, unless 24256d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 24266d63101aSMike Smith */ 24276d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2428e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA + 2429e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) { 24306d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 24316d63101aSMike Smith return (AE_NO_MEMORY); 24326d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 2433a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2434a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 24356d63101aSMike Smith AcpiOsFree(buf->Pointer); 24366d63101aSMike Smith buf->Pointer = newp; 24376d63101aSMike Smith buf->Length += buf->Length; 24386d63101aSMike Smith } 24396d63101aSMike Smith 2440be2b1797SNate Lawson /* Insert the new resource. */ 2441e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 24426d63101aSMike Smith 2443be2b1797SNate Lawson /* And add the terminator. */ 2444abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 2445e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 244660d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN; 24476d63101aSMike Smith 24486f69255bSMike Smith return (AE_OK); 24496f69255bSMike Smith } 2450c5ba0be4SMike Smith 2451da14ac9fSJohn Baldwin /* 2452da14ac9fSJohn Baldwin * Set interrupt model. 2453da14ac9fSJohn Baldwin */ 2454da14ac9fSJohn Baldwin ACPI_STATUS 2455da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 2456da14ac9fSJohn Baldwin { 2457da14ac9fSJohn Baldwin 2458c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2459da14ac9fSJohn Baldwin } 2460da14ac9fSJohn Baldwin 246100a30448SNate Lawson /* 2462d95e7f5aSJohn Baldwin * Walk subtables of a table and call a callback routine for each 2463d95e7f5aSJohn Baldwin * subtable. The caller should provide the first subtable and a 2464d95e7f5aSJohn Baldwin * pointer to the end of the table. This can be used to walk tables 2465d95e7f5aSJohn Baldwin * such as MADT and SRAT that use subtable entries. 2466d95e7f5aSJohn Baldwin */ 2467d95e7f5aSJohn Baldwin void 2468d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2469d95e7f5aSJohn Baldwin void *arg) 2470d95e7f5aSJohn Baldwin { 2471d95e7f5aSJohn Baldwin ACPI_SUBTABLE_HEADER *entry; 2472d95e7f5aSJohn Baldwin 2473d95e7f5aSJohn Baldwin for (entry = first; (void *)entry < end; ) { 2474d95e7f5aSJohn Baldwin /* Avoid an infinite loop if we hit a bogus entry. */ 2475d95e7f5aSJohn Baldwin if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2476d95e7f5aSJohn Baldwin return; 2477d95e7f5aSJohn Baldwin 2478d95e7f5aSJohn Baldwin handler(entry, arg); 2479d95e7f5aSJohn Baldwin entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2480d95e7f5aSJohn Baldwin } 2481d95e7f5aSJohn Baldwin } 2482d95e7f5aSJohn Baldwin 2483d95e7f5aSJohn Baldwin /* 248400a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be 248500a30448SNate Lawson * removed. 248600a30448SNate Lawson * 248700a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran 248800a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 248900a30448SNate Lawson */ 249000a30448SNate Lawson ACPI_STATUS 249100a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state) 249200a30448SNate Lawson { 249300a30448SNate Lawson static int once; 249400a30448SNate Lawson 249500a30448SNate Lawson if (!once) { 24962d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 249700a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n"); 249800a30448SNate Lawson once = 1; 249900a30448SNate Lawson } 250000a30448SNate Lawson return (acpi_EnterSleepState(sc, state)); 250100a30448SNate Lawson } 250200a30448SNate Lawson 2503c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 250400a30448SNate Lawson static void 2505ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context) 2506ffe3f1f8SMitsuru IWASAKI { 2507ffe3f1f8SMitsuru IWASAKI struct acpi_softc *sc = (struct acpi_softc *)context; 2508ffe3f1f8SMitsuru IWASAKI 2509ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2510ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 2511ffe3f1f8SMitsuru IWASAKI sc->acpi_next_sstate); 2512ffe3f1f8SMitsuru IWASAKI } 2513ffe3f1f8SMitsuru IWASAKI 2514ffe3f1f8SMitsuru IWASAKI static void 251500a30448SNate Lawson acpi_sleep_force(void *arg) 251600a30448SNate Lawson { 25172d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 251800a30448SNate Lawson 25192d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 25202d0c82e8SJung-uk Kim "suspend request timed out, forcing sleep now\n"); 2521ffe3f1f8SMitsuru IWASAKI /* 2522fadf3fb9SJohn Baldwin * XXX Suspending from callout causes freezes in DEVICE_SUSPEND(). 2523ffe3f1f8SMitsuru IWASAKI * Suspend from acpi_task thread instead. 2524ffe3f1f8SMitsuru IWASAKI */ 2525ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 2526ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task, sc))) 2527ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n"); 252800a30448SNate Lawson } 2529c66d2b38SJung-uk Kim #endif 253000a30448SNate Lawson 253100a30448SNate Lawson /* 253200a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm 253300a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to 253400a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all 253500a30448SNate Lawson * acks are in. 253600a30448SNate Lawson */ 253700a30448SNate Lawson int 253800a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state) 253900a30448SNate Lawson { 254071f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 254100a30448SNate Lawson struct apm_clone_data *clone; 2542337744d9SJung-uk Kim ACPI_STATUS status; 254300a30448SNate Lawson 2544a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 254500a30448SNate Lawson return (EINVAL); 2546a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 2547a0e73a12SJung-uk Kim return (EOPNOTSUPP); 254800a30448SNate Lawson 2549b12bc99fSMitsuru IWASAKI /* If a suspend request is already in progress, just return. */ 2550b12bc99fSMitsuru IWASAKI if (sc->acpi_next_sstate != 0) { 2551b12bc99fSMitsuru IWASAKI return (0); 2552b12bc99fSMitsuru IWASAKI } 2553b12bc99fSMitsuru IWASAKI 2554f22377a8SMitsuru IWASAKI /* Wait until sleep is enabled. */ 2555f22377a8SMitsuru IWASAKI while (sc->acpi_sleep_disabled) { 2556f22377a8SMitsuru IWASAKI AcpiOsSleep(1000); 2557f22377a8SMitsuru IWASAKI } 2558f22377a8SMitsuru IWASAKI 2559337744d9SJung-uk Kim ACPI_LOCK(acpi); 256000a30448SNate Lawson 2561f22377a8SMitsuru IWASAKI sc->acpi_next_sstate = state; 256200a30448SNate Lawson 2563337744d9SJung-uk Kim /* S5 (soft-off) should be entered directly with no waiting. */ 2564337744d9SJung-uk Kim if (state == ACPI_STATE_S5) { 2565337744d9SJung-uk Kim ACPI_UNLOCK(acpi); 2566337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2567337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2568337744d9SJung-uk Kim } 2569337744d9SJung-uk Kim 257000a30448SNate Lawson /* Record the pending state and notify all apm devices. */ 257100a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 257200a30448SNate Lawson clone->notify_status = APM_EV_NONE; 257300a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) { 257400a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO); 2575374d9c4dSJohn Baldwin KNOTE_LOCKED(&clone->sel_read.si_note, 0); 257600a30448SNate Lawson } 257700a30448SNate Lawson } 257800a30448SNate Lawson 25790c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */ 2580d42f0ad8SJung-uk Kim if (!devctl_process_running()) { 25810c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi); 2582337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2583337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 25840c26519eSMitsuru IWASAKI } 25850c26519eSMitsuru IWASAKI 258600a30448SNate Lawson /* 258700a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request 258800a30448SNate Lawson * in time. This way we still eventually go to sleep if we were 258900a30448SNate Lawson * overheating or running low on battery, even if userland is hung. 259000a30448SNate Lawson * We cancel this timeout once all userland acks are in or the 259100a30448SNate Lawson * suspend request is aborted. 259200a30448SNate Lawson */ 259300a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 259400a30448SNate Lawson ACPI_UNLOCK(acpi); 2595d42f0ad8SJung-uk Kim 2596d42f0ad8SJung-uk Kim /* Now notify devd(8) also. */ 2597d42f0ad8SJung-uk Kim acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2598d42f0ad8SJung-uk Kim 259900a30448SNate Lawson return (0); 2600c66d2b38SJung-uk Kim #else 2601c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2602c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2603c66d2b38SJung-uk Kim #endif 260400a30448SNate Lawson } 260500a30448SNate Lawson 260600a30448SNate Lawson /* 260700a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has 260800a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the 260900a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled 261000a30448SNate Lawson * and gives an errno value describing why. Once all votes are in, 261100a30448SNate Lawson * we suspend the system. 261200a30448SNate Lawson */ 261300a30448SNate Lawson int 261400a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error) 261500a30448SNate Lawson { 2616c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 261700a30448SNate Lawson struct acpi_softc *sc; 261800a30448SNate Lawson int ret, sleeping; 261900a30448SNate Lawson 262000a30448SNate Lawson /* If no pending sleep state, return an error. */ 262100a30448SNate Lawson ACPI_LOCK(acpi); 262200a30448SNate Lawson sc = clone->acpi_sc; 262300a30448SNate Lawson if (sc->acpi_next_sstate == 0) { 262400a30448SNate Lawson ACPI_UNLOCK(acpi); 262500a30448SNate Lawson return (ENXIO); 262600a30448SNate Lawson } 262700a30448SNate Lawson 262800a30448SNate Lawson /* Caller wants to abort suspend process. */ 262900a30448SNate Lawson if (error) { 263000a30448SNate Lawson sc->acpi_next_sstate = 0; 263100a30448SNate Lawson callout_stop(&sc->susp_force_to); 26322d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 26332d0c82e8SJung-uk Kim "listener on %s cancelled the pending suspend\n", 263400a30448SNate Lawson devtoname(clone->cdev)); 263500a30448SNate Lawson ACPI_UNLOCK(acpi); 263600a30448SNate Lawson return (0); 263700a30448SNate Lawson } 263800a30448SNate Lawson 263900a30448SNate Lawson /* 264000a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through 264100a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that 264200a30448SNate Lawson * are writable since read-only devices couldn't ack the request. 264300a30448SNate Lawson */ 264400a30448SNate Lawson sleeping = TRUE; 2645c66d2b38SJung-uk Kim clone->notify_status = APM_EV_ACKED; 264600a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 264700a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 && 264800a30448SNate Lawson clone->notify_status != APM_EV_ACKED) { 264900a30448SNate Lawson sleeping = FALSE; 265000a30448SNate Lawson break; 265100a30448SNate Lawson } 265200a30448SNate Lawson } 265300a30448SNate Lawson 265400a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */ 265500a30448SNate Lawson if (sleeping) 265600a30448SNate Lawson callout_stop(&sc->susp_force_to); 265700a30448SNate Lawson ACPI_UNLOCK(acpi); 265800a30448SNate Lawson ret = 0; 265900a30448SNate Lawson if (sleeping) { 266000a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 266100a30448SNate Lawson ret = ENODEV; 266200a30448SNate Lawson } 266300a30448SNate Lawson return (ret); 2664c66d2b38SJung-uk Kim #else 2665c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2666c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2667c66d2b38SJung-uk Kim #endif 266800a30448SNate Lawson } 266900a30448SNate Lawson 2670ece50487SMitsuru IWASAKI static void 2671ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 2672ece50487SMitsuru IWASAKI { 2673d42f0ad8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 2674bee4aa4aSNate Lawson 2675fadf3fb9SJohn Baldwin ACPI_LOCK_ASSERT(acpi); 2676fadf3fb9SJohn Baldwin 2677a0e73a12SJung-uk Kim /* Reschedule if the system is not fully up and running. */ 2678a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) { 2679fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME); 2680a0e73a12SJung-uk Kim return; 2681a0e73a12SJung-uk Kim } 2682a0e73a12SJung-uk Kim 2683a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = FALSE; 2684d42f0ad8SJung-uk Kim } 2685d42f0ad8SJung-uk Kim 2686d42f0ad8SJung-uk Kim static ACPI_STATUS 2687d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc) 2688d42f0ad8SJung-uk Kim { 2689d42f0ad8SJung-uk Kim ACPI_STATUS status; 2690d42f0ad8SJung-uk Kim 2691a0e73a12SJung-uk Kim /* Fail if the system is not fully up and running. */ 2692a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) 2693a0e73a12SJung-uk Kim return (AE_ERROR); 2694a0e73a12SJung-uk Kim 2695d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2696d42f0ad8SJung-uk Kim status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2697a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 2698d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2699d42f0ad8SJung-uk Kim 2700d42f0ad8SJung-uk Kim return (status); 2701ece50487SMitsuru IWASAKI } 2702c30382dfSMitsuru IWASAKI 270315e2f34fSNate Lawson enum acpi_sleep_state { 270415e2f34fSNate Lawson ACPI_SS_NONE, 270515e2f34fSNate Lawson ACPI_SS_GPE_SET, 270615e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 270715e2f34fSNate Lawson ACPI_SS_SLP_PREP, 270815e2f34fSNate Lawson ACPI_SS_SLEPT, 270915e2f34fSNate Lawson }; 271015e2f34fSNate Lawson 271115e32d5dSMike Smith /* 271200a30448SNate Lawson * Enter the desired system sleep state. 271315e32d5dSMike Smith * 2714be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 271515e32d5dSMike Smith */ 271600a30448SNate Lawson static ACPI_STATUS 271700a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state) 271815e32d5dSMike Smith { 271971804adcSJung-uk Kim register_t intr; 272015e2f34fSNate Lawson ACPI_STATUS status; 2721b913a7d5SAndriy Gapon ACPI_EVENT_STATUS power_button_status; 272215e2f34fSNate Lawson enum acpi_sleep_state slp_state; 2723f0a101b7SMitsuru IWASAKI int sleep_result; 272415e32d5dSMike Smith 2725b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 27260ae55423SMike Smith 2727a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 272833c70d41SAndriy Gapon return_ACPI_STATUS (AE_BAD_PARAMETER); 2729a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) { 2730a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2731a0e73a12SJung-uk Kim state); 2732a0e73a12SJung-uk Kim return (AE_SUPPORT); 2733a0e73a12SJung-uk Kim } 2734a0e73a12SJung-uk Kim 2735a0e73a12SJung-uk Kim /* Re-entry once we're suspending is not allowed. */ 2736a0e73a12SJung-uk Kim status = acpi_sleep_disable(sc); 2737a0e73a12SJung-uk Kim if (ACPI_FAILURE(status)) { 2738a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, 2739a0e73a12SJung-uk Kim "suspend request ignored (not ready yet)\n"); 2740a0e73a12SJung-uk Kim return (status); 2741a0e73a12SJung-uk Kim } 274233c70d41SAndriy Gapon 274333c70d41SAndriy Gapon if (state == ACPI_STATE_S5) { 274433c70d41SAndriy Gapon /* 274533c70d41SAndriy Gapon * Shut down cleanly and power off. This will call us back through the 274633c70d41SAndriy Gapon * shutdown handlers. 274733c70d41SAndriy Gapon */ 274833c70d41SAndriy Gapon shutdown_nice(RB_POWEROFF); 274933c70d41SAndriy Gapon return_ACPI_STATUS (AE_OK); 275033c70d41SAndriy Gapon } 275133c70d41SAndriy Gapon 27524a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_suspend); 27534a8fa6feSJung-uk Kim 275436a483bbSJung-uk Kim if (smp_started) { 2755c66d2b38SJung-uk Kim thread_lock(curthread); 2756c66d2b38SJung-uk Kim sched_bind(curthread, 0); 2757c66d2b38SJung-uk Kim thread_unlock(curthread); 275836a483bbSJung-uk Kim } 2759c66d2b38SJung-uk Kim 2760a56fe095SJohn Baldwin /* 2761a56fe095SJohn Baldwin * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2762a56fe095SJohn Baldwin * drivers need this. 2763a56fe095SJohn Baldwin */ 2764a56fe095SJohn Baldwin mtx_lock(&Giant); 2765c66d2b38SJung-uk Kim 276615e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 276756d8cb57SMitsuru IWASAKI 2768a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 2769a1fccb47SMitsuru IWASAKI 2770d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 2771d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 277215e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 2773e8b4d56eSNate Lawson 277415e32d5dSMike Smith /* 2775c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 2776c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 277715e32d5dSMike Smith * 2778c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 2779c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 2780c7f88fc3SNate Lawson * bus interface does not provide for this. 278115e32d5dSMike Smith */ 278215e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 278315e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 278433c70d41SAndriy Gapon goto backout; 278515e2f34fSNate Lawson } 278615e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 2787b9c780d6SMitsuru IWASAKI 278893bfd059SNate Lawson /* If testing device suspend only, back out of everything here. */ 278993bfd059SNate Lawson if (acpi_susp_bounce) 279033c70d41SAndriy Gapon goto backout; 279193bfd059SNate Lawson 2792be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 2793be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2794b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2795b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 279633c70d41SAndriy Gapon goto backout; 2797b9c780d6SMitsuru IWASAKI } 279815e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 2799b9c780d6SMitsuru IWASAKI 2800be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 2801ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 2802ff01efb5SMitsuru IWASAKI 2803f0a101b7SMitsuru IWASAKI intr = intr_disable(); 28044d46ef51SJung-uk Kim if (state != ACPI_STATE_S1) { 2805f0a101b7SMitsuru IWASAKI sleep_result = acpi_sleep_machdep(sc, state); 2806f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 0); 2807b1a5c017SAndriy Gapon 2808b1a5c017SAndriy Gapon /* 2809b1a5c017SAndriy Gapon * XXX According to ACPI specification SCI_EN bit should be restored 2810b1a5c017SAndriy Gapon * by ACPI platform (BIOS, firmware) to its pre-sleep state. 2811b1a5c017SAndriy Gapon * Unfortunately some BIOSes fail to do that and that leads to 2812b1a5c017SAndriy Gapon * unexpected and serious consequences during wake up like a system 2813b1a5c017SAndriy Gapon * getting stuck in SMI handlers. 2814b1a5c017SAndriy Gapon * This hack is picked up from Linux, which claims that it follows 2815b1a5c017SAndriy Gapon * Windows behavior. 2816b1a5c017SAndriy Gapon */ 2817b1a5c017SAndriy Gapon if (sleep_result == 1 && state != ACPI_STATE_S4) 2818b1a5c017SAndriy Gapon AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT); 2819b1a5c017SAndriy Gapon 28201df130f1SJung-uk Kim AcpiLeaveSleepStatePrep(state); 2821b913a7d5SAndriy Gapon 2822b913a7d5SAndriy Gapon if (sleep_result == 1 && state == ACPI_STATE_S3) { 2823b913a7d5SAndriy Gapon /* 2824b913a7d5SAndriy Gapon * Prevent mis-interpretation of the wakeup by power button 2825b913a7d5SAndriy Gapon * as a request for power off. 2826b913a7d5SAndriy Gapon * Ideally we should post an appropriate wakeup event, 2827b913a7d5SAndriy Gapon * perhaps using acpi_event_power_button_wake or alike. 2828b913a7d5SAndriy Gapon * 2829b913a7d5SAndriy Gapon * Clearing of power button status after wakeup is mandated 2830b913a7d5SAndriy Gapon * by ACPI specification in section "Fixed Power Button". 2831b913a7d5SAndriy Gapon * 2832b913a7d5SAndriy Gapon * XXX As of ACPICA 20121114 AcpiGetEventStatus provides 2833b913a7d5SAndriy Gapon * status as 0/1 corressponding to inactive/active despite 2834b913a7d5SAndriy Gapon * its type being ACPI_EVENT_STATUS. In other words, 2835b913a7d5SAndriy Gapon * we should not test for ACPI_EVENT_FLAG_SET for time being. 2836b913a7d5SAndriy Gapon */ 2837b913a7d5SAndriy Gapon if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON, 2838b913a7d5SAndriy Gapon &power_button_status)) && power_button_status != 0) { 2839b913a7d5SAndriy Gapon AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 2840b913a7d5SAndriy Gapon device_printf(sc->acpi_dev, 2841b913a7d5SAndriy Gapon "cleared fixed power button status\n"); 2842b913a7d5SAndriy Gapon } 2843b913a7d5SAndriy Gapon } 2844b913a7d5SAndriy Gapon 2845f0a101b7SMitsuru IWASAKI intr_restore(intr); 2846f0a101b7SMitsuru IWASAKI 2847f0a101b7SMitsuru IWASAKI /* call acpi_wakeup_machdep() again with interrupt enabled */ 2848f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 1); 2849f0a101b7SMitsuru IWASAKI 2850f0a101b7SMitsuru IWASAKI if (sleep_result == -1) 2851a159c266SJung-uk Kim goto backout; 28526161544cSTakanori Watanabe 28536161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2854be2b1797SNate Lawson if (state == ACPI_STATE_S4) 2855964679ceSMitsuru IWASAKI AcpiEnable(); 28566161544cSTakanori Watanabe } else { 28571df130f1SJung-uk Kim status = AcpiEnterSleepState(state); 28581df130f1SJung-uk Kim AcpiLeaveSleepStatePrep(state); 285971804adcSJung-uk Kim intr_restore(intr); 2860be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2861be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2862be2b1797SNate Lawson AcpiFormatException(status)); 286333c70d41SAndriy Gapon goto backout; 286415e32d5dSMike Smith } 28656161544cSTakanori Watanabe } 286615e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 2867ece50487SMitsuru IWASAKI 286815e2f34fSNate Lawson /* 286915e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 287015e2f34fSNate Lawson * process. This handles both the error and success cases. 287115e2f34fSNate Lawson */ 287233c70d41SAndriy Gapon backout: 287315e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 287415e2f34fSNate Lawson acpi_wake_prep_walk(state); 287515e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 287615e2f34fSNate Lawson } 2877e3b56b66SMitsuru IWASAKI if (slp_state >= ACPI_SS_DEV_SUSPEND) 2878e3b56b66SMitsuru IWASAKI DEVICE_RESUME(root_bus); 2879f0a101b7SMitsuru IWASAKI if (slp_state >= ACPI_SS_SLP_PREP) 288015e2f34fSNate Lawson AcpiLeaveSleepState(state); 2881a0a15716SJung-uk Kim if (slp_state >= ACPI_SS_SLEPT) { 2882a0a15716SJung-uk Kim acpi_resync_clock(sc); 288315e2f34fSNate Lawson acpi_enable_fixed_events(sc); 2884a0a15716SJung-uk Kim } 2885337744d9SJung-uk Kim sc->acpi_next_sstate = 0; 288615e2f34fSNate Lawson 2887a56fe095SJohn Baldwin mtx_unlock(&Giant); 2888c66d2b38SJung-uk Kim 288936a483bbSJung-uk Kim if (smp_started) { 2890c66d2b38SJung-uk Kim thread_lock(curthread); 2891c66d2b38SJung-uk Kim sched_unbind(curthread); 2892c66d2b38SJung-uk Kim thread_unlock(curthread); 289336a483bbSJung-uk Kim } 2894c66d2b38SJung-uk Kim 28954a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_resume); 28964a8fa6feSJung-uk Kim 2897d42f0ad8SJung-uk Kim /* Allow another sleep request after a while. */ 2898fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME); 2899d42f0ad8SJung-uk Kim 2900d42f0ad8SJung-uk Kim /* Run /etc/rc.resume after we are back. */ 2901d42f0ad8SJung-uk Kim if (devctl_process_running()) 2902d42f0ad8SJung-uk Kim acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2903d42f0ad8SJung-uk Kim 29040ae55423SMike Smith return_ACPI_STATUS (status); 290515e32d5dSMike Smith } 290615e32d5dSMike Smith 2907a0a15716SJung-uk Kim static void 29080755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc) 29090755473bSJung-uk Kim { 2910ebbed20dSAndriy Gapon #ifdef __amd64__ 29110755473bSJung-uk Kim if (!acpi_reset_clock) 29120755473bSJung-uk Kim return; 29130755473bSJung-uk Kim 29140755473bSJung-uk Kim /* 29150755473bSJung-uk Kim * Warm up timecounter again and reset system clock. 29160755473bSJung-uk Kim */ 29170755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 29180755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 29190755473bSJung-uk Kim inittodr(time_second + sc->acpi_sleep_delay); 2920a0a15716SJung-uk Kim #endif 29210755473bSJung-uk Kim } 29220755473bSJung-uk Kim 2923e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 2924e8b4d56eSNate Lawson int 2925e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 2926e8b4d56eSNate Lawson { 2927e8b4d56eSNate Lawson struct acpi_prw_data prw; 2928e8b4d56eSNate Lawson ACPI_STATUS status; 2929e8b4d56eSNate Lawson int flags; 2930e8b4d56eSNate Lawson 2931d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 29322be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2933e8b4d56eSNate Lawson return (ENXIO); 2934e8b4d56eSNate Lawson 2935d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 2936e8b4d56eSNate Lawson if (enable) { 29375a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 29385a77b11bSJung-uk Kim ACPI_GPE_ENABLE); 2939e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2940e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 2941e8b4d56eSNate Lawson return (ENXIO); 2942e8b4d56eSNate Lawson } 2943d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2944e8b4d56eSNate Lawson } else { 29455a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 29465a77b11bSJung-uk Kim ACPI_GPE_DISABLE); 2947e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2948e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 2949e8b4d56eSNate Lawson return (ENXIO); 2950e8b4d56eSNate Lawson } 2951d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2952e8b4d56eSNate Lawson } 2953e8b4d56eSNate Lawson 2954e8b4d56eSNate Lawson return (0); 2955e8b4d56eSNate Lawson } 2956e8b4d56eSNate Lawson 2957d4b9ff91SNate Lawson static int 2958d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2959e8b4d56eSNate Lawson { 2960e8b4d56eSNate Lawson struct acpi_prw_data prw; 2961d4b9ff91SNate Lawson device_t dev; 2962e8b4d56eSNate Lawson 2963d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 2964e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2965e8b4d56eSNate Lawson return (ENXIO); 2966d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2967e8b4d56eSNate Lawson 2968e8b4d56eSNate Lawson /* 2969d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 2970d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 2971d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 2972d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 2973d4b9ff91SNate Lawson * and set _PSW. 2974e8b4d56eSNate Lawson */ 2975d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 29765a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 2977e8b4d56eSNate Lawson if (bootverbose) 2978d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2979d4b9ff91SNate Lawson acpi_name(handle), sstate); 2980d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2981d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 2982d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 2983d4b9ff91SNate Lawson if (bootverbose) 2984d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2985d4b9ff91SNate Lawson acpi_name(handle), sstate); 2986d4b9ff91SNate Lawson } 2987cc85c78cSNate Lawson 2988cc85c78cSNate Lawson return (0); 2989e8b4d56eSNate Lawson } 2990e8b4d56eSNate Lawson 2991d4b9ff91SNate Lawson static int 2992d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2993cc85c78cSNate Lawson { 2994cc85c78cSNate Lawson struct acpi_prw_data prw; 2995d4b9ff91SNate Lawson device_t dev; 2996cc85c78cSNate Lawson 2997cc85c78cSNate Lawson /* 2998d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 2999d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 3000cc85c78cSNate Lawson */ 3001d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 3002d4b9ff91SNate Lawson return (ENXIO); 3003d4b9ff91SNate Lawson dev = acpi_get_device(handle); 3004d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 3005d4b9ff91SNate Lawson return (0); 3006cc85c78cSNate Lawson 3007d4b9ff91SNate Lawson /* 3008d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 3009d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 3010d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 3011d4b9ff91SNate Lawson */ 3012d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 30135a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 3014d4b9ff91SNate Lawson if (bootverbose) 3015d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 3016d4b9ff91SNate Lawson } else { 3017d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 3018d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 3019d4b9ff91SNate Lawson if (bootverbose) 3020d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 3021d4b9ff91SNate Lawson acpi_name(handle)); 3022d4b9ff91SNate Lawson } 3023d4b9ff91SNate Lawson 3024e8b4d56eSNate Lawson return (0); 3025e8b4d56eSNate Lawson } 3026e8b4d56eSNate Lawson 3027e8b4d56eSNate Lawson static ACPI_STATUS 3028d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 3029e8b4d56eSNate Lawson { 3030d4b9ff91SNate Lawson int sstate; 3031e8b4d56eSNate Lawson 3032d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 3033d4b9ff91SNate Lawson sstate = *(int *)context; 3034d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 3035d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 3036d4b9ff91SNate Lawson else 3037d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 3038e8b4d56eSNate Lawson return (AE_OK); 3039e8b4d56eSNate Lawson } 3040e8b4d56eSNate Lawson 3041d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 3042e8b4d56eSNate Lawson static int 3043d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 3044e8b4d56eSNate Lawson { 3045e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 3046e8b4d56eSNate Lawson 3047e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 3048d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 30492272d050SJung-uk Kim acpi_wake_prep, NULL, &sstate, NULL); 3050e8b4d56eSNate Lawson return (0); 3051e8b4d56eSNate Lawson } 3052e8b4d56eSNate Lawson 305388a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 305488a79fc0SNate Lawson static int 305588a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 305688a79fc0SNate Lawson { 305788a79fc0SNate Lawson int error, i, numdevs; 305888a79fc0SNate Lawson device_t *devlist; 305988a79fc0SNate Lawson device_t child; 3060d4b9ff91SNate Lawson ACPI_STATUS status; 306188a79fc0SNate Lawson 306288a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 30631c9ec538SNate Lawson if (error != 0 || numdevs == 0) { 30641c9ec538SNate Lawson if (numdevs == 0) 30651c9ec538SNate Lawson free(devlist, M_TEMP); 306688a79fc0SNate Lawson return (error); 30671c9ec538SNate Lawson } 306888a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 306988a79fc0SNate Lawson child = devlist[i]; 3070d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 307188a79fc0SNate Lawson if (!device_is_attached(child)) 307288a79fc0SNate Lawson continue; 3073d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 3074d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 307588a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 307688a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 307788a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 307888a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 307988a79fc0SNate Lawson } 308088a79fc0SNate Lawson } 308188a79fc0SNate Lawson free(devlist, M_TEMP); 308288a79fc0SNate Lawson 308388a79fc0SNate Lawson return (0); 308488a79fc0SNate Lawson } 308588a79fc0SNate Lawson 308688a79fc0SNate Lawson /* Enable or disable wake from userland. */ 308788a79fc0SNate Lawson static int 308888a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 308988a79fc0SNate Lawson { 309088a79fc0SNate Lawson int enable, error; 309188a79fc0SNate Lawson device_t dev; 309288a79fc0SNate Lawson 309388a79fc0SNate Lawson dev = (device_t)arg1; 3094d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 309588a79fc0SNate Lawson 309688a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 309788a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 309888a79fc0SNate Lawson return (error); 309988a79fc0SNate Lawson if (enable != 0 && enable != 1) 310088a79fc0SNate Lawson return (EINVAL); 310188a79fc0SNate Lawson 310288a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 310388a79fc0SNate Lawson } 310488a79fc0SNate Lawson 3105e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 3106d4b9ff91SNate Lawson int 3107e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 3108e8b4d56eSNate Lawson { 3109e8b4d56eSNate Lawson ACPI_STATUS status; 3110e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 3111e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 3112d4b9ff91SNate Lawson int error, i, power_count; 3113e8b4d56eSNate Lawson 3114e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 3115e8b4d56eSNate Lawson return (EINVAL); 3116e8b4d56eSNate Lawson 3117e8b4d56eSNate Lawson /* 3118e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 3119e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 3120e8b4d56eSNate Lawson */ 3121e8b4d56eSNate Lawson error = EINVAL; 3122e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 3123e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 3124e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 3125e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 3126e8b4d56eSNate Lawson return (ENOENT); 3127e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 3128e8b4d56eSNate Lawson if (res == NULL) 3129e8b4d56eSNate Lawson return (ENOENT); 3130e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 3131e8b4d56eSNate Lawson goto out; 3132e8b4d56eSNate Lawson 3133e8b4d56eSNate Lawson /* 3134e8b4d56eSNate Lawson * Element 1 of the _PRW object: 3135e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 3136e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 3137e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 3138e8b4d56eSNate Lawson */ 3139e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 3140e8b4d56eSNate Lawson goto out; 3141e8b4d56eSNate Lawson 3142e8b4d56eSNate Lawson /* 3143e8b4d56eSNate Lawson * Element 0 of the _PRW object: 3144e8b4d56eSNate Lawson */ 3145e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 3146e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 3147e8b4d56eSNate Lawson /* 3148e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 3149e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 3150e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 3151e8b4d56eSNate Lawson * enabled for the wake event. 3152e8b4d56eSNate Lawson */ 3153e8b4d56eSNate Lawson prw->gpe_handle = NULL; 3154e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 3155e8b4d56eSNate Lawson error = 0; 3156e8b4d56eSNate Lawson break; 3157e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 3158e8b4d56eSNate Lawson /* 3159e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 3160e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 3161e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 3162e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 3163e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 3164e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 3165e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 3166e8b4d56eSNate Lawson * the wake event. 3167e8b4d56eSNate Lawson * 3168e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 3169e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 3170e8b4d56eSNate Lawson */ 3171e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 3172e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 3173e8b4d56eSNate Lawson goto out; 3174e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 3175e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 3176e8b4d56eSNate Lawson goto out; 3177e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 3178e8b4d56eSNate Lawson goto out; 3179e8b4d56eSNate Lawson error = 0; 3180e8b4d56eSNate Lawson break; 3181e8b4d56eSNate Lawson default: 3182e8b4d56eSNate Lawson goto out; 3183e8b4d56eSNate Lawson } 3184e8b4d56eSNate Lawson 3185d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 3186d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 3187d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 3188d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 3189d4b9ff91SNate Lawson power_count = 0; 3190d4b9ff91SNate Lawson } 3191d4b9ff91SNate Lawson prw->power_res_count = power_count; 3192d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 3193d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 3194e8b4d56eSNate Lawson 3195e8b4d56eSNate Lawson out: 3196e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 3197e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 3198e8b4d56eSNate Lawson return (error); 3199e8b4d56eSNate Lawson } 3200e8b4d56eSNate Lawson 320115e32d5dSMike Smith /* 320215e32d5dSMike Smith * ACPI Event Handlers 320315e32d5dSMike Smith */ 320415e32d5dSMike Smith 320515e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 320615e32d5dSMike Smith 320715e32d5dSMike Smith static void 320815e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 320915e32d5dSMike Smith { 32102d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 321100a30448SNate Lawson int ret; 3212bee4aa4aSNate Lawson 3213b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 321415e32d5dSMike Smith 3215a0e73a12SJung-uk Kim /* Check if button action is disabled or unknown. */ 3216a0e73a12SJung-uk Kim if (state == ACPI_STATE_UNKNOWN) 3217813d6dcaSNate Lawson return; 3218813d6dcaSNate Lawson 321900a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */ 32202d0c82e8SJung-uk Kim ret = acpi_ReqSleepState(sc, state); 322100a30448SNate Lawson if (ret != 0) 32222d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 32232d0c82e8SJung-uk Kim "request to enter state S%d failed (err %d)\n", state, ret); 3224bee4aa4aSNate Lawson 32250ae55423SMike Smith return_VOID; 322615e32d5dSMike Smith } 322715e32d5dSMike Smith 322815e32d5dSMike Smith static void 322915e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 323015e32d5dSMike Smith { 3231bee4aa4aSNate Lawson 3232b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 32330ae55423SMike Smith 3234bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 3235cb9b0d80SMike Smith 32360ae55423SMike Smith return_VOID; 323715e32d5dSMike Smith } 323815e32d5dSMike Smith 323915e32d5dSMike Smith /* 324015e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 324115e32d5dSMike Smith */ 3242b5854f5fSJung-uk Kim static void 3243b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context) 3244b5854f5fSJung-uk Kim { 3245b5854f5fSJung-uk Kim 3246b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context); 3247b5854f5fSJung-uk Kim } 3248b5854f5fSJung-uk Kim 3249b5854f5fSJung-uk Kim static void 3250b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context) 3251b5854f5fSJung-uk Kim { 3252b5854f5fSJung-uk Kim 3253b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context); 3254b5854f5fSJung-uk Kim } 3255b5854f5fSJung-uk Kim 325615e32d5dSMike Smith UINT32 325733febf93SNate Lawson acpi_event_power_button_sleep(void *context) 325815e32d5dSMike Smith { 325915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 326015e32d5dSMike Smith 3261b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 32620ae55423SMike Smith 3263b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3264b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx))) 3265b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3266b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 326715e32d5dSMike Smith } 326815e32d5dSMike Smith 326915e32d5dSMike Smith UINT32 327033febf93SNate Lawson acpi_event_power_button_wake(void *context) 327115e32d5dSMike Smith { 327215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 327315e32d5dSMike Smith 3274b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 32750ae55423SMike Smith 3276b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3277b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx))) 3278b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3279b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 328015e32d5dSMike Smith } 328115e32d5dSMike Smith 328215e32d5dSMike Smith UINT32 328333febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 328415e32d5dSMike Smith { 328515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 328615e32d5dSMike Smith 3287b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 32880ae55423SMike Smith 3289b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3290b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx))) 3291b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3292b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 329315e32d5dSMike Smith } 329415e32d5dSMike Smith 329515e32d5dSMike Smith UINT32 329633febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 329715e32d5dSMike Smith { 329815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 329915e32d5dSMike Smith 3300b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 33010ae55423SMike Smith 3302b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3303b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx))) 3304b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3305b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 330615e32d5dSMike Smith } 330715e32d5dSMike Smith 330815e32d5dSMike Smith /* 3309bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 3310bee4aa4aSNate Lawson * use this for single-threaded callers. 331115e32d5dSMike Smith */ 331215e32d5dSMike Smith char * 331315e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 331415e32d5dSMike Smith { 3315bee4aa4aSNate Lawson ACPI_BUFFER buf; 3316bee4aa4aSNate Lawson static char data[256]; 331715e32d5dSMike Smith 3318bee4aa4aSNate Lawson buf.Length = sizeof(data); 3319bee4aa4aSNate Lawson buf.Pointer = data; 3320cb9b0d80SMike Smith 33210a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3322bee4aa4aSNate Lawson return (data); 3323bee4aa4aSNate Lawson return ("(unknown)"); 332415e32d5dSMike Smith } 332515e32d5dSMike Smith 332615e32d5dSMike Smith /* 332715e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 332815e32d5dSMike Smith * parts of the namespace. 332915e32d5dSMike Smith */ 333015e32d5dSMike Smith int 333115e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 333215e32d5dSMike Smith { 33333c600cfbSJohn Baldwin char *cp, *env, *np; 333415e32d5dSMike Smith int len; 333515e32d5dSMike Smith 333615e32d5dSMike Smith np = acpi_name(handle); 333715e32d5dSMike Smith if (*np == '\\') 333815e32d5dSMike Smith np++; 33392be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.avoid")) == NULL) 334015e32d5dSMike Smith return (0); 334115e32d5dSMike Smith 3342be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 33433c600cfbSJohn Baldwin cp = env; 334415e32d5dSMike Smith for (;;) { 3345bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 334615e32d5dSMike Smith cp++; 334715e32d5dSMike Smith if (*cp == 0) 334815e32d5dSMike Smith break; 334915e32d5dSMike Smith len = 0; 3350bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 335115e32d5dSMike Smith len++; 3352d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 33533c600cfbSJohn Baldwin freeenv(env); 33540ae55423SMike Smith return(1); 3355d786139cSMaxime Henrion } 33560ae55423SMike Smith cp += len; 33570ae55423SMike Smith } 33583c600cfbSJohn Baldwin freeenv(env); 3359be2b1797SNate Lawson 33600ae55423SMike Smith return (0); 33610ae55423SMike Smith } 33620ae55423SMike Smith 33630ae55423SMike Smith /* 33640ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 33650ae55423SMike Smith */ 33660ae55423SMike Smith int 33670ae55423SMike Smith acpi_disabled(char *subsys) 33680ae55423SMike Smith { 336978689b15SMaxime Henrion char *cp, *env; 33700ae55423SMike Smith int len; 33710ae55423SMike Smith 33722be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.disabled")) == NULL) 33730ae55423SMike Smith return (0); 33743184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 337578689b15SMaxime Henrion freeenv(env); 33760ae55423SMike Smith return (1); 3377d786139cSMaxime Henrion } 33780ae55423SMike Smith 33793184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 338078689b15SMaxime Henrion cp = env; 33810ae55423SMike Smith for (;;) { 33823184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 33830ae55423SMike Smith cp++; 33843184cf5aSNate Lawson if (*cp == '\0') 33850ae55423SMike Smith break; 33860ae55423SMike Smith len = 0; 33873184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 33880ae55423SMike Smith len++; 33893184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 339078689b15SMaxime Henrion freeenv(env); 339115e32d5dSMike Smith return (1); 3392d786139cSMaxime Henrion } 339315e32d5dSMike Smith cp += len; 339415e32d5dSMike Smith } 339578689b15SMaxime Henrion freeenv(env); 3396be2b1797SNate Lawson 339715e32d5dSMike Smith return (0); 339815e32d5dSMike Smith } 339915e32d5dSMike Smith 340015e32d5dSMike Smith /* 340115e32d5dSMike Smith * Control interface. 340215e32d5dSMike Smith * 34030ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 3404be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 3405be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 340615e32d5dSMike Smith */ 34070ae55423SMike Smith struct acpi_ioctl_hook 34080ae55423SMike Smith { 34090ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 34100ae55423SMike Smith u_long cmd; 3411be2b1797SNate Lawson acpi_ioctl_fn fn; 34120ae55423SMike Smith void *arg; 34130ae55423SMike Smith }; 34140ae55423SMike Smith 34150ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 34160ae55423SMike Smith static int acpi_ioctl_hooks_initted; 34170ae55423SMike Smith 34180ae55423SMike Smith int 3419be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 34200ae55423SMike Smith { 34210ae55423SMike Smith struct acpi_ioctl_hook *hp; 34220ae55423SMike Smith 34230ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 34240ae55423SMike Smith return (ENOMEM); 34250ae55423SMike Smith hp->cmd = cmd; 34260ae55423SMike Smith hp->fn = fn; 34270ae55423SMike Smith hp->arg = arg; 342815e2f34fSNate Lawson 342915e2f34fSNate Lawson ACPI_LOCK(acpi); 34300ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 34310ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 34320ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 34330ae55423SMike Smith } 34340ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 343515e2f34fSNate Lawson ACPI_UNLOCK(acpi); 343615e2f34fSNate Lawson 34370ae55423SMike Smith return (0); 34380ae55423SMike Smith } 34390ae55423SMike Smith 34400ae55423SMike Smith void 3441be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 34420ae55423SMike Smith { 34430ae55423SMike Smith struct acpi_ioctl_hook *hp; 34440ae55423SMike Smith 344515e2f34fSNate Lawson ACPI_LOCK(acpi); 34460ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 344715e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 34480ae55423SMike Smith break; 34490ae55423SMike Smith 34500ae55423SMike Smith if (hp != NULL) { 34510ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 34520ae55423SMike Smith free(hp, M_ACPIDEV); 34530ae55423SMike Smith } 345415e2f34fSNate Lawson ACPI_UNLOCK(acpi); 34550ae55423SMike Smith } 34560ae55423SMike Smith 345715e32d5dSMike Smith static int 345800b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 345915e32d5dSMike Smith { 346015e32d5dSMike Smith return (0); 346115e32d5dSMike Smith } 346215e32d5dSMike Smith 346315e32d5dSMike Smith static int 346400b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 346515e32d5dSMike Smith { 346615e32d5dSMike Smith return (0); 346715e32d5dSMike Smith } 346815e32d5dSMike Smith 346915e32d5dSMike Smith static int 347000b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 347115e32d5dSMike Smith { 347215e32d5dSMike Smith struct acpi_softc *sc; 34730ae55423SMike Smith struct acpi_ioctl_hook *hp; 3474bee4aa4aSNate Lawson int error, state; 347515e32d5dSMike Smith 3476a2e35898SDavid E. O'Brien error = 0; 3477a2e35898SDavid E. O'Brien hp = NULL; 347815e32d5dSMike Smith sc = dev->si_drv1; 347915e32d5dSMike Smith 34800ae55423SMike Smith /* 34810ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 34820ae55423SMike Smith */ 348315e2f34fSNate Lawson ACPI_LOCK(acpi); 3484bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 34850ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3486bee4aa4aSNate Lawson if (hp->cmd == cmd) 3487bee4aa4aSNate Lawson break; 34880ae55423SMike Smith } 348915e2f34fSNate Lawson ACPI_UNLOCK(acpi); 3490bee4aa4aSNate Lawson if (hp) 3491bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 34920ae55423SMike Smith 34930ae55423SMike Smith /* 3494a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 3495a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 3496a89fcf28STakanori Watanabe * Not changing system behavior. 3497a89fcf28STakanori Watanabe */ 3498be2b1797SNate Lawson if ((flag & FWRITE) == 0) 3499be2b1797SNate Lawson return (EPERM); 3500a89fcf28STakanori Watanabe 3501be2b1797SNate Lawson /* Core system ioctls. */ 350215e32d5dSMike Smith switch (cmd) { 350300a30448SNate Lawson case ACPIIO_REQSLPSTATE: 350400a30448SNate Lawson state = *(int *)addr; 350500a30448SNate Lawson if (state != ACPI_STATE_S5) 3506a0e73a12SJung-uk Kim return (acpi_ReqSleepState(sc, state)); 3507a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3508a0e73a12SJung-uk Kim error = EOPNOTSUPP; 350900a30448SNate Lawson break; 351000a30448SNate Lawson case ACPIIO_ACKSLPSTATE: 351100a30448SNate Lawson error = *(int *)addr; 351200a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error); 351300a30448SNate Lawson break; 351400a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 351515e32d5dSMike Smith state = *(int *)addr; 3516a0e73a12SJung-uk Kim if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3517a0e73a12SJung-uk Kim return (EINVAL); 3518a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 3519a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3520a0e73a12SJung-uk Kim if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3521a0e73a12SJung-uk Kim error = ENXIO; 352215e32d5dSMike Smith break; 352315e32d5dSMike Smith default: 3524bee4aa4aSNate Lawson error = ENXIO; 352515e32d5dSMike Smith break; 352615e32d5dSMike Smith } 3527917d44c8SMitsuru IWASAKI 352815e32d5dSMike Smith return (error); 352915e32d5dSMike Smith } 353015e32d5dSMike Smith 35311d073b1dSJohn Baldwin static int 3532a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname) 3533a0e73a12SJung-uk Kim { 3534a0e73a12SJung-uk Kim int sstate; 3535a0e73a12SJung-uk Kim 3536a0e73a12SJung-uk Kim if (toupper(sname[0]) == 'S') { 3537a0e73a12SJung-uk Kim sstate = sname[1] - '0'; 3538a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3539a0e73a12SJung-uk Kim sname[2] == '\0') 3540a0e73a12SJung-uk Kim return (sstate); 3541a0e73a12SJung-uk Kim } else if (strcasecmp(sname, "NONE") == 0) 3542a0e73a12SJung-uk Kim return (ACPI_STATE_UNKNOWN); 3543a0e73a12SJung-uk Kim return (-1); 3544a0e73a12SJung-uk Kim } 3545a0e73a12SJung-uk Kim 3546a0e73a12SJung-uk Kim static const char * 3547a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate) 3548a0e73a12SJung-uk Kim { 3549a0e73a12SJung-uk Kim static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3550a0e73a12SJung-uk Kim 3551a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3552a0e73a12SJung-uk Kim return (snames[sstate]); 3553a0e73a12SJung-uk Kim else if (sstate == ACPI_STATE_UNKNOWN) 3554a0e73a12SJung-uk Kim return ("NONE"); 3555a0e73a12SJung-uk Kim return (NULL); 3556a0e73a12SJung-uk Kim } 3557a0e73a12SJung-uk Kim 3558a0e73a12SJung-uk Kim static int 3559d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3560d75de536SMitsuru IWASAKI { 3561d75de536SMitsuru IWASAKI int error; 3562bee4aa4aSNate Lawson struct sbuf sb; 3563a0e73a12SJung-uk Kim UINT8 state; 3564d75de536SMitsuru IWASAKI 3565bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3566a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3567a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) 3568a0e73a12SJung-uk Kim sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3569bee4aa4aSNate Lawson sbuf_trim(&sb); 3570bee4aa4aSNate Lawson sbuf_finish(&sb); 3571bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3572bee4aa4aSNate Lawson sbuf_delete(&sb); 3573d75de536SMitsuru IWASAKI return (error); 3574d75de536SMitsuru IWASAKI } 3575d75de536SMitsuru IWASAKI 3576d75de536SMitsuru IWASAKI static int 35771d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 35781d073b1dSJohn Baldwin { 35791d073b1dSJohn Baldwin char sleep_state[10]; 3580a0e73a12SJung-uk Kim int error, new_state, old_state; 35811d073b1dSJohn Baldwin 3582a0e73a12SJung-uk Kim old_state = *(int *)oidp->oid_arg1; 3583a0e73a12SJung-uk Kim strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 35841d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 35851d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 3586a0e73a12SJung-uk Kim new_state = acpi_sname2sstate(sleep_state); 3587a0e73a12SJung-uk Kim if (new_state < ACPI_STATE_S1) 3588a0e73a12SJung-uk Kim return (EINVAL); 358954b43614SJung-uk Kim if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3590a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3591be2b1797SNate Lawson if (new_state != old_state) 3592a0e73a12SJung-uk Kim *(int *)oidp->oid_arg1 = new_state; 35931d073b1dSJohn Baldwin } 35941d073b1dSJohn Baldwin return (error); 35951d073b1dSJohn Baldwin } 35961d073b1dSJohn Baldwin 35979b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 35989b937d48SNate Lawson void 35999b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 36009b937d48SNate Lawson { 36019b937d48SNate Lawson char notify_buf[16]; 36029b937d48SNate Lawson ACPI_BUFFER handle_buf; 36039b937d48SNate Lawson ACPI_STATUS status; 36049b937d48SNate Lawson 36059b937d48SNate Lawson if (subsystem == NULL) 36069b937d48SNate Lawson return; 36079b937d48SNate Lawson 36089b937d48SNate Lawson handle_buf.Pointer = NULL; 36099b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 36109b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 36119b937d48SNate Lawson if (ACPI_FAILURE(status)) 36129b937d48SNate Lawson return; 36139b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 36149b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 36159b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 36169b937d48SNate Lawson } 36179b937d48SNate Lawson 361815e32d5dSMike Smith #ifdef ACPI_DEBUG 36190ae55423SMike Smith /* 36200ae55423SMike Smith * Support for parsing debug options from the kernel environment. 36210ae55423SMike Smith * 36220ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 36230ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 36240ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 36250ae55423SMike Smith * prefixing the bit name with !. 36260ae55423SMike Smith */ 362715e32d5dSMike Smith struct debugtag 362815e32d5dSMike Smith { 362915e32d5dSMike Smith char *name; 363015e32d5dSMike Smith UINT32 value; 363115e32d5dSMike Smith }; 363215e32d5dSMike Smith 363315e32d5dSMike Smith static struct debugtag dbg_layer[] = { 3634c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 3635c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 3636c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 3637c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 3638c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3639c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 3640c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3641c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 3642c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 3643d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 36444c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3645d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3646297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 36474c1cdee6SMike Smith 3648c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3649c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 36503184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 3651c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 36523184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 36533184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 36543184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 36554c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3656cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 36573184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 3658b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 365915e32d5dSMike Smith {NULL, 0} 366015e32d5dSMike Smith }; 366115e32d5dSMike Smith 366215e32d5dSMike Smith static struct debugtag dbg_level[] = { 36639501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 36644c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 36659501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 3666cc6455afSJung-uk Kim {"ACPI_LV_REPAIR", ACPI_LV_REPAIR}, 36674c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3668d62ab2f4SMitsuru IWASAKI 3669d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 3670297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 36714c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 36724c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3673d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 36744c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 36754c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 36764c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 36774c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 36784c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 36794c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 36804c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 36814c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 36824c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 36834c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3684d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3685d62ab2f4SMitsuru IWASAKI 3686d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3687d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3688d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3689d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3690d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 36914c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 3692d62ab2f4SMitsuru IWASAKI 3693d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3694d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3695d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3696d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 3697d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3698d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3699d62ab2f4SMitsuru IWASAKI 3700d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 370198479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 370298479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 370398479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 370498479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 370598479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 370615e32d5dSMike Smith {NULL, 0} 370715e32d5dSMike Smith }; 370815e32d5dSMike Smith 370915e32d5dSMike Smith static void 371015e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 371115e32d5dSMike Smith { 371215e32d5dSMike Smith char *ep; 371315e32d5dSMike Smith int i, l; 37140ae55423SMike Smith int set; 371515e32d5dSMike Smith 371615e32d5dSMike Smith while (*cp) { 371715e32d5dSMike Smith if (isspace(*cp)) { 371815e32d5dSMike Smith cp++; 371915e32d5dSMike Smith continue; 372015e32d5dSMike Smith } 372115e32d5dSMike Smith ep = cp; 372215e32d5dSMike Smith while (*ep && !isspace(*ep)) 372315e32d5dSMike Smith ep++; 37240ae55423SMike Smith if (*cp == '!') { 37250ae55423SMike Smith set = 0; 37260ae55423SMike Smith cp++; 37270ae55423SMike Smith if (cp == ep) 37280ae55423SMike Smith continue; 37290ae55423SMike Smith } else { 37300ae55423SMike Smith set = 1; 37310ae55423SMike Smith } 373215e32d5dSMike Smith l = ep - cp; 373315e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 373415e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 3735be2b1797SNate Lawson if (set) 373615e32d5dSMike Smith *flag |= tag[i].value; 3737be2b1797SNate Lawson else 37380ae55423SMike Smith *flag &= ~tag[i].value; 373915e32d5dSMike Smith } 374015e32d5dSMike Smith } 374115e32d5dSMike Smith cp = ep; 374215e32d5dSMike Smith } 374315e32d5dSMike Smith } 374415e32d5dSMike Smith 374515e32d5dSMike Smith static void 37462a4ac806SMike Smith acpi_set_debugging(void *junk) 374715e32d5dSMike Smith { 37487e639165SNate Lawson char *layer, *level; 374915e32d5dSMike Smith 37501d7b121cSNate Lawson if (cold) { 37510ae55423SMike Smith AcpiDbgLayer = 0; 37520ae55423SMike Smith AcpiDbgLevel = 0; 37531d7b121cSNate Lawson } 37541d7b121cSNate Lawson 37552be111bfSDavide Italiano layer = kern_getenv("debug.acpi.layer"); 37562be111bfSDavide Italiano level = kern_getenv("debug.acpi.level"); 37577e639165SNate Lawson if (layer == NULL && level == NULL) 37587e639165SNate Lawson return; 37590ae55423SMike Smith 37607e639165SNate Lawson printf("ACPI set debug"); 37617e639165SNate Lawson if (layer != NULL) { 37627e639165SNate Lawson if (strcmp("NONE", layer) != 0) 37637e639165SNate Lawson printf(" layer '%s'", layer); 37647e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 37657e639165SNate Lawson freeenv(layer); 37661d7b121cSNate Lawson } 37677e639165SNate Lawson if (level != NULL) { 37687e639165SNate Lawson if (strcmp("NONE", level) != 0) 37697e639165SNate Lawson printf(" level '%s'", level); 37707e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 37717e639165SNate Lawson freeenv(level); 37727e639165SNate Lawson } 37737e639165SNate Lawson printf("\n"); 377415e32d5dSMike Smith } 3775bee4aa4aSNate Lawson 3776be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3777be2b1797SNate Lawson NULL); 37781d7b121cSNate Lawson 37791d7b121cSNate Lawson static int 37801d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 37811d7b121cSNate Lawson { 378254f6bca0SNate Lawson int error, *dbg; 37831d7b121cSNate Lawson struct debugtag *tag; 378454f6bca0SNate Lawson struct sbuf sb; 37850e1152fcSHans Petter Selasky char temp[128]; 37861d7b121cSNate Lawson 378754f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 378854f6bca0SNate Lawson return (ENOMEM); 37891d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 37901d7b121cSNate Lawson tag = &dbg_layer[0]; 37911d7b121cSNate Lawson dbg = &AcpiDbgLayer; 37921d7b121cSNate Lawson } else { 37931d7b121cSNate Lawson tag = &dbg_level[0]; 37941d7b121cSNate Lawson dbg = &AcpiDbgLevel; 37951d7b121cSNate Lawson } 37961d7b121cSNate Lawson 37971d7b121cSNate Lawson /* Get old values if this is a get request. */ 379815e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 37991d7b121cSNate Lawson if (*dbg == 0) { 380054f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 38011d7b121cSNate Lawson } else if (req->newptr == NULL) { 38021d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 380354f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 380454f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 38051d7b121cSNate Lawson } 38061d7b121cSNate Lawson } 380754f6bca0SNate Lawson sbuf_trim(&sb); 380854f6bca0SNate Lawson sbuf_finish(&sb); 38090e1152fcSHans Petter Selasky strlcpy(temp, sbuf_data(&sb), sizeof(temp)); 381054f6bca0SNate Lawson sbuf_delete(&sb); 38111d7b121cSNate Lawson 38120e1152fcSHans Petter Selasky error = sysctl_handle_string(oidp, temp, sizeof(temp), req); 38130e1152fcSHans Petter Selasky 38140e1152fcSHans Petter Selasky /* Check for error or no change */ 38151d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 38161d7b121cSNate Lawson *dbg = 0; 38170e1152fcSHans Petter Selasky kern_setenv((char *)oidp->oid_arg1, temp); 38181d7b121cSNate Lawson acpi_set_debugging(NULL); 38191d7b121cSNate Lawson } 382015e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 38211d7b121cSNate Lawson 38221d7b121cSNate Lawson return (error); 38231d7b121cSNate Lawson } 3824bee4aa4aSNate Lawson 38251d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 38261d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 38271d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 38281d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3829bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 3830f9390180SMitsuru IWASAKI 3831f9390180SMitsuru IWASAKI static int 38322a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 38332a18c71dSJung-uk Kim { 38342a18c71dSJung-uk Kim int error; 38352a18c71dSJung-uk Kim int old; 38362a18c71dSJung-uk Kim 38372a18c71dSJung-uk Kim old = acpi_debug_objects; 38382a18c71dSJung-uk Kim error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 38392a18c71dSJung-uk Kim if (error != 0 || req->newptr == NULL) 38402a18c71dSJung-uk Kim return (error); 38412a18c71dSJung-uk Kim if (old == acpi_debug_objects || (old && acpi_debug_objects)) 38422a18c71dSJung-uk Kim return (0); 38432a18c71dSJung-uk Kim 38442a18c71dSJung-uk Kim ACPI_SERIAL_BEGIN(acpi); 38452a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 38462a18c71dSJung-uk Kim ACPI_SERIAL_END(acpi); 38472a18c71dSJung-uk Kim 38482a18c71dSJung-uk Kim return (0); 38492a18c71dSJung-uk Kim } 38502a18c71dSJung-uk Kim 38512a18c71dSJung-uk Kim static int 3852ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface) 3853ae19af49SJung-uk Kim { 3854ae19af49SJung-uk Kim char *p; 3855ae19af49SJung-uk Kim size_t len; 3856ae19af49SJung-uk Kim int i, j; 3857ae19af49SJung-uk Kim 3858ae19af49SJung-uk Kim p = str; 3859ae19af49SJung-uk Kim while (isspace(*p) || *p == ',') 3860ae19af49SJung-uk Kim p++; 3861ae19af49SJung-uk Kim len = strlen(p); 3862ae19af49SJung-uk Kim if (len == 0) 3863ae19af49SJung-uk Kim return (0); 3864ae19af49SJung-uk Kim p = strdup(p, M_TEMP); 3865ae19af49SJung-uk Kim for (i = 0; i < len; i++) 3866ae19af49SJung-uk Kim if (p[i] == ',') 3867ae19af49SJung-uk Kim p[i] = '\0'; 3868ae19af49SJung-uk Kim i = j = 0; 3869ae19af49SJung-uk Kim while (i < len) 3870ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 3871ae19af49SJung-uk Kim i++; 3872ae19af49SJung-uk Kim else { 3873ae19af49SJung-uk Kim i += strlen(p + i) + 1; 3874ae19af49SJung-uk Kim j++; 3875ae19af49SJung-uk Kim } 3876ae19af49SJung-uk Kim if (j == 0) { 3877ae19af49SJung-uk Kim free(p, M_TEMP); 3878ae19af49SJung-uk Kim return (0); 3879ae19af49SJung-uk Kim } 3880ae19af49SJung-uk Kim iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK); 3881ae19af49SJung-uk Kim iface->num = j; 3882ae19af49SJung-uk Kim i = j = 0; 3883ae19af49SJung-uk Kim while (i < len) 3884ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 3885ae19af49SJung-uk Kim i++; 3886ae19af49SJung-uk Kim else { 3887ae19af49SJung-uk Kim iface->data[j] = p + i; 3888ae19af49SJung-uk Kim i += strlen(p + i) + 1; 3889ae19af49SJung-uk Kim j++; 3890ae19af49SJung-uk Kim } 3891ae19af49SJung-uk Kim 3892ae19af49SJung-uk Kim return (j); 3893ae19af49SJung-uk Kim } 3894ae19af49SJung-uk Kim 3895ae19af49SJung-uk Kim static void 3896ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface) 3897ae19af49SJung-uk Kim { 3898ae19af49SJung-uk Kim 3899ae19af49SJung-uk Kim free(iface->data[0], M_TEMP); 3900ae19af49SJung-uk Kim free(iface->data, M_TEMP); 3901ae19af49SJung-uk Kim } 3902ae19af49SJung-uk Kim 3903ae19af49SJung-uk Kim static void 3904ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev) 3905ae19af49SJung-uk Kim { 3906ae19af49SJung-uk Kim struct acpi_interface list; 3907ae19af49SJung-uk Kim ACPI_STATUS status; 3908ae19af49SJung-uk Kim int i; 3909ae19af49SJung-uk Kim 3910ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) { 3911ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 3912ae19af49SJung-uk Kim status = AcpiInstallInterface(list.data[i]); 3913ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 3914ae19af49SJung-uk Kim device_printf(dev, 3915ae19af49SJung-uk Kim "failed to install _OSI(\"%s\"): %s\n", 3916ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 3917ae19af49SJung-uk Kim else if (bootverbose) 3918ae19af49SJung-uk Kim device_printf(dev, "installed _OSI(\"%s\")\n", 3919ae19af49SJung-uk Kim list.data[i]); 3920ae19af49SJung-uk Kim } 3921ae19af49SJung-uk Kim acpi_free_interfaces(&list); 3922ae19af49SJung-uk Kim } 3923ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) { 3924ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 3925ae19af49SJung-uk Kim status = AcpiRemoveInterface(list.data[i]); 3926ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 3927ae19af49SJung-uk Kim device_printf(dev, 3928ae19af49SJung-uk Kim "failed to remove _OSI(\"%s\"): %s\n", 3929ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 3930ae19af49SJung-uk Kim else if (bootverbose) 3931ae19af49SJung-uk Kim device_printf(dev, "removed _OSI(\"%s\")\n", 3932ae19af49SJung-uk Kim list.data[i]); 3933ae19af49SJung-uk Kim } 3934ae19af49SJung-uk Kim acpi_free_interfaces(&list); 3935ae19af49SJung-uk Kim } 3936ae19af49SJung-uk Kim } 3937ae19af49SJung-uk Kim 3938ae19af49SJung-uk Kim static int 3939f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 3940f9390180SMitsuru IWASAKI { 3941f9390180SMitsuru IWASAKI int state, acpi_state; 3942f9390180SMitsuru IWASAKI int error; 3943f9390180SMitsuru IWASAKI struct acpi_softc *sc; 3944f9390180SMitsuru IWASAKI va_list ap; 3945f9390180SMitsuru IWASAKI 3946f9390180SMitsuru IWASAKI error = 0; 3947f9390180SMitsuru IWASAKI switch (cmd) { 3948f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 3949f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 3950f9390180SMitsuru IWASAKI if (sc == NULL) { 3951f9390180SMitsuru IWASAKI error = EINVAL; 3952f9390180SMitsuru IWASAKI goto out; 3953f9390180SMitsuru IWASAKI } 3954f9390180SMitsuru IWASAKI 3955f9390180SMitsuru IWASAKI va_start(ap, arg); 3956f9390180SMitsuru IWASAKI state = va_arg(ap, int); 3957f9390180SMitsuru IWASAKI va_end(ap); 3958f9390180SMitsuru IWASAKI 3959f9390180SMitsuru IWASAKI switch (state) { 3960f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 3961f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 3962f9390180SMitsuru IWASAKI break; 3963f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 3964f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 3965f9390180SMitsuru IWASAKI break; 3966f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 3967f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 3968f9390180SMitsuru IWASAKI break; 3969f9390180SMitsuru IWASAKI default: 3970f9390180SMitsuru IWASAKI error = EINVAL; 3971f9390180SMitsuru IWASAKI goto out; 3972f9390180SMitsuru IWASAKI } 3973f9390180SMitsuru IWASAKI 397400a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 397500a30448SNate Lawson error = ENXIO; 3976f9390180SMitsuru IWASAKI break; 3977f9390180SMitsuru IWASAKI default: 3978f9390180SMitsuru IWASAKI error = EINVAL; 3979f9390180SMitsuru IWASAKI goto out; 3980f9390180SMitsuru IWASAKI } 3981f9390180SMitsuru IWASAKI 3982f9390180SMitsuru IWASAKI out: 3983f9390180SMitsuru IWASAKI return (error); 3984f9390180SMitsuru IWASAKI } 3985f9390180SMitsuru IWASAKI 3986f9390180SMitsuru IWASAKI static void 3987f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 3988f9390180SMitsuru IWASAKI { 3989be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 39906c407052SMitsuru IWASAKI return; 3991f9390180SMitsuru IWASAKI 3992f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3993f9390180SMitsuru IWASAKI } 3994f9390180SMitsuru IWASAKI 3995f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3996