115e32d5dSMike Smith /*- 215e32d5dSMike Smith * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 315e32d5dSMike Smith * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4cb9b0d80SMike Smith * Copyright (c) 2000, 2001 Michael Smith 515e32d5dSMike Smith * Copyright (c) 2000 BSDi 615e32d5dSMike Smith * All rights reserved. 715e32d5dSMike Smith * 815e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 915e32d5dSMike Smith * modification, are permitted provided that the following conditions 1015e32d5dSMike Smith * are met: 1115e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1315e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1415e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1515e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1615e32d5dSMike Smith * 1715e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1815e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2015e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2115e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2215e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2315e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2415e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2515e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2615e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2715e32d5dSMike Smith * SUCH DAMAGE. 2815e32d5dSMike Smith */ 2915e32d5dSMike Smith 30dad97feeSDavid E. O'Brien #include <sys/cdefs.h> 31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$"); 32dad97feeSDavid E. O'Brien 3315e32d5dSMike Smith #include "opt_acpi.h" 3415e32d5dSMike Smith #include <sys/param.h> 3515e32d5dSMike Smith #include <sys/kernel.h> 36fb919e4dSMark Murray #include <sys/proc.h> 37a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3815e32d5dSMike Smith #include <sys/malloc.h> 39fe12f24bSPoul-Henning Kamp #include <sys/module.h> 4015e32d5dSMike Smith #include <sys/bus.h> 4115e32d5dSMike Smith #include <sys/conf.h> 4215e32d5dSMike Smith #include <sys/ioccom.h> 4315e32d5dSMike Smith #include <sys/reboot.h> 4415e32d5dSMike Smith #include <sys/sysctl.h> 4515e32d5dSMike Smith #include <sys/ctype.h> 461611ea87SMitsuru IWASAKI #include <sys/linker.h> 47f9390180SMitsuru IWASAKI #include <sys/power.h> 4854f6bca0SNate Lawson #include <sys/sbuf.h> 49c66d2b38SJung-uk Kim #include <sys/sched.h> 50eeb3a05fSNate Lawson #include <sys/smp.h> 51c66d2b38SJung-uk Kim #include <sys/timetc.h> 5215e32d5dSMike Smith 53d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 54d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h> 55d320e05cSJohn Baldwin #endif 5615e32d5dSMike Smith #include <machine/resource.h> 57dc750869SNate Lawson #include <machine/bus.h> 58dc750869SNate Lawson #include <sys/rman.h> 59bc0f2195SMike Smith #include <isa/isavar.h> 60c796e27bSNate Lawson #include <isa/pnpvar.h> 61bc0f2195SMike Smith 62129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h> 63129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h> 64129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h> 65129d3046SJung-uk Kim 6615e32d5dSMike Smith #include <dev/acpica/acpivar.h> 6715e32d5dSMike Smith #include <dev/acpica/acpiio.h> 6815e32d5dSMike Smith 6910ce62b9SNate Lawson #include "pci_if.h" 7010ce62b9SNate Lawson #include <dev/pci/pcivar.h> 7110ce62b9SNate Lawson #include <dev/pci/pci_private.h> 7210ce62b9SNate Lawson 732be4e471SJung-uk Kim #include <vm/vm_param.h> 742be4e471SJung-uk Kim 7515e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 7615e32d5dSMike Smith 77be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 78cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 79b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 800ae55423SMike Smith 8115e32d5dSMike Smith static d_open_t acpiopen; 8215e32d5dSMike Smith static d_close_t acpiclose; 8315e32d5dSMike Smith static d_ioctl_t acpiioctl; 8415e32d5dSMike Smith 8515e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 86dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 877ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 887ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 897ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 907ac40f5fSPoul-Henning Kamp .d_name = "acpi", 9115e32d5dSMike Smith }; 9215e32d5dSMike Smith 93346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 94cb9b0d80SMike Smith struct mtx acpi_mutex; 95cb9b0d80SMike Smith 9689fb5af8SNate Lawson /* Bitmap of device quirks. */ 9789fb5af8SNate Lawson int acpi_quirks; 9889fb5af8SNate Lawson 99a0e73a12SJung-uk Kim /* Supported sleep states. */ 100a0e73a12SJung-uk Kim static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 101a0e73a12SJung-uk Kim 1026d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10315e32d5dSMike Smith static int acpi_probe(device_t dev); 10415e32d5dSMike Smith static int acpi_attach(device_t dev); 10510ce62b9SNate Lawson static int acpi_suspend(device_t dev); 10610ce62b9SNate Lawson static int acpi_resume(device_t dev); 107169b539aSNate Lawson static int acpi_shutdown(device_t dev); 108be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 109be2b1797SNate Lawson int unit); 11015e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 11110ce62b9SNate Lawson static void acpi_probe_nomatch(device_t bus, device_t child); 11210ce62b9SNate Lawson static void acpi_driver_added(device_t dev, driver_t *driver); 113be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 114be2b1797SNate Lawson uintptr_t *result); 115be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 116be2b1797SNate Lawson uintptr_t value); 11791233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 118adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 119be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 120be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 121be2b1797SNate Lawson u_long count, u_int flags); 122be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 123be2b1797SNate Lawson int rid, struct resource *r); 1248b28b622SNate Lawson static void acpi_delete_resource(device_t bus, device_t child, int type, 1258b28b622SNate Lawson int rid); 1261e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1271e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 128ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 129ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 130ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 131ce619b2eSNate Lawson ACPI_BUFFER *ret); 132df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 133df8d2a32SNate Lawson void *context, void **retval); 134df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 135df8d2a32SNate Lawson int max_depth, acpi_scan_cb_t user_fn, void *arg); 13610ce62b9SNate Lawson static int acpi_set_powerstate_method(device_t bus, device_t child, 13710ce62b9SNate Lawson int state); 138be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 139be2b1797SNate Lawson struct isa_pnp_id *ids); 14015e32d5dSMike Smith static void acpi_probe_children(device_t bus); 141d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order); 142be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 143be2b1797SNate Lawson void *context, void **status); 144a0e73a12SJung-uk Kim static void acpi_sleep_enable(void *arg); 145a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 14600a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 14715e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 14813d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 149d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 150d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 151d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 15288a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 15388a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 15415e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 15515e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 156a0e73a12SJung-uk Kim static int acpi_sname2sstate(const char *sname); 157a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate); 158d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1591d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1602a18c71dSJung-uk Kim static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 161f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 162879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 163879d6c23STakanori Watanabe char *buf, size_t buflen); 164879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 165879d6c23STakanori Watanabe char *buf, size_t buflen); 166d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 167d320e05cSJohn Baldwin static void acpi_enable_pcie(void); 168d320e05cSJohn Baldwin #endif 1690d484d24SJohn Baldwin static void acpi_hint_device_unit(device_t acdev, device_t child, 1700d484d24SJohn Baldwin const char *name, int *unitp); 171879d6c23STakanori Watanabe 17215e32d5dSMike Smith static device_method_t acpi_methods[] = { 17315e32d5dSMike Smith /* Device interface */ 17415e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 17515e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 176169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 17756a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 17810ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend), 17910ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume), 18015e32d5dSMike Smith 18115e32d5dSMike Smith /* Bus interface */ 18215e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 18315e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 18410ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 18510ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added), 18615e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 18715e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 18891233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 18991233413SNate Lawson DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 19091233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 19115e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 19215e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 1938b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource), 194879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 195879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 19615e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 19715e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 19815e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 19915e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 2000d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 20115e32d5dSMike Smith 202ce619b2eSNate Lawson /* ACPI bus */ 203ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 204ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 20510ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 206df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 207ce619b2eSNate Lawson 20810ce62b9SNate Lawson /* PCI emulation */ 20910ce62b9SNate Lawson DEVMETHOD(pci_set_powerstate, acpi_set_powerstate_method), 21010ce62b9SNate Lawson 211bc0f2195SMike Smith /* ISA emulation */ 212bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 213bc0f2195SMike Smith 21415e32d5dSMike Smith {0, 0} 21515e32d5dSMike Smith }; 21615e32d5dSMike Smith 21715e32d5dSMike Smith static driver_t acpi_driver = { 21815e32d5dSMike Smith "acpi", 21915e32d5dSMike Smith acpi_methods, 22015e32d5dSMike Smith sizeof(struct acpi_softc), 22115e32d5dSMike Smith }; 22215e32d5dSMike Smith 2233273b005SMike Smith static devclass_t acpi_devclass; 2246d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 225a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 22615e32d5dSMike Smith 22715e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 22815e2f34fSNate Lawson 229adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 230adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 231adad4744SNate Lawson 232bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 233bee4aa4aSNate Lawson 2345217af30SJohn Baldwin /* Holds the description of the acpi0 device. */ 2355217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 2365217af30SJohn Baldwin 237197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 2381d7b121cSNate Lawson static char acpi_ca_version[12]; 2391d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2401d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 24115e32d5dSMike Smith 24215e32d5dSMike Smith /* 243413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 244c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 245c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 246c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 247c9b8d77dSNate Lawson * some IBMs use such code. 248413081d7SNate Lawson */ 249c9b8d77dSNate Lawson static int acpi_serialize_methods; 250413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 251413081d7SNate Lawson 2522a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */ 2532a18c71dSJung-uk Kim static int acpi_debug_objects; 2542a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 2552a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 2562a18c71dSJung-uk Kim CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 2572a18c71dSJung-uk Kim "Enable Debug objects"); 2582a18c71dSJung-uk Kim 2592a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */ 2602a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1; 2612a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 2622a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 2632a18c71dSJung-uk Kim &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 2642a18c71dSJung-uk Kim 26510ce62b9SNate Lawson /* Power devices off and on in suspend and resume. XXX Remove once tested. */ 26610ce62b9SNate Lawson static int acpi_do_powerstate = 1; 26710ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate); 26810ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW, 26910ce62b9SNate Lawson &acpi_do_powerstate, 1, "Turn off devices when suspending."); 27010ce62b9SNate Lawson 2710755473bSJung-uk Kim /* Reset system clock while resuming. XXX Remove once tested. */ 2720755473bSJung-uk Kim static int acpi_reset_clock = 1; 2730755473bSJung-uk Kim TUNABLE_INT("debug.acpi.reset_clock", &acpi_reset_clock); 2740755473bSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, reset_clock, CTLFLAG_RW, 2750755473bSJung-uk Kim &acpi_reset_clock, 1, "Reset system clock while resuming."); 2760755473bSJung-uk Kim 27739da3eb3SNate Lawson /* Allow users to override quirks. */ 27839da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 27939da3eb3SNate Lawson 28093bfd059SNate Lawson static int acpi_susp_bounce; 28193bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 28293bfd059SNate Lawson &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 28393bfd059SNate Lawson 284c9b8d77dSNate Lawson /* 2856d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2866d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 2878c0879b6SJohn Baldwin * It also cannot be unloaded, since the entire system bus hierarchy hangs 288be2b1797SNate Lawson * off it. 2896d63101aSMike Smith */ 2906d63101aSMike Smith static int 2916d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2926d63101aSMike Smith { 2936d63101aSMike Smith switch (event) { 2946d63101aSMike Smith case MOD_LOAD: 29587b45ed5SMitsuru IWASAKI if (!cold) { 29687b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2976d63101aSMike Smith return (EPERM); 29887b45ed5SMitsuru IWASAKI } 2996d63101aSMike Smith break; 3006d63101aSMike Smith case MOD_UNLOAD: 301f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 3026d63101aSMike Smith return (EBUSY); 303f9390180SMitsuru IWASAKI break; 3046d63101aSMike Smith default: 3056d63101aSMike Smith break; 3066d63101aSMike Smith } 3076d63101aSMike Smith return (0); 3086d63101aSMike Smith } 3096d63101aSMike Smith 3106d63101aSMike Smith /* 311bbc2815cSJohn Baldwin * Perform early initialization. 31215e32d5dSMike Smith */ 313bbc2815cSJohn Baldwin ACPI_STATUS 314bbc2815cSJohn Baldwin acpi_Startup(void) 31515e32d5dSMike Smith { 31689fb5af8SNate Lawson static int started = 0; 3172be4e471SJung-uk Kim ACPI_STATUS status; 3182be4e471SJung-uk Kim int val; 31915e32d5dSMike Smith 3201b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3211b8c233dSPeter Pentchev 322bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */ 323bbc2815cSJohn Baldwin if (started) 3242be4e471SJung-uk Kim return_VALUE (AE_OK); 325bbc2815cSJohn Baldwin started = 1; 32615e32d5dSMike Smith 327413081d7SNate Lawson /* 3282be4e471SJung-uk Kim * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 3292be4e471SJung-uk Kim * if more tables exist. 330413081d7SNate Lawson */ 3312be4e471SJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 3322be4e471SJung-uk Kim printf("ACPI: Table initialisation failed: %s\n", 3332be4e471SJung-uk Kim AcpiFormatException(status)); 3342be4e471SJung-uk Kim return_VALUE (status); 33515e32d5dSMike Smith } 3363184cf5aSNate Lawson 33789fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 3382be4e471SJung-uk Kim if (acpi_quirks == ACPI_Q_OK) 33989fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 34089fb5af8SNate Lawson 34139da3eb3SNate Lawson /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 34289fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 34389fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 34489fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 34589fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 3462be4e471SJung-uk Kim status = AE_SUPPORT; 34789fb5af8SNate Lawson } 3483184cf5aSNate Lawson 3492be4e471SJung-uk Kim return_VALUE (status); 350bbc2815cSJohn Baldwin } 351bbc2815cSJohn Baldwin 352bbc2815cSJohn Baldwin /* 3535217af30SJohn Baldwin * Detect ACPI and perform early initialisation. 354bbc2815cSJohn Baldwin */ 3555217af30SJohn Baldwin int 3565217af30SJohn Baldwin acpi_identify(void) 357bbc2815cSJohn Baldwin { 3585217af30SJohn Baldwin ACPI_TABLE_RSDP *rsdp; 3595217af30SJohn Baldwin ACPI_TABLE_HEADER *rsdt; 3605217af30SJohn Baldwin ACPI_PHYSICAL_ADDRESS paddr; 3615217af30SJohn Baldwin struct sbuf sb; 362bbc2815cSJohn Baldwin 363bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 364bbc2815cSJohn Baldwin 365bbc2815cSJohn Baldwin if (!cold) 3665217af30SJohn Baldwin return (ENXIO); 367bbc2815cSJohn Baldwin 368bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 369bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 3705217af30SJohn Baldwin return (ENXIO); 371bbc2815cSJohn Baldwin 3725217af30SJohn Baldwin /* Check for other PM systems. */ 3735217af30SJohn Baldwin if (power_pm_get_type() != POWER_PM_TYPE_NONE && 3745217af30SJohn Baldwin power_pm_get_type() != POWER_PM_TYPE_ACPI) { 3755217af30SJohn Baldwin printf("ACPI identify failed, other PM system enabled.\n"); 3765217af30SJohn Baldwin return (ENXIO); 3775217af30SJohn Baldwin } 3784b1ff978SMark Santcroos 3792be4e471SJung-uk Kim /* Initialize root tables. */ 3802be4e471SJung-uk Kim if (ACPI_FAILURE(acpi_Startup())) { 3812be4e471SJung-uk Kim printf("ACPI: Try disabling either ACPI or apic support.\n"); 3825217af30SJohn Baldwin return (ENXIO); 3832be4e471SJung-uk Kim } 38415e32d5dSMike Smith 3855217af30SJohn Baldwin if ((paddr = AcpiOsGetRootPointer()) == 0 || 3865217af30SJohn Baldwin (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 3875217af30SJohn Baldwin return (ENXIO); 3885217af30SJohn Baldwin if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 3895217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 3905217af30SJohn Baldwin else 3915217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 3925217af30SJohn Baldwin AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 3935217af30SJohn Baldwin 3945217af30SJohn Baldwin if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 3955217af30SJohn Baldwin return (ENXIO); 3965217af30SJohn Baldwin sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 3975217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 3985217af30SJohn Baldwin sbuf_trim(&sb); 3995217af30SJohn Baldwin sbuf_putc(&sb, ' '); 4005217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 4015217af30SJohn Baldwin sbuf_trim(&sb); 4025217af30SJohn Baldwin sbuf_finish(&sb); 4035217af30SJohn Baldwin sbuf_delete(&sb); 4045217af30SJohn Baldwin AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 4055217af30SJohn Baldwin 4065217af30SJohn Baldwin snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 4075217af30SJohn Baldwin 4085217af30SJohn Baldwin return (0); 40915e32d5dSMike Smith } 41015e32d5dSMike Smith 41115e32d5dSMike Smith /* 412bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message. 41315e32d5dSMike Smith */ 41415e32d5dSMike Smith static int 41515e32d5dSMike Smith acpi_probe(device_t dev) 41615e32d5dSMike Smith { 41715e32d5dSMike Smith 418b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 419f9390180SMitsuru IWASAKI 4205217af30SJohn Baldwin device_set_desc(dev, acpi_desc); 421bee4aa4aSNate Lawson 4222be4e471SJung-uk Kim return_VALUE (0); 42315e32d5dSMike Smith } 42415e32d5dSMike Smith 42515e32d5dSMike Smith static int 42615e32d5dSMike Smith acpi_attach(device_t dev) 42715e32d5dSMike Smith { 42815e32d5dSMike Smith struct acpi_softc *sc; 429cb9b0d80SMike Smith ACPI_STATUS status; 430ea27c63eSNate Lawson int error, state; 43132d18aa5SMike Smith UINT32 flags; 432ea27c63eSNate Lawson UINT8 TypeA, TypeB; 433498d464fSMitsuru IWASAKI char *env; 43415e32d5dSMike Smith 435b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 436bee4aa4aSNate Lawson 43715e32d5dSMike Smith sc = device_get_softc(dev); 43815e32d5dSMike Smith sc->acpi_dev = dev; 43900a30448SNate Lawson callout_init(&sc->susp_force_to, TRUE); 44015e32d5dSMike Smith 4412be4e471SJung-uk Kim error = ENXIO; 4422be4e471SJung-uk Kim 44391233413SNate Lawson /* Initialize resource manager. */ 44491233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 44591233413SNate Lawson acpi_rman_io.rm_start = 0; 44691233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 4470bba6acfSJohn Baldwin acpi_rman_io.rm_descr = "ACPI I/O ports"; 44891233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 44991233413SNate Lawson panic("acpi rman_init IO ports failed"); 45091233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 45191233413SNate Lawson acpi_rman_mem.rm_start = 0; 45291233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 4530bba6acfSJohn Baldwin acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 45491233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 45591233413SNate Lawson panic("acpi rman_init memory failed"); 45691233413SNate Lawson 4572be4e471SJung-uk Kim /* Initialise the ACPI mutex */ 4582be4e471SJung-uk Kim mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 4592be4e471SJung-uk Kim 4602be4e471SJung-uk Kim /* 4612be4e471SJung-uk Kim * Set the globals from our tunables. This is needed because ACPI-CA 4622be4e471SJung-uk Kim * uses UINT8 for some values and we have no tunable_byte. 4632be4e471SJung-uk Kim */ 4642a18c71dSJung-uk Kim AcpiGbl_AllMethodsSerialized = acpi_serialize_methods ? TRUE : FALSE; 4652a18c71dSJung-uk Kim AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE; 4662a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 4672a18c71dSJung-uk Kim 4682a18c71dSJung-uk Kim #ifndef ACPI_DEBUG 4692a18c71dSJung-uk Kim /* 4702a18c71dSJung-uk Kim * Disable all debugging layers and levels. 4712a18c71dSJung-uk Kim */ 4722a18c71dSJung-uk Kim AcpiDbgLayer = 0; 4732a18c71dSJung-uk Kim AcpiDbgLevel = 0; 4742a18c71dSJung-uk Kim #endif 4752be4e471SJung-uk Kim 4762be4e471SJung-uk Kim /* Start up the ACPI CA subsystem. */ 4772be4e471SJung-uk Kim status = AcpiInitializeSubsystem(); 4782be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4792be4e471SJung-uk Kim device_printf(dev, "Could not initialize Subsystem: %s\n", 4802be4e471SJung-uk Kim AcpiFormatException(status)); 4812be4e471SJung-uk Kim goto out; 4822be4e471SJung-uk Kim } 4832be4e471SJung-uk Kim 4842be4e471SJung-uk Kim /* Load ACPI name space. */ 4852be4e471SJung-uk Kim status = AcpiLoadTables(); 4862be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4872be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n", 4882be4e471SJung-uk Kim AcpiFormatException(status)); 4892be4e471SJung-uk Kim goto out; 4902be4e471SJung-uk Kim } 4912be4e471SJung-uk Kim 492d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 493d320e05cSJohn Baldwin /* Handle MCFG table if present. */ 494d320e05cSJohn Baldwin acpi_enable_pcie(); 495d320e05cSJohn Baldwin #endif 496d320e05cSJohn Baldwin 497be2b1797SNate Lawson /* Install the default address space handlers. */ 498be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 499be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 500be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 501be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 502be2b1797SNate Lawson AcpiFormatException(status)); 503cb9b0d80SMike Smith goto out; 50415e32d5dSMike Smith } 505be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 506be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 507be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 508be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 509be2b1797SNate Lawson AcpiFormatException(status)); 510cb9b0d80SMike Smith goto out; 51115e32d5dSMike Smith } 512be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 513be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 514be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 515be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 516be2b1797SNate Lawson AcpiFormatException(status)); 517cb9b0d80SMike Smith goto out; 51815e32d5dSMike Smith } 51915e32d5dSMike Smith 52015e32d5dSMike Smith /* 521be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 522be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 523be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 524be2b1797SNate Lawson * object init pass. 52515e32d5dSMike Smith * 52632d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 52732d18aa5SMike Smith * 528be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 529be2b1797SNate Lawson * all our child devices, but on many systems it works here. 53015e32d5dSMike Smith */ 53132d18aa5SMike Smith flags = 0; 532d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 53332d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 534bee4aa4aSNate Lawson 535bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 536b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 537be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 538be2b1797SNate Lawson AcpiFormatException(status)); 539cb9b0d80SMike Smith goto out; 54015e32d5dSMike Smith } 54115e32d5dSMike Smith 542f8335e3aSNate Lawson /* 543f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 544f8335e3aSNate Lawson * the namespace has been evaluated. 545da72d149SNate Lawson * 546da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and 547da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't 548da72d149SNate Lawson * a problem but should be addressed eventually. 549f8335e3aSNate Lawson */ 550f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 551f8335e3aSNate Lawson 552bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 553b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 554be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 555be2b1797SNate Lawson AcpiFormatException(status)); 556b69ed3f4SMitsuru IWASAKI goto out; 557b69ed3f4SMitsuru IWASAKI } 558b69ed3f4SMitsuru IWASAKI 55915e32d5dSMike Smith /* 5601d073b1dSJohn Baldwin * Setup our sysctl tree. 5611d073b1dSJohn Baldwin * 5621d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5631d073b1dSJohn Baldwin */ 5641d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5651d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5661d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5671d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5681d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 569d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 570d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 571d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5721d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5731d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5741d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5751d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5761d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5771d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5781d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5791d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 580f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 581f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 582f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 583f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 584f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 585f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5862d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 587197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 588197b4dccSNate Lawson "sleep delay"); 589ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 590197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 5911611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 592197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 593d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 594d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 595d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 596d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 597d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW, 598d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 599bf0c18ecSNate Lawson 600bf0c18ecSNate Lawson /* 6012b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 602bf0c18ecSNate Lawson * stabilize. 603bf0c18ecSNate Lawson */ 6042b9609abSNate Lawson sc->acpi_sleep_delay = 1; 6052d644607SMitsuru IWASAKI if (bootverbose) 6062d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 607965a34fbSNate Lawson if ((env = getenv("hw.acpi.verbose")) != NULL) { 608965a34fbSNate Lawson if (strcmp(env, "0") != 0) 609498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 610498d464fSMitsuru IWASAKI freeenv(env); 611498d464fSMitsuru IWASAKI } 6121d073b1dSJohn Baldwin 6132d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 614129d3046SJung-uk Kim if (AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 6152d610c46SNate Lawson sc->acpi_s4bios = 1; 6162d610c46SNate Lawson 617a0e73a12SJung-uk Kim /* Probe all supported sleep states. */ 618a0e73a12SJung-uk Kim acpi_sleep_states[ACPI_STATE_S0] = TRUE; 619a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 620a0e73a12SJung-uk Kim if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 621a0e73a12SJung-uk Kim acpi_sleep_states[state] = TRUE; 622a0e73a12SJung-uk Kim 6231d073b1dSJohn Baldwin /* 624ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 625a0e73a12SJung-uk Kim * to UNKNOWN by default to avoid surprising users. 62615e32d5dSMike Smith */ 627a0e73a12SJung-uk Kim sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 628a0e73a12SJung-uk Kim ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 629a0e73a12SJung-uk Kim sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 630a0e73a12SJung-uk Kim sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 631a0e73a12SJung-uk Kim ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 632a0e73a12SJung-uk Kim sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 633a0e73a12SJung-uk Kim ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 63415e32d5dSMike Smith 635ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 636a0e73a12SJung-uk Kim sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 63700a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 638a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) { 639ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 640ea27c63eSNate Lawson break; 641ea27c63eSNate Lawson } 642ea27c63eSNate Lawson 64313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 64415e32d5dSMike Smith 64515e32d5dSMike Smith /* 64615e32d5dSMike Smith * Scan the namespace and attach/initialise children. 64715e32d5dSMike Smith */ 64815e32d5dSMike Smith 64959e472e9SNate Lawson /* Register our shutdown handler. */ 650be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 651be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 65215e32d5dSMike Smith 65315e32d5dSMike Smith /* 65415e32d5dSMike Smith * Register our acpi event handlers. 65515e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 65615e32d5dSMike Smith */ 657be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 658be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 659be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 660be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 66115e32d5dSMike Smith 662be2b1797SNate Lawson /* Flag our initial states. */ 663a0e73a12SJung-uk Kim sc->acpi_enabled = TRUE; 66415e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 665a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 66615e32d5dSMike Smith 667be2b1797SNate Lawson /* Create the control device */ 668a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 6692a4689e8SRobert Watson "acpi"); 67015e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 67115e32d5dSMike Smith 672be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 673f86214b6SMitsuru IWASAKI goto out; 674f86214b6SMitsuru IWASAKI 675f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 676f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 677f9390180SMitsuru IWASAKI 678eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 679eeb6dba2SJohn Baldwin acpi_probe_children(dev); 680eeb6dba2SJohn Baldwin 681a0e73a12SJung-uk Kim /* Allow sleep request after a while. */ 682a0e73a12SJung-uk Kim timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 683a0e73a12SJung-uk Kim 684cb9b0d80SMike Smith error = 0; 685cb9b0d80SMike Smith 686cb9b0d80SMike Smith out: 687cb9b0d80SMike Smith return_VALUE (error); 68815e32d5dSMike Smith } 68915e32d5dSMike Smith 690169b539aSNate Lawson static int 69110ce62b9SNate Lawson acpi_suspend(device_t dev) 69210ce62b9SNate Lawson { 69310ce62b9SNate Lawson device_t child, *devlist; 69410ce62b9SNate Lawson int error, i, numdevs, pstate; 69510ce62b9SNate Lawson 696a56fe095SJohn Baldwin GIANT_REQUIRED; 697a56fe095SJohn Baldwin 69810ce62b9SNate Lawson /* First give child devices a chance to suspend. */ 69910ce62b9SNate Lawson error = bus_generic_suspend(dev); 70010ce62b9SNate Lawson if (error) 70110ce62b9SNate Lawson return (error); 70210ce62b9SNate Lawson 70310ce62b9SNate Lawson /* 70410ce62b9SNate Lawson * Now, set them into the appropriate power state, usually D3. If the 70510ce62b9SNate Lawson * device has an _SxD method for the next sleep state, use that power 70610ce62b9SNate Lawson * state instead. 70710ce62b9SNate Lawson */ 708099ea4b5SWarner Losh error = device_get_children(dev, &devlist, &numdevs); 709099ea4b5SWarner Losh if (error) 710099ea4b5SWarner Losh return (error); 71110ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 71210ce62b9SNate Lawson /* If the device is not attached, we've powered it down elsewhere. */ 71310ce62b9SNate Lawson child = devlist[i]; 71410ce62b9SNate Lawson if (!device_is_attached(child)) 71510ce62b9SNate Lawson continue; 71610ce62b9SNate Lawson 71710ce62b9SNate Lawson /* 71810ce62b9SNate Lawson * Default to D3 for all sleep states. The _SxD method is optional 71910ce62b9SNate Lawson * so set the powerstate even if it's absent. 72010ce62b9SNate Lawson */ 72110ce62b9SNate Lawson pstate = PCI_POWERSTATE_D3; 72210ce62b9SNate Lawson error = acpi_device_pwr_for_sleep(device_get_parent(child), 72310ce62b9SNate Lawson child, &pstate); 72410ce62b9SNate Lawson if ((error == 0 || error == ESRCH) && acpi_do_powerstate) 72510ce62b9SNate Lawson pci_set_powerstate(child, pstate); 72610ce62b9SNate Lawson } 72710ce62b9SNate Lawson free(devlist, M_TEMP); 72810ce62b9SNate Lawson error = 0; 72910ce62b9SNate Lawson 73010ce62b9SNate Lawson return (error); 73110ce62b9SNate Lawson } 73210ce62b9SNate Lawson 73310ce62b9SNate Lawson static int 73410ce62b9SNate Lawson acpi_resume(device_t dev) 73510ce62b9SNate Lawson { 73610ce62b9SNate Lawson ACPI_HANDLE handle; 737099ea4b5SWarner Losh int i, numdevs, error; 73810ce62b9SNate Lawson device_t child, *devlist; 73910ce62b9SNate Lawson 740a56fe095SJohn Baldwin GIANT_REQUIRED; 741a56fe095SJohn Baldwin 74210ce62b9SNate Lawson /* 74310ce62b9SNate Lawson * Put all devices in D0 before resuming them. Call _S0D on each one 74410ce62b9SNate Lawson * since some systems expect this. 74510ce62b9SNate Lawson */ 746099ea4b5SWarner Losh error = device_get_children(dev, &devlist, &numdevs); 747099ea4b5SWarner Losh if (error) 748099ea4b5SWarner Losh return (error); 74910ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 75010ce62b9SNate Lawson child = devlist[i]; 75110ce62b9SNate Lawson handle = acpi_get_handle(child); 75210ce62b9SNate Lawson if (handle) 75310ce62b9SNate Lawson AcpiEvaluateObject(handle, "_S0D", NULL, NULL); 75410ce62b9SNate Lawson if (device_is_attached(child) && acpi_do_powerstate) 75510ce62b9SNate Lawson pci_set_powerstate(child, PCI_POWERSTATE_D0); 75610ce62b9SNate Lawson } 75710ce62b9SNate Lawson free(devlist, M_TEMP); 75810ce62b9SNate Lawson 75910ce62b9SNate Lawson return (bus_generic_resume(dev)); 76010ce62b9SNate Lawson } 76110ce62b9SNate Lawson 76210ce62b9SNate Lawson static int 763169b539aSNate Lawson acpi_shutdown(device_t dev) 764169b539aSNate Lawson { 765169b539aSNate Lawson 766a56fe095SJohn Baldwin GIANT_REQUIRED; 767a56fe095SJohn Baldwin 7680e414868SNate Lawson /* Allow children to shutdown first. */ 7690e414868SNate Lawson bus_generic_shutdown(dev); 7700e414868SNate Lawson 771bee4aa4aSNate Lawson /* 772bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 773bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 774bee4aa4aSNate Lawson */ 775d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 776d4b9ff91SNate Lawson 777169b539aSNate Lawson return (0); 778169b539aSNate Lawson } 779169b539aSNate Lawson 78015e32d5dSMike Smith /* 78115e32d5dSMike Smith * Handle a new device being added 78215e32d5dSMike Smith */ 78315e32d5dSMike Smith static device_t 78415e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 78515e32d5dSMike Smith { 78615e32d5dSMike Smith struct acpi_device *ad; 78715e32d5dSMike Smith device_t child; 78815e32d5dSMike Smith 789be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 79015e32d5dSMike Smith return (NULL); 79115e32d5dSMike Smith 79215e32d5dSMike Smith resource_list_init(&ad->ad_rl); 79315e32d5dSMike Smith 79415e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 79515e32d5dSMike Smith if (child != NULL) 79615e32d5dSMike Smith device_set_ivars(child, ad); 79743ce1c77SNate Lawson else 79843ce1c77SNate Lawson free(ad, M_ACPIDEV); 79915e32d5dSMike Smith return (child); 80015e32d5dSMike Smith } 80115e32d5dSMike Smith 80215e32d5dSMike Smith static int 80315e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 80415e32d5dSMike Smith { 80515e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 80615e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 80715e32d5dSMike Smith int retval = 0; 80815e32d5dSMike Smith 80915e32d5dSMike Smith retval += bus_print_child_header(bus, child); 8109ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 8119ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 8129ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 8139ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 814a91c5fa8SNate Lawson if (device_get_flags(child)) 815a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 8162c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 81715e32d5dSMike Smith 81815e32d5dSMike Smith return (retval); 81915e32d5dSMike Smith } 82015e32d5dSMike Smith 82110ce62b9SNate Lawson /* 82210ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt 82310ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added. 82410ce62b9SNate Lawson * 82510ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and 82610ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect 82710ce62b9SNate Lawson * them to be powered up. 82810ce62b9SNate Lawson */ 82910ce62b9SNate Lawson static void 83010ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child) 83110ce62b9SNate Lawson { 83296cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 83396cf0b6dSWarner Losh pci_set_powerstate(child, PCI_POWERSTATE_D3); 83496cf0b6dSWarner Losh #endif 83510ce62b9SNate Lawson } 83610ce62b9SNate Lawson 83710ce62b9SNate Lawson /* 83810ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up. 83910ce62b9SNate Lawson * 84010ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details). 84110ce62b9SNate Lawson */ 84210ce62b9SNate Lawson static void 84310ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver) 84410ce62b9SNate Lawson { 84510ce62b9SNate Lawson device_t child, *devlist; 84610ce62b9SNate Lawson int i, numdevs; 84710ce62b9SNate Lawson 84810ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev); 849099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs)) 850099ea4b5SWarner Losh return; 85110ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 85210ce62b9SNate Lawson child = devlist[i]; 85310ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) { 85496cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 85596cf0b6dSWarner Losh pci_set_powerstate(child, PCI_POWERSTATE_D0); 85610ce62b9SNate Lawson if (device_probe_and_attach(child) != 0) 85796cf0b6dSWarner Losh pci_set_powerstate(child, PCI_POWERSTATE_D3); 85896cf0b6dSWarner Losh #else 85996cf0b6dSWarner Losh device_probe_and_attach(child); 86096cf0b6dSWarner Losh #endif 86110ce62b9SNate Lawson } 86210ce62b9SNate Lawson } 86310ce62b9SNate Lawson free(devlist, M_TEMP); 86410ce62b9SNate Lawson } 86510ce62b9SNate Lawson 86663600cc3SNate Lawson /* Location hint for devctl(8) */ 86763600cc3SNate Lawson static int 868247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 869247648afSTakanori Watanabe size_t buflen) 870247648afSTakanori Watanabe { 871247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 872247648afSTakanori Watanabe 873247648afSTakanori Watanabe if (dinfo->ad_handle) 874d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 875247648afSTakanori Watanabe else 876d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 877247648afSTakanori Watanabe return (0); 878247648afSTakanori Watanabe } 879247648afSTakanori Watanabe 88063600cc3SNate Lawson /* PnP information for devctl(8) */ 88163600cc3SNate Lawson static int 882247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 883247648afSTakanori Watanabe size_t buflen) 884247648afSTakanori Watanabe { 88563600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 88692488a57SJung-uk Kim ACPI_DEVICE_INFO *adinfo; 887247648afSTakanori Watanabe 88892488a57SJung-uk Kim if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 889d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 89092488a57SJung-uk Kim return (0); 89192488a57SJung-uk Kim } 89292488a57SJung-uk Kim 893247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 894247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 89592488a57SJung-uk Kim adinfo->HardwareId.String : "none", 89663600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 89792488a57SJung-uk Kim strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 898247648afSTakanori Watanabe AcpiOsFree(adinfo); 899247648afSTakanori Watanabe 900247648afSTakanori Watanabe return (0); 901247648afSTakanori Watanabe } 902247648afSTakanori Watanabe 903247648afSTakanori Watanabe /* 90415e32d5dSMike Smith * Handle per-device ivars 90515e32d5dSMike Smith */ 90615e32d5dSMike Smith static int 90715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 90815e32d5dSMike Smith { 90915e32d5dSMike Smith struct acpi_device *ad; 91015e32d5dSMike Smith 91115e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9122d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 91315e32d5dSMike Smith return (ENOENT); 91415e32d5dSMike Smith } 91515e32d5dSMike Smith 916be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 91715e32d5dSMike Smith switch(index) { 91815e32d5dSMike Smith case ACPI_IVAR_HANDLE: 91915e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 92015e32d5dSMike Smith break; 92115e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 92215e32d5dSMike Smith *(void **)result = ad->ad_private; 92315e32d5dSMike Smith break; 924d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 925d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 926d4b9ff91SNate Lawson break; 92732d18aa5SMike Smith case ISA_IVAR_VENDORID: 92832d18aa5SMike Smith case ISA_IVAR_SERIAL: 92932d18aa5SMike Smith case ISA_IVAR_COMPATID: 93032d18aa5SMike Smith *(int *)result = -1; 93132d18aa5SMike Smith break; 93232d18aa5SMike Smith case ISA_IVAR_LOGICALID: 93332d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 93432d18aa5SMike Smith break; 93515e32d5dSMike Smith default: 93615e32d5dSMike Smith return (ENOENT); 93715e32d5dSMike Smith } 938be2b1797SNate Lawson 93915e32d5dSMike Smith return (0); 94015e32d5dSMike Smith } 94115e32d5dSMike Smith 94215e32d5dSMike Smith static int 94315e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 94415e32d5dSMike Smith { 94515e32d5dSMike Smith struct acpi_device *ad; 94615e32d5dSMike Smith 94715e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9482d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 94915e32d5dSMike Smith return (ENOENT); 95015e32d5dSMike Smith } 95115e32d5dSMike Smith 95215e32d5dSMike Smith switch(index) { 95315e32d5dSMike Smith case ACPI_IVAR_HANDLE: 95415e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 95515e32d5dSMike Smith break; 95615e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 95715e32d5dSMike Smith ad->ad_private = (void *)value; 95815e32d5dSMike Smith break; 959d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 960d4b9ff91SNate Lawson ad->ad_flags = (int)value; 961d4b9ff91SNate Lawson break; 96215e32d5dSMike Smith default: 9632ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 96415e32d5dSMike Smith return (ENOENT); 96515e32d5dSMike Smith } 966be2b1797SNate Lawson 96715e32d5dSMike Smith return (0); 96815e32d5dSMike Smith } 96915e32d5dSMike Smith 97015e32d5dSMike Smith /* 97115e32d5dSMike Smith * Handle child resource allocation/removal 97215e32d5dSMike Smith */ 97391233413SNate Lawson static struct resource_list * 97491233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 97515e32d5dSMike Smith { 97691233413SNate Lawson struct acpi_device *ad; 97715e32d5dSMike Smith 97891233413SNate Lawson ad = device_get_ivars(child); 97991233413SNate Lawson return (&ad->ad_rl); 98015e32d5dSMike Smith } 98115e32d5dSMike Smith 9820d484d24SJohn Baldwin static int 9830d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value) 9840d484d24SJohn Baldwin { 9850d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev); 9860d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 9870d484d24SJohn Baldwin struct resource_list_entry *rle; 9880d484d24SJohn Baldwin 9890d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 9900d484d24SJohn Baldwin if (rle->type != type) 9910d484d24SJohn Baldwin continue; 9920d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value) 9930d484d24SJohn Baldwin return (1); 9940d484d24SJohn Baldwin } 9950d484d24SJohn Baldwin return (0); 9960d484d24SJohn Baldwin } 9970d484d24SJohn Baldwin 9980d484d24SJohn Baldwin /* 9990d484d24SJohn Baldwin * Wire device unit numbers based on resource matches in hints. 10000d484d24SJohn Baldwin */ 10010d484d24SJohn Baldwin static void 10020d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 10030d484d24SJohn Baldwin int *unitp) 10040d484d24SJohn Baldwin { 10050d484d24SJohn Baldwin const char *s; 10060d484d24SJohn Baldwin long value; 10070d484d24SJohn Baldwin int line, matches, unit; 10080d484d24SJohn Baldwin 10090d484d24SJohn Baldwin /* 10100d484d24SJohn Baldwin * Iterate over all the hints for the devices with the specified 10110d484d24SJohn Baldwin * name to see if one's resources are a subset of this device. 10120d484d24SJohn Baldwin */ 10130d484d24SJohn Baldwin line = 0; 10140d484d24SJohn Baldwin for (;;) { 10150d484d24SJohn Baldwin if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 10160d484d24SJohn Baldwin break; 10170d484d24SJohn Baldwin 10180d484d24SJohn Baldwin /* Must have an "at" for acpi or isa. */ 10190d484d24SJohn Baldwin resource_string_value(name, unit, "at", &s); 10200d484d24SJohn Baldwin if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 10210d484d24SJohn Baldwin strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 10220d484d24SJohn Baldwin continue; 10230d484d24SJohn Baldwin 10240d484d24SJohn Baldwin /* 10252fcd493cSJohn Baldwin * Check for matching resources. We must have at least one match. 10262fcd493cSJohn Baldwin * Since I/O and memory resources cannot be shared, if we get a 10272fcd493cSJohn Baldwin * match on either of those, ignore any mismatches in IRQs or DRQs. 10280d484d24SJohn Baldwin * 10290d484d24SJohn Baldwin * XXX: We may want to revisit this to be more lenient and wire 10300d484d24SJohn Baldwin * as long as it gets one match. 10310d484d24SJohn Baldwin */ 10320d484d24SJohn Baldwin matches = 0; 10330d484d24SJohn Baldwin if (resource_long_value(name, unit, "port", &value) == 0) { 10342fcd493cSJohn Baldwin /* 10352fcd493cSJohn Baldwin * Floppy drive controllers are notorious for having a 10362fcd493cSJohn Baldwin * wide variety of resources not all of which include the 10372fcd493cSJohn Baldwin * first port that is specified by the hint (typically 10382fcd493cSJohn Baldwin * 0x3f0) (see the comment above fdc_isa_alloc_resources() 10392fcd493cSJohn Baldwin * in fdc_isa.c). However, they do all seem to include 10402fcd493cSJohn Baldwin * port + 2 (e.g. 0x3f2) so for a floppy device, look for 10412fcd493cSJohn Baldwin * 'value + 2' in the port resources instead of the hint 10422fcd493cSJohn Baldwin * value. 10432fcd493cSJohn Baldwin */ 10442fcd493cSJohn Baldwin if (strcmp(name, "fdc") == 0) 10452fcd493cSJohn Baldwin value += 2; 10460d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 10470d484d24SJohn Baldwin matches++; 10480d484d24SJohn Baldwin else 10490d484d24SJohn Baldwin continue; 10500d484d24SJohn Baldwin } 10510d484d24SJohn Baldwin if (resource_long_value(name, unit, "maddr", &value) == 0) { 10520d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 10530d484d24SJohn Baldwin matches++; 10540d484d24SJohn Baldwin else 10550d484d24SJohn Baldwin continue; 10560d484d24SJohn Baldwin } 10572fcd493cSJohn Baldwin if (matches > 0) 10582fcd493cSJohn Baldwin goto matched; 10590d484d24SJohn Baldwin if (resource_long_value(name, unit, "irq", &value) == 0) { 10600d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 10610d484d24SJohn Baldwin matches++; 10620d484d24SJohn Baldwin else 10630d484d24SJohn Baldwin continue; 10640d484d24SJohn Baldwin } 10650d484d24SJohn Baldwin if (resource_long_value(name, unit, "drq", &value) == 0) { 10660d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 10670d484d24SJohn Baldwin matches++; 10680d484d24SJohn Baldwin else 10690d484d24SJohn Baldwin continue; 10700d484d24SJohn Baldwin } 10710d484d24SJohn Baldwin 10722fcd493cSJohn Baldwin matched: 10730d484d24SJohn Baldwin if (matches > 0) { 10740d484d24SJohn Baldwin /* We have a winner! */ 10750d484d24SJohn Baldwin *unitp = unit; 10760d484d24SJohn Baldwin break; 10770d484d24SJohn Baldwin } 10780d484d24SJohn Baldwin } 10790d484d24SJohn Baldwin } 10800d484d24SJohn Baldwin 1081adad4744SNate Lawson /* 1082adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1083adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 1084adad4744SNate Lawson */ 1085adad4744SNate Lawson static int 1086adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 1087adad4744SNate Lawson { 1088adad4744SNate Lawson struct resource *res; 1089adad4744SNate Lawson struct resource_list *rl; 1090adad4744SNate Lawson struct resource_list_entry *rle; 1091adad4744SNate Lawson struct rman *rm; 1092da72d149SNate Lawson char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1093da72d149SNate Lawson device_t *children; 1094da72d149SNate Lawson int child_count, i; 1095da72d149SNate Lawson 1096da72d149SNate Lawson /* 1097da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we 1098da72d149SNate Lawson * had multi-pass probe/attach. 1099da72d149SNate Lawson */ 1100da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0) 1101da72d149SNate Lawson return (ENXIO); 1102da72d149SNate Lawson for (i = 0; i < child_count; i++) { 1103da72d149SNate Lawson if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1104da72d149SNate Lawson device_probe_and_attach(children[i]); 1105da72d149SNate Lawson } 1106da72d149SNate Lawson free(children, M_TEMP); 1107adad4744SNate Lawson 1108adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1109be1bf4d2SPoul-Henning Kamp STAILQ_FOREACH(rle, rl, link) { 1110adad4744SNate Lawson if (rle->res != NULL) { 1111adad4744SNate Lawson device_printf(dev, "duplicate resource for %lx\n", rle->start); 1112adad4744SNate Lawson continue; 1113adad4744SNate Lawson } 1114adad4744SNate Lawson 1115adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 1116adad4744SNate Lawson switch (rle->type) { 1117adad4744SNate Lawson case SYS_RES_IOPORT: 1118adad4744SNate Lawson rm = &acpi_rman_io; 1119adad4744SNate Lawson break; 1120adad4744SNate Lawson case SYS_RES_MEMORY: 1121adad4744SNate Lawson rm = &acpi_rman_mem; 1122adad4744SNate Lawson break; 1123adad4744SNate Lawson default: 1124adad4744SNate Lawson continue; 1125adad4744SNate Lawson } 1126adad4744SNate Lawson 1127adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 1128adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1129adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1130adad4744SNate Lawson if (res != NULL) { 1131adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1132adad4744SNate Lawson rle->res = res; 1133adad4744SNate Lawson } else 1134adad4744SNate Lawson device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 1135adad4744SNate Lawson rle->start, rle->count, rle->type); 1136adad4744SNate Lawson } 1137adad4744SNate Lawson return (0); 1138adad4744SNate Lawson } 1139adad4744SNate Lawson 114015e32d5dSMike Smith static struct resource * 114115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 114215e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 114315e32d5dSMike Smith { 114495957f62SJohn Baldwin ACPI_RESOURCE ares; 114515e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 114615e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 114791233413SNate Lawson struct resource_list_entry *rle; 114891233413SNate Lawson struct resource *res; 114991233413SNate Lawson struct rman *rm; 115015e32d5dSMike Smith 115115e2f34fSNate Lawson res = NULL; 1152397c30a8SJohn Baldwin 1153397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1154397c30a8SJohn Baldwin switch (type) { 1155397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1156397c30a8SJohn Baldwin rm = &acpi_rman_io; 1157397c30a8SJohn Baldwin break; 1158397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1159397c30a8SJohn Baldwin rm = &acpi_rman_mem; 1160397c30a8SJohn Baldwin break; 1161397c30a8SJohn Baldwin default: 1162397c30a8SJohn Baldwin rm = NULL; 1163397c30a8SJohn Baldwin } 1164397c30a8SJohn Baldwin 116515e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 116615e2f34fSNate Lawson 116791233413SNate Lawson /* 116891233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 116991233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 117091233413SNate Lawson * child's resource list), use those start/end values. 117191233413SNate Lawson */ 1172f09aa88cSWarner Losh if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) { 117391233413SNate Lawson rle = resource_list_find(rl, type, *rid); 117491233413SNate Lawson if (rle == NULL) 117515e2f34fSNate Lawson goto out; 117691233413SNate Lawson start = rle->start; 117791233413SNate Lawson end = rle->end; 117891233413SNate Lawson count = rle->count; 117991233413SNate Lawson } 118091233413SNate Lawson 1181397c30a8SJohn Baldwin /* 1182397c30a8SJohn Baldwin * If this is an allocation of a specific range, see if we can satisfy 1183397c30a8SJohn Baldwin * the request from our system resource regions. If we can't, pass the 1184397c30a8SJohn Baldwin * request up to the parent. 1185397c30a8SJohn Baldwin */ 1186147c0ad0SJohn Baldwin if (start + count - 1 == end && rm != NULL) 1187397c30a8SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1188397c30a8SJohn Baldwin child); 1189397c30a8SJohn Baldwin if (res == NULL) { 119095957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 119195957f62SJohn Baldwin start, end, count, flags); 119295957f62SJohn Baldwin } else { 1193c3f861f4SWarner Losh rman_set_rid(res, *rid); 119491233413SNate Lawson 119591233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 119695957f62SJohn Baldwin if (flags & RF_ACTIVE) 119791233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 119891233413SNate Lawson rman_release_resource(res); 119915e2f34fSNate Lawson res = NULL; 120015e2f34fSNate Lawson goto out; 120191233413SNate Lawson } 120295957f62SJohn Baldwin } 120391233413SNate Lawson 120495957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 120595957f62SJohn Baldwin switch (type) { 120695957f62SJohn Baldwin case SYS_RES_IRQ: 120795957f62SJohn Baldwin /* 120895957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 120995957f62SJohn Baldwin * configure the interrupt associated with a device when we 121095957f62SJohn Baldwin * parse the resources but have to defer it until a driver 121195957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 121295957f62SJohn Baldwin * 121395957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 121495957f62SJohn Baldwin */ 121595957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 121695957f62SJohn Baldwin acpi_config_intr(child, &ares); 121795957f62SJohn Baldwin break; 121895957f62SJohn Baldwin } 121915e2f34fSNate Lawson 122015e2f34fSNate Lawson out: 122115e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 122291233413SNate Lawson return (res); 122315e32d5dSMike Smith } 122415e32d5dSMike Smith 122515e32d5dSMike Smith static int 122691233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 122791233413SNate Lawson struct resource *r) 122815e32d5dSMike Smith { 1229397c30a8SJohn Baldwin struct rman *rm; 123091233413SNate Lawson int ret; 123115e32d5dSMike Smith 1232397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1233397c30a8SJohn Baldwin switch (type) { 1234397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1235397c30a8SJohn Baldwin rm = &acpi_rman_io; 1236397c30a8SJohn Baldwin break; 1237397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1238397c30a8SJohn Baldwin rm = &acpi_rman_mem; 1239397c30a8SJohn Baldwin break; 1240397c30a8SJohn Baldwin default: 1241397c30a8SJohn Baldwin rm = NULL; 1242397c30a8SJohn Baldwin } 1243397c30a8SJohn Baldwin 124415e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 124515e2f34fSNate Lawson 124691233413SNate Lawson /* 1247397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers, 1248397c30a8SJohn Baldwin * deactivate it and release it to the local pool. If it doesn't, 1249397c30a8SJohn Baldwin * pass this request up to the parent. 125091233413SNate Lawson */ 1251397c30a8SJohn Baldwin if (rm != NULL && rman_is_region_manager(r, rm)) { 125291233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 125391233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 125491233413SNate Lawson if (ret != 0) 125515e2f34fSNate Lawson goto out; 125691233413SNate Lawson } 125791233413SNate Lawson ret = rman_release_resource(r); 125891233413SNate Lawson } else 125991233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 126091233413SNate Lawson 126115e2f34fSNate Lawson out: 126215e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 126391233413SNate Lawson return (ret); 126415e32d5dSMike Smith } 126515e32d5dSMike Smith 12668b28b622SNate Lawson static void 12678b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid) 12688b28b622SNate Lawson { 12698b28b622SNate Lawson struct resource_list *rl; 12708b28b622SNate Lawson 12718b28b622SNate Lawson rl = acpi_get_rlist(bus, child); 12728b28b622SNate Lawson resource_list_delete(rl, type, rid); 12738b28b622SNate Lawson } 12748b28b622SNate Lawson 1275dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 1276e1c4bf3fSNate Lawson int 1277e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1278907b6777SNate Lawson struct resource **res, u_int flags) 1279dc750869SNate Lawson { 1280e1c4bf3fSNate Lawson int error, res_type; 1281dc750869SNate Lawson 1282e1c4bf3fSNate Lawson error = ENOMEM; 12838f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1284e1c4bf3fSNate Lawson return (EINVAL); 1285dc750869SNate Lawson 12868f118e25SNate Lawson /* We only support memory and IO spaces. */ 12872be4e471SJung-uk Kim switch (gas->SpaceId) { 1288dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1289e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY; 1290dc750869SNate Lawson break; 1291dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 1292e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT; 1293dc750869SNate Lawson break; 1294dc750869SNate Lawson default: 1295e1c4bf3fSNate Lawson return (EOPNOTSUPP); 1296dc750869SNate Lawson } 1297dc750869SNate Lawson 12982fe912dfSNate Lawson /* 1299f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means 1300f45fc848SNate Lawson * it is a bit field and just allocate a byte. 13012fe912dfSNate Lawson */ 13022be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8) 13032be4e471SJung-uk Kim gas->BitWidth = 8; 13042fe912dfSNate Lawson 13058f118e25SNate Lawson /* Validate the address after we're sure we support the space. */ 13062be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0) 13078f118e25SNate Lawson return (EINVAL); 13088f118e25SNate Lawson 1309e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address, 13102be4e471SJung-uk Kim gas->BitWidth / 8); 1311907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1312e1c4bf3fSNate Lawson if (*res != NULL) { 1313e1c4bf3fSNate Lawson *type = res_type; 1314e1c4bf3fSNate Lawson error = 0; 1315ca2c69c8SNate Lawson } else 1316ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid); 1317ca2c69c8SNate Lawson 1318e1c4bf3fSNate Lawson return (error); 1319dc750869SNate Lawson } 1320dc750869SNate Lawson 1321c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 13221e4925e8SNate Lawson static uint32_t 132332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 1324bc0f2195SMike Smith { 13251e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1326bc0f2195SMike Smith ACPI_HANDLE h; 132792488a57SJung-uk Kim uint32_t pnpid; 132832d18aa5SMike Smith 1329b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 133032d18aa5SMike Smith 13316fca9360SNate Lawson /* Fetch and validate the HID. */ 133292488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 133392488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 133492488a57SJung-uk Kim return_VALUE (0); 133532d18aa5SMike Smith 133692488a57SJung-uk Kim pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 133792488a57SJung-uk Kim devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 133892488a57SJung-uk Kim PNP_EISAID(devinfo->HardwareId.String) : 0; 133992488a57SJung-uk Kim AcpiOsFree(devinfo); 1340be2b1797SNate Lawson 134132d18aa5SMike Smith return_VALUE (pnpid); 134232d18aa5SMike Smith } 134332d18aa5SMike Smith 13441e4925e8SNate Lawson static int 13451e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1346b2566207STakanori Watanabe { 13471e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 134892488a57SJung-uk Kim ACPI_DEVICE_ID *ids; 1349b2566207STakanori Watanabe ACPI_HANDLE h; 13501e4925e8SNate Lawson uint32_t *pnpid; 135192488a57SJung-uk Kim int i, valid; 1352b2566207STakanori Watanabe 1353b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1354b2566207STakanori Watanabe 13551e4925e8SNate Lawson pnpid = cids; 1356bd189fe7SNate Lawson 13571e4925e8SNate Lawson /* Fetch and validate the CID */ 135892488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 135992488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 136092488a57SJung-uk Kim return_VALUE (0); 1361b2566207STakanori Watanabe 136292488a57SJung-uk Kim if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 136392488a57SJung-uk Kim AcpiOsFree(devinfo); 136492488a57SJung-uk Kim return_VALUE (0); 1365b2566207STakanori Watanabe } 1366b2566207STakanori Watanabe 136792488a57SJung-uk Kim if (devinfo->CompatibleIdList.Count < count) 136892488a57SJung-uk Kim count = devinfo->CompatibleIdList.Count; 136992488a57SJung-uk Kim ids = devinfo->CompatibleIdList.Ids; 137092488a57SJung-uk Kim for (i = 0, valid = 0; i < count; i++) 137192488a57SJung-uk Kim if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 137292488a57SJung-uk Kim strncmp(ids[i].String, "PNP", 3) == 0) { 137392488a57SJung-uk Kim *pnpid++ = PNP_EISAID(ids[i].String); 137492488a57SJung-uk Kim valid++; 137592488a57SJung-uk Kim } 137692488a57SJung-uk Kim AcpiOsFree(devinfo); 137792488a57SJung-uk Kim 13781e4925e8SNate Lawson return_VALUE (valid); 13791e4925e8SNate Lawson } 1380b2566207STakanori Watanabe 1381ce619b2eSNate Lawson static char * 1382ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1383ce619b2eSNate Lawson { 1384ce619b2eSNate Lawson ACPI_HANDLE h; 138592488a57SJung-uk Kim ACPI_OBJECT_TYPE t; 1386ce619b2eSNate Lawson int i; 1387ce619b2eSNate Lawson 1388ce619b2eSNate Lawson h = acpi_get_handle(dev); 138992488a57SJung-uk Kim if (ids == NULL || h == NULL) 139092488a57SJung-uk Kim return (NULL); 139192488a57SJung-uk Kim t = acpi_get_type(dev); 139292488a57SJung-uk Kim if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 1393ce619b2eSNate Lawson return (NULL); 1394ce619b2eSNate Lawson 1395ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1396ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1397ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1398ce619b2eSNate Lawson return (ids[i]); 1399ce619b2eSNate Lawson } 1400ce619b2eSNate Lawson return (NULL); 1401ce619b2eSNate Lawson } 1402ce619b2eSNate Lawson 1403ce619b2eSNate Lawson static ACPI_STATUS 1404ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1405ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1406ce619b2eSNate Lawson { 1407ce619b2eSNate Lawson ACPI_HANDLE h; 1408ce619b2eSNate Lawson 1409df8d2a32SNate Lawson if (dev == NULL) 1410df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1411df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1412ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1413ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1414ce619b2eSNate Lawson } 1415ce619b2eSNate Lawson 141662508c53SJohn Baldwin int 141710ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 141810ce62b9SNate Lawson { 141910ce62b9SNate Lawson struct acpi_softc *sc; 142010ce62b9SNate Lawson ACPI_HANDLE handle; 142110ce62b9SNate Lawson ACPI_STATUS status; 142210ce62b9SNate Lawson char sxd[8]; 142310ce62b9SNate Lawson int error; 142410ce62b9SNate Lawson 142510ce62b9SNate Lawson sc = device_get_softc(bus); 142610ce62b9SNate Lawson handle = acpi_get_handle(dev); 142710ce62b9SNate Lawson 142810ce62b9SNate Lawson /* 142910ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down. 143010ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when 143110ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may 143210ce62b9SNate Lawson * need special handling in their drivers. 143310ce62b9SNate Lawson */ 143410ce62b9SNate Lawson if (handle == NULL || 143510ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") || 143610ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") || 143710ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") || 143810ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") || 143910ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511")) 144010ce62b9SNate Lawson return (ENXIO); 144110ce62b9SNate Lawson 144210ce62b9SNate Lawson /* 144310ce62b9SNate Lawson * Override next state with the value from _SxD, if present. If no 144410ce62b9SNate Lawson * dstate argument was provided, don't fetch the return value. 144510ce62b9SNate Lawson */ 144610ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 144710ce62b9SNate Lawson if (dstate) 144810ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate); 144910ce62b9SNate Lawson else 145010ce62b9SNate Lawson status = AcpiEvaluateObject(handle, sxd, NULL, NULL); 145110ce62b9SNate Lawson 145210ce62b9SNate Lawson switch (status) { 145310ce62b9SNate Lawson case AE_OK: 145410ce62b9SNate Lawson error = 0; 145510ce62b9SNate Lawson break; 145610ce62b9SNate Lawson case AE_NOT_FOUND: 145710ce62b9SNate Lawson error = ESRCH; 145810ce62b9SNate Lawson break; 145910ce62b9SNate Lawson default: 146010ce62b9SNate Lawson error = ENXIO; 146110ce62b9SNate Lawson break; 146210ce62b9SNate Lawson } 146310ce62b9SNate Lawson 146410ce62b9SNate Lawson return (error); 146510ce62b9SNate Lawson } 146610ce62b9SNate Lawson 1467df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1468df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1469df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1470df8d2a32SNate Lawson void *arg; 1471df8d2a32SNate Lawson ACPI_HANDLE parent; 1472df8d2a32SNate Lawson }; 1473df8d2a32SNate Lawson 1474ce619b2eSNate Lawson static ACPI_STATUS 1475df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1476df8d2a32SNate Lawson { 1477df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1478df8d2a32SNate Lawson device_t dev, old_dev; 1479df8d2a32SNate Lawson ACPI_STATUS status; 1480df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1481df8d2a32SNate Lawson 1482df8d2a32SNate Lawson /* 1483df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1484df8d2a32SNate Lawson * the parent where the scan began. 1485df8d2a32SNate Lawson */ 1486df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1487df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1488df8d2a32SNate Lawson return (AE_OK); 1489df8d2a32SNate Lawson 1490df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1491df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1492df8d2a32SNate Lawson return (AE_OK); 1493df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1494df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1495df8d2a32SNate Lawson return (AE_OK); 1496df8d2a32SNate Lawson 1497df8d2a32SNate Lawson /* 1498df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1499df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1500df8d2a32SNate Lawson */ 1501df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1502df8d2a32SNate Lawson dev = old_dev; 1503df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1504df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1505df8d2a32SNate Lawson return (status); 1506df8d2a32SNate Lawson 1507df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1508df8d2a32SNate Lawson if (old_dev != NULL) { 1509df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1510df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1511df8d2a32SNate Lawson } 1512df8d2a32SNate Lawson 1513df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1514df8d2a32SNate Lawson if (dev != NULL) 1515df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1516df8d2a32SNate Lawson 1517df8d2a32SNate Lawson return (AE_OK); 1518df8d2a32SNate Lawson } 1519df8d2a32SNate Lawson 1520df8d2a32SNate Lawson static ACPI_STATUS 1521df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1522df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1523ce619b2eSNate Lawson { 1524ce619b2eSNate Lawson ACPI_HANDLE h; 1525df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1526ce619b2eSNate Lawson 1527df8d2a32SNate Lawson if (acpi_disabled("children")) 1528df8d2a32SNate Lawson return (AE_OK); 1529df8d2a32SNate Lawson 1530df8d2a32SNate Lawson if (dev == NULL) 1531df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1532df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1533ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1534df8d2a32SNate Lawson ctx.user_fn = user_fn; 1535df8d2a32SNate Lawson ctx.arg = arg; 1536df8d2a32SNate Lawson ctx.parent = h; 1537df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 15382272d050SJung-uk Kim acpi_device_scan_cb, NULL, &ctx, NULL)); 1539ce619b2eSNate Lawson } 1540ce619b2eSNate Lawson 154110ce62b9SNate Lawson /* 154210ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting 154310ce62b9SNate Lawson * device power states since it's close enough to ACPI. 154410ce62b9SNate Lawson */ 154510ce62b9SNate Lawson static int 154610ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state) 154710ce62b9SNate Lawson { 154810ce62b9SNate Lawson ACPI_HANDLE h; 154910ce62b9SNate Lawson ACPI_STATUS status; 155010ce62b9SNate Lawson int error; 155110ce62b9SNate Lawson 155210ce62b9SNate Lawson error = 0; 155310ce62b9SNate Lawson h = acpi_get_handle(child); 1554a0e73a12SJung-uk Kim if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 155510ce62b9SNate Lawson return (EINVAL); 155610ce62b9SNate Lawson if (h == NULL) 155710ce62b9SNate Lawson return (0); 155810ce62b9SNate Lawson 155910ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */ 156010ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state); 156110ce62b9SNate Lawson if (ACPI_FAILURE(status) && status != AE_NOT_FOUND 156210ce62b9SNate Lawson && status != AE_BAD_PARAMETER) 156310ce62b9SNate Lawson device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n", 156410ce62b9SNate Lawson state, acpi_name(h), AcpiFormatException(status)); 156510ce62b9SNate Lawson 156610ce62b9SNate Lawson return (error); 156710ce62b9SNate Lawson } 156810ce62b9SNate Lawson 156932d18aa5SMike Smith static int 157032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 157132d18aa5SMike Smith { 15721e4925e8SNate Lawson int result, cid_count, i; 15731e4925e8SNate Lawson uint32_t lid, cids[8]; 1574bc0f2195SMike Smith 1575b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1576bc0f2195SMike Smith 1577bc0f2195SMike Smith /* 1578bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1579bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1580bc0f2195SMike Smith * that to happen, so don't ever return it. 1581bc0f2195SMike Smith */ 1582bc0f2195SMike Smith result = ENXIO; 1583bc0f2195SMike Smith 1584be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1585b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 15861e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1587bc0f2195SMike Smith while (ids && ids->ip_id) { 15881e4925e8SNate Lawson if (lid == ids->ip_id) { 1589bc0f2195SMike Smith result = 0; 1590bc0f2195SMike Smith goto out; 1591bc0f2195SMike Smith } 15921e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 15931e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 15941e4925e8SNate Lawson result = 0; 15951e4925e8SNate Lawson goto out; 15961e4925e8SNate Lawson } 15971e4925e8SNate Lawson } 1598bc0f2195SMike Smith ids++; 1599bc0f2195SMike Smith } 1600be2b1797SNate Lawson 1601bc0f2195SMike Smith out: 16029e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 16039e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 16049e0dd54fSNate Lawson 1605bc0f2195SMike Smith return_VALUE (result); 1606bc0f2195SMike Smith } 1607bc0f2195SMike Smith 1608d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1609d320e05cSJohn Baldwin /* 1610d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for 1611d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory 1612d320e05cSJohn Baldwin * map. 1613d320e05cSJohn Baldwin */ 1614d320e05cSJohn Baldwin static void 1615d320e05cSJohn Baldwin acpi_enable_pcie(void) 1616d320e05cSJohn Baldwin { 1617d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr; 1618d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end; 1619d320e05cSJohn Baldwin ACPI_STATUS status; 1620d320e05cSJohn Baldwin 1621d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1622d320e05cSJohn Baldwin if (ACPI_FAILURE(status)) 1623d320e05cSJohn Baldwin return; 1624d320e05cSJohn Baldwin 1625d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1626d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1627d320e05cSJohn Baldwin while (alloc < end) { 1628d320e05cSJohn Baldwin if (alloc->PciSegment == 0) { 1629d320e05cSJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1630d320e05cSJohn Baldwin alloc->EndBusNumber); 1631d320e05cSJohn Baldwin return; 1632d320e05cSJohn Baldwin } 1633d320e05cSJohn Baldwin alloc++; 1634d320e05cSJohn Baldwin } 1635d320e05cSJohn Baldwin } 1636d320e05cSJohn Baldwin #endif 1637d320e05cSJohn Baldwin 1638bc0f2195SMike Smith /* 163933332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices. 164015e32d5dSMike Smith * 164133332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 164233332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 164333332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link 164433332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the 164533332dc2SNate Lawson * type of namespace nodes, so this should be ok. 164615e32d5dSMike Smith */ 164715e32d5dSMike Smith static void 164815e32d5dSMike Smith acpi_probe_children(device_t bus) 164915e32d5dSMike Smith { 165015e32d5dSMike Smith 1651b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16520ae55423SMike Smith 165315e32d5dSMike Smith /* 165415e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1655edc13633SNate Lawson * we find. We also probe/attach any early devices. 165615e32d5dSMike Smith * 165715e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1658be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1659be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1660be2b1797SNate Lawson * devices as they appear, which might be smarter.) 166115e32d5dSMike Smith */ 16624c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 166333332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 16642272d050SJung-uk Kim NULL, bus, NULL); 166515e32d5dSMike Smith 1666adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1667adad4744SNate Lawson acpi_sysres_alloc(bus); 1668adad4744SNate Lawson 1669edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1670edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1671edc13633SNate Lawson bus_generic_probe(bus); 1672edc13633SNate Lawson 1673edc13633SNate Lawson /* Probe/attach all children, created staticly and from the namespace. */ 16740430ba9eSAndriy Gapon ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 167515e32d5dSMike Smith bus_generic_attach(bus); 16760ae55423SMike Smith 167788a79fc0SNate Lawson /* Attach wake sysctls. */ 16785c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 167988a79fc0SNate Lawson 1680bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 16810ae55423SMike Smith return_VOID; 168215e32d5dSMike Smith } 168315e32d5dSMike Smith 1684b82ca9a9SNate Lawson /* 1685d227204dSJohn Baldwin * Determine the probe order for a given device. 1686b82ca9a9SNate Lawson */ 1687d227204dSJohn Baldwin static void 1688b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 168991233413SNate Lawson { 1690463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type; 169191233413SNate Lawson 1692b82ca9a9SNate Lawson /* 1693*b1f9b996SAndriy Gapon * 1. CPUs 1694*b1f9b996SAndriy Gapon * 2. I/O port and memory system resource holders 1695*b1f9b996SAndriy Gapon * 3. Embedded controllers (to handle early accesses) 1696*b1f9b996SAndriy Gapon * 4. PCI Link Devices 1697b82ca9a9SNate Lawson */ 1698463e0f91SJohn Baldwin AcpiGetType(handle, &type); 1699aa835160SAndriy Gapon if (type == ACPI_TYPE_PROCESSOR) 170091233413SNate Lawson *order = 1; 1701aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) 170291233413SNate Lawson *order = 2; 1703aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C09")) 1704b460c6f8SJohn Baldwin *order = 3; 1705aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C0F")) 1706aa835160SAndriy Gapon *order = 4; 170791233413SNate Lawson } 170891233413SNate Lawson 170915e32d5dSMike Smith /* 171015e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 171115e32d5dSMike Smith * it. 171215e32d5dSMike Smith */ 171315e32d5dSMike Smith static ACPI_STATUS 171415e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 171515e32d5dSMike Smith { 171615e32d5dSMike Smith ACPI_OBJECT_TYPE type; 1717858a52f4SMitsuru IWASAKI ACPI_HANDLE h; 171833332dc2SNate Lawson device_t bus, child; 1719da72d149SNate Lawson int order; 172033332dc2SNate Lawson char *handle_str, **search; 172133332dc2SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL}; 172215e32d5dSMike Smith 1723b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 17240ae55423SMike Smith 1725be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 17260ae55423SMike Smith if (acpi_avoid(handle)) 17270ae55423SMike Smith return_ACPI_STATUS (AE_OK); 17280ae55423SMike Smith 172991233413SNate Lawson bus = (device_t)context; 1730b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 173115e32d5dSMike Smith switch (type) { 173215e32d5dSMike Smith case ACPI_TYPE_DEVICE: 173315e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 173415e32d5dSMike Smith case ACPI_TYPE_THERMAL: 173515e32d5dSMike Smith case ACPI_TYPE_POWER: 17360ae55423SMike Smith if (acpi_disabled("children")) 17370ae55423SMike Smith break; 1738be2b1797SNate Lawson 173915e32d5dSMike Smith /* 174033332dc2SNate Lawson * Since we scan from \, be sure to skip system scope objects. 174133332dc2SNate Lawson * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?) 174233332dc2SNate Lawson */ 174333332dc2SNate Lawson handle_str = acpi_name(handle); 174433332dc2SNate Lawson for (search = scopes; *search != NULL; search++) { 174533332dc2SNate Lawson if (strcmp(handle_str, *search) == 0) 174633332dc2SNate Lawson break; 174733332dc2SNate Lawson } 174833332dc2SNate Lawson if (*search != NULL) 174933332dc2SNate Lawson break; 175033332dc2SNate Lawson 175133332dc2SNate Lawson /* 1752463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the 1753463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run 1754463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1755463e0f91SJohn Baldwin * are reserved for special objects (i.e., system 1756*b1f9b996SAndriy Gapon * resources). 175715e32d5dSMike Smith */ 175833332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1759d227204dSJohn Baldwin order = level * 10 + 100; 1760da72d149SNate Lawson acpi_probe_order(handle, &order); 176191233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1762bc0f2195SMike Smith if (child == NULL) 1763bc0f2195SMike Smith break; 176459a890e6SNate Lawson 176559a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 176615e32d5dSMike Smith acpi_set_handle(child, handle); 176759a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1768bc0f2195SMike Smith 1769bc0f2195SMike Smith /* 1770b519f9d6SMike Smith * Check that the device is present. If it's not present, 1771b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1772b519f9d6SMike Smith * the handle, but we don't probe it). 1773893f750aSNate Lawson * 1774893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not 1775893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them 1776893f750aSNate Lawson * anyway since we may enable them later. 1777b519f9d6SMike Smith */ 1778858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1779858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */ 1780858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F")) 1781858a52f4SMitsuru IWASAKI break; 1782858a52f4SMitsuru IWASAKI /* 1783858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system 1784858a52f4SMitsuru IWASAKI * may be undocked at boot. 1785858a52f4SMitsuru IWASAKI */ 1786858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1787858a52f4SMitsuru IWASAKI break; 1788858a52f4SMitsuru IWASAKI 1789b519f9d6SMike Smith device_disable(child); 1790b519f9d6SMike Smith break; 1791b519f9d6SMike Smith } 1792b519f9d6SMike Smith 1793b519f9d6SMike Smith /* 1794bc0f2195SMike Smith * Get the device's resource settings and attach them. 1795bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1796bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1797bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1798bc0f2195SMike Smith * device not to have any resources. 1799bc0f2195SMike Smith */ 180072ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 18010ae55423SMike Smith break; 180215e32d5dSMike Smith } 180315e32d5dSMike Smith } 1804be2b1797SNate Lawson 18050ae55423SMike Smith return_ACPI_STATUS (AE_OK); 180615e32d5dSMike Smith } 180715e32d5dSMike Smith 180859a890e6SNate Lawson /* 180959a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 181059a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 181159a890e6SNate Lawson */ 181259a890e6SNate Lawson void 181392488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data) 181459a890e6SNate Lawson { 181559a890e6SNate Lawson } 181659a890e6SNate Lawson 181715e32d5dSMike Smith static void 181815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 181915e32d5dSMike Smith { 18202d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 1821d33f4987STakanori Watanabe ACPI_STATUS status; 1822eeb3a05fSNate Lawson 1823eeb3a05fSNate Lawson /* 182480f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 182580f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 182680f0e4c2SNate Lawson * an AP. 1827eeb3a05fSNate Lawson */ 1828eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1829904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1830904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 18312d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1832904bf0c2SNate Lawson AcpiFormatException(status)); 1833904bf0c2SNate Lawson return; 1834904bf0c2SNate Lawson } 18352d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Powering system off\n"); 183655398047SNate Lawson ACPI_DISABLE_IRQS(); 1837865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 18382d0c82e8SJung-uk Kim if (ACPI_FAILURE(status)) 18392d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - %s\n", 18402d0c82e8SJung-uk Kim AcpiFormatException(status)); 18412d0c82e8SJung-uk Kim else { 184215e32d5dSMike Smith DELAY(1000000); 18432d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 184415e32d5dSMike Smith } 18452be4e471SJung-uk Kim } else if ((howto & RB_HALT) == 0 && 18462be4e471SJung-uk Kim (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) && 1847d1b16e18SNate Lawson sc->acpi_handle_reboot) { 1848d85e6785SNate Lawson /* Reboot using the reset register. */ 1849129d3046SJung-uk Kim status = AcpiWrite( 18502be4e471SJung-uk Kim AcpiGbl_FADT.ResetValue, &AcpiGbl_FADT.ResetRegister); 18512d0c82e8SJung-uk Kim if (ACPI_FAILURE(status)) 18522d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - %s\n", 18532d0c82e8SJung-uk Kim AcpiFormatException(status)); 18542d0c82e8SJung-uk Kim else { 185587a500cdSNate Lawson DELAY(1000000); 18562d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - timeout\n"); 185787a500cdSNate Lawson } 1858d85e6785SNate Lawson } else if (sc->acpi_do_disable && panicstr == NULL) { 1859d85e6785SNate Lawson /* 1860d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing 1861d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system. 1862d85e6785SNate Lawson */ 18632d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Shutting down\n"); 186480f0e4c2SNate Lawson AcpiTerminate(); 186580f0e4c2SNate Lawson } 186615e32d5dSMike Smith } 186715e32d5dSMike Smith 186813d4f7baSMitsuru IWASAKI static void 186913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 187013d4f7baSMitsuru IWASAKI { 187113d4f7baSMitsuru IWASAKI static int first_time = 1; 187213d4f7baSMitsuru IWASAKI 187313d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 18742be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 187559ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 187613d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 187733febf93SNate Lawson acpi_event_power_button_sleep, sc); 1878be2b1797SNate Lawson if (first_time) 18796c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 188013d4f7baSMitsuru IWASAKI } 18812be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 188259ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 188313d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 188433febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1885be2b1797SNate Lawson if (first_time) 18866c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 188713d4f7baSMitsuru IWASAKI } 188813d4f7baSMitsuru IWASAKI 188913d4f7baSMitsuru IWASAKI first_time = 0; 189013d4f7baSMitsuru IWASAKI } 189113d4f7baSMitsuru IWASAKI 189215e32d5dSMike Smith /* 1893c5ba0be4SMike Smith * Returns true if the device is actually present and should 1894c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1895c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1896c5ba0be4SMike Smith */ 1897c5ba0be4SMike Smith BOOLEAN 1898c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1899c5ba0be4SMike Smith { 19001e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1901c5ba0be4SMike Smith ACPI_HANDLE h; 190292488a57SJung-uk Kim BOOLEAN present; 1903c5ba0be4SMike Smith 190492488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 190592488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1906c5ba0be4SMike Smith return (FALSE); 1907be2b1797SNate Lawson 1908be2b1797SNate Lawson /* If no _STA method, must be present */ 190992488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 191092488a57SJung-uk Kim ACPI_DEVICE_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 1911be2b1797SNate Lawson 191292488a57SJung-uk Kim AcpiOsFree(devinfo); 191392488a57SJung-uk Kim return (present); 1914c5ba0be4SMike Smith } 1915c5ba0be4SMike Smith 1916c5ba0be4SMike Smith /* 1917b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1918b53f2771SMike Smith */ 1919b53f2771SMike Smith BOOLEAN 1920b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1921b53f2771SMike Smith { 19221e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1923b53f2771SMike Smith ACPI_HANDLE h; 192492488a57SJung-uk Kim BOOLEAN present; 1925b53f2771SMike Smith 192692488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 192792488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 1928b53f2771SMike Smith return (FALSE); 1929be2b1797SNate Lawson 1930be2b1797SNate Lawson /* If no _STA method, must be present */ 193192488a57SJung-uk Kim present = (devinfo->Valid & ACPI_VALID_STA) == 0 || 193292488a57SJung-uk Kim ACPI_BATTERY_PRESENT(devinfo->CurrentStatus) ? TRUE : FALSE; 1933be2b1797SNate Lawson 193492488a57SJung-uk Kim AcpiOsFree(devinfo); 193592488a57SJung-uk Kim return (present); 1936b53f2771SMike Smith } 1937b53f2771SMike Smith 1938b53f2771SMike Smith /* 193991233413SNate Lawson * Match a HID string against a handle 194015e32d5dSMike Smith */ 19413c4c08dcSAlexander Motin BOOLEAN 1942ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 194315e32d5dSMike Smith { 19441e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 194592488a57SJung-uk Kim BOOLEAN ret; 194692488a57SJung-uk Kim int i; 194792488a57SJung-uk Kim 194892488a57SJung-uk Kim if (hid == NULL || h == NULL || 194992488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 195092488a57SJung-uk Kim return (FALSE); 195115e32d5dSMike Smith 19521e4925e8SNate Lawson ret = FALSE; 1953c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 195492488a57SJung-uk Kim strcmp(hid, devinfo->HardwareId.String) == 0) 19551e4925e8SNate Lawson ret = TRUE; 195692488a57SJung-uk Kim else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 195792488a57SJung-uk Kim for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 195892488a57SJung-uk Kim if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 19591e4925e8SNate Lawson ret = TRUE; 19601e4925e8SNate Lawson break; 19611e4925e8SNate Lawson } 19621e4925e8SNate Lawson } 1963be2b1797SNate Lawson 196492488a57SJung-uk Kim AcpiOsFree(devinfo); 19651e4925e8SNate Lawson return (ret); 196615e32d5dSMike Smith } 196715e32d5dSMike Smith 196815e32d5dSMike Smith /* 1969c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1970c5ba0be4SMike Smith * or one if its parents. 1971c5ba0be4SMike Smith */ 1972c5ba0be4SMike Smith ACPI_STATUS 1973c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1974c5ba0be4SMike Smith { 1975c5ba0be4SMike Smith ACPI_HANDLE r; 1976c5ba0be4SMike Smith ACPI_STATUS status; 1977c5ba0be4SMike Smith 1978be2b1797SNate Lawson /* Walk back up the tree to the root */ 1979c5ba0be4SMike Smith for (;;) { 1980be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1981be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1982c5ba0be4SMike Smith *result = r; 1983c5ba0be4SMike Smith return (AE_OK); 1984c5ba0be4SMike Smith } 1985bee4aa4aSNate Lawson /* XXX Return error here? */ 1986c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1987c5ba0be4SMike Smith return (AE_OK); 1988b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1989c5ba0be4SMike Smith return (AE_NOT_FOUND); 1990c5ba0be4SMike Smith parent = r; 1991c5ba0be4SMike Smith } 1992c5ba0be4SMike Smith } 1993c5ba0be4SMike Smith 1994eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1995eea17c34SNate Lawson uint32_t 1996eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1997eea17c34SNate Lawson { 1998eea17c34SNate Lawson uint32_t delta; 1999eea17c34SNate Lawson 2000eea17c34SNate Lawson if (end >= start) 2001eea17c34SNate Lawson delta = end - start; 20022be4e471SJung-uk Kim else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 2003eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 20042be4e471SJung-uk Kim else 20052be4e471SJung-uk Kim delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 2006eea17c34SNate Lawson return (delta); 2007eea17c34SNate Lawson } 2008eea17c34SNate Lawson 2009c5ba0be4SMike Smith /* 2010c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 2011c5ba0be4SMike Smith */ 2012c5ba0be4SMike Smith ACPI_BUFFER * 2013c5ba0be4SMike Smith acpi_AllocBuffer(int size) 2014c5ba0be4SMike Smith { 2015c5ba0be4SMike Smith ACPI_BUFFER *buf; 2016c5ba0be4SMike Smith 2017c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2018c5ba0be4SMike Smith return (NULL); 2019c5ba0be4SMike Smith buf->Length = size; 2020c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 2021c5ba0be4SMike Smith return (buf); 2022c5ba0be4SMike Smith } 2023c5ba0be4SMike Smith 2024c310653eSNate Lawson ACPI_STATUS 2025cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2026c310653eSNate Lawson { 2027c310653eSNate Lawson ACPI_OBJECT arg1; 2028c310653eSNate Lawson ACPI_OBJECT_LIST args; 2029c310653eSNate Lawson 2030c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 2031c310653eSNate Lawson arg1.Integer.Value = number; 2032c310653eSNate Lawson args.Count = 1; 2033c310653eSNate Lawson args.Pointer = &arg1; 2034c310653eSNate Lawson 2035c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 2036c310653eSNate Lawson } 2037c310653eSNate Lawson 2038c5ba0be4SMike Smith /* 2039c5ba0be4SMike Smith * Evaluate a path that should return an integer. 2040c5ba0be4SMike Smith */ 2041c5ba0be4SMike Smith ACPI_STATUS 2042cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2043c5ba0be4SMike Smith { 2044be2b1797SNate Lawson ACPI_STATUS status; 2045c5ba0be4SMike Smith ACPI_BUFFER buf; 2046c573e654SMitsuru IWASAKI ACPI_OBJECT param; 2047c5ba0be4SMike Smith 2048c5ba0be4SMike Smith if (handle == NULL) 2049c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 20500c7da7acSJohn Baldwin 20510c7da7acSJohn Baldwin /* 20520c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 20530c7da7acSJohn Baldwin * a method that will return an Integer. 20540c7da7acSJohn Baldwin */ 2055c5ba0be4SMike Smith buf.Pointer = ¶m; 2056c5ba0be4SMike Smith buf.Length = sizeof(param); 2057be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2058be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2059be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 2060c5ba0be4SMike Smith *number = param.Integer.Value; 2061be2b1797SNate Lawson else 2062be2b1797SNate Lawson status = AE_TYPE; 2063c5ba0be4SMike Smith } 20640c7da7acSJohn Baldwin 20650c7da7acSJohn Baldwin /* 20660c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 20670c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 20680c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 20690c7da7acSJohn Baldwin * convert it into an Integer as best we can. 20700c7da7acSJohn Baldwin * 20710c7da7acSJohn Baldwin * This is a hack. 20720c7da7acSJohn Baldwin */ 2073be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 2074b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2075be2b1797SNate Lawson status = AE_NO_MEMORY; 20760c7da7acSJohn Baldwin } else { 2077be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2078be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 2079be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 20800c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 20810c7da7acSJohn Baldwin } 2082f97739daSNate Lawson } 2083be2b1797SNate Lawson return (status); 2084c5ba0be4SMike Smith } 2085c5ba0be4SMike Smith 2086c573e654SMitsuru IWASAKI ACPI_STATUS 2087cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2088c573e654SMitsuru IWASAKI { 2089c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 2090dba55fa2SNate Lawson UINT8 *val; 2091c573e654SMitsuru IWASAKI int i; 2092c573e654SMitsuru IWASAKI 2093c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 2094c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 2095c573e654SMitsuru IWASAKI *number = p->Integer.Value; 2096c573e654SMitsuru IWASAKI return (AE_OK); 2097c573e654SMitsuru IWASAKI } 2098c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 2099c573e654SMitsuru IWASAKI return (AE_TYPE); 2100c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 2101c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 2102be2b1797SNate Lawson 2103c573e654SMitsuru IWASAKI *number = 0; 2104dba55fa2SNate Lawson val = p->Buffer.Pointer; 2105c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 2106dba55fa2SNate Lawson *number += val[i] << (i * 8); 2107c573e654SMitsuru IWASAKI return (AE_OK); 2108c573e654SMitsuru IWASAKI } 2109c573e654SMitsuru IWASAKI 2110c5ba0be4SMike Smith /* 2111c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 2112c5ba0be4SMike Smith * function for each element. 2113c5ba0be4SMike Smith * 2114c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 2115c5ba0be4SMike Smith */ 2116c5ba0be4SMike Smith ACPI_STATUS 2117be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2118be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2119c5ba0be4SMike Smith { 2120c5ba0be4SMike Smith ACPI_OBJECT *comp; 2121c5ba0be4SMike Smith int i; 2122c5ba0be4SMike Smith 2123be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2124c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 2125c5ba0be4SMike Smith 2126be2b1797SNate Lawson /* Iterate over components */ 2127be2b1797SNate Lawson i = 0; 2128be2b1797SNate Lawson comp = pkg->Package.Elements; 2129be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 2130c5ba0be4SMike Smith func(comp, arg); 2131c5ba0be4SMike Smith 2132c5ba0be4SMike Smith return (AE_OK); 2133c5ba0be4SMike Smith } 2134c5ba0be4SMike Smith 21356f69255bSMike Smith /* 21366f69255bSMike Smith * Find the (index)th resource object in a set. 21376f69255bSMike Smith */ 21386f69255bSMike Smith ACPI_STATUS 21396d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 21406f69255bSMike Smith { 21416d63101aSMike Smith ACPI_RESOURCE *rp; 21426f69255bSMike Smith int i; 21436f69255bSMike Smith 21446d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 21456f69255bSMike Smith i = index; 21466d63101aSMike Smith while (i-- > 0) { 2147be2b1797SNate Lawson /* Range check */ 21486d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 21496f69255bSMike Smith return (AE_BAD_PARAMETER); 2150be2b1797SNate Lawson 2151be2b1797SNate Lawson /* Check for terminator */ 2152e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 21536d63101aSMike Smith return (AE_NOT_FOUND); 2154abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 21556f69255bSMike Smith } 21566f69255bSMike Smith if (resp != NULL) 21576d63101aSMike Smith *resp = rp; 2158be2b1797SNate Lawson 21596d63101aSMike Smith return (AE_OK); 21606d63101aSMike Smith } 21616d63101aSMike Smith 21626d63101aSMike Smith /* 21636d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 21646d63101aSMike Smith * 21656d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 21666d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 21676d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 21686d63101aSMike Smith * resources. 21696d63101aSMike Smith */ 21706d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 21716d63101aSMike Smith 21726d63101aSMike Smith ACPI_STATUS 21736d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 21746d63101aSMike Smith { 21756d63101aSMike Smith ACPI_RESOURCE *rp; 21766d63101aSMike Smith void *newp; 21776d63101aSMike Smith 2178be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 21796d63101aSMike Smith if (buf->Pointer == NULL) { 21806d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 21816d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 21826d63101aSMike Smith return (AE_NO_MEMORY); 21836d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 2184e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 21856d63101aSMike Smith rp->Length = 0; 21866d63101aSMike Smith } 21876d63101aSMike Smith if (res == NULL) 21886d63101aSMike Smith return (AE_OK); 21896d63101aSMike Smith 21906d63101aSMike Smith /* 21916d63101aSMike Smith * Scan the current buffer looking for the terminator. 21926d63101aSMike Smith * This will either find the terminator or hit the end 21936d63101aSMike Smith * of the buffer and return an error. 21946d63101aSMike Smith */ 21956d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 21966d63101aSMike Smith for (;;) { 2197be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 21986d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 21996d63101aSMike Smith return (AE_BAD_PARAMETER); 2200e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 22016d63101aSMike Smith break; 2202abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 22036d63101aSMike Smith } 22046d63101aSMike Smith 22056d63101aSMike Smith /* 22066d63101aSMike Smith * Check the size of the buffer and expand if required. 22076d63101aSMike Smith * 22086d63101aSMike Smith * Required size is: 22096d63101aSMike Smith * size of existing resources before terminator + 22106d63101aSMike Smith * size of new resource and header + 22116d63101aSMike Smith * size of terminator. 22126d63101aSMike Smith * 22136d63101aSMike Smith * Note that this loop should really only run once, unless 22146d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 22156d63101aSMike Smith */ 22166d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2217e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA + 2218e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) { 22196d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 22206d63101aSMike Smith return (AE_NO_MEMORY); 22216d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 2222a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2223a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 22246d63101aSMike Smith AcpiOsFree(buf->Pointer); 22256d63101aSMike Smith buf->Pointer = newp; 22266d63101aSMike Smith buf->Length += buf->Length; 22276d63101aSMike Smith } 22286d63101aSMike Smith 2229be2b1797SNate Lawson /* Insert the new resource. */ 2230e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 22316d63101aSMike Smith 2232be2b1797SNate Lawson /* And add the terminator. */ 2233abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 2234e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 22356d63101aSMike Smith rp->Length = 0; 22366d63101aSMike Smith 22376f69255bSMike Smith return (AE_OK); 22386f69255bSMike Smith } 2239c5ba0be4SMike Smith 2240da14ac9fSJohn Baldwin /* 2241da14ac9fSJohn Baldwin * Set interrupt model. 2242da14ac9fSJohn Baldwin */ 2243da14ac9fSJohn Baldwin ACPI_STATUS 2244da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 2245da14ac9fSJohn Baldwin { 2246da14ac9fSJohn Baldwin 2247c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2248da14ac9fSJohn Baldwin } 2249da14ac9fSJohn Baldwin 225000a30448SNate Lawson /* 2251d95e7f5aSJohn Baldwin * Walk subtables of a table and call a callback routine for each 2252d95e7f5aSJohn Baldwin * subtable. The caller should provide the first subtable and a 2253d95e7f5aSJohn Baldwin * pointer to the end of the table. This can be used to walk tables 2254d95e7f5aSJohn Baldwin * such as MADT and SRAT that use subtable entries. 2255d95e7f5aSJohn Baldwin */ 2256d95e7f5aSJohn Baldwin void 2257d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2258d95e7f5aSJohn Baldwin void *arg) 2259d95e7f5aSJohn Baldwin { 2260d95e7f5aSJohn Baldwin ACPI_SUBTABLE_HEADER *entry; 2261d95e7f5aSJohn Baldwin 2262d95e7f5aSJohn Baldwin for (entry = first; (void *)entry < end; ) { 2263d95e7f5aSJohn Baldwin /* Avoid an infinite loop if we hit a bogus entry. */ 2264d95e7f5aSJohn Baldwin if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2265d95e7f5aSJohn Baldwin return; 2266d95e7f5aSJohn Baldwin 2267d95e7f5aSJohn Baldwin handler(entry, arg); 2268d95e7f5aSJohn Baldwin entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2269d95e7f5aSJohn Baldwin } 2270d95e7f5aSJohn Baldwin } 2271d95e7f5aSJohn Baldwin 2272d95e7f5aSJohn Baldwin /* 227300a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be 227400a30448SNate Lawson * removed. 227500a30448SNate Lawson * 227600a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran 227700a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 227800a30448SNate Lawson */ 227900a30448SNate Lawson ACPI_STATUS 228000a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state) 228100a30448SNate Lawson { 228200a30448SNate Lawson static int once; 228300a30448SNate Lawson 228400a30448SNate Lawson if (!once) { 22852d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 228600a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n"); 228700a30448SNate Lawson once = 1; 228800a30448SNate Lawson } 228900a30448SNate Lawson return (acpi_EnterSleepState(sc, state)); 229000a30448SNate Lawson } 229100a30448SNate Lawson 2292c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 229300a30448SNate Lawson static void 229400a30448SNate Lawson acpi_sleep_force(void *arg) 229500a30448SNate Lawson { 22962d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 229700a30448SNate Lawson 22982d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 22992d0c82e8SJung-uk Kim "suspend request timed out, forcing sleep now\n"); 230000a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 23012d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 23022d0c82e8SJung-uk Kim sc->acpi_next_sstate); 230300a30448SNate Lawson } 2304c66d2b38SJung-uk Kim #endif 230500a30448SNate Lawson 230600a30448SNate Lawson /* 230700a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm 230800a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to 230900a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all 231000a30448SNate Lawson * acks are in. 231100a30448SNate Lawson */ 231200a30448SNate Lawson int 231300a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state) 231400a30448SNate Lawson { 231571f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 231600a30448SNate Lawson struct apm_clone_data *clone; 2317337744d9SJung-uk Kim ACPI_STATUS status; 231800a30448SNate Lawson 2319a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 232000a30448SNate Lawson return (EINVAL); 2321a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 2322a0e73a12SJung-uk Kim return (EOPNOTSUPP); 232300a30448SNate Lawson 2324337744d9SJung-uk Kim ACPI_LOCK(acpi); 232500a30448SNate Lawson 232600a30448SNate Lawson /* If a suspend request is already in progress, just return. */ 232700a30448SNate Lawson if (sc->acpi_next_sstate != 0) { 232800a30448SNate Lawson ACPI_UNLOCK(acpi); 232900a30448SNate Lawson return (0); 233000a30448SNate Lawson } 233100a30448SNate Lawson 2332337744d9SJung-uk Kim /* S5 (soft-off) should be entered directly with no waiting. */ 2333337744d9SJung-uk Kim if (state == ACPI_STATE_S5) { 2334337744d9SJung-uk Kim ACPI_UNLOCK(acpi); 2335337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2336337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2337337744d9SJung-uk Kim } 2338337744d9SJung-uk Kim 233900a30448SNate Lawson /* Record the pending state and notify all apm devices. */ 234000a30448SNate Lawson sc->acpi_next_sstate = state; 234100a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 234200a30448SNate Lawson clone->notify_status = APM_EV_NONE; 234300a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) { 234400a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO); 2345374d9c4dSJohn Baldwin KNOTE_LOCKED(&clone->sel_read.si_note, 0); 234600a30448SNate Lawson } 234700a30448SNate Lawson } 234800a30448SNate Lawson 23490c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */ 2350d42f0ad8SJung-uk Kim if (!devctl_process_running()) { 23510c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi); 2352337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2353337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 23540c26519eSMitsuru IWASAKI } 23550c26519eSMitsuru IWASAKI 235600a30448SNate Lawson /* 235700a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request 235800a30448SNate Lawson * in time. This way we still eventually go to sleep if we were 235900a30448SNate Lawson * overheating or running low on battery, even if userland is hung. 236000a30448SNate Lawson * We cancel this timeout once all userland acks are in or the 236100a30448SNate Lawson * suspend request is aborted. 236200a30448SNate Lawson */ 236300a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 236400a30448SNate Lawson ACPI_UNLOCK(acpi); 2365d42f0ad8SJung-uk Kim 2366d42f0ad8SJung-uk Kim /* Now notify devd(8) also. */ 2367d42f0ad8SJung-uk Kim acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2368d42f0ad8SJung-uk Kim 236900a30448SNate Lawson return (0); 2370c66d2b38SJung-uk Kim #else 2371c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2372c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2373c66d2b38SJung-uk Kim #endif 237400a30448SNate Lawson } 237500a30448SNate Lawson 237600a30448SNate Lawson /* 237700a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has 237800a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the 237900a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled 238000a30448SNate Lawson * and gives an errno value describing why. Once all votes are in, 238100a30448SNate Lawson * we suspend the system. 238200a30448SNate Lawson */ 238300a30448SNate Lawson int 238400a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error) 238500a30448SNate Lawson { 2386c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 238700a30448SNate Lawson struct acpi_softc *sc; 238800a30448SNate Lawson int ret, sleeping; 238900a30448SNate Lawson 239000a30448SNate Lawson /* If no pending sleep state, return an error. */ 239100a30448SNate Lawson ACPI_LOCK(acpi); 239200a30448SNate Lawson sc = clone->acpi_sc; 239300a30448SNate Lawson if (sc->acpi_next_sstate == 0) { 239400a30448SNate Lawson ACPI_UNLOCK(acpi); 239500a30448SNate Lawson return (ENXIO); 239600a30448SNate Lawson } 239700a30448SNate Lawson 239800a30448SNate Lawson /* Caller wants to abort suspend process. */ 239900a30448SNate Lawson if (error) { 240000a30448SNate Lawson sc->acpi_next_sstate = 0; 240100a30448SNate Lawson callout_stop(&sc->susp_force_to); 24022d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 24032d0c82e8SJung-uk Kim "listener on %s cancelled the pending suspend\n", 240400a30448SNate Lawson devtoname(clone->cdev)); 240500a30448SNate Lawson ACPI_UNLOCK(acpi); 240600a30448SNate Lawson return (0); 240700a30448SNate Lawson } 240800a30448SNate Lawson 240900a30448SNate Lawson /* 241000a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through 241100a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that 241200a30448SNate Lawson * are writable since read-only devices couldn't ack the request. 241300a30448SNate Lawson */ 241400a30448SNate Lawson sleeping = TRUE; 2415c66d2b38SJung-uk Kim clone->notify_status = APM_EV_ACKED; 241600a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 241700a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 && 241800a30448SNate Lawson clone->notify_status != APM_EV_ACKED) { 241900a30448SNate Lawson sleeping = FALSE; 242000a30448SNate Lawson break; 242100a30448SNate Lawson } 242200a30448SNate Lawson } 242300a30448SNate Lawson 242400a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */ 242500a30448SNate Lawson if (sleeping) 242600a30448SNate Lawson callout_stop(&sc->susp_force_to); 242700a30448SNate Lawson ACPI_UNLOCK(acpi); 242800a30448SNate Lawson ret = 0; 242900a30448SNate Lawson if (sleeping) { 243000a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 243100a30448SNate Lawson ret = ENODEV; 243200a30448SNate Lawson } 243300a30448SNate Lawson return (ret); 2434c66d2b38SJung-uk Kim #else 2435c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2436c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2437c66d2b38SJung-uk Kim #endif 243800a30448SNate Lawson } 243900a30448SNate Lawson 2440ece50487SMitsuru IWASAKI static void 2441ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 2442ece50487SMitsuru IWASAKI { 2443d42f0ad8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 2444bee4aa4aSNate Lawson 2445a0e73a12SJung-uk Kim /* Reschedule if the system is not fully up and running. */ 2446a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) { 2447a0e73a12SJung-uk Kim timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2448a0e73a12SJung-uk Kim return; 2449a0e73a12SJung-uk Kim } 2450a0e73a12SJung-uk Kim 2451d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2452a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = FALSE; 2453d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2454d42f0ad8SJung-uk Kim } 2455d42f0ad8SJung-uk Kim 2456d42f0ad8SJung-uk Kim static ACPI_STATUS 2457d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc) 2458d42f0ad8SJung-uk Kim { 2459d42f0ad8SJung-uk Kim ACPI_STATUS status; 2460d42f0ad8SJung-uk Kim 2461a0e73a12SJung-uk Kim /* Fail if the system is not fully up and running. */ 2462a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) 2463a0e73a12SJung-uk Kim return (AE_ERROR); 2464a0e73a12SJung-uk Kim 2465d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2466d42f0ad8SJung-uk Kim status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2467a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 2468d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2469d42f0ad8SJung-uk Kim 2470d42f0ad8SJung-uk Kim return (status); 2471ece50487SMitsuru IWASAKI } 2472c30382dfSMitsuru IWASAKI 247315e2f34fSNate Lawson enum acpi_sleep_state { 247415e2f34fSNate Lawson ACPI_SS_NONE, 247515e2f34fSNate Lawson ACPI_SS_GPE_SET, 247615e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 247715e2f34fSNate Lawson ACPI_SS_SLP_PREP, 247815e2f34fSNate Lawson ACPI_SS_SLEPT, 247915e2f34fSNate Lawson }; 248015e2f34fSNate Lawson 248115e32d5dSMike Smith /* 248200a30448SNate Lawson * Enter the desired system sleep state. 248315e32d5dSMike Smith * 2484be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 248515e32d5dSMike Smith */ 248600a30448SNate Lawson static ACPI_STATUS 248700a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state) 248815e32d5dSMike Smith { 248915e2f34fSNate Lawson ACPI_STATUS status; 249015e2f34fSNate Lawson enum acpi_sleep_state slp_state; 249115e32d5dSMike Smith 2492b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 24930ae55423SMike Smith 2494a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 249533c70d41SAndriy Gapon return_ACPI_STATUS (AE_BAD_PARAMETER); 2496a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) { 2497a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2498a0e73a12SJung-uk Kim state); 2499a0e73a12SJung-uk Kim return (AE_SUPPORT); 2500a0e73a12SJung-uk Kim } 2501a0e73a12SJung-uk Kim 2502a0e73a12SJung-uk Kim /* Re-entry once we're suspending is not allowed. */ 2503a0e73a12SJung-uk Kim status = acpi_sleep_disable(sc); 2504a0e73a12SJung-uk Kim if (ACPI_FAILURE(status)) { 2505a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, 2506a0e73a12SJung-uk Kim "suspend request ignored (not ready yet)\n"); 2507a0e73a12SJung-uk Kim return (status); 2508a0e73a12SJung-uk Kim } 250933c70d41SAndriy Gapon 251033c70d41SAndriy Gapon if (state == ACPI_STATE_S5) { 251133c70d41SAndriy Gapon /* 251233c70d41SAndriy Gapon * Shut down cleanly and power off. This will call us back through the 251333c70d41SAndriy Gapon * shutdown handlers. 251433c70d41SAndriy Gapon */ 251533c70d41SAndriy Gapon shutdown_nice(RB_POWEROFF); 251633c70d41SAndriy Gapon return_ACPI_STATUS (AE_OK); 251733c70d41SAndriy Gapon } 251833c70d41SAndriy Gapon 251936a483bbSJung-uk Kim if (smp_started) { 2520c66d2b38SJung-uk Kim thread_lock(curthread); 2521c66d2b38SJung-uk Kim sched_bind(curthread, 0); 2522c66d2b38SJung-uk Kim thread_unlock(curthread); 252336a483bbSJung-uk Kim } 2524c66d2b38SJung-uk Kim 2525a56fe095SJohn Baldwin /* 2526a56fe095SJohn Baldwin * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2527a56fe095SJohn Baldwin * drivers need this. 2528a56fe095SJohn Baldwin */ 2529a56fe095SJohn Baldwin mtx_lock(&Giant); 2530c66d2b38SJung-uk Kim 253115e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 253256d8cb57SMitsuru IWASAKI 2533a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 2534a1fccb47SMitsuru IWASAKI 2535d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 2536d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 253715e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 2538e8b4d56eSNate Lawson 253915e32d5dSMike Smith /* 2540c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 2541c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 254215e32d5dSMike Smith * 2543c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 2544c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 2545c7f88fc3SNate Lawson * bus interface does not provide for this. 254615e32d5dSMike Smith */ 254715e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 254815e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 254933c70d41SAndriy Gapon goto backout; 255015e2f34fSNate Lawson } 255115e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 2552b9c780d6SMitsuru IWASAKI 255393bfd059SNate Lawson /* If testing device suspend only, back out of everything here. */ 255493bfd059SNate Lawson if (acpi_susp_bounce) 255533c70d41SAndriy Gapon goto backout; 255693bfd059SNate Lawson 2557be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 2558be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2559b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2560b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 256133c70d41SAndriy Gapon goto backout; 2562b9c780d6SMitsuru IWASAKI } 256315e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 2564b9c780d6SMitsuru IWASAKI 2565be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 2566ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 2567ff01efb5SMitsuru IWASAKI 25686161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 25696161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 25706161544cSTakanori Watanabe 25716161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2572be2b1797SNate Lawson if (state == ACPI_STATE_S4) 2573964679ceSMitsuru IWASAKI AcpiEnable(); 25746161544cSTakanori Watanabe } else { 2575c7f88fc3SNate Lawson ACPI_DISABLE_IRQS(); 2576adad4744SNate Lawson status = AcpiEnterSleepState(state); 2577be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2578be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2579be2b1797SNate Lawson AcpiFormatException(status)); 258033c70d41SAndriy Gapon goto backout; 258115e32d5dSMike Smith } 25826161544cSTakanori Watanabe } 258315e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 2584ece50487SMitsuru IWASAKI 258515e2f34fSNate Lawson /* 258615e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 258715e2f34fSNate Lawson * process. This handles both the error and success cases. 258815e2f34fSNate Lawson */ 258933c70d41SAndriy Gapon backout: 259015e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 259115e2f34fSNate Lawson acpi_wake_prep_walk(state); 259215e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 259315e2f34fSNate Lawson } 259415e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLP_PREP) 259515e2f34fSNate Lawson AcpiLeaveSleepState(state); 2596071339e2SNate Lawson if (slp_state >= ACPI_SS_DEV_SUSPEND) 2597071339e2SNate Lawson DEVICE_RESUME(root_bus); 259815e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLEPT) 259915e2f34fSNate Lawson acpi_enable_fixed_events(sc); 2600337744d9SJung-uk Kim sc->acpi_next_sstate = 0; 260115e2f34fSNate Lawson 2602a56fe095SJohn Baldwin mtx_unlock(&Giant); 2603c66d2b38SJung-uk Kim 260436a483bbSJung-uk Kim if (smp_started) { 2605c66d2b38SJung-uk Kim thread_lock(curthread); 2606c66d2b38SJung-uk Kim sched_unbind(curthread); 2607c66d2b38SJung-uk Kim thread_unlock(curthread); 260836a483bbSJung-uk Kim } 2609c66d2b38SJung-uk Kim 2610d42f0ad8SJung-uk Kim /* Allow another sleep request after a while. */ 2611d42f0ad8SJung-uk Kim timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 2612d42f0ad8SJung-uk Kim 2613d42f0ad8SJung-uk Kim /* Run /etc/rc.resume after we are back. */ 2614d42f0ad8SJung-uk Kim if (devctl_process_running()) 2615d42f0ad8SJung-uk Kim acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2616d42f0ad8SJung-uk Kim 26170ae55423SMike Smith return_ACPI_STATUS (status); 261815e32d5dSMike Smith } 261915e32d5dSMike Smith 26200755473bSJung-uk Kim void 26210755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc) 26220755473bSJung-uk Kim { 26230755473bSJung-uk Kim 26240755473bSJung-uk Kim if (!acpi_reset_clock) 26250755473bSJung-uk Kim return; 26260755473bSJung-uk Kim 26270755473bSJung-uk Kim /* 26280755473bSJung-uk Kim * Warm up timecounter again and reset system clock. 26290755473bSJung-uk Kim */ 26300755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 26310755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 26320755473bSJung-uk Kim inittodr(time_second + sc->acpi_sleep_delay); 26330755473bSJung-uk Kim } 26340755473bSJung-uk Kim 2635e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 2636e8b4d56eSNate Lawson int 2637e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 2638e8b4d56eSNate Lawson { 2639e8b4d56eSNate Lawson struct acpi_prw_data prw; 2640e8b4d56eSNate Lawson ACPI_STATUS status; 2641e8b4d56eSNate Lawson int flags; 2642e8b4d56eSNate Lawson 2643d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 26442be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2645e8b4d56eSNate Lawson return (ENXIO); 2646e8b4d56eSNate Lawson 2647d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 2648e8b4d56eSNate Lawson if (enable) { 2649a88e22b7SJung-uk Kim status = AcpiGpeWakeup(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 2650e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2651e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 2652e8b4d56eSNate Lawson return (ENXIO); 2653e8b4d56eSNate Lawson } 2654d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2655e8b4d56eSNate Lawson } else { 2656a88e22b7SJung-uk Kim status = AcpiGpeWakeup(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 2657e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2658e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 2659e8b4d56eSNate Lawson return (ENXIO); 2660e8b4d56eSNate Lawson } 2661d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2662e8b4d56eSNate Lawson } 2663e8b4d56eSNate Lawson 2664e8b4d56eSNate Lawson return (0); 2665e8b4d56eSNate Lawson } 2666e8b4d56eSNate Lawson 2667d4b9ff91SNate Lawson static int 2668d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2669e8b4d56eSNate Lawson { 2670e8b4d56eSNate Lawson struct acpi_prw_data prw; 2671d4b9ff91SNate Lawson device_t dev; 2672e8b4d56eSNate Lawson 2673d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 2674e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2675e8b4d56eSNate Lawson return (ENXIO); 2676d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2677e8b4d56eSNate Lawson 2678e8b4d56eSNate Lawson /* 2679d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 2680d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 2681d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 2682d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 2683d4b9ff91SNate Lawson * and set _PSW. 2684e8b4d56eSNate Lawson */ 2685d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 268636646862SJung-uk Kim AcpiGpeWakeup(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 2687e8b4d56eSNate Lawson if (bootverbose) 2688d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2689d4b9ff91SNate Lawson acpi_name(handle), sstate); 2690d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2691d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 2692d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 2693d4b9ff91SNate Lawson if (bootverbose) 2694d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2695d4b9ff91SNate Lawson acpi_name(handle), sstate); 2696d4b9ff91SNate Lawson } 2697cc85c78cSNate Lawson 2698cc85c78cSNate Lawson return (0); 2699e8b4d56eSNate Lawson } 2700e8b4d56eSNate Lawson 2701d4b9ff91SNate Lawson static int 2702d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2703cc85c78cSNate Lawson { 2704cc85c78cSNate Lawson struct acpi_prw_data prw; 2705d4b9ff91SNate Lawson device_t dev; 2706cc85c78cSNate Lawson 2707cc85c78cSNate Lawson /* 2708d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 2709d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 2710cc85c78cSNate Lawson */ 2711d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2712d4b9ff91SNate Lawson return (ENXIO); 2713d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2714d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2715d4b9ff91SNate Lawson return (0); 2716cc85c78cSNate Lawson 2717d4b9ff91SNate Lawson /* 2718d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 2719d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 2720d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 2721d4b9ff91SNate Lawson */ 2722d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 272336646862SJung-uk Kim AcpiGpeWakeup(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 2724d4b9ff91SNate Lawson if (bootverbose) 2725d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2726d4b9ff91SNate Lawson } else { 2727d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2728d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 2729d4b9ff91SNate Lawson if (bootverbose) 2730d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 2731d4b9ff91SNate Lawson acpi_name(handle)); 2732d4b9ff91SNate Lawson } 2733d4b9ff91SNate Lawson 2734e8b4d56eSNate Lawson return (0); 2735e8b4d56eSNate Lawson } 2736e8b4d56eSNate Lawson 2737e8b4d56eSNate Lawson static ACPI_STATUS 2738d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2739e8b4d56eSNate Lawson { 2740d4b9ff91SNate Lawson int sstate; 2741e8b4d56eSNate Lawson 2742d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 2743d4b9ff91SNate Lawson sstate = *(int *)context; 2744d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 2745d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 2746d4b9ff91SNate Lawson else 2747d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 2748e8b4d56eSNate Lawson return (AE_OK); 2749e8b4d56eSNate Lawson } 2750e8b4d56eSNate Lawson 2751d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2752e8b4d56eSNate Lawson static int 2753d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 2754e8b4d56eSNate Lawson { 2755e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2756e8b4d56eSNate Lawson 2757e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2758d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 27592272d050SJung-uk Kim acpi_wake_prep, NULL, &sstate, NULL); 2760e8b4d56eSNate Lawson return (0); 2761e8b4d56eSNate Lawson } 2762e8b4d56eSNate Lawson 276388a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 276488a79fc0SNate Lawson static int 276588a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 276688a79fc0SNate Lawson { 276788a79fc0SNate Lawson int error, i, numdevs; 276888a79fc0SNate Lawson device_t *devlist; 276988a79fc0SNate Lawson device_t child; 2770d4b9ff91SNate Lawson ACPI_STATUS status; 277188a79fc0SNate Lawson 277288a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 27731c9ec538SNate Lawson if (error != 0 || numdevs == 0) { 27741c9ec538SNate Lawson if (numdevs == 0) 27751c9ec538SNate Lawson free(devlist, M_TEMP); 277688a79fc0SNate Lawson return (error); 27771c9ec538SNate Lawson } 277888a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 277988a79fc0SNate Lawson child = devlist[i]; 2780d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 278188a79fc0SNate Lawson if (!device_is_attached(child)) 278288a79fc0SNate Lawson continue; 2783d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2784d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 278588a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 278688a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 278788a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 278888a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 278988a79fc0SNate Lawson } 279088a79fc0SNate Lawson } 279188a79fc0SNate Lawson free(devlist, M_TEMP); 279288a79fc0SNate Lawson 279388a79fc0SNate Lawson return (0); 279488a79fc0SNate Lawson } 279588a79fc0SNate Lawson 279688a79fc0SNate Lawson /* Enable or disable wake from userland. */ 279788a79fc0SNate Lawson static int 279888a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 279988a79fc0SNate Lawson { 280088a79fc0SNate Lawson int enable, error; 280188a79fc0SNate Lawson device_t dev; 280288a79fc0SNate Lawson 280388a79fc0SNate Lawson dev = (device_t)arg1; 2804d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 280588a79fc0SNate Lawson 280688a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 280788a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 280888a79fc0SNate Lawson return (error); 280988a79fc0SNate Lawson if (enable != 0 && enable != 1) 281088a79fc0SNate Lawson return (EINVAL); 281188a79fc0SNate Lawson 281288a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 281388a79fc0SNate Lawson } 281488a79fc0SNate Lawson 2815e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2816d4b9ff91SNate Lawson int 2817e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2818e8b4d56eSNate Lawson { 2819e8b4d56eSNate Lawson ACPI_STATUS status; 2820e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2821e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2822d4b9ff91SNate Lawson int error, i, power_count; 2823e8b4d56eSNate Lawson 2824e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2825e8b4d56eSNate Lawson return (EINVAL); 2826e8b4d56eSNate Lawson 2827e8b4d56eSNate Lawson /* 2828e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2829e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2830e8b4d56eSNate Lawson */ 2831e8b4d56eSNate Lawson error = EINVAL; 2832e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2833e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2834e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2835e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2836e8b4d56eSNate Lawson return (ENOENT); 2837e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2838e8b4d56eSNate Lawson if (res == NULL) 2839e8b4d56eSNate Lawson return (ENOENT); 2840e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2841e8b4d56eSNate Lawson goto out; 2842e8b4d56eSNate Lawson 2843e8b4d56eSNate Lawson /* 2844e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2845e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2846e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2847e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2848e8b4d56eSNate Lawson */ 2849e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2850e8b4d56eSNate Lawson goto out; 2851e8b4d56eSNate Lawson 2852e8b4d56eSNate Lawson /* 2853e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2854e8b4d56eSNate Lawson */ 2855e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2856e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2857e8b4d56eSNate Lawson /* 2858e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2859e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2860e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2861e8b4d56eSNate Lawson * enabled for the wake event. 2862e8b4d56eSNate Lawson */ 2863e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2864e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2865e8b4d56eSNate Lawson error = 0; 2866e8b4d56eSNate Lawson break; 2867e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2868e8b4d56eSNate Lawson /* 2869e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2870e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2871e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2872e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2873e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2874e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2875e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2876e8b4d56eSNate Lawson * the wake event. 2877e8b4d56eSNate Lawson * 2878e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2879e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2880e8b4d56eSNate Lawson */ 2881e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2882e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2883e8b4d56eSNate Lawson goto out; 2884e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2885e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2886e8b4d56eSNate Lawson goto out; 2887e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2888e8b4d56eSNate Lawson goto out; 2889e8b4d56eSNate Lawson error = 0; 2890e8b4d56eSNate Lawson break; 2891e8b4d56eSNate Lawson default: 2892e8b4d56eSNate Lawson goto out; 2893e8b4d56eSNate Lawson } 2894e8b4d56eSNate Lawson 2895d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 2896d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 2897d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 2898d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2899d4b9ff91SNate Lawson power_count = 0; 2900d4b9ff91SNate Lawson } 2901d4b9ff91SNate Lawson prw->power_res_count = power_count; 2902d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 2903d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 2904e8b4d56eSNate Lawson 2905e8b4d56eSNate Lawson out: 2906e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2907e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2908e8b4d56eSNate Lawson return (error); 2909e8b4d56eSNate Lawson } 2910e8b4d56eSNate Lawson 291115e32d5dSMike Smith /* 291215e32d5dSMike Smith * ACPI Event Handlers 291315e32d5dSMike Smith */ 291415e32d5dSMike Smith 291515e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 291615e32d5dSMike Smith 291715e32d5dSMike Smith static void 291815e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 291915e32d5dSMike Smith { 29202d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 292100a30448SNate Lawson int ret; 2922bee4aa4aSNate Lawson 2923b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 292415e32d5dSMike Smith 2925a0e73a12SJung-uk Kim /* Check if button action is disabled or unknown. */ 2926a0e73a12SJung-uk Kim if (state == ACPI_STATE_UNKNOWN) 2927813d6dcaSNate Lawson return; 2928813d6dcaSNate Lawson 292900a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */ 29302d0c82e8SJung-uk Kim ret = acpi_ReqSleepState(sc, state); 293100a30448SNate Lawson if (ret != 0) 29322d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 29332d0c82e8SJung-uk Kim "request to enter state S%d failed (err %d)\n", state, ret); 2934bee4aa4aSNate Lawson 29350ae55423SMike Smith return_VOID; 293615e32d5dSMike Smith } 293715e32d5dSMike Smith 293815e32d5dSMike Smith static void 293915e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 294015e32d5dSMike Smith { 2941bee4aa4aSNate Lawson 2942b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 29430ae55423SMike Smith 2944bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 2945cb9b0d80SMike Smith 29460ae55423SMike Smith return_VOID; 294715e32d5dSMike Smith } 294815e32d5dSMike Smith 294915e32d5dSMike Smith /* 295015e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 295115e32d5dSMike Smith */ 295215e32d5dSMike Smith UINT32 295333febf93SNate Lawson acpi_event_power_button_sleep(void *context) 295415e32d5dSMike Smith { 295515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 295615e32d5dSMike Smith 2957b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29580ae55423SMike Smith 295915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 29600ae55423SMike Smith 2961b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 296215e32d5dSMike Smith } 296315e32d5dSMike Smith 296415e32d5dSMike Smith UINT32 296533febf93SNate Lawson acpi_event_power_button_wake(void *context) 296615e32d5dSMike Smith { 296715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 296815e32d5dSMike Smith 2969b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29700ae55423SMike Smith 297115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 29720ae55423SMike Smith 2973b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 297415e32d5dSMike Smith } 297515e32d5dSMike Smith 297615e32d5dSMike Smith UINT32 297733febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 297815e32d5dSMike Smith { 297915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 298015e32d5dSMike Smith 2981b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29820ae55423SMike Smith 298315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 29840ae55423SMike Smith 2985b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 298615e32d5dSMike Smith } 298715e32d5dSMike Smith 298815e32d5dSMike Smith UINT32 298933febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 299015e32d5dSMike Smith { 299115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 299215e32d5dSMike Smith 2993b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29940ae55423SMike Smith 299515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 29960ae55423SMike Smith 2997b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 299815e32d5dSMike Smith } 299915e32d5dSMike Smith 300015e32d5dSMike Smith /* 3001bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 3002bee4aa4aSNate Lawson * use this for single-threaded callers. 300315e32d5dSMike Smith */ 300415e32d5dSMike Smith char * 300515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 300615e32d5dSMike Smith { 3007bee4aa4aSNate Lawson ACPI_BUFFER buf; 3008bee4aa4aSNate Lawson static char data[256]; 300915e32d5dSMike Smith 3010bee4aa4aSNate Lawson buf.Length = sizeof(data); 3011bee4aa4aSNate Lawson buf.Pointer = data; 3012cb9b0d80SMike Smith 30130a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3014bee4aa4aSNate Lawson return (data); 3015bee4aa4aSNate Lawson return ("(unknown)"); 301615e32d5dSMike Smith } 301715e32d5dSMike Smith 301815e32d5dSMike Smith /* 301915e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 302015e32d5dSMike Smith * parts of the namespace. 302115e32d5dSMike Smith */ 302215e32d5dSMike Smith int 302315e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 302415e32d5dSMike Smith { 30253c600cfbSJohn Baldwin char *cp, *env, *np; 302615e32d5dSMike Smith int len; 302715e32d5dSMike Smith 302815e32d5dSMike Smith np = acpi_name(handle); 302915e32d5dSMike Smith if (*np == '\\') 303015e32d5dSMike Smith np++; 30313c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 303215e32d5dSMike Smith return (0); 303315e32d5dSMike Smith 3034be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 30353c600cfbSJohn Baldwin cp = env; 303615e32d5dSMike Smith for (;;) { 3037bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 303815e32d5dSMike Smith cp++; 303915e32d5dSMike Smith if (*cp == 0) 304015e32d5dSMike Smith break; 304115e32d5dSMike Smith len = 0; 3042bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 304315e32d5dSMike Smith len++; 3044d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 30453c600cfbSJohn Baldwin freeenv(env); 30460ae55423SMike Smith return(1); 3047d786139cSMaxime Henrion } 30480ae55423SMike Smith cp += len; 30490ae55423SMike Smith } 30503c600cfbSJohn Baldwin freeenv(env); 3051be2b1797SNate Lawson 30520ae55423SMike Smith return (0); 30530ae55423SMike Smith } 30540ae55423SMike Smith 30550ae55423SMike Smith /* 30560ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 30570ae55423SMike Smith */ 30580ae55423SMike Smith int 30590ae55423SMike Smith acpi_disabled(char *subsys) 30600ae55423SMike Smith { 306178689b15SMaxime Henrion char *cp, *env; 30620ae55423SMike Smith int len; 30630ae55423SMike Smith 30643184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 30650ae55423SMike Smith return (0); 30663184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 306778689b15SMaxime Henrion freeenv(env); 30680ae55423SMike Smith return (1); 3069d786139cSMaxime Henrion } 30700ae55423SMike Smith 30713184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 307278689b15SMaxime Henrion cp = env; 30730ae55423SMike Smith for (;;) { 30743184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 30750ae55423SMike Smith cp++; 30763184cf5aSNate Lawson if (*cp == '\0') 30770ae55423SMike Smith break; 30780ae55423SMike Smith len = 0; 30793184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 30800ae55423SMike Smith len++; 30813184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 308278689b15SMaxime Henrion freeenv(env); 308315e32d5dSMike Smith return (1); 3084d786139cSMaxime Henrion } 308515e32d5dSMike Smith cp += len; 308615e32d5dSMike Smith } 308778689b15SMaxime Henrion freeenv(env); 3088be2b1797SNate Lawson 308915e32d5dSMike Smith return (0); 309015e32d5dSMike Smith } 309115e32d5dSMike Smith 309215e32d5dSMike Smith /* 309315e32d5dSMike Smith * Control interface. 309415e32d5dSMike Smith * 30950ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 3096be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 3097be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 309815e32d5dSMike Smith */ 30990ae55423SMike Smith struct acpi_ioctl_hook 31000ae55423SMike Smith { 31010ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 31020ae55423SMike Smith u_long cmd; 3103be2b1797SNate Lawson acpi_ioctl_fn fn; 31040ae55423SMike Smith void *arg; 31050ae55423SMike Smith }; 31060ae55423SMike Smith 31070ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 31080ae55423SMike Smith static int acpi_ioctl_hooks_initted; 31090ae55423SMike Smith 31100ae55423SMike Smith int 3111be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 31120ae55423SMike Smith { 31130ae55423SMike Smith struct acpi_ioctl_hook *hp; 31140ae55423SMike Smith 31150ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 31160ae55423SMike Smith return (ENOMEM); 31170ae55423SMike Smith hp->cmd = cmd; 31180ae55423SMike Smith hp->fn = fn; 31190ae55423SMike Smith hp->arg = arg; 312015e2f34fSNate Lawson 312115e2f34fSNate Lawson ACPI_LOCK(acpi); 31220ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 31230ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 31240ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 31250ae55423SMike Smith } 31260ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 312715e2f34fSNate Lawson ACPI_UNLOCK(acpi); 312815e2f34fSNate Lawson 31290ae55423SMike Smith return (0); 31300ae55423SMike Smith } 31310ae55423SMike Smith 31320ae55423SMike Smith void 3133be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 31340ae55423SMike Smith { 31350ae55423SMike Smith struct acpi_ioctl_hook *hp; 31360ae55423SMike Smith 313715e2f34fSNate Lawson ACPI_LOCK(acpi); 31380ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 313915e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 31400ae55423SMike Smith break; 31410ae55423SMike Smith 31420ae55423SMike Smith if (hp != NULL) { 31430ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 31440ae55423SMike Smith free(hp, M_ACPIDEV); 31450ae55423SMike Smith } 314615e2f34fSNate Lawson ACPI_UNLOCK(acpi); 31470ae55423SMike Smith } 31480ae55423SMike Smith 314915e32d5dSMike Smith static int 315000b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 315115e32d5dSMike Smith { 315215e32d5dSMike Smith return (0); 315315e32d5dSMike Smith } 315415e32d5dSMike Smith 315515e32d5dSMike Smith static int 315600b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 315715e32d5dSMike Smith { 315815e32d5dSMike Smith return (0); 315915e32d5dSMike Smith } 316015e32d5dSMike Smith 316115e32d5dSMike Smith static int 316200b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 316315e32d5dSMike Smith { 316415e32d5dSMike Smith struct acpi_softc *sc; 31650ae55423SMike Smith struct acpi_ioctl_hook *hp; 3166bee4aa4aSNate Lawson int error, state; 316715e32d5dSMike Smith 3168a2e35898SDavid E. O'Brien error = 0; 3169a2e35898SDavid E. O'Brien hp = NULL; 317015e32d5dSMike Smith sc = dev->si_drv1; 317115e32d5dSMike Smith 31720ae55423SMike Smith /* 31730ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 31740ae55423SMike Smith */ 317515e2f34fSNate Lawson ACPI_LOCK(acpi); 3176bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 31770ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3178bee4aa4aSNate Lawson if (hp->cmd == cmd) 3179bee4aa4aSNate Lawson break; 31800ae55423SMike Smith } 318115e2f34fSNate Lawson ACPI_UNLOCK(acpi); 3182bee4aa4aSNate Lawson if (hp) 3183bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 31840ae55423SMike Smith 31850ae55423SMike Smith /* 3186a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 3187a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 3188a89fcf28STakanori Watanabe * Not changing system behavior. 3189a89fcf28STakanori Watanabe */ 3190be2b1797SNate Lawson if ((flag & FWRITE) == 0) 3191be2b1797SNate Lawson return (EPERM); 3192a89fcf28STakanori Watanabe 3193be2b1797SNate Lawson /* Core system ioctls. */ 319415e32d5dSMike Smith switch (cmd) { 319500a30448SNate Lawson case ACPIIO_REQSLPSTATE: 319600a30448SNate Lawson state = *(int *)addr; 319700a30448SNate Lawson if (state != ACPI_STATE_S5) 3198a0e73a12SJung-uk Kim return (acpi_ReqSleepState(sc, state)); 3199a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3200a0e73a12SJung-uk Kim error = EOPNOTSUPP; 320100a30448SNate Lawson break; 320200a30448SNate Lawson case ACPIIO_ACKSLPSTATE: 320300a30448SNate Lawson error = *(int *)addr; 320400a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error); 320500a30448SNate Lawson break; 320600a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 320715e32d5dSMike Smith state = *(int *)addr; 3208a0e73a12SJung-uk Kim if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3209a0e73a12SJung-uk Kim return (EINVAL); 3210a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 3211a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3212a0e73a12SJung-uk Kim if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3213a0e73a12SJung-uk Kim error = ENXIO; 321415e32d5dSMike Smith break; 321515e32d5dSMike Smith default: 3216bee4aa4aSNate Lawson error = ENXIO; 321715e32d5dSMike Smith break; 321815e32d5dSMike Smith } 3219917d44c8SMitsuru IWASAKI 322015e32d5dSMike Smith return (error); 322115e32d5dSMike Smith } 322215e32d5dSMike Smith 32231d073b1dSJohn Baldwin static int 3224a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname) 3225a0e73a12SJung-uk Kim { 3226a0e73a12SJung-uk Kim int sstate; 3227a0e73a12SJung-uk Kim 3228a0e73a12SJung-uk Kim if (toupper(sname[0]) == 'S') { 3229a0e73a12SJung-uk Kim sstate = sname[1] - '0'; 3230a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3231a0e73a12SJung-uk Kim sname[2] == '\0') 3232a0e73a12SJung-uk Kim return (sstate); 3233a0e73a12SJung-uk Kim } else if (strcasecmp(sname, "NONE") == 0) 3234a0e73a12SJung-uk Kim return (ACPI_STATE_UNKNOWN); 3235a0e73a12SJung-uk Kim return (-1); 3236a0e73a12SJung-uk Kim } 3237a0e73a12SJung-uk Kim 3238a0e73a12SJung-uk Kim static const char * 3239a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate) 3240a0e73a12SJung-uk Kim { 3241a0e73a12SJung-uk Kim static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3242a0e73a12SJung-uk Kim 3243a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3244a0e73a12SJung-uk Kim return (snames[sstate]); 3245a0e73a12SJung-uk Kim else if (sstate == ACPI_STATE_UNKNOWN) 3246a0e73a12SJung-uk Kim return ("NONE"); 3247a0e73a12SJung-uk Kim return (NULL); 3248a0e73a12SJung-uk Kim } 3249a0e73a12SJung-uk Kim 3250a0e73a12SJung-uk Kim static int 3251d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3252d75de536SMitsuru IWASAKI { 3253d75de536SMitsuru IWASAKI int error; 3254bee4aa4aSNate Lawson struct sbuf sb; 3255a0e73a12SJung-uk Kim UINT8 state; 3256d75de536SMitsuru IWASAKI 3257bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3258a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3259a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) 3260a0e73a12SJung-uk Kim sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3261bee4aa4aSNate Lawson sbuf_trim(&sb); 3262bee4aa4aSNate Lawson sbuf_finish(&sb); 3263bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3264bee4aa4aSNate Lawson sbuf_delete(&sb); 3265d75de536SMitsuru IWASAKI return (error); 3266d75de536SMitsuru IWASAKI } 3267d75de536SMitsuru IWASAKI 3268d75de536SMitsuru IWASAKI static int 32691d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 32701d073b1dSJohn Baldwin { 32711d073b1dSJohn Baldwin char sleep_state[10]; 3272a0e73a12SJung-uk Kim int error, new_state, old_state; 32731d073b1dSJohn Baldwin 3274a0e73a12SJung-uk Kim old_state = *(int *)oidp->oid_arg1; 3275a0e73a12SJung-uk Kim strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 32761d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 32771d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 3278a0e73a12SJung-uk Kim new_state = acpi_sname2sstate(sleep_state); 3279a0e73a12SJung-uk Kim if (new_state < ACPI_STATE_S1) 3280a0e73a12SJung-uk Kim return (EINVAL); 328154b43614SJung-uk Kim if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3282a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3283be2b1797SNate Lawson if (new_state != old_state) 3284a0e73a12SJung-uk Kim *(int *)oidp->oid_arg1 = new_state; 32851d073b1dSJohn Baldwin } 32861d073b1dSJohn Baldwin return (error); 32871d073b1dSJohn Baldwin } 32881d073b1dSJohn Baldwin 32899b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 32909b937d48SNate Lawson void 32919b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 32929b937d48SNate Lawson { 32939b937d48SNate Lawson char notify_buf[16]; 32949b937d48SNate Lawson ACPI_BUFFER handle_buf; 32959b937d48SNate Lawson ACPI_STATUS status; 32969b937d48SNate Lawson 32979b937d48SNate Lawson if (subsystem == NULL) 32989b937d48SNate Lawson return; 32999b937d48SNate Lawson 33009b937d48SNate Lawson handle_buf.Pointer = NULL; 33019b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 33029b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 33039b937d48SNate Lawson if (ACPI_FAILURE(status)) 33049b937d48SNate Lawson return; 33059b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 33069b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 33079b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 33089b937d48SNate Lawson } 33099b937d48SNate Lawson 331015e32d5dSMike Smith #ifdef ACPI_DEBUG 33110ae55423SMike Smith /* 33120ae55423SMike Smith * Support for parsing debug options from the kernel environment. 33130ae55423SMike Smith * 33140ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 33150ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 33160ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 33170ae55423SMike Smith * prefixing the bit name with !. 33180ae55423SMike Smith */ 331915e32d5dSMike Smith struct debugtag 332015e32d5dSMike Smith { 332115e32d5dSMike Smith char *name; 332215e32d5dSMike Smith UINT32 value; 332315e32d5dSMike Smith }; 332415e32d5dSMike Smith 332515e32d5dSMike Smith static struct debugtag dbg_layer[] = { 3326c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 3327c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 3328c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 3329c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 3330c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3331c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 3332c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3333c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 3334c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 3335d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 33364c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3337d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3338297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 33394c1cdee6SMike Smith 3340c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3341c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 33423184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 3343c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 33443184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 33453184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 33463184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 33474c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3348cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 33493184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 3350b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 335115e32d5dSMike Smith {NULL, 0} 335215e32d5dSMike Smith }; 335315e32d5dSMike Smith 335415e32d5dSMike Smith static struct debugtag dbg_level[] = { 33559501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 33564c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 33579501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 33584c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3359d62ab2f4SMitsuru IWASAKI 3360d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 3361297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 33624c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 33634c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3364d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 33654c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 33664c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 33674c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 33684c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 33694c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 33704c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 33714c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 33724c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 33734c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 33744c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3375d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3376d62ab2f4SMitsuru IWASAKI 3377d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3378d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3379d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3380d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3381d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 33824c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 3383d62ab2f4SMitsuru IWASAKI 3384d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3385d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3386d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3387d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 3388d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3389d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3390d62ab2f4SMitsuru IWASAKI 3391d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 339298479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 339398479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 339498479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 339598479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 339698479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 339715e32d5dSMike Smith {NULL, 0} 339815e32d5dSMike Smith }; 339915e32d5dSMike Smith 340015e32d5dSMike Smith static void 340115e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 340215e32d5dSMike Smith { 340315e32d5dSMike Smith char *ep; 340415e32d5dSMike Smith int i, l; 34050ae55423SMike Smith int set; 340615e32d5dSMike Smith 340715e32d5dSMike Smith while (*cp) { 340815e32d5dSMike Smith if (isspace(*cp)) { 340915e32d5dSMike Smith cp++; 341015e32d5dSMike Smith continue; 341115e32d5dSMike Smith } 341215e32d5dSMike Smith ep = cp; 341315e32d5dSMike Smith while (*ep && !isspace(*ep)) 341415e32d5dSMike Smith ep++; 34150ae55423SMike Smith if (*cp == '!') { 34160ae55423SMike Smith set = 0; 34170ae55423SMike Smith cp++; 34180ae55423SMike Smith if (cp == ep) 34190ae55423SMike Smith continue; 34200ae55423SMike Smith } else { 34210ae55423SMike Smith set = 1; 34220ae55423SMike Smith } 342315e32d5dSMike Smith l = ep - cp; 342415e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 342515e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 3426be2b1797SNate Lawson if (set) 342715e32d5dSMike Smith *flag |= tag[i].value; 3428be2b1797SNate Lawson else 34290ae55423SMike Smith *flag &= ~tag[i].value; 343015e32d5dSMike Smith } 343115e32d5dSMike Smith } 343215e32d5dSMike Smith cp = ep; 343315e32d5dSMike Smith } 343415e32d5dSMike Smith } 343515e32d5dSMike Smith 343615e32d5dSMike Smith static void 34372a4ac806SMike Smith acpi_set_debugging(void *junk) 343815e32d5dSMike Smith { 34397e639165SNate Lawson char *layer, *level; 344015e32d5dSMike Smith 34411d7b121cSNate Lawson if (cold) { 34420ae55423SMike Smith AcpiDbgLayer = 0; 34430ae55423SMike Smith AcpiDbgLevel = 0; 34441d7b121cSNate Lawson } 34451d7b121cSNate Lawson 34467e639165SNate Lawson layer = getenv("debug.acpi.layer"); 34477e639165SNate Lawson level = getenv("debug.acpi.level"); 34487e639165SNate Lawson if (layer == NULL && level == NULL) 34497e639165SNate Lawson return; 34500ae55423SMike Smith 34517e639165SNate Lawson printf("ACPI set debug"); 34527e639165SNate Lawson if (layer != NULL) { 34537e639165SNate Lawson if (strcmp("NONE", layer) != 0) 34547e639165SNate Lawson printf(" layer '%s'", layer); 34557e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 34567e639165SNate Lawson freeenv(layer); 34571d7b121cSNate Lawson } 34587e639165SNate Lawson if (level != NULL) { 34597e639165SNate Lawson if (strcmp("NONE", level) != 0) 34607e639165SNate Lawson printf(" level '%s'", level); 34617e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 34627e639165SNate Lawson freeenv(level); 34637e639165SNate Lawson } 34647e639165SNate Lawson printf("\n"); 346515e32d5dSMike Smith } 3466bee4aa4aSNate Lawson 3467be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3468be2b1797SNate Lawson NULL); 34691d7b121cSNate Lawson 34701d7b121cSNate Lawson static int 34711d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 34721d7b121cSNate Lawson { 347354f6bca0SNate Lawson int error, *dbg; 34741d7b121cSNate Lawson struct debugtag *tag; 347554f6bca0SNate Lawson struct sbuf sb; 34761d7b121cSNate Lawson 347754f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 347854f6bca0SNate Lawson return (ENOMEM); 34791d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 34801d7b121cSNate Lawson tag = &dbg_layer[0]; 34811d7b121cSNate Lawson dbg = &AcpiDbgLayer; 34821d7b121cSNate Lawson } else { 34831d7b121cSNate Lawson tag = &dbg_level[0]; 34841d7b121cSNate Lawson dbg = &AcpiDbgLevel; 34851d7b121cSNate Lawson } 34861d7b121cSNate Lawson 34871d7b121cSNate Lawson /* Get old values if this is a get request. */ 348815e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 34891d7b121cSNate Lawson if (*dbg == 0) { 349054f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 34911d7b121cSNate Lawson } else if (req->newptr == NULL) { 34921d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 349354f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 349454f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 34951d7b121cSNate Lawson } 34961d7b121cSNate Lawson } 349754f6bca0SNate Lawson sbuf_trim(&sb); 349854f6bca0SNate Lawson sbuf_finish(&sb); 34991d7b121cSNate Lawson 35007e639165SNate Lawson /* Copy out the old values to the user. */ 35017e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 350254f6bca0SNate Lawson sbuf_delete(&sb); 35031d7b121cSNate Lawson 35041d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 35051d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 35061d7b121cSNate Lawson *dbg = 0; 35071d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 35081d7b121cSNate Lawson acpi_set_debugging(NULL); 35091d7b121cSNate Lawson } 351015e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 35111d7b121cSNate Lawson 35121d7b121cSNate Lawson return (error); 35131d7b121cSNate Lawson } 3514bee4aa4aSNate Lawson 35151d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 35161d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 35171d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 35181d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3519bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 3520f9390180SMitsuru IWASAKI 3521f9390180SMitsuru IWASAKI static int 35222a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 35232a18c71dSJung-uk Kim { 35242a18c71dSJung-uk Kim int error; 35252a18c71dSJung-uk Kim int old; 35262a18c71dSJung-uk Kim 35272a18c71dSJung-uk Kim old = acpi_debug_objects; 35282a18c71dSJung-uk Kim error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 35292a18c71dSJung-uk Kim if (error != 0 || req->newptr == NULL) 35302a18c71dSJung-uk Kim return (error); 35312a18c71dSJung-uk Kim if (old == acpi_debug_objects || (old && acpi_debug_objects)) 35322a18c71dSJung-uk Kim return (0); 35332a18c71dSJung-uk Kim 35342a18c71dSJung-uk Kim ACPI_SERIAL_BEGIN(acpi); 35352a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 35362a18c71dSJung-uk Kim ACPI_SERIAL_END(acpi); 35372a18c71dSJung-uk Kim 35382a18c71dSJung-uk Kim return (0); 35392a18c71dSJung-uk Kim } 35402a18c71dSJung-uk Kim 35412a18c71dSJung-uk Kim static int 3542f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 3543f9390180SMitsuru IWASAKI { 3544f9390180SMitsuru IWASAKI int state, acpi_state; 3545f9390180SMitsuru IWASAKI int error; 3546f9390180SMitsuru IWASAKI struct acpi_softc *sc; 3547f9390180SMitsuru IWASAKI va_list ap; 3548f9390180SMitsuru IWASAKI 3549f9390180SMitsuru IWASAKI error = 0; 3550f9390180SMitsuru IWASAKI switch (cmd) { 3551f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 3552f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 3553f9390180SMitsuru IWASAKI if (sc == NULL) { 3554f9390180SMitsuru IWASAKI error = EINVAL; 3555f9390180SMitsuru IWASAKI goto out; 3556f9390180SMitsuru IWASAKI } 3557f9390180SMitsuru IWASAKI 3558f9390180SMitsuru IWASAKI va_start(ap, arg); 3559f9390180SMitsuru IWASAKI state = va_arg(ap, int); 3560f9390180SMitsuru IWASAKI va_end(ap); 3561f9390180SMitsuru IWASAKI 3562f9390180SMitsuru IWASAKI switch (state) { 3563f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 3564f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 3565f9390180SMitsuru IWASAKI break; 3566f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 3567f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 3568f9390180SMitsuru IWASAKI break; 3569f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 3570f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 3571f9390180SMitsuru IWASAKI break; 3572f9390180SMitsuru IWASAKI default: 3573f9390180SMitsuru IWASAKI error = EINVAL; 3574f9390180SMitsuru IWASAKI goto out; 3575f9390180SMitsuru IWASAKI } 3576f9390180SMitsuru IWASAKI 357700a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 357800a30448SNate Lawson error = ENXIO; 3579f9390180SMitsuru IWASAKI break; 3580f9390180SMitsuru IWASAKI default: 3581f9390180SMitsuru IWASAKI error = EINVAL; 3582f9390180SMitsuru IWASAKI goto out; 3583f9390180SMitsuru IWASAKI } 3584f9390180SMitsuru IWASAKI 3585f9390180SMitsuru IWASAKI out: 3586f9390180SMitsuru IWASAKI return (error); 3587f9390180SMitsuru IWASAKI } 3588f9390180SMitsuru IWASAKI 3589f9390180SMitsuru IWASAKI static void 3590f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 3591f9390180SMitsuru IWASAKI { 3592be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 35936c407052SMitsuru IWASAKI return; 3594f9390180SMitsuru IWASAKI 3595f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3596f9390180SMitsuru IWASAKI } 3597f9390180SMitsuru IWASAKI 3598f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3599