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" 3462d70a81SJohn Baldwin #include "opt_device_numa.h" 3562d70a81SJohn Baldwin 3615e32d5dSMike Smith #include <sys/param.h> 3715e32d5dSMike Smith #include <sys/kernel.h> 38fb919e4dSMark Murray #include <sys/proc.h> 39a89fcf28STakanori Watanabe #include <sys/fcntl.h> 4015e32d5dSMike Smith #include <sys/malloc.h> 41fe12f24bSPoul-Henning Kamp #include <sys/module.h> 4215e32d5dSMike Smith #include <sys/bus.h> 4315e32d5dSMike Smith #include <sys/conf.h> 4415e32d5dSMike Smith #include <sys/ioccom.h> 4515e32d5dSMike Smith #include <sys/reboot.h> 4615e32d5dSMike Smith #include <sys/sysctl.h> 4715e32d5dSMike Smith #include <sys/ctype.h> 481611ea87SMitsuru IWASAKI #include <sys/linker.h> 49f9390180SMitsuru IWASAKI #include <sys/power.h> 5054f6bca0SNate Lawson #include <sys/sbuf.h> 51c66d2b38SJung-uk Kim #include <sys/sched.h> 52eeb3a05fSNate Lawson #include <sys/smp.h> 53c66d2b38SJung-uk Kim #include <sys/timetc.h> 5415e32d5dSMike Smith 55d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 56d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h> 57d320e05cSJohn Baldwin #endif 5815e32d5dSMike Smith #include <machine/resource.h> 59dc750869SNate Lawson #include <machine/bus.h> 60dc750869SNate Lawson #include <sys/rman.h> 61bc0f2195SMike Smith #include <isa/isavar.h> 62c796e27bSNate Lawson #include <isa/pnpvar.h> 63bc0f2195SMike Smith 64129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h> 65129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h> 66129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h> 67129d3046SJung-uk Kim 6815e32d5dSMike Smith #include <dev/acpica/acpivar.h> 6915e32d5dSMike Smith #include <dev/acpica/acpiio.h> 7015e32d5dSMike Smith 712be4e471SJung-uk Kim #include <vm/vm_param.h> 722be4e471SJung-uk Kim 73d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 7415e32d5dSMike Smith 75be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 76cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 77b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 780ae55423SMike Smith 7915e32d5dSMike Smith static d_open_t acpiopen; 8015e32d5dSMike Smith static d_close_t acpiclose; 8115e32d5dSMike Smith static d_ioctl_t acpiioctl; 8215e32d5dSMike Smith 8315e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 84dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 857ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 867ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 877ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 887ac40f5fSPoul-Henning Kamp .d_name = "acpi", 8915e32d5dSMike Smith }; 9015e32d5dSMike Smith 91ae19af49SJung-uk Kim struct acpi_interface { 92ae19af49SJung-uk Kim ACPI_STRING *data; 93ae19af49SJung-uk Kim int num; 94ae19af49SJung-uk Kim }; 95ae19af49SJung-uk Kim 96346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 97cb9b0d80SMike Smith struct mtx acpi_mutex; 98fadf3fb9SJohn Baldwin struct callout acpi_sleep_timer; 99cb9b0d80SMike Smith 10089fb5af8SNate Lawson /* Bitmap of device quirks. */ 10189fb5af8SNate Lawson int acpi_quirks; 10289fb5af8SNate Lawson 103a0e73a12SJung-uk Kim /* Supported sleep states. */ 104a0e73a12SJung-uk Kim static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 105a0e73a12SJung-uk Kim 10664de8019SJohn Baldwin static void acpi_lookup(void *arg, const char *name, device_t *dev); 1076d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10815e32d5dSMike Smith static int acpi_probe(device_t dev); 10915e32d5dSMike Smith static int acpi_attach(device_t dev); 11010ce62b9SNate Lawson static int acpi_suspend(device_t dev); 11110ce62b9SNate Lawson static int acpi_resume(device_t dev); 112169b539aSNate Lawson static int acpi_shutdown(device_t dev); 1133d844eddSAndriy Gapon static device_t acpi_add_child(device_t bus, u_int order, const char *name, 114be2b1797SNate Lawson int unit); 11515e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 11610ce62b9SNate Lawson static void acpi_probe_nomatch(device_t bus, device_t child); 11710ce62b9SNate Lawson static void acpi_driver_added(device_t dev, driver_t *driver); 118be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 119be2b1797SNate Lawson uintptr_t *result); 120be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 121be2b1797SNate Lawson uintptr_t value); 12291233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 123ea233199SJohn Baldwin static void acpi_reserve_resources(device_t dev); 124adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 125ea233199SJohn Baldwin static int acpi_set_resource(device_t dev, device_t child, int type, 1262dd1bdf1SJustin Hibbits int rid, rman_res_t start, rman_res_t count); 127be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 1282dd1bdf1SJustin Hibbits int type, int *rid, rman_res_t start, rman_res_t end, 1292dd1bdf1SJustin Hibbits rman_res_t count, u_int flags); 130049dc0d1SJohn Baldwin static int acpi_adjust_resource(device_t bus, device_t child, int type, 1312dd1bdf1SJustin Hibbits struct resource *r, rman_res_t start, rman_res_t end); 132be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 133be2b1797SNate Lawson int rid, struct resource *r); 1348b28b622SNate Lawson static void acpi_delete_resource(device_t bus, device_t child, int type, 1358b28b622SNate Lawson int rid); 1361e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1371e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 138ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 139ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 140ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 141ce619b2eSNate Lawson ACPI_BUFFER *ret); 142df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 143df8d2a32SNate Lawson void *context, void **retval); 144df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 145df8d2a32SNate Lawson int max_depth, acpi_scan_cb_t user_fn, void *arg); 146a7a3177fSJung-uk Kim static int acpi_set_powerstate(device_t child, int state); 147be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 148be2b1797SNate Lawson struct isa_pnp_id *ids); 14915e32d5dSMike Smith static void acpi_probe_children(device_t bus); 150d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order); 151be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 152be2b1797SNate Lawson void *context, void **status); 153a0e73a12SJung-uk Kim static void acpi_sleep_enable(void *arg); 154a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 15500a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 15615e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 15713d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 158183c8af3SJohn Baldwin static BOOLEAN acpi_has_hid(ACPI_HANDLE handle); 159a0a15716SJung-uk Kim static void acpi_resync_clock(struct acpi_softc *sc); 160d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 161d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 162d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 16388a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 16488a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 16515e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 16615e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 167a0e73a12SJung-uk Kim static int acpi_sname2sstate(const char *sname); 168a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate); 169d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1701d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1712a18c71dSJung-uk Kim static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 172f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 173879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 174879d6c23STakanori Watanabe char *buf, size_t buflen); 175879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 176879d6c23STakanori Watanabe char *buf, size_t buflen); 177d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 178d320e05cSJohn Baldwin static void acpi_enable_pcie(void); 179d320e05cSJohn Baldwin #endif 1800d484d24SJohn Baldwin static void acpi_hint_device_unit(device_t acdev, device_t child, 1810d484d24SJohn Baldwin const char *name, int *unitp); 182ae19af49SJung-uk Kim static void acpi_reset_interfaces(device_t dev); 183879d6c23STakanori Watanabe 18415e32d5dSMike Smith static device_method_t acpi_methods[] = { 18515e32d5dSMike Smith /* Device interface */ 18615e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 18715e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 188169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 18956a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 19010ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend), 19110ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume), 19215e32d5dSMike Smith 19315e32d5dSMike Smith /* Bus interface */ 19415e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 19515e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 19610ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 19710ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added), 19815e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 19915e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 20091233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 201ea233199SJohn Baldwin DEVMETHOD(bus_set_resource, acpi_set_resource), 20291233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 20315e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 204049dc0d1SJohn Baldwin DEVMETHOD(bus_adjust_resource, acpi_adjust_resource), 20515e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 2068b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource), 207879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 208879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 20915e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 21015e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 21115e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 21215e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 2130d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 214ffcf962dSAdrian Chadd DEVMETHOD(bus_get_domain, acpi_get_domain), 21515e32d5dSMike Smith 216ce619b2eSNate Lawson /* ACPI bus */ 217ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 218ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 21910ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 220df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 221ce619b2eSNate Lawson 222bc0f2195SMike Smith /* ISA emulation */ 223bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 224bc0f2195SMike Smith 22561bfd867SSofian Brabez DEVMETHOD_END 22615e32d5dSMike Smith }; 22715e32d5dSMike Smith 22815e32d5dSMike Smith static driver_t acpi_driver = { 22915e32d5dSMike Smith "acpi", 23015e32d5dSMike Smith acpi_methods, 23115e32d5dSMike Smith sizeof(struct acpi_softc), 23215e32d5dSMike Smith }; 23315e32d5dSMike Smith 2343273b005SMike Smith static devclass_t acpi_devclass; 2356d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 236a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 23715e32d5dSMike Smith 23815e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 23915e2f34fSNate Lawson 240adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 241adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 242adad4744SNate Lawson 243bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 244bee4aa4aSNate Lawson 2455217af30SJohn Baldwin /* Holds the description of the acpi0 device. */ 2465217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 2475217af30SJohn Baldwin 248197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 2491d7b121cSNate Lawson static char acpi_ca_version[12]; 2501d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2511d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 25215e32d5dSMike Smith 25315e32d5dSMike Smith /* 254ae19af49SJung-uk Kim * Allow overriding _OSI methods. 255ae19af49SJung-uk Kim */ 256ae19af49SJung-uk Kim static char acpi_install_interface[256]; 257ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface, 258ae19af49SJung-uk Kim sizeof(acpi_install_interface)); 259ae19af49SJung-uk Kim static char acpi_remove_interface[256]; 260ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface, 261ae19af49SJung-uk Kim sizeof(acpi_remove_interface)); 262ae19af49SJung-uk Kim 2632a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */ 2642a18c71dSJung-uk Kim static int acpi_debug_objects; 2652a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 2662a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 2672a18c71dSJung-uk Kim CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 2682a18c71dSJung-uk Kim "Enable Debug objects"); 2692a18c71dSJung-uk Kim 2702a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */ 2712a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1; 2722a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 2732a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 2742a18c71dSJung-uk Kim &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 2752a18c71dSJung-uk Kim 276313a0c13SJung-uk Kim /* Ignore register widths set by FADT and use default widths instead. */ 277313a0c13SJung-uk Kim static int acpi_ignore_reg_width = 1; 278313a0c13SJung-uk Kim TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width); 279313a0c13SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN, 280313a0c13SJung-uk Kim &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT"); 281313a0c13SJung-uk Kim 282ebbed20dSAndriy Gapon #ifdef __amd64__ 2830755473bSJung-uk Kim /* Reset system clock while resuming. XXX Remove once tested. */ 2840755473bSJung-uk Kim static int acpi_reset_clock = 1; 2850755473bSJung-uk Kim TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock); 2860755473bSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW, 2870755473bSJung-uk Kim &acpi_reset_clock, 1, "Reset system clock while resuming."); 288a0a15716SJung-uk Kim #endif 2890755473bSJung-uk Kim 29039da3eb3SNate Lawson /* Allow users to override quirks. */ 29139da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 29239da3eb3SNate Lawson 29393bfd059SNate Lawson static int acpi_susp_bounce; 29493bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 29593bfd059SNate Lawson &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 29693bfd059SNate Lawson 297c9b8d77dSNate Lawson /* 2986d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2996d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 3008c0879b6SJohn Baldwin * It also cannot be unloaded, since the entire system bus hierarchy hangs 301be2b1797SNate Lawson * off it. 3026d63101aSMike Smith */ 3036d63101aSMike Smith static int 3046d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 3056d63101aSMike Smith { 3066d63101aSMike Smith switch (event) { 3076d63101aSMike Smith case MOD_LOAD: 30887b45ed5SMitsuru IWASAKI if (!cold) { 30987b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 3106d63101aSMike Smith return (EPERM); 31187b45ed5SMitsuru IWASAKI } 3126d63101aSMike Smith break; 3136d63101aSMike Smith case MOD_UNLOAD: 314f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 3156d63101aSMike Smith return (EBUSY); 316f9390180SMitsuru IWASAKI break; 3176d63101aSMike Smith default: 3186d63101aSMike Smith break; 3196d63101aSMike Smith } 3206d63101aSMike Smith return (0); 3216d63101aSMike Smith } 3226d63101aSMike Smith 3236d63101aSMike Smith /* 324bbc2815cSJohn Baldwin * Perform early initialization. 32515e32d5dSMike Smith */ 326bbc2815cSJohn Baldwin ACPI_STATUS 327bbc2815cSJohn Baldwin acpi_Startup(void) 32815e32d5dSMike Smith { 32989fb5af8SNate Lawson static int started = 0; 3302be4e471SJung-uk Kim ACPI_STATUS status; 3312be4e471SJung-uk Kim int val; 33215e32d5dSMike Smith 3331b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3341b8c233dSPeter Pentchev 335bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */ 336bbc2815cSJohn Baldwin if (started) 3372be4e471SJung-uk Kim return_VALUE (AE_OK); 338bbc2815cSJohn Baldwin started = 1; 33915e32d5dSMike Smith 340413081d7SNate Lawson /* 3412be4e471SJung-uk Kim * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 3422be4e471SJung-uk Kim * if more tables exist. 343413081d7SNate Lawson */ 3442be4e471SJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 3452be4e471SJung-uk Kim printf("ACPI: Table initialisation failed: %s\n", 3462be4e471SJung-uk Kim AcpiFormatException(status)); 3472be4e471SJung-uk Kim return_VALUE (status); 34815e32d5dSMike Smith } 3493184cf5aSNate Lawson 35089fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 3512be4e471SJung-uk Kim if (acpi_quirks == ACPI_Q_OK) 35289fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 35389fb5af8SNate Lawson 35439da3eb3SNate Lawson /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 35589fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 35689fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 35789fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 35889fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 3592be4e471SJung-uk Kim status = AE_SUPPORT; 36089fb5af8SNate Lawson } 3613184cf5aSNate Lawson 3622be4e471SJung-uk Kim return_VALUE (status); 363bbc2815cSJohn Baldwin } 364bbc2815cSJohn Baldwin 365bbc2815cSJohn Baldwin /* 3665217af30SJohn Baldwin * Detect ACPI and perform early initialisation. 367bbc2815cSJohn Baldwin */ 3685217af30SJohn Baldwin int 3695217af30SJohn Baldwin acpi_identify(void) 370bbc2815cSJohn Baldwin { 3715217af30SJohn Baldwin ACPI_TABLE_RSDP *rsdp; 3725217af30SJohn Baldwin ACPI_TABLE_HEADER *rsdt; 3735217af30SJohn Baldwin ACPI_PHYSICAL_ADDRESS paddr; 3745217af30SJohn Baldwin struct sbuf sb; 375bbc2815cSJohn Baldwin 376bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 377bbc2815cSJohn Baldwin 378bbc2815cSJohn Baldwin if (!cold) 3795217af30SJohn Baldwin return (ENXIO); 380bbc2815cSJohn Baldwin 381a8de37b0SEitan Adler /* Check that we haven't been disabled with a hint. */ 382a8de37b0SEitan Adler if (resource_disabled("acpi", 0)) 383a8de37b0SEitan Adler return (ENXIO); 384a8de37b0SEitan Adler 3855217af30SJohn Baldwin /* Check for other PM systems. */ 3865217af30SJohn Baldwin if (power_pm_get_type() != POWER_PM_TYPE_NONE && 3875217af30SJohn Baldwin power_pm_get_type() != POWER_PM_TYPE_ACPI) { 3885217af30SJohn Baldwin printf("ACPI identify failed, other PM system enabled.\n"); 3895217af30SJohn Baldwin return (ENXIO); 3905217af30SJohn Baldwin } 3914b1ff978SMark Santcroos 3922be4e471SJung-uk Kim /* Initialize root tables. */ 3932be4e471SJung-uk Kim if (ACPI_FAILURE(acpi_Startup())) { 3942be4e471SJung-uk Kim printf("ACPI: Try disabling either ACPI or apic support.\n"); 3955217af30SJohn Baldwin return (ENXIO); 3962be4e471SJung-uk Kim } 39715e32d5dSMike Smith 3985217af30SJohn Baldwin if ((paddr = AcpiOsGetRootPointer()) == 0 || 3995217af30SJohn Baldwin (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 4005217af30SJohn Baldwin return (ENXIO); 4015217af30SJohn Baldwin if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 4025217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 4035217af30SJohn Baldwin else 4045217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 4055217af30SJohn Baldwin AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 4065217af30SJohn Baldwin 4075217af30SJohn Baldwin if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 4085217af30SJohn Baldwin return (ENXIO); 4095217af30SJohn Baldwin sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 4105217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 4115217af30SJohn Baldwin sbuf_trim(&sb); 4125217af30SJohn Baldwin sbuf_putc(&sb, ' '); 4135217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 4145217af30SJohn Baldwin sbuf_trim(&sb); 4155217af30SJohn Baldwin sbuf_finish(&sb); 4165217af30SJohn Baldwin sbuf_delete(&sb); 4175217af30SJohn Baldwin AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 4185217af30SJohn Baldwin 4195217af30SJohn Baldwin snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 4205217af30SJohn Baldwin 4215217af30SJohn Baldwin return (0); 42215e32d5dSMike Smith } 42315e32d5dSMike Smith 42415e32d5dSMike Smith /* 425bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message. 42615e32d5dSMike Smith */ 42715e32d5dSMike Smith static int 42815e32d5dSMike Smith acpi_probe(device_t dev) 42915e32d5dSMike Smith { 43015e32d5dSMike Smith 431b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 432f9390180SMitsuru IWASAKI 4335217af30SJohn Baldwin device_set_desc(dev, acpi_desc); 434bee4aa4aSNate Lawson 435feeec74dSNathan Whitehorn return_VALUE (BUS_PROBE_NOWILDCARD); 43615e32d5dSMike Smith } 43715e32d5dSMike Smith 43815e32d5dSMike Smith static int 43915e32d5dSMike Smith acpi_attach(device_t dev) 44015e32d5dSMike Smith { 44115e32d5dSMike Smith struct acpi_softc *sc; 442cb9b0d80SMike Smith ACPI_STATUS status; 443ea27c63eSNate Lawson int error, state; 44432d18aa5SMike Smith UINT32 flags; 445ea27c63eSNate Lawson UINT8 TypeA, TypeB; 446498d464fSMitsuru IWASAKI char *env; 44715e32d5dSMike Smith 448b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 449bee4aa4aSNate Lawson 45015e32d5dSMike Smith sc = device_get_softc(dev); 45115e32d5dSMike Smith sc->acpi_dev = dev; 452fd90e2edSJung-uk Kim callout_init(&sc->susp_force_to, 1); 45315e32d5dSMike Smith 4542be4e471SJung-uk Kim error = ENXIO; 4552be4e471SJung-uk Kim 45691233413SNate Lawson /* Initialize resource manager. */ 45791233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 45891233413SNate Lawson acpi_rman_io.rm_start = 0; 45991233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 4600bba6acfSJohn Baldwin acpi_rman_io.rm_descr = "ACPI I/O ports"; 46191233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 46291233413SNate Lawson panic("acpi rman_init IO ports failed"); 46391233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 4640bba6acfSJohn Baldwin acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 46591233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 46691233413SNate Lawson panic("acpi rman_init memory failed"); 46791233413SNate Lawson 4682be4e471SJung-uk Kim /* Initialise the ACPI mutex */ 4692be4e471SJung-uk Kim mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 4702be4e471SJung-uk Kim 4712be4e471SJung-uk Kim /* 4722be4e471SJung-uk Kim * Set the globals from our tunables. This is needed because ACPI-CA 4732be4e471SJung-uk Kim * uses UINT8 for some values and we have no tunable_byte. 4742be4e471SJung-uk Kim */ 4752a18c71dSJung-uk Kim AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE; 4762a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 477313a0c13SJung-uk Kim AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE; 4782a18c71dSJung-uk Kim 4792a18c71dSJung-uk Kim #ifndef ACPI_DEBUG 4802a18c71dSJung-uk Kim /* 4812a18c71dSJung-uk Kim * Disable all debugging layers and levels. 4822a18c71dSJung-uk Kim */ 4832a18c71dSJung-uk Kim AcpiDbgLayer = 0; 4842a18c71dSJung-uk Kim AcpiDbgLevel = 0; 4852a18c71dSJung-uk Kim #endif 4862be4e471SJung-uk Kim 4872be4e471SJung-uk Kim /* Start up the ACPI CA subsystem. */ 4882be4e471SJung-uk Kim status = AcpiInitializeSubsystem(); 4892be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4902be4e471SJung-uk Kim device_printf(dev, "Could not initialize Subsystem: %s\n", 4912be4e471SJung-uk Kim AcpiFormatException(status)); 4922be4e471SJung-uk Kim goto out; 4932be4e471SJung-uk Kim } 4942be4e471SJung-uk Kim 495ae19af49SJung-uk Kim /* Override OS interfaces if the user requested. */ 496ae19af49SJung-uk Kim acpi_reset_interfaces(dev); 497ae19af49SJung-uk Kim 4982be4e471SJung-uk Kim /* Load ACPI name space. */ 4992be4e471SJung-uk Kim status = AcpiLoadTables(); 5002be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 5012be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n", 5022be4e471SJung-uk Kim AcpiFormatException(status)); 5032be4e471SJung-uk Kim goto out; 5042be4e471SJung-uk Kim } 5052be4e471SJung-uk Kim 506d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 507d320e05cSJohn Baldwin /* Handle MCFG table if present. */ 508d320e05cSJohn Baldwin acpi_enable_pcie(); 509d320e05cSJohn Baldwin #endif 510d320e05cSJohn Baldwin 51115e32d5dSMike Smith /* 512be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 513be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 514be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 515be2b1797SNate Lawson * object init pass. 51615e32d5dSMike Smith * 51732d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 51832d18aa5SMike Smith * 519be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 520be2b1797SNate Lawson * all our child devices, but on many systems it works here. 52115e32d5dSMike Smith */ 52232d18aa5SMike Smith flags = 0; 523d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 52432d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 525bee4aa4aSNate Lawson 526bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 527b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 528be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 529be2b1797SNate Lawson AcpiFormatException(status)); 530cb9b0d80SMike Smith goto out; 53115e32d5dSMike Smith } 53215e32d5dSMike Smith 533f8335e3aSNate Lawson /* 534f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 535f8335e3aSNate Lawson * the namespace has been evaluated. 536da72d149SNate Lawson * 537da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and 538da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't 539da72d149SNate Lawson * a problem but should be addressed eventually. 540f8335e3aSNate Lawson */ 541f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 542f8335e3aSNate Lawson 543bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 544b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 545be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 546be2b1797SNate Lawson AcpiFormatException(status)); 547b69ed3f4SMitsuru IWASAKI goto out; 548b69ed3f4SMitsuru IWASAKI } 549b69ed3f4SMitsuru IWASAKI 55015e32d5dSMike Smith /* 5511d073b1dSJohn Baldwin * Setup our sysctl tree. 5521d073b1dSJohn Baldwin * 5531d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5541d073b1dSJohn Baldwin */ 5551d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5561d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5571d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5581d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5591d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 560d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 561ad73b301SAdrian Chadd 0, 0, acpi_supported_sleep_state_sysctl, "A", 562ad73b301SAdrian Chadd "List supported ACPI sleep states."); 563d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5641d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 565ad73b301SAdrian Chadd &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", 566ad73b301SAdrian Chadd "Power button ACPI sleep state."); 5671d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5681d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 569ad73b301SAdrian Chadd &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", 570ad73b301SAdrian Chadd "Sleep button ACPI sleep state."); 5711d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5721d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 573ad73b301SAdrian Chadd &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", 574ad73b301SAdrian Chadd "Lid ACPI sleep state. Set to S3 if you want to suspend your laptop when close the Lid."); 575f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 576f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 577f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 578f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 579f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 580f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5812d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 582197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 58331f301d0SChristian Brueffer "sleep delay in seconds"); 584ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 585197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 5861611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 587197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 588d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 589d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 590d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 591d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 592d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW, 593d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 594bf0c18ecSNate Lawson 595bf0c18ecSNate Lawson /* 5962b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 597bf0c18ecSNate Lawson * stabilize. 598bf0c18ecSNate Lawson */ 5992b9609abSNate Lawson sc->acpi_sleep_delay = 1; 6002d644607SMitsuru IWASAKI if (bootverbose) 6012d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 6022be111bfSDavide Italiano if ((env = kern_getenv("hw.acpi.verbose")) != NULL) { 603965a34fbSNate Lawson if (strcmp(env, "0") != 0) 604498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 605498d464fSMitsuru IWASAKI freeenv(env); 606498d464fSMitsuru IWASAKI } 6071d073b1dSJohn Baldwin 608ac731af5SJung-uk Kim /* Only enable reboot by default if the FADT says it is available. */ 609ac731af5SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) 610ac731af5SJung-uk Kim sc->acpi_handle_reboot = 1; 611ac731af5SJung-uk Kim 612fe5d5d7dSAndrew Turner #if !ACPI_REDUCED_HARDWARE 6132d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 614d8c37c3aSAndrew Turner if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 6152d610c46SNate Lawson sc->acpi_s4bios = 1; 616fe5d5d7dSAndrew Turner #endif 6172d610c46SNate Lawson 618a0e73a12SJung-uk Kim /* Probe all supported sleep states. */ 619a0e73a12SJung-uk Kim acpi_sleep_states[ACPI_STATE_S0] = TRUE; 620a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 621efcc2a30SJung-uk Kim if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT, 622efcc2a30SJung-uk Kim __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) && 623efcc2a30SJung-uk Kim ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 624a0e73a12SJung-uk Kim acpi_sleep_states[state] = TRUE; 625a0e73a12SJung-uk Kim 6261d073b1dSJohn Baldwin /* 627ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 628a0e73a12SJung-uk Kim * to UNKNOWN by default to avoid surprising users. 62915e32d5dSMike Smith */ 630a0e73a12SJung-uk Kim sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 631a0e73a12SJung-uk Kim ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 632a0e73a12SJung-uk Kim sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 633a0e73a12SJung-uk Kim sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 634a0e73a12SJung-uk Kim ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 635a0e73a12SJung-uk Kim sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 636a0e73a12SJung-uk Kim ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 63715e32d5dSMike Smith 638ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 639a0e73a12SJung-uk Kim sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 64000a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 641a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) { 642ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 643ea27c63eSNate Lawson break; 644ea27c63eSNate Lawson } 645ea27c63eSNate Lawson 64613d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 64715e32d5dSMike Smith 64815e32d5dSMike Smith /* 64915e32d5dSMike Smith * Scan the namespace and attach/initialise children. 65015e32d5dSMike Smith */ 65115e32d5dSMike Smith 65259e472e9SNate Lawson /* Register our shutdown handler. */ 653be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 654be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 65515e32d5dSMike Smith 65615e32d5dSMike Smith /* 65715e32d5dSMike Smith * Register our acpi event handlers. 65815e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 65915e32d5dSMike Smith */ 660be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 661be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 662be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 663be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 66415e32d5dSMike Smith 665be2b1797SNate Lawson /* Flag our initial states. */ 666a0e73a12SJung-uk Kim sc->acpi_enabled = TRUE; 66715e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 668a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 66915e32d5dSMike Smith 670be2b1797SNate Lawson /* Create the control device */ 671a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 6722a4689e8SRobert Watson "acpi"); 67315e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 67415e32d5dSMike Smith 675be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 676f86214b6SMitsuru IWASAKI goto out; 677f86214b6SMitsuru IWASAKI 678f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 679f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 680f9390180SMitsuru IWASAKI 68164de8019SJohn Baldwin if (!acpi_disabled("bus")) { 68264de8019SJohn Baldwin EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000); 683eeb6dba2SJohn Baldwin acpi_probe_children(dev); 68464de8019SJohn Baldwin } 685eeb6dba2SJohn Baldwin 6865a77b11bSJung-uk Kim /* Update all GPEs and enable runtime GPEs. */ 6875a77b11bSJung-uk Kim status = AcpiUpdateAllGpes(); 6885a77b11bSJung-uk Kim if (ACPI_FAILURE(status)) 6895a77b11bSJung-uk Kim device_printf(dev, "Could not update all GPEs: %s\n", 6905a77b11bSJung-uk Kim AcpiFormatException(status)); 6915a77b11bSJung-uk Kim 692a0e73a12SJung-uk Kim /* Allow sleep request after a while. */ 693fadf3fb9SJohn Baldwin callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0); 694fadf3fb9SJohn Baldwin callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME, 695fadf3fb9SJohn Baldwin acpi_sleep_enable, sc); 696a0e73a12SJung-uk Kim 697cb9b0d80SMike Smith error = 0; 698cb9b0d80SMike Smith 699cb9b0d80SMike Smith out: 700cb9b0d80SMike Smith return_VALUE (error); 70115e32d5dSMike Smith } 70215e32d5dSMike Smith 703a7a3177fSJung-uk Kim static void 704a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state) 705a7a3177fSJung-uk Kim { 70644aba0f6SJung-uk Kim device_t child; 707a7a3177fSJung-uk Kim device_t *devlist; 708a7a3177fSJung-uk Kim int dstate, i, numdevs; 709a7a3177fSJung-uk Kim 710a7a3177fSJung-uk Kim if (device_get_children(dev, &devlist, &numdevs) != 0) 711a7a3177fSJung-uk Kim return; 712a7a3177fSJung-uk Kim 713a7a3177fSJung-uk Kim /* 714a7a3177fSJung-uk Kim * Retrieve and set D-state for the sleep state if _SxD is present. 715a7a3177fSJung-uk Kim * Skip children who aren't attached since they are handled separately. 716a7a3177fSJung-uk Kim */ 717a7a3177fSJung-uk Kim for (i = 0; i < numdevs; i++) { 718a7a3177fSJung-uk Kim child = devlist[i]; 719a7a3177fSJung-uk Kim dstate = state; 720a7a3177fSJung-uk Kim if (device_is_attached(child) && 72144aba0f6SJung-uk Kim acpi_device_pwr_for_sleep(dev, child, &dstate) == 0) 722a7a3177fSJung-uk Kim acpi_set_powerstate(child, dstate); 723a7a3177fSJung-uk Kim } 724a7a3177fSJung-uk Kim free(devlist, M_TEMP); 725a7a3177fSJung-uk Kim } 726a7a3177fSJung-uk Kim 727169b539aSNate Lawson static int 72810ce62b9SNate Lawson acpi_suspend(device_t dev) 72910ce62b9SNate Lawson { 730a7a3177fSJung-uk Kim int error; 73110ce62b9SNate Lawson 732a56fe095SJohn Baldwin GIANT_REQUIRED; 733a56fe095SJohn Baldwin 73410ce62b9SNate Lawson error = bus_generic_suspend(dev); 735a7a3177fSJung-uk Kim if (error == 0) 736a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D3); 73710ce62b9SNate Lawson 73810ce62b9SNate Lawson return (error); 73910ce62b9SNate Lawson } 74010ce62b9SNate Lawson 74110ce62b9SNate Lawson static int 74210ce62b9SNate Lawson acpi_resume(device_t dev) 74310ce62b9SNate Lawson { 74410ce62b9SNate Lawson 745a56fe095SJohn Baldwin GIANT_REQUIRED; 746a56fe095SJohn Baldwin 747a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D0); 74810ce62b9SNate Lawson 74910ce62b9SNate Lawson return (bus_generic_resume(dev)); 75010ce62b9SNate Lawson } 75110ce62b9SNate Lawson 75210ce62b9SNate Lawson static int 753169b539aSNate Lawson acpi_shutdown(device_t dev) 754169b539aSNate Lawson { 755169b539aSNate Lawson 756a56fe095SJohn Baldwin GIANT_REQUIRED; 757a56fe095SJohn Baldwin 7580e414868SNate Lawson /* Allow children to shutdown first. */ 7590e414868SNate Lawson bus_generic_shutdown(dev); 7600e414868SNate Lawson 761bee4aa4aSNate Lawson /* 762bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 763bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 764bee4aa4aSNate Lawson */ 765d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 766d4b9ff91SNate Lawson 767169b539aSNate Lawson return (0); 768169b539aSNate Lawson } 769169b539aSNate Lawson 77015e32d5dSMike Smith /* 77115e32d5dSMike Smith * Handle a new device being added 77215e32d5dSMike Smith */ 77315e32d5dSMike Smith static device_t 7743d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit) 77515e32d5dSMike Smith { 77615e32d5dSMike Smith struct acpi_device *ad; 77715e32d5dSMike Smith device_t child; 77815e32d5dSMike Smith 779be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 78015e32d5dSMike Smith return (NULL); 78115e32d5dSMike Smith 78215e32d5dSMike Smith resource_list_init(&ad->ad_rl); 78315e32d5dSMike Smith 78415e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 78515e32d5dSMike Smith if (child != NULL) 78615e32d5dSMike Smith device_set_ivars(child, ad); 78743ce1c77SNate Lawson else 78843ce1c77SNate Lawson free(ad, M_ACPIDEV); 78915e32d5dSMike Smith return (child); 79015e32d5dSMike Smith } 79115e32d5dSMike Smith 79215e32d5dSMike Smith static int 79315e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 79415e32d5dSMike Smith { 79515e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 79615e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 79715e32d5dSMike Smith int retval = 0; 79815e32d5dSMike Smith 79915e32d5dSMike Smith retval += bus_print_child_header(bus, child); 800da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx"); 801da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx"); 802da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 803da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd"); 804a91c5fa8SNate Lawson if (device_get_flags(child)) 805a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 806ffcf962dSAdrian Chadd retval += bus_print_child_domain(bus, child); 8072c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 80815e32d5dSMike Smith 80915e32d5dSMike Smith return (retval); 81015e32d5dSMike Smith } 81115e32d5dSMike Smith 81210ce62b9SNate Lawson /* 81310ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt 81410ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added. 81510ce62b9SNate Lawson * 81610ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and 81710ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect 81810ce62b9SNate Lawson * them to be powered up. 81910ce62b9SNate Lawson */ 82010ce62b9SNate Lawson static void 82110ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child) 82210ce62b9SNate Lawson { 82396cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 824a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 82596cf0b6dSWarner Losh #endif 82610ce62b9SNate Lawson } 82710ce62b9SNate Lawson 82810ce62b9SNate Lawson /* 82910ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up. 83010ce62b9SNate Lawson * 83110ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details). 83210ce62b9SNate Lawson */ 83310ce62b9SNate Lawson static void 83410ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver) 83510ce62b9SNate Lawson { 83610ce62b9SNate Lawson device_t child, *devlist; 83710ce62b9SNate Lawson int i, numdevs; 83810ce62b9SNate Lawson 83910ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev); 840099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs)) 841099ea4b5SWarner Losh return; 84210ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 84310ce62b9SNate Lawson child = devlist[i]; 84410ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) { 84596cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 846a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D0); 84710ce62b9SNate Lawson if (device_probe_and_attach(child) != 0) 848a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 84996cf0b6dSWarner Losh #else 85096cf0b6dSWarner Losh device_probe_and_attach(child); 85196cf0b6dSWarner Losh #endif 85210ce62b9SNate Lawson } 85310ce62b9SNate Lawson } 85410ce62b9SNate Lawson free(devlist, M_TEMP); 85510ce62b9SNate Lawson } 85610ce62b9SNate Lawson 85763600cc3SNate Lawson /* Location hint for devctl(8) */ 85863600cc3SNate Lawson static int 859247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 860247648afSTakanori Watanabe size_t buflen) 861247648afSTakanori Watanabe { 862247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 8639cf5a6aaSAdrian Chadd char buf2[32]; 8649cf5a6aaSAdrian Chadd int pxm; 865247648afSTakanori Watanabe 8669cf5a6aaSAdrian Chadd if (dinfo->ad_handle) { 867d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 8689cf5a6aaSAdrian Chadd if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) { 8699cf5a6aaSAdrian Chadd snprintf(buf2, 32, " _PXM=%d", pxm); 8709cf5a6aaSAdrian Chadd strlcat(buf, buf2, buflen); 8719cf5a6aaSAdrian Chadd } 8729cf5a6aaSAdrian Chadd } else { 873d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 8749cf5a6aaSAdrian Chadd } 875247648afSTakanori Watanabe return (0); 876247648afSTakanori Watanabe } 877247648afSTakanori Watanabe 87863600cc3SNate Lawson /* PnP information for devctl(8) */ 87963600cc3SNate Lawson static int 880247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 881247648afSTakanori Watanabe size_t buflen) 882247648afSTakanori Watanabe { 88363600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 88492488a57SJung-uk Kim ACPI_DEVICE_INFO *adinfo; 885247648afSTakanori Watanabe 88692488a57SJung-uk Kim if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 887d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 88892488a57SJung-uk Kim return (0); 88992488a57SJung-uk Kim } 89092488a57SJung-uk Kim 891247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 892247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 89392488a57SJung-uk Kim adinfo->HardwareId.String : "none", 89463600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 89592488a57SJung-uk Kim strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 896247648afSTakanori Watanabe AcpiOsFree(adinfo); 897247648afSTakanori Watanabe 898247648afSTakanori Watanabe return (0); 899247648afSTakanori Watanabe } 900247648afSTakanori Watanabe 901247648afSTakanori Watanabe /* 90215e32d5dSMike Smith * Handle per-device ivars 90315e32d5dSMike Smith */ 90415e32d5dSMike Smith static int 90515e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 90615e32d5dSMike Smith { 90715e32d5dSMike Smith struct acpi_device *ad; 90815e32d5dSMike Smith 90915e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9102d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 91115e32d5dSMike Smith return (ENOENT); 91215e32d5dSMike Smith } 91315e32d5dSMike Smith 914be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 91515e32d5dSMike Smith switch(index) { 91615e32d5dSMike Smith case ACPI_IVAR_HANDLE: 91715e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 91815e32d5dSMike Smith break; 91915e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 92015e32d5dSMike Smith *(void **)result = ad->ad_private; 92115e32d5dSMike Smith break; 922d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 923d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 924d4b9ff91SNate Lawson break; 92532d18aa5SMike Smith case ISA_IVAR_VENDORID: 92632d18aa5SMike Smith case ISA_IVAR_SERIAL: 92732d18aa5SMike Smith case ISA_IVAR_COMPATID: 92832d18aa5SMike Smith *(int *)result = -1; 92932d18aa5SMike Smith break; 93032d18aa5SMike Smith case ISA_IVAR_LOGICALID: 93132d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 93232d18aa5SMike Smith break; 93315e32d5dSMike Smith default: 93415e32d5dSMike Smith return (ENOENT); 93515e32d5dSMike Smith } 936be2b1797SNate Lawson 93715e32d5dSMike Smith return (0); 93815e32d5dSMike Smith } 93915e32d5dSMike Smith 94015e32d5dSMike Smith static int 94115e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 94215e32d5dSMike Smith { 94315e32d5dSMike Smith struct acpi_device *ad; 94415e32d5dSMike Smith 94515e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9462d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 94715e32d5dSMike Smith return (ENOENT); 94815e32d5dSMike Smith } 94915e32d5dSMike Smith 95015e32d5dSMike Smith switch(index) { 95115e32d5dSMike Smith case ACPI_IVAR_HANDLE: 95215e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 95315e32d5dSMike Smith break; 95415e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 95515e32d5dSMike Smith ad->ad_private = (void *)value; 95615e32d5dSMike Smith break; 957d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 958d4b9ff91SNate Lawson ad->ad_flags = (int)value; 959d4b9ff91SNate Lawson break; 96015e32d5dSMike Smith default: 9612ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 96215e32d5dSMike Smith return (ENOENT); 96315e32d5dSMike Smith } 964be2b1797SNate Lawson 96515e32d5dSMike Smith return (0); 96615e32d5dSMike Smith } 96715e32d5dSMike Smith 96815e32d5dSMike Smith /* 96915e32d5dSMike Smith * Handle child resource allocation/removal 97015e32d5dSMike Smith */ 97191233413SNate Lawson static struct resource_list * 97291233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 97315e32d5dSMike Smith { 97491233413SNate Lawson struct acpi_device *ad; 97515e32d5dSMike Smith 97691233413SNate Lawson ad = device_get_ivars(child); 97791233413SNate Lawson return (&ad->ad_rl); 97815e32d5dSMike Smith } 97915e32d5dSMike Smith 9800d484d24SJohn Baldwin static int 9810d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value) 9820d484d24SJohn Baldwin { 9830d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev); 9840d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 9850d484d24SJohn Baldwin struct resource_list_entry *rle; 9860d484d24SJohn Baldwin 9870d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 9880d484d24SJohn Baldwin if (rle->type != type) 9890d484d24SJohn Baldwin continue; 9900d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value) 9910d484d24SJohn Baldwin return (1); 9920d484d24SJohn Baldwin } 9930d484d24SJohn Baldwin return (0); 9940d484d24SJohn Baldwin } 9950d484d24SJohn Baldwin 9960d484d24SJohn Baldwin /* 9970d484d24SJohn Baldwin * Wire device unit numbers based on resource matches in hints. 9980d484d24SJohn Baldwin */ 9990d484d24SJohn Baldwin static void 10000d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 10010d484d24SJohn Baldwin int *unitp) 10020d484d24SJohn Baldwin { 10030d484d24SJohn Baldwin const char *s; 10040d484d24SJohn Baldwin long value; 10050d484d24SJohn Baldwin int line, matches, unit; 10060d484d24SJohn Baldwin 10070d484d24SJohn Baldwin /* 10080d484d24SJohn Baldwin * Iterate over all the hints for the devices with the specified 10090d484d24SJohn Baldwin * name to see if one's resources are a subset of this device. 10100d484d24SJohn Baldwin */ 10110d484d24SJohn Baldwin line = 0; 10120d484d24SJohn Baldwin for (;;) { 10130d484d24SJohn Baldwin if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 10140d484d24SJohn Baldwin break; 10150d484d24SJohn Baldwin 10160d484d24SJohn Baldwin /* Must have an "at" for acpi or isa. */ 10170d484d24SJohn Baldwin resource_string_value(name, unit, "at", &s); 10180d484d24SJohn Baldwin if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 10190d484d24SJohn Baldwin strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 10200d484d24SJohn Baldwin continue; 10210d484d24SJohn Baldwin 10220d484d24SJohn Baldwin /* 10232fcd493cSJohn Baldwin * Check for matching resources. We must have at least one match. 10242fcd493cSJohn Baldwin * Since I/O and memory resources cannot be shared, if we get a 10252fcd493cSJohn Baldwin * match on either of those, ignore any mismatches in IRQs or DRQs. 10260d484d24SJohn Baldwin * 10270d484d24SJohn Baldwin * XXX: We may want to revisit this to be more lenient and wire 10280d484d24SJohn Baldwin * as long as it gets one match. 10290d484d24SJohn Baldwin */ 10300d484d24SJohn Baldwin matches = 0; 10310d484d24SJohn Baldwin if (resource_long_value(name, unit, "port", &value) == 0) { 10322fcd493cSJohn Baldwin /* 10332fcd493cSJohn Baldwin * Floppy drive controllers are notorious for having a 10342fcd493cSJohn Baldwin * wide variety of resources not all of which include the 10352fcd493cSJohn Baldwin * first port that is specified by the hint (typically 10362fcd493cSJohn Baldwin * 0x3f0) (see the comment above fdc_isa_alloc_resources() 10372fcd493cSJohn Baldwin * in fdc_isa.c). However, they do all seem to include 10382fcd493cSJohn Baldwin * port + 2 (e.g. 0x3f2) so for a floppy device, look for 10392fcd493cSJohn Baldwin * 'value + 2' in the port resources instead of the hint 10402fcd493cSJohn Baldwin * value. 10412fcd493cSJohn Baldwin */ 10422fcd493cSJohn Baldwin if (strcmp(name, "fdc") == 0) 10432fcd493cSJohn Baldwin value += 2; 10440d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 10450d484d24SJohn Baldwin matches++; 10460d484d24SJohn Baldwin else 10470d484d24SJohn Baldwin continue; 10480d484d24SJohn Baldwin } 10490d484d24SJohn Baldwin if (resource_long_value(name, unit, "maddr", &value) == 0) { 10500d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 10510d484d24SJohn Baldwin matches++; 10520d484d24SJohn Baldwin else 10530d484d24SJohn Baldwin continue; 10540d484d24SJohn Baldwin } 10552fcd493cSJohn Baldwin if (matches > 0) 10562fcd493cSJohn Baldwin goto matched; 10570d484d24SJohn Baldwin if (resource_long_value(name, unit, "irq", &value) == 0) { 10580d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 10590d484d24SJohn Baldwin matches++; 10600d484d24SJohn Baldwin else 10610d484d24SJohn Baldwin continue; 10620d484d24SJohn Baldwin } 10630d484d24SJohn Baldwin if (resource_long_value(name, unit, "drq", &value) == 0) { 10640d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 10650d484d24SJohn Baldwin matches++; 10660d484d24SJohn Baldwin else 10670d484d24SJohn Baldwin continue; 10680d484d24SJohn Baldwin } 10690d484d24SJohn Baldwin 10702fcd493cSJohn Baldwin matched: 10710d484d24SJohn Baldwin if (matches > 0) { 10720d484d24SJohn Baldwin /* We have a winner! */ 10730d484d24SJohn Baldwin *unitp = unit; 10740d484d24SJohn Baldwin break; 10750d484d24SJohn Baldwin } 10760d484d24SJohn Baldwin } 10770d484d24SJohn Baldwin } 10780d484d24SJohn Baldwin 1079adad4744SNate Lawson /* 10805d18c60aSAdrian Chadd * Fetch the VM domain for the given device 'dev'. 10815d18c60aSAdrian Chadd * 10825d18c60aSAdrian Chadd * Return 1 + domain if there's a domain, 0 if not found; 10835d18c60aSAdrian Chadd * -1 upon an error. 10845d18c60aSAdrian Chadd */ 10855d18c60aSAdrian Chadd int 10865d18c60aSAdrian Chadd acpi_parse_pxm(device_t dev, int *domain) 10875d18c60aSAdrian Chadd { 108862d70a81SJohn Baldwin #ifdef DEVICE_NUMA 10895d18c60aSAdrian Chadd ACPI_HANDLE h; 10905d18c60aSAdrian Chadd int d, pxm; 10915d18c60aSAdrian Chadd 10925d18c60aSAdrian Chadd h = acpi_get_handle(dev); 10935d18c60aSAdrian Chadd if ((h != NULL) && 10945d18c60aSAdrian Chadd ACPI_SUCCESS(acpi_GetInteger(h, "_PXM", &pxm))) { 10955d18c60aSAdrian Chadd d = acpi_map_pxm_to_vm_domainid(pxm); 10965d18c60aSAdrian Chadd if (d < 0) 10975d18c60aSAdrian Chadd return (-1); 10985d18c60aSAdrian Chadd *domain = d; 10995d18c60aSAdrian Chadd return (1); 11005d18c60aSAdrian Chadd } 11015d18c60aSAdrian Chadd #endif 11025d18c60aSAdrian Chadd 11035d18c60aSAdrian Chadd return (0); 11045d18c60aSAdrian Chadd } 11055d18c60aSAdrian Chadd 11065d18c60aSAdrian Chadd /* 110715ae88baSJohn Baldwin * Fetch the NUMA domain for the given device. 1108ffcf962dSAdrian Chadd * 1109ffcf962dSAdrian Chadd * If a device has a _PXM method, map that to a NUMA domain. 1110ffcf962dSAdrian Chadd * 1111ffcf962dSAdrian Chadd * If none is found, then it'll call the parent method. 1112ffcf962dSAdrian Chadd * If there's no domain, return ENOENT. 1113ffcf962dSAdrian Chadd */ 1114ffcf962dSAdrian Chadd int 1115ffcf962dSAdrian Chadd acpi_get_domain(device_t dev, device_t child, int *domain) 1116ffcf962dSAdrian Chadd { 11175d18c60aSAdrian Chadd int ret; 1118ffcf962dSAdrian Chadd 11195d18c60aSAdrian Chadd ret = acpi_parse_pxm(child, domain); 11205d18c60aSAdrian Chadd /* Error */ 11215d18c60aSAdrian Chadd if (ret == -1) 1122ffcf962dSAdrian Chadd return (ENOENT); 11235d18c60aSAdrian Chadd /* Found */ 11245d18c60aSAdrian Chadd if (ret == 1) 1125ffcf962dSAdrian Chadd return (0); 11265d18c60aSAdrian Chadd 1127ffcf962dSAdrian Chadd /* No _PXM node; go up a level */ 1128ffcf962dSAdrian Chadd return (bus_generic_get_domain(dev, child, domain)); 1129ffcf962dSAdrian Chadd } 1130ffcf962dSAdrian Chadd 1131ffcf962dSAdrian Chadd /* 1132adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1133adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 1134adad4744SNate Lawson */ 1135adad4744SNate Lawson static int 1136adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 1137adad4744SNate Lawson { 1138adad4744SNate Lawson struct resource *res; 1139adad4744SNate Lawson struct resource_list *rl; 1140adad4744SNate Lawson struct resource_list_entry *rle; 1141adad4744SNate Lawson struct rman *rm; 1142da72d149SNate Lawson char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1143da72d149SNate Lawson device_t *children; 1144da72d149SNate Lawson int child_count, i; 1145da72d149SNate Lawson 1146da72d149SNate Lawson /* 1147da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we 1148da72d149SNate Lawson * had multi-pass probe/attach. 1149da72d149SNate Lawson */ 1150da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0) 1151da72d149SNate Lawson return (ENXIO); 1152da72d149SNate Lawson for (i = 0; i < child_count; i++) { 1153da72d149SNate Lawson if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1154da72d149SNate Lawson device_probe_and_attach(children[i]); 1155da72d149SNate Lawson } 1156da72d149SNate Lawson free(children, M_TEMP); 1157adad4744SNate Lawson 1158adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1159be1bf4d2SPoul-Henning Kamp STAILQ_FOREACH(rle, rl, link) { 1160adad4744SNate Lawson if (rle->res != NULL) { 1161da1b038aSJustin Hibbits device_printf(dev, "duplicate resource for %jx\n", rle->start); 1162adad4744SNate Lawson continue; 1163adad4744SNate Lawson } 1164adad4744SNate Lawson 1165adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 1166adad4744SNate Lawson switch (rle->type) { 1167adad4744SNate Lawson case SYS_RES_IOPORT: 1168adad4744SNate Lawson rm = &acpi_rman_io; 1169adad4744SNate Lawson break; 1170adad4744SNate Lawson case SYS_RES_MEMORY: 1171adad4744SNate Lawson rm = &acpi_rman_mem; 1172adad4744SNate Lawson break; 1173adad4744SNate Lawson default: 1174adad4744SNate Lawson continue; 1175adad4744SNate Lawson } 1176adad4744SNate Lawson 1177adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 1178adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1179adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1180adad4744SNate Lawson if (res != NULL) { 1181adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1182adad4744SNate Lawson rle->res = res; 118344947d3aSJohn Baldwin } else if (bootverbose) 1184da1b038aSJustin Hibbits device_printf(dev, "reservation of %jx, %jx (%d) failed\n", 1185adad4744SNate Lawson rle->start, rle->count, rle->type); 1186adad4744SNate Lawson } 1187adad4744SNate Lawson return (0); 1188adad4744SNate Lawson } 1189adad4744SNate Lawson 1190ea233199SJohn Baldwin static char *pcilink_ids[] = { "PNP0C0F", NULL }; 1191ea233199SJohn Baldwin static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1192ea233199SJohn Baldwin 1193ea233199SJohn Baldwin /* 1194ea233199SJohn Baldwin * Reserve declared resources for devices found during attach once system 1195ea233199SJohn Baldwin * resources have been allocated. 1196ea233199SJohn Baldwin */ 1197ea233199SJohn Baldwin static void 1198ea233199SJohn Baldwin acpi_reserve_resources(device_t dev) 1199ea233199SJohn Baldwin { 1200ea233199SJohn Baldwin struct resource_list_entry *rle; 1201ea233199SJohn Baldwin struct resource_list *rl; 1202ea233199SJohn Baldwin struct acpi_device *ad; 1203ea233199SJohn Baldwin struct acpi_softc *sc; 1204ea233199SJohn Baldwin device_t *children; 1205ea233199SJohn Baldwin int child_count, i; 1206ea233199SJohn Baldwin 1207ea233199SJohn Baldwin sc = device_get_softc(dev); 1208ea233199SJohn Baldwin if (device_get_children(dev, &children, &child_count) != 0) 1209ea233199SJohn Baldwin return; 1210ea233199SJohn Baldwin for (i = 0; i < child_count; i++) { 1211ea233199SJohn Baldwin ad = device_get_ivars(children[i]); 1212ea233199SJohn Baldwin rl = &ad->ad_rl; 1213ea233199SJohn Baldwin 1214ea233199SJohn Baldwin /* Don't reserve system resources. */ 1215ea233199SJohn Baldwin if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1216ea233199SJohn Baldwin continue; 1217ea233199SJohn Baldwin 1218ea233199SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 1219ea233199SJohn Baldwin /* 1220ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things 1221ea233199SJohn Baldwin * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET 1222ea233199SJohn Baldwin * when using legacy routing). 1223ea233199SJohn Baldwin */ 1224ea233199SJohn Baldwin if (rle->type == SYS_RES_IRQ) 1225ea233199SJohn Baldwin continue; 1226ea233199SJohn Baldwin 1227ea233199SJohn Baldwin /* 12280d81cf12SJohn Baldwin * Don't reserve the resource if it is already allocated. 12290d81cf12SJohn Baldwin * The acpi_ec(4) driver can allocate its resources early 12300d81cf12SJohn Baldwin * if ECDT is present. 12310d81cf12SJohn Baldwin */ 12320d81cf12SJohn Baldwin if (rle->res != NULL) 12330d81cf12SJohn Baldwin continue; 12340d81cf12SJohn Baldwin 12350d81cf12SJohn Baldwin /* 1236ea233199SJohn Baldwin * Try to reserve the resource from our parent. If this 1237ea233199SJohn Baldwin * fails because the resource is a system resource, just 1238ea233199SJohn Baldwin * let it be. The resource range is already reserved so 1239ea233199SJohn Baldwin * that other devices will not use it. If the driver 1240ea233199SJohn Baldwin * needs to allocate the resource, then 1241ea233199SJohn Baldwin * acpi_alloc_resource() will sub-alloc from the system 1242ea233199SJohn Baldwin * resource. 1243ea233199SJohn Baldwin */ 1244ea233199SJohn Baldwin resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid, 1245ea233199SJohn Baldwin rle->start, rle->end, rle->count, 0); 1246ea233199SJohn Baldwin } 1247ea233199SJohn Baldwin } 1248ea233199SJohn Baldwin free(children, M_TEMP); 1249ea233199SJohn Baldwin sc->acpi_resources_reserved = 1; 1250ea233199SJohn Baldwin } 1251ea233199SJohn Baldwin 1252ea233199SJohn Baldwin static int 1253ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid, 12542dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t count) 1255ea233199SJohn Baldwin { 1256ea233199SJohn Baldwin struct acpi_softc *sc = device_get_softc(dev); 1257ea233199SJohn Baldwin struct acpi_device *ad = device_get_ivars(child); 1258ea233199SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 12597e3343bfSJohn Baldwin ACPI_DEVICE_INFO *devinfo; 12602dd1bdf1SJustin Hibbits rman_res_t end; 1261ea233199SJohn Baldwin 1262ea233199SJohn Baldwin /* Ignore IRQ resources for PCI link devices. */ 1263ea233199SJohn Baldwin if (type == SYS_RES_IRQ && ACPI_ID_PROBE(dev, child, pcilink_ids) != NULL) 1264ea233199SJohn Baldwin return (0); 1265ea233199SJohn Baldwin 12667e3343bfSJohn Baldwin /* 1267d2dc06caSJohn Baldwin * Ignore most resources for PCI root bridges. Some BIOSes 12687e3343bfSJohn Baldwin * incorrectly enumerate the memory ranges they decode as plain 1269d2dc06caSJohn Baldwin * memory resources instead of as ResourceProducer ranges. Other 1270d2dc06caSJohn Baldwin * BIOSes incorrectly list system resource entries for I/O ranges 1271d2dc06caSJohn Baldwin * under the PCI bridge. Do allow the one known-correct case on 1272d2dc06caSJohn Baldwin * x86 of a PCI bridge claiming the I/O ports used for PCI config 1273d2dc06caSJohn Baldwin * access. 12747e3343bfSJohn Baldwin */ 1275d2dc06caSJohn Baldwin if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 12767e3343bfSJohn Baldwin if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) { 12777e3343bfSJohn Baldwin if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) { 1278d2dc06caSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1279d2dc06caSJohn Baldwin if (!(type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT)) 1280d2dc06caSJohn Baldwin #endif 1281d2dc06caSJohn Baldwin { 12827e3343bfSJohn Baldwin AcpiOsFree(devinfo); 12837e3343bfSJohn Baldwin return (0); 12847e3343bfSJohn Baldwin } 1285d2dc06caSJohn Baldwin } 12867e3343bfSJohn Baldwin AcpiOsFree(devinfo); 12877e3343bfSJohn Baldwin } 12887e3343bfSJohn Baldwin } 12897e3343bfSJohn Baldwin 1290ea233199SJohn Baldwin /* If the resource is already allocated, fail. */ 1291ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) 1292ea233199SJohn Baldwin return (EBUSY); 1293ea233199SJohn Baldwin 1294ea233199SJohn Baldwin /* If the resource is already reserved, release it. */ 1295ea233199SJohn Baldwin if (resource_list_reserved(rl, type, rid)) 1296ea233199SJohn Baldwin resource_list_unreserve(rl, dev, child, type, rid); 1297ea233199SJohn Baldwin 1298ea233199SJohn Baldwin /* Add the resource. */ 1299ea233199SJohn Baldwin end = (start + count - 1); 1300ea233199SJohn Baldwin resource_list_add(rl, type, rid, start, end, count); 1301ea233199SJohn Baldwin 1302ea233199SJohn Baldwin /* Don't reserve resources until the system resources are allocated. */ 1303ea233199SJohn Baldwin if (!sc->acpi_resources_reserved) 1304ea233199SJohn Baldwin return (0); 1305ea233199SJohn Baldwin 1306ea233199SJohn Baldwin /* Don't reserve system resources. */ 1307ea233199SJohn Baldwin if (ACPI_ID_PROBE(dev, child, sysres_ids) != NULL) 1308ea233199SJohn Baldwin return (0); 1309ea233199SJohn Baldwin 1310ea233199SJohn Baldwin /* 1311ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things to 1312ea233199SJohn Baldwin * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when 1313ea233199SJohn Baldwin * using legacy routing). 1314ea233199SJohn Baldwin */ 1315ea233199SJohn Baldwin if (type == SYS_RES_IRQ) 1316ea233199SJohn Baldwin return (0); 1317ea233199SJohn Baldwin 1318ea233199SJohn Baldwin /* 1319ea233199SJohn Baldwin * Reserve the resource. 1320ea233199SJohn Baldwin * 1321ea233199SJohn Baldwin * XXX: Ignores failure for now. Failure here is probably a 1322ea233199SJohn Baldwin * BIOS/firmware bug? 1323ea233199SJohn Baldwin */ 1324ea233199SJohn Baldwin resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0); 1325ea233199SJohn Baldwin return (0); 1326ea233199SJohn Baldwin } 1327ea233199SJohn Baldwin 132815e32d5dSMike Smith static struct resource * 132915e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 13302dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 133115e32d5dSMike Smith { 133295957f62SJohn Baldwin ACPI_RESOURCE ares; 1333ea233199SJohn Baldwin struct acpi_device *ad; 133491233413SNate Lawson struct resource_list_entry *rle; 1335ea233199SJohn Baldwin struct resource_list *rl; 133691233413SNate Lawson struct resource *res; 13377915adb5SJustin Hibbits int isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 133815e2f34fSNate Lawson 133991233413SNate Lawson /* 1340ea233199SJohn Baldwin * First attempt at allocating the resource. For direct children, 1341ea233199SJohn Baldwin * use resource_list_alloc() to handle reserved resources. For 1342f6133e71SJohn Baldwin * other devices, pass the request up to our parent. 134391233413SNate Lawson */ 1344ea233199SJohn Baldwin if (bus == device_get_parent(child)) { 1345ea233199SJohn Baldwin ad = device_get_ivars(child); 1346ea233199SJohn Baldwin rl = &ad->ad_rl; 134791233413SNate Lawson 1348397c30a8SJohn Baldwin /* 1349ea233199SJohn Baldwin * Simulate the behavior of the ISA bus for direct children 1350ea233199SJohn Baldwin * devices. That is, if a non-default range is specified for 1351ea233199SJohn Baldwin * a resource that doesn't exist, use bus_set_resource() to 1352ea233199SJohn Baldwin * add the resource before allocating it. Note that these 1353ea233199SJohn Baldwin * resources will not be reserved. 1354397c30a8SJohn Baldwin */ 1355ea233199SJohn Baldwin if (!isdefault && resource_list_find(rl, type, *rid) == NULL) 1356ea233199SJohn Baldwin resource_list_add(rl, type, *rid, start, end, count); 1357ea233199SJohn Baldwin res = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 1358ea233199SJohn Baldwin flags); 1359ea233199SJohn Baldwin if (res != NULL && type == SYS_RES_IRQ) { 136095957f62SJohn Baldwin /* 136195957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 136295957f62SJohn Baldwin * configure the interrupt associated with a device when we 136395957f62SJohn Baldwin * parse the resources but have to defer it until a driver 136495957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 136595957f62SJohn Baldwin * 136695957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 136795957f62SJohn Baldwin */ 136895957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 136995957f62SJohn Baldwin acpi_config_intr(child, &ares); 137095957f62SJohn Baldwin } 137115e2f34fSNate Lawson 1372ea233199SJohn Baldwin /* 1373ea233199SJohn Baldwin * If this is an allocation of the "default" range for a given 1374ea233199SJohn Baldwin * RID, fetch the exact bounds for this resource from the 1375ea233199SJohn Baldwin * resource list entry to try to allocate the range from the 1376ea233199SJohn Baldwin * system resource regions. 1377ea233199SJohn Baldwin */ 1378ea233199SJohn Baldwin if (res == NULL && isdefault) { 1379ea233199SJohn Baldwin rle = resource_list_find(rl, type, *rid); 1380ea233199SJohn Baldwin if (rle != NULL) { 1381ea233199SJohn Baldwin start = rle->start; 1382ea233199SJohn Baldwin end = rle->end; 1383ea233199SJohn Baldwin count = rle->count; 1384ea233199SJohn Baldwin } 1385ea233199SJohn Baldwin } 1386ea233199SJohn Baldwin } else 1387ea233199SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1388ea233199SJohn Baldwin start, end, count, flags); 1389ea233199SJohn Baldwin 1390ea233199SJohn Baldwin /* 1391ea233199SJohn Baldwin * If the first attempt failed and this is an allocation of a 1392ea233199SJohn Baldwin * specific range, try to satisfy the request via a suballocation 13935d0d779bSJohn Baldwin * from our system resource regions. 1394ea233199SJohn Baldwin */ 13955d0d779bSJohn Baldwin if (res == NULL && start + count - 1 == end) 13965d0d779bSJohn Baldwin res = acpi_alloc_sysres(child, type, rid, start, end, count, flags); 13975d0d779bSJohn Baldwin return (res); 13985d0d779bSJohn Baldwin } 13995d0d779bSJohn Baldwin 14005d0d779bSJohn Baldwin /* 14015d0d779bSJohn Baldwin * Attempt to allocate a specific resource range from the system 14025d0d779bSJohn Baldwin * resource ranges. Note that we only handle memory and I/O port 14035d0d779bSJohn Baldwin * system resources. 14045d0d779bSJohn Baldwin */ 14055d0d779bSJohn Baldwin struct resource * 14062dd1bdf1SJustin Hibbits acpi_alloc_sysres(device_t child, int type, int *rid, rman_res_t start, 14072dd1bdf1SJustin Hibbits rman_res_t end, rman_res_t count, u_int flags) 14085d0d779bSJohn Baldwin { 14095d0d779bSJohn Baldwin struct rman *rm; 14105d0d779bSJohn Baldwin struct resource *res; 14115d0d779bSJohn Baldwin 1412ea233199SJohn Baldwin switch (type) { 1413ea233199SJohn Baldwin case SYS_RES_IOPORT: 1414ea233199SJohn Baldwin rm = &acpi_rman_io; 1415ea233199SJohn Baldwin break; 1416ea233199SJohn Baldwin case SYS_RES_MEMORY: 1417ea233199SJohn Baldwin rm = &acpi_rman_mem; 1418ea233199SJohn Baldwin break; 1419ea233199SJohn Baldwin default: 1420ea233199SJohn Baldwin return (NULL); 1421ea233199SJohn Baldwin } 1422ea233199SJohn Baldwin 14235d0d779bSJohn Baldwin KASSERT(start + count - 1 == end, ("wildcard resource range")); 1424ea233199SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1425ea233199SJohn Baldwin child); 1426ea233199SJohn Baldwin if (res == NULL) 1427ea233199SJohn Baldwin return (NULL); 1428ea233199SJohn Baldwin 1429ea233199SJohn Baldwin rman_set_rid(res, *rid); 1430ea233199SJohn Baldwin 1431ea233199SJohn Baldwin /* If requested, activate the resource using the parent's method. */ 1432ea233199SJohn Baldwin if (flags & RF_ACTIVE) 1433ea233199SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 1434ea233199SJohn Baldwin rman_release_resource(res); 1435ea233199SJohn Baldwin return (NULL); 1436ea233199SJohn Baldwin } 1437ea233199SJohn Baldwin 143891233413SNate Lawson return (res); 143915e32d5dSMike Smith } 144015e32d5dSMike Smith 144115e32d5dSMike Smith static int 1442049dc0d1SJohn Baldwin acpi_is_resource_managed(int type, struct resource *r) 144315e32d5dSMike Smith { 144415e32d5dSMike Smith 1445397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1446397c30a8SJohn Baldwin switch (type) { 1447397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1448049dc0d1SJohn Baldwin return (rman_is_region_manager(r, &acpi_rman_io)); 1449397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1450049dc0d1SJohn Baldwin return (rman_is_region_manager(r, &acpi_rman_mem)); 1451397c30a8SJohn Baldwin } 1452049dc0d1SJohn Baldwin return (0); 1453049dc0d1SJohn Baldwin } 1454049dc0d1SJohn Baldwin 1455049dc0d1SJohn Baldwin static int 1456049dc0d1SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 14572dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 1458049dc0d1SJohn Baldwin { 1459049dc0d1SJohn Baldwin 1460049dc0d1SJohn Baldwin if (acpi_is_resource_managed(type, r)) 1461049dc0d1SJohn Baldwin return (rman_adjust_resource(r, start, end)); 1462049dc0d1SJohn Baldwin return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 1463049dc0d1SJohn Baldwin } 1464049dc0d1SJohn Baldwin 1465049dc0d1SJohn Baldwin static int 1466049dc0d1SJohn Baldwin acpi_release_resource(device_t bus, device_t child, int type, int rid, 1467049dc0d1SJohn Baldwin struct resource *r) 1468049dc0d1SJohn Baldwin { 1469049dc0d1SJohn Baldwin int ret; 1470397c30a8SJohn Baldwin 147191233413SNate Lawson /* 1472397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers, 1473ea233199SJohn Baldwin * deactivate it and release it to the local pool. 147491233413SNate Lawson */ 1475049dc0d1SJohn Baldwin if (acpi_is_resource_managed(type, r)) { 147691233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 147791233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 147891233413SNate Lawson if (ret != 0) 147991233413SNate Lawson return (ret); 148015e32d5dSMike Smith } 1481ea233199SJohn Baldwin return (rman_release_resource(r)); 1482ea233199SJohn Baldwin } 1483ea233199SJohn Baldwin 1484ea233199SJohn Baldwin return (bus_generic_rl_release_resource(bus, child, type, rid, r)); 1485ea233199SJohn Baldwin } 148615e32d5dSMike Smith 14878b28b622SNate Lawson static void 14888b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid) 14898b28b622SNate Lawson { 14908b28b622SNate Lawson struct resource_list *rl; 14918b28b622SNate Lawson 14928b28b622SNate Lawson rl = acpi_get_rlist(bus, child); 1493ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) { 1494ea233199SJohn Baldwin device_printf(bus, "delete_resource: Resource still owned by child" 1495ea233199SJohn Baldwin " (type=%d, rid=%d)\n", type, rid); 1496ea233199SJohn Baldwin return; 1497ea233199SJohn Baldwin } 1498ea233199SJohn Baldwin resource_list_unreserve(rl, bus, child, type, rid); 14998b28b622SNate Lawson resource_list_delete(rl, type, rid); 15008b28b622SNate Lawson } 15018b28b622SNate Lawson 1502dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 1503e1c4bf3fSNate Lawson int 1504e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1505907b6777SNate Lawson struct resource **res, u_int flags) 1506dc750869SNate Lawson { 1507e1c4bf3fSNate Lawson int error, res_type; 1508dc750869SNate Lawson 1509e1c4bf3fSNate Lawson error = ENOMEM; 15108f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1511e1c4bf3fSNate Lawson return (EINVAL); 1512dc750869SNate Lawson 15138f118e25SNate Lawson /* We only support memory and IO spaces. */ 15142be4e471SJung-uk Kim switch (gas->SpaceId) { 1515dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1516e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY; 1517dc750869SNate Lawson break; 1518dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 1519e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT; 1520dc750869SNate Lawson break; 1521dc750869SNate Lawson default: 1522e1c4bf3fSNate Lawson return (EOPNOTSUPP); 1523dc750869SNate Lawson } 1524dc750869SNate Lawson 15252fe912dfSNate Lawson /* 1526f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means 1527f45fc848SNate Lawson * it is a bit field and just allocate a byte. 15282fe912dfSNate Lawson */ 15292be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8) 15302be4e471SJung-uk Kim gas->BitWidth = 8; 15312fe912dfSNate Lawson 15328f118e25SNate Lawson /* Validate the address after we're sure we support the space. */ 15332be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0) 15348f118e25SNate Lawson return (EINVAL); 15358f118e25SNate Lawson 1536e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address, 15372be4e471SJung-uk Kim gas->BitWidth / 8); 1538907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1539e1c4bf3fSNate Lawson if (*res != NULL) { 1540e1c4bf3fSNate Lawson *type = res_type; 1541e1c4bf3fSNate Lawson error = 0; 1542ca2c69c8SNate Lawson } else 1543ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid); 1544ca2c69c8SNate Lawson 1545e1c4bf3fSNate Lawson return (error); 1546dc750869SNate Lawson } 1547dc750869SNate Lawson 1548c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 15491e4925e8SNate Lawson static uint32_t 155032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 1551bc0f2195SMike Smith { 15521e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1553bc0f2195SMike Smith ACPI_HANDLE h; 155492488a57SJung-uk Kim uint32_t pnpid; 155532d18aa5SMike Smith 1556b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 155732d18aa5SMike Smith 15586fca9360SNate Lawson /* Fetch and validate the HID. */ 155992488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 156092488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 156192488a57SJung-uk Kim return_VALUE (0); 156232d18aa5SMike Smith 156392488a57SJung-uk Kim pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 156492488a57SJung-uk Kim devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 156592488a57SJung-uk Kim PNP_EISAID(devinfo->HardwareId.String) : 0; 156692488a57SJung-uk Kim AcpiOsFree(devinfo); 1567be2b1797SNate Lawson 156832d18aa5SMike Smith return_VALUE (pnpid); 156932d18aa5SMike Smith } 157032d18aa5SMike Smith 15711e4925e8SNate Lawson static int 15721e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1573b2566207STakanori Watanabe { 15741e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 15758ef1a331SJung-uk Kim ACPI_PNP_DEVICE_ID *ids; 1576b2566207STakanori Watanabe ACPI_HANDLE h; 15771e4925e8SNate Lawson uint32_t *pnpid; 157892488a57SJung-uk Kim int i, valid; 1579b2566207STakanori Watanabe 1580b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1581b2566207STakanori Watanabe 15821e4925e8SNate Lawson pnpid = cids; 1583bd189fe7SNate Lawson 15841e4925e8SNate Lawson /* Fetch and validate the CID */ 158592488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 158692488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 158792488a57SJung-uk Kim return_VALUE (0); 1588b2566207STakanori Watanabe 158992488a57SJung-uk Kim if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 159092488a57SJung-uk Kim AcpiOsFree(devinfo); 159192488a57SJung-uk Kim return_VALUE (0); 1592b2566207STakanori Watanabe } 1593b2566207STakanori Watanabe 159492488a57SJung-uk Kim if (devinfo->CompatibleIdList.Count < count) 159592488a57SJung-uk Kim count = devinfo->CompatibleIdList.Count; 159692488a57SJung-uk Kim ids = devinfo->CompatibleIdList.Ids; 159792488a57SJung-uk Kim for (i = 0, valid = 0; i < count; i++) 159892488a57SJung-uk Kim if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 159992488a57SJung-uk Kim strncmp(ids[i].String, "PNP", 3) == 0) { 160092488a57SJung-uk Kim *pnpid++ = PNP_EISAID(ids[i].String); 160192488a57SJung-uk Kim valid++; 160292488a57SJung-uk Kim } 160392488a57SJung-uk Kim AcpiOsFree(devinfo); 160492488a57SJung-uk Kim 16051e4925e8SNate Lawson return_VALUE (valid); 16061e4925e8SNate Lawson } 1607b2566207STakanori Watanabe 1608ce619b2eSNate Lawson static char * 1609ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1610ce619b2eSNate Lawson { 1611ce619b2eSNate Lawson ACPI_HANDLE h; 161292488a57SJung-uk Kim ACPI_OBJECT_TYPE t; 1613ce619b2eSNate Lawson int i; 1614ce619b2eSNate Lawson 1615ce619b2eSNate Lawson h = acpi_get_handle(dev); 161692488a57SJung-uk Kim if (ids == NULL || h == NULL) 161792488a57SJung-uk Kim return (NULL); 161892488a57SJung-uk Kim t = acpi_get_type(dev); 161992488a57SJung-uk Kim if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 1620ce619b2eSNate Lawson return (NULL); 1621ce619b2eSNate Lawson 1622ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1623ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1624ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1625ce619b2eSNate Lawson return (ids[i]); 1626ce619b2eSNate Lawson } 1627ce619b2eSNate Lawson return (NULL); 1628ce619b2eSNate Lawson } 1629ce619b2eSNate Lawson 1630ce619b2eSNate Lawson static ACPI_STATUS 1631ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1632ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1633ce619b2eSNate Lawson { 1634ce619b2eSNate Lawson ACPI_HANDLE h; 1635ce619b2eSNate Lawson 1636df8d2a32SNate Lawson if (dev == NULL) 1637df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1638df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1639ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1640ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1641ce619b2eSNate Lawson } 1642ce619b2eSNate Lawson 164362508c53SJohn Baldwin int 164410ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 164510ce62b9SNate Lawson { 164610ce62b9SNate Lawson struct acpi_softc *sc; 164710ce62b9SNate Lawson ACPI_HANDLE handle; 164810ce62b9SNate Lawson ACPI_STATUS status; 164910ce62b9SNate Lawson char sxd[8]; 165010ce62b9SNate Lawson 165110ce62b9SNate Lawson handle = acpi_get_handle(dev); 165210ce62b9SNate Lawson 165310ce62b9SNate Lawson /* 165410ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down. 165510ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when 165610ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may 165710ce62b9SNate Lawson * need special handling in their drivers. 165810ce62b9SNate Lawson */ 1659a7a3177fSJung-uk Kim if (dstate == NULL || handle == NULL || 166010ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") || 166110ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") || 166210ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") || 166310ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") || 166410ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511")) 166510ce62b9SNate Lawson return (ENXIO); 166610ce62b9SNate Lawson 166710ce62b9SNate Lawson /* 1668a7a3177fSJung-uk Kim * Override next state with the value from _SxD, if present. 1669a7a3177fSJung-uk Kim * Note illegal _S0D is evaluated because some systems expect this. 167010ce62b9SNate Lawson */ 1671a7a3177fSJung-uk Kim sc = device_get_softc(bus); 167210ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 167310ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate); 1674a7a3177fSJung-uk Kim if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1675a7a3177fSJung-uk Kim device_printf(dev, "failed to get %s on %s: %s\n", sxd, 1676a7a3177fSJung-uk Kim acpi_name(handle), AcpiFormatException(status)); 1677a7a3177fSJung-uk Kim return (ENXIO); 167810ce62b9SNate Lawson } 167910ce62b9SNate Lawson 1680a7a3177fSJung-uk Kim return (0); 168110ce62b9SNate Lawson } 168210ce62b9SNate Lawson 1683df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1684df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1685df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1686df8d2a32SNate Lawson void *arg; 1687df8d2a32SNate Lawson ACPI_HANDLE parent; 1688df8d2a32SNate Lawson }; 1689df8d2a32SNate Lawson 1690ce619b2eSNate Lawson static ACPI_STATUS 1691df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1692df8d2a32SNate Lawson { 1693df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1694df8d2a32SNate Lawson device_t dev, old_dev; 1695df8d2a32SNate Lawson ACPI_STATUS status; 1696df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1697df8d2a32SNate Lawson 1698df8d2a32SNate Lawson /* 1699df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1700df8d2a32SNate Lawson * the parent where the scan began. 1701df8d2a32SNate Lawson */ 1702df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1703df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1704df8d2a32SNate Lawson return (AE_OK); 1705df8d2a32SNate Lawson 1706df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1707df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1708df8d2a32SNate Lawson return (AE_OK); 1709df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1710df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1711df8d2a32SNate Lawson return (AE_OK); 1712df8d2a32SNate Lawson 1713df8d2a32SNate Lawson /* 1714df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1715df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1716df8d2a32SNate Lawson */ 1717df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1718df8d2a32SNate Lawson dev = old_dev; 1719df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1720df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1721df8d2a32SNate Lawson return (status); 1722df8d2a32SNate Lawson 1723df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1724df8d2a32SNate Lawson if (old_dev != NULL) { 1725df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1726df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1727df8d2a32SNate Lawson } 1728df8d2a32SNate Lawson 1729df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1730df8d2a32SNate Lawson if (dev != NULL) 1731df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1732df8d2a32SNate Lawson 1733df8d2a32SNate Lawson return (AE_OK); 1734df8d2a32SNate Lawson } 1735df8d2a32SNate Lawson 1736df8d2a32SNate Lawson static ACPI_STATUS 1737df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1738df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1739ce619b2eSNate Lawson { 1740ce619b2eSNate Lawson ACPI_HANDLE h; 1741df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1742ce619b2eSNate Lawson 1743df8d2a32SNate Lawson if (acpi_disabled("children")) 1744df8d2a32SNate Lawson return (AE_OK); 1745df8d2a32SNate Lawson 1746df8d2a32SNate Lawson if (dev == NULL) 1747df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1748df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1749ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1750df8d2a32SNate Lawson ctx.user_fn = user_fn; 1751df8d2a32SNate Lawson ctx.arg = arg; 1752df8d2a32SNate Lawson ctx.parent = h; 1753df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 17542272d050SJung-uk Kim acpi_device_scan_cb, NULL, &ctx, NULL)); 1755ce619b2eSNate Lawson } 1756ce619b2eSNate Lawson 175710ce62b9SNate Lawson /* 175810ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting 175910ce62b9SNate Lawson * device power states since it's close enough to ACPI. 176010ce62b9SNate Lawson */ 176110ce62b9SNate Lawson static int 1762a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state) 176310ce62b9SNate Lawson { 176410ce62b9SNate Lawson ACPI_HANDLE h; 176510ce62b9SNate Lawson ACPI_STATUS status; 176610ce62b9SNate Lawson 176710ce62b9SNate Lawson h = acpi_get_handle(child); 1768a0e73a12SJung-uk Kim if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 176910ce62b9SNate Lawson return (EINVAL); 177010ce62b9SNate Lawson if (h == NULL) 177110ce62b9SNate Lawson return (0); 177210ce62b9SNate Lawson 177310ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */ 177410ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state); 1775a7a3177fSJung-uk Kim if (ACPI_SUCCESS(status)) { 1776a7a3177fSJung-uk Kim if (bootverbose) 1777a7a3177fSJung-uk Kim device_printf(child, "set ACPI power state D%d on %s\n", 1778a7a3177fSJung-uk Kim state, acpi_name(h)); 1779a7a3177fSJung-uk Kim } else if (status != AE_NOT_FOUND) 1780a7a3177fSJung-uk Kim device_printf(child, 1781a7a3177fSJung-uk Kim "failed to set ACPI power state D%d on %s: %s\n", state, 1782a7a3177fSJung-uk Kim acpi_name(h), AcpiFormatException(status)); 178310ce62b9SNate Lawson 1784a7a3177fSJung-uk Kim return (0); 178510ce62b9SNate Lawson } 178610ce62b9SNate Lawson 178732d18aa5SMike Smith static int 178832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 178932d18aa5SMike Smith { 17901e4925e8SNate Lawson int result, cid_count, i; 17911e4925e8SNate Lawson uint32_t lid, cids[8]; 1792bc0f2195SMike Smith 1793b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1794bc0f2195SMike Smith 1795bc0f2195SMike Smith /* 1796bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1797bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1798bc0f2195SMike Smith * that to happen, so don't ever return it. 1799bc0f2195SMike Smith */ 1800bc0f2195SMike Smith result = ENXIO; 1801bc0f2195SMike Smith 1802be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1803b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 18041e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1805bc0f2195SMike Smith while (ids && ids->ip_id) { 18061e4925e8SNate Lawson if (lid == ids->ip_id) { 1807bc0f2195SMike Smith result = 0; 1808bc0f2195SMike Smith goto out; 1809bc0f2195SMike Smith } 18101e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 18111e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 18121e4925e8SNate Lawson result = 0; 18131e4925e8SNate Lawson goto out; 18141e4925e8SNate Lawson } 18151e4925e8SNate Lawson } 1816bc0f2195SMike Smith ids++; 1817bc0f2195SMike Smith } 1818be2b1797SNate Lawson 1819bc0f2195SMike Smith out: 18209e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 18219e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 18229e0dd54fSNate Lawson 1823bc0f2195SMike Smith return_VALUE (result); 1824bc0f2195SMike Smith } 1825bc0f2195SMike Smith 1826d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1827d320e05cSJohn Baldwin /* 1828d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for 1829d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory 1830d320e05cSJohn Baldwin * map. 1831d320e05cSJohn Baldwin */ 1832d320e05cSJohn Baldwin static void 1833d320e05cSJohn Baldwin acpi_enable_pcie(void) 1834d320e05cSJohn Baldwin { 1835d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr; 1836d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end; 1837d320e05cSJohn Baldwin ACPI_STATUS status; 1838d320e05cSJohn Baldwin 1839d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1840d320e05cSJohn Baldwin if (ACPI_FAILURE(status)) 1841d320e05cSJohn Baldwin return; 1842d320e05cSJohn Baldwin 1843d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1844d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1845d320e05cSJohn Baldwin while (alloc < end) { 1846d320e05cSJohn Baldwin if (alloc->PciSegment == 0) { 1847d320e05cSJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1848d320e05cSJohn Baldwin alloc->EndBusNumber); 1849d320e05cSJohn Baldwin return; 1850d320e05cSJohn Baldwin } 1851d320e05cSJohn Baldwin alloc++; 1852d320e05cSJohn Baldwin } 1853d320e05cSJohn Baldwin } 1854d320e05cSJohn Baldwin #endif 1855d320e05cSJohn Baldwin 1856bc0f2195SMike Smith /* 185733332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices. 185815e32d5dSMike Smith * 185933332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 186033332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 186133332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link 186233332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the 186333332dc2SNate Lawson * type of namespace nodes, so this should be ok. 186415e32d5dSMike Smith */ 186515e32d5dSMike Smith static void 186615e32d5dSMike Smith acpi_probe_children(device_t bus) 186715e32d5dSMike Smith { 186815e32d5dSMike Smith 1869b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 18700ae55423SMike Smith 187115e32d5dSMike Smith /* 187215e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1873edc13633SNate Lawson * we find. We also probe/attach any early devices. 187415e32d5dSMike Smith * 187515e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1876be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1877be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1878be2b1797SNate Lawson * devices as they appear, which might be smarter.) 187915e32d5dSMike Smith */ 18804c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 188133332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 18822272d050SJung-uk Kim NULL, bus, NULL); 188315e32d5dSMike Smith 1884adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1885adad4744SNate Lawson acpi_sysres_alloc(bus); 1886adad4744SNate Lawson 1887ea233199SJohn Baldwin /* Reserve resources already allocated to children. */ 1888ea233199SJohn Baldwin acpi_reserve_resources(bus); 1889ea233199SJohn Baldwin 1890edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1891edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1892edc13633SNate Lawson bus_generic_probe(bus); 1893edc13633SNate Lawson 18945e66b8e2SJohn Baldwin /* Probe/attach all children, created statically and from the namespace. */ 18950430ba9eSAndriy Gapon ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 189615e32d5dSMike Smith bus_generic_attach(bus); 18970ae55423SMike Smith 189888a79fc0SNate Lawson /* Attach wake sysctls. */ 18995c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 190088a79fc0SNate Lawson 1901bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 19020ae55423SMike Smith return_VOID; 190315e32d5dSMike Smith } 190415e32d5dSMike Smith 1905b82ca9a9SNate Lawson /* 1906d227204dSJohn Baldwin * Determine the probe order for a given device. 1907b82ca9a9SNate Lawson */ 1908d227204dSJohn Baldwin static void 1909b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 191091233413SNate Lawson { 1911463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type; 191291233413SNate Lawson 1913b82ca9a9SNate Lawson /* 1914404b0d10SJung-uk Kim * 0. CPUs 1915404b0d10SJung-uk Kim * 1. I/O port and memory system resource holders 1916404b0d10SJung-uk Kim * 2. Clocks and timers (to handle early accesses) 1917b1f9b996SAndriy Gapon * 3. Embedded controllers (to handle early accesses) 1918b1f9b996SAndriy Gapon * 4. PCI Link Devices 1919b82ca9a9SNate Lawson */ 1920463e0f91SJohn Baldwin AcpiGetType(handle, &type); 1921aa835160SAndriy Gapon if (type == ACPI_TYPE_PROCESSOR) 1922404b0d10SJung-uk Kim *order = 0; 1923404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0C01") || 1924404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0C02")) 192591233413SNate Lawson *order = 1; 1926404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0100") || 1927404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0103") || 1928404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0B00")) 192991233413SNate Lawson *order = 2; 1930aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C09")) 1931b460c6f8SJohn Baldwin *order = 3; 1932aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C0F")) 1933aa835160SAndriy Gapon *order = 4; 193491233413SNate Lawson } 193591233413SNate Lawson 193615e32d5dSMike Smith /* 193715e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 193815e32d5dSMike Smith * it. 193915e32d5dSMike Smith */ 194015e32d5dSMike Smith static ACPI_STATUS 194115e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 194215e32d5dSMike Smith { 19435a77b11bSJung-uk Kim struct acpi_prw_data prw; 194415e32d5dSMike Smith ACPI_OBJECT_TYPE type; 1945858a52f4SMitsuru IWASAKI ACPI_HANDLE h; 194633332dc2SNate Lawson device_t bus, child; 1947495a4144SJung-uk Kim char *handle_str; 1948e4cd9dcfSJohn Baldwin int order; 194915e32d5dSMike Smith 1950b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19510ae55423SMike Smith 1952495a4144SJung-uk Kim if (acpi_disabled("children")) 1953495a4144SJung-uk Kim return_ACPI_STATUS (AE_OK); 1954495a4144SJung-uk Kim 1955be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 19560ae55423SMike Smith if (acpi_avoid(handle)) 19570ae55423SMike Smith return_ACPI_STATUS (AE_OK); 19580ae55423SMike Smith 195991233413SNate Lawson bus = (device_t)context; 1960b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 1961495a4144SJung-uk Kim handle_str = acpi_name(handle); 196215e32d5dSMike Smith switch (type) { 196315e32d5dSMike Smith case ACPI_TYPE_DEVICE: 1964495a4144SJung-uk Kim /* 1965495a4144SJung-uk Kim * Since we scan from \, be sure to skip system scope objects. 1966495a4144SJung-uk Kim * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around 1967affa1826SJung-uk Kim * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run 1968495a4144SJung-uk Kim * during the intialization and \_TZ_ is to support Notify() on it. 1969495a4144SJung-uk Kim */ 1970495a4144SJung-uk Kim if (strcmp(handle_str, "\\_SB_") == 0 || 1971495a4144SJung-uk Kim strcmp(handle_str, "\\_TZ_") == 0) 1972495a4144SJung-uk Kim break; 19735a77b11bSJung-uk Kim if (acpi_parse_prw(handle, &prw) == 0) 19745a77b11bSJung-uk Kim AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit); 1975183c8af3SJohn Baldwin 1976183c8af3SJohn Baldwin /* 1977183c8af3SJohn Baldwin * Ignore devices that do not have a _HID or _CID. They should 1978183c8af3SJohn Baldwin * be discovered by other buses (e.g. the PCI bus driver). 1979183c8af3SJohn Baldwin */ 1980183c8af3SJohn Baldwin if (!acpi_has_hid(handle)) 1981183c8af3SJohn Baldwin break; 1982495a4144SJung-uk Kim /* FALLTHROUGH */ 198315e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 198415e32d5dSMike Smith case ACPI_TYPE_THERMAL: 198515e32d5dSMike Smith case ACPI_TYPE_POWER: 198633332dc2SNate Lawson /* 1987463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the 1988463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run 1989463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1990463e0f91SJohn Baldwin * are reserved for special objects (i.e., system 1991b1f9b996SAndriy Gapon * resources). 199215e32d5dSMike Smith */ 199333332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1994404b0d10SJung-uk Kim order = level * 10 + ACPI_DEV_BASE_ORDER; 1995da72d149SNate Lawson acpi_probe_order(handle, &order); 1996e4cd9dcfSJohn Baldwin child = BUS_ADD_CHILD(bus, order, NULL, -1); 1997bc0f2195SMike Smith if (child == NULL) 1998bc0f2195SMike Smith break; 199959a890e6SNate Lawson 200059a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 200115e32d5dSMike Smith acpi_set_handle(child, handle); 200259a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 2003bc0f2195SMike Smith 2004bc0f2195SMike Smith /* 2005b519f9d6SMike Smith * Check that the device is present. If it's not present, 2006b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 2007b519f9d6SMike Smith * the handle, but we don't probe it). 2008893f750aSNate Lawson * 2009893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not 2010893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them 2011893f750aSNate Lawson * anyway since we may enable them later. 2012b519f9d6SMike Smith */ 2013858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 2014858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */ 2015858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F")) 2016858a52f4SMitsuru IWASAKI break; 2017858a52f4SMitsuru IWASAKI /* 2018858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system 2019858a52f4SMitsuru IWASAKI * may be undocked at boot. 2020858a52f4SMitsuru IWASAKI */ 2021858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 2022858a52f4SMitsuru IWASAKI break; 2023858a52f4SMitsuru IWASAKI 2024b519f9d6SMike Smith device_disable(child); 2025b519f9d6SMike Smith break; 2026b519f9d6SMike Smith } 2027b519f9d6SMike Smith 2028b519f9d6SMike Smith /* 2029bc0f2195SMike Smith * Get the device's resource settings and attach them. 2030bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 2031bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 2032bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 2033bc0f2195SMike Smith * device not to have any resources. 2034bc0f2195SMike Smith */ 203572ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 20360ae55423SMike Smith break; 203715e32d5dSMike Smith } 203815e32d5dSMike Smith } 2039be2b1797SNate Lawson 20400ae55423SMike Smith return_ACPI_STATUS (AE_OK); 204115e32d5dSMike Smith } 204215e32d5dSMike Smith 204359a890e6SNate Lawson /* 204459a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 204559a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 204659a890e6SNate Lawson */ 204759a890e6SNate Lawson void 204892488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data) 204959a890e6SNate Lawson { 205059a890e6SNate Lawson } 205159a890e6SNate Lawson 205215e32d5dSMike Smith static void 205315e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 205415e32d5dSMike Smith { 20552d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 205671804adcSJung-uk Kim register_t intr; 2057d33f4987STakanori Watanabe ACPI_STATUS status; 2058eeb3a05fSNate Lawson 2059eeb3a05fSNate Lawson /* 206080f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 206180f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 206280f0e4c2SNate Lawson * an AP. 2063eeb3a05fSNate Lawson */ 2064eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 2065904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 2066904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 20672d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2068904bf0c2SNate Lawson AcpiFormatException(status)); 2069904bf0c2SNate Lawson return; 2070904bf0c2SNate Lawson } 20712d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Powering system off\n"); 207271804adcSJung-uk Kim intr = intr_disable(); 20731df130f1SJung-uk Kim status = AcpiEnterSleepState(ACPI_STATE_S5); 207471804adcSJung-uk Kim if (ACPI_FAILURE(status)) { 207571804adcSJung-uk Kim intr_restore(intr); 20762d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - %s\n", 20772d0c82e8SJung-uk Kim AcpiFormatException(status)); 207871804adcSJung-uk Kim } else { 207915e32d5dSMike Smith DELAY(1000000); 208071804adcSJung-uk Kim intr_restore(intr); 20812d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 208215e32d5dSMike Smith } 2083ac731af5SJung-uk Kim } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) { 2084d85e6785SNate Lawson /* Reboot using the reset register. */ 2085ac731af5SJung-uk Kim status = AcpiReset(); 2086ac731af5SJung-uk Kim if (ACPI_SUCCESS(status)) { 208787a500cdSNate Lawson DELAY(1000000); 20882d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - timeout\n"); 2089ac731af5SJung-uk Kim } else if (status != AE_NOT_EXIST) 2090ac731af5SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - %s\n", 2091ac731af5SJung-uk Kim AcpiFormatException(status)); 2092d85e6785SNate Lawson } else if (sc->acpi_do_disable && panicstr == NULL) { 2093d85e6785SNate Lawson /* 2094d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing 2095d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system. 2096d85e6785SNate Lawson */ 20972d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Shutting down\n"); 209880f0e4c2SNate Lawson AcpiTerminate(); 209980f0e4c2SNate Lawson } 210015e32d5dSMike Smith } 210115e32d5dSMike Smith 210213d4f7baSMitsuru IWASAKI static void 210313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 210413d4f7baSMitsuru IWASAKI { 210513d4f7baSMitsuru IWASAKI static int first_time = 1; 210613d4f7baSMitsuru IWASAKI 210713d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 21082be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 210959ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 211013d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 211133febf93SNate Lawson acpi_event_power_button_sleep, sc); 2112be2b1797SNate Lawson if (first_time) 21136c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 211413d4f7baSMitsuru IWASAKI } 21152be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 211659ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 211713d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 211833febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 2119be2b1797SNate Lawson if (first_time) 21206c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 212113d4f7baSMitsuru IWASAKI } 212213d4f7baSMitsuru IWASAKI 212313d4f7baSMitsuru IWASAKI first_time = 0; 212413d4f7baSMitsuru IWASAKI } 212513d4f7baSMitsuru IWASAKI 212615e32d5dSMike Smith /* 2127c5ba0be4SMike Smith * Returns true if the device is actually present and should 2128c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 2129c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 2130c5ba0be4SMike Smith */ 2131c5ba0be4SMike Smith BOOLEAN 2132c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 2133c5ba0be4SMike Smith { 21341e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 2135c5ba0be4SMike Smith ACPI_HANDLE h; 213692488a57SJung-uk Kim BOOLEAN present; 2137c5ba0be4SMike Smith 213892488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 213992488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2140c5ba0be4SMike Smith return (FALSE); 2141be2b1797SNate Lawson 2142be2b1797SNate Lawson /* If no _STA method, must be present */ 214392488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 214492488a57SJung-uk Kim ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2145be2b1797SNate Lawson 214692488a57SJung-uk Kim AcpiOsFree(devinfo); 214792488a57SJung-uk Kim return (present); 2148c5ba0be4SMike Smith } 2149c5ba0be4SMike Smith 2150c5ba0be4SMike Smith /* 2151b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 2152b53f2771SMike Smith */ 2153b53f2771SMike Smith BOOLEAN 2154b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 2155b53f2771SMike Smith { 21561e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 2157b53f2771SMike Smith ACPI_HANDLE h; 215892488a57SJung-uk Kim BOOLEAN present; 2159b53f2771SMike Smith 216092488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 216192488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2162b53f2771SMike Smith return (FALSE); 2163be2b1797SNate Lawson 2164be2b1797SNate Lawson /* If no _STA method, must be present */ 216592488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 216692488a57SJung-uk Kim ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 2167be2b1797SNate Lawson 216892488a57SJung-uk Kim AcpiOsFree(devinfo); 216992488a57SJung-uk Kim return (present); 2170b53f2771SMike Smith } 2171b53f2771SMike Smith 2172b53f2771SMike Smith /* 2173183c8af3SJohn Baldwin * Returns true if a device has at least one valid device ID. 2174183c8af3SJohn Baldwin */ 2175183c8af3SJohn Baldwin static BOOLEAN 2176183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h) 2177183c8af3SJohn Baldwin { 2178183c8af3SJohn Baldwin ACPI_DEVICE_INFO *devinfo; 2179183c8af3SJohn Baldwin BOOLEAN ret; 2180183c8af3SJohn Baldwin 2181183c8af3SJohn Baldwin if (h == NULL || 2182183c8af3SJohn Baldwin ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2183183c8af3SJohn Baldwin return (FALSE); 2184183c8af3SJohn Baldwin 2185183c8af3SJohn Baldwin ret = FALSE; 2186183c8af3SJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0) 2187183c8af3SJohn Baldwin ret = TRUE; 2188183c8af3SJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2189183c8af3SJohn Baldwin if (devinfo->CompatibleIdList.Count > 0) 2190183c8af3SJohn Baldwin ret = TRUE; 2191183c8af3SJohn Baldwin 2192183c8af3SJohn Baldwin AcpiOsFree(devinfo); 2193183c8af3SJohn Baldwin return (ret); 2194183c8af3SJohn Baldwin } 2195183c8af3SJohn Baldwin 2196183c8af3SJohn Baldwin /* 219791233413SNate Lawson * Match a HID string against a handle 219815e32d5dSMike Smith */ 21993c4c08dcSAlexander Motin BOOLEAN 2200ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 220115e32d5dSMike Smith { 22021e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 220392488a57SJung-uk Kim BOOLEAN ret; 220492488a57SJung-uk Kim int i; 220592488a57SJung-uk Kim 220692488a57SJung-uk Kim if (hid == NULL || h == NULL || 220792488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 220892488a57SJung-uk Kim return (FALSE); 220915e32d5dSMike Smith 22101e4925e8SNate Lawson ret = FALSE; 2211c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 221292488a57SJung-uk Kim strcmp(hid, devinfo->HardwareId.String) == 0) 22131e4925e8SNate Lawson ret = TRUE; 221492488a57SJung-uk Kim else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 221592488a57SJung-uk Kim for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 221692488a57SJung-uk Kim if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 22171e4925e8SNate Lawson ret = TRUE; 22181e4925e8SNate Lawson break; 22191e4925e8SNate Lawson } 22201e4925e8SNate Lawson } 2221be2b1797SNate Lawson 222292488a57SJung-uk Kim AcpiOsFree(devinfo); 22231e4925e8SNate Lawson return (ret); 222415e32d5dSMike Smith } 222515e32d5dSMike Smith 222615e32d5dSMike Smith /* 2227c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 2228c5ba0be4SMike Smith * or one if its parents. 2229c5ba0be4SMike Smith */ 2230c5ba0be4SMike Smith ACPI_STATUS 2231c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 2232c5ba0be4SMike Smith { 2233c5ba0be4SMike Smith ACPI_HANDLE r; 2234c5ba0be4SMike Smith ACPI_STATUS status; 2235c5ba0be4SMike Smith 2236be2b1797SNate Lawson /* Walk back up the tree to the root */ 2237c5ba0be4SMike Smith for (;;) { 2238be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 2239be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2240c5ba0be4SMike Smith *result = r; 2241c5ba0be4SMike Smith return (AE_OK); 2242c5ba0be4SMike Smith } 2243bee4aa4aSNate Lawson /* XXX Return error here? */ 2244c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 2245c5ba0be4SMike Smith return (AE_OK); 2246b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 2247c5ba0be4SMike Smith return (AE_NOT_FOUND); 2248c5ba0be4SMike Smith parent = r; 2249c5ba0be4SMike Smith } 2250c5ba0be4SMike Smith } 2251c5ba0be4SMike Smith 2252c5ba0be4SMike Smith /* 2253c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 2254c5ba0be4SMike Smith */ 2255c5ba0be4SMike Smith ACPI_BUFFER * 2256c5ba0be4SMike Smith acpi_AllocBuffer(int size) 2257c5ba0be4SMike Smith { 2258c5ba0be4SMike Smith ACPI_BUFFER *buf; 2259c5ba0be4SMike Smith 2260c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2261c5ba0be4SMike Smith return (NULL); 2262c5ba0be4SMike Smith buf->Length = size; 2263c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 2264c5ba0be4SMike Smith return (buf); 2265c5ba0be4SMike Smith } 2266c5ba0be4SMike Smith 2267c310653eSNate Lawson ACPI_STATUS 2268cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2269c310653eSNate Lawson { 2270c310653eSNate Lawson ACPI_OBJECT arg1; 2271c310653eSNate Lawson ACPI_OBJECT_LIST args; 2272c310653eSNate Lawson 2273c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 2274c310653eSNate Lawson arg1.Integer.Value = number; 2275c310653eSNate Lawson args.Count = 1; 2276c310653eSNate Lawson args.Pointer = &arg1; 2277c310653eSNate Lawson 2278c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 2279c310653eSNate Lawson } 2280c310653eSNate Lawson 2281c5ba0be4SMike Smith /* 2282c5ba0be4SMike Smith * Evaluate a path that should return an integer. 2283c5ba0be4SMike Smith */ 2284c5ba0be4SMike Smith ACPI_STATUS 2285cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2286c5ba0be4SMike Smith { 2287be2b1797SNate Lawson ACPI_STATUS status; 2288c5ba0be4SMike Smith ACPI_BUFFER buf; 2289c573e654SMitsuru IWASAKI ACPI_OBJECT param; 2290c5ba0be4SMike Smith 2291c5ba0be4SMike Smith if (handle == NULL) 2292c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 22930c7da7acSJohn Baldwin 22940c7da7acSJohn Baldwin /* 22950c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 22960c7da7acSJohn Baldwin * a method that will return an Integer. 22970c7da7acSJohn Baldwin */ 2298c5ba0be4SMike Smith buf.Pointer = ¶m; 2299c5ba0be4SMike Smith buf.Length = sizeof(param); 2300be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2301be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2302be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 2303c5ba0be4SMike Smith *number = param.Integer.Value; 2304be2b1797SNate Lawson else 2305be2b1797SNate Lawson status = AE_TYPE; 2306c5ba0be4SMike Smith } 23070c7da7acSJohn Baldwin 23080c7da7acSJohn Baldwin /* 23090c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 23100c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 23110c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 23120c7da7acSJohn Baldwin * convert it into an Integer as best we can. 23130c7da7acSJohn Baldwin * 23140c7da7acSJohn Baldwin * This is a hack. 23150c7da7acSJohn Baldwin */ 2316be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 2317b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2318be2b1797SNate Lawson status = AE_NO_MEMORY; 23190c7da7acSJohn Baldwin } else { 2320be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2321be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 2322be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 23230c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 23240c7da7acSJohn Baldwin } 2325f97739daSNate Lawson } 2326be2b1797SNate Lawson return (status); 2327c5ba0be4SMike Smith } 2328c5ba0be4SMike Smith 2329c573e654SMitsuru IWASAKI ACPI_STATUS 2330cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2331c573e654SMitsuru IWASAKI { 2332c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 2333dba55fa2SNate Lawson UINT8 *val; 2334c573e654SMitsuru IWASAKI int i; 2335c573e654SMitsuru IWASAKI 2336c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 2337c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 2338c573e654SMitsuru IWASAKI *number = p->Integer.Value; 2339c573e654SMitsuru IWASAKI return (AE_OK); 2340c573e654SMitsuru IWASAKI } 2341c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 2342c573e654SMitsuru IWASAKI return (AE_TYPE); 2343c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 2344c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 2345be2b1797SNate Lawson 2346c573e654SMitsuru IWASAKI *number = 0; 2347dba55fa2SNate Lawson val = p->Buffer.Pointer; 2348c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 2349dba55fa2SNate Lawson *number += val[i] << (i * 8); 2350c573e654SMitsuru IWASAKI return (AE_OK); 2351c573e654SMitsuru IWASAKI } 2352c573e654SMitsuru IWASAKI 2353c5ba0be4SMike Smith /* 2354c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 2355c5ba0be4SMike Smith * function for each element. 2356c5ba0be4SMike Smith * 2357c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 2358c5ba0be4SMike Smith */ 2359c5ba0be4SMike Smith ACPI_STATUS 2360be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2361be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2362c5ba0be4SMike Smith { 2363c5ba0be4SMike Smith ACPI_OBJECT *comp; 2364c5ba0be4SMike Smith int i; 2365c5ba0be4SMike Smith 2366be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2367c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 2368c5ba0be4SMike Smith 2369be2b1797SNate Lawson /* Iterate over components */ 2370be2b1797SNate Lawson i = 0; 2371be2b1797SNate Lawson comp = pkg->Package.Elements; 2372be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 2373c5ba0be4SMike Smith func(comp, arg); 2374c5ba0be4SMike Smith 2375c5ba0be4SMike Smith return (AE_OK); 2376c5ba0be4SMike Smith } 2377c5ba0be4SMike Smith 23786f69255bSMike Smith /* 23796f69255bSMike Smith * Find the (index)th resource object in a set. 23806f69255bSMike Smith */ 23816f69255bSMike Smith ACPI_STATUS 23826d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 23836f69255bSMike Smith { 23846d63101aSMike Smith ACPI_RESOURCE *rp; 23856f69255bSMike Smith int i; 23866f69255bSMike Smith 23876d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 23886f69255bSMike Smith i = index; 23896d63101aSMike Smith while (i-- > 0) { 2390be2b1797SNate Lawson /* Range check */ 23916d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 23926f69255bSMike Smith return (AE_BAD_PARAMETER); 2393be2b1797SNate Lawson 2394be2b1797SNate Lawson /* Check for terminator */ 2395e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 23966d63101aSMike Smith return (AE_NOT_FOUND); 2397abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 23986f69255bSMike Smith } 23996f69255bSMike Smith if (resp != NULL) 24006d63101aSMike Smith *resp = rp; 2401be2b1797SNate Lawson 24026d63101aSMike Smith return (AE_OK); 24036d63101aSMike Smith } 24046d63101aSMike Smith 24056d63101aSMike Smith /* 24066d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 24076d63101aSMike Smith * 24086d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 24096d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 24106d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 24116d63101aSMike Smith * resources. 24126d63101aSMike Smith */ 24136d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 24146d63101aSMike Smith 24156d63101aSMike Smith ACPI_STATUS 24166d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 24176d63101aSMike Smith { 24186d63101aSMike Smith ACPI_RESOURCE *rp; 24196d63101aSMike Smith void *newp; 24206d63101aSMike Smith 2421be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 24226d63101aSMike Smith if (buf->Pointer == NULL) { 24236d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 24246d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 24256d63101aSMike Smith return (AE_NO_MEMORY); 24266d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 2427e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 242860d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN; 24296d63101aSMike Smith } 24306d63101aSMike Smith if (res == NULL) 24316d63101aSMike Smith return (AE_OK); 24326d63101aSMike Smith 24336d63101aSMike Smith /* 24346d63101aSMike Smith * Scan the current buffer looking for the terminator. 24356d63101aSMike Smith * This will either find the terminator or hit the end 24366d63101aSMike Smith * of the buffer and return an error. 24376d63101aSMike Smith */ 24386d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 24396d63101aSMike Smith for (;;) { 2440be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 24416d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 24426d63101aSMike Smith return (AE_BAD_PARAMETER); 2443e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 24446d63101aSMike Smith break; 2445abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 24466d63101aSMike Smith } 24476d63101aSMike Smith 24486d63101aSMike Smith /* 24496d63101aSMike Smith * Check the size of the buffer and expand if required. 24506d63101aSMike Smith * 24516d63101aSMike Smith * Required size is: 24526d63101aSMike Smith * size of existing resources before terminator + 24536d63101aSMike Smith * size of new resource and header + 24546d63101aSMike Smith * size of terminator. 24556d63101aSMike Smith * 24566d63101aSMike Smith * Note that this loop should really only run once, unless 24576d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 24586d63101aSMike Smith */ 24596d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2460e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA + 2461e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) { 24626d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 24636d63101aSMike Smith return (AE_NO_MEMORY); 24646d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 2465a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2466a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 24676d63101aSMike Smith AcpiOsFree(buf->Pointer); 24686d63101aSMike Smith buf->Pointer = newp; 24696d63101aSMike Smith buf->Length += buf->Length; 24706d63101aSMike Smith } 24716d63101aSMike Smith 2472be2b1797SNate Lawson /* Insert the new resource. */ 2473e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 24746d63101aSMike Smith 2475be2b1797SNate Lawson /* And add the terminator. */ 2476abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 2477e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 247860d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN; 24796d63101aSMike Smith 24806f69255bSMike Smith return (AE_OK); 24816f69255bSMike Smith } 2482c5ba0be4SMike Smith 2483*5f3dd91aSJohn Baldwin ACPI_STATUS 2484*5f3dd91aSJohn Baldwin acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count, 2485*5f3dd91aSJohn Baldwin uint32_t *caps, bool query) 2486*5f3dd91aSJohn Baldwin { 2487*5f3dd91aSJohn Baldwin ACPI_OBJECT arg[4]; 2488*5f3dd91aSJohn Baldwin ACPI_OBJECT_LIST arglist; 2489*5f3dd91aSJohn Baldwin 2490*5f3dd91aSJohn Baldwin arglist.Pointer = arg; 2491*5f3dd91aSJohn Baldwin arglist.Count = 4; 2492*5f3dd91aSJohn Baldwin arg[0].Type = ACPI_TYPE_BUFFER; 2493*5f3dd91aSJohn Baldwin arg[0].Buffer.Length = ACPI_UUID_LENGTH; 2494*5f3dd91aSJohn Baldwin arg[0].Buffer.Pointer = uuid; 2495*5f3dd91aSJohn Baldwin arg[1].Type = ACPI_TYPE_INTEGER; 2496*5f3dd91aSJohn Baldwin arg[1].Integer.Value = revision; 2497*5f3dd91aSJohn Baldwin arg[2].Type = ACPI_TYPE_INTEGER; 2498*5f3dd91aSJohn Baldwin arg[2].Integer.Value = count; 2499*5f3dd91aSJohn Baldwin arg[3].Type = ACPI_TYPE_BUFFER; 2500*5f3dd91aSJohn Baldwin arg[3].Buffer.Length = count * sizeof(uint32_t); 2501*5f3dd91aSJohn Baldwin arg[3].Buffer.Pointer = (uint8_t *)caps; 2502*5f3dd91aSJohn Baldwin caps[0] = query ? 1 : 0; 2503*5f3dd91aSJohn Baldwin return (AcpiEvaluateObject(handle, "_OSC", &arglist, NULL)); 2504*5f3dd91aSJohn Baldwin } 2505*5f3dd91aSJohn Baldwin 2506da14ac9fSJohn Baldwin /* 2507da14ac9fSJohn Baldwin * Set interrupt model. 2508da14ac9fSJohn Baldwin */ 2509da14ac9fSJohn Baldwin ACPI_STATUS 2510da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 2511da14ac9fSJohn Baldwin { 2512da14ac9fSJohn Baldwin 2513c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2514da14ac9fSJohn Baldwin } 2515da14ac9fSJohn Baldwin 251600a30448SNate Lawson /* 2517d95e7f5aSJohn Baldwin * Walk subtables of a table and call a callback routine for each 2518d95e7f5aSJohn Baldwin * subtable. The caller should provide the first subtable and a 2519d95e7f5aSJohn Baldwin * pointer to the end of the table. This can be used to walk tables 2520d95e7f5aSJohn Baldwin * such as MADT and SRAT that use subtable entries. 2521d95e7f5aSJohn Baldwin */ 2522d95e7f5aSJohn Baldwin void 2523d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2524d95e7f5aSJohn Baldwin void *arg) 2525d95e7f5aSJohn Baldwin { 2526d95e7f5aSJohn Baldwin ACPI_SUBTABLE_HEADER *entry; 2527d95e7f5aSJohn Baldwin 2528d95e7f5aSJohn Baldwin for (entry = first; (void *)entry < end; ) { 2529d95e7f5aSJohn Baldwin /* Avoid an infinite loop if we hit a bogus entry. */ 2530d95e7f5aSJohn Baldwin if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2531d95e7f5aSJohn Baldwin return; 2532d95e7f5aSJohn Baldwin 2533d95e7f5aSJohn Baldwin handler(entry, arg); 2534d95e7f5aSJohn Baldwin entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2535d95e7f5aSJohn Baldwin } 2536d95e7f5aSJohn Baldwin } 2537d95e7f5aSJohn Baldwin 2538d95e7f5aSJohn Baldwin /* 253900a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be 254000a30448SNate Lawson * removed. 254100a30448SNate Lawson * 254200a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran 254300a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 254400a30448SNate Lawson */ 254500a30448SNate Lawson ACPI_STATUS 254600a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state) 254700a30448SNate Lawson { 254800a30448SNate Lawson static int once; 254900a30448SNate Lawson 255000a30448SNate Lawson if (!once) { 25512d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 255200a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n"); 255300a30448SNate Lawson once = 1; 255400a30448SNate Lawson } 255500a30448SNate Lawson return (acpi_EnterSleepState(sc, state)); 255600a30448SNate Lawson } 255700a30448SNate Lawson 2558c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 255900a30448SNate Lawson static void 2560ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context) 2561ffe3f1f8SMitsuru IWASAKI { 2562ffe3f1f8SMitsuru IWASAKI struct acpi_softc *sc = (struct acpi_softc *)context; 2563ffe3f1f8SMitsuru IWASAKI 2564ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2565ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 2566ffe3f1f8SMitsuru IWASAKI sc->acpi_next_sstate); 2567ffe3f1f8SMitsuru IWASAKI } 2568ffe3f1f8SMitsuru IWASAKI 2569ffe3f1f8SMitsuru IWASAKI static void 257000a30448SNate Lawson acpi_sleep_force(void *arg) 257100a30448SNate Lawson { 25722d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 257300a30448SNate Lawson 25742d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 25752d0c82e8SJung-uk Kim "suspend request timed out, forcing sleep now\n"); 2576ffe3f1f8SMitsuru IWASAKI /* 2577fadf3fb9SJohn Baldwin * XXX Suspending from callout causes freezes in DEVICE_SUSPEND(). 2578ffe3f1f8SMitsuru IWASAKI * Suspend from acpi_task thread instead. 2579ffe3f1f8SMitsuru IWASAKI */ 2580ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 2581ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task, sc))) 2582ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n"); 258300a30448SNate Lawson } 2584c66d2b38SJung-uk Kim #endif 258500a30448SNate Lawson 258600a30448SNate Lawson /* 258700a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm 258800a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to 258900a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all 259000a30448SNate Lawson * acks are in. 259100a30448SNate Lawson */ 259200a30448SNate Lawson int 259300a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state) 259400a30448SNate Lawson { 259571f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 259600a30448SNate Lawson struct apm_clone_data *clone; 2597337744d9SJung-uk Kim ACPI_STATUS status; 259800a30448SNate Lawson 2599a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 260000a30448SNate Lawson return (EINVAL); 2601a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 2602a0e73a12SJung-uk Kim return (EOPNOTSUPP); 260300a30448SNate Lawson 26042eb0015aSColin Percival /* 26052eb0015aSColin Percival * If a reboot/shutdown/suspend request is already in progress or 26062eb0015aSColin Percival * suspend is blocked due to an upcoming shutdown, just return. 26072eb0015aSColin Percival */ 26082eb0015aSColin Percival if (rebooting || sc->acpi_next_sstate != 0 || suspend_blocked) { 2609b12bc99fSMitsuru IWASAKI return (0); 2610b12bc99fSMitsuru IWASAKI } 2611b12bc99fSMitsuru IWASAKI 2612f22377a8SMitsuru IWASAKI /* Wait until sleep is enabled. */ 2613f22377a8SMitsuru IWASAKI while (sc->acpi_sleep_disabled) { 2614f22377a8SMitsuru IWASAKI AcpiOsSleep(1000); 2615f22377a8SMitsuru IWASAKI } 2616f22377a8SMitsuru IWASAKI 2617337744d9SJung-uk Kim ACPI_LOCK(acpi); 261800a30448SNate Lawson 2619f22377a8SMitsuru IWASAKI sc->acpi_next_sstate = state; 262000a30448SNate Lawson 2621337744d9SJung-uk Kim /* S5 (soft-off) should be entered directly with no waiting. */ 2622337744d9SJung-uk Kim if (state == ACPI_STATE_S5) { 2623337744d9SJung-uk Kim ACPI_UNLOCK(acpi); 2624337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2625337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2626337744d9SJung-uk Kim } 2627337744d9SJung-uk Kim 262800a30448SNate Lawson /* Record the pending state and notify all apm devices. */ 262900a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 263000a30448SNate Lawson clone->notify_status = APM_EV_NONE; 263100a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) { 263200a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO); 2633374d9c4dSJohn Baldwin KNOTE_LOCKED(&clone->sel_read.si_note, 0); 263400a30448SNate Lawson } 263500a30448SNate Lawson } 263600a30448SNate Lawson 26370c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */ 2638d42f0ad8SJung-uk Kim if (!devctl_process_running()) { 26390c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi); 2640337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2641337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 26420c26519eSMitsuru IWASAKI } 26430c26519eSMitsuru IWASAKI 264400a30448SNate Lawson /* 264500a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request 264600a30448SNate Lawson * in time. This way we still eventually go to sleep if we were 264700a30448SNate Lawson * overheating or running low on battery, even if userland is hung. 264800a30448SNate Lawson * We cancel this timeout once all userland acks are in or the 264900a30448SNate Lawson * suspend request is aborted. 265000a30448SNate Lawson */ 265100a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 265200a30448SNate Lawson ACPI_UNLOCK(acpi); 2653d42f0ad8SJung-uk Kim 2654d42f0ad8SJung-uk Kim /* Now notify devd(8) also. */ 2655d42f0ad8SJung-uk Kim acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2656d42f0ad8SJung-uk Kim 265700a30448SNate Lawson return (0); 2658c66d2b38SJung-uk Kim #else 2659c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2660c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2661c66d2b38SJung-uk Kim #endif 266200a30448SNate Lawson } 266300a30448SNate Lawson 266400a30448SNate Lawson /* 266500a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has 266600a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the 266700a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled 266800a30448SNate Lawson * and gives an errno value describing why. Once all votes are in, 266900a30448SNate Lawson * we suspend the system. 267000a30448SNate Lawson */ 267100a30448SNate Lawson int 267200a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error) 267300a30448SNate Lawson { 2674c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 267500a30448SNate Lawson struct acpi_softc *sc; 267600a30448SNate Lawson int ret, sleeping; 267700a30448SNate Lawson 267800a30448SNate Lawson /* If no pending sleep state, return an error. */ 267900a30448SNate Lawson ACPI_LOCK(acpi); 268000a30448SNate Lawson sc = clone->acpi_sc; 268100a30448SNate Lawson if (sc->acpi_next_sstate == 0) { 268200a30448SNate Lawson ACPI_UNLOCK(acpi); 268300a30448SNate Lawson return (ENXIO); 268400a30448SNate Lawson } 268500a30448SNate Lawson 268600a30448SNate Lawson /* Caller wants to abort suspend process. */ 268700a30448SNate Lawson if (error) { 268800a30448SNate Lawson sc->acpi_next_sstate = 0; 268900a30448SNate Lawson callout_stop(&sc->susp_force_to); 26902d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 26912d0c82e8SJung-uk Kim "listener on %s cancelled the pending suspend\n", 269200a30448SNate Lawson devtoname(clone->cdev)); 269300a30448SNate Lawson ACPI_UNLOCK(acpi); 269400a30448SNate Lawson return (0); 269500a30448SNate Lawson } 269600a30448SNate Lawson 269700a30448SNate Lawson /* 269800a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through 269900a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that 270000a30448SNate Lawson * are writable since read-only devices couldn't ack the request. 270100a30448SNate Lawson */ 270200a30448SNate Lawson sleeping = TRUE; 2703c66d2b38SJung-uk Kim clone->notify_status = APM_EV_ACKED; 270400a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 270500a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 && 270600a30448SNate Lawson clone->notify_status != APM_EV_ACKED) { 270700a30448SNate Lawson sleeping = FALSE; 270800a30448SNate Lawson break; 270900a30448SNate Lawson } 271000a30448SNate Lawson } 271100a30448SNate Lawson 271200a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */ 271300a30448SNate Lawson if (sleeping) 271400a30448SNate Lawson callout_stop(&sc->susp_force_to); 271500a30448SNate Lawson ACPI_UNLOCK(acpi); 271600a30448SNate Lawson ret = 0; 271700a30448SNate Lawson if (sleeping) { 271800a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 271900a30448SNate Lawson ret = ENODEV; 272000a30448SNate Lawson } 272100a30448SNate Lawson return (ret); 2722c66d2b38SJung-uk Kim #else 2723c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2724c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2725c66d2b38SJung-uk Kim #endif 272600a30448SNate Lawson } 272700a30448SNate Lawson 2728ece50487SMitsuru IWASAKI static void 2729ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 2730ece50487SMitsuru IWASAKI { 2731d42f0ad8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 2732bee4aa4aSNate Lawson 2733fadf3fb9SJohn Baldwin ACPI_LOCK_ASSERT(acpi); 2734fadf3fb9SJohn Baldwin 2735a0e73a12SJung-uk Kim /* Reschedule if the system is not fully up and running. */ 2736a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) { 2737fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME); 2738a0e73a12SJung-uk Kim return; 2739a0e73a12SJung-uk Kim } 2740a0e73a12SJung-uk Kim 2741a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = FALSE; 2742d42f0ad8SJung-uk Kim } 2743d42f0ad8SJung-uk Kim 2744d42f0ad8SJung-uk Kim static ACPI_STATUS 2745d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc) 2746d42f0ad8SJung-uk Kim { 2747d42f0ad8SJung-uk Kim ACPI_STATUS status; 2748d42f0ad8SJung-uk Kim 2749a0e73a12SJung-uk Kim /* Fail if the system is not fully up and running. */ 2750a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) 2751a0e73a12SJung-uk Kim return (AE_ERROR); 2752a0e73a12SJung-uk Kim 2753d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2754d42f0ad8SJung-uk Kim status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2755a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 2756d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2757d42f0ad8SJung-uk Kim 2758d42f0ad8SJung-uk Kim return (status); 2759ece50487SMitsuru IWASAKI } 2760c30382dfSMitsuru IWASAKI 276115e2f34fSNate Lawson enum acpi_sleep_state { 276215e2f34fSNate Lawson ACPI_SS_NONE, 276315e2f34fSNate Lawson ACPI_SS_GPE_SET, 276415e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 276515e2f34fSNate Lawson ACPI_SS_SLP_PREP, 276615e2f34fSNate Lawson ACPI_SS_SLEPT, 276715e2f34fSNate Lawson }; 276815e2f34fSNate Lawson 276915e32d5dSMike Smith /* 277000a30448SNate Lawson * Enter the desired system sleep state. 277115e32d5dSMike Smith * 2772be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 277315e32d5dSMike Smith */ 277400a30448SNate Lawson static ACPI_STATUS 277500a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state) 277615e32d5dSMike Smith { 277771804adcSJung-uk Kim register_t intr; 277815e2f34fSNate Lawson ACPI_STATUS status; 2779b913a7d5SAndriy Gapon ACPI_EVENT_STATUS power_button_status; 278015e2f34fSNate Lawson enum acpi_sleep_state slp_state; 2781f0a101b7SMitsuru IWASAKI int sleep_result; 278215e32d5dSMike Smith 2783b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 27840ae55423SMike Smith 2785a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 278633c70d41SAndriy Gapon return_ACPI_STATUS (AE_BAD_PARAMETER); 2787a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) { 2788a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2789a0e73a12SJung-uk Kim state); 2790a0e73a12SJung-uk Kim return (AE_SUPPORT); 2791a0e73a12SJung-uk Kim } 2792a0e73a12SJung-uk Kim 2793a0e73a12SJung-uk Kim /* Re-entry once we're suspending is not allowed. */ 2794a0e73a12SJung-uk Kim status = acpi_sleep_disable(sc); 2795a0e73a12SJung-uk Kim if (ACPI_FAILURE(status)) { 2796a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, 2797a0e73a12SJung-uk Kim "suspend request ignored (not ready yet)\n"); 2798a0e73a12SJung-uk Kim return (status); 2799a0e73a12SJung-uk Kim } 280033c70d41SAndriy Gapon 280133c70d41SAndriy Gapon if (state == ACPI_STATE_S5) { 280233c70d41SAndriy Gapon /* 280333c70d41SAndriy Gapon * Shut down cleanly and power off. This will call us back through the 280433c70d41SAndriy Gapon * shutdown handlers. 280533c70d41SAndriy Gapon */ 280633c70d41SAndriy Gapon shutdown_nice(RB_POWEROFF); 280733c70d41SAndriy Gapon return_ACPI_STATUS (AE_OK); 280833c70d41SAndriy Gapon } 280933c70d41SAndriy Gapon 281085f95fffSAndriy Gapon EVENTHANDLER_INVOKE(power_suspend_early); 281185f95fffSAndriy Gapon stop_all_proc(); 28124a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_suspend); 28134a8fa6feSJung-uk Kim 281436a483bbSJung-uk Kim if (smp_started) { 2815c66d2b38SJung-uk Kim thread_lock(curthread); 2816c66d2b38SJung-uk Kim sched_bind(curthread, 0); 2817c66d2b38SJung-uk Kim thread_unlock(curthread); 281836a483bbSJung-uk Kim } 2819c66d2b38SJung-uk Kim 2820a56fe095SJohn Baldwin /* 2821a56fe095SJohn Baldwin * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2822a56fe095SJohn Baldwin * drivers need this. 2823a56fe095SJohn Baldwin */ 2824a56fe095SJohn Baldwin mtx_lock(&Giant); 2825c66d2b38SJung-uk Kim 282615e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 282756d8cb57SMitsuru IWASAKI 2828a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 2829a1fccb47SMitsuru IWASAKI 2830d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 2831d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 283215e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 2833e8b4d56eSNate Lawson 283415e32d5dSMike Smith /* 2835c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 2836c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 283715e32d5dSMike Smith * 2838c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 2839c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 2840c7f88fc3SNate Lawson * bus interface does not provide for this. 284115e32d5dSMike Smith */ 284215e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 284315e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 284433c70d41SAndriy Gapon goto backout; 284515e2f34fSNate Lawson } 284615e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 2847b9c780d6SMitsuru IWASAKI 284893bfd059SNate Lawson /* If testing device suspend only, back out of everything here. */ 284993bfd059SNate Lawson if (acpi_susp_bounce) 285033c70d41SAndriy Gapon goto backout; 285193bfd059SNate Lawson 2852be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 2853be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2854b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2855b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 285633c70d41SAndriy Gapon goto backout; 2857b9c780d6SMitsuru IWASAKI } 285815e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 2859b9c780d6SMitsuru IWASAKI 2860be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 2861ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 2862ff01efb5SMitsuru IWASAKI 2863f0a101b7SMitsuru IWASAKI intr = intr_disable(); 28644d46ef51SJung-uk Kim if (state != ACPI_STATE_S1) { 2865f0a101b7SMitsuru IWASAKI sleep_result = acpi_sleep_machdep(sc, state); 2866f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 0); 2867b1a5c017SAndriy Gapon 2868b1a5c017SAndriy Gapon /* 2869b1a5c017SAndriy Gapon * XXX According to ACPI specification SCI_EN bit should be restored 2870b1a5c017SAndriy Gapon * by ACPI platform (BIOS, firmware) to its pre-sleep state. 2871b1a5c017SAndriy Gapon * Unfortunately some BIOSes fail to do that and that leads to 2872b1a5c017SAndriy Gapon * unexpected and serious consequences during wake up like a system 2873b1a5c017SAndriy Gapon * getting stuck in SMI handlers. 2874b1a5c017SAndriy Gapon * This hack is picked up from Linux, which claims that it follows 2875b1a5c017SAndriy Gapon * Windows behavior. 2876b1a5c017SAndriy Gapon */ 2877b1a5c017SAndriy Gapon if (sleep_result == 1 && state != ACPI_STATE_S4) 2878b1a5c017SAndriy Gapon AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT); 2879b1a5c017SAndriy Gapon 28801df130f1SJung-uk Kim AcpiLeaveSleepStatePrep(state); 2881b913a7d5SAndriy Gapon 2882b913a7d5SAndriy Gapon if (sleep_result == 1 && state == ACPI_STATE_S3) { 2883b913a7d5SAndriy Gapon /* 2884b913a7d5SAndriy Gapon * Prevent mis-interpretation of the wakeup by power button 2885b913a7d5SAndriy Gapon * as a request for power off. 2886b913a7d5SAndriy Gapon * Ideally we should post an appropriate wakeup event, 2887b913a7d5SAndriy Gapon * perhaps using acpi_event_power_button_wake or alike. 2888b913a7d5SAndriy Gapon * 2889b913a7d5SAndriy Gapon * Clearing of power button status after wakeup is mandated 2890b913a7d5SAndriy Gapon * by ACPI specification in section "Fixed Power Button". 2891b913a7d5SAndriy Gapon * 2892b913a7d5SAndriy Gapon * XXX As of ACPICA 20121114 AcpiGetEventStatus provides 2893b913a7d5SAndriy Gapon * status as 0/1 corressponding to inactive/active despite 2894b913a7d5SAndriy Gapon * its type being ACPI_EVENT_STATUS. In other words, 2895b913a7d5SAndriy Gapon * we should not test for ACPI_EVENT_FLAG_SET for time being. 2896b913a7d5SAndriy Gapon */ 2897b913a7d5SAndriy Gapon if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON, 2898b913a7d5SAndriy Gapon &power_button_status)) && power_button_status != 0) { 2899b913a7d5SAndriy Gapon AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 2900b913a7d5SAndriy Gapon device_printf(sc->acpi_dev, 2901b913a7d5SAndriy Gapon "cleared fixed power button status\n"); 2902b913a7d5SAndriy Gapon } 2903b913a7d5SAndriy Gapon } 2904b913a7d5SAndriy Gapon 2905f0a101b7SMitsuru IWASAKI intr_restore(intr); 2906f0a101b7SMitsuru IWASAKI 2907f0a101b7SMitsuru IWASAKI /* call acpi_wakeup_machdep() again with interrupt enabled */ 2908f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 1); 2909f0a101b7SMitsuru IWASAKI 2910f0a101b7SMitsuru IWASAKI if (sleep_result == -1) 2911a159c266SJung-uk Kim goto backout; 29126161544cSTakanori Watanabe 29136161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2914be2b1797SNate Lawson if (state == ACPI_STATE_S4) 2915964679ceSMitsuru IWASAKI AcpiEnable(); 29166161544cSTakanori Watanabe } else { 29171df130f1SJung-uk Kim status = AcpiEnterSleepState(state); 29181df130f1SJung-uk Kim AcpiLeaveSleepStatePrep(state); 291971804adcSJung-uk Kim intr_restore(intr); 2920be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2921be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2922be2b1797SNate Lawson AcpiFormatException(status)); 292333c70d41SAndriy Gapon goto backout; 292415e32d5dSMike Smith } 29256161544cSTakanori Watanabe } 292615e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 2927ece50487SMitsuru IWASAKI 292815e2f34fSNate Lawson /* 292915e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 293015e2f34fSNate Lawson * process. This handles both the error and success cases. 293115e2f34fSNate Lawson */ 293233c70d41SAndriy Gapon backout: 293315e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 293415e2f34fSNate Lawson acpi_wake_prep_walk(state); 293515e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 293615e2f34fSNate Lawson } 2937e3b56b66SMitsuru IWASAKI if (slp_state >= ACPI_SS_DEV_SUSPEND) 2938e3b56b66SMitsuru IWASAKI DEVICE_RESUME(root_bus); 2939f0a101b7SMitsuru IWASAKI if (slp_state >= ACPI_SS_SLP_PREP) 294015e2f34fSNate Lawson AcpiLeaveSleepState(state); 2941a0a15716SJung-uk Kim if (slp_state >= ACPI_SS_SLEPT) { 2942a0a15716SJung-uk Kim acpi_resync_clock(sc); 294315e2f34fSNate Lawson acpi_enable_fixed_events(sc); 2944a0a15716SJung-uk Kim } 2945337744d9SJung-uk Kim sc->acpi_next_sstate = 0; 294615e2f34fSNate Lawson 2947a56fe095SJohn Baldwin mtx_unlock(&Giant); 2948c66d2b38SJung-uk Kim 294936a483bbSJung-uk Kim if (smp_started) { 2950c66d2b38SJung-uk Kim thread_lock(curthread); 2951c66d2b38SJung-uk Kim sched_unbind(curthread); 2952c66d2b38SJung-uk Kim thread_unlock(curthread); 295336a483bbSJung-uk Kim } 2954c66d2b38SJung-uk Kim 295585f95fffSAndriy Gapon resume_all_proc(); 295685f95fffSAndriy Gapon 29574a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_resume); 29584a8fa6feSJung-uk Kim 2959d42f0ad8SJung-uk Kim /* Allow another sleep request after a while. */ 2960fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME); 2961d42f0ad8SJung-uk Kim 2962d42f0ad8SJung-uk Kim /* Run /etc/rc.resume after we are back. */ 2963d42f0ad8SJung-uk Kim if (devctl_process_running()) 2964d42f0ad8SJung-uk Kim acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2965d42f0ad8SJung-uk Kim 29660ae55423SMike Smith return_ACPI_STATUS (status); 296715e32d5dSMike Smith } 296815e32d5dSMike Smith 2969a0a15716SJung-uk Kim static void 29700755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc) 29710755473bSJung-uk Kim { 2972ebbed20dSAndriy Gapon #ifdef __amd64__ 29730755473bSJung-uk Kim if (!acpi_reset_clock) 29740755473bSJung-uk Kim return; 29750755473bSJung-uk Kim 29760755473bSJung-uk Kim /* 29770755473bSJung-uk Kim * Warm up timecounter again and reset system clock. 29780755473bSJung-uk Kim */ 29790755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 29800755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 29810755473bSJung-uk Kim inittodr(time_second + sc->acpi_sleep_delay); 2982a0a15716SJung-uk Kim #endif 29830755473bSJung-uk Kim } 29840755473bSJung-uk Kim 2985e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 2986e8b4d56eSNate Lawson int 2987e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 2988e8b4d56eSNate Lawson { 2989e8b4d56eSNate Lawson struct acpi_prw_data prw; 2990e8b4d56eSNate Lawson ACPI_STATUS status; 2991e8b4d56eSNate Lawson int flags; 2992e8b4d56eSNate Lawson 2993d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 29942be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2995e8b4d56eSNate Lawson return (ENXIO); 2996e8b4d56eSNate Lawson 2997d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 2998e8b4d56eSNate Lawson if (enable) { 29995a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 30005a77b11bSJung-uk Kim ACPI_GPE_ENABLE); 3001e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 3002e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 3003e8b4d56eSNate Lawson return (ENXIO); 3004e8b4d56eSNate Lawson } 3005d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 3006e8b4d56eSNate Lawson } else { 30075a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 30085a77b11bSJung-uk Kim ACPI_GPE_DISABLE); 3009e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 3010e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 3011e8b4d56eSNate Lawson return (ENXIO); 3012e8b4d56eSNate Lawson } 3013d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 3014e8b4d56eSNate Lawson } 3015e8b4d56eSNate Lawson 3016e8b4d56eSNate Lawson return (0); 3017e8b4d56eSNate Lawson } 3018e8b4d56eSNate Lawson 3019d4b9ff91SNate Lawson static int 3020d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 3021e8b4d56eSNate Lawson { 3022e8b4d56eSNate Lawson struct acpi_prw_data prw; 3023d4b9ff91SNate Lawson device_t dev; 3024e8b4d56eSNate Lawson 3025d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 3026e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 3027e8b4d56eSNate Lawson return (ENXIO); 3028d4b9ff91SNate Lawson dev = acpi_get_device(handle); 3029e8b4d56eSNate Lawson 3030e8b4d56eSNate Lawson /* 3031d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 3032d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 3033d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 3034d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 3035d4b9ff91SNate Lawson * and set _PSW. 3036e8b4d56eSNate Lawson */ 3037d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 30385a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 3039e8b4d56eSNate Lawson if (bootverbose) 3040d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 3041d4b9ff91SNate Lawson acpi_name(handle), sstate); 3042d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 3043d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 3044d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 3045d4b9ff91SNate Lawson if (bootverbose) 3046d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 3047d4b9ff91SNate Lawson acpi_name(handle), sstate); 3048d4b9ff91SNate Lawson } 3049cc85c78cSNate Lawson 3050cc85c78cSNate Lawson return (0); 3051e8b4d56eSNate Lawson } 3052e8b4d56eSNate Lawson 3053d4b9ff91SNate Lawson static int 3054d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 3055cc85c78cSNate Lawson { 3056cc85c78cSNate Lawson struct acpi_prw_data prw; 3057d4b9ff91SNate Lawson device_t dev; 3058cc85c78cSNate Lawson 3059cc85c78cSNate Lawson /* 3060d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 3061d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 3062cc85c78cSNate Lawson */ 3063d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 3064d4b9ff91SNate Lawson return (ENXIO); 3065d4b9ff91SNate Lawson dev = acpi_get_device(handle); 3066d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 3067d4b9ff91SNate Lawson return (0); 3068cc85c78cSNate Lawson 3069d4b9ff91SNate Lawson /* 3070d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 3071d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 3072d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 3073d4b9ff91SNate Lawson */ 3074d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 30755a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 3076d4b9ff91SNate Lawson if (bootverbose) 3077d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 3078d4b9ff91SNate Lawson } else { 3079d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 3080d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 3081d4b9ff91SNate Lawson if (bootverbose) 3082d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 3083d4b9ff91SNate Lawson acpi_name(handle)); 3084d4b9ff91SNate Lawson } 3085d4b9ff91SNate Lawson 3086e8b4d56eSNate Lawson return (0); 3087e8b4d56eSNate Lawson } 3088e8b4d56eSNate Lawson 3089e8b4d56eSNate Lawson static ACPI_STATUS 3090d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 3091e8b4d56eSNate Lawson { 3092d4b9ff91SNate Lawson int sstate; 3093e8b4d56eSNate Lawson 3094d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 3095d4b9ff91SNate Lawson sstate = *(int *)context; 3096d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 3097d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 3098d4b9ff91SNate Lawson else 3099d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 3100e8b4d56eSNate Lawson return (AE_OK); 3101e8b4d56eSNate Lawson } 3102e8b4d56eSNate Lawson 3103d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 3104e8b4d56eSNate Lawson static int 3105d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 3106e8b4d56eSNate Lawson { 3107e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 3108e8b4d56eSNate Lawson 3109e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 3110d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 31112272d050SJung-uk Kim acpi_wake_prep, NULL, &sstate, NULL); 3112e8b4d56eSNate Lawson return (0); 3113e8b4d56eSNate Lawson } 3114e8b4d56eSNate Lawson 311588a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 311688a79fc0SNate Lawson static int 311788a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 311888a79fc0SNate Lawson { 311988a79fc0SNate Lawson int error, i, numdevs; 312088a79fc0SNate Lawson device_t *devlist; 312188a79fc0SNate Lawson device_t child; 3122d4b9ff91SNate Lawson ACPI_STATUS status; 312388a79fc0SNate Lawson 312488a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 31251c9ec538SNate Lawson if (error != 0 || numdevs == 0) { 31261c9ec538SNate Lawson if (numdevs == 0) 31271c9ec538SNate Lawson free(devlist, M_TEMP); 312888a79fc0SNate Lawson return (error); 31291c9ec538SNate Lawson } 313088a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 313188a79fc0SNate Lawson child = devlist[i]; 3132d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 313388a79fc0SNate Lawson if (!device_is_attached(child)) 313488a79fc0SNate Lawson continue; 3135d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 3136d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 313788a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 313888a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 313988a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 314088a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 314188a79fc0SNate Lawson } 314288a79fc0SNate Lawson } 314388a79fc0SNate Lawson free(devlist, M_TEMP); 314488a79fc0SNate Lawson 314588a79fc0SNate Lawson return (0); 314688a79fc0SNate Lawson } 314788a79fc0SNate Lawson 314888a79fc0SNate Lawson /* Enable or disable wake from userland. */ 314988a79fc0SNate Lawson static int 315088a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 315188a79fc0SNate Lawson { 315288a79fc0SNate Lawson int enable, error; 315388a79fc0SNate Lawson device_t dev; 315488a79fc0SNate Lawson 315588a79fc0SNate Lawson dev = (device_t)arg1; 3156d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 315788a79fc0SNate Lawson 315888a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 315988a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 316088a79fc0SNate Lawson return (error); 316188a79fc0SNate Lawson if (enable != 0 && enable != 1) 316288a79fc0SNate Lawson return (EINVAL); 316388a79fc0SNate Lawson 316488a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 316588a79fc0SNate Lawson } 316688a79fc0SNate Lawson 3167e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 3168d4b9ff91SNate Lawson int 3169e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 3170e8b4d56eSNate Lawson { 3171e8b4d56eSNate Lawson ACPI_STATUS status; 3172e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 3173e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 3174d4b9ff91SNate Lawson int error, i, power_count; 3175e8b4d56eSNate Lawson 3176e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 3177e8b4d56eSNate Lawson return (EINVAL); 3178e8b4d56eSNate Lawson 3179e8b4d56eSNate Lawson /* 3180e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 3181e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 3182e8b4d56eSNate Lawson */ 3183e8b4d56eSNate Lawson error = EINVAL; 3184e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 3185e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 3186e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 3187e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 3188e8b4d56eSNate Lawson return (ENOENT); 3189e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 3190e8b4d56eSNate Lawson if (res == NULL) 3191e8b4d56eSNate Lawson return (ENOENT); 3192e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 3193e8b4d56eSNate Lawson goto out; 3194e8b4d56eSNate Lawson 3195e8b4d56eSNate Lawson /* 3196e8b4d56eSNate Lawson * Element 1 of the _PRW object: 3197e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 3198e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 3199e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 3200e8b4d56eSNate Lawson */ 3201e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 3202e8b4d56eSNate Lawson goto out; 3203e8b4d56eSNate Lawson 3204e8b4d56eSNate Lawson /* 3205e8b4d56eSNate Lawson * Element 0 of the _PRW object: 3206e8b4d56eSNate Lawson */ 3207e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 3208e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 3209e8b4d56eSNate Lawson /* 3210e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 3211e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 3212e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 3213e8b4d56eSNate Lawson * enabled for the wake event. 3214e8b4d56eSNate Lawson */ 3215e8b4d56eSNate Lawson prw->gpe_handle = NULL; 3216e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 3217e8b4d56eSNate Lawson error = 0; 3218e8b4d56eSNate Lawson break; 3219e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 3220e8b4d56eSNate Lawson /* 3221e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 3222e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 3223e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 3224e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 3225e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 3226e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 3227e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 3228e8b4d56eSNate Lawson * the wake event. 3229e8b4d56eSNate Lawson * 3230e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 3231e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 3232e8b4d56eSNate Lawson */ 3233e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 3234e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 3235e8b4d56eSNate Lawson goto out; 3236e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 3237e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 3238e8b4d56eSNate Lawson goto out; 3239e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 3240e8b4d56eSNate Lawson goto out; 3241e8b4d56eSNate Lawson error = 0; 3242e8b4d56eSNate Lawson break; 3243e8b4d56eSNate Lawson default: 3244e8b4d56eSNate Lawson goto out; 3245e8b4d56eSNate Lawson } 3246e8b4d56eSNate Lawson 3247d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 3248d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 3249d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 3250d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 3251d4b9ff91SNate Lawson power_count = 0; 3252d4b9ff91SNate Lawson } 3253d4b9ff91SNate Lawson prw->power_res_count = power_count; 3254d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 3255d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 3256e8b4d56eSNate Lawson 3257e8b4d56eSNate Lawson out: 3258e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 3259e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 3260e8b4d56eSNate Lawson return (error); 3261e8b4d56eSNate Lawson } 3262e8b4d56eSNate Lawson 326315e32d5dSMike Smith /* 326415e32d5dSMike Smith * ACPI Event Handlers 326515e32d5dSMike Smith */ 326615e32d5dSMike Smith 326715e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 326815e32d5dSMike Smith 326915e32d5dSMike Smith static void 327015e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 327115e32d5dSMike Smith { 32722d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 327300a30448SNate Lawson int ret; 3274bee4aa4aSNate Lawson 3275b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 327615e32d5dSMike Smith 3277a0e73a12SJung-uk Kim /* Check if button action is disabled or unknown. */ 3278a0e73a12SJung-uk Kim if (state == ACPI_STATE_UNKNOWN) 3279813d6dcaSNate Lawson return; 3280813d6dcaSNate Lawson 328100a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */ 32822d0c82e8SJung-uk Kim ret = acpi_ReqSleepState(sc, state); 328300a30448SNate Lawson if (ret != 0) 32842d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 32852d0c82e8SJung-uk Kim "request to enter state S%d failed (err %d)\n", state, ret); 3286bee4aa4aSNate Lawson 32870ae55423SMike Smith return_VOID; 328815e32d5dSMike Smith } 328915e32d5dSMike Smith 329015e32d5dSMike Smith static void 329115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 329215e32d5dSMike Smith { 3293bee4aa4aSNate Lawson 3294b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 32950ae55423SMike Smith 3296bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 3297cb9b0d80SMike Smith 32980ae55423SMike Smith return_VOID; 329915e32d5dSMike Smith } 330015e32d5dSMike Smith 330115e32d5dSMike Smith /* 330215e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 330315e32d5dSMike Smith */ 3304b5854f5fSJung-uk Kim static void 3305b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context) 3306b5854f5fSJung-uk Kim { 3307b5854f5fSJung-uk Kim 3308b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context); 3309b5854f5fSJung-uk Kim } 3310b5854f5fSJung-uk Kim 3311b5854f5fSJung-uk Kim static void 3312b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context) 3313b5854f5fSJung-uk Kim { 3314b5854f5fSJung-uk Kim 3315b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context); 3316b5854f5fSJung-uk Kim } 3317b5854f5fSJung-uk Kim 331815e32d5dSMike Smith UINT32 331933febf93SNate Lawson acpi_event_power_button_sleep(void *context) 332015e32d5dSMike Smith { 332115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 332215e32d5dSMike Smith 3323b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 33240ae55423SMike Smith 3325b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3326b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx))) 3327b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3328b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 332915e32d5dSMike Smith } 333015e32d5dSMike Smith 333115e32d5dSMike Smith UINT32 333233febf93SNate Lawson acpi_event_power_button_wake(void *context) 333315e32d5dSMike Smith { 333415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 333515e32d5dSMike Smith 3336b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 33370ae55423SMike Smith 3338b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3339b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx))) 3340b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3341b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 334215e32d5dSMike Smith } 334315e32d5dSMike Smith 334415e32d5dSMike Smith UINT32 334533febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 334615e32d5dSMike Smith { 334715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 334815e32d5dSMike Smith 3349b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 33500ae55423SMike Smith 3351b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3352b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx))) 3353b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3354b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 335515e32d5dSMike Smith } 335615e32d5dSMike Smith 335715e32d5dSMike Smith UINT32 335833febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 335915e32d5dSMike Smith { 336015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 336115e32d5dSMike Smith 3362b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 33630ae55423SMike Smith 3364b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3365b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx))) 3366b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3367b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 336815e32d5dSMike Smith } 336915e32d5dSMike Smith 337015e32d5dSMike Smith /* 3371bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 3372bee4aa4aSNate Lawson * use this for single-threaded callers. 337315e32d5dSMike Smith */ 337415e32d5dSMike Smith char * 337515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 337615e32d5dSMike Smith { 3377bee4aa4aSNate Lawson ACPI_BUFFER buf; 3378bee4aa4aSNate Lawson static char data[256]; 337915e32d5dSMike Smith 3380bee4aa4aSNate Lawson buf.Length = sizeof(data); 3381bee4aa4aSNate Lawson buf.Pointer = data; 3382cb9b0d80SMike Smith 33830a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3384bee4aa4aSNate Lawson return (data); 3385bee4aa4aSNate Lawson return ("(unknown)"); 338615e32d5dSMike Smith } 338715e32d5dSMike Smith 338815e32d5dSMike Smith /* 338915e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 339015e32d5dSMike Smith * parts of the namespace. 339115e32d5dSMike Smith */ 339215e32d5dSMike Smith int 339315e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 339415e32d5dSMike Smith { 33953c600cfbSJohn Baldwin char *cp, *env, *np; 339615e32d5dSMike Smith int len; 339715e32d5dSMike Smith 339815e32d5dSMike Smith np = acpi_name(handle); 339915e32d5dSMike Smith if (*np == '\\') 340015e32d5dSMike Smith np++; 34012be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.avoid")) == NULL) 340215e32d5dSMike Smith return (0); 340315e32d5dSMike Smith 3404be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 34053c600cfbSJohn Baldwin cp = env; 340615e32d5dSMike Smith for (;;) { 3407bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 340815e32d5dSMike Smith cp++; 340915e32d5dSMike Smith if (*cp == 0) 341015e32d5dSMike Smith break; 341115e32d5dSMike Smith len = 0; 3412bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 341315e32d5dSMike Smith len++; 3414d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 34153c600cfbSJohn Baldwin freeenv(env); 34160ae55423SMike Smith return(1); 3417d786139cSMaxime Henrion } 34180ae55423SMike Smith cp += len; 34190ae55423SMike Smith } 34203c600cfbSJohn Baldwin freeenv(env); 3421be2b1797SNate Lawson 34220ae55423SMike Smith return (0); 34230ae55423SMike Smith } 34240ae55423SMike Smith 34250ae55423SMike Smith /* 34260ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 34270ae55423SMike Smith */ 34280ae55423SMike Smith int 34290ae55423SMike Smith acpi_disabled(char *subsys) 34300ae55423SMike Smith { 343178689b15SMaxime Henrion char *cp, *env; 34320ae55423SMike Smith int len; 34330ae55423SMike Smith 34342be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.disabled")) == NULL) 34350ae55423SMike Smith return (0); 34363184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 343778689b15SMaxime Henrion freeenv(env); 34380ae55423SMike Smith return (1); 3439d786139cSMaxime Henrion } 34400ae55423SMike Smith 34413184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 344278689b15SMaxime Henrion cp = env; 34430ae55423SMike Smith for (;;) { 34443184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 34450ae55423SMike Smith cp++; 34463184cf5aSNate Lawson if (*cp == '\0') 34470ae55423SMike Smith break; 34480ae55423SMike Smith len = 0; 34493184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 34500ae55423SMike Smith len++; 34513184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 345278689b15SMaxime Henrion freeenv(env); 345315e32d5dSMike Smith return (1); 3454d786139cSMaxime Henrion } 345515e32d5dSMike Smith cp += len; 345615e32d5dSMike Smith } 345778689b15SMaxime Henrion freeenv(env); 3458be2b1797SNate Lawson 345915e32d5dSMike Smith return (0); 346015e32d5dSMike Smith } 346115e32d5dSMike Smith 346264de8019SJohn Baldwin static void 346364de8019SJohn Baldwin acpi_lookup(void *arg, const char *name, device_t *dev) 346464de8019SJohn Baldwin { 346564de8019SJohn Baldwin ACPI_HANDLE handle; 346664de8019SJohn Baldwin 346764de8019SJohn Baldwin if (*dev != NULL) 346864de8019SJohn Baldwin return; 346964de8019SJohn Baldwin 347064de8019SJohn Baldwin /* 347164de8019SJohn Baldwin * Allow any handle name that is specified as an absolute path and 347264de8019SJohn Baldwin * starts with '\'. We could restrict this to \_SB and friends, 347364de8019SJohn Baldwin * but see acpi_probe_children() for notes on why we scan the entire 347464de8019SJohn Baldwin * namespace for devices. 347564de8019SJohn Baldwin * 347664de8019SJohn Baldwin * XXX: The pathname argument to AcpiGetHandle() should be fixed to 347764de8019SJohn Baldwin * be const. 347864de8019SJohn Baldwin */ 347964de8019SJohn Baldwin if (name[0] != '\\') 348064de8019SJohn Baldwin return; 348164de8019SJohn Baldwin if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name), 348264de8019SJohn Baldwin &handle))) 348364de8019SJohn Baldwin return; 348464de8019SJohn Baldwin *dev = acpi_get_device(handle); 348564de8019SJohn Baldwin } 348664de8019SJohn Baldwin 348715e32d5dSMike Smith /* 348815e32d5dSMike Smith * Control interface. 348915e32d5dSMike Smith * 34900ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 3491be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 3492be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 349315e32d5dSMike Smith */ 34940ae55423SMike Smith struct acpi_ioctl_hook 34950ae55423SMike Smith { 34960ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 34970ae55423SMike Smith u_long cmd; 3498be2b1797SNate Lawson acpi_ioctl_fn fn; 34990ae55423SMike Smith void *arg; 35000ae55423SMike Smith }; 35010ae55423SMike Smith 35020ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 35030ae55423SMike Smith static int acpi_ioctl_hooks_initted; 35040ae55423SMike Smith 35050ae55423SMike Smith int 3506be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 35070ae55423SMike Smith { 35080ae55423SMike Smith struct acpi_ioctl_hook *hp; 35090ae55423SMike Smith 35100ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 35110ae55423SMike Smith return (ENOMEM); 35120ae55423SMike Smith hp->cmd = cmd; 35130ae55423SMike Smith hp->fn = fn; 35140ae55423SMike Smith hp->arg = arg; 351515e2f34fSNate Lawson 351615e2f34fSNate Lawson ACPI_LOCK(acpi); 35170ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 35180ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 35190ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 35200ae55423SMike Smith } 35210ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 352215e2f34fSNate Lawson ACPI_UNLOCK(acpi); 352315e2f34fSNate Lawson 35240ae55423SMike Smith return (0); 35250ae55423SMike Smith } 35260ae55423SMike Smith 35270ae55423SMike Smith void 3528be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 35290ae55423SMike Smith { 35300ae55423SMike Smith struct acpi_ioctl_hook *hp; 35310ae55423SMike Smith 353215e2f34fSNate Lawson ACPI_LOCK(acpi); 35330ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 353415e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 35350ae55423SMike Smith break; 35360ae55423SMike Smith 35370ae55423SMike Smith if (hp != NULL) { 35380ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 35390ae55423SMike Smith free(hp, M_ACPIDEV); 35400ae55423SMike Smith } 354115e2f34fSNate Lawson ACPI_UNLOCK(acpi); 35420ae55423SMike Smith } 35430ae55423SMike Smith 354415e32d5dSMike Smith static int 354500b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 354615e32d5dSMike Smith { 354715e32d5dSMike Smith return (0); 354815e32d5dSMike Smith } 354915e32d5dSMike Smith 355015e32d5dSMike Smith static int 355100b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 355215e32d5dSMike Smith { 355315e32d5dSMike Smith return (0); 355415e32d5dSMike Smith } 355515e32d5dSMike Smith 355615e32d5dSMike Smith static int 355700b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 355815e32d5dSMike Smith { 355915e32d5dSMike Smith struct acpi_softc *sc; 35600ae55423SMike Smith struct acpi_ioctl_hook *hp; 3561bee4aa4aSNate Lawson int error, state; 356215e32d5dSMike Smith 3563a2e35898SDavid E. O'Brien error = 0; 3564a2e35898SDavid E. O'Brien hp = NULL; 356515e32d5dSMike Smith sc = dev->si_drv1; 356615e32d5dSMike Smith 35670ae55423SMike Smith /* 35680ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 35690ae55423SMike Smith */ 357015e2f34fSNate Lawson ACPI_LOCK(acpi); 3571bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 35720ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3573bee4aa4aSNate Lawson if (hp->cmd == cmd) 3574bee4aa4aSNate Lawson break; 35750ae55423SMike Smith } 357615e2f34fSNate Lawson ACPI_UNLOCK(acpi); 3577bee4aa4aSNate Lawson if (hp) 3578bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 35790ae55423SMike Smith 35800ae55423SMike Smith /* 3581a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 3582a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 3583a89fcf28STakanori Watanabe * Not changing system behavior. 3584a89fcf28STakanori Watanabe */ 3585be2b1797SNate Lawson if ((flag & FWRITE) == 0) 3586be2b1797SNate Lawson return (EPERM); 3587a89fcf28STakanori Watanabe 3588be2b1797SNate Lawson /* Core system ioctls. */ 358915e32d5dSMike Smith switch (cmd) { 359000a30448SNate Lawson case ACPIIO_REQSLPSTATE: 359100a30448SNate Lawson state = *(int *)addr; 359200a30448SNate Lawson if (state != ACPI_STATE_S5) 3593a0e73a12SJung-uk Kim return (acpi_ReqSleepState(sc, state)); 3594a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3595a0e73a12SJung-uk Kim error = EOPNOTSUPP; 359600a30448SNate Lawson break; 359700a30448SNate Lawson case ACPIIO_ACKSLPSTATE: 359800a30448SNate Lawson error = *(int *)addr; 359900a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error); 360000a30448SNate Lawson break; 360100a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 360215e32d5dSMike Smith state = *(int *)addr; 3603a0e73a12SJung-uk Kim if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3604a0e73a12SJung-uk Kim return (EINVAL); 3605a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 3606a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3607a0e73a12SJung-uk Kim if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3608a0e73a12SJung-uk Kim error = ENXIO; 360915e32d5dSMike Smith break; 361015e32d5dSMike Smith default: 3611bee4aa4aSNate Lawson error = ENXIO; 361215e32d5dSMike Smith break; 361315e32d5dSMike Smith } 3614917d44c8SMitsuru IWASAKI 361515e32d5dSMike Smith return (error); 361615e32d5dSMike Smith } 361715e32d5dSMike Smith 36181d073b1dSJohn Baldwin static int 3619a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname) 3620a0e73a12SJung-uk Kim { 3621a0e73a12SJung-uk Kim int sstate; 3622a0e73a12SJung-uk Kim 3623a0e73a12SJung-uk Kim if (toupper(sname[0]) == 'S') { 3624a0e73a12SJung-uk Kim sstate = sname[1] - '0'; 3625a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3626a0e73a12SJung-uk Kim sname[2] == '\0') 3627a0e73a12SJung-uk Kim return (sstate); 3628a0e73a12SJung-uk Kim } else if (strcasecmp(sname, "NONE") == 0) 3629a0e73a12SJung-uk Kim return (ACPI_STATE_UNKNOWN); 3630a0e73a12SJung-uk Kim return (-1); 3631a0e73a12SJung-uk Kim } 3632a0e73a12SJung-uk Kim 3633a0e73a12SJung-uk Kim static const char * 3634a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate) 3635a0e73a12SJung-uk Kim { 3636a0e73a12SJung-uk Kim static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3637a0e73a12SJung-uk Kim 3638a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3639a0e73a12SJung-uk Kim return (snames[sstate]); 3640a0e73a12SJung-uk Kim else if (sstate == ACPI_STATE_UNKNOWN) 3641a0e73a12SJung-uk Kim return ("NONE"); 3642a0e73a12SJung-uk Kim return (NULL); 3643a0e73a12SJung-uk Kim } 3644a0e73a12SJung-uk Kim 3645a0e73a12SJung-uk Kim static int 3646d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3647d75de536SMitsuru IWASAKI { 3648d75de536SMitsuru IWASAKI int error; 3649bee4aa4aSNate Lawson struct sbuf sb; 3650a0e73a12SJung-uk Kim UINT8 state; 3651d75de536SMitsuru IWASAKI 3652bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3653a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3654a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) 3655a0e73a12SJung-uk Kim sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3656bee4aa4aSNate Lawson sbuf_trim(&sb); 3657bee4aa4aSNate Lawson sbuf_finish(&sb); 3658bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3659bee4aa4aSNate Lawson sbuf_delete(&sb); 3660d75de536SMitsuru IWASAKI return (error); 3661d75de536SMitsuru IWASAKI } 3662d75de536SMitsuru IWASAKI 3663d75de536SMitsuru IWASAKI static int 36641d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 36651d073b1dSJohn Baldwin { 36661d073b1dSJohn Baldwin char sleep_state[10]; 3667a0e73a12SJung-uk Kim int error, new_state, old_state; 36681d073b1dSJohn Baldwin 3669a0e73a12SJung-uk Kim old_state = *(int *)oidp->oid_arg1; 3670a0e73a12SJung-uk Kim strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 36711d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 36721d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 3673a0e73a12SJung-uk Kim new_state = acpi_sname2sstate(sleep_state); 3674a0e73a12SJung-uk Kim if (new_state < ACPI_STATE_S1) 3675a0e73a12SJung-uk Kim return (EINVAL); 367654b43614SJung-uk Kim if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3677a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3678be2b1797SNate Lawson if (new_state != old_state) 3679a0e73a12SJung-uk Kim *(int *)oidp->oid_arg1 = new_state; 36801d073b1dSJohn Baldwin } 36811d073b1dSJohn Baldwin return (error); 36821d073b1dSJohn Baldwin } 36831d073b1dSJohn Baldwin 36849b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 36859b937d48SNate Lawson void 36869b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 36879b937d48SNate Lawson { 36889b937d48SNate Lawson char notify_buf[16]; 36899b937d48SNate Lawson ACPI_BUFFER handle_buf; 36909b937d48SNate Lawson ACPI_STATUS status; 36919b937d48SNate Lawson 36929b937d48SNate Lawson if (subsystem == NULL) 36939b937d48SNate Lawson return; 36949b937d48SNate Lawson 36959b937d48SNate Lawson handle_buf.Pointer = NULL; 36969b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 36970594dadeSJung-uk Kim status = AcpiNsHandleToPathname(h, &handle_buf, FALSE); 36989b937d48SNate Lawson if (ACPI_FAILURE(status)) 36999b937d48SNate Lawson return; 37009b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 37019b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 37029b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 37039b937d48SNate Lawson } 37049b937d48SNate Lawson 370515e32d5dSMike Smith #ifdef ACPI_DEBUG 37060ae55423SMike Smith /* 37070ae55423SMike Smith * Support for parsing debug options from the kernel environment. 37080ae55423SMike Smith * 37090ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 37100ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 37110ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 37120ae55423SMike Smith * prefixing the bit name with !. 37130ae55423SMike Smith */ 371415e32d5dSMike Smith struct debugtag 371515e32d5dSMike Smith { 371615e32d5dSMike Smith char *name; 371715e32d5dSMike Smith UINT32 value; 371815e32d5dSMike Smith }; 371915e32d5dSMike Smith 372015e32d5dSMike Smith static struct debugtag dbg_layer[] = { 3721c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 3722c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 3723c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 3724c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 3725c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3726c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 3727c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3728c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 3729c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 3730d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 37314c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3732d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3733297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 37344c1cdee6SMike Smith 3735c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3736c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 37373184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 3738c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 37393184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 37403184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 37413184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 37424c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3743cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 37443184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 3745b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 374615e32d5dSMike Smith {NULL, 0} 374715e32d5dSMike Smith }; 374815e32d5dSMike Smith 374915e32d5dSMike Smith static struct debugtag dbg_level[] = { 37509501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 37514c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 37529501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 3753cc6455afSJung-uk Kim {"ACPI_LV_REPAIR", ACPI_LV_REPAIR}, 37544c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3755d62ab2f4SMitsuru IWASAKI 3756d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 3757297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 37584c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 37594c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3760d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 37614c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 37624c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 37634c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 37644c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 37654c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 37664c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 37674c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 37684c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 37694c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 37704c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3771d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3772d62ab2f4SMitsuru IWASAKI 3773d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3774d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3775d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3776d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3777d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 37784c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 3779d62ab2f4SMitsuru IWASAKI 3780d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3781d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3782d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3783d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 3784d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3785d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3786d62ab2f4SMitsuru IWASAKI 3787d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 378898479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 378998479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 379098479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 379198479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 379298479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 379315e32d5dSMike Smith {NULL, 0} 379415e32d5dSMike Smith }; 379515e32d5dSMike Smith 379615e32d5dSMike Smith static void 379715e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 379815e32d5dSMike Smith { 379915e32d5dSMike Smith char *ep; 380015e32d5dSMike Smith int i, l; 38010ae55423SMike Smith int set; 380215e32d5dSMike Smith 380315e32d5dSMike Smith while (*cp) { 380415e32d5dSMike Smith if (isspace(*cp)) { 380515e32d5dSMike Smith cp++; 380615e32d5dSMike Smith continue; 380715e32d5dSMike Smith } 380815e32d5dSMike Smith ep = cp; 380915e32d5dSMike Smith while (*ep && !isspace(*ep)) 381015e32d5dSMike Smith ep++; 38110ae55423SMike Smith if (*cp == '!') { 38120ae55423SMike Smith set = 0; 38130ae55423SMike Smith cp++; 38140ae55423SMike Smith if (cp == ep) 38150ae55423SMike Smith continue; 38160ae55423SMike Smith } else { 38170ae55423SMike Smith set = 1; 38180ae55423SMike Smith } 381915e32d5dSMike Smith l = ep - cp; 382015e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 382115e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 3822be2b1797SNate Lawson if (set) 382315e32d5dSMike Smith *flag |= tag[i].value; 3824be2b1797SNate Lawson else 38250ae55423SMike Smith *flag &= ~tag[i].value; 382615e32d5dSMike Smith } 382715e32d5dSMike Smith } 382815e32d5dSMike Smith cp = ep; 382915e32d5dSMike Smith } 383015e32d5dSMike Smith } 383115e32d5dSMike Smith 383215e32d5dSMike Smith static void 38332a4ac806SMike Smith acpi_set_debugging(void *junk) 383415e32d5dSMike Smith { 38357e639165SNate Lawson char *layer, *level; 383615e32d5dSMike Smith 38371d7b121cSNate Lawson if (cold) { 38380ae55423SMike Smith AcpiDbgLayer = 0; 38390ae55423SMike Smith AcpiDbgLevel = 0; 38401d7b121cSNate Lawson } 38411d7b121cSNate Lawson 38422be111bfSDavide Italiano layer = kern_getenv("debug.acpi.layer"); 38432be111bfSDavide Italiano level = kern_getenv("debug.acpi.level"); 38447e639165SNate Lawson if (layer == NULL && level == NULL) 38457e639165SNate Lawson return; 38460ae55423SMike Smith 38477e639165SNate Lawson printf("ACPI set debug"); 38487e639165SNate Lawson if (layer != NULL) { 38497e639165SNate Lawson if (strcmp("NONE", layer) != 0) 38507e639165SNate Lawson printf(" layer '%s'", layer); 38517e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 38527e639165SNate Lawson freeenv(layer); 38531d7b121cSNate Lawson } 38547e639165SNate Lawson if (level != NULL) { 38557e639165SNate Lawson if (strcmp("NONE", level) != 0) 38567e639165SNate Lawson printf(" level '%s'", level); 38577e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 38587e639165SNate Lawson freeenv(level); 38597e639165SNate Lawson } 38607e639165SNate Lawson printf("\n"); 386115e32d5dSMike Smith } 3862bee4aa4aSNate Lawson 3863be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3864be2b1797SNate Lawson NULL); 38651d7b121cSNate Lawson 38661d7b121cSNate Lawson static int 38671d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 38681d7b121cSNate Lawson { 386954f6bca0SNate Lawson int error, *dbg; 38701d7b121cSNate Lawson struct debugtag *tag; 387154f6bca0SNate Lawson struct sbuf sb; 38720e1152fcSHans Petter Selasky char temp[128]; 38731d7b121cSNate Lawson 387454f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 387554f6bca0SNate Lawson return (ENOMEM); 38761d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 38771d7b121cSNate Lawson tag = &dbg_layer[0]; 38781d7b121cSNate Lawson dbg = &AcpiDbgLayer; 38791d7b121cSNate Lawson } else { 38801d7b121cSNate Lawson tag = &dbg_level[0]; 38811d7b121cSNate Lawson dbg = &AcpiDbgLevel; 38821d7b121cSNate Lawson } 38831d7b121cSNate Lawson 38841d7b121cSNate Lawson /* Get old values if this is a get request. */ 388515e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 38861d7b121cSNate Lawson if (*dbg == 0) { 388754f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 38881d7b121cSNate Lawson } else if (req->newptr == NULL) { 38891d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 389054f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 389154f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 38921d7b121cSNate Lawson } 38931d7b121cSNate Lawson } 389454f6bca0SNate Lawson sbuf_trim(&sb); 389554f6bca0SNate Lawson sbuf_finish(&sb); 38960e1152fcSHans Petter Selasky strlcpy(temp, sbuf_data(&sb), sizeof(temp)); 389754f6bca0SNate Lawson sbuf_delete(&sb); 38981d7b121cSNate Lawson 38990e1152fcSHans Petter Selasky error = sysctl_handle_string(oidp, temp, sizeof(temp), req); 39000e1152fcSHans Petter Selasky 39010e1152fcSHans Petter Selasky /* Check for error or no change */ 39021d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 39031d7b121cSNate Lawson *dbg = 0; 39040e1152fcSHans Petter Selasky kern_setenv((char *)oidp->oid_arg1, temp); 39051d7b121cSNate Lawson acpi_set_debugging(NULL); 39061d7b121cSNate Lawson } 390715e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 39081d7b121cSNate Lawson 39091d7b121cSNate Lawson return (error); 39101d7b121cSNate Lawson } 3911bee4aa4aSNate Lawson 39121d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 39131d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 39141d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 39151d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3916bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 3917f9390180SMitsuru IWASAKI 3918f9390180SMitsuru IWASAKI static int 39192a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 39202a18c71dSJung-uk Kim { 39212a18c71dSJung-uk Kim int error; 39222a18c71dSJung-uk Kim int old; 39232a18c71dSJung-uk Kim 39242a18c71dSJung-uk Kim old = acpi_debug_objects; 39252a18c71dSJung-uk Kim error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 39262a18c71dSJung-uk Kim if (error != 0 || req->newptr == NULL) 39272a18c71dSJung-uk Kim return (error); 39282a18c71dSJung-uk Kim if (old == acpi_debug_objects || (old && acpi_debug_objects)) 39292a18c71dSJung-uk Kim return (0); 39302a18c71dSJung-uk Kim 39312a18c71dSJung-uk Kim ACPI_SERIAL_BEGIN(acpi); 39322a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 39332a18c71dSJung-uk Kim ACPI_SERIAL_END(acpi); 39342a18c71dSJung-uk Kim 39352a18c71dSJung-uk Kim return (0); 39362a18c71dSJung-uk Kim } 39372a18c71dSJung-uk Kim 39382a18c71dSJung-uk Kim static int 3939ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface) 3940ae19af49SJung-uk Kim { 3941ae19af49SJung-uk Kim char *p; 3942ae19af49SJung-uk Kim size_t len; 3943ae19af49SJung-uk Kim int i, j; 3944ae19af49SJung-uk Kim 3945ae19af49SJung-uk Kim p = str; 3946ae19af49SJung-uk Kim while (isspace(*p) || *p == ',') 3947ae19af49SJung-uk Kim p++; 3948ae19af49SJung-uk Kim len = strlen(p); 3949ae19af49SJung-uk Kim if (len == 0) 3950ae19af49SJung-uk Kim return (0); 3951ae19af49SJung-uk Kim p = strdup(p, M_TEMP); 3952ae19af49SJung-uk Kim for (i = 0; i < len; i++) 3953ae19af49SJung-uk Kim if (p[i] == ',') 3954ae19af49SJung-uk Kim p[i] = '\0'; 3955ae19af49SJung-uk Kim i = j = 0; 3956ae19af49SJung-uk Kim while (i < len) 3957ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 3958ae19af49SJung-uk Kim i++; 3959ae19af49SJung-uk Kim else { 3960ae19af49SJung-uk Kim i += strlen(p + i) + 1; 3961ae19af49SJung-uk Kim j++; 3962ae19af49SJung-uk Kim } 3963ae19af49SJung-uk Kim if (j == 0) { 3964ae19af49SJung-uk Kim free(p, M_TEMP); 3965ae19af49SJung-uk Kim return (0); 3966ae19af49SJung-uk Kim } 3967ae19af49SJung-uk Kim iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK); 3968ae19af49SJung-uk Kim iface->num = j; 3969ae19af49SJung-uk Kim i = j = 0; 3970ae19af49SJung-uk Kim while (i < len) 3971ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 3972ae19af49SJung-uk Kim i++; 3973ae19af49SJung-uk Kim else { 3974ae19af49SJung-uk Kim iface->data[j] = p + i; 3975ae19af49SJung-uk Kim i += strlen(p + i) + 1; 3976ae19af49SJung-uk Kim j++; 3977ae19af49SJung-uk Kim } 3978ae19af49SJung-uk Kim 3979ae19af49SJung-uk Kim return (j); 3980ae19af49SJung-uk Kim } 3981ae19af49SJung-uk Kim 3982ae19af49SJung-uk Kim static void 3983ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface) 3984ae19af49SJung-uk Kim { 3985ae19af49SJung-uk Kim 3986ae19af49SJung-uk Kim free(iface->data[0], M_TEMP); 3987ae19af49SJung-uk Kim free(iface->data, M_TEMP); 3988ae19af49SJung-uk Kim } 3989ae19af49SJung-uk Kim 3990ae19af49SJung-uk Kim static void 3991ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev) 3992ae19af49SJung-uk Kim { 3993ae19af49SJung-uk Kim struct acpi_interface list; 3994ae19af49SJung-uk Kim ACPI_STATUS status; 3995ae19af49SJung-uk Kim int i; 3996ae19af49SJung-uk Kim 3997ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) { 3998ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 3999ae19af49SJung-uk Kim status = AcpiInstallInterface(list.data[i]); 4000ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 4001ae19af49SJung-uk Kim device_printf(dev, 4002ae19af49SJung-uk Kim "failed to install _OSI(\"%s\"): %s\n", 4003ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 4004ae19af49SJung-uk Kim else if (bootverbose) 4005ae19af49SJung-uk Kim device_printf(dev, "installed _OSI(\"%s\")\n", 4006ae19af49SJung-uk Kim list.data[i]); 4007ae19af49SJung-uk Kim } 4008ae19af49SJung-uk Kim acpi_free_interfaces(&list); 4009ae19af49SJung-uk Kim } 4010ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) { 4011ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 4012ae19af49SJung-uk Kim status = AcpiRemoveInterface(list.data[i]); 4013ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 4014ae19af49SJung-uk Kim device_printf(dev, 4015ae19af49SJung-uk Kim "failed to remove _OSI(\"%s\"): %s\n", 4016ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 4017ae19af49SJung-uk Kim else if (bootverbose) 4018ae19af49SJung-uk Kim device_printf(dev, "removed _OSI(\"%s\")\n", 4019ae19af49SJung-uk Kim list.data[i]); 4020ae19af49SJung-uk Kim } 4021ae19af49SJung-uk Kim acpi_free_interfaces(&list); 4022ae19af49SJung-uk Kim } 4023ae19af49SJung-uk Kim } 4024ae19af49SJung-uk Kim 4025ae19af49SJung-uk Kim static int 4026f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 4027f9390180SMitsuru IWASAKI { 4028f9390180SMitsuru IWASAKI int state, acpi_state; 4029f9390180SMitsuru IWASAKI int error; 4030f9390180SMitsuru IWASAKI struct acpi_softc *sc; 4031f9390180SMitsuru IWASAKI va_list ap; 4032f9390180SMitsuru IWASAKI 4033f9390180SMitsuru IWASAKI error = 0; 4034f9390180SMitsuru IWASAKI switch (cmd) { 4035f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 4036f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 4037f9390180SMitsuru IWASAKI if (sc == NULL) { 4038f9390180SMitsuru IWASAKI error = EINVAL; 4039f9390180SMitsuru IWASAKI goto out; 4040f9390180SMitsuru IWASAKI } 4041f9390180SMitsuru IWASAKI 4042f9390180SMitsuru IWASAKI va_start(ap, arg); 4043f9390180SMitsuru IWASAKI state = va_arg(ap, int); 4044f9390180SMitsuru IWASAKI va_end(ap); 4045f9390180SMitsuru IWASAKI 4046f9390180SMitsuru IWASAKI switch (state) { 4047f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 4048f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 4049f9390180SMitsuru IWASAKI break; 4050f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 4051f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 4052f9390180SMitsuru IWASAKI break; 4053f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 4054f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 4055f9390180SMitsuru IWASAKI break; 4056f9390180SMitsuru IWASAKI default: 4057f9390180SMitsuru IWASAKI error = EINVAL; 4058f9390180SMitsuru IWASAKI goto out; 4059f9390180SMitsuru IWASAKI } 4060f9390180SMitsuru IWASAKI 406100a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 406200a30448SNate Lawson error = ENXIO; 4063f9390180SMitsuru IWASAKI break; 4064f9390180SMitsuru IWASAKI default: 4065f9390180SMitsuru IWASAKI error = EINVAL; 4066f9390180SMitsuru IWASAKI goto out; 4067f9390180SMitsuru IWASAKI } 4068f9390180SMitsuru IWASAKI 4069f9390180SMitsuru IWASAKI out: 4070f9390180SMitsuru IWASAKI return (error); 4071f9390180SMitsuru IWASAKI } 4072f9390180SMitsuru IWASAKI 4073f9390180SMitsuru IWASAKI static void 4074f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 4075f9390180SMitsuru IWASAKI { 4076be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 40776c407052SMitsuru IWASAKI return; 4078f9390180SMitsuru IWASAKI 4079f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 4080f9390180SMitsuru IWASAKI } 4081f9390180SMitsuru IWASAKI 4082f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 4083