115e32d5dSMike Smith /*- 215e32d5dSMike Smith * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 315e32d5dSMike Smith * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4cb9b0d80SMike Smith * Copyright (c) 2000, 2001 Michael Smith 515e32d5dSMike Smith * Copyright (c) 2000 BSDi 615e32d5dSMike Smith * All rights reserved. 715e32d5dSMike Smith * 815e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 915e32d5dSMike Smith * modification, are permitted provided that the following conditions 1015e32d5dSMike Smith * are met: 1115e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1315e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1415e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1515e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1615e32d5dSMike Smith * 1715e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1815e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2015e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2115e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2215e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2315e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2415e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2515e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2615e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2715e32d5dSMike Smith * SUCH DAMAGE. 2815e32d5dSMike Smith */ 2915e32d5dSMike Smith 30dad97feeSDavid E. O'Brien #include <sys/cdefs.h> 31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$"); 32dad97feeSDavid E. O'Brien 3315e32d5dSMike Smith #include "opt_acpi.h" 3462d70a81SJohn Baldwin 3515e32d5dSMike Smith #include <sys/param.h> 36*e2e050c8SConrad Meyer #include <sys/eventhandler.h> 3715e32d5dSMike Smith #include <sys/kernel.h> 38fb919e4dSMark Murray #include <sys/proc.h> 39a89fcf28STakanori Watanabe #include <sys/fcntl.h> 4015e32d5dSMike Smith #include <sys/malloc.h> 41fe12f24bSPoul-Henning Kamp #include <sys/module.h> 4215e32d5dSMike Smith #include <sys/bus.h> 4315e32d5dSMike Smith #include <sys/conf.h> 4415e32d5dSMike Smith #include <sys/ioccom.h> 4515e32d5dSMike Smith #include <sys/reboot.h> 4615e32d5dSMike Smith #include <sys/sysctl.h> 4715e32d5dSMike Smith #include <sys/ctype.h> 481611ea87SMitsuru IWASAKI #include <sys/linker.h> 49f9390180SMitsuru IWASAKI #include <sys/power.h> 5054f6bca0SNate Lawson #include <sys/sbuf.h> 51c66d2b38SJung-uk Kim #include <sys/sched.h> 52eeb3a05fSNate Lawson #include <sys/smp.h> 53c66d2b38SJung-uk Kim #include <sys/timetc.h> 5415e32d5dSMike Smith 55d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 56279be68bSAndriy Gapon #include <machine/clock.h> 57d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h> 58d320e05cSJohn Baldwin #endif 5915e32d5dSMike Smith #include <machine/resource.h> 60dc750869SNate Lawson #include <machine/bus.h> 61dc750869SNate Lawson #include <sys/rman.h> 62bc0f2195SMike Smith #include <isa/isavar.h> 63c796e27bSNate Lawson #include <isa/pnpvar.h> 64bc0f2195SMike Smith 65129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h> 66129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h> 67129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h> 68129d3046SJung-uk Kim 6915e32d5dSMike Smith #include <dev/acpica/acpivar.h> 7015e32d5dSMike Smith #include <dev/acpica/acpiio.h> 7115e32d5dSMike Smith 720e68afe5SAndrew Turner #include <dev/pci/pcivar.h> 730e68afe5SAndrew Turner 742be4e471SJung-uk Kim #include <vm/vm_param.h> 752be4e471SJung-uk Kim 76d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 7715e32d5dSMike Smith 78be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 79cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 80b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 810ae55423SMike Smith 8215e32d5dSMike Smith static d_open_t acpiopen; 8315e32d5dSMike Smith static d_close_t acpiclose; 8415e32d5dSMike Smith static d_ioctl_t acpiioctl; 8515e32d5dSMike Smith 8615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 87dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 887ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 897ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 907ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 917ac40f5fSPoul-Henning Kamp .d_name = "acpi", 9215e32d5dSMike Smith }; 9315e32d5dSMike Smith 94ae19af49SJung-uk Kim struct acpi_interface { 95ae19af49SJung-uk Kim ACPI_STRING *data; 96ae19af49SJung-uk Kim int num; 97ae19af49SJung-uk Kim }; 98ae19af49SJung-uk Kim 99dc8ab16eSWarner Losh static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 100dc8ab16eSWarner Losh static char *pcilink_ids[] = { "PNP0C0F", NULL }; 101dc8ab16eSWarner Losh 102346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 103cb9b0d80SMike Smith struct mtx acpi_mutex; 104fadf3fb9SJohn Baldwin struct callout acpi_sleep_timer; 105cb9b0d80SMike Smith 10689fb5af8SNate Lawson /* Bitmap of device quirks. */ 10789fb5af8SNate Lawson int acpi_quirks; 10889fb5af8SNate Lawson 109a0e73a12SJung-uk Kim /* Supported sleep states. */ 110a0e73a12SJung-uk Kim static BOOLEAN acpi_sleep_states[ACPI_S_STATE_COUNT]; 111a0e73a12SJung-uk Kim 11264de8019SJohn Baldwin static void acpi_lookup(void *arg, const char *name, device_t *dev); 1136d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 11415e32d5dSMike Smith static int acpi_probe(device_t dev); 11515e32d5dSMike Smith static int acpi_attach(device_t dev); 11610ce62b9SNate Lawson static int acpi_suspend(device_t dev); 11710ce62b9SNate Lawson static int acpi_resume(device_t dev); 118169b539aSNate Lawson static int acpi_shutdown(device_t dev); 1193d844eddSAndriy Gapon static device_t acpi_add_child(device_t bus, u_int order, const char *name, 120be2b1797SNate Lawson int unit); 12115e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 12210ce62b9SNate Lawson static void acpi_probe_nomatch(device_t bus, device_t child); 12310ce62b9SNate Lawson static void acpi_driver_added(device_t dev, driver_t *driver); 124be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 125be2b1797SNate Lawson uintptr_t *result); 126be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 127be2b1797SNate Lawson uintptr_t value); 12891233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 129ea233199SJohn Baldwin static void acpi_reserve_resources(device_t dev); 130adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 131ea233199SJohn Baldwin static int acpi_set_resource(device_t dev, device_t child, int type, 1322dd1bdf1SJustin Hibbits int rid, rman_res_t start, rman_res_t count); 133be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 1342dd1bdf1SJustin Hibbits int type, int *rid, rman_res_t start, rman_res_t end, 1352dd1bdf1SJustin Hibbits rman_res_t count, u_int flags); 136049dc0d1SJohn Baldwin static int acpi_adjust_resource(device_t bus, device_t child, int type, 1372dd1bdf1SJustin Hibbits struct resource *r, rman_res_t start, rman_res_t end); 138be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 139be2b1797SNate Lawson int rid, struct resource *r); 1408b28b622SNate Lawson static void acpi_delete_resource(device_t bus, device_t child, int type, 1418b28b622SNate Lawson int rid); 1421e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1431e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 1445efca36fSTakanori Watanabe static int acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match); 145ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 146ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 147ce619b2eSNate Lawson ACPI_BUFFER *ret); 148df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 149df8d2a32SNate Lawson void *context, void **retval); 150df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 151df8d2a32SNate Lawson int max_depth, acpi_scan_cb_t user_fn, void *arg); 152a7a3177fSJung-uk Kim static int acpi_set_powerstate(device_t child, int state); 153be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 154be2b1797SNate Lawson struct isa_pnp_id *ids); 15515e32d5dSMike Smith static void acpi_probe_children(device_t bus); 156d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order); 157be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 158be2b1797SNate Lawson void *context, void **status); 159a0e73a12SJung-uk Kim static void acpi_sleep_enable(void *arg); 160a0e73a12SJung-uk Kim static ACPI_STATUS acpi_sleep_disable(struct acpi_softc *sc); 16100a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 16215e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 16313d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 164183c8af3SJohn Baldwin static BOOLEAN acpi_has_hid(ACPI_HANDLE handle); 165a0a15716SJung-uk Kim static void acpi_resync_clock(struct acpi_softc *sc); 166d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 167d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 168d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 16988a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 17088a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 17115e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 17215e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 173a0e73a12SJung-uk Kim static int acpi_sname2sstate(const char *sname); 174a0e73a12SJung-uk Kim static const char *acpi_sstate2sname(int sstate); 175d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1761d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1772a18c71dSJung-uk Kim static int acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS); 178f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 179879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 180879d6c23STakanori Watanabe char *buf, size_t buflen); 181879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 182879d6c23STakanori Watanabe char *buf, size_t buflen); 183d320e05cSJohn Baldwin static void acpi_enable_pcie(void); 1840d484d24SJohn Baldwin static void acpi_hint_device_unit(device_t acdev, device_t child, 1850d484d24SJohn Baldwin const char *name, int *unitp); 186ae19af49SJung-uk Kim static void acpi_reset_interfaces(device_t dev); 187879d6c23STakanori Watanabe 18815e32d5dSMike Smith static device_method_t acpi_methods[] = { 18915e32d5dSMike Smith /* Device interface */ 19015e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 19115e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 192169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 19356a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 19410ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend), 19510ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume), 19615e32d5dSMike Smith 19715e32d5dSMike Smith /* Bus interface */ 19815e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 19915e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 20010ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 20110ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added), 20215e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 20315e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 20491233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 205ea233199SJohn Baldwin DEVMETHOD(bus_set_resource, acpi_set_resource), 20691233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 20715e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 208049dc0d1SJohn Baldwin DEVMETHOD(bus_adjust_resource, acpi_adjust_resource), 20915e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 2108b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource), 211879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 212879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 21315e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 21415e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 21515e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 21615e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 2170d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 2188d791e5aSJohn Baldwin DEVMETHOD(bus_get_cpus, acpi_get_cpus), 219ffcf962dSAdrian Chadd DEVMETHOD(bus_get_domain, acpi_get_domain), 22015e32d5dSMike Smith 221ce619b2eSNate Lawson /* ACPI bus */ 222ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 223ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 22410ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 225df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 226ce619b2eSNate Lawson 227bc0f2195SMike Smith /* ISA emulation */ 228bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 229bc0f2195SMike Smith 23061bfd867SSofian Brabez DEVMETHOD_END 23115e32d5dSMike Smith }; 23215e32d5dSMike Smith 23315e32d5dSMike Smith static driver_t acpi_driver = { 23415e32d5dSMike Smith "acpi", 23515e32d5dSMike Smith acpi_methods, 23615e32d5dSMike Smith sizeof(struct acpi_softc), 23715e32d5dSMike Smith }; 23815e32d5dSMike Smith 2393273b005SMike Smith static devclass_t acpi_devclass; 2406d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 241a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 24215e32d5dSMike Smith 24315e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 24415e2f34fSNate Lawson 245adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 246adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 247adad4744SNate Lawson 248bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 249bee4aa4aSNate Lawson 2505217af30SJohn Baldwin /* Holds the description of the acpi0 device. */ 2515217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 2525217af30SJohn Baldwin 253197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 2541d7b121cSNate Lawson static char acpi_ca_version[12]; 2551d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2561d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 25715e32d5dSMike Smith 25815e32d5dSMike Smith /* 259ae19af49SJung-uk Kim * Allow overriding _OSI methods. 260ae19af49SJung-uk Kim */ 261ae19af49SJung-uk Kim static char acpi_install_interface[256]; 262ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.install_interface", acpi_install_interface, 263ae19af49SJung-uk Kim sizeof(acpi_install_interface)); 264ae19af49SJung-uk Kim static char acpi_remove_interface[256]; 265ae19af49SJung-uk Kim TUNABLE_STR("hw.acpi.remove_interface", acpi_remove_interface, 266ae19af49SJung-uk Kim sizeof(acpi_remove_interface)); 267ae19af49SJung-uk Kim 2682a18c71dSJung-uk Kim /* Allow users to dump Debug objects without ACPI debugger. */ 2692a18c71dSJung-uk Kim static int acpi_debug_objects; 2702a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.enable_debug_objects", &acpi_debug_objects); 2712a18c71dSJung-uk Kim SYSCTL_PROC(_debug_acpi, OID_AUTO, enable_debug_objects, 2722a18c71dSJung-uk Kim CTLFLAG_RW | CTLTYPE_INT, NULL, 0, acpi_debug_objects_sysctl, "I", 2732a18c71dSJung-uk Kim "Enable Debug objects"); 2742a18c71dSJung-uk Kim 2752a18c71dSJung-uk Kim /* Allow the interpreter to ignore common mistakes in BIOS. */ 2762a18c71dSJung-uk Kim static int acpi_interpreter_slack = 1; 2772a18c71dSJung-uk Kim TUNABLE_INT("debug.acpi.interpreter_slack", &acpi_interpreter_slack); 2782a18c71dSJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, interpreter_slack, CTLFLAG_RDTUN, 2792a18c71dSJung-uk Kim &acpi_interpreter_slack, 1, "Turn on interpreter slack mode."); 2802a18c71dSJung-uk Kim 281313a0c13SJung-uk Kim /* Ignore register widths set by FADT and use default widths instead. */ 282313a0c13SJung-uk Kim static int acpi_ignore_reg_width = 1; 283313a0c13SJung-uk Kim TUNABLE_INT("debug.acpi.default_register_width", &acpi_ignore_reg_width); 284313a0c13SJung-uk Kim SYSCTL_INT(_debug_acpi, OID_AUTO, default_register_width, CTLFLAG_RDTUN, 285313a0c13SJung-uk Kim &acpi_ignore_reg_width, 1, "Ignore register widths set by FADT"); 286313a0c13SJung-uk Kim 28739da3eb3SNate Lawson /* Allow users to override quirks. */ 28839da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 28939da3eb3SNate Lawson 290e787342eSJung-uk Kim int acpi_susp_bounce; 29193bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 29293bfd059SNate Lawson &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 29393bfd059SNate Lawson 294c9b8d77dSNate Lawson /* 2956d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2966d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 2978c0879b6SJohn Baldwin * It also cannot be unloaded, since the entire system bus hierarchy hangs 298be2b1797SNate Lawson * off it. 2996d63101aSMike Smith */ 3006d63101aSMike Smith static int 3016d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 3026d63101aSMike Smith { 3036d63101aSMike Smith switch (event) { 3046d63101aSMike Smith case MOD_LOAD: 30587b45ed5SMitsuru IWASAKI if (!cold) { 30687b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 3076d63101aSMike Smith return (EPERM); 30887b45ed5SMitsuru IWASAKI } 3096d63101aSMike Smith break; 3106d63101aSMike Smith case MOD_UNLOAD: 311f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 3126d63101aSMike Smith return (EBUSY); 313f9390180SMitsuru IWASAKI break; 3146d63101aSMike Smith default: 3156d63101aSMike Smith break; 3166d63101aSMike Smith } 3176d63101aSMike Smith return (0); 3186d63101aSMike Smith } 3196d63101aSMike Smith 3206d63101aSMike Smith /* 321bbc2815cSJohn Baldwin * Perform early initialization. 32215e32d5dSMike Smith */ 323bbc2815cSJohn Baldwin ACPI_STATUS 324bbc2815cSJohn Baldwin acpi_Startup(void) 32515e32d5dSMike Smith { 32689fb5af8SNate Lawson static int started = 0; 3272be4e471SJung-uk Kim ACPI_STATUS status; 3282be4e471SJung-uk Kim int val; 32915e32d5dSMike Smith 3301b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 3311b8c233dSPeter Pentchev 332bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */ 333bbc2815cSJohn Baldwin if (started) 3342be4e471SJung-uk Kim return_VALUE (AE_OK); 335bbc2815cSJohn Baldwin started = 1; 33615e32d5dSMike Smith 337413081d7SNate Lawson /* 3385f9b24faSJung-uk Kim * Initialize the ACPICA subsystem. 3395f9b24faSJung-uk Kim */ 3405f9b24faSJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) { 3415f9b24faSJung-uk Kim printf("ACPI: Could not initialize Subsystem: %s\n", 3425f9b24faSJung-uk Kim AcpiFormatException(status)); 3435f9b24faSJung-uk Kim return_VALUE (status); 3445f9b24faSJung-uk Kim } 3455f9b24faSJung-uk Kim 3465f9b24faSJung-uk Kim /* 3472be4e471SJung-uk Kim * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 3482be4e471SJung-uk Kim * if more tables exist. 349413081d7SNate Lawson */ 3502be4e471SJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 3512be4e471SJung-uk Kim printf("ACPI: Table initialisation failed: %s\n", 3522be4e471SJung-uk Kim AcpiFormatException(status)); 3532be4e471SJung-uk Kim return_VALUE (status); 35415e32d5dSMike Smith } 3553184cf5aSNate Lawson 35689fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 3572be4e471SJung-uk Kim if (acpi_quirks == ACPI_Q_OK) 35889fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 35989fb5af8SNate Lawson 36039da3eb3SNate Lawson /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 36189fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 36289fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 36389fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 36489fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 3652be4e471SJung-uk Kim status = AE_SUPPORT; 36689fb5af8SNate Lawson } 3673184cf5aSNate Lawson 3682be4e471SJung-uk Kim return_VALUE (status); 369bbc2815cSJohn Baldwin } 370bbc2815cSJohn Baldwin 371bbc2815cSJohn Baldwin /* 3725217af30SJohn Baldwin * Detect ACPI and perform early initialisation. 373bbc2815cSJohn Baldwin */ 3745217af30SJohn Baldwin int 3755217af30SJohn Baldwin acpi_identify(void) 376bbc2815cSJohn Baldwin { 3775217af30SJohn Baldwin ACPI_TABLE_RSDP *rsdp; 3785217af30SJohn Baldwin ACPI_TABLE_HEADER *rsdt; 3795217af30SJohn Baldwin ACPI_PHYSICAL_ADDRESS paddr; 3805217af30SJohn Baldwin struct sbuf sb; 381bbc2815cSJohn Baldwin 382bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 383bbc2815cSJohn Baldwin 384bbc2815cSJohn Baldwin if (!cold) 3855217af30SJohn Baldwin return (ENXIO); 386bbc2815cSJohn Baldwin 387a8de37b0SEitan Adler /* Check that we haven't been disabled with a hint. */ 388a8de37b0SEitan Adler if (resource_disabled("acpi", 0)) 389a8de37b0SEitan Adler return (ENXIO); 390a8de37b0SEitan Adler 3915217af30SJohn Baldwin /* Check for other PM systems. */ 3925217af30SJohn Baldwin if (power_pm_get_type() != POWER_PM_TYPE_NONE && 3935217af30SJohn Baldwin power_pm_get_type() != POWER_PM_TYPE_ACPI) { 3945217af30SJohn Baldwin printf("ACPI identify failed, other PM system enabled.\n"); 3955217af30SJohn Baldwin return (ENXIO); 3965217af30SJohn Baldwin } 3974b1ff978SMark Santcroos 3982be4e471SJung-uk Kim /* Initialize root tables. */ 3992be4e471SJung-uk Kim if (ACPI_FAILURE(acpi_Startup())) { 4002be4e471SJung-uk Kim printf("ACPI: Try disabling either ACPI or apic support.\n"); 4015217af30SJohn Baldwin return (ENXIO); 4022be4e471SJung-uk Kim } 40315e32d5dSMike Smith 4045217af30SJohn Baldwin if ((paddr = AcpiOsGetRootPointer()) == 0 || 4055217af30SJohn Baldwin (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 4065217af30SJohn Baldwin return (ENXIO); 4075217af30SJohn Baldwin if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 4085217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 4095217af30SJohn Baldwin else 4105217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 4115217af30SJohn Baldwin AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 4125217af30SJohn Baldwin 4135217af30SJohn Baldwin if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 4145217af30SJohn Baldwin return (ENXIO); 4155217af30SJohn Baldwin sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 4165217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 4175217af30SJohn Baldwin sbuf_trim(&sb); 4185217af30SJohn Baldwin sbuf_putc(&sb, ' '); 4195217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 4205217af30SJohn Baldwin sbuf_trim(&sb); 4215217af30SJohn Baldwin sbuf_finish(&sb); 4225217af30SJohn Baldwin sbuf_delete(&sb); 4235217af30SJohn Baldwin AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 4245217af30SJohn Baldwin 4255217af30SJohn Baldwin snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 4265217af30SJohn Baldwin 4275217af30SJohn Baldwin return (0); 42815e32d5dSMike Smith } 42915e32d5dSMike Smith 43015e32d5dSMike Smith /* 431bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message. 43215e32d5dSMike Smith */ 43315e32d5dSMike Smith static int 43415e32d5dSMike Smith acpi_probe(device_t dev) 43515e32d5dSMike Smith { 43615e32d5dSMike Smith 437b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 438f9390180SMitsuru IWASAKI 4395217af30SJohn Baldwin device_set_desc(dev, acpi_desc); 440bee4aa4aSNate Lawson 441feeec74dSNathan Whitehorn return_VALUE (BUS_PROBE_NOWILDCARD); 44215e32d5dSMike Smith } 44315e32d5dSMike Smith 44415e32d5dSMike Smith static int 44515e32d5dSMike Smith acpi_attach(device_t dev) 44615e32d5dSMike Smith { 44715e32d5dSMike Smith struct acpi_softc *sc; 448cb9b0d80SMike Smith ACPI_STATUS status; 449ea27c63eSNate Lawson int error, state; 45032d18aa5SMike Smith UINT32 flags; 451ea27c63eSNate Lawson UINT8 TypeA, TypeB; 452498d464fSMitsuru IWASAKI char *env; 45315e32d5dSMike Smith 454b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 455bee4aa4aSNate Lawson 45615e32d5dSMike Smith sc = device_get_softc(dev); 45715e32d5dSMike Smith sc->acpi_dev = dev; 458fd90e2edSJung-uk Kim callout_init(&sc->susp_force_to, 1); 45915e32d5dSMike Smith 4602be4e471SJung-uk Kim error = ENXIO; 4612be4e471SJung-uk Kim 46291233413SNate Lawson /* Initialize resource manager. */ 46391233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 46491233413SNate Lawson acpi_rman_io.rm_start = 0; 46591233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 4660bba6acfSJohn Baldwin acpi_rman_io.rm_descr = "ACPI I/O ports"; 46791233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 46891233413SNate Lawson panic("acpi rman_init IO ports failed"); 46991233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 4700bba6acfSJohn Baldwin acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 47191233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 47291233413SNate Lawson panic("acpi rman_init memory failed"); 47391233413SNate Lawson 4742be4e471SJung-uk Kim /* Initialise the ACPI mutex */ 4752be4e471SJung-uk Kim mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 4762be4e471SJung-uk Kim 4772be4e471SJung-uk Kim /* 4782be4e471SJung-uk Kim * Set the globals from our tunables. This is needed because ACPI-CA 4792be4e471SJung-uk Kim * uses UINT8 for some values and we have no tunable_byte. 4802be4e471SJung-uk Kim */ 4812a18c71dSJung-uk Kim AcpiGbl_EnableInterpreterSlack = acpi_interpreter_slack ? TRUE : FALSE; 4822a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 483313a0c13SJung-uk Kim AcpiGbl_UseDefaultRegisterWidths = acpi_ignore_reg_width ? TRUE : FALSE; 4842a18c71dSJung-uk Kim 4852a18c71dSJung-uk Kim #ifndef ACPI_DEBUG 4862a18c71dSJung-uk Kim /* 4872a18c71dSJung-uk Kim * Disable all debugging layers and levels. 4882a18c71dSJung-uk Kim */ 4892a18c71dSJung-uk Kim AcpiDbgLayer = 0; 4902a18c71dSJung-uk Kim AcpiDbgLevel = 0; 4912a18c71dSJung-uk Kim #endif 4922be4e471SJung-uk Kim 493ae19af49SJung-uk Kim /* Override OS interfaces if the user requested. */ 494ae19af49SJung-uk Kim acpi_reset_interfaces(dev); 495ae19af49SJung-uk Kim 4962be4e471SJung-uk Kim /* Load ACPI name space. */ 4972be4e471SJung-uk Kim status = AcpiLoadTables(); 4982be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4992be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n", 5002be4e471SJung-uk Kim AcpiFormatException(status)); 5012be4e471SJung-uk Kim goto out; 5022be4e471SJung-uk Kim } 5032be4e471SJung-uk Kim 504d320e05cSJohn Baldwin /* Handle MCFG table if present. */ 505d320e05cSJohn Baldwin acpi_enable_pcie(); 506d320e05cSJohn Baldwin 50715e32d5dSMike Smith /* 508be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 509be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 510be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 511be2b1797SNate Lawson * object init pass. 51215e32d5dSMike Smith * 51332d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 51432d18aa5SMike Smith * 515be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 516be2b1797SNate Lawson * all our child devices, but on many systems it works here. 51715e32d5dSMike Smith */ 51832d18aa5SMike Smith flags = 0; 519d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 52032d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 521bee4aa4aSNate Lawson 522bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 523b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 524be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 525be2b1797SNate Lawson AcpiFormatException(status)); 526cb9b0d80SMike Smith goto out; 52715e32d5dSMike Smith } 52815e32d5dSMike Smith 529f8335e3aSNate Lawson /* 530f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 531f8335e3aSNate Lawson * the namespace has been evaluated. 532da72d149SNate Lawson * 533da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and 534da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't 535da72d149SNate Lawson * a problem but should be addressed eventually. 536f8335e3aSNate Lawson */ 537f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 538f8335e3aSNate Lawson 539bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 540b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 541be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 542be2b1797SNate Lawson AcpiFormatException(status)); 543b69ed3f4SMitsuru IWASAKI goto out; 544b69ed3f4SMitsuru IWASAKI } 545b69ed3f4SMitsuru IWASAKI 54615e32d5dSMike Smith /* 5471d073b1dSJohn Baldwin * Setup our sysctl tree. 5481d073b1dSJohn Baldwin * 5491d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5501d073b1dSJohn Baldwin */ 5511d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5521d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5531d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5541d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5551d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 556d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 557ad73b301SAdrian Chadd 0, 0, acpi_supported_sleep_state_sysctl, "A", 558ad73b301SAdrian Chadd "List supported ACPI sleep states."); 559d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5601d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 561ad73b301SAdrian Chadd &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", 562ad73b301SAdrian Chadd "Power button ACPI sleep state."); 5631d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5641d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 565ad73b301SAdrian Chadd &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", 566ad73b301SAdrian Chadd "Sleep button ACPI sleep state."); 5671d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5681d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 569ad73b301SAdrian Chadd &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", 570ad73b301SAdrian Chadd "Lid ACPI sleep state. Set to S3 if you want to suspend your laptop when close the Lid."); 571f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 572f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 573f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 574f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 575f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 576f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5772d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 578197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 57931f301d0SChristian Brueffer "sleep delay in seconds"); 580ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 581197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 5821611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 583197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 584d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 585d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 586d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 587d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 588d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW, 589d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 590bf0c18ecSNate Lawson 591bf0c18ecSNate Lawson /* 5922b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 593bf0c18ecSNate Lawson * stabilize. 594bf0c18ecSNate Lawson */ 5952b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5962d644607SMitsuru IWASAKI if (bootverbose) 5972d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 5982be111bfSDavide Italiano if ((env = kern_getenv("hw.acpi.verbose")) != NULL) { 599965a34fbSNate Lawson if (strcmp(env, "0") != 0) 600498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 601498d464fSMitsuru IWASAKI freeenv(env); 602498d464fSMitsuru IWASAKI } 6031d073b1dSJohn Baldwin 604ac731af5SJung-uk Kim /* Only enable reboot by default if the FADT says it is available. */ 605ac731af5SJung-uk Kim if (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) 606ac731af5SJung-uk Kim sc->acpi_handle_reboot = 1; 607ac731af5SJung-uk Kim 608fe5d5d7dSAndrew Turner #if !ACPI_REDUCED_HARDWARE 6092d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 610d8c37c3aSAndrew Turner if (AcpiGbl_FACS != NULL && AcpiGbl_FACS->Flags & ACPI_FACS_S4_BIOS_PRESENT) 6112d610c46SNate Lawson sc->acpi_s4bios = 1; 612fe5d5d7dSAndrew Turner #endif 6132d610c46SNate Lawson 614a0e73a12SJung-uk Kim /* Probe all supported sleep states. */ 615a0e73a12SJung-uk Kim acpi_sleep_states[ACPI_STATE_S0] = TRUE; 616a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 617efcc2a30SJung-uk Kim if (ACPI_SUCCESS(AcpiEvaluateObject(ACPI_ROOT_OBJECT, 618efcc2a30SJung-uk Kim __DECONST(char *, AcpiGbl_SleepStateNames[state]), NULL, NULL)) && 619efcc2a30SJung-uk Kim ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 620a0e73a12SJung-uk Kim acpi_sleep_states[state] = TRUE; 621a0e73a12SJung-uk Kim 6221d073b1dSJohn Baldwin /* 623ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 624a0e73a12SJung-uk Kim * to UNKNOWN by default to avoid surprising users. 62515e32d5dSMike Smith */ 626a0e73a12SJung-uk Kim sc->acpi_power_button_sx = acpi_sleep_states[ACPI_STATE_S5] ? 627a0e73a12SJung-uk Kim ACPI_STATE_S5 : ACPI_STATE_UNKNOWN; 628a0e73a12SJung-uk Kim sc->acpi_lid_switch_sx = ACPI_STATE_UNKNOWN; 629a0e73a12SJung-uk Kim sc->acpi_standby_sx = acpi_sleep_states[ACPI_STATE_S1] ? 630a0e73a12SJung-uk Kim ACPI_STATE_S1 : ACPI_STATE_UNKNOWN; 631a0e73a12SJung-uk Kim sc->acpi_suspend_sx = acpi_sleep_states[ACPI_STATE_S3] ? 632a0e73a12SJung-uk Kim ACPI_STATE_S3 : ACPI_STATE_UNKNOWN; 63315e32d5dSMike Smith 634ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 635a0e73a12SJung-uk Kim sc->acpi_sleep_button_sx = ACPI_STATE_UNKNOWN; 63600a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 637a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) { 638ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 639ea27c63eSNate Lawson break; 640ea27c63eSNate Lawson } 641ea27c63eSNate Lawson 64213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 64315e32d5dSMike Smith 64415e32d5dSMike Smith /* 64515e32d5dSMike Smith * Scan the namespace and attach/initialise children. 64615e32d5dSMike Smith */ 64715e32d5dSMike Smith 64859e472e9SNate Lawson /* Register our shutdown handler. */ 649be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 650be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 65115e32d5dSMike Smith 65215e32d5dSMike Smith /* 65315e32d5dSMike Smith * Register our acpi event handlers. 65415e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 65515e32d5dSMike Smith */ 656be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 657be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 658be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 659be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 66015e32d5dSMike Smith 661be2b1797SNate Lawson /* Flag our initial states. */ 662a0e73a12SJung-uk Kim sc->acpi_enabled = TRUE; 66315e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 664a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 66515e32d5dSMike Smith 666be2b1797SNate Lawson /* Create the control device */ 667d1655c6fSEdward Tomasz Napierala sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664, 6682a4689e8SRobert Watson "acpi"); 66915e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 67015e32d5dSMike Smith 671be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 672f86214b6SMitsuru IWASAKI goto out; 673f86214b6SMitsuru IWASAKI 674f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 675f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 676f9390180SMitsuru IWASAKI 67764de8019SJohn Baldwin if (!acpi_disabled("bus")) { 67864de8019SJohn Baldwin EVENTHANDLER_REGISTER(dev_lookup, acpi_lookup, NULL, 1000); 679eeb6dba2SJohn Baldwin acpi_probe_children(dev); 68064de8019SJohn Baldwin } 681eeb6dba2SJohn Baldwin 6825a77b11bSJung-uk Kim /* Update all GPEs and enable runtime GPEs. */ 6835a77b11bSJung-uk Kim status = AcpiUpdateAllGpes(); 6845a77b11bSJung-uk Kim if (ACPI_FAILURE(status)) 6855a77b11bSJung-uk Kim device_printf(dev, "Could not update all GPEs: %s\n", 6865a77b11bSJung-uk Kim AcpiFormatException(status)); 6875a77b11bSJung-uk Kim 688a0e73a12SJung-uk Kim /* Allow sleep request after a while. */ 689fadf3fb9SJohn Baldwin callout_init_mtx(&acpi_sleep_timer, &acpi_mutex, 0); 690fadf3fb9SJohn Baldwin callout_reset(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME, 691fadf3fb9SJohn Baldwin acpi_sleep_enable, sc); 692a0e73a12SJung-uk Kim 693cb9b0d80SMike Smith error = 0; 694cb9b0d80SMike Smith 695cb9b0d80SMike Smith out: 696cb9b0d80SMike Smith return_VALUE (error); 69715e32d5dSMike Smith } 69815e32d5dSMike Smith 699a7a3177fSJung-uk Kim static void 700a7a3177fSJung-uk Kim acpi_set_power_children(device_t dev, int state) 701a7a3177fSJung-uk Kim { 70244aba0f6SJung-uk Kim device_t child; 703a7a3177fSJung-uk Kim device_t *devlist; 704a7a3177fSJung-uk Kim int dstate, i, numdevs; 705a7a3177fSJung-uk Kim 706a7a3177fSJung-uk Kim if (device_get_children(dev, &devlist, &numdevs) != 0) 707a7a3177fSJung-uk Kim return; 708a7a3177fSJung-uk Kim 709a7a3177fSJung-uk Kim /* 710a7a3177fSJung-uk Kim * Retrieve and set D-state for the sleep state if _SxD is present. 711a7a3177fSJung-uk Kim * Skip children who aren't attached since they are handled separately. 712a7a3177fSJung-uk Kim */ 713a7a3177fSJung-uk Kim for (i = 0; i < numdevs; i++) { 714a7a3177fSJung-uk Kim child = devlist[i]; 715a7a3177fSJung-uk Kim dstate = state; 716a7a3177fSJung-uk Kim if (device_is_attached(child) && 71744aba0f6SJung-uk Kim acpi_device_pwr_for_sleep(dev, child, &dstate) == 0) 718a7a3177fSJung-uk Kim acpi_set_powerstate(child, dstate); 719a7a3177fSJung-uk Kim } 720a7a3177fSJung-uk Kim free(devlist, M_TEMP); 721a7a3177fSJung-uk Kim } 722a7a3177fSJung-uk Kim 723169b539aSNate Lawson static int 72410ce62b9SNate Lawson acpi_suspend(device_t dev) 72510ce62b9SNate Lawson { 726a7a3177fSJung-uk Kim int error; 72710ce62b9SNate Lawson 728a56fe095SJohn Baldwin GIANT_REQUIRED; 729a56fe095SJohn Baldwin 73010ce62b9SNate Lawson error = bus_generic_suspend(dev); 731a7a3177fSJung-uk Kim if (error == 0) 732a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D3); 73310ce62b9SNate Lawson 73410ce62b9SNate Lawson return (error); 73510ce62b9SNate Lawson } 73610ce62b9SNate Lawson 73710ce62b9SNate Lawson static int 73810ce62b9SNate Lawson acpi_resume(device_t dev) 73910ce62b9SNate Lawson { 74010ce62b9SNate Lawson 741a56fe095SJohn Baldwin GIANT_REQUIRED; 742a56fe095SJohn Baldwin 743a7a3177fSJung-uk Kim acpi_set_power_children(dev, ACPI_STATE_D0); 74410ce62b9SNate Lawson 74510ce62b9SNate Lawson return (bus_generic_resume(dev)); 74610ce62b9SNate Lawson } 74710ce62b9SNate Lawson 74810ce62b9SNate Lawson static int 749169b539aSNate Lawson acpi_shutdown(device_t dev) 750169b539aSNate Lawson { 751169b539aSNate Lawson 752a56fe095SJohn Baldwin GIANT_REQUIRED; 753a56fe095SJohn Baldwin 7540e414868SNate Lawson /* Allow children to shutdown first. */ 7550e414868SNate Lawson bus_generic_shutdown(dev); 7560e414868SNate Lawson 757bee4aa4aSNate Lawson /* 758bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 759bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 760bee4aa4aSNate Lawson */ 761d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 762d4b9ff91SNate Lawson 763169b539aSNate Lawson return (0); 764169b539aSNate Lawson } 765169b539aSNate Lawson 76615e32d5dSMike Smith /* 76715e32d5dSMike Smith * Handle a new device being added 76815e32d5dSMike Smith */ 76915e32d5dSMike Smith static device_t 7703d844eddSAndriy Gapon acpi_add_child(device_t bus, u_int order, const char *name, int unit) 77115e32d5dSMike Smith { 77215e32d5dSMike Smith struct acpi_device *ad; 77315e32d5dSMike Smith device_t child; 77415e32d5dSMike Smith 775be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 77615e32d5dSMike Smith return (NULL); 77715e32d5dSMike Smith 77815e32d5dSMike Smith resource_list_init(&ad->ad_rl); 77915e32d5dSMike Smith 78015e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 78115e32d5dSMike Smith if (child != NULL) 78215e32d5dSMike Smith device_set_ivars(child, ad); 78343ce1c77SNate Lawson else 78443ce1c77SNate Lawson free(ad, M_ACPIDEV); 78515e32d5dSMike Smith return (child); 78615e32d5dSMike Smith } 78715e32d5dSMike Smith 78815e32d5dSMike Smith static int 78915e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 79015e32d5dSMike Smith { 79115e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 79215e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 79315e32d5dSMike Smith int retval = 0; 79415e32d5dSMike Smith 79515e32d5dSMike Smith retval += bus_print_child_header(bus, child); 796da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx"); 797da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx"); 798da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 799da1b038aSJustin Hibbits retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd"); 800a91c5fa8SNate Lawson if (device_get_flags(child)) 801a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 802ffcf962dSAdrian Chadd retval += bus_print_child_domain(bus, child); 8032c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 80415e32d5dSMike Smith 80515e32d5dSMike Smith return (retval); 80615e32d5dSMike Smith } 80715e32d5dSMike Smith 80810ce62b9SNate Lawson /* 80910ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt 81010ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added. 81110ce62b9SNate Lawson * 81210ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and 81310ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect 81410ce62b9SNate Lawson * them to be powered up. 81510ce62b9SNate Lawson */ 81610ce62b9SNate Lawson static void 81710ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child) 81810ce62b9SNate Lawson { 81996cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 820a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 82196cf0b6dSWarner Losh #endif 82210ce62b9SNate Lawson } 82310ce62b9SNate Lawson 82410ce62b9SNate Lawson /* 82510ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up. 82610ce62b9SNate Lawson * 82710ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details). 82810ce62b9SNate Lawson */ 82910ce62b9SNate Lawson static void 83010ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver) 83110ce62b9SNate Lawson { 83210ce62b9SNate Lawson device_t child, *devlist; 83310ce62b9SNate Lawson int i, numdevs; 83410ce62b9SNate Lawson 83510ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev); 836099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs)) 837099ea4b5SWarner Losh return; 83810ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 83910ce62b9SNate Lawson child = devlist[i]; 84010ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) { 84196cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 842a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D0); 84310ce62b9SNate Lawson if (device_probe_and_attach(child) != 0) 844a7a3177fSJung-uk Kim acpi_set_powerstate(child, ACPI_STATE_D3); 84596cf0b6dSWarner Losh #else 84696cf0b6dSWarner Losh device_probe_and_attach(child); 84796cf0b6dSWarner Losh #endif 84810ce62b9SNate Lawson } 84910ce62b9SNate Lawson } 85010ce62b9SNate Lawson free(devlist, M_TEMP); 85110ce62b9SNate Lawson } 85210ce62b9SNate Lawson 85363600cc3SNate Lawson /* Location hint for devctl(8) */ 85463600cc3SNate Lawson static int 855247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 856247648afSTakanori Watanabe size_t buflen) 857247648afSTakanori Watanabe { 858247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 8599cf5a6aaSAdrian Chadd char buf2[32]; 8609cf5a6aaSAdrian Chadd int pxm; 861247648afSTakanori Watanabe 8629cf5a6aaSAdrian Chadd if (dinfo->ad_handle) { 863d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 8649cf5a6aaSAdrian Chadd if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ad_handle, "_PXM", &pxm))) { 8659cf5a6aaSAdrian Chadd snprintf(buf2, 32, " _PXM=%d", pxm); 8669cf5a6aaSAdrian Chadd strlcat(buf, buf2, buflen); 8679cf5a6aaSAdrian Chadd } 8689cf5a6aaSAdrian Chadd } else { 869d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 8709cf5a6aaSAdrian Chadd } 871247648afSTakanori Watanabe return (0); 872247648afSTakanori Watanabe } 873247648afSTakanori Watanabe 87463600cc3SNate Lawson /* PnP information for devctl(8) */ 87563600cc3SNate Lawson static int 876247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 877247648afSTakanori Watanabe size_t buflen) 878247648afSTakanori Watanabe { 87963600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 88092488a57SJung-uk Kim ACPI_DEVICE_INFO *adinfo; 881247648afSTakanori Watanabe 88292488a57SJung-uk Kim if (ACPI_FAILURE(AcpiGetObjectInfo(dinfo->ad_handle, &adinfo))) { 883d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 88492488a57SJung-uk Kim return (0); 88592488a57SJung-uk Kim } 88692488a57SJung-uk Kim 887247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 888247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 88992488a57SJung-uk Kim adinfo->HardwareId.String : "none", 89063600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 89192488a57SJung-uk Kim strtoul(adinfo->UniqueId.String, NULL, 10) : 0UL); 892247648afSTakanori Watanabe AcpiOsFree(adinfo); 893247648afSTakanori Watanabe 894247648afSTakanori Watanabe return (0); 895247648afSTakanori Watanabe } 896247648afSTakanori Watanabe 897247648afSTakanori Watanabe /* 89815e32d5dSMike Smith * Handle per-device ivars 89915e32d5dSMike Smith */ 90015e32d5dSMike Smith static int 90115e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 90215e32d5dSMike Smith { 90315e32d5dSMike Smith struct acpi_device *ad; 90415e32d5dSMike Smith 90515e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9062d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 90715e32d5dSMike Smith return (ENOENT); 90815e32d5dSMike Smith } 90915e32d5dSMike Smith 910be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 91115e32d5dSMike Smith switch(index) { 91215e32d5dSMike Smith case ACPI_IVAR_HANDLE: 91315e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 91415e32d5dSMike Smith break; 91515e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 91615e32d5dSMike Smith *(void **)result = ad->ad_private; 91715e32d5dSMike Smith break; 918d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 919d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 920d4b9ff91SNate Lawson break; 92132d18aa5SMike Smith case ISA_IVAR_VENDORID: 92232d18aa5SMike Smith case ISA_IVAR_SERIAL: 92332d18aa5SMike Smith case ISA_IVAR_COMPATID: 92432d18aa5SMike Smith *(int *)result = -1; 92532d18aa5SMike Smith break; 92632d18aa5SMike Smith case ISA_IVAR_LOGICALID: 92732d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 92832d18aa5SMike Smith break; 9290e68afe5SAndrew Turner case PCI_IVAR_CLASS: 9300e68afe5SAndrew Turner *(uint8_t*)result = (ad->ad_cls_class >> 16) & 0xff; 9310e68afe5SAndrew Turner break; 9320e68afe5SAndrew Turner case PCI_IVAR_SUBCLASS: 9330e68afe5SAndrew Turner *(uint8_t*)result = (ad->ad_cls_class >> 8) & 0xff; 9340e68afe5SAndrew Turner break; 9350e68afe5SAndrew Turner case PCI_IVAR_PROGIF: 9360e68afe5SAndrew Turner *(uint8_t*)result = (ad->ad_cls_class >> 0) & 0xff; 9370e68afe5SAndrew Turner break; 93815e32d5dSMike Smith default: 93915e32d5dSMike Smith return (ENOENT); 94015e32d5dSMike Smith } 941be2b1797SNate Lawson 94215e32d5dSMike Smith return (0); 94315e32d5dSMike Smith } 94415e32d5dSMike Smith 94515e32d5dSMike Smith static int 94615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 94715e32d5dSMike Smith { 94815e32d5dSMike Smith struct acpi_device *ad; 94915e32d5dSMike Smith 95015e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 9512d0c82e8SJung-uk Kim device_printf(child, "device has no ivars\n"); 95215e32d5dSMike Smith return (ENOENT); 95315e32d5dSMike Smith } 95415e32d5dSMike Smith 95515e32d5dSMike Smith switch(index) { 95615e32d5dSMike Smith case ACPI_IVAR_HANDLE: 95715e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 95815e32d5dSMike Smith break; 95915e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 96015e32d5dSMike Smith ad->ad_private = (void *)value; 96115e32d5dSMike Smith break; 962d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 963d4b9ff91SNate Lawson ad->ad_flags = (int)value; 964d4b9ff91SNate Lawson break; 96515e32d5dSMike Smith default: 9662ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 96715e32d5dSMike Smith return (ENOENT); 96815e32d5dSMike Smith } 969be2b1797SNate Lawson 97015e32d5dSMike Smith return (0); 97115e32d5dSMike Smith } 97215e32d5dSMike Smith 97315e32d5dSMike Smith /* 97415e32d5dSMike Smith * Handle child resource allocation/removal 97515e32d5dSMike Smith */ 97691233413SNate Lawson static struct resource_list * 97791233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 97815e32d5dSMike Smith { 97991233413SNate Lawson struct acpi_device *ad; 98015e32d5dSMike Smith 98191233413SNate Lawson ad = device_get_ivars(child); 98291233413SNate Lawson return (&ad->ad_rl); 98315e32d5dSMike Smith } 98415e32d5dSMike Smith 9850d484d24SJohn Baldwin static int 9860d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value) 9870d484d24SJohn Baldwin { 9880d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev); 9890d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 9900d484d24SJohn Baldwin struct resource_list_entry *rle; 9910d484d24SJohn Baldwin 9920d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 9930d484d24SJohn Baldwin if (rle->type != type) 9940d484d24SJohn Baldwin continue; 9950d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value) 9960d484d24SJohn Baldwin return (1); 9970d484d24SJohn Baldwin } 9980d484d24SJohn Baldwin return (0); 9990d484d24SJohn Baldwin } 10000d484d24SJohn Baldwin 10010d484d24SJohn Baldwin /* 10020d484d24SJohn Baldwin * Wire device unit numbers based on resource matches in hints. 10030d484d24SJohn Baldwin */ 10040d484d24SJohn Baldwin static void 10050d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 10060d484d24SJohn Baldwin int *unitp) 10070d484d24SJohn Baldwin { 10080d484d24SJohn Baldwin const char *s; 10090d484d24SJohn Baldwin long value; 10100d484d24SJohn Baldwin int line, matches, unit; 10110d484d24SJohn Baldwin 10120d484d24SJohn Baldwin /* 10130d484d24SJohn Baldwin * Iterate over all the hints for the devices with the specified 10140d484d24SJohn Baldwin * name to see if one's resources are a subset of this device. 10150d484d24SJohn Baldwin */ 10160d484d24SJohn Baldwin line = 0; 10174571c92fSWarner Losh while (resource_find_dev(&line, name, &unit, "at", NULL) == 0) { 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 /* 10828d791e5aSJohn Baldwin * Fetch the NUMA domain for a device by mapping the value returned by 10838d791e5aSJohn Baldwin * _PXM to a NUMA domain. If the device does not have a _PXM method, 10848d791e5aSJohn Baldwin * -2 is returned. If any other error occurs, -1 is returned. 10855d18c60aSAdrian Chadd */ 10868d791e5aSJohn Baldwin static int 10878d791e5aSJohn Baldwin acpi_parse_pxm(device_t dev) 10885d18c60aSAdrian Chadd { 1089b6715dabSJeff Roberson #ifdef NUMA 1090bcc51ef4SMark Johnston #if defined(__i386__) || defined(__amd64__) 10918d791e5aSJohn Baldwin ACPI_HANDLE handle; 10928d791e5aSJohn Baldwin ACPI_STATUS status; 10938d791e5aSJohn Baldwin int pxm; 10945d18c60aSAdrian Chadd 10958d791e5aSJohn Baldwin handle = acpi_get_handle(dev); 10968d791e5aSJohn Baldwin if (handle == NULL) 10978d791e5aSJohn Baldwin return (-2); 10988d791e5aSJohn Baldwin status = acpi_GetInteger(handle, "_PXM", &pxm); 10998d791e5aSJohn Baldwin if (ACPI_SUCCESS(status)) 11008d791e5aSJohn Baldwin return (acpi_map_pxm_to_vm_domainid(pxm)); 11018d791e5aSJohn Baldwin if (status == AE_NOT_FOUND) 11028d791e5aSJohn Baldwin return (-2); 11038a08b7d3SJohn Baldwin #endif 1104bcc51ef4SMark Johnston #endif 11058d791e5aSJohn Baldwin return (-1); 11068d791e5aSJohn Baldwin } 11078a08b7d3SJohn Baldwin 11088d791e5aSJohn Baldwin int 11098d791e5aSJohn Baldwin acpi_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize, 11108d791e5aSJohn Baldwin cpuset_t *cpuset) 11118d791e5aSJohn Baldwin { 11128d791e5aSJohn Baldwin int d, error; 11138d791e5aSJohn Baldwin 11148d791e5aSJohn Baldwin d = acpi_parse_pxm(child); 11158d791e5aSJohn Baldwin if (d < 0) 11168d791e5aSJohn Baldwin return (bus_generic_get_cpus(dev, child, op, setsize, cpuset)); 11178d791e5aSJohn Baldwin 11188d791e5aSJohn Baldwin switch (op) { 11198d791e5aSJohn Baldwin case LOCAL_CPUS: 11208d791e5aSJohn Baldwin if (setsize != sizeof(cpuset_t)) 11218d791e5aSJohn Baldwin return (EINVAL); 11228d791e5aSJohn Baldwin *cpuset = cpuset_domain[d]; 11238a08b7d3SJohn Baldwin return (0); 11248d791e5aSJohn Baldwin case INTR_CPUS: 11258d791e5aSJohn Baldwin error = bus_generic_get_cpus(dev, child, op, setsize, cpuset); 11268d791e5aSJohn Baldwin if (error != 0) 11278d791e5aSJohn Baldwin return (error); 11288d791e5aSJohn Baldwin if (setsize != sizeof(cpuset_t)) 11298d791e5aSJohn Baldwin return (EINVAL); 11308d791e5aSJohn Baldwin CPU_AND(cpuset, &cpuset_domain[d]); 11318d791e5aSJohn Baldwin return (0); 11328d791e5aSJohn Baldwin default: 11338d791e5aSJohn Baldwin return (bus_generic_get_cpus(dev, child, op, setsize, cpuset)); 11348d791e5aSJohn Baldwin } 11355d18c60aSAdrian Chadd } 11365d18c60aSAdrian Chadd 11375d18c60aSAdrian Chadd /* 11388d791e5aSJohn Baldwin * Fetch the NUMA domain for the given device 'dev'. 1139ffcf962dSAdrian Chadd * 1140ffcf962dSAdrian Chadd * If a device has a _PXM method, map that to a NUMA domain. 11418d791e5aSJohn Baldwin * Otherwise, pass the request up to the parent. 11428d791e5aSJohn Baldwin * If there's no matching domain or the domain cannot be 11438d791e5aSJohn Baldwin * determined, return ENOENT. 1144ffcf962dSAdrian Chadd */ 1145ffcf962dSAdrian Chadd int 1146ffcf962dSAdrian Chadd acpi_get_domain(device_t dev, device_t child, int *domain) 1147ffcf962dSAdrian Chadd { 11488d791e5aSJohn Baldwin int d; 1149ffcf962dSAdrian Chadd 11508d791e5aSJohn Baldwin d = acpi_parse_pxm(child); 11518d791e5aSJohn Baldwin if (d >= 0) { 11528d791e5aSJohn Baldwin *domain = d; 11538a08b7d3SJohn Baldwin return (0); 11548d791e5aSJohn Baldwin } 11558d791e5aSJohn Baldwin if (d == -1) 11568d791e5aSJohn Baldwin return (ENOENT); 11575d18c60aSAdrian Chadd 1158ffcf962dSAdrian Chadd /* No _PXM node; go up a level */ 1159ffcf962dSAdrian Chadd return (bus_generic_get_domain(dev, child, domain)); 1160ffcf962dSAdrian Chadd } 1161ffcf962dSAdrian Chadd 1162ffcf962dSAdrian Chadd /* 1163adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1164adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 1165adad4744SNate Lawson */ 1166adad4744SNate Lawson static int 1167adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 1168adad4744SNate Lawson { 1169adad4744SNate Lawson struct resource *res; 1170adad4744SNate Lawson struct resource_list *rl; 1171adad4744SNate Lawson struct resource_list_entry *rle; 1172adad4744SNate Lawson struct rman *rm; 1173da72d149SNate Lawson device_t *children; 1174da72d149SNate Lawson int child_count, i; 1175da72d149SNate Lawson 1176da72d149SNate Lawson /* 1177da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we 1178da72d149SNate Lawson * had multi-pass probe/attach. 1179da72d149SNate Lawson */ 1180da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0) 1181da72d149SNate Lawson return (ENXIO); 1182da72d149SNate Lawson for (i = 0; i < child_count; i++) { 11835efca36fSTakanori Watanabe if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0) 1184da72d149SNate Lawson device_probe_and_attach(children[i]); 1185da72d149SNate Lawson } 1186da72d149SNate Lawson free(children, M_TEMP); 1187adad4744SNate Lawson 1188adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1189be1bf4d2SPoul-Henning Kamp STAILQ_FOREACH(rle, rl, link) { 1190adad4744SNate Lawson if (rle->res != NULL) { 1191da1b038aSJustin Hibbits device_printf(dev, "duplicate resource for %jx\n", rle->start); 1192adad4744SNate Lawson continue; 1193adad4744SNate Lawson } 1194adad4744SNate Lawson 1195adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 1196adad4744SNate Lawson switch (rle->type) { 1197adad4744SNate Lawson case SYS_RES_IOPORT: 1198adad4744SNate Lawson rm = &acpi_rman_io; 1199adad4744SNate Lawson break; 1200adad4744SNate Lawson case SYS_RES_MEMORY: 1201adad4744SNate Lawson rm = &acpi_rman_mem; 1202adad4744SNate Lawson break; 1203adad4744SNate Lawson default: 1204adad4744SNate Lawson continue; 1205adad4744SNate Lawson } 1206adad4744SNate Lawson 1207adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 1208adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1209adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1210adad4744SNate Lawson if (res != NULL) { 1211adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1212adad4744SNate Lawson rle->res = res; 121344947d3aSJohn Baldwin } else if (bootverbose) 1214da1b038aSJustin Hibbits device_printf(dev, "reservation of %jx, %jx (%d) failed\n", 1215adad4744SNate Lawson rle->start, rle->count, rle->type); 1216adad4744SNate Lawson } 1217adad4744SNate Lawson return (0); 1218adad4744SNate Lawson } 1219adad4744SNate Lawson 1220ea233199SJohn Baldwin /* 1221ea233199SJohn Baldwin * Reserve declared resources for devices found during attach once system 1222ea233199SJohn Baldwin * resources have been allocated. 1223ea233199SJohn Baldwin */ 1224ea233199SJohn Baldwin static void 1225ea233199SJohn Baldwin acpi_reserve_resources(device_t dev) 1226ea233199SJohn Baldwin { 1227ea233199SJohn Baldwin struct resource_list_entry *rle; 1228ea233199SJohn Baldwin struct resource_list *rl; 1229ea233199SJohn Baldwin struct acpi_device *ad; 1230ea233199SJohn Baldwin struct acpi_softc *sc; 1231ea233199SJohn Baldwin device_t *children; 1232ea233199SJohn Baldwin int child_count, i; 1233ea233199SJohn Baldwin 1234ea233199SJohn Baldwin sc = device_get_softc(dev); 1235ea233199SJohn Baldwin if (device_get_children(dev, &children, &child_count) != 0) 1236ea233199SJohn Baldwin return; 1237ea233199SJohn Baldwin for (i = 0; i < child_count; i++) { 1238ea233199SJohn Baldwin ad = device_get_ivars(children[i]); 1239ea233199SJohn Baldwin rl = &ad->ad_rl; 1240ea233199SJohn Baldwin 1241ea233199SJohn Baldwin /* Don't reserve system resources. */ 12425efca36fSTakanori Watanabe if (ACPI_ID_PROBE(dev, children[i], sysres_ids, NULL) <= 0) 1243ea233199SJohn Baldwin continue; 1244ea233199SJohn Baldwin 1245ea233199SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 1246ea233199SJohn Baldwin /* 1247ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things 1248ea233199SJohn Baldwin * to get right otherwise (e.g. IRQs for psm, atkbd, and HPET 1249ea233199SJohn Baldwin * when using legacy routing). 1250ea233199SJohn Baldwin */ 1251ea233199SJohn Baldwin if (rle->type == SYS_RES_IRQ) 1252ea233199SJohn Baldwin continue; 1253ea233199SJohn Baldwin 1254ea233199SJohn Baldwin /* 12550d81cf12SJohn Baldwin * Don't reserve the resource if it is already allocated. 12560d81cf12SJohn Baldwin * The acpi_ec(4) driver can allocate its resources early 12570d81cf12SJohn Baldwin * if ECDT is present. 12580d81cf12SJohn Baldwin */ 12590d81cf12SJohn Baldwin if (rle->res != NULL) 12600d81cf12SJohn Baldwin continue; 12610d81cf12SJohn Baldwin 12620d81cf12SJohn Baldwin /* 1263ea233199SJohn Baldwin * Try to reserve the resource from our parent. If this 1264ea233199SJohn Baldwin * fails because the resource is a system resource, just 1265ea233199SJohn Baldwin * let it be. The resource range is already reserved so 1266ea233199SJohn Baldwin * that other devices will not use it. If the driver 1267ea233199SJohn Baldwin * needs to allocate the resource, then 1268ea233199SJohn Baldwin * acpi_alloc_resource() will sub-alloc from the system 1269ea233199SJohn Baldwin * resource. 1270ea233199SJohn Baldwin */ 1271ea233199SJohn Baldwin resource_list_reserve(rl, dev, children[i], rle->type, &rle->rid, 1272ea233199SJohn Baldwin rle->start, rle->end, rle->count, 0); 1273ea233199SJohn Baldwin } 1274ea233199SJohn Baldwin } 1275ea233199SJohn Baldwin free(children, M_TEMP); 1276ea233199SJohn Baldwin sc->acpi_resources_reserved = 1; 1277ea233199SJohn Baldwin } 1278ea233199SJohn Baldwin 1279ea233199SJohn Baldwin static int 1280ea233199SJohn Baldwin acpi_set_resource(device_t dev, device_t child, int type, int rid, 12812dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t count) 1282ea233199SJohn Baldwin { 1283ea233199SJohn Baldwin struct acpi_softc *sc = device_get_softc(dev); 1284ea233199SJohn Baldwin struct acpi_device *ad = device_get_ivars(child); 1285ea233199SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 12867e3343bfSJohn Baldwin ACPI_DEVICE_INFO *devinfo; 12872dd1bdf1SJustin Hibbits rman_res_t end; 1288185c34f7SJayachandran C. int allow; 1289ea233199SJohn Baldwin 1290ea233199SJohn Baldwin /* Ignore IRQ resources for PCI link devices. */ 12915efca36fSTakanori Watanabe if (type == SYS_RES_IRQ && 12925efca36fSTakanori Watanabe ACPI_ID_PROBE(dev, child, pcilink_ids, NULL) <= 0) 1293ea233199SJohn Baldwin return (0); 1294ea233199SJohn Baldwin 12957e3343bfSJohn Baldwin /* 1296d2dc06caSJohn Baldwin * Ignore most resources for PCI root bridges. Some BIOSes 12977e3343bfSJohn Baldwin * incorrectly enumerate the memory ranges they decode as plain 1298d2dc06caSJohn Baldwin * memory resources instead of as ResourceProducer ranges. Other 1299d2dc06caSJohn Baldwin * BIOSes incorrectly list system resource entries for I/O ranges 1300d2dc06caSJohn Baldwin * under the PCI bridge. Do allow the one known-correct case on 1301d2dc06caSJohn Baldwin * x86 of a PCI bridge claiming the I/O ports used for PCI config 1302d2dc06caSJohn Baldwin * access. 13037e3343bfSJohn Baldwin */ 1304d2dc06caSJohn Baldwin if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 13057e3343bfSJohn Baldwin if (ACPI_SUCCESS(AcpiGetObjectInfo(ad->ad_handle, &devinfo))) { 13067e3343bfSJohn Baldwin if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) { 1307185c34f7SJayachandran C. #if defined(__i386__) || defined(__amd64__) 1308185c34f7SJayachandran C. allow = (type == SYS_RES_IOPORT && start == CONF1_ADDR_PORT); 1309185c34f7SJayachandran C. #else 1310185c34f7SJayachandran C. allow = 0; 1311185c34f7SJayachandran C. #endif 1312185c34f7SJayachandran C. if (!allow) { 13137e3343bfSJohn Baldwin AcpiOsFree(devinfo); 13147e3343bfSJohn Baldwin return (0); 13157e3343bfSJohn Baldwin } 1316d2dc06caSJohn Baldwin } 13177e3343bfSJohn Baldwin AcpiOsFree(devinfo); 13187e3343bfSJohn Baldwin } 13197e3343bfSJohn Baldwin } 13207e3343bfSJohn Baldwin 1321d4d6ad3fSJayachandran C. #ifdef INTRNG 1322d4d6ad3fSJayachandran C. /* map with default for now */ 1323d4d6ad3fSJayachandran C. if (type == SYS_RES_IRQ) 1324d4d6ad3fSJayachandran C. start = (rman_res_t)acpi_map_intr(child, (u_int)start, 1325d4d6ad3fSJayachandran C. acpi_get_handle(child)); 1326d4d6ad3fSJayachandran C. #endif 1327d4d6ad3fSJayachandran C. 1328ea233199SJohn Baldwin /* If the resource is already allocated, fail. */ 1329ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) 1330ea233199SJohn Baldwin return (EBUSY); 1331ea233199SJohn Baldwin 1332ea233199SJohn Baldwin /* If the resource is already reserved, release it. */ 1333ea233199SJohn Baldwin if (resource_list_reserved(rl, type, rid)) 1334ea233199SJohn Baldwin resource_list_unreserve(rl, dev, child, type, rid); 1335ea233199SJohn Baldwin 1336ea233199SJohn Baldwin /* Add the resource. */ 1337ea233199SJohn Baldwin end = (start + count - 1); 1338ea233199SJohn Baldwin resource_list_add(rl, type, rid, start, end, count); 1339ea233199SJohn Baldwin 1340ea233199SJohn Baldwin /* Don't reserve resources until the system resources are allocated. */ 1341ea233199SJohn Baldwin if (!sc->acpi_resources_reserved) 1342ea233199SJohn Baldwin return (0); 1343ea233199SJohn Baldwin 1344ea233199SJohn Baldwin /* Don't reserve system resources. */ 13455efca36fSTakanori Watanabe if (ACPI_ID_PROBE(dev, child, sysres_ids, NULL) <= 0) 1346ea233199SJohn Baldwin return (0); 1347ea233199SJohn Baldwin 1348ea233199SJohn Baldwin /* 1349ea233199SJohn Baldwin * Don't reserve IRQ resources. There are many sticky things to 1350ea233199SJohn Baldwin * get right otherwise (e.g. IRQs for psm, atkbd, and HPET when 1351ea233199SJohn Baldwin * using legacy routing). 1352ea233199SJohn Baldwin */ 1353ea233199SJohn Baldwin if (type == SYS_RES_IRQ) 1354ea233199SJohn Baldwin return (0); 1355ea233199SJohn Baldwin 1356ea233199SJohn Baldwin /* 13571fe39413SJohn Baldwin * Don't reserve resources for CPU devices. Some of these 13581fe39413SJohn Baldwin * resources need to be allocated as shareable, but reservations 13591fe39413SJohn Baldwin * are always non-shareable. 13601fe39413SJohn Baldwin */ 13611fe39413SJohn Baldwin if (device_get_devclass(child) == devclass_find("cpu")) 13621fe39413SJohn Baldwin return (0); 13631fe39413SJohn Baldwin 13641fe39413SJohn Baldwin /* 1365ea233199SJohn Baldwin * Reserve the resource. 1366ea233199SJohn Baldwin * 1367ea233199SJohn Baldwin * XXX: Ignores failure for now. Failure here is probably a 1368ea233199SJohn Baldwin * BIOS/firmware bug? 1369ea233199SJohn Baldwin */ 1370ea233199SJohn Baldwin resource_list_reserve(rl, dev, child, type, &rid, start, end, count, 0); 1371ea233199SJohn Baldwin return (0); 1372ea233199SJohn Baldwin } 1373ea233199SJohn Baldwin 137415e32d5dSMike Smith static struct resource * 137515e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 13762dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 137715e32d5dSMike Smith { 1378224c3776SAndrew Turner #ifndef INTRNG 137995957f62SJohn Baldwin ACPI_RESOURCE ares; 1380224c3776SAndrew Turner #endif 1381ea233199SJohn Baldwin struct acpi_device *ad; 138291233413SNate Lawson struct resource_list_entry *rle; 1383ea233199SJohn Baldwin struct resource_list *rl; 138491233413SNate Lawson struct resource *res; 13857915adb5SJustin Hibbits int isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 138615e2f34fSNate Lawson 138791233413SNate Lawson /* 1388ea233199SJohn Baldwin * First attempt at allocating the resource. For direct children, 1389ea233199SJohn Baldwin * use resource_list_alloc() to handle reserved resources. For 1390f6133e71SJohn Baldwin * other devices, pass the request up to our parent. 139191233413SNate Lawson */ 1392ea233199SJohn Baldwin if (bus == device_get_parent(child)) { 1393ea233199SJohn Baldwin ad = device_get_ivars(child); 1394ea233199SJohn Baldwin rl = &ad->ad_rl; 139591233413SNate Lawson 1396397c30a8SJohn Baldwin /* 1397ea233199SJohn Baldwin * Simulate the behavior of the ISA bus for direct children 1398ea233199SJohn Baldwin * devices. That is, if a non-default range is specified for 1399ea233199SJohn Baldwin * a resource that doesn't exist, use bus_set_resource() to 1400ea233199SJohn Baldwin * add the resource before allocating it. Note that these 1401ea233199SJohn Baldwin * resources will not be reserved. 1402397c30a8SJohn Baldwin */ 1403ea233199SJohn Baldwin if (!isdefault && resource_list_find(rl, type, *rid) == NULL) 1404ea233199SJohn Baldwin resource_list_add(rl, type, *rid, start, end, count); 1405ea233199SJohn Baldwin res = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 1406ea233199SJohn Baldwin flags); 1407224c3776SAndrew Turner #ifndef INTRNG 1408ea233199SJohn Baldwin if (res != NULL && type == SYS_RES_IRQ) { 140995957f62SJohn Baldwin /* 141095957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 141195957f62SJohn Baldwin * configure the interrupt associated with a device when we 141295957f62SJohn Baldwin * parse the resources but have to defer it until a driver 141395957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 141495957f62SJohn Baldwin * 141595957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 141695957f62SJohn Baldwin */ 141795957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 141895957f62SJohn Baldwin acpi_config_intr(child, &ares); 141995957f62SJohn Baldwin } 1420224c3776SAndrew Turner #endif 142115e2f34fSNate Lawson 1422ea233199SJohn Baldwin /* 1423ea233199SJohn Baldwin * If this is an allocation of the "default" range for a given 1424ea233199SJohn Baldwin * RID, fetch the exact bounds for this resource from the 1425ea233199SJohn Baldwin * resource list entry to try to allocate the range from the 1426ea233199SJohn Baldwin * system resource regions. 1427ea233199SJohn Baldwin */ 1428ea233199SJohn Baldwin if (res == NULL && isdefault) { 1429ea233199SJohn Baldwin rle = resource_list_find(rl, type, *rid); 1430ea233199SJohn Baldwin if (rle != NULL) { 1431ea233199SJohn Baldwin start = rle->start; 1432ea233199SJohn Baldwin end = rle->end; 1433ea233199SJohn Baldwin count = rle->count; 1434ea233199SJohn Baldwin } 1435ea233199SJohn Baldwin } 1436ea233199SJohn Baldwin } else 1437ea233199SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 1438ea233199SJohn Baldwin start, end, count, flags); 1439ea233199SJohn Baldwin 1440ea233199SJohn Baldwin /* 1441ea233199SJohn Baldwin * If the first attempt failed and this is an allocation of a 1442ea233199SJohn Baldwin * specific range, try to satisfy the request via a suballocation 14435d0d779bSJohn Baldwin * from our system resource regions. 1444ea233199SJohn Baldwin */ 14455d0d779bSJohn Baldwin if (res == NULL && start + count - 1 == end) 14465d0d779bSJohn Baldwin res = acpi_alloc_sysres(child, type, rid, start, end, count, flags); 14475d0d779bSJohn Baldwin return (res); 14485d0d779bSJohn Baldwin } 14495d0d779bSJohn Baldwin 14505d0d779bSJohn Baldwin /* 14515d0d779bSJohn Baldwin * Attempt to allocate a specific resource range from the system 14525d0d779bSJohn Baldwin * resource ranges. Note that we only handle memory and I/O port 14535d0d779bSJohn Baldwin * system resources. 14545d0d779bSJohn Baldwin */ 14555d0d779bSJohn Baldwin struct resource * 14562dd1bdf1SJustin Hibbits acpi_alloc_sysres(device_t child, int type, int *rid, rman_res_t start, 14572dd1bdf1SJustin Hibbits rman_res_t end, rman_res_t count, u_int flags) 14585d0d779bSJohn Baldwin { 14595d0d779bSJohn Baldwin struct rman *rm; 14605d0d779bSJohn Baldwin struct resource *res; 14615d0d779bSJohn Baldwin 1462ea233199SJohn Baldwin switch (type) { 1463ea233199SJohn Baldwin case SYS_RES_IOPORT: 1464ea233199SJohn Baldwin rm = &acpi_rman_io; 1465ea233199SJohn Baldwin break; 1466ea233199SJohn Baldwin case SYS_RES_MEMORY: 1467ea233199SJohn Baldwin rm = &acpi_rman_mem; 1468ea233199SJohn Baldwin break; 1469ea233199SJohn Baldwin default: 1470ea233199SJohn Baldwin return (NULL); 1471ea233199SJohn Baldwin } 1472ea233199SJohn Baldwin 14735d0d779bSJohn Baldwin KASSERT(start + count - 1 == end, ("wildcard resource range")); 1474ea233199SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1475ea233199SJohn Baldwin child); 1476ea233199SJohn Baldwin if (res == NULL) 1477ea233199SJohn Baldwin return (NULL); 1478ea233199SJohn Baldwin 1479ea233199SJohn Baldwin rman_set_rid(res, *rid); 1480ea233199SJohn Baldwin 1481ea233199SJohn Baldwin /* If requested, activate the resource using the parent's method. */ 1482ea233199SJohn Baldwin if (flags & RF_ACTIVE) 1483ea233199SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 1484ea233199SJohn Baldwin rman_release_resource(res); 1485ea233199SJohn Baldwin return (NULL); 1486ea233199SJohn Baldwin } 1487ea233199SJohn Baldwin 148891233413SNate Lawson return (res); 148915e32d5dSMike Smith } 149015e32d5dSMike Smith 149115e32d5dSMike Smith static int 1492049dc0d1SJohn Baldwin acpi_is_resource_managed(int type, struct resource *r) 149315e32d5dSMike Smith { 149415e32d5dSMike Smith 1495397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1496397c30a8SJohn Baldwin switch (type) { 1497397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1498049dc0d1SJohn Baldwin return (rman_is_region_manager(r, &acpi_rman_io)); 1499397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1500049dc0d1SJohn Baldwin return (rman_is_region_manager(r, &acpi_rman_mem)); 1501397c30a8SJohn Baldwin } 1502049dc0d1SJohn Baldwin return (0); 1503049dc0d1SJohn Baldwin } 1504049dc0d1SJohn Baldwin 1505049dc0d1SJohn Baldwin static int 1506049dc0d1SJohn Baldwin acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 15072dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 1508049dc0d1SJohn Baldwin { 1509049dc0d1SJohn Baldwin 1510049dc0d1SJohn Baldwin if (acpi_is_resource_managed(type, r)) 1511049dc0d1SJohn Baldwin return (rman_adjust_resource(r, start, end)); 1512049dc0d1SJohn Baldwin return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 1513049dc0d1SJohn Baldwin } 1514049dc0d1SJohn Baldwin 1515049dc0d1SJohn Baldwin static int 1516049dc0d1SJohn Baldwin acpi_release_resource(device_t bus, device_t child, int type, int rid, 1517049dc0d1SJohn Baldwin struct resource *r) 1518049dc0d1SJohn Baldwin { 1519049dc0d1SJohn Baldwin int ret; 1520397c30a8SJohn Baldwin 152191233413SNate Lawson /* 1522397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers, 1523ea233199SJohn Baldwin * deactivate it and release it to the local pool. 152491233413SNate Lawson */ 1525049dc0d1SJohn Baldwin if (acpi_is_resource_managed(type, r)) { 152691233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 152791233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 152891233413SNate Lawson if (ret != 0) 152991233413SNate Lawson return (ret); 153015e32d5dSMike Smith } 1531ea233199SJohn Baldwin return (rman_release_resource(r)); 1532ea233199SJohn Baldwin } 1533ea233199SJohn Baldwin 1534ea233199SJohn Baldwin return (bus_generic_rl_release_resource(bus, child, type, rid, r)); 1535ea233199SJohn Baldwin } 153615e32d5dSMike Smith 15378b28b622SNate Lawson static void 15388b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid) 15398b28b622SNate Lawson { 15408b28b622SNate Lawson struct resource_list *rl; 15418b28b622SNate Lawson 15428b28b622SNate Lawson rl = acpi_get_rlist(bus, child); 1543ea233199SJohn Baldwin if (resource_list_busy(rl, type, rid)) { 1544ea233199SJohn Baldwin device_printf(bus, "delete_resource: Resource still owned by child" 1545ea233199SJohn Baldwin " (type=%d, rid=%d)\n", type, rid); 1546ea233199SJohn Baldwin return; 1547ea233199SJohn Baldwin } 1548ea233199SJohn Baldwin resource_list_unreserve(rl, bus, child, type, rid); 15498b28b622SNate Lawson resource_list_delete(rl, type, rid); 15508b28b622SNate Lawson } 15518b28b622SNate Lawson 1552dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 1553e1c4bf3fSNate Lawson int 1554e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1555907b6777SNate Lawson struct resource **res, u_int flags) 1556dc750869SNate Lawson { 1557e1c4bf3fSNate Lawson int error, res_type; 1558dc750869SNate Lawson 1559e1c4bf3fSNate Lawson error = ENOMEM; 15608f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1561e1c4bf3fSNate Lawson return (EINVAL); 1562dc750869SNate Lawson 15638f118e25SNate Lawson /* We only support memory and IO spaces. */ 15642be4e471SJung-uk Kim switch (gas->SpaceId) { 1565dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1566e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY; 1567dc750869SNate Lawson break; 1568dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 1569e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT; 1570dc750869SNate Lawson break; 1571dc750869SNate Lawson default: 1572e1c4bf3fSNate Lawson return (EOPNOTSUPP); 1573dc750869SNate Lawson } 1574dc750869SNate Lawson 15752fe912dfSNate Lawson /* 1576f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means 1577f45fc848SNate Lawson * it is a bit field and just allocate a byte. 15782fe912dfSNate Lawson */ 15792be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8) 15802be4e471SJung-uk Kim gas->BitWidth = 8; 15812fe912dfSNate Lawson 15828f118e25SNate Lawson /* Validate the address after we're sure we support the space. */ 15832be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0) 15848f118e25SNate Lawson return (EINVAL); 15858f118e25SNate Lawson 1586e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address, 15872be4e471SJung-uk Kim gas->BitWidth / 8); 1588907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1589e1c4bf3fSNate Lawson if (*res != NULL) { 1590e1c4bf3fSNate Lawson *type = res_type; 1591e1c4bf3fSNate Lawson error = 0; 1592ca2c69c8SNate Lawson } else 1593ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid); 1594ca2c69c8SNate Lawson 1595e1c4bf3fSNate Lawson return (error); 1596dc750869SNate Lawson } 1597dc750869SNate Lawson 1598c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 15991e4925e8SNate Lawson static uint32_t 160032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 1601bc0f2195SMike Smith { 16021e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1603bc0f2195SMike Smith ACPI_HANDLE h; 160492488a57SJung-uk Kim uint32_t pnpid; 160532d18aa5SMike Smith 1606b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 160732d18aa5SMike Smith 16086fca9360SNate Lawson /* Fetch and validate the HID. */ 160992488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 161092488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 161192488a57SJung-uk Kim return_VALUE (0); 161232d18aa5SMike Smith 161392488a57SJung-uk Kim pnpid = (devinfo->Valid & ACPI_VALID_HID) != 0 && 161492488a57SJung-uk Kim devinfo->HardwareId.Length >= ACPI_EISAID_STRING_SIZE ? 161592488a57SJung-uk Kim PNP_EISAID(devinfo->HardwareId.String) : 0; 161692488a57SJung-uk Kim AcpiOsFree(devinfo); 1617be2b1797SNate Lawson 161832d18aa5SMike Smith return_VALUE (pnpid); 161932d18aa5SMike Smith } 162032d18aa5SMike Smith 16211e4925e8SNate Lawson static int 16221e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1623b2566207STakanori Watanabe { 16241e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 16258ef1a331SJung-uk Kim ACPI_PNP_DEVICE_ID *ids; 1626b2566207STakanori Watanabe ACPI_HANDLE h; 16271e4925e8SNate Lawson uint32_t *pnpid; 162892488a57SJung-uk Kim int i, valid; 1629b2566207STakanori Watanabe 1630b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1631b2566207STakanori Watanabe 16321e4925e8SNate Lawson pnpid = cids; 1633bd189fe7SNate Lawson 16341e4925e8SNate Lawson /* Fetch and validate the CID */ 163592488a57SJung-uk Kim if ((h = acpi_get_handle(dev)) == NULL || 163692488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 163792488a57SJung-uk Kim return_VALUE (0); 1638b2566207STakanori Watanabe 163992488a57SJung-uk Kim if ((devinfo->Valid & ACPI_VALID_CID) == 0) { 164092488a57SJung-uk Kim AcpiOsFree(devinfo); 164192488a57SJung-uk Kim return_VALUE (0); 1642b2566207STakanori Watanabe } 1643b2566207STakanori Watanabe 164492488a57SJung-uk Kim if (devinfo->CompatibleIdList.Count < count) 164592488a57SJung-uk Kim count = devinfo->CompatibleIdList.Count; 164692488a57SJung-uk Kim ids = devinfo->CompatibleIdList.Ids; 164792488a57SJung-uk Kim for (i = 0, valid = 0; i < count; i++) 164892488a57SJung-uk Kim if (ids[i].Length >= ACPI_EISAID_STRING_SIZE && 164992488a57SJung-uk Kim strncmp(ids[i].String, "PNP", 3) == 0) { 165092488a57SJung-uk Kim *pnpid++ = PNP_EISAID(ids[i].String); 165192488a57SJung-uk Kim valid++; 165292488a57SJung-uk Kim } 165392488a57SJung-uk Kim AcpiOsFree(devinfo); 165492488a57SJung-uk Kim 16551e4925e8SNate Lawson return_VALUE (valid); 16561e4925e8SNate Lawson } 1657b2566207STakanori Watanabe 16585efca36fSTakanori Watanabe static int 16595efca36fSTakanori Watanabe acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match) 1660ce619b2eSNate Lawson { 1661ce619b2eSNate Lawson ACPI_HANDLE h; 166292488a57SJung-uk Kim ACPI_OBJECT_TYPE t; 16635efca36fSTakanori Watanabe int rv; 1664ce619b2eSNate Lawson int i; 1665ce619b2eSNate Lawson 1666ce619b2eSNate Lawson h = acpi_get_handle(dev); 166792488a57SJung-uk Kim if (ids == NULL || h == NULL) 16685efca36fSTakanori Watanabe return (ENXIO); 166992488a57SJung-uk Kim t = acpi_get_type(dev); 167092488a57SJung-uk Kim if (t != ACPI_TYPE_DEVICE && t != ACPI_TYPE_PROCESSOR) 16715efca36fSTakanori Watanabe return (ENXIO); 1672ce619b2eSNate Lawson 1673ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1674ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 16755efca36fSTakanori Watanabe rv = acpi_MatchHid(h, ids[i]); 16765efca36fSTakanori Watanabe if (rv == ACPI_MATCHHID_NOMATCH) 16775efca36fSTakanori Watanabe continue; 16785efca36fSTakanori Watanabe 16795efca36fSTakanori Watanabe if (match != NULL) { 16805efca36fSTakanori Watanabe *match = ids[i]; 1681ce619b2eSNate Lawson } 16825efca36fSTakanori Watanabe return ((rv == ACPI_MATCHHID_HID)? 16835efca36fSTakanori Watanabe BUS_PROBE_DEFAULT : BUS_PROBE_LOW_PRIORITY); 16845efca36fSTakanori Watanabe } 16855efca36fSTakanori Watanabe return (ENXIO); 1686ce619b2eSNate Lawson } 1687ce619b2eSNate Lawson 1688ce619b2eSNate Lawson static ACPI_STATUS 1689ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1690ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1691ce619b2eSNate Lawson { 1692ce619b2eSNate Lawson ACPI_HANDLE h; 1693ce619b2eSNate Lawson 1694df8d2a32SNate Lawson if (dev == NULL) 1695df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1696df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1697ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1698ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1699ce619b2eSNate Lawson } 1700ce619b2eSNate Lawson 170162508c53SJohn Baldwin int 170210ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 170310ce62b9SNate Lawson { 170410ce62b9SNate Lawson struct acpi_softc *sc; 170510ce62b9SNate Lawson ACPI_HANDLE handle; 170610ce62b9SNate Lawson ACPI_STATUS status; 170710ce62b9SNate Lawson char sxd[8]; 170810ce62b9SNate Lawson 170910ce62b9SNate Lawson handle = acpi_get_handle(dev); 171010ce62b9SNate Lawson 171110ce62b9SNate Lawson /* 171210ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down. 171310ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when 171410ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may 171510ce62b9SNate Lawson * need special handling in their drivers. 171610ce62b9SNate Lawson */ 1717a7a3177fSJung-uk Kim if (dstate == NULL || handle == NULL || 171810ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") || 171910ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") || 172010ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") || 172110ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") || 172210ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511")) 172310ce62b9SNate Lawson return (ENXIO); 172410ce62b9SNate Lawson 172510ce62b9SNate Lawson /* 1726a7a3177fSJung-uk Kim * Override next state with the value from _SxD, if present. 1727a7a3177fSJung-uk Kim * Note illegal _S0D is evaluated because some systems expect this. 172810ce62b9SNate Lawson */ 1729a7a3177fSJung-uk Kim sc = device_get_softc(bus); 173010ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 173110ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate); 1732a7a3177fSJung-uk Kim if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1733a7a3177fSJung-uk Kim device_printf(dev, "failed to get %s on %s: %s\n", sxd, 1734a7a3177fSJung-uk Kim acpi_name(handle), AcpiFormatException(status)); 1735a7a3177fSJung-uk Kim return (ENXIO); 173610ce62b9SNate Lawson } 173710ce62b9SNate Lawson 1738a7a3177fSJung-uk Kim return (0); 173910ce62b9SNate Lawson } 174010ce62b9SNate Lawson 1741df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1742df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1743df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1744df8d2a32SNate Lawson void *arg; 1745df8d2a32SNate Lawson ACPI_HANDLE parent; 1746df8d2a32SNate Lawson }; 1747df8d2a32SNate Lawson 1748ce619b2eSNate Lawson static ACPI_STATUS 1749df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1750df8d2a32SNate Lawson { 1751df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1752df8d2a32SNate Lawson device_t dev, old_dev; 1753df8d2a32SNate Lawson ACPI_STATUS status; 1754df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1755df8d2a32SNate Lawson 1756df8d2a32SNate Lawson /* 1757df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1758df8d2a32SNate Lawson * the parent where the scan began. 1759df8d2a32SNate Lawson */ 1760df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1761df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1762df8d2a32SNate Lawson return (AE_OK); 1763df8d2a32SNate Lawson 1764df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1765df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1766df8d2a32SNate Lawson return (AE_OK); 1767df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1768df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1769df8d2a32SNate Lawson return (AE_OK); 1770df8d2a32SNate Lawson 1771df8d2a32SNate Lawson /* 1772df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1773df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1774df8d2a32SNate Lawson */ 1775df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1776df8d2a32SNate Lawson dev = old_dev; 1777df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1778df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1779df8d2a32SNate Lawson return (status); 1780df8d2a32SNate Lawson 1781df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1782df8d2a32SNate Lawson if (old_dev != NULL) { 1783df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1784df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1785df8d2a32SNate Lawson } 1786df8d2a32SNate Lawson 1787df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1788df8d2a32SNate Lawson if (dev != NULL) 1789df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1790df8d2a32SNate Lawson 1791df8d2a32SNate Lawson return (AE_OK); 1792df8d2a32SNate Lawson } 1793df8d2a32SNate Lawson 1794df8d2a32SNate Lawson static ACPI_STATUS 1795df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1796df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1797ce619b2eSNate Lawson { 1798ce619b2eSNate Lawson ACPI_HANDLE h; 1799df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1800ce619b2eSNate Lawson 1801df8d2a32SNate Lawson if (acpi_disabled("children")) 1802df8d2a32SNate Lawson return (AE_OK); 1803df8d2a32SNate Lawson 1804df8d2a32SNate Lawson if (dev == NULL) 1805df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1806df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1807ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1808df8d2a32SNate Lawson ctx.user_fn = user_fn; 1809df8d2a32SNate Lawson ctx.arg = arg; 1810df8d2a32SNate Lawson ctx.parent = h; 1811df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 18122272d050SJung-uk Kim acpi_device_scan_cb, NULL, &ctx, NULL)); 1813ce619b2eSNate Lawson } 1814ce619b2eSNate Lawson 181510ce62b9SNate Lawson /* 181610ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting 181710ce62b9SNate Lawson * device power states since it's close enough to ACPI. 181810ce62b9SNate Lawson */ 181910ce62b9SNate Lawson static int 1820a7a3177fSJung-uk Kim acpi_set_powerstate(device_t child, int state) 182110ce62b9SNate Lawson { 182210ce62b9SNate Lawson ACPI_HANDLE h; 182310ce62b9SNate Lawson ACPI_STATUS status; 182410ce62b9SNate Lawson 182510ce62b9SNate Lawson h = acpi_get_handle(child); 1826a0e73a12SJung-uk Kim if (state < ACPI_STATE_D0 || state > ACPI_D_STATES_MAX) 182710ce62b9SNate Lawson return (EINVAL); 182810ce62b9SNate Lawson if (h == NULL) 182910ce62b9SNate Lawson return (0); 183010ce62b9SNate Lawson 183110ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */ 183210ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state); 1833a7a3177fSJung-uk Kim if (ACPI_SUCCESS(status)) { 1834a7a3177fSJung-uk Kim if (bootverbose) 1835a7a3177fSJung-uk Kim device_printf(child, "set ACPI power state D%d on %s\n", 1836a7a3177fSJung-uk Kim state, acpi_name(h)); 1837a7a3177fSJung-uk Kim } else if (status != AE_NOT_FOUND) 1838a7a3177fSJung-uk Kim device_printf(child, 1839a7a3177fSJung-uk Kim "failed to set ACPI power state D%d on %s: %s\n", state, 1840a7a3177fSJung-uk Kim acpi_name(h), AcpiFormatException(status)); 184110ce62b9SNate Lawson 1842a7a3177fSJung-uk Kim return (0); 184310ce62b9SNate Lawson } 184410ce62b9SNate Lawson 184532d18aa5SMike Smith static int 184632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 184732d18aa5SMike Smith { 18481e4925e8SNate Lawson int result, cid_count, i; 18491e4925e8SNate Lawson uint32_t lid, cids[8]; 1850bc0f2195SMike Smith 1851b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1852bc0f2195SMike Smith 1853bc0f2195SMike Smith /* 1854bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1855bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1856bc0f2195SMike Smith * that to happen, so don't ever return it. 1857bc0f2195SMike Smith */ 1858bc0f2195SMike Smith result = ENXIO; 1859bc0f2195SMike Smith 1860be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1861b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 18621e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1863bc0f2195SMike Smith while (ids && ids->ip_id) { 18641e4925e8SNate Lawson if (lid == ids->ip_id) { 1865bc0f2195SMike Smith result = 0; 1866bc0f2195SMike Smith goto out; 1867bc0f2195SMike Smith } 18681e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 18691e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 18701e4925e8SNate Lawson result = 0; 18711e4925e8SNate Lawson goto out; 18721e4925e8SNate Lawson } 18731e4925e8SNate Lawson } 1874bc0f2195SMike Smith ids++; 1875bc0f2195SMike Smith } 1876be2b1797SNate Lawson 1877bc0f2195SMike Smith out: 18789e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 18799e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 18809e0dd54fSNate Lawson 1881bc0f2195SMike Smith return_VALUE (result); 1882bc0f2195SMike Smith } 1883bc0f2195SMike Smith 1884d320e05cSJohn Baldwin /* 1885d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for 1886d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory 1887d320e05cSJohn Baldwin * map. 1888185c34f7SJayachandran C. * 1889185c34f7SJayachandran C. * On non-x86 architectures (arm64 for now), this will be done from the 1890185c34f7SJayachandran C. * PCI host bridge driver. 1891d320e05cSJohn Baldwin */ 1892d320e05cSJohn Baldwin static void 1893d320e05cSJohn Baldwin acpi_enable_pcie(void) 1894d320e05cSJohn Baldwin { 1895185c34f7SJayachandran C. #if defined(__i386__) || defined(__amd64__) 1896d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr; 1897d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end; 1898d320e05cSJohn Baldwin ACPI_STATUS status; 1899d320e05cSJohn Baldwin 1900d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1901d320e05cSJohn Baldwin if (ACPI_FAILURE(status)) 1902d320e05cSJohn Baldwin return; 1903d320e05cSJohn Baldwin 1904d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1905d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1906d320e05cSJohn Baldwin while (alloc < end) { 1907d320e05cSJohn Baldwin if (alloc->PciSegment == 0) { 1908d320e05cSJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1909d320e05cSJohn Baldwin alloc->EndBusNumber); 1910d320e05cSJohn Baldwin return; 1911d320e05cSJohn Baldwin } 1912d320e05cSJohn Baldwin alloc++; 1913d320e05cSJohn Baldwin } 1914d320e05cSJohn Baldwin #endif 1915185c34f7SJayachandran C. } 1916d320e05cSJohn Baldwin 1917bc0f2195SMike Smith /* 191833332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices. 191915e32d5dSMike Smith * 192033332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 192133332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 192233332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link 192333332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the 192433332dc2SNate Lawson * type of namespace nodes, so this should be ok. 192515e32d5dSMike Smith */ 192615e32d5dSMike Smith static void 192715e32d5dSMike Smith acpi_probe_children(device_t bus) 192815e32d5dSMike Smith { 192915e32d5dSMike Smith 1930b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19310ae55423SMike Smith 193215e32d5dSMike Smith /* 193315e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1934edc13633SNate Lawson * we find. We also probe/attach any early devices. 193515e32d5dSMike Smith * 193615e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1937be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1938be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1939be2b1797SNate Lawson * devices as they appear, which might be smarter.) 194015e32d5dSMike Smith */ 19414c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 194233332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 19432272d050SJung-uk Kim NULL, bus, NULL); 194415e32d5dSMike Smith 1945adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1946adad4744SNate Lawson acpi_sysres_alloc(bus); 1947adad4744SNate Lawson 1948ea233199SJohn Baldwin /* Reserve resources already allocated to children. */ 1949ea233199SJohn Baldwin acpi_reserve_resources(bus); 1950ea233199SJohn Baldwin 1951edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1952edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1953edc13633SNate Lawson bus_generic_probe(bus); 1954edc13633SNate Lawson 19555e66b8e2SJohn Baldwin /* Probe/attach all children, created statically and from the namespace. */ 19560430ba9eSAndriy Gapon ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "acpi bus_generic_attach\n")); 195715e32d5dSMike Smith bus_generic_attach(bus); 19580ae55423SMike Smith 195988a79fc0SNate Lawson /* Attach wake sysctls. */ 19605c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 196188a79fc0SNate Lawson 1962bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 19630ae55423SMike Smith return_VOID; 196415e32d5dSMike Smith } 196515e32d5dSMike Smith 1966b82ca9a9SNate Lawson /* 1967d227204dSJohn Baldwin * Determine the probe order for a given device. 1968b82ca9a9SNate Lawson */ 1969d227204dSJohn Baldwin static void 1970b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 197191233413SNate Lawson { 1972463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type; 197391233413SNate Lawson 1974b82ca9a9SNate Lawson /* 1975404b0d10SJung-uk Kim * 0. CPUs 1976404b0d10SJung-uk Kim * 1. I/O port and memory system resource holders 1977404b0d10SJung-uk Kim * 2. Clocks and timers (to handle early accesses) 1978b1f9b996SAndriy Gapon * 3. Embedded controllers (to handle early accesses) 1979b1f9b996SAndriy Gapon * 4. PCI Link Devices 1980b82ca9a9SNate Lawson */ 1981463e0f91SJohn Baldwin AcpiGetType(handle, &type); 1982aa835160SAndriy Gapon if (type == ACPI_TYPE_PROCESSOR) 1983404b0d10SJung-uk Kim *order = 0; 1984404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0C01") || 1985404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0C02")) 198691233413SNate Lawson *order = 1; 1987404b0d10SJung-uk Kim else if (acpi_MatchHid(handle, "PNP0100") || 1988404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0103") || 1989404b0d10SJung-uk Kim acpi_MatchHid(handle, "PNP0B00")) 199091233413SNate Lawson *order = 2; 1991aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C09")) 1992b460c6f8SJohn Baldwin *order = 3; 1993aa835160SAndriy Gapon else if (acpi_MatchHid(handle, "PNP0C0F")) 1994aa835160SAndriy Gapon *order = 4; 199591233413SNate Lawson } 199691233413SNate Lawson 199715e32d5dSMike Smith /* 199815e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 199915e32d5dSMike Smith * it. 200015e32d5dSMike Smith */ 200115e32d5dSMike Smith static ACPI_STATUS 200215e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 200315e32d5dSMike Smith { 20040e68afe5SAndrew Turner ACPI_DEVICE_INFO *devinfo; 20050e68afe5SAndrew Turner struct acpi_device *ad; 20065a77b11bSJung-uk Kim struct acpi_prw_data prw; 200715e32d5dSMike Smith ACPI_OBJECT_TYPE type; 2008858a52f4SMitsuru IWASAKI ACPI_HANDLE h; 200933332dc2SNate Lawson device_t bus, child; 2010495a4144SJung-uk Kim char *handle_str; 2011e4cd9dcfSJohn Baldwin int order; 201215e32d5dSMike Smith 2013b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 20140ae55423SMike Smith 2015495a4144SJung-uk Kim if (acpi_disabled("children")) 2016495a4144SJung-uk Kim return_ACPI_STATUS (AE_OK); 2017495a4144SJung-uk Kim 2018be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 20190ae55423SMike Smith if (acpi_avoid(handle)) 20200ae55423SMike Smith return_ACPI_STATUS (AE_OK); 20210ae55423SMike Smith 202291233413SNate Lawson bus = (device_t)context; 2023b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 2024495a4144SJung-uk Kim handle_str = acpi_name(handle); 202515e32d5dSMike Smith switch (type) { 202615e32d5dSMike Smith case ACPI_TYPE_DEVICE: 2027495a4144SJung-uk Kim /* 2028495a4144SJung-uk Kim * Since we scan from \, be sure to skip system scope objects. 2029495a4144SJung-uk Kim * \_SB_ and \_TZ_ are defined in ACPICA as devices to work around 2030affa1826SJung-uk Kim * BIOS bugs. For example, \_SB_ is to allow \_SB_._INI to be run 2031453130d9SPedro F. Giffuni * during the initialization and \_TZ_ is to support Notify() on it. 2032495a4144SJung-uk Kim */ 2033495a4144SJung-uk Kim if (strcmp(handle_str, "\\_SB_") == 0 || 2034495a4144SJung-uk Kim strcmp(handle_str, "\\_TZ_") == 0) 2035495a4144SJung-uk Kim break; 20365a77b11bSJung-uk Kim if (acpi_parse_prw(handle, &prw) == 0) 20375a77b11bSJung-uk Kim AcpiSetupGpeForWake(handle, prw.gpe_handle, prw.gpe_bit); 2038183c8af3SJohn Baldwin 2039183c8af3SJohn Baldwin /* 2040183c8af3SJohn Baldwin * Ignore devices that do not have a _HID or _CID. They should 2041183c8af3SJohn Baldwin * be discovered by other buses (e.g. the PCI bus driver). 2042183c8af3SJohn Baldwin */ 2043183c8af3SJohn Baldwin if (!acpi_has_hid(handle)) 2044183c8af3SJohn Baldwin break; 2045495a4144SJung-uk Kim /* FALLTHROUGH */ 204615e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 204715e32d5dSMike Smith case ACPI_TYPE_THERMAL: 204815e32d5dSMike Smith case ACPI_TYPE_POWER: 204933332dc2SNate Lawson /* 2050463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the 2051463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run 2052463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 2053463e0f91SJohn Baldwin * are reserved for special objects (i.e., system 2054b1f9b996SAndriy Gapon * resources). 205515e32d5dSMike Smith */ 205633332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 2057404b0d10SJung-uk Kim order = level * 10 + ACPI_DEV_BASE_ORDER; 2058da72d149SNate Lawson acpi_probe_order(handle, &order); 2059e4cd9dcfSJohn Baldwin child = BUS_ADD_CHILD(bus, order, NULL, -1); 2060bc0f2195SMike Smith if (child == NULL) 2061bc0f2195SMike Smith break; 206259a890e6SNate Lawson 206359a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 206415e32d5dSMike Smith acpi_set_handle(child, handle); 206559a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 2066bc0f2195SMike Smith 2067bc0f2195SMike Smith /* 2068b519f9d6SMike Smith * Check that the device is present. If it's not present, 2069b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 2070b519f9d6SMike Smith * the handle, but we don't probe it). 2071893f750aSNate Lawson * 2072893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not 2073893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them 2074893f750aSNate Lawson * anyway since we may enable them later. 2075b519f9d6SMike Smith */ 2076858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 2077858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */ 2078858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F")) 2079858a52f4SMitsuru IWASAKI break; 2080858a52f4SMitsuru IWASAKI /* 2081858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system 2082858a52f4SMitsuru IWASAKI * may be undocked at boot. 2083858a52f4SMitsuru IWASAKI */ 2084858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 2085858a52f4SMitsuru IWASAKI break; 2086858a52f4SMitsuru IWASAKI 2087b519f9d6SMike Smith device_disable(child); 2088b519f9d6SMike Smith break; 2089b519f9d6SMike Smith } 2090b519f9d6SMike Smith 2091b519f9d6SMike Smith /* 2092bc0f2195SMike Smith * Get the device's resource settings and attach them. 2093bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 2094bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 2095bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 2096bc0f2195SMike Smith * device not to have any resources. 2097bc0f2195SMike Smith */ 209872ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 20990e68afe5SAndrew Turner 21000e68afe5SAndrew Turner ad = device_get_ivars(child); 21010e68afe5SAndrew Turner ad->ad_cls_class = 0xffffff; 21020e68afe5SAndrew Turner if (ACPI_SUCCESS(AcpiGetObjectInfo(handle, &devinfo))) { 21030e68afe5SAndrew Turner if ((devinfo->Valid & ACPI_VALID_CLS) != 0 && 21040e68afe5SAndrew Turner devinfo->ClassCode.Length >= ACPI_PCICLS_STRING_SIZE) { 21050e68afe5SAndrew Turner ad->ad_cls_class = strtoul(devinfo->ClassCode.String, 21060e68afe5SAndrew Turner NULL, 16); 21070e68afe5SAndrew Turner } 21080e68afe5SAndrew Turner AcpiOsFree(devinfo); 21090e68afe5SAndrew Turner } 21100ae55423SMike Smith break; 211115e32d5dSMike Smith } 211215e32d5dSMike Smith } 2113be2b1797SNate Lawson 21140ae55423SMike Smith return_ACPI_STATUS (AE_OK); 211515e32d5dSMike Smith } 211615e32d5dSMike Smith 211759a890e6SNate Lawson /* 211859a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 211959a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 212059a890e6SNate Lawson */ 212159a890e6SNate Lawson void 212292488a57SJung-uk Kim acpi_fake_objhandler(ACPI_HANDLE h, void *data) 212359a890e6SNate Lawson { 212459a890e6SNate Lawson } 212559a890e6SNate Lawson 212615e32d5dSMike Smith static void 212715e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 212815e32d5dSMike Smith { 21292d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 213071804adcSJung-uk Kim register_t intr; 2131d33f4987STakanori Watanabe ACPI_STATUS status; 2132eeb3a05fSNate Lawson 2133eeb3a05fSNate Lawson /* 213480f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 213580f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 213680f0e4c2SNate Lawson * an AP. 2137eeb3a05fSNate Lawson */ 2138eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 2139904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 2140904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 21412d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2142904bf0c2SNate Lawson AcpiFormatException(status)); 2143904bf0c2SNate Lawson return; 2144904bf0c2SNate Lawson } 21452d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Powering system off\n"); 214671804adcSJung-uk Kim intr = intr_disable(); 21471df130f1SJung-uk Kim status = AcpiEnterSleepState(ACPI_STATE_S5); 214871804adcSJung-uk Kim if (ACPI_FAILURE(status)) { 214971804adcSJung-uk Kim intr_restore(intr); 21502d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - %s\n", 21512d0c82e8SJung-uk Kim AcpiFormatException(status)); 215271804adcSJung-uk Kim } else { 215315e32d5dSMike Smith DELAY(1000000); 215471804adcSJung-uk Kim intr_restore(intr); 21552d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "power-off failed - timeout\n"); 215615e32d5dSMike Smith } 2157ac731af5SJung-uk Kim } else if ((howto & RB_HALT) == 0 && sc->acpi_handle_reboot) { 2158d85e6785SNate Lawson /* Reboot using the reset register. */ 2159ac731af5SJung-uk Kim status = AcpiReset(); 2160ac731af5SJung-uk Kim if (ACPI_SUCCESS(status)) { 216187a500cdSNate Lawson DELAY(1000000); 21622d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - timeout\n"); 2163ac731af5SJung-uk Kim } else if (status != AE_NOT_EXIST) 2164ac731af5SJung-uk Kim device_printf(sc->acpi_dev, "reset failed - %s\n", 2165ac731af5SJung-uk Kim AcpiFormatException(status)); 2166d85e6785SNate Lawson } else if (sc->acpi_do_disable && panicstr == NULL) { 2167d85e6785SNate Lawson /* 2168d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing 2169d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system. 2170d85e6785SNate Lawson */ 21712d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, "Shutting down\n"); 217280f0e4c2SNate Lawson AcpiTerminate(); 217380f0e4c2SNate Lawson } 217415e32d5dSMike Smith } 217515e32d5dSMike Smith 217613d4f7baSMitsuru IWASAKI static void 217713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 217813d4f7baSMitsuru IWASAKI { 217913d4f7baSMitsuru IWASAKI static int first_time = 1; 218013d4f7baSMitsuru IWASAKI 218113d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 21822be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 218359ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 218413d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 218533febf93SNate Lawson acpi_event_power_button_sleep, sc); 2186be2b1797SNate Lawson if (first_time) 21876c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 218813d4f7baSMitsuru IWASAKI } 21892be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 219059ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 219113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 219233febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 2193be2b1797SNate Lawson if (first_time) 21946c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 219513d4f7baSMitsuru IWASAKI } 219613d4f7baSMitsuru IWASAKI 219713d4f7baSMitsuru IWASAKI first_time = 0; 219813d4f7baSMitsuru IWASAKI } 219913d4f7baSMitsuru IWASAKI 220015e32d5dSMike Smith /* 2201c5ba0be4SMike Smith * Returns true if the device is actually present and should 2202c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 2203c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 2204c5ba0be4SMike Smith */ 2205c5ba0be4SMike Smith BOOLEAN 2206c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 2207c5ba0be4SMike Smith { 2208c5ba0be4SMike Smith ACPI_HANDLE h; 22098438a7a8SJung-uk Kim UINT32 s; 22108438a7a8SJung-uk Kim ACPI_STATUS status; 2211c5ba0be4SMike Smith 22128438a7a8SJung-uk Kim h = acpi_get_handle(dev); 22138438a7a8SJung-uk Kim if (h == NULL) 2214c5ba0be4SMike Smith return (FALSE); 2215381388b9SMatt Macy /* 2216bf15eaf2SWarner Losh * Certain Treadripper boards always returns 0 for FreeBSD because it 2217bf15eaf2SWarner Losh * only returns non-zero for the OS string "Windows 2015". Otherwise it 2218bf15eaf2SWarner Losh * will return zero. Force them to always be treated as present. 2219bf15eaf2SWarner Losh * Beata versions were worse: they always returned 0. 2220381388b9SMatt Macy */ 2221381388b9SMatt Macy if (acpi_MatchHid(h, "AMDI0020") || acpi_MatchHid(h, "AMDI0010")) 2222381388b9SMatt Macy return (TRUE); 2223381388b9SMatt Macy 2224817b71bbSAndriy Gapon status = acpi_GetInteger(h, "_STA", &s); 2225817b71bbSAndriy Gapon 2226817b71bbSAndriy Gapon /* 2227817b71bbSAndriy Gapon * If no _STA method or if it failed, then assume that 2228817b71bbSAndriy Gapon * the device is present. 2229817b71bbSAndriy Gapon */ 22308438a7a8SJung-uk Kim if (ACPI_FAILURE(status)) 2231817b71bbSAndriy Gapon return (TRUE); 2232be2b1797SNate Lawson 22338438a7a8SJung-uk Kim return (ACPI_DEVICE_PRESENT(s) ? TRUE : FALSE); 2234c5ba0be4SMike Smith } 2235c5ba0be4SMike Smith 2236c5ba0be4SMike Smith /* 2237b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 2238b53f2771SMike Smith */ 2239b53f2771SMike Smith BOOLEAN 2240b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 2241b53f2771SMike Smith { 2242b53f2771SMike Smith ACPI_HANDLE h; 22438438a7a8SJung-uk Kim UINT32 s; 22448438a7a8SJung-uk Kim ACPI_STATUS status; 2245b53f2771SMike Smith 22468438a7a8SJung-uk Kim h = acpi_get_handle(dev); 22478438a7a8SJung-uk Kim if (h == NULL) 2248b53f2771SMike Smith return (FALSE); 22498438a7a8SJung-uk Kim status = acpi_GetInteger(h, "_STA", &s); 2250be2b1797SNate Lawson 2251817b71bbSAndriy Gapon /* 2252817b71bbSAndriy Gapon * If no _STA method or if it failed, then assume that 2253817b71bbSAndriy Gapon * the device is present. 2254817b71bbSAndriy Gapon */ 22558438a7a8SJung-uk Kim if (ACPI_FAILURE(status)) 2256817b71bbSAndriy Gapon return (TRUE); 2257be2b1797SNate Lawson 22588438a7a8SJung-uk Kim return (ACPI_BATTERY_PRESENT(s) ? TRUE : FALSE); 2259b53f2771SMike Smith } 2260b53f2771SMike Smith 2261b53f2771SMike Smith /* 2262183c8af3SJohn Baldwin * Returns true if a device has at least one valid device ID. 2263183c8af3SJohn Baldwin */ 2264183c8af3SJohn Baldwin static BOOLEAN 2265183c8af3SJohn Baldwin acpi_has_hid(ACPI_HANDLE h) 2266183c8af3SJohn Baldwin { 2267183c8af3SJohn Baldwin ACPI_DEVICE_INFO *devinfo; 2268183c8af3SJohn Baldwin BOOLEAN ret; 2269183c8af3SJohn Baldwin 2270183c8af3SJohn Baldwin if (h == NULL || 2271183c8af3SJohn Baldwin ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 2272183c8af3SJohn Baldwin return (FALSE); 2273183c8af3SJohn Baldwin 2274183c8af3SJohn Baldwin ret = FALSE; 2275183c8af3SJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0) 2276183c8af3SJohn Baldwin ret = TRUE; 2277183c8af3SJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 2278183c8af3SJohn Baldwin if (devinfo->CompatibleIdList.Count > 0) 2279183c8af3SJohn Baldwin ret = TRUE; 2280183c8af3SJohn Baldwin 2281183c8af3SJohn Baldwin AcpiOsFree(devinfo); 2282183c8af3SJohn Baldwin return (ret); 2283183c8af3SJohn Baldwin } 2284183c8af3SJohn Baldwin 2285183c8af3SJohn Baldwin /* 228691233413SNate Lawson * Match a HID string against a handle 22875efca36fSTakanori Watanabe * returns ACPI_MATCHHID_HID if _HID match 22885efca36fSTakanori Watanabe * ACPI_MATCHHID_CID if _CID match and not _HID match. 22895efca36fSTakanori Watanabe * ACPI_MATCHHID_NOMATCH=0 if no match. 229015e32d5dSMike Smith */ 22915efca36fSTakanori Watanabe int 2292ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 229315e32d5dSMike Smith { 22941e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 229592488a57SJung-uk Kim BOOLEAN ret; 229692488a57SJung-uk Kim int i; 229792488a57SJung-uk Kim 229892488a57SJung-uk Kim if (hid == NULL || h == NULL || 229992488a57SJung-uk Kim ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) 23005efca36fSTakanori Watanabe return (ACPI_MATCHHID_NOMATCH); 230115e32d5dSMike Smith 23026d29ba58SAndriy Gapon ret = ACPI_MATCHHID_NOMATCH; 2303c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 230492488a57SJung-uk Kim strcmp(hid, devinfo->HardwareId.String) == 0) 23055efca36fSTakanori Watanabe ret = ACPI_MATCHHID_HID; 230692488a57SJung-uk Kim else if ((devinfo->Valid & ACPI_VALID_CID) != 0) 230792488a57SJung-uk Kim for (i = 0; i < devinfo->CompatibleIdList.Count; i++) { 230892488a57SJung-uk Kim if (strcmp(hid, devinfo->CompatibleIdList.Ids[i].String) == 0) { 23095efca36fSTakanori Watanabe ret = ACPI_MATCHHID_CID; 23101e4925e8SNate Lawson break; 23111e4925e8SNate Lawson } 23121e4925e8SNate Lawson } 2313be2b1797SNate Lawson 231492488a57SJung-uk Kim AcpiOsFree(devinfo); 23151e4925e8SNate Lawson return (ret); 231615e32d5dSMike Smith } 231715e32d5dSMike Smith 231815e32d5dSMike Smith /* 2319c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 2320c5ba0be4SMike Smith * or one if its parents. 2321c5ba0be4SMike Smith */ 2322c5ba0be4SMike Smith ACPI_STATUS 2323c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 2324c5ba0be4SMike Smith { 2325c5ba0be4SMike Smith ACPI_HANDLE r; 2326c5ba0be4SMike Smith ACPI_STATUS status; 2327c5ba0be4SMike Smith 2328be2b1797SNate Lawson /* Walk back up the tree to the root */ 2329c5ba0be4SMike Smith for (;;) { 2330be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 2331be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2332c5ba0be4SMike Smith *result = r; 2333c5ba0be4SMike Smith return (AE_OK); 2334c5ba0be4SMike Smith } 2335bee4aa4aSNate Lawson /* XXX Return error here? */ 2336c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 2337c5ba0be4SMike Smith return (AE_OK); 2338b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 2339c5ba0be4SMike Smith return (AE_NOT_FOUND); 2340c5ba0be4SMike Smith parent = r; 2341c5ba0be4SMike Smith } 2342c5ba0be4SMike Smith } 2343c5ba0be4SMike Smith 2344c5ba0be4SMike Smith /* 2345c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 2346c5ba0be4SMike Smith */ 2347c5ba0be4SMike Smith ACPI_BUFFER * 2348c5ba0be4SMike Smith acpi_AllocBuffer(int size) 2349c5ba0be4SMike Smith { 2350c5ba0be4SMike Smith ACPI_BUFFER *buf; 2351c5ba0be4SMike Smith 2352c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2353c5ba0be4SMike Smith return (NULL); 2354c5ba0be4SMike Smith buf->Length = size; 2355c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 2356c5ba0be4SMike Smith return (buf); 2357c5ba0be4SMike Smith } 2358c5ba0be4SMike Smith 2359c310653eSNate Lawson ACPI_STATUS 2360cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2361c310653eSNate Lawson { 2362c310653eSNate Lawson ACPI_OBJECT arg1; 2363c310653eSNate Lawson ACPI_OBJECT_LIST args; 2364c310653eSNate Lawson 2365c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 2366c310653eSNate Lawson arg1.Integer.Value = number; 2367c310653eSNate Lawson args.Count = 1; 2368c310653eSNate Lawson args.Pointer = &arg1; 2369c310653eSNate Lawson 2370c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 2371c310653eSNate Lawson } 2372c310653eSNate Lawson 2373c5ba0be4SMike Smith /* 2374c5ba0be4SMike Smith * Evaluate a path that should return an integer. 2375c5ba0be4SMike Smith */ 2376c5ba0be4SMike Smith ACPI_STATUS 2377cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2378c5ba0be4SMike Smith { 2379be2b1797SNate Lawson ACPI_STATUS status; 2380c5ba0be4SMike Smith ACPI_BUFFER buf; 2381c573e654SMitsuru IWASAKI ACPI_OBJECT param; 2382c5ba0be4SMike Smith 2383c5ba0be4SMike Smith if (handle == NULL) 2384c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 23850c7da7acSJohn Baldwin 23860c7da7acSJohn Baldwin /* 23870c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 23880c7da7acSJohn Baldwin * a method that will return an Integer. 23890c7da7acSJohn Baldwin */ 2390c5ba0be4SMike Smith buf.Pointer = ¶m; 2391c5ba0be4SMike Smith buf.Length = sizeof(param); 2392be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2393be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2394be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 2395c5ba0be4SMike Smith *number = param.Integer.Value; 2396be2b1797SNate Lawson else 2397be2b1797SNate Lawson status = AE_TYPE; 2398c5ba0be4SMike Smith } 23990c7da7acSJohn Baldwin 24000c7da7acSJohn Baldwin /* 24010c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 24020c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 24030c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 24040c7da7acSJohn Baldwin * convert it into an Integer as best we can. 24050c7da7acSJohn Baldwin * 24060c7da7acSJohn Baldwin * This is a hack. 24070c7da7acSJohn Baldwin */ 2408be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 2409b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2410be2b1797SNate Lawson status = AE_NO_MEMORY; 24110c7da7acSJohn Baldwin } else { 2412be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2413be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 2414be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 24150c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 24160c7da7acSJohn Baldwin } 2417f97739daSNate Lawson } 2418be2b1797SNate Lawson return (status); 2419c5ba0be4SMike Smith } 2420c5ba0be4SMike Smith 2421c573e654SMitsuru IWASAKI ACPI_STATUS 2422cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2423c573e654SMitsuru IWASAKI { 2424c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 2425dba55fa2SNate Lawson UINT8 *val; 2426c573e654SMitsuru IWASAKI int i; 2427c573e654SMitsuru IWASAKI 2428c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 2429c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 2430c573e654SMitsuru IWASAKI *number = p->Integer.Value; 2431c573e654SMitsuru IWASAKI return (AE_OK); 2432c573e654SMitsuru IWASAKI } 2433c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 2434c573e654SMitsuru IWASAKI return (AE_TYPE); 2435c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 2436c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 2437be2b1797SNate Lawson 2438c573e654SMitsuru IWASAKI *number = 0; 2439dba55fa2SNate Lawson val = p->Buffer.Pointer; 2440c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 2441dba55fa2SNate Lawson *number += val[i] << (i * 8); 2442c573e654SMitsuru IWASAKI return (AE_OK); 2443c573e654SMitsuru IWASAKI } 2444c573e654SMitsuru IWASAKI 2445c5ba0be4SMike Smith /* 2446c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 2447c5ba0be4SMike Smith * function for each element. 2448c5ba0be4SMike Smith * 2449c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 2450c5ba0be4SMike Smith */ 2451c5ba0be4SMike Smith ACPI_STATUS 2452be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2453be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2454c5ba0be4SMike Smith { 2455c5ba0be4SMike Smith ACPI_OBJECT *comp; 2456c5ba0be4SMike Smith int i; 2457c5ba0be4SMike Smith 2458be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2459c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 2460c5ba0be4SMike Smith 2461be2b1797SNate Lawson /* Iterate over components */ 2462be2b1797SNate Lawson i = 0; 2463be2b1797SNate Lawson comp = pkg->Package.Elements; 2464be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 2465c5ba0be4SMike Smith func(comp, arg); 2466c5ba0be4SMike Smith 2467c5ba0be4SMike Smith return (AE_OK); 2468c5ba0be4SMike Smith } 2469c5ba0be4SMike Smith 24706f69255bSMike Smith /* 24716f69255bSMike Smith * Find the (index)th resource object in a set. 24726f69255bSMike Smith */ 24736f69255bSMike Smith ACPI_STATUS 24746d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 24756f69255bSMike Smith { 24766d63101aSMike Smith ACPI_RESOURCE *rp; 24776f69255bSMike Smith int i; 24786f69255bSMike Smith 24796d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 24806f69255bSMike Smith i = index; 24816d63101aSMike Smith while (i-- > 0) { 2482be2b1797SNate Lawson /* Range check */ 24836d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 24846f69255bSMike Smith return (AE_BAD_PARAMETER); 2485be2b1797SNate Lawson 2486be2b1797SNate Lawson /* Check for terminator */ 2487e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 24886d63101aSMike Smith return (AE_NOT_FOUND); 2489abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 24906f69255bSMike Smith } 24916f69255bSMike Smith if (resp != NULL) 24926d63101aSMike Smith *resp = rp; 2493be2b1797SNate Lawson 24946d63101aSMike Smith return (AE_OK); 24956d63101aSMike Smith } 24966d63101aSMike Smith 24976d63101aSMike Smith /* 24986d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 24996d63101aSMike Smith * 25006d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 25016d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 25026d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 25036d63101aSMike Smith * resources. 25046d63101aSMike Smith */ 25056d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 25066d63101aSMike Smith 25076d63101aSMike Smith ACPI_STATUS 25086d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 25096d63101aSMike Smith { 25106d63101aSMike Smith ACPI_RESOURCE *rp; 25116d63101aSMike Smith void *newp; 25126d63101aSMike Smith 2513be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 25146d63101aSMike Smith if (buf->Pointer == NULL) { 25156d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 25166d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 25176d63101aSMike Smith return (AE_NO_MEMORY); 25186d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 2519e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 252060d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN; 25216d63101aSMike Smith } 25226d63101aSMike Smith if (res == NULL) 25236d63101aSMike Smith return (AE_OK); 25246d63101aSMike Smith 25256d63101aSMike Smith /* 25266d63101aSMike Smith * Scan the current buffer looking for the terminator. 25276d63101aSMike Smith * This will either find the terminator or hit the end 25286d63101aSMike Smith * of the buffer and return an error. 25296d63101aSMike Smith */ 25306d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 25316d63101aSMike Smith for (;;) { 2532be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 25336d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 25346d63101aSMike Smith return (AE_BAD_PARAMETER); 2535e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 25366d63101aSMike Smith break; 2537abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 25386d63101aSMike Smith } 25396d63101aSMike Smith 25406d63101aSMike Smith /* 25416d63101aSMike Smith * Check the size of the buffer and expand if required. 25426d63101aSMike Smith * 25436d63101aSMike Smith * Required size is: 25446d63101aSMike Smith * size of existing resources before terminator + 25456d63101aSMike Smith * size of new resource and header + 25466d63101aSMike Smith * size of terminator. 25476d63101aSMike Smith * 25486d63101aSMike Smith * Note that this loop should really only run once, unless 25496d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 25506d63101aSMike Smith */ 25516d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2552e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA + 2553e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) { 25546d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 25556d63101aSMike Smith return (AE_NO_MEMORY); 25566d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 2557a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2558a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 25596d63101aSMike Smith AcpiOsFree(buf->Pointer); 25606d63101aSMike Smith buf->Pointer = newp; 25616d63101aSMike Smith buf->Length += buf->Length; 25626d63101aSMike Smith } 25636d63101aSMike Smith 2564be2b1797SNate Lawson /* Insert the new resource. */ 2565e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 25666d63101aSMike Smith 2567be2b1797SNate Lawson /* And add the terminator. */ 2568abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 2569e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 257060d306f0SJohn Baldwin rp->Length = ACPI_RS_SIZE_MIN; 25716d63101aSMike Smith 25726f69255bSMike Smith return (AE_OK); 25736f69255bSMike Smith } 2574c5ba0be4SMike Smith 25758fd10880SBen Widawsky UINT8 25768fd10880SBen Widawsky acpi_DSMQuery(ACPI_HANDLE h, uint8_t *uuid, int revision) 25778fd10880SBen Widawsky { 25788fd10880SBen Widawsky /* 25798fd10880SBen Widawsky * ACPI spec 9.1.1 defines this. 25808fd10880SBen Widawsky * 25818fd10880SBen Widawsky * "Arg2: Function Index Represents a specific function whose meaning is 25828fd10880SBen Widawsky * specific to the UUID and Revision ID. Function indices should start 25838fd10880SBen Widawsky * with 1. Function number zero is a query function (see the special 25848fd10880SBen Widawsky * return code defined below)." 25858fd10880SBen Widawsky */ 25868fd10880SBen Widawsky ACPI_BUFFER buf; 25878fd10880SBen Widawsky ACPI_OBJECT *obj; 25888fd10880SBen Widawsky UINT8 ret = 0; 25898fd10880SBen Widawsky 25908fd10880SBen Widawsky if (!ACPI_SUCCESS(acpi_EvaluateDSM(h, uuid, revision, 0, NULL, &buf))) { 25918fd10880SBen Widawsky ACPI_INFO(("Failed to enumerate DSM functions\n")); 25928fd10880SBen Widawsky return (0); 25938fd10880SBen Widawsky } 25948fd10880SBen Widawsky 25958fd10880SBen Widawsky obj = (ACPI_OBJECT *)buf.Pointer; 25968fd10880SBen Widawsky KASSERT(obj, ("Object not allowed to be NULL\n")); 25978fd10880SBen Widawsky 25988fd10880SBen Widawsky /* 25998fd10880SBen Widawsky * From ACPI 6.2 spec 9.1.1: 26008fd10880SBen Widawsky * If Function Index = 0, a Buffer containing a function index bitfield. 26018fd10880SBen Widawsky * Otherwise, the return value and type depends on the UUID and revision 26028fd10880SBen Widawsky * ID (see below). 26038fd10880SBen Widawsky */ 26048fd10880SBen Widawsky switch (obj->Type) { 26058fd10880SBen Widawsky case ACPI_TYPE_BUFFER: 26068fd10880SBen Widawsky ret = *(uint8_t *)obj->Buffer.Pointer; 26078fd10880SBen Widawsky break; 26088fd10880SBen Widawsky case ACPI_TYPE_INTEGER: 26098fd10880SBen Widawsky ACPI_BIOS_WARNING((AE_INFO, 26108fd10880SBen Widawsky "Possibly buggy BIOS with ACPI_TYPE_INTEGER for function enumeration\n")); 26118fd10880SBen Widawsky ret = obj->Integer.Value & 0xFF; 26128fd10880SBen Widawsky break; 26138fd10880SBen Widawsky default: 26148fd10880SBen Widawsky ACPI_WARNING((AE_INFO, "Unexpected return type %u\n", obj->Type)); 26158fd10880SBen Widawsky }; 26168fd10880SBen Widawsky 26178fd10880SBen Widawsky AcpiOsFree(obj); 26188fd10880SBen Widawsky return ret; 26198fd10880SBen Widawsky } 26208fd10880SBen Widawsky 26218fd10880SBen Widawsky /* 26228fd10880SBen Widawsky * DSM may return multiple types depending on the function. It is therefore 26238fd10880SBen Widawsky * unsafe to use the typed evaluation. It is highly recommended that the caller 26248fd10880SBen Widawsky * check the type of the returned object. 26258fd10880SBen Widawsky */ 26268fd10880SBen Widawsky ACPI_STATUS 26278fd10880SBen Widawsky acpi_EvaluateDSM(ACPI_HANDLE handle, uint8_t *uuid, int revision, 26288fd10880SBen Widawsky uint64_t function, union acpi_object *package, ACPI_BUFFER *out_buf) 26298fd10880SBen Widawsky { 26308fd10880SBen Widawsky ACPI_OBJECT arg[4]; 26318fd10880SBen Widawsky ACPI_OBJECT_LIST arglist; 26328fd10880SBen Widawsky ACPI_BUFFER buf; 26338fd10880SBen Widawsky ACPI_STATUS status; 26348fd10880SBen Widawsky 26358fd10880SBen Widawsky if (out_buf == NULL) 26368fd10880SBen Widawsky return (AE_NO_MEMORY); 26378fd10880SBen Widawsky 26388fd10880SBen Widawsky arg[0].Type = ACPI_TYPE_BUFFER; 26398fd10880SBen Widawsky arg[0].Buffer.Length = ACPI_UUID_LENGTH; 26408fd10880SBen Widawsky arg[0].Buffer.Pointer = uuid; 26418fd10880SBen Widawsky arg[1].Type = ACPI_TYPE_INTEGER; 26428fd10880SBen Widawsky arg[1].Integer.Value = revision; 26438fd10880SBen Widawsky arg[2].Type = ACPI_TYPE_INTEGER; 26448fd10880SBen Widawsky arg[2].Integer.Value = function; 26458fd10880SBen Widawsky if (package) { 26468fd10880SBen Widawsky arg[3] = *package; 26478fd10880SBen Widawsky } else { 26488fd10880SBen Widawsky arg[3].Type = ACPI_TYPE_PACKAGE; 26498fd10880SBen Widawsky arg[3].Package.Count = 0; 26508fd10880SBen Widawsky arg[3].Package.Elements = NULL; 26518fd10880SBen Widawsky } 26528fd10880SBen Widawsky 26538fd10880SBen Widawsky arglist.Pointer = arg; 26548fd10880SBen Widawsky arglist.Count = 4; 26558fd10880SBen Widawsky buf.Pointer = NULL; 26568fd10880SBen Widawsky buf.Length = ACPI_ALLOCATE_BUFFER; 26578fd10880SBen Widawsky status = AcpiEvaluateObject(handle, "_DSM", &arglist, &buf); 26588fd10880SBen Widawsky if (ACPI_FAILURE(status)) 26598fd10880SBen Widawsky return (status); 26608fd10880SBen Widawsky 26618fd10880SBen Widawsky KASSERT(ACPI_SUCCESS(status), ("Unexpected status")); 26628fd10880SBen Widawsky 26638fd10880SBen Widawsky *out_buf = buf; 26648fd10880SBen Widawsky return (status); 26658fd10880SBen Widawsky } 26668fd10880SBen Widawsky 26675f3dd91aSJohn Baldwin ACPI_STATUS 26685f3dd91aSJohn Baldwin acpi_EvaluateOSC(ACPI_HANDLE handle, uint8_t *uuid, int revision, int count, 26694c26ac69SJohn Baldwin uint32_t *caps_in, uint32_t *caps_out, bool query) 26705f3dd91aSJohn Baldwin { 26714c26ac69SJohn Baldwin ACPI_OBJECT arg[4], *ret; 26725f3dd91aSJohn Baldwin ACPI_OBJECT_LIST arglist; 26734c26ac69SJohn Baldwin ACPI_BUFFER buf; 26744c26ac69SJohn Baldwin ACPI_STATUS status; 26755f3dd91aSJohn Baldwin 26765f3dd91aSJohn Baldwin arglist.Pointer = arg; 26775f3dd91aSJohn Baldwin arglist.Count = 4; 26785f3dd91aSJohn Baldwin arg[0].Type = ACPI_TYPE_BUFFER; 26795f3dd91aSJohn Baldwin arg[0].Buffer.Length = ACPI_UUID_LENGTH; 26805f3dd91aSJohn Baldwin arg[0].Buffer.Pointer = uuid; 26815f3dd91aSJohn Baldwin arg[1].Type = ACPI_TYPE_INTEGER; 26825f3dd91aSJohn Baldwin arg[1].Integer.Value = revision; 26835f3dd91aSJohn Baldwin arg[2].Type = ACPI_TYPE_INTEGER; 26845f3dd91aSJohn Baldwin arg[2].Integer.Value = count; 26855f3dd91aSJohn Baldwin arg[3].Type = ACPI_TYPE_BUFFER; 26864c26ac69SJohn Baldwin arg[3].Buffer.Length = count * sizeof(*caps_in); 26874c26ac69SJohn Baldwin arg[3].Buffer.Pointer = (uint8_t *)caps_in; 26884c26ac69SJohn Baldwin caps_in[0] = query ? 1 : 0; 26894c26ac69SJohn Baldwin buf.Pointer = NULL; 26904c26ac69SJohn Baldwin buf.Length = ACPI_ALLOCATE_BUFFER; 26914c26ac69SJohn Baldwin status = AcpiEvaluateObjectTyped(handle, "_OSC", &arglist, &buf, 26924c26ac69SJohn Baldwin ACPI_TYPE_BUFFER); 26934c26ac69SJohn Baldwin if (ACPI_FAILURE(status)) 26944c26ac69SJohn Baldwin return (status); 26954c26ac69SJohn Baldwin if (caps_out != NULL) { 26964c26ac69SJohn Baldwin ret = buf.Pointer; 26974c26ac69SJohn Baldwin if (ret->Buffer.Length != count * sizeof(*caps_out)) { 26984c26ac69SJohn Baldwin AcpiOsFree(buf.Pointer); 26994c26ac69SJohn Baldwin return (AE_BUFFER_OVERFLOW); 27004c26ac69SJohn Baldwin } 27014c26ac69SJohn Baldwin bcopy(ret->Buffer.Pointer, caps_out, ret->Buffer.Length); 27024c26ac69SJohn Baldwin } 27034c26ac69SJohn Baldwin AcpiOsFree(buf.Pointer); 27044c26ac69SJohn Baldwin return (status); 27055f3dd91aSJohn Baldwin } 27065f3dd91aSJohn Baldwin 2707da14ac9fSJohn Baldwin /* 2708da14ac9fSJohn Baldwin * Set interrupt model. 2709da14ac9fSJohn Baldwin */ 2710da14ac9fSJohn Baldwin ACPI_STATUS 2711da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 2712da14ac9fSJohn Baldwin { 2713da14ac9fSJohn Baldwin 2714c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2715da14ac9fSJohn Baldwin } 2716da14ac9fSJohn Baldwin 271700a30448SNate Lawson /* 2718d95e7f5aSJohn Baldwin * Walk subtables of a table and call a callback routine for each 2719d95e7f5aSJohn Baldwin * subtable. The caller should provide the first subtable and a 2720d95e7f5aSJohn Baldwin * pointer to the end of the table. This can be used to walk tables 2721d95e7f5aSJohn Baldwin * such as MADT and SRAT that use subtable entries. 2722d95e7f5aSJohn Baldwin */ 2723d95e7f5aSJohn Baldwin void 2724d95e7f5aSJohn Baldwin acpi_walk_subtables(void *first, void *end, acpi_subtable_handler *handler, 2725d95e7f5aSJohn Baldwin void *arg) 2726d95e7f5aSJohn Baldwin { 2727d95e7f5aSJohn Baldwin ACPI_SUBTABLE_HEADER *entry; 2728d95e7f5aSJohn Baldwin 2729d95e7f5aSJohn Baldwin for (entry = first; (void *)entry < end; ) { 2730d95e7f5aSJohn Baldwin /* Avoid an infinite loop if we hit a bogus entry. */ 2731d95e7f5aSJohn Baldwin if (entry->Length < sizeof(ACPI_SUBTABLE_HEADER)) 2732d95e7f5aSJohn Baldwin return; 2733d95e7f5aSJohn Baldwin 2734d95e7f5aSJohn Baldwin handler(entry, arg); 2735d95e7f5aSJohn Baldwin entry = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, entry, entry->Length); 2736d95e7f5aSJohn Baldwin } 2737d95e7f5aSJohn Baldwin } 2738d95e7f5aSJohn Baldwin 2739d95e7f5aSJohn Baldwin /* 274000a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be 274100a30448SNate Lawson * removed. 274200a30448SNate Lawson * 274300a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran 274400a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 274500a30448SNate Lawson */ 274600a30448SNate Lawson ACPI_STATUS 274700a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state) 274800a30448SNate Lawson { 274900a30448SNate Lawson static int once; 275000a30448SNate Lawson 275100a30448SNate Lawson if (!once) { 27522d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 275300a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n"); 275400a30448SNate Lawson once = 1; 275500a30448SNate Lawson } 275600a30448SNate Lawson return (acpi_EnterSleepState(sc, state)); 275700a30448SNate Lawson } 275800a30448SNate Lawson 2759c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 276000a30448SNate Lawson static void 2761ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task(void *context) 2762ffe3f1f8SMitsuru IWASAKI { 2763ffe3f1f8SMitsuru IWASAKI struct acpi_softc *sc = (struct acpi_softc *)context; 2764ffe3f1f8SMitsuru IWASAKI 2765ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 2766ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "force sleep state S%d failed\n", 2767ffe3f1f8SMitsuru IWASAKI sc->acpi_next_sstate); 2768ffe3f1f8SMitsuru IWASAKI } 2769ffe3f1f8SMitsuru IWASAKI 2770ffe3f1f8SMitsuru IWASAKI static void 277100a30448SNate Lawson acpi_sleep_force(void *arg) 277200a30448SNate Lawson { 27732d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 277400a30448SNate Lawson 27752d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 27762d0c82e8SJung-uk Kim "suspend request timed out, forcing sleep now\n"); 2777ffe3f1f8SMitsuru IWASAKI /* 2778fadf3fb9SJohn Baldwin * XXX Suspending from callout causes freezes in DEVICE_SUSPEND(). 2779ffe3f1f8SMitsuru IWASAKI * Suspend from acpi_task thread instead. 2780ffe3f1f8SMitsuru IWASAKI */ 2781ffe3f1f8SMitsuru IWASAKI if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 2782ffe3f1f8SMitsuru IWASAKI acpi_sleep_force_task, sc))) 2783ffe3f1f8SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiOsExecute() for sleeping failed\n"); 278400a30448SNate Lawson } 2785c66d2b38SJung-uk Kim #endif 278600a30448SNate Lawson 278700a30448SNate Lawson /* 278800a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm 278900a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to 279000a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all 279100a30448SNate Lawson * acks are in. 279200a30448SNate Lawson */ 279300a30448SNate Lawson int 279400a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state) 279500a30448SNate Lawson { 279671f99e63SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 279700a30448SNate Lawson struct apm_clone_data *clone; 2798337744d9SJung-uk Kim ACPI_STATUS status; 279900a30448SNate Lawson 2800a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 280100a30448SNate Lawson return (EINVAL); 2802a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 2803a0e73a12SJung-uk Kim return (EOPNOTSUPP); 280400a30448SNate Lawson 28052eb0015aSColin Percival /* 28062eb0015aSColin Percival * If a reboot/shutdown/suspend request is already in progress or 28072eb0015aSColin Percival * suspend is blocked due to an upcoming shutdown, just return. 28082eb0015aSColin Percival */ 28092eb0015aSColin Percival if (rebooting || sc->acpi_next_sstate != 0 || suspend_blocked) { 2810b12bc99fSMitsuru IWASAKI return (0); 2811b12bc99fSMitsuru IWASAKI } 2812b12bc99fSMitsuru IWASAKI 2813f22377a8SMitsuru IWASAKI /* Wait until sleep is enabled. */ 2814f22377a8SMitsuru IWASAKI while (sc->acpi_sleep_disabled) { 2815f22377a8SMitsuru IWASAKI AcpiOsSleep(1000); 2816f22377a8SMitsuru IWASAKI } 2817f22377a8SMitsuru IWASAKI 2818337744d9SJung-uk Kim ACPI_LOCK(acpi); 281900a30448SNate Lawson 2820f22377a8SMitsuru IWASAKI sc->acpi_next_sstate = state; 282100a30448SNate Lawson 2822337744d9SJung-uk Kim /* S5 (soft-off) should be entered directly with no waiting. */ 2823337744d9SJung-uk Kim if (state == ACPI_STATE_S5) { 2824337744d9SJung-uk Kim ACPI_UNLOCK(acpi); 2825337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2826337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 2827337744d9SJung-uk Kim } 2828337744d9SJung-uk Kim 282900a30448SNate Lawson /* Record the pending state and notify all apm devices. */ 283000a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 283100a30448SNate Lawson clone->notify_status = APM_EV_NONE; 283200a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) { 283300a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO); 2834374d9c4dSJohn Baldwin KNOTE_LOCKED(&clone->sel_read.si_note, 0); 283500a30448SNate Lawson } 283600a30448SNate Lawson } 283700a30448SNate Lawson 28380c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */ 2839d42f0ad8SJung-uk Kim if (!devctl_process_running()) { 28400c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi); 2841337744d9SJung-uk Kim status = acpi_EnterSleepState(sc, state); 2842337744d9SJung-uk Kim return (ACPI_SUCCESS(status) ? 0 : ENXIO); 28430c26519eSMitsuru IWASAKI } 28440c26519eSMitsuru IWASAKI 284500a30448SNate Lawson /* 284600a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request 284700a30448SNate Lawson * in time. This way we still eventually go to sleep if we were 284800a30448SNate Lawson * overheating or running low on battery, even if userland is hung. 284900a30448SNate Lawson * We cancel this timeout once all userland acks are in or the 285000a30448SNate Lawson * suspend request is aborted. 285100a30448SNate Lawson */ 285200a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 285300a30448SNate Lawson ACPI_UNLOCK(acpi); 2854d42f0ad8SJung-uk Kim 2855d42f0ad8SJung-uk Kim /* Now notify devd(8) also. */ 2856d42f0ad8SJung-uk Kim acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 2857d42f0ad8SJung-uk Kim 285800a30448SNate Lawson return (0); 2859c66d2b38SJung-uk Kim #else 2860c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2861c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2862c66d2b38SJung-uk Kim #endif 286300a30448SNate Lawson } 286400a30448SNate Lawson 286500a30448SNate Lawson /* 286600a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has 286700a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the 286800a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled 286900a30448SNate Lawson * and gives an errno value describing why. Once all votes are in, 287000a30448SNate Lawson * we suspend the system. 287100a30448SNate Lawson */ 287200a30448SNate Lawson int 287300a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error) 287400a30448SNate Lawson { 2875c66d2b38SJung-uk Kim #if defined(__amd64__) || defined(__i386__) 287600a30448SNate Lawson struct acpi_softc *sc; 287700a30448SNate Lawson int ret, sleeping; 287800a30448SNate Lawson 287900a30448SNate Lawson /* If no pending sleep state, return an error. */ 288000a30448SNate Lawson ACPI_LOCK(acpi); 288100a30448SNate Lawson sc = clone->acpi_sc; 288200a30448SNate Lawson if (sc->acpi_next_sstate == 0) { 288300a30448SNate Lawson ACPI_UNLOCK(acpi); 288400a30448SNate Lawson return (ENXIO); 288500a30448SNate Lawson } 288600a30448SNate Lawson 288700a30448SNate Lawson /* Caller wants to abort suspend process. */ 288800a30448SNate Lawson if (error) { 288900a30448SNate Lawson sc->acpi_next_sstate = 0; 289000a30448SNate Lawson callout_stop(&sc->susp_force_to); 28912d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 28922d0c82e8SJung-uk Kim "listener on %s cancelled the pending suspend\n", 289300a30448SNate Lawson devtoname(clone->cdev)); 289400a30448SNate Lawson ACPI_UNLOCK(acpi); 289500a30448SNate Lawson return (0); 289600a30448SNate Lawson } 289700a30448SNate Lawson 289800a30448SNate Lawson /* 289900a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through 290000a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that 290100a30448SNate Lawson * are writable since read-only devices couldn't ack the request. 290200a30448SNate Lawson */ 290300a30448SNate Lawson sleeping = TRUE; 2904c66d2b38SJung-uk Kim clone->notify_status = APM_EV_ACKED; 290500a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 290600a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 && 290700a30448SNate Lawson clone->notify_status != APM_EV_ACKED) { 290800a30448SNate Lawson sleeping = FALSE; 290900a30448SNate Lawson break; 291000a30448SNate Lawson } 291100a30448SNate Lawson } 291200a30448SNate Lawson 291300a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */ 291400a30448SNate Lawson if (sleeping) 291500a30448SNate Lawson callout_stop(&sc->susp_force_to); 291600a30448SNate Lawson ACPI_UNLOCK(acpi); 291700a30448SNate Lawson ret = 0; 291800a30448SNate Lawson if (sleeping) { 291900a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 292000a30448SNate Lawson ret = ENODEV; 292100a30448SNate Lawson } 292200a30448SNate Lawson return (ret); 2923c66d2b38SJung-uk Kim #else 2924c66d2b38SJung-uk Kim /* This platform does not support acpi suspend/resume. */ 2925c66d2b38SJung-uk Kim return (EOPNOTSUPP); 2926c66d2b38SJung-uk Kim #endif 292700a30448SNate Lawson } 292800a30448SNate Lawson 2929ece50487SMitsuru IWASAKI static void 2930ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 2931ece50487SMitsuru IWASAKI { 2932d42f0ad8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 2933bee4aa4aSNate Lawson 2934fadf3fb9SJohn Baldwin ACPI_LOCK_ASSERT(acpi); 2935fadf3fb9SJohn Baldwin 2936a0e73a12SJung-uk Kim /* Reschedule if the system is not fully up and running. */ 2937a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) { 2938fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME); 2939a0e73a12SJung-uk Kim return; 2940a0e73a12SJung-uk Kim } 2941a0e73a12SJung-uk Kim 2942a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = FALSE; 2943d42f0ad8SJung-uk Kim } 2944d42f0ad8SJung-uk Kim 2945d42f0ad8SJung-uk Kim static ACPI_STATUS 2946d42f0ad8SJung-uk Kim acpi_sleep_disable(struct acpi_softc *sc) 2947d42f0ad8SJung-uk Kim { 2948d42f0ad8SJung-uk Kim ACPI_STATUS status; 2949d42f0ad8SJung-uk Kim 2950a0e73a12SJung-uk Kim /* Fail if the system is not fully up and running. */ 2951a0e73a12SJung-uk Kim if (!AcpiGbl_SystemAwakeAndRunning) 2952a0e73a12SJung-uk Kim return (AE_ERROR); 2953a0e73a12SJung-uk Kim 2954d42f0ad8SJung-uk Kim ACPI_LOCK(acpi); 2955d42f0ad8SJung-uk Kim status = sc->acpi_sleep_disabled ? AE_ERROR : AE_OK; 2956a0e73a12SJung-uk Kim sc->acpi_sleep_disabled = TRUE; 2957d42f0ad8SJung-uk Kim ACPI_UNLOCK(acpi); 2958d42f0ad8SJung-uk Kim 2959d42f0ad8SJung-uk Kim return (status); 2960ece50487SMitsuru IWASAKI } 2961c30382dfSMitsuru IWASAKI 296215e2f34fSNate Lawson enum acpi_sleep_state { 296315e2f34fSNate Lawson ACPI_SS_NONE, 296415e2f34fSNate Lawson ACPI_SS_GPE_SET, 296515e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 296615e2f34fSNate Lawson ACPI_SS_SLP_PREP, 296715e2f34fSNate Lawson ACPI_SS_SLEPT, 296815e2f34fSNate Lawson }; 296915e2f34fSNate Lawson 297015e32d5dSMike Smith /* 297100a30448SNate Lawson * Enter the desired system sleep state. 297215e32d5dSMike Smith * 2973be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 297415e32d5dSMike Smith */ 297500a30448SNate Lawson static ACPI_STATUS 297600a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state) 297715e32d5dSMike Smith { 297871804adcSJung-uk Kim register_t intr; 297915e2f34fSNate Lawson ACPI_STATUS status; 2980b913a7d5SAndriy Gapon ACPI_EVENT_STATUS power_button_status; 298115e2f34fSNate Lawson enum acpi_sleep_state slp_state; 2982f0a101b7SMitsuru IWASAKI int sleep_result; 298315e32d5dSMike Smith 2984b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 29850ae55423SMike Smith 2986a0e73a12SJung-uk Kim if (state < ACPI_STATE_S1 || state > ACPI_S_STATES_MAX) 298733c70d41SAndriy Gapon return_ACPI_STATUS (AE_BAD_PARAMETER); 2988a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) { 2989a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "Sleep state S%d not supported by BIOS\n", 2990a0e73a12SJung-uk Kim state); 2991a0e73a12SJung-uk Kim return (AE_SUPPORT); 2992a0e73a12SJung-uk Kim } 2993a0e73a12SJung-uk Kim 2994a0e73a12SJung-uk Kim /* Re-entry once we're suspending is not allowed. */ 2995a0e73a12SJung-uk Kim status = acpi_sleep_disable(sc); 2996a0e73a12SJung-uk Kim if (ACPI_FAILURE(status)) { 2997a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, 2998a0e73a12SJung-uk Kim "suspend request ignored (not ready yet)\n"); 2999a0e73a12SJung-uk Kim return (status); 3000a0e73a12SJung-uk Kim } 300133c70d41SAndriy Gapon 300233c70d41SAndriy Gapon if (state == ACPI_STATE_S5) { 300333c70d41SAndriy Gapon /* 300433c70d41SAndriy Gapon * Shut down cleanly and power off. This will call us back through the 300533c70d41SAndriy Gapon * shutdown handlers. 300633c70d41SAndriy Gapon */ 300733c70d41SAndriy Gapon shutdown_nice(RB_POWEROFF); 300833c70d41SAndriy Gapon return_ACPI_STATUS (AE_OK); 300933c70d41SAndriy Gapon } 301033c70d41SAndriy Gapon 301185f95fffSAndriy Gapon EVENTHANDLER_INVOKE(power_suspend_early); 301285f95fffSAndriy Gapon stop_all_proc(); 30134a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_suspend); 30144a8fa6feSJung-uk Kim 3015fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 3016fdce57a0SJohn Baldwin MPASS(mp_ncpus == 1 || smp_started); 3017fdce57a0SJohn Baldwin thread_lock(curthread); 3018fdce57a0SJohn Baldwin sched_bind(curthread, 0); 3019fdce57a0SJohn Baldwin thread_unlock(curthread); 3020fdce57a0SJohn Baldwin #else 302136a483bbSJung-uk Kim if (smp_started) { 3022c66d2b38SJung-uk Kim thread_lock(curthread); 3023c66d2b38SJung-uk Kim sched_bind(curthread, 0); 3024c66d2b38SJung-uk Kim thread_unlock(curthread); 302536a483bbSJung-uk Kim } 3026fdce57a0SJohn Baldwin #endif 3027c66d2b38SJung-uk Kim 3028a56fe095SJohn Baldwin /* 3029a56fe095SJohn Baldwin * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 3030a56fe095SJohn Baldwin * drivers need this. 3031a56fe095SJohn Baldwin */ 3032a56fe095SJohn Baldwin mtx_lock(&Giant); 3033c66d2b38SJung-uk Kim 303415e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 303556d8cb57SMitsuru IWASAKI 3036a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 3037a1fccb47SMitsuru IWASAKI 3038d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 3039d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 304015e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 3041e8b4d56eSNate Lawson 304215e32d5dSMike Smith /* 3043c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 3044c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 304515e32d5dSMike Smith * 3046c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 3047c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 3048c7f88fc3SNate Lawson * bus interface does not provide for this. 304915e32d5dSMike Smith */ 305015e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 305115e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 305233c70d41SAndriy Gapon goto backout; 305315e2f34fSNate Lawson } 305415e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 3055b9c780d6SMitsuru IWASAKI 3056be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 3057be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 3058b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 3059b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 306033c70d41SAndriy Gapon goto backout; 3061b9c780d6SMitsuru IWASAKI } 306215e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 3063b9c780d6SMitsuru IWASAKI 3064be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 3065ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 3066ff01efb5SMitsuru IWASAKI 306727dca831SAndriy Gapon suspendclock(); 3068f0a101b7SMitsuru IWASAKI intr = intr_disable(); 30694d46ef51SJung-uk Kim if (state != ACPI_STATE_S1) { 3070f0a101b7SMitsuru IWASAKI sleep_result = acpi_sleep_machdep(sc, state); 3071f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 0); 3072b1a5c017SAndriy Gapon 3073b1a5c017SAndriy Gapon /* 3074b1a5c017SAndriy Gapon * XXX According to ACPI specification SCI_EN bit should be restored 3075b1a5c017SAndriy Gapon * by ACPI platform (BIOS, firmware) to its pre-sleep state. 3076b1a5c017SAndriy Gapon * Unfortunately some BIOSes fail to do that and that leads to 3077b1a5c017SAndriy Gapon * unexpected and serious consequences during wake up like a system 3078b1a5c017SAndriy Gapon * getting stuck in SMI handlers. 3079b1a5c017SAndriy Gapon * This hack is picked up from Linux, which claims that it follows 3080b1a5c017SAndriy Gapon * Windows behavior. 3081b1a5c017SAndriy Gapon */ 3082b1a5c017SAndriy Gapon if (sleep_result == 1 && state != ACPI_STATE_S4) 3083b1a5c017SAndriy Gapon AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, ACPI_ENABLE_EVENT); 3084b1a5c017SAndriy Gapon 3085b913a7d5SAndriy Gapon if (sleep_result == 1 && state == ACPI_STATE_S3) { 3086b913a7d5SAndriy Gapon /* 3087b913a7d5SAndriy Gapon * Prevent mis-interpretation of the wakeup by power button 3088b913a7d5SAndriy Gapon * as a request for power off. 3089b913a7d5SAndriy Gapon * Ideally we should post an appropriate wakeup event, 3090b913a7d5SAndriy Gapon * perhaps using acpi_event_power_button_wake or alike. 3091b913a7d5SAndriy Gapon * 3092b913a7d5SAndriy Gapon * Clearing of power button status after wakeup is mandated 3093b913a7d5SAndriy Gapon * by ACPI specification in section "Fixed Power Button". 3094b913a7d5SAndriy Gapon * 3095b913a7d5SAndriy Gapon * XXX As of ACPICA 20121114 AcpiGetEventStatus provides 3096b913a7d5SAndriy Gapon * status as 0/1 corressponding to inactive/active despite 3097b913a7d5SAndriy Gapon * its type being ACPI_EVENT_STATUS. In other words, 3098b913a7d5SAndriy Gapon * we should not test for ACPI_EVENT_FLAG_SET for time being. 3099b913a7d5SAndriy Gapon */ 3100b913a7d5SAndriy Gapon if (ACPI_SUCCESS(AcpiGetEventStatus(ACPI_EVENT_POWER_BUTTON, 3101b913a7d5SAndriy Gapon &power_button_status)) && power_button_status != 0) { 3102b913a7d5SAndriy Gapon AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 3103b913a7d5SAndriy Gapon device_printf(sc->acpi_dev, 3104b913a7d5SAndriy Gapon "cleared fixed power button status\n"); 3105b913a7d5SAndriy Gapon } 3106b913a7d5SAndriy Gapon } 3107b913a7d5SAndriy Gapon 3108f0a101b7SMitsuru IWASAKI intr_restore(intr); 3109f0a101b7SMitsuru IWASAKI 3110f0a101b7SMitsuru IWASAKI /* call acpi_wakeup_machdep() again with interrupt enabled */ 3111f0a101b7SMitsuru IWASAKI acpi_wakeup_machdep(sc, state, sleep_result, 1); 3112f0a101b7SMitsuru IWASAKI 31130a15ff37SAndriy Gapon AcpiLeaveSleepStatePrep(state); 31140a15ff37SAndriy Gapon 3115f0a101b7SMitsuru IWASAKI if (sleep_result == -1) 3116a159c266SJung-uk Kim goto backout; 31176161544cSTakanori Watanabe 31186161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 3119be2b1797SNate Lawson if (state == ACPI_STATE_S4) 3120964679ceSMitsuru IWASAKI AcpiEnable(); 31216161544cSTakanori Watanabe } else { 31221df130f1SJung-uk Kim status = AcpiEnterSleepState(state); 312371804adcSJung-uk Kim intr_restore(intr); 31240a15ff37SAndriy Gapon AcpiLeaveSleepStatePrep(state); 3125be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 3126be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 3127be2b1797SNate Lawson AcpiFormatException(status)); 312833c70d41SAndriy Gapon goto backout; 312915e32d5dSMike Smith } 31306161544cSTakanori Watanabe } 313115e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 3132ece50487SMitsuru IWASAKI 313315e2f34fSNate Lawson /* 313415e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 313515e2f34fSNate Lawson * process. This handles both the error and success cases. 313615e2f34fSNate Lawson */ 313733c70d41SAndriy Gapon backout: 313827dca831SAndriy Gapon if (slp_state >= ACPI_SS_SLP_PREP) 313927dca831SAndriy Gapon resumeclock(); 314015e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 314115e2f34fSNate Lawson acpi_wake_prep_walk(state); 314215e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 314315e2f34fSNate Lawson } 3144e3b56b66SMitsuru IWASAKI if (slp_state >= ACPI_SS_DEV_SUSPEND) 3145e3b56b66SMitsuru IWASAKI DEVICE_RESUME(root_bus); 3146f0a101b7SMitsuru IWASAKI if (slp_state >= ACPI_SS_SLP_PREP) 314715e2f34fSNate Lawson AcpiLeaveSleepState(state); 3148a0a15716SJung-uk Kim if (slp_state >= ACPI_SS_SLEPT) { 3149279be68bSAndriy Gapon #if defined(__i386__) || defined(__amd64__) 3150279be68bSAndriy Gapon /* NB: we are still using ACPI timecounter at this point. */ 3151279be68bSAndriy Gapon resume_TSC(); 3152279be68bSAndriy Gapon #endif 3153a0a15716SJung-uk Kim acpi_resync_clock(sc); 315415e2f34fSNate Lawson acpi_enable_fixed_events(sc); 3155a0a15716SJung-uk Kim } 3156337744d9SJung-uk Kim sc->acpi_next_sstate = 0; 315715e2f34fSNate Lawson 3158a56fe095SJohn Baldwin mtx_unlock(&Giant); 3159c66d2b38SJung-uk Kim 3160fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 3161fdce57a0SJohn Baldwin thread_lock(curthread); 3162fdce57a0SJohn Baldwin sched_unbind(curthread); 3163fdce57a0SJohn Baldwin thread_unlock(curthread); 3164fdce57a0SJohn Baldwin #else 316536a483bbSJung-uk Kim if (smp_started) { 3166c66d2b38SJung-uk Kim thread_lock(curthread); 3167c66d2b38SJung-uk Kim sched_unbind(curthread); 3168c66d2b38SJung-uk Kim thread_unlock(curthread); 316936a483bbSJung-uk Kim } 3170fdce57a0SJohn Baldwin #endif 3171c66d2b38SJung-uk Kim 317285f95fffSAndriy Gapon resume_all_proc(); 317385f95fffSAndriy Gapon 31744a8fa6feSJung-uk Kim EVENTHANDLER_INVOKE(power_resume); 31754a8fa6feSJung-uk Kim 3176d42f0ad8SJung-uk Kim /* Allow another sleep request after a while. */ 3177fadf3fb9SJohn Baldwin callout_schedule(&acpi_sleep_timer, hz * ACPI_MINIMUM_AWAKETIME); 3178d42f0ad8SJung-uk Kim 3179d42f0ad8SJung-uk Kim /* Run /etc/rc.resume after we are back. */ 3180d42f0ad8SJung-uk Kim if (devctl_process_running()) 3181d42f0ad8SJung-uk Kim acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 3182d42f0ad8SJung-uk Kim 31830ae55423SMike Smith return_ACPI_STATUS (status); 318415e32d5dSMike Smith } 318515e32d5dSMike Smith 3186a0a15716SJung-uk Kim static void 31870755473bSJung-uk Kim acpi_resync_clock(struct acpi_softc *sc) 31880755473bSJung-uk Kim { 31890755473bSJung-uk Kim 31900755473bSJung-uk Kim /* 31910755473bSJung-uk Kim * Warm up timecounter again and reset system clock. 31920755473bSJung-uk Kim */ 31930755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 31940755473bSJung-uk Kim (void)timecounter->tc_get_timecount(timecounter); 31950755473bSJung-uk Kim inittodr(time_second + sc->acpi_sleep_delay); 31960755473bSJung-uk Kim } 31970755473bSJung-uk Kim 3198e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 3199e8b4d56eSNate Lawson int 3200e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 3201e8b4d56eSNate Lawson { 3202e8b4d56eSNate Lawson struct acpi_prw_data prw; 3203e8b4d56eSNate Lawson ACPI_STATUS status; 3204e8b4d56eSNate Lawson int flags; 3205e8b4d56eSNate Lawson 3206d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 32072be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 3208e8b4d56eSNate Lawson return (ENXIO); 3209e8b4d56eSNate Lawson 3210d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 3211e8b4d56eSNate Lawson if (enable) { 32125a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 32135a77b11bSJung-uk Kim ACPI_GPE_ENABLE); 3214e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 3215e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 3216e8b4d56eSNate Lawson return (ENXIO); 3217e8b4d56eSNate Lawson } 3218d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 3219e8b4d56eSNate Lawson } else { 32205a77b11bSJung-uk Kim status = AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, 32215a77b11bSJung-uk Kim ACPI_GPE_DISABLE); 3222e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 3223e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 3224e8b4d56eSNate Lawson return (ENXIO); 3225e8b4d56eSNate Lawson } 3226d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 3227e8b4d56eSNate Lawson } 3228e8b4d56eSNate Lawson 3229e8b4d56eSNate Lawson return (0); 3230e8b4d56eSNate Lawson } 3231e8b4d56eSNate Lawson 3232d4b9ff91SNate Lawson static int 3233d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 3234e8b4d56eSNate Lawson { 3235e8b4d56eSNate Lawson struct acpi_prw_data prw; 3236d4b9ff91SNate Lawson device_t dev; 3237e8b4d56eSNate Lawson 3238d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 3239e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 3240e8b4d56eSNate Lawson return (ENXIO); 3241d4b9ff91SNate Lawson dev = acpi_get_device(handle); 3242e8b4d56eSNate Lawson 3243e8b4d56eSNate Lawson /* 3244d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 3245d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 3246d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 3247d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 3248d4b9ff91SNate Lawson * and set _PSW. 3249e8b4d56eSNate Lawson */ 3250d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 32515a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_DISABLE); 3252e8b4d56eSNate Lawson if (bootverbose) 3253d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 3254d4b9ff91SNate Lawson acpi_name(handle), sstate); 3255d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 3256d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 3257d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 3258d4b9ff91SNate Lawson if (bootverbose) 3259d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 3260d4b9ff91SNate Lawson acpi_name(handle), sstate); 3261d4b9ff91SNate Lawson } 3262cc85c78cSNate Lawson 3263cc85c78cSNate Lawson return (0); 3264e8b4d56eSNate Lawson } 3265e8b4d56eSNate Lawson 3266d4b9ff91SNate Lawson static int 3267d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 3268cc85c78cSNate Lawson { 3269cc85c78cSNate Lawson struct acpi_prw_data prw; 3270d4b9ff91SNate Lawson device_t dev; 3271cc85c78cSNate Lawson 3272cc85c78cSNate Lawson /* 3273d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 3274d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 3275cc85c78cSNate Lawson */ 3276d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 3277d4b9ff91SNate Lawson return (ENXIO); 3278d4b9ff91SNate Lawson dev = acpi_get_device(handle); 3279d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 3280d4b9ff91SNate Lawson return (0); 3281cc85c78cSNate Lawson 3282d4b9ff91SNate Lawson /* 3283d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 3284d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 3285d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 3286d4b9ff91SNate Lawson */ 3287d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 32885a77b11bSJung-uk Kim AcpiSetGpeWakeMask(prw.gpe_handle, prw.gpe_bit, ACPI_GPE_ENABLE); 3289d4b9ff91SNate Lawson if (bootverbose) 3290d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 3291d4b9ff91SNate Lawson } else { 3292d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 3293d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 3294d4b9ff91SNate Lawson if (bootverbose) 3295d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 3296d4b9ff91SNate Lawson acpi_name(handle)); 3297d4b9ff91SNate Lawson } 3298d4b9ff91SNate Lawson 3299e8b4d56eSNate Lawson return (0); 3300e8b4d56eSNate Lawson } 3301e8b4d56eSNate Lawson 3302e8b4d56eSNate Lawson static ACPI_STATUS 3303d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 3304e8b4d56eSNate Lawson { 3305d4b9ff91SNate Lawson int sstate; 3306e8b4d56eSNate Lawson 3307d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 3308d4b9ff91SNate Lawson sstate = *(int *)context; 3309d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 3310d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 3311d4b9ff91SNate Lawson else 3312d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 3313e8b4d56eSNate Lawson return (AE_OK); 3314e8b4d56eSNate Lawson } 3315e8b4d56eSNate Lawson 3316d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 3317e8b4d56eSNate Lawson static int 3318d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 3319e8b4d56eSNate Lawson { 3320e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 3321e8b4d56eSNate Lawson 3322e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 3323d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 33242272d050SJung-uk Kim acpi_wake_prep, NULL, &sstate, NULL); 3325e8b4d56eSNate Lawson return (0); 3326e8b4d56eSNate Lawson } 3327e8b4d56eSNate Lawson 332888a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 332988a79fc0SNate Lawson static int 333088a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 333188a79fc0SNate Lawson { 333288a79fc0SNate Lawson int error, i, numdevs; 333388a79fc0SNate Lawson device_t *devlist; 333488a79fc0SNate Lawson device_t child; 3335d4b9ff91SNate Lawson ACPI_STATUS status; 333688a79fc0SNate Lawson 333788a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 33381c9ec538SNate Lawson if (error != 0 || numdevs == 0) { 33391c9ec538SNate Lawson if (numdevs == 0) 33401c9ec538SNate Lawson free(devlist, M_TEMP); 334188a79fc0SNate Lawson return (error); 33421c9ec538SNate Lawson } 334388a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 334488a79fc0SNate Lawson child = devlist[i]; 3345d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 334688a79fc0SNate Lawson if (!device_is_attached(child)) 334788a79fc0SNate Lawson continue; 3348d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 3349d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 335088a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 335188a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 335288a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 335388a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 335488a79fc0SNate Lawson } 335588a79fc0SNate Lawson } 335688a79fc0SNate Lawson free(devlist, M_TEMP); 335788a79fc0SNate Lawson 335888a79fc0SNate Lawson return (0); 335988a79fc0SNate Lawson } 336088a79fc0SNate Lawson 336188a79fc0SNate Lawson /* Enable or disable wake from userland. */ 336288a79fc0SNate Lawson static int 336388a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 336488a79fc0SNate Lawson { 336588a79fc0SNate Lawson int enable, error; 336688a79fc0SNate Lawson device_t dev; 336788a79fc0SNate Lawson 336888a79fc0SNate Lawson dev = (device_t)arg1; 3369d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 337088a79fc0SNate Lawson 337188a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 337288a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 337388a79fc0SNate Lawson return (error); 337488a79fc0SNate Lawson if (enable != 0 && enable != 1) 337588a79fc0SNate Lawson return (EINVAL); 337688a79fc0SNate Lawson 337788a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 337888a79fc0SNate Lawson } 337988a79fc0SNate Lawson 3380e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 3381d4b9ff91SNate Lawson int 3382e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 3383e8b4d56eSNate Lawson { 3384e8b4d56eSNate Lawson ACPI_STATUS status; 3385e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 3386e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 3387d4b9ff91SNate Lawson int error, i, power_count; 3388e8b4d56eSNate Lawson 3389e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 3390e8b4d56eSNate Lawson return (EINVAL); 3391e8b4d56eSNate Lawson 3392e8b4d56eSNate Lawson /* 3393e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 3394e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 3395e8b4d56eSNate Lawson */ 3396e8b4d56eSNate Lawson error = EINVAL; 3397e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 3398e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 3399e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 3400e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 3401e8b4d56eSNate Lawson return (ENOENT); 3402e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 3403e8b4d56eSNate Lawson if (res == NULL) 3404e8b4d56eSNate Lawson return (ENOENT); 3405e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 3406e8b4d56eSNate Lawson goto out; 3407e8b4d56eSNate Lawson 3408e8b4d56eSNate Lawson /* 3409e8b4d56eSNate Lawson * Element 1 of the _PRW object: 3410e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 3411e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 3412e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 3413e8b4d56eSNate Lawson */ 3414e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 3415e8b4d56eSNate Lawson goto out; 3416e8b4d56eSNate Lawson 3417e8b4d56eSNate Lawson /* 3418e8b4d56eSNate Lawson * Element 0 of the _PRW object: 3419e8b4d56eSNate Lawson */ 3420e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 3421e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 3422e8b4d56eSNate Lawson /* 3423e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 3424e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 3425e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 3426e8b4d56eSNate Lawson * enabled for the wake event. 3427e8b4d56eSNate Lawson */ 3428e8b4d56eSNate Lawson prw->gpe_handle = NULL; 3429e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 3430e8b4d56eSNate Lawson error = 0; 3431e8b4d56eSNate Lawson break; 3432e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 3433e8b4d56eSNate Lawson /* 3434e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 3435e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 3436e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 3437e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 3438e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 3439e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 3440e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 3441e8b4d56eSNate Lawson * the wake event. 3442e8b4d56eSNate Lawson * 3443e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 3444e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 3445e8b4d56eSNate Lawson */ 3446e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 3447e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 3448e8b4d56eSNate Lawson goto out; 3449e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 3450e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 3451e8b4d56eSNate Lawson goto out; 3452e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 3453e8b4d56eSNate Lawson goto out; 3454e8b4d56eSNate Lawson error = 0; 3455e8b4d56eSNate Lawson break; 3456e8b4d56eSNate Lawson default: 3457e8b4d56eSNate Lawson goto out; 3458e8b4d56eSNate Lawson } 3459e8b4d56eSNate Lawson 3460d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 3461d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 3462d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 3463d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 3464d4b9ff91SNate Lawson power_count = 0; 3465d4b9ff91SNate Lawson } 3466d4b9ff91SNate Lawson prw->power_res_count = power_count; 3467d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 3468d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 3469e8b4d56eSNate Lawson 3470e8b4d56eSNate Lawson out: 3471e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 3472e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 3473e8b4d56eSNate Lawson return (error); 3474e8b4d56eSNate Lawson } 3475e8b4d56eSNate Lawson 347615e32d5dSMike Smith /* 347715e32d5dSMike Smith * ACPI Event Handlers 347815e32d5dSMike Smith */ 347915e32d5dSMike Smith 348015e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 348115e32d5dSMike Smith 348215e32d5dSMike Smith static void 348315e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 348415e32d5dSMike Smith { 34852d0c82e8SJung-uk Kim struct acpi_softc *sc = (struct acpi_softc *)arg; 348600a30448SNate Lawson int ret; 3487bee4aa4aSNate Lawson 3488b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 348915e32d5dSMike Smith 3490a0e73a12SJung-uk Kim /* Check if button action is disabled or unknown. */ 3491a0e73a12SJung-uk Kim if (state == ACPI_STATE_UNKNOWN) 3492813d6dcaSNate Lawson return; 3493813d6dcaSNate Lawson 349400a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */ 34952d0c82e8SJung-uk Kim ret = acpi_ReqSleepState(sc, state); 349600a30448SNate Lawson if (ret != 0) 34972d0c82e8SJung-uk Kim device_printf(sc->acpi_dev, 34982d0c82e8SJung-uk Kim "request to enter state S%d failed (err %d)\n", state, ret); 3499bee4aa4aSNate Lawson 35000ae55423SMike Smith return_VOID; 350115e32d5dSMike Smith } 350215e32d5dSMike Smith 350315e32d5dSMike Smith static void 350415e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 350515e32d5dSMike Smith { 3506bee4aa4aSNate Lawson 3507b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 35080ae55423SMike Smith 3509bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 3510cb9b0d80SMike Smith 35110ae55423SMike Smith return_VOID; 351215e32d5dSMike Smith } 351315e32d5dSMike Smith 351415e32d5dSMike Smith /* 351515e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 351615e32d5dSMike Smith */ 3517b5854f5fSJung-uk Kim static void 3518b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler(void *context) 3519b5854f5fSJung-uk Kim { 3520b5854f5fSJung-uk Kim 3521b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_sleep_event, *(int *)context); 3522b5854f5fSJung-uk Kim } 3523b5854f5fSJung-uk Kim 3524b5854f5fSJung-uk Kim static void 3525b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler(void *context) 3526b5854f5fSJung-uk Kim { 3527b5854f5fSJung-uk Kim 3528b5854f5fSJung-uk Kim EVENTHANDLER_INVOKE(acpi_wakeup_event, *(int *)context); 3529b5854f5fSJung-uk Kim } 3530b5854f5fSJung-uk Kim 353115e32d5dSMike Smith UINT32 353233febf93SNate Lawson acpi_event_power_button_sleep(void *context) 353315e32d5dSMike Smith { 353415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 353515e32d5dSMike Smith 3536b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 35370ae55423SMike Smith 3538b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3539b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_power_button_sx))) 3540b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3541b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 354215e32d5dSMike Smith } 354315e32d5dSMike Smith 354415e32d5dSMike Smith UINT32 354533febf93SNate Lawson acpi_event_power_button_wake(void *context) 354615e32d5dSMike Smith { 354715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 354815e32d5dSMike Smith 3549b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 35500ae55423SMike Smith 3551b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3552b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_power_button_sx))) 3553b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3554b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 355515e32d5dSMike Smith } 355615e32d5dSMike Smith 355715e32d5dSMike Smith UINT32 355833febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 355915e32d5dSMike Smith { 356015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 356115e32d5dSMike Smith 3562b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 35630ae55423SMike Smith 3564b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3565b5854f5fSJung-uk Kim acpi_invoke_sleep_eventhandler, &sc->acpi_sleep_button_sx))) 3566b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3567b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 356815e32d5dSMike Smith } 356915e32d5dSMike Smith 357015e32d5dSMike Smith UINT32 357133febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 357215e32d5dSMike Smith { 357315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 357415e32d5dSMike Smith 3575b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 35760ae55423SMike Smith 3577b5854f5fSJung-uk Kim if (ACPI_FAILURE(AcpiOsExecute(OSL_NOTIFY_HANDLER, 3578b5854f5fSJung-uk Kim acpi_invoke_wake_eventhandler, &sc->acpi_sleep_button_sx))) 3579b5854f5fSJung-uk Kim return_VALUE (ACPI_INTERRUPT_NOT_HANDLED); 3580b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 358115e32d5dSMike Smith } 358215e32d5dSMike Smith 358315e32d5dSMike Smith /* 3584bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 3585bee4aa4aSNate Lawson * use this for single-threaded callers. 358615e32d5dSMike Smith */ 358715e32d5dSMike Smith char * 358815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 358915e32d5dSMike Smith { 3590bee4aa4aSNate Lawson ACPI_BUFFER buf; 3591bee4aa4aSNate Lawson static char data[256]; 359215e32d5dSMike Smith 3593bee4aa4aSNate Lawson buf.Length = sizeof(data); 3594bee4aa4aSNate Lawson buf.Pointer = data; 3595cb9b0d80SMike Smith 35960a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 3597bee4aa4aSNate Lawson return (data); 3598bee4aa4aSNate Lawson return ("(unknown)"); 359915e32d5dSMike Smith } 360015e32d5dSMike Smith 360115e32d5dSMike Smith /* 360215e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 360315e32d5dSMike Smith * parts of the namespace. 360415e32d5dSMike Smith */ 360515e32d5dSMike Smith int 360615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 360715e32d5dSMike Smith { 36083c600cfbSJohn Baldwin char *cp, *env, *np; 360915e32d5dSMike Smith int len; 361015e32d5dSMike Smith 361115e32d5dSMike Smith np = acpi_name(handle); 361215e32d5dSMike Smith if (*np == '\\') 361315e32d5dSMike Smith np++; 36142be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.avoid")) == NULL) 361515e32d5dSMike Smith return (0); 361615e32d5dSMike Smith 3617be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 36183c600cfbSJohn Baldwin cp = env; 361915e32d5dSMike Smith for (;;) { 3620bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 362115e32d5dSMike Smith cp++; 362215e32d5dSMike Smith if (*cp == 0) 362315e32d5dSMike Smith break; 362415e32d5dSMike Smith len = 0; 3625bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 362615e32d5dSMike Smith len++; 3627d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 36283c600cfbSJohn Baldwin freeenv(env); 36290ae55423SMike Smith return(1); 3630d786139cSMaxime Henrion } 36310ae55423SMike Smith cp += len; 36320ae55423SMike Smith } 36333c600cfbSJohn Baldwin freeenv(env); 3634be2b1797SNate Lawson 36350ae55423SMike Smith return (0); 36360ae55423SMike Smith } 36370ae55423SMike Smith 36380ae55423SMike Smith /* 36390ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 36400ae55423SMike Smith */ 36410ae55423SMike Smith int 36420ae55423SMike Smith acpi_disabled(char *subsys) 36430ae55423SMike Smith { 364478689b15SMaxime Henrion char *cp, *env; 36450ae55423SMike Smith int len; 36460ae55423SMike Smith 36472be111bfSDavide Italiano if ((env = kern_getenv("debug.acpi.disabled")) == NULL) 36480ae55423SMike Smith return (0); 36493184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 365078689b15SMaxime Henrion freeenv(env); 36510ae55423SMike Smith return (1); 3652d786139cSMaxime Henrion } 36530ae55423SMike Smith 36543184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 365578689b15SMaxime Henrion cp = env; 36560ae55423SMike Smith for (;;) { 36573184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 36580ae55423SMike Smith cp++; 36593184cf5aSNate Lawson if (*cp == '\0') 36600ae55423SMike Smith break; 36610ae55423SMike Smith len = 0; 36623184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 36630ae55423SMike Smith len++; 36643184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 366578689b15SMaxime Henrion freeenv(env); 366615e32d5dSMike Smith return (1); 3667d786139cSMaxime Henrion } 366815e32d5dSMike Smith cp += len; 366915e32d5dSMike Smith } 367078689b15SMaxime Henrion freeenv(env); 3671be2b1797SNate Lawson 367215e32d5dSMike Smith return (0); 367315e32d5dSMike Smith } 367415e32d5dSMike Smith 367564de8019SJohn Baldwin static void 367664de8019SJohn Baldwin acpi_lookup(void *arg, const char *name, device_t *dev) 367764de8019SJohn Baldwin { 367864de8019SJohn Baldwin ACPI_HANDLE handle; 367964de8019SJohn Baldwin 368064de8019SJohn Baldwin if (*dev != NULL) 368164de8019SJohn Baldwin return; 368264de8019SJohn Baldwin 368364de8019SJohn Baldwin /* 368464de8019SJohn Baldwin * Allow any handle name that is specified as an absolute path and 368564de8019SJohn Baldwin * starts with '\'. We could restrict this to \_SB and friends, 368664de8019SJohn Baldwin * but see acpi_probe_children() for notes on why we scan the entire 368764de8019SJohn Baldwin * namespace for devices. 368864de8019SJohn Baldwin * 368964de8019SJohn Baldwin * XXX: The pathname argument to AcpiGetHandle() should be fixed to 369064de8019SJohn Baldwin * be const. 369164de8019SJohn Baldwin */ 369264de8019SJohn Baldwin if (name[0] != '\\') 369364de8019SJohn Baldwin return; 369464de8019SJohn Baldwin if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, __DECONST(char *, name), 369564de8019SJohn Baldwin &handle))) 369664de8019SJohn Baldwin return; 369764de8019SJohn Baldwin *dev = acpi_get_device(handle); 369864de8019SJohn Baldwin } 369964de8019SJohn Baldwin 370015e32d5dSMike Smith /* 370115e32d5dSMike Smith * Control interface. 370215e32d5dSMike Smith * 37030ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 3704be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 3705be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 370615e32d5dSMike Smith */ 37070ae55423SMike Smith struct acpi_ioctl_hook 37080ae55423SMike Smith { 37090ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 37100ae55423SMike Smith u_long cmd; 3711be2b1797SNate Lawson acpi_ioctl_fn fn; 37120ae55423SMike Smith void *arg; 37130ae55423SMike Smith }; 37140ae55423SMike Smith 37150ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 37160ae55423SMike Smith static int acpi_ioctl_hooks_initted; 37170ae55423SMike Smith 37180ae55423SMike Smith int 3719be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 37200ae55423SMike Smith { 37210ae55423SMike Smith struct acpi_ioctl_hook *hp; 37220ae55423SMike Smith 37230ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 37240ae55423SMike Smith return (ENOMEM); 37250ae55423SMike Smith hp->cmd = cmd; 37260ae55423SMike Smith hp->fn = fn; 37270ae55423SMike Smith hp->arg = arg; 372815e2f34fSNate Lawson 372915e2f34fSNate Lawson ACPI_LOCK(acpi); 37300ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 37310ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 37320ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 37330ae55423SMike Smith } 37340ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 373515e2f34fSNate Lawson ACPI_UNLOCK(acpi); 373615e2f34fSNate Lawson 37370ae55423SMike Smith return (0); 37380ae55423SMike Smith } 37390ae55423SMike Smith 37400ae55423SMike Smith void 3741be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 37420ae55423SMike Smith { 37430ae55423SMike Smith struct acpi_ioctl_hook *hp; 37440ae55423SMike Smith 374515e2f34fSNate Lawson ACPI_LOCK(acpi); 37460ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 374715e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 37480ae55423SMike Smith break; 37490ae55423SMike Smith 37500ae55423SMike Smith if (hp != NULL) { 37510ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 37520ae55423SMike Smith free(hp, M_ACPIDEV); 37530ae55423SMike Smith } 375415e2f34fSNate Lawson ACPI_UNLOCK(acpi); 37550ae55423SMike Smith } 37560ae55423SMike Smith 375715e32d5dSMike Smith static int 375800b4e54aSWarner Losh acpiopen(struct cdev *dev, int flag, int fmt, struct thread *td) 375915e32d5dSMike Smith { 376015e32d5dSMike Smith return (0); 376115e32d5dSMike Smith } 376215e32d5dSMike Smith 376315e32d5dSMike Smith static int 376400b4e54aSWarner Losh acpiclose(struct cdev *dev, int flag, int fmt, struct thread *td) 376515e32d5dSMike Smith { 376615e32d5dSMike Smith return (0); 376715e32d5dSMike Smith } 376815e32d5dSMike Smith 376915e32d5dSMike Smith static int 377000b4e54aSWarner Losh acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 377115e32d5dSMike Smith { 377215e32d5dSMike Smith struct acpi_softc *sc; 37730ae55423SMike Smith struct acpi_ioctl_hook *hp; 3774bee4aa4aSNate Lawson int error, state; 377515e32d5dSMike Smith 3776a2e35898SDavid E. O'Brien error = 0; 3777a2e35898SDavid E. O'Brien hp = NULL; 377815e32d5dSMike Smith sc = dev->si_drv1; 377915e32d5dSMike Smith 37800ae55423SMike Smith /* 37810ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 37820ae55423SMike Smith */ 378315e2f34fSNate Lawson ACPI_LOCK(acpi); 3784bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 37850ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3786bee4aa4aSNate Lawson if (hp->cmd == cmd) 3787bee4aa4aSNate Lawson break; 37880ae55423SMike Smith } 378915e2f34fSNate Lawson ACPI_UNLOCK(acpi); 3790bee4aa4aSNate Lawson if (hp) 3791bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 37920ae55423SMike Smith 37930ae55423SMike Smith /* 3794a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 3795a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 3796a89fcf28STakanori Watanabe * Not changing system behavior. 3797a89fcf28STakanori Watanabe */ 3798be2b1797SNate Lawson if ((flag & FWRITE) == 0) 3799be2b1797SNate Lawson return (EPERM); 3800a89fcf28STakanori Watanabe 3801be2b1797SNate Lawson /* Core system ioctls. */ 380215e32d5dSMike Smith switch (cmd) { 380300a30448SNate Lawson case ACPIIO_REQSLPSTATE: 380400a30448SNate Lawson state = *(int *)addr; 380500a30448SNate Lawson if (state != ACPI_STATE_S5) 3806a0e73a12SJung-uk Kim return (acpi_ReqSleepState(sc, state)); 3807a0e73a12SJung-uk Kim device_printf(sc->acpi_dev, "power off via acpi ioctl not supported\n"); 3808a0e73a12SJung-uk Kim error = EOPNOTSUPP; 380900a30448SNate Lawson break; 381000a30448SNate Lawson case ACPIIO_ACKSLPSTATE: 381100a30448SNate Lawson error = *(int *)addr; 381200a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error); 381300a30448SNate Lawson break; 381400a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 381515e32d5dSMike Smith state = *(int *)addr; 3816a0e73a12SJung-uk Kim if (state < ACPI_STATE_S0 || state > ACPI_S_STATES_MAX) 3817a0e73a12SJung-uk Kim return (EINVAL); 3818a0e73a12SJung-uk Kim if (!acpi_sleep_states[state]) 3819a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3820a0e73a12SJung-uk Kim if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 3821a0e73a12SJung-uk Kim error = ENXIO; 382215e32d5dSMike Smith break; 382315e32d5dSMike Smith default: 3824bee4aa4aSNate Lawson error = ENXIO; 382515e32d5dSMike Smith break; 382615e32d5dSMike Smith } 3827917d44c8SMitsuru IWASAKI 382815e32d5dSMike Smith return (error); 382915e32d5dSMike Smith } 383015e32d5dSMike Smith 38311d073b1dSJohn Baldwin static int 3832a0e73a12SJung-uk Kim acpi_sname2sstate(const char *sname) 3833a0e73a12SJung-uk Kim { 3834a0e73a12SJung-uk Kim int sstate; 3835a0e73a12SJung-uk Kim 3836a0e73a12SJung-uk Kim if (toupper(sname[0]) == 'S') { 3837a0e73a12SJung-uk Kim sstate = sname[1] - '0'; 3838a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5 && 3839a0e73a12SJung-uk Kim sname[2] == '\0') 3840a0e73a12SJung-uk Kim return (sstate); 3841a0e73a12SJung-uk Kim } else if (strcasecmp(sname, "NONE") == 0) 3842a0e73a12SJung-uk Kim return (ACPI_STATE_UNKNOWN); 3843a0e73a12SJung-uk Kim return (-1); 3844a0e73a12SJung-uk Kim } 3845a0e73a12SJung-uk Kim 3846a0e73a12SJung-uk Kim static const char * 3847a0e73a12SJung-uk Kim acpi_sstate2sname(int sstate) 3848a0e73a12SJung-uk Kim { 3849a0e73a12SJung-uk Kim static const char *snames[] = { "S0", "S1", "S2", "S3", "S4", "S5" }; 3850a0e73a12SJung-uk Kim 3851a0e73a12SJung-uk Kim if (sstate >= ACPI_STATE_S0 && sstate <= ACPI_STATE_S5) 3852a0e73a12SJung-uk Kim return (snames[sstate]); 3853a0e73a12SJung-uk Kim else if (sstate == ACPI_STATE_UNKNOWN) 3854a0e73a12SJung-uk Kim return ("NONE"); 3855a0e73a12SJung-uk Kim return (NULL); 3856a0e73a12SJung-uk Kim } 3857a0e73a12SJung-uk Kim 3858a0e73a12SJung-uk Kim static int 3859d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3860d75de536SMitsuru IWASAKI { 3861d75de536SMitsuru IWASAKI int error; 3862bee4aa4aSNate Lawson struct sbuf sb; 3863a0e73a12SJung-uk Kim UINT8 state; 3864d75de536SMitsuru IWASAKI 3865bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3866a0e73a12SJung-uk Kim for (state = ACPI_STATE_S1; state < ACPI_S_STATE_COUNT; state++) 3867a0e73a12SJung-uk Kim if (acpi_sleep_states[state]) 3868a0e73a12SJung-uk Kim sbuf_printf(&sb, "%s ", acpi_sstate2sname(state)); 3869bee4aa4aSNate Lawson sbuf_trim(&sb); 3870bee4aa4aSNate Lawson sbuf_finish(&sb); 3871bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3872bee4aa4aSNate Lawson sbuf_delete(&sb); 3873d75de536SMitsuru IWASAKI return (error); 3874d75de536SMitsuru IWASAKI } 3875d75de536SMitsuru IWASAKI 3876d75de536SMitsuru IWASAKI static int 38771d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 38781d073b1dSJohn Baldwin { 38791d073b1dSJohn Baldwin char sleep_state[10]; 3880a0e73a12SJung-uk Kim int error, new_state, old_state; 38811d073b1dSJohn Baldwin 3882a0e73a12SJung-uk Kim old_state = *(int *)oidp->oid_arg1; 3883a0e73a12SJung-uk Kim strlcpy(sleep_state, acpi_sstate2sname(old_state), sizeof(sleep_state)); 38841d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 38851d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 3886a0e73a12SJung-uk Kim new_state = acpi_sname2sstate(sleep_state); 3887a0e73a12SJung-uk Kim if (new_state < ACPI_STATE_S1) 3888a0e73a12SJung-uk Kim return (EINVAL); 388954b43614SJung-uk Kim if (new_state < ACPI_S_STATE_COUNT && !acpi_sleep_states[new_state]) 3890a0e73a12SJung-uk Kim return (EOPNOTSUPP); 3891be2b1797SNate Lawson if (new_state != old_state) 3892a0e73a12SJung-uk Kim *(int *)oidp->oid_arg1 = new_state; 38931d073b1dSJohn Baldwin } 38941d073b1dSJohn Baldwin return (error); 38951d073b1dSJohn Baldwin } 38961d073b1dSJohn Baldwin 38979b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 38989b937d48SNate Lawson void 38999b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 39009b937d48SNate Lawson { 39019b937d48SNate Lawson char notify_buf[16]; 39029b937d48SNate Lawson ACPI_BUFFER handle_buf; 39039b937d48SNate Lawson ACPI_STATUS status; 39049b937d48SNate Lawson 39059b937d48SNate Lawson if (subsystem == NULL) 39069b937d48SNate Lawson return; 39079b937d48SNate Lawson 39089b937d48SNate Lawson handle_buf.Pointer = NULL; 39099b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 39100594dadeSJung-uk Kim status = AcpiNsHandleToPathname(h, &handle_buf, FALSE); 39119b937d48SNate Lawson if (ACPI_FAILURE(status)) 39129b937d48SNate Lawson return; 39139b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 39149b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 39159b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 39169b937d48SNate Lawson } 39179b937d48SNate Lawson 391815e32d5dSMike Smith #ifdef ACPI_DEBUG 39190ae55423SMike Smith /* 39200ae55423SMike Smith * Support for parsing debug options from the kernel environment. 39210ae55423SMike Smith * 39220ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 39230ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 39240ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 39250ae55423SMike Smith * prefixing the bit name with !. 39260ae55423SMike Smith */ 392715e32d5dSMike Smith struct debugtag 392815e32d5dSMike Smith { 392915e32d5dSMike Smith char *name; 393015e32d5dSMike Smith UINT32 value; 393115e32d5dSMike Smith }; 393215e32d5dSMike Smith 393315e32d5dSMike Smith static struct debugtag dbg_layer[] = { 3934c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 3935c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 3936c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 3937c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 3938c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3939c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 3940c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3941c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 3942c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 3943d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 39444c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3945d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3946297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 39474c1cdee6SMike Smith 3948c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3949c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 39503184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 3951c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 39523184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 39533184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 39543184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 39554c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3956cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 39573184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 3958b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 395915e32d5dSMike Smith {NULL, 0} 396015e32d5dSMike Smith }; 396115e32d5dSMike Smith 396215e32d5dSMike Smith static struct debugtag dbg_level[] = { 39639501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 39644c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 39659501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 3966cc6455afSJung-uk Kim {"ACPI_LV_REPAIR", ACPI_LV_REPAIR}, 39674c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3968d62ab2f4SMitsuru IWASAKI 3969d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 3970297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 39714c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 39724c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3973d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 39744c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 39754c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 39764c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 39774c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 39784c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 39794c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 39804c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 39814c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 39824c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 39834c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3984d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3985d62ab2f4SMitsuru IWASAKI 3986d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3987d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3988d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3989d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3990d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 39914c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 3992d62ab2f4SMitsuru IWASAKI 3993d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3994d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3995d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3996d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 3997d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3998d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3999d62ab2f4SMitsuru IWASAKI 4000d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 400198479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 400298479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 400398479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 400498479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 400598479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 400615e32d5dSMike Smith {NULL, 0} 400715e32d5dSMike Smith }; 400815e32d5dSMike Smith 400915e32d5dSMike Smith static void 401015e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 401115e32d5dSMike Smith { 401215e32d5dSMike Smith char *ep; 401315e32d5dSMike Smith int i, l; 40140ae55423SMike Smith int set; 401515e32d5dSMike Smith 401615e32d5dSMike Smith while (*cp) { 401715e32d5dSMike Smith if (isspace(*cp)) { 401815e32d5dSMike Smith cp++; 401915e32d5dSMike Smith continue; 402015e32d5dSMike Smith } 402115e32d5dSMike Smith ep = cp; 402215e32d5dSMike Smith while (*ep && !isspace(*ep)) 402315e32d5dSMike Smith ep++; 40240ae55423SMike Smith if (*cp == '!') { 40250ae55423SMike Smith set = 0; 40260ae55423SMike Smith cp++; 40270ae55423SMike Smith if (cp == ep) 40280ae55423SMike Smith continue; 40290ae55423SMike Smith } else { 40300ae55423SMike Smith set = 1; 40310ae55423SMike Smith } 403215e32d5dSMike Smith l = ep - cp; 403315e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 403415e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 4035be2b1797SNate Lawson if (set) 403615e32d5dSMike Smith *flag |= tag[i].value; 4037be2b1797SNate Lawson else 40380ae55423SMike Smith *flag &= ~tag[i].value; 403915e32d5dSMike Smith } 404015e32d5dSMike Smith } 404115e32d5dSMike Smith cp = ep; 404215e32d5dSMike Smith } 404315e32d5dSMike Smith } 404415e32d5dSMike Smith 404515e32d5dSMike Smith static void 40462a4ac806SMike Smith acpi_set_debugging(void *junk) 404715e32d5dSMike Smith { 40487e639165SNate Lawson char *layer, *level; 404915e32d5dSMike Smith 40501d7b121cSNate Lawson if (cold) { 40510ae55423SMike Smith AcpiDbgLayer = 0; 40520ae55423SMike Smith AcpiDbgLevel = 0; 40531d7b121cSNate Lawson } 40541d7b121cSNate Lawson 40552be111bfSDavide Italiano layer = kern_getenv("debug.acpi.layer"); 40562be111bfSDavide Italiano level = kern_getenv("debug.acpi.level"); 40577e639165SNate Lawson if (layer == NULL && level == NULL) 40587e639165SNate Lawson return; 40590ae55423SMike Smith 40607e639165SNate Lawson printf("ACPI set debug"); 40617e639165SNate Lawson if (layer != NULL) { 40627e639165SNate Lawson if (strcmp("NONE", layer) != 0) 40637e639165SNate Lawson printf(" layer '%s'", layer); 40647e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 40657e639165SNate Lawson freeenv(layer); 40661d7b121cSNate Lawson } 40677e639165SNate Lawson if (level != NULL) { 40687e639165SNate Lawson if (strcmp("NONE", level) != 0) 40697e639165SNate Lawson printf(" level '%s'", level); 40707e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 40717e639165SNate Lawson freeenv(level); 40727e639165SNate Lawson } 40737e639165SNate Lawson printf("\n"); 407415e32d5dSMike Smith } 4075bee4aa4aSNate Lawson 4076be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 4077be2b1797SNate Lawson NULL); 40781d7b121cSNate Lawson 40791d7b121cSNate Lawson static int 40801d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 40811d7b121cSNate Lawson { 408254f6bca0SNate Lawson int error, *dbg; 40831d7b121cSNate Lawson struct debugtag *tag; 408454f6bca0SNate Lawson struct sbuf sb; 40850e1152fcSHans Petter Selasky char temp[128]; 40861d7b121cSNate Lawson 408754f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 408854f6bca0SNate Lawson return (ENOMEM); 40891d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 40901d7b121cSNate Lawson tag = &dbg_layer[0]; 40911d7b121cSNate Lawson dbg = &AcpiDbgLayer; 40921d7b121cSNate Lawson } else { 40931d7b121cSNate Lawson tag = &dbg_level[0]; 40941d7b121cSNate Lawson dbg = &AcpiDbgLevel; 40951d7b121cSNate Lawson } 40961d7b121cSNate Lawson 40971d7b121cSNate Lawson /* Get old values if this is a get request. */ 409815e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 40991d7b121cSNate Lawson if (*dbg == 0) { 410054f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 41011d7b121cSNate Lawson } else if (req->newptr == NULL) { 41021d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 410354f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 410454f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 41051d7b121cSNate Lawson } 41061d7b121cSNate Lawson } 410754f6bca0SNate Lawson sbuf_trim(&sb); 410854f6bca0SNate Lawson sbuf_finish(&sb); 41090e1152fcSHans Petter Selasky strlcpy(temp, sbuf_data(&sb), sizeof(temp)); 411054f6bca0SNate Lawson sbuf_delete(&sb); 41111d7b121cSNate Lawson 41120e1152fcSHans Petter Selasky error = sysctl_handle_string(oidp, temp, sizeof(temp), req); 41130e1152fcSHans Petter Selasky 41140e1152fcSHans Petter Selasky /* Check for error or no change */ 41151d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 41161d7b121cSNate Lawson *dbg = 0; 41170e1152fcSHans Petter Selasky kern_setenv((char *)oidp->oid_arg1, temp); 41181d7b121cSNate Lawson acpi_set_debugging(NULL); 41191d7b121cSNate Lawson } 412015e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 41211d7b121cSNate Lawson 41221d7b121cSNate Lawson return (error); 41231d7b121cSNate Lawson } 4124bee4aa4aSNate Lawson 41251d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 41261d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 41271d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 41281d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 4129bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 4130f9390180SMitsuru IWASAKI 4131f9390180SMitsuru IWASAKI static int 41322a18c71dSJung-uk Kim acpi_debug_objects_sysctl(SYSCTL_HANDLER_ARGS) 41332a18c71dSJung-uk Kim { 41342a18c71dSJung-uk Kim int error; 41352a18c71dSJung-uk Kim int old; 41362a18c71dSJung-uk Kim 41372a18c71dSJung-uk Kim old = acpi_debug_objects; 41382a18c71dSJung-uk Kim error = sysctl_handle_int(oidp, &acpi_debug_objects, 0, req); 41392a18c71dSJung-uk Kim if (error != 0 || req->newptr == NULL) 41402a18c71dSJung-uk Kim return (error); 41412a18c71dSJung-uk Kim if (old == acpi_debug_objects || (old && acpi_debug_objects)) 41422a18c71dSJung-uk Kim return (0); 41432a18c71dSJung-uk Kim 41442a18c71dSJung-uk Kim ACPI_SERIAL_BEGIN(acpi); 41452a18c71dSJung-uk Kim AcpiGbl_EnableAmlDebugObject = acpi_debug_objects ? TRUE : FALSE; 41462a18c71dSJung-uk Kim ACPI_SERIAL_END(acpi); 41472a18c71dSJung-uk Kim 41482a18c71dSJung-uk Kim return (0); 41492a18c71dSJung-uk Kim } 41502a18c71dSJung-uk Kim 41512a18c71dSJung-uk Kim static int 4152ae19af49SJung-uk Kim acpi_parse_interfaces(char *str, struct acpi_interface *iface) 4153ae19af49SJung-uk Kim { 4154ae19af49SJung-uk Kim char *p; 4155ae19af49SJung-uk Kim size_t len; 4156ae19af49SJung-uk Kim int i, j; 4157ae19af49SJung-uk Kim 4158ae19af49SJung-uk Kim p = str; 4159ae19af49SJung-uk Kim while (isspace(*p) || *p == ',') 4160ae19af49SJung-uk Kim p++; 4161ae19af49SJung-uk Kim len = strlen(p); 4162ae19af49SJung-uk Kim if (len == 0) 4163ae19af49SJung-uk Kim return (0); 4164ae19af49SJung-uk Kim p = strdup(p, M_TEMP); 4165ae19af49SJung-uk Kim for (i = 0; i < len; i++) 4166ae19af49SJung-uk Kim if (p[i] == ',') 4167ae19af49SJung-uk Kim p[i] = '\0'; 4168ae19af49SJung-uk Kim i = j = 0; 4169ae19af49SJung-uk Kim while (i < len) 4170ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 4171ae19af49SJung-uk Kim i++; 4172ae19af49SJung-uk Kim else { 4173ae19af49SJung-uk Kim i += strlen(p + i) + 1; 4174ae19af49SJung-uk Kim j++; 4175ae19af49SJung-uk Kim } 4176ae19af49SJung-uk Kim if (j == 0) { 4177ae19af49SJung-uk Kim free(p, M_TEMP); 4178ae19af49SJung-uk Kim return (0); 4179ae19af49SJung-uk Kim } 4180ae19af49SJung-uk Kim iface->data = malloc(sizeof(*iface->data) * j, M_TEMP, M_WAITOK); 4181ae19af49SJung-uk Kim iface->num = j; 4182ae19af49SJung-uk Kim i = j = 0; 4183ae19af49SJung-uk Kim while (i < len) 4184ae19af49SJung-uk Kim if (isspace(p[i]) || p[i] == '\0') 4185ae19af49SJung-uk Kim i++; 4186ae19af49SJung-uk Kim else { 4187ae19af49SJung-uk Kim iface->data[j] = p + i; 4188ae19af49SJung-uk Kim i += strlen(p + i) + 1; 4189ae19af49SJung-uk Kim j++; 4190ae19af49SJung-uk Kim } 4191ae19af49SJung-uk Kim 4192ae19af49SJung-uk Kim return (j); 4193ae19af49SJung-uk Kim } 4194ae19af49SJung-uk Kim 4195ae19af49SJung-uk Kim static void 4196ae19af49SJung-uk Kim acpi_free_interfaces(struct acpi_interface *iface) 4197ae19af49SJung-uk Kim { 4198ae19af49SJung-uk Kim 4199ae19af49SJung-uk Kim free(iface->data[0], M_TEMP); 4200ae19af49SJung-uk Kim free(iface->data, M_TEMP); 4201ae19af49SJung-uk Kim } 4202ae19af49SJung-uk Kim 4203ae19af49SJung-uk Kim static void 4204ae19af49SJung-uk Kim acpi_reset_interfaces(device_t dev) 4205ae19af49SJung-uk Kim { 4206ae19af49SJung-uk Kim struct acpi_interface list; 4207ae19af49SJung-uk Kim ACPI_STATUS status; 4208ae19af49SJung-uk Kim int i; 4209ae19af49SJung-uk Kim 4210ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_install_interface, &list) > 0) { 4211ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 4212ae19af49SJung-uk Kim status = AcpiInstallInterface(list.data[i]); 4213ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 4214ae19af49SJung-uk Kim device_printf(dev, 4215ae19af49SJung-uk Kim "failed to install _OSI(\"%s\"): %s\n", 4216ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 4217ae19af49SJung-uk Kim else if (bootverbose) 4218ae19af49SJung-uk Kim device_printf(dev, "installed _OSI(\"%s\")\n", 4219ae19af49SJung-uk Kim list.data[i]); 4220ae19af49SJung-uk Kim } 4221ae19af49SJung-uk Kim acpi_free_interfaces(&list); 4222ae19af49SJung-uk Kim } 4223ae19af49SJung-uk Kim if (acpi_parse_interfaces(acpi_remove_interface, &list) > 0) { 4224ae19af49SJung-uk Kim for (i = 0; i < list.num; i++) { 4225ae19af49SJung-uk Kim status = AcpiRemoveInterface(list.data[i]); 4226ae19af49SJung-uk Kim if (ACPI_FAILURE(status)) 4227ae19af49SJung-uk Kim device_printf(dev, 4228ae19af49SJung-uk Kim "failed to remove _OSI(\"%s\"): %s\n", 4229ae19af49SJung-uk Kim list.data[i], AcpiFormatException(status)); 4230ae19af49SJung-uk Kim else if (bootverbose) 4231ae19af49SJung-uk Kim device_printf(dev, "removed _OSI(\"%s\")\n", 4232ae19af49SJung-uk Kim list.data[i]); 4233ae19af49SJung-uk Kim } 4234ae19af49SJung-uk Kim acpi_free_interfaces(&list); 4235ae19af49SJung-uk Kim } 4236ae19af49SJung-uk Kim } 4237ae19af49SJung-uk Kim 4238ae19af49SJung-uk Kim static int 4239f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 4240f9390180SMitsuru IWASAKI { 4241f9390180SMitsuru IWASAKI int state, acpi_state; 4242f9390180SMitsuru IWASAKI int error; 4243f9390180SMitsuru IWASAKI struct acpi_softc *sc; 4244f9390180SMitsuru IWASAKI va_list ap; 4245f9390180SMitsuru IWASAKI 4246f9390180SMitsuru IWASAKI error = 0; 4247f9390180SMitsuru IWASAKI switch (cmd) { 4248f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 4249f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 4250f9390180SMitsuru IWASAKI if (sc == NULL) { 4251f9390180SMitsuru IWASAKI error = EINVAL; 4252f9390180SMitsuru IWASAKI goto out; 4253f9390180SMitsuru IWASAKI } 4254f9390180SMitsuru IWASAKI 4255f9390180SMitsuru IWASAKI va_start(ap, arg); 4256f9390180SMitsuru IWASAKI state = va_arg(ap, int); 4257f9390180SMitsuru IWASAKI va_end(ap); 4258f9390180SMitsuru IWASAKI 4259f9390180SMitsuru IWASAKI switch (state) { 4260f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 4261f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 4262f9390180SMitsuru IWASAKI break; 4263f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 4264f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 4265f9390180SMitsuru IWASAKI break; 4266f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 4267f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 4268f9390180SMitsuru IWASAKI break; 4269f9390180SMitsuru IWASAKI default: 4270f9390180SMitsuru IWASAKI error = EINVAL; 4271f9390180SMitsuru IWASAKI goto out; 4272f9390180SMitsuru IWASAKI } 4273f9390180SMitsuru IWASAKI 427400a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 427500a30448SNate Lawson error = ENXIO; 4276f9390180SMitsuru IWASAKI break; 4277f9390180SMitsuru IWASAKI default: 4278f9390180SMitsuru IWASAKI error = EINVAL; 4279f9390180SMitsuru IWASAKI goto out; 4280f9390180SMitsuru IWASAKI } 4281f9390180SMitsuru IWASAKI 4282f9390180SMitsuru IWASAKI out: 4283f9390180SMitsuru IWASAKI return (error); 4284f9390180SMitsuru IWASAKI } 4285f9390180SMitsuru IWASAKI 4286f9390180SMitsuru IWASAKI static void 4287f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 4288f9390180SMitsuru IWASAKI { 4289be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 42906c407052SMitsuru IWASAKI return; 4291f9390180SMitsuru IWASAKI 4292f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 4293f9390180SMitsuru IWASAKI } 4294f9390180SMitsuru IWASAKI 4295891cf3edSEd Maste SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, NULL); 4296